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