Blender V5.3
icons.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006-2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11#include <memory>
12
13#include "CLG_log.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_ID.h"
19
20#include "BLI_fileops.hh"
22#include "BLI_map.hh"
23#include "BLI_mutex.hh"
24#include "BLI_span.hh"
25#include "BLI_threads.hh"
26#include "BLI_utildefines.hh"
27
28#include "BKE_global.hh" /* only for G.background test */
29#include "BKE_icons.hh"
30#include "BKE_preview_image.hh"
31#include "BKE_studiolight.h"
32
33#include "BLI_sys_types.hh" /* for intptr_t support */
34
35#include "IMB_imbuf.hh"
36#include "IMB_imbuf_types.hh"
37
38namespace blender {
39
44enum {
46};
47
48/* GLOBALS */
49
50static CLG_LogRef LOG = {"lib.icons"};
51
52/* Protected by gIconMutex. */
55{
56 static GlobalIconsMap gIcons;
57 return gIcons;
58}
59
60/* Protected by gIconMutex. */
61static int gNextIconId = 1;
62
63/* Protected by gIconMutex. */
64static int gFirstIconId = 1;
65
67
68/* Queue of icons for deferred deletion. */
73/* Protected by gIconMutex. */
75
76static void icon_free(void *val)
77{
78 Icon *icon = static_cast<Icon *>(val);
79 if (!icon) {
80 return;
81 }
82
83 if (icon->obj_type == ICON_DATA_GEOM) {
84 Icon_Geom *obj = static_cast<Icon_Geom *>(icon->obj);
85 if (obj->mem) {
86 /* coords & colors are part of this memory. */
87 MEM_delete(obj->mem);
88 }
89 else {
90 MEM_delete(obj->coords);
91 MEM_delete(obj->colors);
92 }
93 MEM_delete(obj);
94 }
95
96 if (icon->drawinfo_free) {
97 icon->drawinfo_free(icon->drawinfo);
98 }
99 else if (icon->drawinfo) {
101 }
102 MEM_delete(icon);
103}
104
105static void icon_free_data(int icon_id, Icon *icon)
106{
107 switch (icon->obj_type) {
108 case ICON_DATA_ID:
109 (static_cast<ID *>(icon->obj))->icon_id = 0;
110 break;
111 case ICON_DATA_IMBUF: {
112 ImBuf *imbuf = static_cast<ImBuf *>(icon->obj);
113 if (imbuf) {
115 }
116 break;
117 }
119 (static_cast<PreviewImage *>(icon->obj))->runtime->icon_id = 0;
120 break;
122 (static_cast<bGPDlayer *>(icon->obj))->runtime.icon_id = 0;
123 break;
124 case ICON_DATA_GEOM:
125 (static_cast<Icon_Geom *>(icon->obj))->icon_id = 0;
126 break;
128 StudioLight *sl = static_cast<StudioLight *>(icon->obj);
129 if (sl != nullptr) {
131 }
132 break;
133 }
134 default:
136 }
137}
138
139static Icon *icon_ghash_lookup(int icon_id)
140{
141 std::scoped_lock lock(gIconMutex);
142 const GlobalIconsMap &gIcons = get_global_icons_map();
143 return gIcons.lookup_default(icon_id, nullptr);
144}
145
146/* create an id for a new icon and make sure that ids from deleted icons get reused
147 * after the integer number range is used up */
149{
150 std::scoped_lock lock(gIconMutex);
151 const GlobalIconsMap &gIcons = get_global_icons_map();
152 int startId = gFirstIconId;
153
154 /* if we haven't used up the int number range, we just return the next int */
155 if (gNextIconId >= gFirstIconId) {
156 int next_id = gNextIconId++;
157 return next_id;
158 }
159
160 /* Now we try to find the smallest icon id not stored in the gIcons hash.
161 * Don't use icon_ghash_lookup here, it would lock recursively (dead-lock). */
162 while (gIcons.contains(startId) && startId >= gFirstIconId) {
163 startId++;
164 }
165
166 /* if we found a suitable one that isn't used yet, return it */
167 if (startId >= gFirstIconId) {
168 return startId;
169 }
170
171 /* fail */
172 return 0;
173}
174
175void BKE_icons_init(int first_dyn_id)
176{
178
179 gNextIconId = first_dyn_id;
180 gFirstIconId = first_dyn_id;
181
183}
184
186{
188
190 for (Icon *icon : gIcons.values()) {
191 icon_free(icon);
192 }
193 gIcons.clear();
194
196}
197
199{
200 std::scoped_lock lock(gIconMutex);
202
203 for (DeferredIconDeleteNode *node = reinterpret_cast<DeferredIconDeleteNode *>(
205 node != nullptr;
206 node = node->next)
207 {
208 if (Icon *icon = gIcons.pop_default(node->icon_id, nullptr)) {
209 icon_free(icon);
210 }
211 }
213}
214
215void BKE_icon_changed(const int icon_id)
216{
217 Icon *icon = nullptr;
218
219 if (!icon_id || G.background) {
220 return;
221 }
222
223 icon = icon_ghash_lookup(icon_id);
224 if (!icon) {
225 return;
226 }
227
228 /* We *only* expect ID-tied icons here, not non-ID icon/preview! */
229 BLI_assert(icon->id_type != 0);
231
232 /* Do not enforce creation of previews for valid ID types using BKE_previewimg_id_ensure()
233 * here, we only want to ensure *existing* preview images are properly tagged as
234 * changed/invalid, that's all. */
235 PreviewImage **p_prv = BKE_previewimg_id_get_p(static_cast<ID *>(icon->obj));
236
237 /* If we have previews, they all are now invalid changed. */
238 if (p_prv && *p_prv) {
239 for (int i = 0; i < NUM_ICON_SIZES; i++) {
240 (*p_prv)->flag[i] |= PRV_CHANGED;
241 (*p_prv)->changed_timestamp[i]++;
242 }
243 }
244}
245
246static Icon *icon_create(int icon_id, int obj_type, void *obj)
247{
248 Icon *new_icon = MEM_new_uninitialized<Icon>(__func__);
249
250 new_icon->obj_type = obj_type;
251 new_icon->obj = obj;
252 new_icon->id_type = 0;
253 new_icon->flag = 0;
254
255 /* next two lines make sure image gets created */
256 new_icon->drawinfo = nullptr;
257 new_icon->drawinfo_free = nullptr;
258
259 {
260 std::scoped_lock lock(gIconMutex);
262 gIcons.add(icon_id, new_icon);
263 }
264
265 return new_icon;
266}
267
269{
271
272 Icon *icon = icon_create(id->icon_id, ICON_DATA_ID, id);
273 icon->id_type = GS(id->name);
274 icon->flag = ICON_FLAG_MANAGED;
275
276 return id->icon_id;
277}
278
280{
281 /* Never handle icons in non-main thread! */
283
284 if (!id || G.background) {
285 return 0;
286 }
287
288 if (id->icon_id) {
289 return id->icon_id;
290 }
291
292 id->icon_id = get_next_free_id();
293
294 if (!id->icon_id) {
295 CLOG_ERROR(&LOG, "not enough IDs");
296 return 0;
297 }
298
299 /* Ensure we synchronize ID icon_id with its previewimage if it has one. */
301 if (p_prv && *p_prv) {
302 BLI_assert(ELEM((*p_prv)->runtime->icon_id, 0, id->icon_id));
303 (*p_prv)->runtime->icon_id = id->icon_id;
304 }
305
307}
308
310{
312
313 /* NOTE: The color previews for GP Layers don't really need
314 * to be "rendered" to image per-se (as it will just be a plain
315 * colored rectangle), we need to define icon data here so that
316 * we can store a pointer to the layer data in icon->obj.
317 */
319 icon->flag = ICON_FLAG_MANAGED;
320
321 return gpl->runtime.icon_id;
322}
323
325{
326 /* Never handle icons in non-main thread! */
328
329 if (!gpl || G.background) {
330 return 0;
331 }
332
333 if (gpl->runtime.icon_id) {
334 return gpl->runtime.icon_id;
335 }
336
338
339 if (!gpl->runtime.icon_id) {
340 CLOG_ERROR(&LOG, "not enough IDs");
341 return 0;
342 }
343
345}
346
348{
349 if (!preview || G.background) {
350 return 0;
351 }
352
353 if (id) {
354 BLI_assert(BKE_previewimg_id_ensure(id) == preview);
355 }
356
357 if (preview->runtime->icon_id) {
358 BLI_assert(!id || !id->icon_id || id->icon_id == preview->runtime->icon_id);
359 return preview->runtime->icon_id;
360 }
361
362 if (id && id->icon_id) {
363 preview->runtime->icon_id = id->icon_id;
364 return preview->runtime->icon_id;
365 }
366
367 preview->runtime->icon_id = get_next_free_id();
368
369 if (!preview->runtime->icon_id) {
370 CLOG_ERROR(&LOG, "not enough IDs");
371 return 0;
372 }
373
374 /* Ensure we synchronize ID icon_id with its previewimage if available,
375 * and generate suitable 'ID' icon. */
376 if (id) {
377 id->icon_id = preview->runtime->icon_id;
379 }
380
381 Icon *icon = icon_create(preview->runtime->icon_id, ICON_DATA_PREVIEW, preview);
382 icon->flag = ICON_FLAG_MANAGED;
383
384 return preview->runtime->icon_id;
385}
386
388{
389 int icon_id = get_next_free_id();
390
391 Icon *icon = icon_create(icon_id, ICON_DATA_IMBUF, ibuf);
392 icon->flag = ICON_FLAG_MANAGED;
393
394 return icon_id;
395}
396
398{
399 Icon *icon = icon_ghash_lookup(icon_id);
400 if (!icon) {
401 CLOG_ERROR(&LOG, "no icon for icon ID: %d", icon_id);
402 return nullptr;
403 }
404 if (icon->obj_type != ICON_DATA_IMBUF) {
405 CLOG_ERROR(&LOG, "icon ID does not refer to an imbuf icon: %d", icon_id);
406 return nullptr;
407 }
408
409 return static_cast<ImBuf *>(icon->obj);
410}
411
412bool BKE_icon_is_imbuf(const int icon_id)
413{
414 const Icon *icon = icon_ghash_lookup(icon_id);
415 if (!icon) {
416 CLOG_ERROR(&LOG, "no icon for icon ID: %d", icon_id);
417 return false;
418 }
419 return icon->obj_type == ICON_DATA_IMBUF;
420}
421
423 const int height,
424 const int channels,
425 const uint8_t *buffer)
426{
427 BLI_assert(buffer != nullptr);
428 BLI_assert(width >= 0);
429 BLI_assert(height >= 0);
430 BLI_assert(channels >= 0);
431
432 return IconBufferRef{
433 .width = width,
434 .height = height,
435 .channels = channels,
436 .buffer = Span(buffer, width * height * channels),
437 };
438}
439
440static std::optional<IconBufferRef> icon_buffer_from_preview(const PreviewImage *preview,
441 const eIconSizes size)
442{
443 if (!preview->rect[size]) {
444 return std::nullopt;
445 }
446
447 const int num_channels = 4; /* #PreviewImage always has 4 color channels. */
448 return construct_icon_buffer(preview->w[size],
449 preview->h[size],
450 num_channels,
451 reinterpret_cast<uint8_t *>(preview->rect[size]));
452}
453
454std::optional<IconBufferRef> BKE_icon_get_buffer(const int icon_id, const eIconSizes size)
455{
456 const Icon *icon = icon_ghash_lookup(icon_id);
457 if (!icon) {
458 CLOG_ERROR(&LOG, "no icon for icon ID: %d", icon_id);
459 return std::nullopt;
460 }
461
462 switch (icon->obj_type) {
463 case ICON_DATA_IMBUF: {
464 const ImBuf *ibuf = static_cast<ImBuf *>(icon->obj);
465 if (!ibuf->byte_data()) {
466 return std::nullopt;
467 }
468 return construct_icon_buffer(ibuf->x, ibuf->y, ibuf->channels, ibuf->byte_data());
469 }
470 case ICON_DATA_ID: {
471 const ID *id = static_cast<ID *>(icon->obj);
472 if (PreviewImage *preview = BKE_previewimg_id_get(id)) {
473 return icon_buffer_from_preview(preview, size);
474 }
475 break;
476 }
477 case ICON_DATA_PREVIEW: {
478 if (const PreviewImage *preview = static_cast<PreviewImage *>(icon->obj)) {
479 if (!BKE_previewimg_is_finished(preview, size)) {
480 return std::nullopt;
481 }
482 return icon_buffer_from_preview(preview, size);
483 }
484 break;
485 }
486 }
487
488 return std::nullopt;
489}
490
491Icon *BKE_icon_get(const int icon_id)
492{
494
495 Icon *icon = nullptr;
496
497 icon = icon_ghash_lookup(icon_id);
498
499 if (!icon) {
500 CLOG_ERROR(&LOG, "no icon for icon ID: %d", icon_id);
501 return nullptr;
502 }
503
504 return icon;
505}
506
507void BKE_icon_set(const int icon_id, Icon *icon)
508{
509 std::scoped_lock lock(gIconMutex);
511 if (!gIcons.add(icon_id, icon)) {
512 CLOG_ERROR(&LOG, "icon already set: %d", icon_id);
513 }
514}
515
516static void icon_add_to_deferred_delete_queue(int icon_id)
517{
519 node->icon_id = icon_id;
520 /* Doesn't need lock. */
522}
523
525{
526 const int icon_id = id->icon_id;
527 if (!icon_id) {
528 return; /* no icon defined for library object */
529 }
530 id->icon_id = 0;
531
532 if (!BLI_thread_is_main()) {
534 return;
535 }
536
538 std::scoped_lock lock(gIconMutex);
540 if (Icon *icon = gIcons.pop_default(icon_id, nullptr)) {
541 icon_free(icon);
542 }
543}
544
545bool BKE_icon_delete(const int icon_id)
546{
547 if (icon_id == 0) {
548 /* no icon defined for library object */
549 return false;
550 }
551
552 std::scoped_lock lock(gIconMutex);
554 if (Icon *icon = gIcons.pop_default(icon_id, nullptr)) {
555 icon_free_data(icon_id, icon);
556 icon_free(icon);
557 return true;
558 }
559
560 return false;
561}
562
563bool BKE_icon_delete_unmanaged(const int icon_id)
564{
565 if (icon_id == 0) {
566 /* no icon defined for library object */
567 return false;
568 }
569
570 std::scoped_lock lock(gIconMutex);
572
573 Icon *icon = gIcons.pop_default(icon_id, nullptr);
574 if (icon) {
575 if (icon->flag & ICON_FLAG_MANAGED) [[unlikely]] {
576 gIcons.add(icon_id, icon);
577 return false;
578 }
579
580 icon_free_data(icon_id, icon);
581 icon_free(icon);
582 return true;
583 }
584
585 return false;
586}
587
588/* -------------------------------------------------------------------- */
591
593{
595
596 if (geom->icon_id) {
597 return geom->icon_id;
598 }
599
600 geom->icon_id = get_next_free_id();
601
602 icon_create(geom->icon_id, ICON_DATA_GEOM, geom);
603 /* Not managed for now, we may want this to be configurable per icon. */
604
605 return geom->icon_id;
606}
607
609{
611 if (data_len <= 8) {
612 return nullptr;
613 }
614 /* Wrapper for RAII early exit cleanups. */
615 std::unique_ptr<uchar> data_wrapper(std::move(data));
616
617 /* Skip the header. */
618 data_len -= 8;
619 const int div = 3 * 2 * 3;
620 const int coords_len = data_len / div;
621 if (coords_len * div != data_len) {
622 return nullptr;
623 }
624
625 const uchar header[4] = {'V', 'C', 'O', 0};
626 uchar *p = data_wrapper.get();
627 if (memcmp(p, header, ARRAY_SIZE(header)) != 0) {
628 return nullptr;
629 }
630 p += 4;
631
633 geom->coords_range[0] = int(*p++);
634 geom->coords_range[1] = int(*p++);
635 /* x, y ignored for now */
636 p += 2;
637
638 geom->coords_len = coords_len;
639 geom->coords = reinterpret_cast<decltype(geom->coords)>(p);
640 geom->colors = reinterpret_cast<decltype(geom->colors)>(p + (data_len / 3));
641 geom->icon_id = 0;
642 /* Move buffer ownership to C buffer. */
643 geom->mem = data_wrapper.release();
644 return geom;
645}
646
648{
650 size_t data_len;
651 uchar *data = static_cast<uchar *>(BLI_file_read_binary_as_mem(filename, 0, &data_len));
652 if (data == nullptr) {
653 return nullptr;
654 }
655 return BKE_icon_geom_from_memory(data, data_len);
656}
657
659
660/* -------------------------------------------------------------------- */
663
665{
666 int icon_id = get_next_free_id();
667 Icon *icon = icon_create(icon_id, ICON_DATA_STUDIOLIGHT, sl);
668 icon->id_type = id_type;
669 return icon_id;
670}
671
673
674} // namespace blender
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
volatile int lock
#define BLI_assert_unreachable()
#define ELEM(...)
void clear()
Definition BLI_map.hh:1038
ValueIterator values() const &
Definition BLI_map.hh:884
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool contains(const Key &key) const
Definition BLI_map.hh:353
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
constexpr Span()=default
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_icon_id_delete(ID *id)
Definition icons.cc:524
static int get_next_free_id()
Definition icons.cc:148
bool BKE_icon_delete_unmanaged(int icon_id)
Definition icons.cc:563
static int gNextIconId
Definition icons.cc:61
@ NUM_ICON_SIZES
static Icon * icon_ghash_lookup(int icon_id)
Definition icons.cc:139
Icon_Geom * BKE_icon_geom_from_file(const char *filename)
Definition icons.cc:647
int BKE_icon_id_ensure(ID *id)
Definition icons.cc:279
bool BKE_icon_is_imbuf(int icon_id) ATTR_WARN_UNUSED_RESULT
Definition icons.cc:412
static int icon_gplayer_color_ensure_create_icon(bGPDlayer *gpl)
Definition icons.cc:309
void BKE_icons_deferred_free()
Definition icons.cc:198
void BKE_icon_set(int icon_id, Icon *icon)
Definition icons.cc:507
static void icon_free(void *val)
Definition icons.cc:76
void BKE_icons_free()
Definition icons.cc:185
@ ICON_DATA_GPLAYER
Definition BKE_icons.hh:42
@ ICON_DATA_GEOM
Definition BKE_icons.hh:38
@ ICON_DATA_STUDIOLIGHT
Definition BKE_icons.hh:40
@ ICON_DATA_ID
Definition BKE_icons.hh:32
@ ICON_DATA_IMBUF
Definition BKE_icons.hh:34
@ ICON_DATA_PREVIEW
Definition BKE_icons.hh:36
Icon * BKE_icon_get(int icon_id)
Definition icons.cc:491
int BKE_icon_ensure_studio_light(StudioLight *sl, int id_type)
Definition icons.cc:664
void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id)
static IconBufferRef construct_icon_buffer(const int width, const int height, const int channels, const uint8_t *buffer)
Definition icons.cc:422
static std::optional< IconBufferRef > icon_buffer_from_preview(const PreviewImage *preview, const eIconSizes size)
Definition icons.cc:440
static GlobalIconsMap & get_global_icons_map()
Definition icons.cc:54
void MEM_delete_void(void *vmemh)
void * BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:550
void BLI_linklist_lockfree_init(LockfreeLinkList *list)
PreviewImage ** BKE_previewimg_id_get_p(const ID *id)
Global G
Definition blender.cc:57
void IMB_freeImBuf(ImBuf *ibuf)
void BKE_icon_changed(int icon_id)
Definition icons.cc:215
Map< int, Icon * > GlobalIconsMap
Definition icons.cc:53
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
void BLI_linklist_lockfree_free(LockfreeLinkList *list, LockfreeeLinkNodeFreeFP free_func)
static void icon_free_data(int icon_id, Icon *icon)
Definition icons.cc:105
static int gFirstIconId
Definition icons.cc:64
Icon_Geom * BKE_icon_geom_from_memory(uchar *data, size_t data_len)
Definition icons.cc:608
unsigned char uint8_t
Definition BKE_text.h:79
@ ICON_FLAG_MANAGED
Definition icons.cc:45
PreviewImage * BKE_previewimg_id_ensure(ID *id)
void BLI_linklist_lockfree_insert(LockfreeLinkList *list, LockfreeLinkNode *node)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
LockfreeLinkNode * BLI_linklist_lockfree_begin(LockfreeLinkList *list)
int BKE_icon_preview_ensure(ID *id, PreviewImage *preview)
Definition icons.cc:347
int BLI_thread_is_main()
Definition threads.cc:184
static Icon * icon_create(int icon_id, int obj_type, void *obj)
Definition icons.cc:246
PreviewImage * BKE_previewimg_id_get(const ID *id)
bool BKE_previewimg_is_finished(const PreviewImage *prv, int size)
static Mutex gIconMutex
Definition icons.cc:66
int BKE_icon_imbuf_create(ImBuf *ibuf) ATTR_WARN_UNUSED_RESULT
Definition icons.cc:387
static void icon_add_to_deferred_delete_queue(int icon_id)
Definition icons.cc:516
static LockfreeLinkList g_icon_delete_queue
Definition icons.cc:74
static int icon_id_ensure_create_icon(ID *id)
Definition icons.cc:268
std::mutex Mutex
Definition BLI_mutex.hh:51
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_icons_init(int first_dyn_id)
Definition icons.cc:175
void BLI_linklist_lockfree_clear(LockfreeLinkList *list, LockfreeeLinkNodeFreeFP free_func)
static CLG_LogRef LOG
Definition logging.cpp:12
int BKE_icon_gplayer_color_ensure(bGPDlayer *gpl)
Definition icons.cc:324
@ PRV_CHANGED
Definition DNA_ID.h:618
bool BKE_icon_delete(int icon_id)
Definition icons.cc:545
ImBuf * BKE_icon_imbuf_get_buffer(int icon_id) ATTR_WARN_UNUSED_RESULT
Definition icons.cc:397
std::optional< IconBufferRef > BKE_icon_get_buffer(int icon_id, eIconSizes size)
Definition icons.cc:454
int BKE_icon_geom_ensure(Icon_Geom *geom)
Definition icons.cc:592
DeferredIconDeleteNode * next
Definition icons.cc:70
const unsigned char * mem
Definition BKE_icons.hh:72
unsigned char(* colors)[4]
Definition BKE_icons.hh:70
unsigned char(* coords)[2]
Definition BKE_icons.hh:69
DrawInfoFreeFP drawinfo_free
Definition BKE_icons.hh:61
void * drawinfo
Definition BKE_icons.hh:49
const uint8_t * byte_data() const
unsigned int w[2]
Definition DNA_ID.h:645
bke::PreviewImageRuntime * runtime
Definition DNA_ID.h:651
unsigned int * rect[2]
Definition DNA_ID.h:649
unsigned int h[2]
Definition DNA_ID.h:646