Blender V5.3
preview_image.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 <string>
10
11#include "DNA_ID.h"
12#include "DNA_brush_types.h"
14#include "DNA_light_types.h"
15#include "DNA_material_types.h"
16#include "DNA_node_types.h"
17#include "DNA_object_types.h"
18#include "DNA_scene_types.h"
19#include "DNA_screen_types.h"
20#include "DNA_texture_types.h"
21#include "DNA_world_types.h"
22
23#include "BKE_icons.hh"
24
25#ifndef NDEBUG
26# include "BLI_threads.hh"
27#endif
28
29#include "BLO_read_write.hh"
30
31#include "GPU_texture.hh"
32
33#include "IMB_imbuf.hh"
34#include "IMB_thumbs.hh"
35
36#include "atomic_ops.h"
37
38#include "BKE_preview_image.hh"
39
40namespace blender {
41
43
44/* Not mutex-protected! */
46{
47 static CachedPreviewMap cached_previews_map;
48 return cached_previews_map;
49}
50
51namespace bke {
52
59
62{
63 if (other.deferred_loading_data) {
64 this->deferred_loading_data = std::make_unique<PreviewDeferredLoadingData>(
66 }
67}
69
70} // namespace bke
71
72static PreviewImage *previewimg_deferred_create(const char *filepath, ThumbSource source)
73{
75 prv->runtime->deferred_loading_data = std::make_unique<bke::PreviewDeferredLoadingData>();
76 prv->runtime->deferred_loading_data->filepath = filepath;
77 prv->runtime->deferred_loading_data->source = source;
78 return prv;
79}
80
86{
87 if (*prv == nullptr) {
88 return false;
89 }
90
92
93 /* User counting is only done in few cases. If the count is 0, no counting is being used. */
94 if ((*prv)->runtime->user_count > 0) {
95 (*prv)->runtime->user_count--;
96 if ((*prv)->runtime->user_count > 0) {
97 /* Don't free yet. */
98 return false;
99 }
100 }
101
102 bool do_delete = true;
103
104 /* If a preview is still being rendered, tag it for deferred deletion in
105 * BKE_previewimg_render_end. */
106 for (int i = 0; i < NUM_ICON_SIZES; i++) {
107 if ((*prv)->runtime->tag[i] & PRV_TAG_DEFERRED_RENDERING) {
108 (*prv)->runtime->tag[i] |= PRV_TAG_DEFERRED_DELETE;
109 do_delete = false;
110 }
111 }
112
113 if (do_delete) {
115 }
116 *prv = nullptr;
117 return true;
118}
119
121{
122 PreviewImage *prv = MEM_new<PreviewImage>(__func__);
123
124 for (int i = 0; i < NUM_ICON_SIZES; i++) {
125 prv->flag[i] |= PRV_CHANGED;
126 prv->changed_timestamp[i] = 0;
127 }
128
129 prv->runtime = MEM_new<bke::PreviewImageRuntime>(__func__);
130 return prv;
131}
132
134{
135 if (prv && (*prv)) {
136 if ((*prv)->runtime->icon_id) {
137 BKE_icon_delete((*prv)->runtime->icon_id);
138 }
139
140 for (int i = 0; i < NUM_ICON_SIZES; i++) {
141 if ((*prv)->rect[i]) {
142 MEM_delete((*prv)->rect[i]);
143 }
144 if ((*prv)->runtime->gputexture[i]) {
145 GPU_texture_free((*prv)->runtime->gputexture[i]);
146 }
147 }
148
149 MEM_delete((*prv)->runtime);
150 MEM_delete(*prv);
151 *prv = nullptr;
152 }
153}
154
156
158{
160 for (PreviewImage *prv : cache.values()) {
162 }
163 cache.clear();
164}
165
167{
168 MEM_SAFE_DELETE(prv->rect[size]);
169 if (prv->runtime->gputexture[size]) {
171 }
172 prv->h[size] = prv->w[size] = 0;
173 prv->flag[size] |= PRV_CHANGED;
174 prv->flag[size] &= ~PRV_USER_EDITED;
175 prv->changed_timestamp[size] = 0;
176}
177
179{
180 for (int i = 0; i < NUM_ICON_SIZES; i++) {
182 }
183}
184
186{
187 if (!prv) {
188 return nullptr;
189 }
190
191 PreviewImage *prv_img = MEM_new<PreviewImage>(__func__);
192 *prv_img = dna::shallow_copy(*prv);
193 prv_img->runtime = MEM_new<bke::PreviewImageRuntime>(__func__, *prv->runtime);
194
195 for (int i = 0; i < NUM_ICON_SIZES; i++) {
196 if (prv->rect[i]) {
197 prv_img->rect[i] = MEM_dupalloc(prv->rect[i]);
198 }
199 prv_img->runtime->gputexture[i] = nullptr;
200 }
201
202 return prv_img;
203}
204
205void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
206{
207 PreviewImage **old_prv_p = BKE_previewimg_id_get_p(old_id);
208 PreviewImage **new_prv_p = BKE_previewimg_id_get_p(new_id);
209
210 if (old_prv_p && *old_prv_p) {
211 BLI_assert(new_prv_p != nullptr && ELEM(*new_prv_p, nullptr, *old_prv_p));
212 // const int new_icon_id = get_next_free_id();
213
214 // if (new_icon_id == 0) {
215 // return; /* Failure. */
216 // }
217 *new_prv_p = BKE_previewimg_copy(*old_prv_p);
218 new_id->icon_id = (*new_prv_p)->runtime->icon_id = 0;
219 }
220}
221
223{
224 switch (GS(id->name)) {
225#define ID_PRV_CASE(id_code, id_struct) \
226 case id_code: { \
227 return &((id_struct *)id)->preview; \
228 } \
229 ((void)0)
242#undef ID_PRV_CASE
243 default:
244 break;
245 }
246
247 return nullptr;
248}
249
251{
253 return prv_p ? *prv_p : nullptr;
254}
255
257{
259 if (prv_p) {
261 }
262}
263
265{
267 if (!prv_p) {
268 return nullptr;
269 }
270
271 if (*prv_p == nullptr) {
272 *prv_p = BKE_previewimg_create();
273 }
274 return *prv_p;
275}
276
277void BKE_previewimg_id_custom_set(ID *id, const char *filepath)
278{
280
281 /* Thumbnail previews must use the deferred pipeline. But we force them to be immediately
282 * generated here still. */
283
286
287 /* Can't lazy-render the preview on access. ID previews are saved to files and we want them to be
288 * there in time. Not only if something happened to have accessed it meanwhile. */
289 for (int i = 0; i < NUM_ICON_SIZES; i++) {
290 BKE_previewimg_ensure(*prv, i);
291 /* Prevent auto-updates. */
292 (*prv)->flag[i] |= PRV_USER_EDITED;
293 }
294}
295
297{
298 return ELEM(GS(id->name), ID_OB, ID_MA, ID_TE, ID_LA, ID_WO, ID_IM, ID_BR, ID_GR, ID_SCE);
299}
300
306
308{
310
312 name, [&]() { return BKE_previewimg_create(); });
313 BLI_assert(prv);
314 return prv;
315}
316
318 const char *filepath,
319 const int source,
320 bool force_update,
321 const bool count_users)
322{
324
325 PreviewImage *prv = nullptr;
326 PreviewImage **prv_p;
327
329 prv_p = cache.lookup_ptr_as(name);
330
331 if (prv_p) {
332 prv = *prv_p;
333 BLI_assert(prv);
335 }
336
337 if (prv && force_update) {
338 if (prv->runtime->deferred_loading_data &&
339 (prv->runtime->deferred_loading_data->source == source) &&
340 (prv->runtime->deferred_loading_data->filepath == filepath))
341 {
342 /* If same filepath, no need to re-allocate preview, just clear it up. */
344 }
345 else {
347 }
348 }
349
350 if (!prv) {
351 prv = previewimg_deferred_create(filepath, ThumbSource(source));
352 force_update = true;
353 }
354
355 if (count_users) {
356 prv->runtime->user_count++;
357 }
358
359 if (force_update) {
360 if (prv_p) {
361 *prv_p = prv;
362 }
363 else {
364 cache.add(name, prv);
365 }
366 }
367
368 return prv;
369}
370
372 const char *dst_filepath,
373 const bool force_update,
374 const bool count_users)
375{
377 name, dst_filepath, THB_SOURCE_DIRECT, force_update, count_users);
378 preview->runtime->deferred_loading_data->is_online = true;
379
380 return preview;
381}
382
383void BKE_previewimg_cached_release(const char *name)
384{
387 PreviewImage *prv = cache.lookup_default(name, nullptr);
388 /* The preview may not be freed when there are more users still. In that case, keep the preview
389 * in the cache, so other users can still find it an free it eventually. */
390 if (previewimg_free_or_defer(&prv)) {
391 cache.pop_try(name);
392 }
393}
394
395void BKE_previewimg_ensure(PreviewImage *prv, const int size)
396{
397 if (!prv->runtime->deferred_loading_data) {
398 return;
399 }
400
401 const bool do_icon = ((size == ICON_SIZE_ICON) && !prv->rect[ICON_SIZE_ICON]);
402 const bool do_preview = ((size == ICON_SIZE_PREVIEW) && !prv->rect[ICON_SIZE_PREVIEW]);
403
404 if (!(do_icon || do_preview)) {
405 /* Nothing to do. */
406 return;
407 }
408
410 int icon_w, icon_h;
411
412 ImBuf *thumb = IMB_thumb_manage(prv_deferred.filepath.c_str(), THB_LARGE, prv_deferred.source);
413 if (!thumb) {
414 return;
415 }
416
417 /* #PreviewImage assumes pre-multiplied alpha. */
419
420 if (do_preview) {
421 prv->w[ICON_SIZE_PREVIEW] = thumb->x;
422 prv->h[ICON_SIZE_PREVIEW] = thumb->y;
423 prv->rect[ICON_SIZE_PREVIEW] = reinterpret_cast<uint *>(
424 MEM_dupalloc<uint8_t>(thumb->byte_data()));
426 }
427 if (do_icon) {
428 if (thumb->x > thumb->y) {
430 icon_h = (thumb->y * icon_w) / thumb->x + 1;
431 }
432 else if (thumb->x < thumb->y) {
434 icon_w = (thumb->x * icon_h) / thumb->y + 1;
435 }
436 else {
437 icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
438 }
439
440 IMB_scale(thumb, icon_w, icon_h, IMBScaleFilter::Box, false);
441 prv->w[ICON_SIZE_ICON] = icon_w;
442 prv->h[ICON_SIZE_ICON] = icon_h;
443 prv->rect[ICON_SIZE_ICON] = reinterpret_cast<uint *>(
444 MEM_dupalloc<uint8_t>(thumb->byte_data()));
446 }
447 IMB_freeImBuf(thumb);
448}
449
451{
452 if (!prv->runtime->deferred_loading_data) {
453 return false;
454 }
455
456 return prv->runtime->deferred_loading_data->is_online;
457}
458
459std::optional<blender::StringRefNull> BKE_previewimg_deferred_filepath_get(const PreviewImage *prv)
460{
461 if (!prv->runtime->deferred_loading_data) {
462 return std::nullopt;
463 }
464
465 return prv->runtime->deferred_loading_data->filepath;
466}
467
469{
470 if (!prv->runtime->deferred_loading_data) {
471 return std::nullopt;
472 }
473
474 return prv->runtime->deferred_loading_data->source;
475}
476
477ImBuf *BKE_previewimg_to_imbuf(const PreviewImage *prv, const int size)
478{
479 const uint w = prv->w[size];
480 const uint h = prv->h[size];
481 const uint *rect = prv->rect[size];
482
483 ImBuf *ima = nullptr;
484
485 if (w > 0 && h > 0 && rect) {
486 /* first allocate imbuf for copying preview into it */
488 memcpy(ima->byte_data_for_write(), rect, w * h * sizeof(uint8_t) * 4);
489 }
490
491 return ima;
492}
493
494void BKE_previewimg_render_start(PreviewImage *prv, const int size, const bool using_job)
495{
497
498 prv->flag[size] |= PRV_RENDERING;
500
501 /* When rendering as a job in another thread, tag so that main thread will not
502 * free it and defer deletion to the job. */
503 if (using_job) {
505 }
506}
507
509 const int size,
510 const PreviewImageRenderEndStatus status)
511{
513
514 bool do_delete = false;
515
517 if (status == PRV_RENDER_STATUS_FAILED) {
518 prv->runtime->tag[size] |= PRV_TAG_DEFERRED_INVALID;
519 }
520
521 /* When job is cancelled for e.g. undo, PRV_RENDERING remains so that it can resume
522 * when going back to that undo step. Also tag for restart in the current state, if
523 * the ID itself was not modified in the undo step but still had its preview cancelled. */
524 if (status == PRV_RENDER_STATUS_CANCELLED) {
525 if (!(prv->flag[size] & PRV_USER_EDITED)) {
527 }
528 }
529 else {
530 prv->flag[size] &= ~PRV_RENDERING;
531 }
532
533 /* Check if we need to do deferred deletion and it's safe to do so. */
534 for (int i = 0; i < NUM_ICON_SIZES; i++) {
535 if (prv->runtime->tag[i] & PRV_TAG_DEFERRED_RENDERING) {
536 /* Another size is still rendering, */
537 return;
538 }
539 if (prv->runtime->tag[i] & PRV_TAG_DEFERRED_DELETE) {
540 /* Deferred deletion is needed. */
541 do_delete = true;
542 }
543 }
544
545 if (do_delete) {
547 }
548}
549
551{
553
554 if (prv->flag[size] & PRV_USER_EDITED) {
555 /* Don't modify custom previews. */
556 return false;
557 }
558
559 if (prv->runtime->tag[size] & PRV_TAG_RESTART_RENDERING) {
561 return true;
562 }
563
564 return false;
565}
566
567bool BKE_previewimg_is_rendering(const PreviewImage *prv, const int size)
568{
569 return (prv->flag[size] & PRV_RENDERING);
570}
571
572bool BKE_previewimg_is_finished(const PreviewImage *prv, const int size)
573{
574 return !(prv->flag[size] & PRV_RENDERING) &&
575 !(prv->runtime->tag[size] & PRV_TAG_RESTART_RENDERING);
576}
577
578bool BKE_previewimg_is_invalid(const PreviewImage *prv, const int size)
579{
580 return (prv->runtime->tag[size] & PRV_TAG_DEFERRED_INVALID) != 0;
581}
582
584{
585 /* Note we write previews also for undo steps. It takes up some memory,
586 * but not doing so would causes all previews to be re-rendered after
587 * undo which is too expensive. */
588
589 if (prv == nullptr) {
590 return;
591 }
592
593 writer->write_struct(prv, [writer](BlendStructWriter<PreviewImage> &struct_writer) {
594 struct_writer.shallow_data.runtime = nullptr;
595 if (!writer->is_undo()) {
596 struct_writer.shallow_data.changed_timestamp[0] = 0;
597 struct_writer.shallow_data.changed_timestamp[1] = 0;
598 }
599 });
600 if (prv->rect[0]) {
601 writer->write_uint32_array(prv->w[0] * prv->h[0], prv->rect[0]);
602 }
603 if (prv->rect[1]) {
604 writer->write_uint32_array(prv->w[1] * prv->h[1], prv->rect[1]);
605 }
606}
607
609{
610 if (prv == nullptr) {
611 return;
612 }
613
614 prv->runtime = MEM_new<bke::PreviewImageRuntime>(__func__);
615
616 for (int i = 0; i < NUM_ICON_SIZES; i++) {
617 if (prv->rect[i]) {
618 if (!BLO_read_array(reader, &prv->rect[i], int64_t(prv->w[i]) * prv->h[i])) {
619 prv->w[i] = 0;
620 prv->h[i] = 0;
621 }
622 }
623
624 /* PRV_RENDERING is a runtime only flag currently, but for undo indicates that we need
625 * to restart preview renders. See ED_preview_restart_work. */
626 if (BLO_read_data_is_undo(reader)) {
627 if ((prv->flag[i] & PRV_RENDERING) && !(prv->flag[i] & PRV_USER_EDITED)) {
630 }
631 }
632 prv->flag[i] &= ~PRV_RENDERING;
633 }
634}
635
636} // namespace blender
#define ICON_RENDER_DEFAULT_HEIGHT
Definition BKE_icons.hh:185
ID and Library types, which are fundamental for SDNA.
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void clear()
Definition BLI_map.hh:1038
std::optional< Value > pop_try(const Key &key)
Definition BLI_map.hh:419
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
const Value * lookup_ptr_as(const ForwardKey &key) const
Definition BLI_map.hh:516
Value & lookup_or_add_cb_as(ForwardKey &&key, const CreateValueF &create_value)
Definition BLI_map.hh:630
Value lookup_default_as(const ForwardKey &key, ForwardValue &&...default_value) const
Definition BLI_map.hh:575
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
BMesh Iterators.
void BKE_previewimg_id_custom_set(ID *id, const char *filepath)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void BKE_previewimg_id_free(ID *id)
void BKE_preview_images_init()
PreviewImage * BKE_previewimg_cached_thumbnail_read(const char *name, const char *filepath, int source, bool force_update, bool count_users=false)
bool BKE_previewimg_render_restart(PreviewImage *prv, int size)
void BKE_previewimg_clear(PreviewImage *prv)
@ NUM_ICON_SIZES
@ ICON_SIZE_ICON
@ ICON_SIZE_PREVIEW
unsigned int uint
Definition BKE_text.h:55
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
void BKE_previewimg_clear_single(PreviewImage *prv, enum eIconSizes size)
bool BKE_previewimg_is_online(const PreviewImage *prv)
@ PRV_TAG_DEFERRED_INVALID
Definition DNA_ID.h:632
@ PRV_TAG_DEFERRED_RENDERING
Definition DNA_ID.h:628
@ PRV_TAG_RESTART_RENDERING
Definition DNA_ID.h:634
@ PRV_TAG_DEFERRED_DELETE
Definition DNA_ID.h:630
void BKE_previewimg_render_end(PreviewImage *prv, int size, PreviewImageRenderEndStatus status)
std::optional< int > BKE_previewimg_deferred_thumb_source_get(const PreviewImage *prv)
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
bool BKE_previewimg_is_invalid(const PreviewImage *prv, int size)
PreviewImage * BKE_previewimg_online_thumbnail_read(const char *name, const char *dst_filepath, bool force_update, bool count_users=false)
void BKE_preview_images_free()
PreviewImage * BKE_previewimg_cached_ensure(const char *name)
bool BKE_previewimg_id_supports_jobs(const ID *id)
void BKE_previewimg_render_start(PreviewImage *prv, int size, bool using_job)
PreviewImage ** BKE_previewimg_id_get_p(const ID *id)
static PreviewImage * previewimg_deferred_create(const char *filepath, ThumbSource source)
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:6819
void IMB_freeImBuf(ImBuf *ibuf)
@ PRV_RENDER_STATUS_CANCELLED
static CachedPreviewMap & get_cached_previews_map()
unsigned char uint8_t
Definition BKE_text.h:79
PreviewImage * BKE_previewimg_id_ensure(ID *id)
int BLI_thread_is_main()
Definition threads.cc:184
void BLO_read_data_set_need_preview_render_restart(BlendDataReader *reader)
Definition readfile.cc:6863
void BKE_previewimg_free(PreviewImage **prv)
Map< std::string, PreviewImage * > CachedPreviewMap
PreviewImage * BKE_previewimg_id_get(const ID *id)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
bool BKE_previewimg_is_finished(const PreviewImage *prv, int size)
@ THB_SOURCE_IMAGE
Definition IMB_thumbs.hh:33
@ THB_SOURCE_DIRECT
Definition IMB_thumbs.hh:47
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
PreviewImage * BKE_previewimg_create()
PreviewImage * BKE_previewimg_cached_get(const char *name)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
bool BKE_previewimg_is_rendering(const PreviewImage *prv, int size)
ImBuf * IMB_thumb_manage(const char *file_or_lib_path, ThumbSize size, ThumbSource source, const ThumbCancellationToken *cancel_token=nullptr)
Definition thumbs.cc:601
std::optional< StringRefNull > BKE_previewimg_deferred_filepath_get(const PreviewImage *prv)
void GPU_texture_free(gpu::Texture *texture)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
PreviewImage * BKE_previewimg_copy(const PreviewImage *prv)
void BKE_previewimg_cached_release(const char *name)
ImBuf * BKE_previewimg_to_imbuf(const PreviewImage *prv, int size)
void BKE_previewimg_ensure(PreviewImage *prv, int size)
@ PRV_USER_EDITED
Definition DNA_ID.h:620
@ PRV_RENDERING
Definition DNA_ID.h:622
@ PRV_CHANGED
Definition DNA_ID.h:618
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
bool BKE_icon_delete(int icon_id)
Definition icons.cc:545
static bool previewimg_free_or_defer(PreviewImage **prv)
void IMB_premultiply_alpha(ImBuf *ibuf)
Definition filter.cc:382
#define ID_PRV_CASE(id_code, id_struct)
void write_uint32_array(int64_t num, const uint32_t *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
int icon_id
Definition DNA_ID.h:445
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
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
short changed_timestamp[2]
Definition DNA_ID.h:648
std::unique_ptr< PreviewDeferredLoadingData > deferred_loading_data
std::array< gpu::Texture *, NUM_ICON_SIZES > gputexture