Logging: Use CLOG for blenkernel
[blender.git] / source / blender / blenkernel / intern / icons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/blenkernel/intern/icons.c
30  *  \ingroup bke
31  */
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "CLG_log.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_brush_types.h"
43 #include "DNA_collection_types.h"
44 #include "DNA_gpencil_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_texture_types.h"
51 #include "DNA_world_types.h"
52
53 #include "BLI_utildefines.h"
54 #include "BLI_ghash.h"
55 #include "BLI_linklist_lockfree.h"
56 #include "BLI_string.h"
57 #include "BLI_fileops.h"
58 #include "BLI_threads.h"
59
60 #include "BKE_icons.h"
61 #include "BKE_global.h" /* only for G.background test */
62 #include "BKE_studiolight.h"
63
64 #include "BLI_sys_types.h" // for intptr_t support
65
66 #include "GPU_texture.h"
67
68 #include "IMB_imbuf.h"
69 #include "IMB_imbuf_types.h"
70 #include "IMB_thumbs.h"
71
72 /**
73  * Only allow non-managed icons to be removed (by Python for eg).
74  * Previews & ID's have their own functions to remove icons.
75  */
76 enum {
77         ICON_FLAG_MANAGED = (1 << 0),
78 };
79
80 /* GLOBALS */
81
82 static CLG_LogRef LOG = {"bke.icons"};
83
84 static GHash *gIcons = NULL;
85
86 static int gNextIconId = 1;
87
88 static int gFirstIconId = 1;
89
90 static GHash *gCachedPreviews = NULL;
91
92 /* Queue of icons for deferred deletion. */
93 typedef struct DeferredIconDeleteNode {
94         struct DeferredIconDeleteNode *next;
95         int icon_id;
96 } DeferredIconDeleteNode;
97 static LockfreeLinkList g_icon_delete_queue;
98
99 static void icon_free(void *val)
100 {
101         Icon *icon = val;
102
103         if (icon) {
104                 if (icon->obj_type == ICON_DATA_GEOM) {
105                         struct Icon_Geom *obj = icon->obj;
106                         if (obj->mem) {
107                                 /* coords & colors are part of this memory. */
108                                 MEM_freeN((void *)obj->mem);
109                         }
110                         else {
111                                 MEM_freeN((void *)obj->coords);
112                                 MEM_freeN((void *)obj->colors);
113                         }
114                         MEM_freeN(icon->obj);
115                 }
116
117                 if (icon->drawinfo_free) {
118                         icon->drawinfo_free(icon->drawinfo);
119                 }
120                 else if (icon->drawinfo) {
121                         MEM_freeN(icon->drawinfo);
122                 }
123                 MEM_freeN(icon);
124         }
125 }
126
127 static void icon_free_data(int icon_id, Icon *icon)
128 {
129         if (icon->obj_type == ICON_DATA_ID) {
130                 ((ID *)(icon->obj))->icon_id = 0;
131         }
132         else if (icon->obj_type == ICON_DATA_PREVIEW) {
133                 ((PreviewImage *)(icon->obj))->icon_id = 0;
134         }
135         else if (icon->obj_type == ICON_DATA_GPLAYER) {
136                 ((bGPDlayer *)(icon->obj))->runtime.icon_id = 0;
137         }
138         else if (icon->obj_type == ICON_DATA_GEOM) {
139                 ((struct Icon_Geom *)(icon->obj))->icon_id = 0;
140         }
141         else if (icon->obj_type == ICON_DATA_STUDIOLIGHT) {
142                 StudioLight *sl = icon->obj;
143                 if (sl != NULL) {
144                         BKE_studiolight_unset_icon_id(sl, icon_id);
145                 }
146         }
147         else {
148                 BLI_assert(0);
149         }
150 }
151
152 /* create an id for a new icon and make sure that ids from deleted icons get reused
153  * after the integer number range is used up */
154 static int get_next_free_id(void)
155 {
156         BLI_assert(BLI_thread_is_main());
157         int startId = gFirstIconId;
158
159         /* if we haven't used up the int number range, we just return the next int */
160         if (gNextIconId >= gFirstIconId)
161                 return gNextIconId++;
162
163         /* now we try to find the smallest icon id not stored in the gIcons hash */
164         while (BLI_ghash_lookup(gIcons, POINTER_FROM_INT(startId)) && startId >= gFirstIconId)
165                 startId++;
166
167         /* if we found a suitable one that isn't used yet, return it */
168         if (startId >= gFirstIconId)
169                 return startId;
170
171         /* fail */
172         return 0;
173 }
174
175 void BKE_icons_init(int first_dyn_id)
176 {
177         BLI_assert(BLI_thread_is_main());
178
179         gNextIconId = first_dyn_id;
180         gFirstIconId = first_dyn_id;
181
182         if (!gIcons) {
183                 gIcons = BLI_ghash_int_new(__func__);
184                 BLI_linklist_lockfree_init(&g_icon_delete_queue);
185         }
186
187         if (!gCachedPreviews) {
188                 gCachedPreviews = BLI_ghash_str_new(__func__);
189         }
190 }
191
192 void BKE_icons_free(void)
193 {
194         BLI_assert(BLI_thread_is_main());
195
196         if (gIcons) {
197                 BLI_ghash_free(gIcons, NULL, icon_free);
198                 gIcons = NULL;
199         }
200
201         if (gCachedPreviews) {
202                 BLI_ghash_free(gCachedPreviews, MEM_freeN, BKE_previewimg_freefunc);
203                 gCachedPreviews = NULL;
204         }
205
206         BLI_linklist_lockfree_free(&g_icon_delete_queue, MEM_freeN);
207 }
208
209 void BKE_icons_deferred_free(void)
210 {
211         BLI_assert(BLI_thread_is_main());
212
213         for (DeferredIconDeleteNode *node =
214                      (DeferredIconDeleteNode *)BLI_linklist_lockfree_begin(&g_icon_delete_queue);
215              node != NULL;
216              node = node->next)
217         {
218                 BLI_ghash_remove(gIcons, POINTER_FROM_INT(node->icon_id), NULL, icon_free);
219         }
220         BLI_linklist_lockfree_clear(&g_icon_delete_queue, MEM_freeN);
221 }
222
223 static PreviewImage *previewimg_create_ex(size_t deferred_data_size)
224 {
225         PreviewImage *prv_img = NULL;
226         int i;
227
228         prv_img = MEM_mallocN(sizeof(PreviewImage) + deferred_data_size, "img_prv");
229         memset(prv_img, 0, sizeof(*prv_img));  /* leave deferred data dirty */
230
231         if (deferred_data_size) {
232                 prv_img->tag |= PRV_TAG_DEFFERED;
233         }
234
235         for (i = 0; i < NUM_ICON_SIZES; ++i) {
236                 prv_img->flag[i] |= PRV_CHANGED;
237                 prv_img->changed_timestamp[i] = 0;
238         }
239         return prv_img;
240 }
241
242 PreviewImage *BKE_previewimg_create(void)
243 {
244         return previewimg_create_ex(0);
245 }
246
247 void BKE_previewimg_freefunc(void *link)
248 {
249         PreviewImage *prv = (PreviewImage *)link;
250         if (prv) {
251                 int i;
252
253                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
254                         if (prv->rect[i]) {
255                                 MEM_freeN(prv->rect[i]);
256                         }
257                         if (prv->gputexture[i])
258                                 GPU_texture_free(prv->gputexture[i]);
259                 }
260
261                 MEM_freeN(prv);
262         }
263 }
264
265 void BKE_previewimg_free(PreviewImage **prv)
266 {
267         if (prv && (*prv)) {
268                 BKE_previewimg_freefunc(*prv);
269                 *prv = NULL;
270         }
271 }
272
273 void BKE_previewimg_clear_single(struct PreviewImage *prv, enum eIconSizes size)
274 {
275         MEM_SAFE_FREE(prv->rect[size]);
276         if (prv->gputexture[size]) {
277                 GPU_texture_free(prv->gputexture[size]);
278         }
279         prv->h[size] = prv->w[size] = 0;
280         prv->flag[size] |= PRV_CHANGED;
281         prv->flag[size] &= ~PRV_USER_EDITED;
282         prv->changed_timestamp[size] = 0;
283 }
284
285 void BKE_previewimg_clear(struct PreviewImage *prv)
286 {
287         int i;
288         for (i = 0; i < NUM_ICON_SIZES; ++i) {
289                 BKE_previewimg_clear_single(prv, i);
290         }
291 }
292
293 PreviewImage *BKE_previewimg_copy(const PreviewImage *prv)
294 {
295         PreviewImage *prv_img = NULL;
296         int i;
297
298         if (prv) {
299                 prv_img = MEM_dupallocN(prv);
300                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
301                         if (prv->rect[i]) {
302                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
303                         }
304                         prv_img->gputexture[i] = NULL;
305                 }
306         }
307         return prv_img;
308 }
309
310 /** Duplicate preview image from \a id and clear icon_id, to be used by datablock copy functions. */
311 void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
312 {
313         PreviewImage **old_prv_p = BKE_previewimg_id_get_p(old_id);
314         PreviewImage **new_prv_p = BKE_previewimg_id_get_p(new_id);
315
316         if (old_prv_p && *old_prv_p) {
317                 BLI_assert(new_prv_p != NULL && ELEM(*new_prv_p, NULL, *old_prv_p));
318 //              const int new_icon_id = get_next_free_id();
319
320 //              if (new_icon_id == 0) {
321 //                      return;  /* Failure. */
322 //              }
323                 *new_prv_p = BKE_previewimg_copy(*old_prv_p);
324                 new_id->icon_id = (*new_prv_p)->icon_id = 0;
325         }
326 }
327
328 PreviewImage **BKE_previewimg_id_get_p(const ID *id)
329 {
330         switch (GS(id->name)) {
331 #define ID_PRV_CASE(id_code, id_struct) case id_code: { return &((id_struct *)id)->preview; } ((void)0)
332                 ID_PRV_CASE(ID_MA, Material);
333                 ID_PRV_CASE(ID_TE, Tex);
334                 ID_PRV_CASE(ID_WO, World);
335                 ID_PRV_CASE(ID_LA, Lamp);
336                 ID_PRV_CASE(ID_IM, Image);
337                 ID_PRV_CASE(ID_BR, Brush);
338                 ID_PRV_CASE(ID_OB, Object);
339                 ID_PRV_CASE(ID_GR, Collection);
340                 ID_PRV_CASE(ID_SCE, Scene);
341                 ID_PRV_CASE(ID_SCR, bScreen);
342 #undef ID_PRV_CASE
343                 default:
344                         break;
345         }
346
347         return NULL;
348 }
349
350 void BKE_previewimg_id_free(ID *id)
351 {
352         PreviewImage **prv_p = BKE_previewimg_id_get_p(id);
353         if (prv_p) {
354                 BKE_previewimg_free(prv_p);
355         }
356 }
357
358 PreviewImage *BKE_previewimg_id_ensure(ID *id)
359 {
360         PreviewImage **prv_p = BKE_previewimg_id_get_p(id);
361
362         if (prv_p) {
363                 if (*prv_p == NULL) {
364                         *prv_p = BKE_previewimg_create();
365                 }
366                 return *prv_p;
367         }
368
369         return NULL;
370 }
371
372 PreviewImage *BKE_previewimg_cached_get(const char *name)
373 {
374         return BLI_ghash_lookup(gCachedPreviews, name);
375 }
376
377 /**
378  * Generate an empty PreviewImage, if not yet existing.
379  */
380 PreviewImage *BKE_previewimg_cached_ensure(const char *name)
381 {
382         PreviewImage *prv = NULL;
383         void **key_p, **prv_p;
384
385         if (!BLI_ghash_ensure_p_ex(gCachedPreviews, name, &key_p, &prv_p)) {
386                 *key_p = BLI_strdup(name);
387                 *prv_p = BKE_previewimg_create();
388         }
389         prv = *prv_p;
390         BLI_assert(prv);
391
392         return prv;
393 }
394
395 /**
396  * Generate a PreviewImage from given file path, using thumbnails management, if not yet existing.
397  */
398 PreviewImage *BKE_previewimg_cached_thumbnail_read(
399         const char *name, const char *path, const int source, bool force_update)
400 {
401         PreviewImage *prv = NULL;
402         void **prv_p;
403
404         prv_p = BLI_ghash_lookup_p(gCachedPreviews, name);
405
406         if (prv_p) {
407                 prv = *prv_p;
408                 BLI_assert(prv);
409         }
410
411         if (prv && force_update) {
412                 const char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
413                 if (((int)prv_deferred_data[0] == source) && STREQ(&prv_deferred_data[1], path)) {
414                         /* If same path, no need to re-allocate preview, just clear it up. */
415                         BKE_previewimg_clear(prv);
416                 }
417                 else {
418                         BKE_previewimg_free(&prv);
419                 }
420         }
421
422         if (!prv) {
423                 /* We pack needed data for lazy loading (source type, in a single char, and path). */
424                 const size_t deferred_data_size = strlen(path) + 2;
425                 char *deferred_data;
426
427                 prv = previewimg_create_ex(deferred_data_size);
428                 deferred_data = PRV_DEFERRED_DATA(prv);
429                 deferred_data[0] = source;
430                 memcpy(&deferred_data[1], path, deferred_data_size - 1);
431
432                 force_update = true;
433         }
434
435         if (force_update) {
436                 if (prv_p) {
437                         *prv_p = prv;
438                 }
439                 else {
440                         BLI_ghash_insert(gCachedPreviews, BLI_strdup(name), prv);
441                 }
442         }
443
444         return prv;
445 }
446
447 void BKE_previewimg_cached_release_pointer(PreviewImage *prv)
448 {
449         if (prv) {
450                 if (prv->tag & PRV_TAG_DEFFERED_RENDERING) {
451                         /* We cannot delete the preview while it is being loaded in another thread... */
452                         prv->tag |= PRV_TAG_DEFFERED_DELETE;
453                         return;
454                 }
455                 if (prv->icon_id) {
456                         BKE_icon_delete(prv->icon_id);
457                 }
458                 BKE_previewimg_freefunc(prv);
459         }
460 }
461
462 void BKE_previewimg_cached_release(const char *name)
463 {
464         PreviewImage *prv = BLI_ghash_popkey(gCachedPreviews, name, MEM_freeN);
465
466         BKE_previewimg_cached_release_pointer(prv);
467 }
468
469 /** Handle deferred (lazy) loading/generation of preview image, if needed.
470  * For now, only used with file thumbnails. */
471 void BKE_previewimg_ensure(PreviewImage *prv, const int size)
472 {
473         if ((prv->tag & PRV_TAG_DEFFERED) != 0) {
474                 const bool do_icon = ((size == ICON_SIZE_ICON) && !prv->rect[ICON_SIZE_ICON]);
475                 const bool do_preview = ((size == ICON_SIZE_PREVIEW) && !prv->rect[ICON_SIZE_PREVIEW]);
476
477                 if (do_icon || do_preview) {
478                         ImBuf *thumb;
479                         char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
480                         int source =  prv_deferred_data[0];
481                         char *path = &prv_deferred_data[1];
482                         int icon_w, icon_h;
483
484                         thumb = IMB_thumb_manage(path, THB_LARGE, source);
485
486                         if (thumb) {
487                                 /* PreviewImage assumes premultiplied alhpa... */
488                                 IMB_premultiply_alpha(thumb);
489
490                                 if (do_preview) {
491                                         prv->w[ICON_SIZE_PREVIEW] = thumb->x;
492                                         prv->h[ICON_SIZE_PREVIEW] = thumb->y;
493                                         prv->rect[ICON_SIZE_PREVIEW] = MEM_dupallocN(thumb->rect);
494                                         prv->flag[ICON_SIZE_PREVIEW] &= ~(PRV_CHANGED | PRV_USER_EDITED);
495                                 }
496                                 if (do_icon) {
497                                         if (thumb->x > thumb->y) {
498                                                 icon_w = ICON_RENDER_DEFAULT_HEIGHT;
499                                                 icon_h = (thumb->y * icon_w) / thumb->x + 1;
500                                         }
501                                         else if (thumb->x < thumb->y) {
502                                                 icon_h = ICON_RENDER_DEFAULT_HEIGHT;
503                                                 icon_w = (thumb->x * icon_h) / thumb->y + 1;
504                                         }
505                                         else {
506                                                 icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
507                                         }
508
509                                         IMB_scaleImBuf(thumb, icon_w, icon_h);
510                                         prv->w[ICON_SIZE_ICON] = icon_w;
511                                         prv->h[ICON_SIZE_ICON] = icon_h;
512                                         prv->rect[ICON_SIZE_ICON] = MEM_dupallocN(thumb->rect);
513                                         prv->flag[ICON_SIZE_ICON] &= ~(PRV_CHANGED | PRV_USER_EDITED);
514                                 }
515                                 IMB_freeImBuf(thumb);
516                         }
517                 }
518         }
519 }
520
521 void BKE_icon_changed(const int icon_id)
522 {
523         BLI_assert(BLI_thread_is_main());
524
525         Icon *icon = NULL;
526
527         if (!icon_id || G.background) return;
528
529         icon = BLI_ghash_lookup(gIcons, POINTER_FROM_INT(icon_id));
530
531         if (icon) {
532                 /* We *only* expect ID-tied icons here, not non-ID icon/preview! */
533                 BLI_assert(icon->id_type != 0);
534                 BLI_assert(icon->obj_type == ICON_DATA_ID);
535
536                 /* Do not enforce creation of previews for valid ID types using BKE_previewimg_id_ensure() here ,
537                  * we only want to ensure *existing* preview images are properly tagged as changed/invalid, that's all. */
538                 PreviewImage **p_prv = BKE_previewimg_id_get_p((ID *)icon->obj);
539
540                 /* If we have previews, they all are now invalid changed. */
541                 if (p_prv && *p_prv) {
542                         int i;
543                         for (i = 0; i < NUM_ICON_SIZES; ++i) {
544                                 (*p_prv)->flag[i] |= PRV_CHANGED;
545                                 (*p_prv)->changed_timestamp[i]++;
546                         }
547                 }
548         }
549 }
550
551 static Icon *icon_create(int icon_id, int obj_type, void *obj)
552 {
553         Icon *new_icon = MEM_mallocN(sizeof(Icon), __func__);
554
555         new_icon->obj_type = obj_type;
556         new_icon->obj = obj;
557         new_icon->id_type = 0;
558         new_icon->flag = 0;
559
560         /* next two lines make sure image gets created */
561         new_icon->drawinfo = NULL;
562         new_icon->drawinfo_free = NULL;
563
564         BLI_ghash_insert(gIcons, POINTER_FROM_INT(icon_id), new_icon);
565
566         return new_icon;
567 }
568
569 static int icon_id_ensure_create_icon(struct ID *id)
570 {
571         BLI_assert(BLI_thread_is_main());
572
573         Icon *icon = icon_create(id->icon_id, ICON_DATA_ID, id);
574         icon->id_type = GS(id->name);
575         icon->flag = ICON_FLAG_MANAGED;
576
577         return id->icon_id;
578 }
579
580 int BKE_icon_id_ensure(struct ID *id)
581 {
582         /* Never handle icons in non-main thread! */
583         BLI_assert(BLI_thread_is_main());
584
585         if (!id || G.background) {
586                 return 0;
587         }
588
589         if (id->icon_id)
590                 return id->icon_id;
591
592         id->icon_id = get_next_free_id();
593
594         if (!id->icon_id) {
595                 CLOG_ERROR(&LOG, "not enough IDs");
596                 return 0;
597         }
598
599         /* Ensure we synchronize ID icon_id with its previewimage if it has one. */
600         PreviewImage **p_prv = BKE_previewimg_id_get_p(id);
601         if (p_prv && *p_prv) {
602                 BLI_assert(ELEM((*p_prv)->icon_id, 0, id->icon_id));
603                 (*p_prv)->icon_id = id->icon_id;
604         }
605
606         return icon_id_ensure_create_icon(id);
607 }
608
609
610 static int icon_gplayer_color_ensure_create_icon(bGPDlayer *gpl)
611 {
612         BLI_assert(BLI_thread_is_main());
613
614         /* NOTE: The color previews for GP Layers don't really need
615          * to be "rendered" to image per se (as it will just be a plain
616          * colored rectangle), we need to define icon data here so that
617          * we can store a pointer to the layer data in icon->obj.
618          */
619         Icon *icon = icon_create(gpl->runtime.icon_id, ICON_DATA_GPLAYER, gpl);
620         icon->flag = ICON_FLAG_MANAGED;
621
622         return gpl->runtime.icon_id;
623 }
624
625 int BKE_icon_gplayer_color_ensure(bGPDlayer *gpl)
626 {
627         /* Never handle icons in non-main thread! */
628         BLI_assert(BLI_thread_is_main());
629
630         if (!gpl || G.background) {
631                 return 0;
632         }
633
634         if (gpl->runtime.icon_id)
635                 return gpl->runtime.icon_id;
636
637         gpl->runtime.icon_id = get_next_free_id();
638
639         if (!gpl->runtime.icon_id) {
640                 CLOG_ERROR(&LOG, "not enough IDs");
641                 return 0;
642         }
643
644         return icon_gplayer_color_ensure_create_icon(gpl);
645 }
646
647 /**
648  * Return icon id of given preview, or create new icon if not found.
649  */
650 int BKE_icon_preview_ensure(ID *id, PreviewImage *preview)
651 {
652         if (!preview || G.background)
653                 return 0;
654
655         if (id) {
656                 BLI_assert(BKE_previewimg_id_ensure(id) == preview);
657         }
658
659         if (preview->icon_id) {
660                 BLI_assert(!id || !id->icon_id || id->icon_id == preview->icon_id);
661                 return preview->icon_id;
662         }
663
664         if (id && id->icon_id) {
665                 preview->icon_id = id->icon_id;
666                 return preview->icon_id;
667         }
668
669         preview->icon_id = get_next_free_id();
670
671         if (!preview->icon_id) {
672                 CLOG_ERROR(&LOG, "not enough IDs");
673                 return 0;
674         }
675
676         /* Ensure we synchronize ID icon_id with its previewimage if available, and generate suitable 'ID' icon. */
677         if (id) {
678                 id->icon_id = preview->icon_id;
679                 return icon_id_ensure_create_icon(id);
680         }
681
682         Icon *icon = icon_create(preview->icon_id, ICON_DATA_PREVIEW, preview);
683         icon->flag = ICON_FLAG_MANAGED;
684
685         return preview->icon_id;
686 }
687
688 Icon *BKE_icon_get(const int icon_id)
689 {
690         BLI_assert(BLI_thread_is_main());
691
692         Icon *icon = NULL;
693
694         icon = BLI_ghash_lookup(gIcons, POINTER_FROM_INT(icon_id));
695
696         if (!icon) {
697                 CLOG_ERROR(&LOG, "no icon for icon ID: %d", icon_id);
698                 return NULL;
699         }
700
701         return icon;
702 }
703
704 void BKE_icon_set(const int icon_id, struct Icon *icon)
705 {
706         BLI_assert(BLI_thread_is_main());
707
708         void **val_p;
709
710         if (BLI_ghash_ensure_p(gIcons, POINTER_FROM_INT(icon_id), &val_p)) {
711                 CLOG_ERROR(&LOG, "icon already set: %d", icon_id);
712                 return;
713         }
714
715         *val_p = icon;
716 }
717
718 static void icon_add_to_deferred_delete_queue(int icon_id)
719 {
720         DeferredIconDeleteNode *node =
721                 MEM_mallocN(sizeof(DeferredIconDeleteNode), __func__);
722         node->icon_id = icon_id;
723         BLI_linklist_lockfree_insert(&g_icon_delete_queue,
724                                      (LockfreeLinkNode *)node);
725 }
726
727 void BKE_icon_id_delete(struct ID *id)
728 {
729         const int icon_id = id->icon_id;
730         if (!icon_id) return;  /* no icon defined for library object */
731         id->icon_id = 0;
732
733         if (!BLI_thread_is_main()) {
734                 icon_add_to_deferred_delete_queue(icon_id);
735                 return;
736         }
737
738         BKE_icons_deferred_free();
739         BLI_ghash_remove(gIcons, POINTER_FROM_INT(icon_id), NULL, icon_free);
740 }
741
742 /**
743  * Remove icon and free data.
744  */
745 bool BKE_icon_delete(const int icon_id)
746 {
747         if (icon_id == 0) {
748                 /* no icon defined for library object */
749                 return false;
750         }
751
752         Icon *icon = BLI_ghash_popkey(gIcons, POINTER_FROM_INT(icon_id), NULL);
753         if (icon) {
754                 icon_free_data(icon_id, icon);
755                 icon_free(icon);
756                 return true;
757         }
758         else {
759                 return false;
760         }
761 }
762
763 bool BKE_icon_delete_unmanaged(const int icon_id)
764 {
765         if (icon_id == 0) {
766                 /* no icon defined for library object */
767                 return false;
768         }
769
770         Icon *icon = BLI_ghash_popkey(gIcons, POINTER_FROM_INT(icon_id), NULL);
771         if (icon) {
772                 if (UNLIKELY(icon->flag & ICON_FLAG_MANAGED)) {
773                         BLI_ghash_insert(gIcons, POINTER_FROM_INT(icon_id), icon);
774                         return false;
775                 }
776                 else {
777                         icon_free_data(icon_id, icon);
778                         icon_free(icon);
779                         return true;
780                 }
781         }
782         else {
783                 return false;
784         }
785 }
786
787 /* -------------------------------------------------------------------- */
788 /** \name Geometry Icon
789  * \{ */
790
791 int BKE_icon_geom_ensure(struct Icon_Geom *geom)
792 {
793         BLI_assert(BLI_thread_is_main());
794
795         if (geom->icon_id) {
796                 return geom->icon_id;
797         }
798
799         geom->icon_id = get_next_free_id();
800
801         icon_create(geom->icon_id, ICON_DATA_GEOM, geom);
802         /* Not managed for now, we may want this to be configurable per icon). */
803
804         return geom->icon_id;
805 }
806
807 struct Icon_Geom *BKE_icon_geom_from_memory(const uchar *data, size_t data_len)
808 {
809         BLI_assert(BLI_thread_is_main());
810         if (data_len <= 8) {
811                 goto fail;
812         }
813         /* Skip the header. */
814         data_len -= 8;
815         const int div = 3 * 2 * 3;
816         const int coords_len = data_len / div;
817         if (coords_len * div != data_len) {
818                 goto fail;
819         }
820
821         const uchar header[4] = {'V', 'C', 'O', 0};
822         const uchar *p = data;
823         if (memcmp(p, header, ARRAY_SIZE(header)) != 0) {
824                 goto fail;
825         }
826         p += 4;
827
828         struct Icon_Geom *geom = MEM_mallocN(sizeof(*geom), __func__);
829         geom->coords_range[0] = (int)*p++;
830         geom->coords_range[1] = (int)*p++;
831         /* x, y ignored for now */
832         p += 2;
833
834         geom->coords_len = coords_len;
835         geom->coords = (const void *)p;
836         geom->colors = (const void *)(p + (data_len / 3));
837         geom->icon_id = 0;
838         geom->mem = data;
839         return geom;
840
841 fail:
842         MEM_freeN((void *)data);
843         return NULL;
844 }
845
846 struct Icon_Geom *BKE_icon_geom_from_file(const char *filename)
847 {
848         BLI_assert(BLI_thread_is_main());
849         size_t data_len;
850         uchar *data = BLI_file_read_binary_as_mem(filename, 0, &data_len);
851         if (data == NULL) {
852                 return NULL;
853         }
854         return BKE_icon_geom_from_memory(data, data_len);
855 }
856
857 /** \} */
858
859 /** \name Studio Light Icon
860  * \{ */
861 int BKE_icon_ensure_studio_light(struct StudioLight *sl, int id_type)
862 {
863         int icon_id = get_next_free_id();
864         Icon *icon = icon_create(icon_id, ICON_DATA_STUDIOLIGHT, sl);
865         icon->id_type = id_type;
866         return icon_id;
867 }
868 /** \} */