Fix T54310: Assert when enable Brush custom icon.
[blender-staging.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_group_types.h"
41 #include "DNA_lamp_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_texture_types.h"
46 #include "DNA_world_types.h"
47 #include "DNA_brush_types.h"
48
49 #include "BLI_utildefines.h"
50 #include "BLI_ghash.h"
51 #include "BLI_string.h"
52
53 #include "BKE_icons.h"
54 #include "BKE_global.h" /* only for G.background test */
55
56 #include "BLI_sys_types.h" // for intptr_t support
57
58 #include "GPU_texture.h"
59
60 #include "IMB_imbuf.h"
61 #include "IMB_imbuf_types.h"
62 #include "IMB_thumbs.h"
63
64 /* GLOBALS */
65
66 static GHash *gIcons = NULL;
67
68 static int gNextIconId = 1;
69
70 static int gFirstIconId = 1;
71
72 static GHash *gCachedPreviews = NULL;
73
74 static void icon_free(void *val)
75 {
76         Icon *icon = val;
77
78         if (icon) {
79                 if (icon->drawinfo_free) {
80                         icon->drawinfo_free(icon->drawinfo);
81                 }
82                 else if (icon->drawinfo) {
83                         MEM_freeN(icon->drawinfo);
84                 }
85                 MEM_freeN(icon);
86         }
87 }
88
89 /* create an id for a new icon and make sure that ids from deleted icons get reused
90  * after the integer number range is used up */
91 static int get_next_free_id(void)
92 {
93         int startId = gFirstIconId;
94
95         /* if we haven't used up the int number range, we just return the next int */
96         if (gNextIconId >= gFirstIconId)
97                 return gNextIconId++;
98         
99         /* now we try to find the smallest icon id not stored in the gIcons hash */
100         while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId >= gFirstIconId)
101                 startId++;
102
103         /* if we found a suitable one that isn't used yet, return it */
104         if (startId >= gFirstIconId)
105                 return startId;
106
107         /* fail */
108         return 0;
109 }
110
111 void BKE_icons_init(int first_dyn_id)
112 {
113         gNextIconId = first_dyn_id;
114         gFirstIconId = first_dyn_id;
115
116         if (!gIcons)
117                 gIcons = BLI_ghash_int_new(__func__);
118
119         if (!gCachedPreviews) {
120                 gCachedPreviews = BLI_ghash_str_new(__func__);
121         }
122 }
123
124 void BKE_icons_free(void)
125 {
126         if (gIcons) {
127                 BLI_ghash_free(gIcons, NULL, icon_free);
128                 gIcons = NULL;
129         }
130
131         if (gCachedPreviews) {
132                 BLI_ghash_free(gCachedPreviews, MEM_freeN, BKE_previewimg_freefunc);
133                 gCachedPreviews = NULL;
134         }
135 }
136
137 static PreviewImage *previewimg_create_ex(size_t deferred_data_size)
138 {
139         PreviewImage *prv_img = NULL;
140         int i;
141
142         prv_img = MEM_mallocN(sizeof(PreviewImage) + deferred_data_size, "img_prv");
143         memset(prv_img, 0, sizeof(*prv_img));  /* leave deferred data dirty */
144
145         if (deferred_data_size) {
146                 prv_img->tag |= PRV_TAG_DEFFERED;
147         }
148
149         for (i = 0; i < NUM_ICON_SIZES; ++i) {
150                 prv_img->flag[i] |= PRV_CHANGED;
151                 prv_img->changed_timestamp[i] = 0;
152         }
153         return prv_img;
154 }
155
156 PreviewImage *BKE_previewimg_create(void)
157 {
158         return previewimg_create_ex(0);
159 }
160
161 void BKE_previewimg_freefunc(void *link)
162 {
163         PreviewImage *prv = (PreviewImage *)link;
164         if (prv) {
165                 int i;
166
167                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
168                         if (prv->rect[i]) {
169                                 MEM_freeN(prv->rect[i]);
170                         }
171                         if (prv->gputexture[i])
172                                 GPU_texture_free(prv->gputexture[i]);
173                 }
174                 
175                 MEM_freeN(prv);
176         }
177 }
178
179 void BKE_previewimg_free(PreviewImage **prv)
180 {
181         if (prv && (*prv)) {
182                 BKE_previewimg_freefunc(*prv);
183                 *prv = NULL;
184         }
185 }
186
187 void BKE_previewimg_clear_single(struct PreviewImage *prv, enum eIconSizes size)
188 {
189         MEM_SAFE_FREE(prv->rect[size]);
190         if (prv->gputexture[size]) {
191                 GPU_texture_free(prv->gputexture[size]);
192         }
193         prv->h[size] = prv->w[size] = 0;
194         prv->flag[size] |= PRV_CHANGED;
195         prv->flag[size] &= ~PRV_USER_EDITED;
196         prv->changed_timestamp[size] = 0;
197 }
198
199 void BKE_previewimg_clear(struct PreviewImage *prv)
200 {
201         int i;
202         for (i = 0; i < NUM_ICON_SIZES; ++i) {
203                 BKE_previewimg_clear_single(prv, i);
204         }
205 }
206
207 PreviewImage *BKE_previewimg_copy(const PreviewImage *prv)
208 {
209         PreviewImage *prv_img = NULL;
210         int i;
211
212         if (prv) {
213                 prv_img = MEM_dupallocN(prv);
214                 for (i = 0; i < NUM_ICON_SIZES; ++i) {
215                         if (prv->rect[i]) {
216                                 prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
217                         }
218                         prv_img->gputexture[i] = NULL;
219                 }
220         }
221         return prv_img;
222 }
223
224 /** Duplicate preview image from \a id and clear icon_id, to be used by datablock copy functions. */
225 void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
226 {
227         PreviewImage **old_prv_p = BKE_previewimg_id_get_p(old_id);
228         PreviewImage **new_prv_p = BKE_previewimg_id_get_p(new_id);
229
230         if (old_prv_p && *old_prv_p) {
231                 BLI_assert(new_prv_p != NULL && ELEM(*new_prv_p, NULL, *old_prv_p));
232 //              const int new_icon_id = get_next_free_id();
233
234 //              if (new_icon_id == 0) {
235 //                      return;  /* Failure. */
236 //              }
237                 *new_prv_p = BKE_previewimg_copy(*old_prv_p);
238                 new_id->icon_id = (*new_prv_p)->icon_id = 0;
239         }
240 }
241
242 PreviewImage **BKE_previewimg_id_get_p(const ID *id)
243 {
244         switch (GS(id->name)) {
245 #define ID_PRV_CASE(id_code, id_struct) case id_code: { return &((id_struct *)id)->preview; } ((void)0)
246                 ID_PRV_CASE(ID_MA, Material);
247                 ID_PRV_CASE(ID_TE, Tex);
248                 ID_PRV_CASE(ID_WO, World);
249                 ID_PRV_CASE(ID_LA, Lamp);
250                 ID_PRV_CASE(ID_IM, Image);
251                 ID_PRV_CASE(ID_BR, Brush);
252                 ID_PRV_CASE(ID_OB, Object);
253                 ID_PRV_CASE(ID_GR, Group);
254                 ID_PRV_CASE(ID_SCE, Scene);
255 #undef ID_PRV_CASE
256                 default:
257                         break;
258         }
259
260         return NULL;
261 }
262
263 void BKE_previewimg_id_free(ID *id)
264 {
265         PreviewImage **prv_p = BKE_previewimg_id_get_p(id);
266         if (prv_p) {
267                 BKE_previewimg_free(prv_p);
268         }
269 }
270
271 PreviewImage *BKE_previewimg_id_ensure(ID *id)
272 {
273         PreviewImage **prv_p = BKE_previewimg_id_get_p(id);
274
275         if (prv_p) {
276                 if (*prv_p == NULL) {
277                         *prv_p = BKE_previewimg_create();
278                 }
279                 return *prv_p;
280         }
281
282         return NULL;
283 }
284
285 PreviewImage *BKE_previewimg_cached_get(const char *name)
286 {
287         return BLI_ghash_lookup(gCachedPreviews, name);
288 }
289
290 /**
291  * Generate an empty PreviewImage, if not yet existing.
292  */
293 PreviewImage *BKE_previewimg_cached_ensure(const char *name)
294 {
295         PreviewImage *prv = NULL;
296         void **key_p, **prv_p;
297
298         if (!BLI_ghash_ensure_p_ex(gCachedPreviews, name, &key_p, &prv_p)) {
299                 *key_p = BLI_strdup(name);
300                 *prv_p = BKE_previewimg_create();
301         }
302         prv = *prv_p;
303         BLI_assert(prv);
304
305         return prv;
306 }
307
308 /**
309  * Generate a PreviewImage from given file path, using thumbnails management, if not yet existing.
310  */
311 PreviewImage *BKE_previewimg_cached_thumbnail_read(
312         const char *name, const char *path, const int source, bool force_update)
313 {
314         PreviewImage *prv = NULL;
315         void **prv_p;
316
317         prv_p = BLI_ghash_lookup_p(gCachedPreviews, name);
318
319         if (prv_p) {
320                 prv = *prv_p;
321                 BLI_assert(prv);
322         }
323
324         if (prv && force_update) {
325                 const char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
326                 if (((int)prv_deferred_data[0] == source) && STREQ(&prv_deferred_data[1], path)) {
327                         /* If same path, no need to re-allocate preview, just clear it up. */
328                         BKE_previewimg_clear(prv);
329                 }
330                 else {
331                         BKE_previewimg_free(&prv);
332                 }
333         }
334
335         if (!prv) {
336                 /* We pack needed data for lazy loading (source type, in a single char, and path). */
337                 const size_t deferred_data_size = strlen(path) + 2;
338                 char *deferred_data;
339
340                 prv = previewimg_create_ex(deferred_data_size);
341                 deferred_data = PRV_DEFERRED_DATA(prv);
342                 deferred_data[0] = source;
343                 memcpy(&deferred_data[1], path, deferred_data_size - 1);
344
345                 force_update = true;
346         }
347
348         if (force_update) {
349                 if (prv_p) {
350                         *prv_p = prv;
351                 }
352                 else {
353                         BLI_ghash_insert(gCachedPreviews, BLI_strdup(name), prv);
354                 }
355         }
356
357         return prv;
358 }
359
360 void BKE_previewimg_cached_release_pointer(PreviewImage *prv)
361 {
362         if (prv) {
363                 if (prv->tag & PRV_TAG_DEFFERED_RENDERING) {
364                         /* We cannot delete the preview while it is being loaded in another thread... */
365                         prv->tag |= PRV_TAG_DEFFERED_DELETE;
366                         return;
367                 }
368                 if (prv->icon_id) {
369                         BKE_icon_delete(prv->icon_id);
370                 }
371                 BKE_previewimg_freefunc(prv);
372         }
373 }
374
375 void BKE_previewimg_cached_release(const char *name)
376 {
377         PreviewImage *prv = BLI_ghash_popkey(gCachedPreviews, name, MEM_freeN);
378
379         BKE_previewimg_cached_release_pointer(prv);
380 }
381
382 /** Handle deferred (lazy) loading/generation of preview image, if needed.
383  * For now, only used with file thumbnails. */
384 void BKE_previewimg_ensure(PreviewImage *prv, const int size)
385 {
386         if ((prv->tag & PRV_TAG_DEFFERED) != 0) {
387                 const bool do_icon = ((size == ICON_SIZE_ICON) && !prv->rect[ICON_SIZE_ICON]);
388                 const bool do_preview = ((size == ICON_SIZE_PREVIEW) && !prv->rect[ICON_SIZE_PREVIEW]);
389
390                 if (do_icon || do_preview) {
391                         ImBuf *thumb;
392                         char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
393                         int source =  prv_deferred_data[0];
394                         char *path = &prv_deferred_data[1];
395                         int icon_w, icon_h;
396
397                         thumb = IMB_thumb_manage(path, THB_LARGE, source);
398
399                         if (thumb) {
400                                 /* PreviewImage assumes premultiplied alhpa... */
401                                 IMB_premultiply_alpha(thumb);
402
403                                 if (do_preview) {
404                                         prv->w[ICON_SIZE_PREVIEW] = thumb->x;
405                                         prv->h[ICON_SIZE_PREVIEW] = thumb->y;
406                                         prv->rect[ICON_SIZE_PREVIEW] = MEM_dupallocN(thumb->rect);
407                                         prv->flag[ICON_SIZE_PREVIEW] &= ~(PRV_CHANGED | PRV_USER_EDITED);
408                                 }
409                                 if (do_icon) {
410                                         if (thumb->x > thumb->y) {
411                                                 icon_w = ICON_RENDER_DEFAULT_HEIGHT;
412                                                 icon_h = (thumb->y * icon_w) / thumb->x + 1;
413                                         }
414                                         else if (thumb->x < thumb->y) {
415                                                 icon_h = ICON_RENDER_DEFAULT_HEIGHT;
416                                                 icon_w = (thumb->x * icon_h) / thumb->y + 1;
417                                         }
418                                         else {
419                                                 icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
420                                         }
421
422                                         IMB_scaleImBuf(thumb, icon_w, icon_h);
423                                         prv->w[ICON_SIZE_ICON] = icon_w;
424                                         prv->h[ICON_SIZE_ICON] = icon_h;
425                                         prv->rect[ICON_SIZE_ICON] = MEM_dupallocN(thumb->rect);
426                                         prv->flag[ICON_SIZE_ICON] &= ~(PRV_CHANGED | PRV_USER_EDITED);
427                                 }
428                                 IMB_freeImBuf(thumb);
429                         }
430                 }
431         }
432 }
433
434 void BKE_icon_changed(const int icon_id)
435 {
436         Icon *icon = NULL;
437         
438         if (!icon_id || G.background) return;
439
440         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
441         
442         if (icon) {
443                 /* Do not enforce creation of previews for valid ID types using BKE_previewimg_id_ensure() here ,
444                  * we only want to ensure *existing* preview images are properly tagged as changed/invalid, that's all. */
445                 PreviewImage **p_prv = BKE_previewimg_id_get_p((ID *)icon->obj);
446
447                 /* If we have previews, they all are now invalid changed. */
448                 if (p_prv && *p_prv) {
449                         int i;
450                         for (i = 0; i < NUM_ICON_SIZES; ++i) {
451                                 (*p_prv)->flag[i] |= PRV_CHANGED;
452                                 (*p_prv)->changed_timestamp[i]++;
453                         }
454                 }
455         }
456 }
457
458 static int icon_id_ensure_create_icon(struct ID *id)
459 {
460         Icon *new_icon = NULL;
461
462         new_icon = MEM_mallocN(sizeof(Icon), __func__);
463
464         new_icon->obj = id;
465         new_icon->type = GS(id->name);
466
467         /* next two lines make sure image gets created */
468         new_icon->drawinfo = NULL;
469         new_icon->drawinfo_free = NULL;
470
471         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);
472
473         return id->icon_id;
474 }
475
476 int BKE_icon_id_ensure(struct ID *id)
477 {
478         if (!id || G.background)
479                 return 0;
480
481         if (id->icon_id)
482                 return id->icon_id;
483
484         id->icon_id = get_next_free_id();
485
486         if (!id->icon_id) {
487                 printf("%s: Internal error - not enough IDs\n", __func__);
488                 return 0;
489         }
490
491         /* Ensure we synchronize ID icon_id with its previewimage if it has one. */
492         PreviewImage **p_prv = BKE_previewimg_id_get_p(id);
493         if (p_prv && *p_prv) {
494                 BLI_assert(ELEM((*p_prv)->icon_id, 0, id->icon_id));
495                 (*p_prv)->icon_id = id->icon_id;
496         }
497
498         return icon_id_ensure_create_icon(id);
499 }
500
501 /**
502  * Return icon id of given preview, or create new icon if not found.
503  */
504 int BKE_icon_preview_ensure(ID *id, PreviewImage *preview)
505 {
506         Icon *new_icon = NULL;
507
508         if (!preview || G.background)
509                 return 0;
510
511         if (id) {
512                 BLI_assert(BKE_previewimg_id_ensure(id) == preview);
513         }
514
515         if (preview->icon_id) {
516                 BLI_assert(!id || !id->icon_id || id->icon_id == preview->icon_id);
517                 return preview->icon_id;
518         }
519
520         if (id && id->icon_id) {
521                 preview->icon_id = id->icon_id;
522                 return preview->icon_id;
523         }
524
525         preview->icon_id = get_next_free_id();
526
527         if (!preview->icon_id) {
528                 printf("%s: Internal error - not enough IDs\n", __func__);
529                 return 0;
530         }
531
532         /* Ensure we synchronize ID icon_id with its previewimage if available, and generate suitable 'ID' icon. */
533         if (id) {
534                 id->icon_id = preview->icon_id;
535                 return icon_id_ensure_create_icon(id);
536         }
537
538         new_icon = MEM_mallocN(sizeof(Icon), __func__);
539
540         new_icon->obj = preview;
541         new_icon->type = 0;  /* Special, tags as non-ID icon/preview. */
542
543         /* next two lines make sure image gets created */
544         new_icon->drawinfo = NULL;
545         new_icon->drawinfo_free = NULL;
546
547         BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(preview->icon_id), new_icon);
548
549         return preview->icon_id;
550 }
551
552 Icon *BKE_icon_get(const int icon_id)
553 {
554         Icon *icon = NULL;
555
556         icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
557         
558         if (!icon) {
559                 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
560                 return NULL;
561         }
562
563         return icon;
564 }
565
566 void BKE_icon_set(const int icon_id, struct Icon *icon)
567 {
568         void **val_p;
569
570         if (BLI_ghash_ensure_p(gIcons, SET_INT_IN_POINTER(icon_id), &val_p)) {
571                 printf("%s: Internal error, icon already set: %d\n", __func__, icon_id);
572                 return;
573         }
574
575         *val_p = icon;
576 }
577
578 void BKE_icon_id_delete(struct ID *id)
579 {
580         if (!id->icon_id) return;  /* no icon defined for library object */
581
582         BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
583         id->icon_id = 0;
584 }
585
586 /**
587  * Remove icon and free data.
588  */
589 void BKE_icon_delete(const int icon_id)
590 {
591         Icon *icon;
592
593         if (!icon_id) return;  /* no icon defined for library object */
594
595         icon = BLI_ghash_popkey(gIcons, SET_INT_IN_POINTER(icon_id), NULL);
596
597         if (icon) {
598                 if (icon->type) {
599                         ((ID *)(icon->obj))->icon_id = 0;
600                 }
601                 else {
602                         ((PreviewImage *)(icon->obj))->icon_id = 0;
603                 }
604                 icon_free(icon);
605         }
606 }