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