Blender V5.3
thumbnail_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_map.hh"
10#include "BLI_math_base_c.hh"
11#include "BLI_math_bits.hh"
12#include "BLI_mutex.hh"
13#include "BLI_path_utils.hh"
14#include "BLI_set.hh"
15#include "BLI_task.hh"
16#include "BLI_vector.hh"
17
18#include "BKE_context.hh"
19#include "BKE_lib_id.hh"
20#include "BKE_main.hh"
21#include "BKE_mask.hh"
22#include "BKE_movieclip.hh"
23
24#include "DNA_mask_types.h"
25#include "DNA_scene_types.h"
26#include "DNA_sequence_types.h"
27
28#include "IMB_imbuf.hh"
29
30#include "MOV_read.hh"
31
32#include "SEQ_render.hh"
33#include "SEQ_sequencer.hh"
35#include "SEQ_time.hh"
36
37#include "WM_api.hh"
38
39#include "render.hh"
40
41namespace blender::seq {
42
43static constexpr int MAX_THUMBNAILS = 5000;
44
45// #define DEBUG_PRINT_THUMB_JOB_TIMES
46
48
49/* Thumbnail cache is a map keyed by either media file path or ID, with values being
50 * the various thumbnails that are loaded for it (images mostly contain just
51 * one thumbnail frame, but movies or image sequences can contain multiple).
52 *
53 * Source entries and individual frame entries also record the timestamp when they were
54 * last accessed, so that when the cache is full, some of the old entries can be removed.
55 *
56 * Thumbnails that are requested but do not have an exact match in the cache, are added
57 * to the "requests" set. The requests are processed in the background by a WM job. */
59
60 struct SourceKey {
61 explicit SourceKey() = default;
62 explicit SourceKey(const std::string &path) : path(path) {}
63 explicit SourceKey(const ID *id) : id_session_uid(id->session_uid) {}
64
65 /* Used for strips where media is a file with a path. */
66 std::string path;
67 /* Used for strips that are IDs. We store the session UID so it stays valid
68 * across undo/redo. */
70 /* Hash of any extra data that needs to be part of source key (e.g. camera for scene
71 * strips). */
73
74 bool is_valid() const
75 {
76 return !path.empty() || id_session_uid != 0;
77 }
78
83 friend auto operator<=>(const SourceKey &a, const SourceKey &b) = default;
84 };
85
86 struct FrameEntry {
87 int frame_index = 0; /* Frame index (for movies) or image index (for image sequences). */
88 int stream_index = 0; /* Stream index (only for multi-stream movies). */
89 ImBuf *thumb = nullptr;
91 /* The source has changed, but keep displaying this until a replacement is generated. */
92 bool stale = false;
93 };
94
100 struct Request {
101 explicit Request(const SourceKey &source_key,
102 int frame,
103 int stream,
104 StripType type,
105 int64_t logical_time,
107 float time_frame,
108 int ch)
110 frame_index(frame),
111 stream_index(stream),
112 strip_type(type),
114 requested_at(logical_time),
115 timeline_frame(time_frame),
116 channel(ch)
117 {
118 }
119 /* These determine request uniqueness (for equality/hash in a Set). */
121 int frame_index = 0; /* Frame index (for movies) or image index (for image sequences). */
122 int stream_index = 0; /* Stream index (only for multi-stream movies). */
125
126 /* The following members are payload and do not contribute to uniqueness. */
128 float timeline_frame = 0;
129 int channel = 0;
130 /* For scene-strip requests only. Pointer is re-validated against live data
131 * before actual use. */
132 const Strip *scene_strip = nullptr;
133
139 friend bool operator==(const Request &a, const Request &b)
140 {
141 return a.frame_index == b.frame_index && a.stream_index == b.stream_index &&
142 a.strip_type == b.strip_type && a.source_generation == b.source_generation &&
143 a.source_key == b.source_key;
144 }
145 };
146
149 /* Local copies of IDs (e.g. movie clip, mask) used for thumbnails, keyed by source generation.
150 * Copies are needed so that the thumbnail generation thread is safe with regards to ID
151 * modifications or deletions that might happen on the main thread. */
153 /* Scene strip thumbnail requests. These need to be rendered on main thread (due to usage of main
154 * GPU/DRW context), and are processed separately. */
158
161 {
162 SourceEntry *entry = map_.lookup_ptr(key);
163 if (entry == nullptr) {
164 return false;
165 }
166
167 const uint64_t old_generation = entry->generation;
168 entry->generation = next_generation_++;
169 for (FrameEntry &thumb : entry->frames) {
170 thumb.stale = true;
171 }
172 requests_.remove_if([&](const Request &request) { return request.source_key == key; });
173 scene_requests_.remove_if([&](const Request &request) { return request.source_key == key; });
174
175 /* Drop an unclaimed ID snapshot: it has already been removed from this map and its
176 * result will be rejected by the generation check. */
177 ID *id_copy = id_copies_.pop_default(old_generation, nullptr);
178 if (id_copy != nullptr) {
179 BKE_id_free(nullptr, id_copy);
180 }
181 return true;
182 }
183
185 {
186 clear();
187 }
188
189 void clear()
190 {
191 for (const auto &item : map_.items()) {
192 for (const auto &thumb : item.value.frames) {
193 IMB_freeImBuf(thumb.thumb);
194 }
195 }
196 map_.clear();
199 for (ID *id_copy : id_copies_.values()) {
200 BKE_id_free(nullptr, id_copy);
201 }
203 logical_time_ = 0;
204 }
205
206 bool remove_entry(const SourceKey &key)
207 {
208 SourceEntry *entry = map_.lookup_ptr(key);
209 if (entry == nullptr) {
210 return false;
211 }
212 for (const FrameEntry &thumb : entry->frames) {
213 IMB_freeImBuf(thumb.thumb);
214 }
216 return true;
217 }
218};
219
221 int frame_index,
222 int stream_index,
223 ImBuf *thumb,
224 int64_t used_at)
225{
226 for (ThumbnailCache::FrameEntry &frame : entry.frames) {
227 if (frame.frame_index == frame_index && frame.stream_index == stream_index) {
228 IMB_freeImBuf(frame.thumb);
229 frame.thumb = thumb;
230 frame.used_at = math::max(frame.used_at, used_at);
231 frame.stale = false;
232 return;
233 }
234 }
235 entry.frames.append({frame_index, stream_index, thumb, used_at, false});
236}
237
239{
240 ThumbnailCache **cache = &scene->ed->runtime->thumbnail_cache;
241 if (*cache == nullptr) {
242 *cache = MEM_new<ThumbnailCache>(__func__);
243 }
244 return *cache;
245}
246
248{
249 if (scene == nullptr || scene->ed == nullptr) {
250 return nullptr;
251 }
252 return scene->ed->runtime->thumbnail_cache;
253}
254
255void image_size_to_thumb_size(int &r_width, int &r_height, int size)
256{
257 const float aspect = float(r_width) / float(r_height);
258 if (r_width > r_height) {
259 r_width = size;
260 r_height = std::max(1, round_fl_to_int(size / aspect));
261 }
262 else {
263 r_height = size;
264 r_width = std::max(1, round_fl_to_int(size * aspect));
265 }
266}
267
268bool strip_can_have_thumbnail(const Scene *scene, const Strip *strip)
269{
270 if (scene == nullptr || scene->ed == nullptr || strip == nullptr) {
271 return false;
272 }
273 if (ELEM(strip->type, STRIP_TYPE_MOVIE, STRIP_TYPE_IMAGE) && strip->data) {
274 const StripElem *se = strip->data->stripdata;
275 if (se->orig_height == 0 || se->orig_width == 0) {
276 return false;
277 }
278 return true;
279 }
280 if (strip->type == STRIP_TYPE_MOVIECLIP && strip->clip) {
281 return true;
282 }
283 if (strip->type == STRIP_TYPE_MASK && strip->mask) {
284 return true;
285 }
286 if (strip->type == STRIP_TYPE_SCENE && (strip->flag & SEQ_SCENE_STRIPS) == 0 && strip->scene &&
287 strip->scene != scene)
288 {
289 return true;
290 }
291 return false;
292}
293
295{
297 BLI_assert(strip->scene);
299 if (strip->scene_camera != nullptr) {
300 key.extra_bits = key.extra_bits * 33 ^ strip->scene_camera->id.session_uid;
301 }
302 if (strip->scene_view_layer_name != nullptr) {
303 key.extra_bits = key.extra_bits * 33 ^ uint32_t(hash_string(strip->scene_view_layer_name));
304 }
305 return key;
306}
307
309 const Strip *strip,
310 float timeline_frame)
311{
312 char filepath[FILE_MAX];
313 filepath[0] = 0;
314 switch (strip->type) {
315 case STRIP_TYPE_IMAGE: {
316 const StripElem *s_elem = render_give_stripelem(scene, strip, timeline_frame);
317 if (s_elem != nullptr) {
318 BLI_path_join(filepath, sizeof(filepath), strip->data->dirpath, s_elem->filename);
319 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&scene->id));
320 return ThumbnailCache::SourceKey(filepath);
321 }
322 } break;
323 case STRIP_TYPE_MOVIE:
325 filepath, sizeof(filepath), strip->data->dirpath, strip->data->stripdata->filename);
326 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&scene->id));
327 return ThumbnailCache::SourceKey(filepath);
329 BLI_assert(strip->clip);
330 return ThumbnailCache::SourceKey(&strip->clip->id);
331 case STRIP_TYPE_MASK:
332 BLI_assert(strip->mask);
333 return ThumbnailCache::SourceKey(&strip->mask->id);
334 case STRIP_TYPE_SCENE:
335 return get_key_from_scene_strip(strip);
336 default:
337 break;
338 }
340}
341
342static ImBuf *make_thumb_for_image(const Scene *scene, const ThumbnailCache::Request &request)
343{
345 request.source_key.path.c_str(), THUMB_SIZE, nullptr, IMBThumbLoadFlags::LoadLargeFiles);
346 if (ibuf == nullptr) {
347 return nullptr;
348 }
349 /* Keep only float buffer if we have both byte & float. */
350 if (ibuf->float_data() != nullptr && ibuf->byte_data() != nullptr) {
352 }
353
354 ensure_ibuf_is_sequencer_space(scene, ibuf, false);
355 return ibuf;
356}
357
359{
360 if (ibuf == nullptr) {
361 return;
362 }
363
364 /* We only need byte thumbnails. */
365 if (ibuf->float_data()) {
366 if (ibuf->byte_data() == nullptr) {
368 }
370 }
371
372 int width = ibuf->x;
373 int height = ibuf->y;
374 image_size_to_thumb_size(width, height);
375 IMB_scale(ibuf, width, height, IMBScaleFilter::Nearest, false);
376}
377
378static ImBuf *render_mask_thumb(Mask *mask, float frame_index)
379{
380 if (!mask) {
381 return nullptr;
382 }
383
384 BKE_mask_evaluate(mask, mask->sfra + frame_index, true);
385
386 constexpr int width = THUMB_SIZE;
387 constexpr int height = THUMB_SIZE;
388 Array<float> mask_buffer(width * height);
390 BKE_maskrasterize_handle_init(raster, mask, width, height, true, true, true);
391 BKE_maskrasterize_buffer(raster, width, height, mask_buffer.data());
393
394 ImBuf *ibuf = IMB_allocImBuf(
396 const float *src = mask_buffer.data();
397 uchar *dst = ibuf->byte_data_for_write();
398 for (int i = 0; i < width * height; i++) {
399 dst[0] = dst[1] = dst[2] = uchar(*src * 255.0f); /* already clamped */
400 dst[3] = 255;
401 src += 1;
402 dst += 4;
403 }
404 return ibuf;
405}
406
407/* Background job that processes in-flight thumbnail requests. */
409 Scene *scene_ = nullptr;
410 ThumbnailCache *cache_ = nullptr;
411
412 public:
413 ThumbGenerationJob(Scene *scene, ThumbnailCache *cache) : scene_(scene), cache_(cache) {}
414
415 static void ensure_job(const bContext *C, ThumbnailCache *cache);
416
417 private:
418 static void run_fn(void *customdata, wmJobWorkerStatus *worker_status);
419 static void end_fn(void *customdata);
420 static void free_fn(void *customdata);
421};
422
424{
426 wmWindow *win = CTX_wm_window(C);
428 wmJob *wm_job = WM_jobs_get(wm,
429 win,
430 scene,
431 "Generating strip thumbnails...",
434 if (!WM_jobs_is_running(wm_job)) {
435 ThumbGenerationJob *tj = MEM_new<ThumbGenerationJob>("ThumbGenerationJob", scene, cache);
436 WM_jobs_customdata_set(wm_job, tj, free_fn);
438 WM_jobs_callbacks(wm_job, run_fn, nullptr, nullptr, end_fn);
439
440 WM_jobs_start(wm, wm_job);
441 }
442}
443
444void ThumbGenerationJob::free_fn(void *customdata)
445{
446 ThumbGenerationJob *job = static_cast<ThumbGenerationJob *>(customdata);
447 MEM_delete(job);
448}
449
450/* Return the worker-owned, localized off-Main copy of the ID-based source for this request, taking
451 * ownership of a new copy (and freeing the previous one) when the source changes. Returns null if
452 * no copy is available (e.g. it was already consumed); the request is then skipped and will be
453 * re-created on the next redraw. */
455 const ThumbnailCache::Request &request,
456 ID *&cur_id_copy,
457 uint64_t &cur_source_generation)
458{
459 if (request.source_generation != cur_source_generation) {
460 if (cur_id_copy != nullptr) {
461 BKE_id_free(nullptr, cur_id_copy);
462 cur_id_copy = nullptr;
463 }
464 {
465 std::scoped_lock lock(thumb_cache_mutex);
466 cur_id_copy = cache->id_copies_.pop_default(request.source_generation, nullptr);
467 }
468 cur_source_generation = request.source_generation;
469 }
470 return cur_id_copy;
471}
472
473void ThumbGenerationJob::run_fn(void *customdata, wmJobWorkerStatus *worker_status)
474{
475#ifdef DEBUG_PRINT_THUMB_JOB_TIMES
476 clock_t t0 = clock();
477 std::atomic<int> total_thumbs = 0, total_images = 0, total_movies = 0;
478#endif
479
480 ThumbGenerationJob *job = static_cast<ThumbGenerationJob *>(customdata);
482 while (!worker_status->stop) {
483 /* Under cache mutex lock: copy all current requests into a vector for processing.
484 * NOTE: keep the requests set intact! We don't want to add new requests for same
485 * items while we are processing them. They will be removed from the set once
486 * they are finished, one by one. */
487 {
488 std::scoped_lock lock(thumb_cache_mutex);
489 requests.clear();
490 requests.reserve(job->cache_->requests_.size());
491 for (const auto &request : job->cache_->requests_) {
492 requests.append(request);
493 }
494 }
495
496 if (requests.is_empty()) {
497 break;
498 }
499
500 /* Sort requests by file, stream and increasing frame index. */
501 std::ranges::sort(requests,
502 [](const ThumbnailCache::Request &a, const ThumbnailCache::Request &b) {
503 if (a.source_key != b.source_key) {
504 return a.source_key < b.source_key;
505 }
506 if (a.stream_index != b.stream_index) {
507 return a.stream_index < b.stream_index;
508 }
509 return a.frame_index < b.frame_index;
510 });
511
512 /* Note: we could process thumbnail cache requests somewhat in parallel,
513 * but let's not do that so that UI responsiveness is not affected much.
514 * Some of video/image loading code parts are multi-threaded internally already,
515 * and that does provide some parallelism. */
516 {
517 /* Often the same movie file is chopped into multiple strips next to each other.
518 * Since the requests are sorted by file path and frame index, we can reuse MovieReader
519 * objects between them for performance. */
520 MovieReader *cur_anim = nullptr;
521 std::string cur_anim_path;
522 int cur_stream = 0;
523 IMB_Proxy_Size cur_proxy_size = IMB_PROXY_NONE;
524
525 /* For ID-based sources (movie clip, mask) we take ownership of the localized
526 * off-Main copy made on the main thread and reuse it across all of this source's frames. By
527 * popping it out of the cache's map, a concurrent cache clear/eviction can never free a copy
528 * we are still rendering from. The copy is freed when switching sources or at loop end. */
529 ID *cur_id_copy = nullptr;
530 uint64_t cur_source_generation = 0;
531 for (const ThumbnailCache::Request &request : requests) {
532 if (worker_status->stop) {
533 break;
534 }
535
536#ifdef DEBUG_PRINT_THUMB_JOB_TIMES
537 ++total_thumbs;
538#endif
539 ImBuf *thumb = nullptr;
540 if (request.strip_type == STRIP_TYPE_IMAGE) {
541 /* Load thumbnail for an image. */
542#ifdef DEBUG_PRINT_THUMB_JOB_TIMES
543 ++total_images;
544#endif
545 thumb = make_thumb_for_image(job->scene_, request);
546 }
547 else if (request.strip_type == STRIP_TYPE_MOVIE) {
548 /* Load thumbnail for a movie. */
549#ifdef DEBUG_PRINT_THUMB_JOB_TIMES
550 ++total_movies;
551#endif
552
553 /* Are we switching to a different movie file / stream? */
554 if (request.source_key.path != cur_anim_path || request.stream_index != cur_stream) {
555 if (cur_anim != nullptr) {
556 MOV_close(cur_anim);
557 cur_anim = nullptr;
558 }
559
560 cur_anim_path = request.source_key.path;
561 cur_stream = request.stream_index;
562 cur_anim = MOV_open_file(
563 cur_anim_path.c_str(), ImBufFlags::Zero, cur_stream, true, nullptr);
564 cur_proxy_size = IMB_PROXY_NONE;
565 if (cur_anim != nullptr) {
566 /* Find the lowest proxy resolution available.
567 * `x & -x` leaves only the lowest bit set. */
568 int proxies_mask = MOV_get_existing_proxies(cur_anim);
569 cur_proxy_size = IMB_Proxy_Size(proxies_mask & -proxies_mask);
570 }
571 }
572
573 /* Decode the movie frame. */
574 if (cur_anim != nullptr) {
575 thumb = MOV_decode_frame(cur_anim, request.frame_index, cur_proxy_size);
576 if (thumb == nullptr && cur_proxy_size != IMB_PROXY_NONE) {
577 /* Broken proxy file, switch to non-proxy. */
578 cur_proxy_size = IMB_PROXY_NONE;
579 thumb = MOV_decode_frame(cur_anim, request.frame_index, cur_proxy_size);
580 }
581 }
582 }
583 else if (request.strip_type == STRIP_TYPE_MOVIECLIP) {
584 /* Load thumbnail for a movie clip. */
585 MovieClip *clip = reinterpret_cast<MovieClip *>(
586 get_id_copy(job->cache_, request, cur_id_copy, cur_source_generation));
587 if (clip != nullptr) {
588 MovieClipUser clip_user = {};
589 BKE_movieclip_user_set_frame(&clip_user, request.frame_index + clip->start_frame);
590 const short build_sizes = clip->proxy.build_size_flag & 0x0F;
591 if ((clip->flag & MCLIP_USE_PROXY) && build_sizes != 0) {
592 /* Find the lowest proxy resolution available, or fall back to full. */
593 clip_user.render_size = eMovieClipProxy_RenderSize(bitscan_forward_i(build_sizes) +
594 1);
595 clip_user.render_flag = MCLIP_PROXY_RENDER_USE_FALLBACK_RENDER;
596 }
598 clip, &clip_user, MovieClipFlag(clip->flag), MovieClipCacheFlag::SkipCache);
599 }
600 }
601 else if (request.strip_type == STRIP_TYPE_MASK) {
602 /* Load thumbnail for a mask. */
603 Mask *mask = reinterpret_cast<Mask *>(
604 get_id_copy(job->cache_, request, cur_id_copy, cur_source_generation));
605 thumb = render_mask_thumb(mask, request.frame_index);
606 }
607 else {
609 }
610
612
613 /* Add result into the cache (under cache mutex lock). */
614 {
615 std::scoped_lock lock(thumb_cache_mutex);
616 ThumbnailCache::SourceEntry *val = job->cache_->map_.lookup_ptr(request.source_key);
617 if (val != nullptr && val->generation == request.source_generation) {
618 val->used_at = math::max(val->used_at, request.requested_at);
620 *val, request.frame_index, request.stream_index, thumb, request.requested_at);
621 }
622 else {
623 IMB_freeImBuf(thumb);
624 }
625 /* Remove the request from original set. */
626 job->cache_->requests_.remove(request);
627 }
628
629 if (thumb) {
630 worker_status->do_update = true;
631 }
632 }
633 if (cur_anim != nullptr) {
634 MOV_close(cur_anim);
635 cur_anim = nullptr;
636 }
637 if (cur_id_copy != nullptr) {
638 BKE_id_free(nullptr, cur_id_copy);
639 cur_id_copy = nullptr;
640 }
641 }
642 }
643
644#ifdef DEBUG_PRINT_THUMB_JOB_TIMES
645 clock_t t1 = clock();
646 printf("VSE thumb job: %i thumbs (%i img, %i movie) in %.3f sec\n",
647 total_thumbs.load(),
648 total_images.load(),
649 total_movies.load(),
650 double(t1 - t0) / CLOCKS_PER_SEC);
651#endif
652}
653
654void ThumbGenerationJob::end_fn(void *customdata)
655{
656 ThumbGenerationJob *job = static_cast<ThumbGenerationJob *>(customdata);
658}
659
661 const ThumbnailCache::SourceKey &key,
662 int frame_index,
663 float timeline_frame,
664 const bContext *C,
665 const Strip *strip)
666{
667 int64_t cur_time = cache.logical_time_;
668 ThumbnailCache::SourceEntry *val = cache.map_.lookup_ptr(key);
669
670 if (val == nullptr) {
671 /* Nothing in cache for this key yet. */
673 value.used_at = cur_time;
674 value.generation = cache.next_generation_++;
675 cache.map_.add_new(key, value);
676 val = cache.map_.lookup_ptr(key);
677 }
678 BLI_assert_msg(val != nullptr, "Thumbnail cache value should never be null here");
679
680 /* Search thumbnail entries of this file for closest match to the frame we want. */
681 int64_t best_index = -1;
682 int best_score = INT_MAX;
683 for (int64_t index = 0; index < val->frames.size(); index++) {
684 if (strip->streamindex != val->frames[index].stream_index) {
685 continue; /* Different video stream than what we need, ignore. */
686 }
687 int score = math::abs(frame_index - val->frames[index].frame_index);
688 if (score < best_score) {
689 best_score = score;
690 best_index = index;
691 if (score == 0) {
692 break;
693 }
694 }
695 }
696
697 /*
698 * Generates a new thumb if we can't find an exact match OR if we have one but its stale
699 */
700 const bool needs_new_thumb = (best_score > 0) ||
701 (best_index >= 0 && val->frames[best_index].stale);
702 if (needs_new_thumb && strip->type == STRIP_TYPE_SCENE) {
703 /* Add thumb generation request for a scene strip. */
704 ThumbnailCache::Request request(key,
705 frame_index,
706 strip->streamindex,
707 strip->type,
708 cur_time,
709 val->generation,
710 timeline_frame,
711 strip->channel);
712 request.scene_strip = strip;
713 cache.scene_requests_.add(request);
714 }
715 else if (needs_new_thumb) {
716 /* We do not have an exact frame match or we have a match that is stale, add a thumb generation
717 * request. */
718
719 /* For ID-based sources, make a copy of the ID so that the worker thread can safely access it.
720 * One copy per source is shared across all frame requests. Lifetime is handled in
721 * run_fn (worker takes ownership) and ThumbnailCache::clear(). */
722 const ID *source_id = nullptr;
723 if (strip->type == STRIP_TYPE_MOVIECLIP && strip->clip != nullptr) {
724 source_id = &strip->clip->id;
725 }
726 else if (strip->type == STRIP_TYPE_MASK && strip->mask != nullptr) {
727 source_id = &strip->mask->id;
728 }
729 if (source_id != nullptr) {
730 cache.id_copies_.lookup_or_add_cb(val->generation, [&]() {
731 return BKE_id_copy_ex(
732 nullptr, source_id, nullptr, LIB_ID_COPY_LOCALIZE | LIB_ID_COPY_NO_ANIMDATA);
733 });
734 }
735
736 ThumbnailCache::Request request(key,
737 frame_index,
738 strip->streamindex,
739 strip->type,
740 cur_time,
741 val->generation,
742 timeline_frame,
743 strip->channel);
744 cache.requests_.add(request);
746 }
747
748 if (best_index < 0) {
749 return nullptr;
750 }
751
752 /* Return the closest thumbnail fit we have so far. */
753 val->used_at = math::max(val->used_at, cur_time);
754 val->frames[best_index].used_at = math::max(val->frames[best_index].used_at, cur_time);
755 return val->frames[best_index].thumb;
756}
757
759 Scene *scene,
760 const Strip *strip,
761 float timeline_frame)
762{
763 if (!strip_can_have_thumbnail(scene, strip)) {
764 return nullptr;
765 }
766
767 timeline_frame = math::round(timeline_frame);
768
769 const ThumbnailCache::SourceKey key = get_key_from_strip(scene, strip, timeline_frame);
770 int frame_index = give_frame_index(scene, strip, timeline_frame);
772 frame_index += strip->anim_startofs;
773 }
774
775 ImBuf *res = nullptr;
776 {
777 std::scoped_lock lock(thumb_cache_mutex);
779 res = query_thumbnail(*cache, key, frame_index, timeline_frame, C, strip);
780 }
781
782 if (res) {
783 IMB_refImBuf(res);
784 }
785 return res;
786}
787
789{
790 if (scene == nullptr || scene->ed == nullptr) {
791 return false;
792 }
793 std::scoped_lock lock(thumb_cache_mutex);
795 return cache != nullptr && !cache->scene_requests_.is_empty();
796}
797
799{
800 if (scene == nullptr || scene->ed == nullptr) {
801 return false;
802 }
803
804 /* Pop a single scene strip thumbnail request out of the queue (we are rendering
805 * just one scene thumbnail per draw to keep UI responsive). */
806 const Strip *strip = nullptr;
808 int frame_index = 0;
809 uint64_t source_generation = 0;
810 bool more_pending = false;
811 {
812 std::scoped_lock lock(thumb_cache_mutex);
814 if (cache == nullptr || cache->scene_requests_.is_empty()) {
815 return false;
816 }
817 auto first_request = cache->scene_requests_.begin();
818 strip = first_request->scene_strip;
819 key = first_request->source_key;
820 frame_index = first_request->frame_index;
821 source_generation = first_request->source_generation;
822 cache->scene_requests_.remove(first_request);
823 more_pending = !cache->scene_requests_.is_empty();
824 }
825
826 /* The strip pointer in the thumbnail request might be stale at this point.
827 * Before accessing it, validate that we still have that scene strip. */
828 Main *bmain = CTX_data_main(C);
829 Scene *scene_from_uid = reinterpret_cast<Scene *>(
831 Span<const Strip *> scene_strips = lookup_strips_by_scene(scene->ed, scene_from_uid);
832
833 bool rendered_thumb = false;
834 if (strip != nullptr && scene_strips.contains(strip) && strip->type == STRIP_TYPE_SCENE &&
835 strip->scene != nullptr)
836 {
837 /* Render the strip thumbnail, outside of the cache lock. */
839 bmain, scene, strip, float(frame_index), THUMB_SIZE);
840 if (thumb != nullptr) {
842
843 /* Add to thumbnail cache. */
844 std::scoped_lock lock(thumb_cache_mutex);
846 ThumbnailCache::SourceEntry *val = cache ? cache->map_.lookup_ptr(key) : nullptr;
847 if (val == nullptr || val->generation != source_generation) {
848 /* Cache entry vanished or the source changed while rendering; drop the result. */
849 IMB_freeImBuf(thumb);
850 }
851 else {
852 val->used_at = math::max(val->used_at, cache->logical_time_);
853 entry_add_or_replace_frame(*val, frame_index, 0, thumb, cache->logical_time_);
854 rendered_thumb = true;
855 }
856 }
857 }
858
859 return rendered_thumb || more_pending;
860}
861
862/* Instead of removing thumbnails when cache is invalidated we mark them as stale
863 * instead, so they keep being displayed until a newly generated thumbnail replaces
864 * them (see #161249). */
866{
867 if (!strip_can_have_thumbnail(scene, strip)) {
868 return;
869 }
870
871 bool invalidated = false;
872 {
873 std::scoped_lock lock(thumb_cache_mutex);
875 if (cache != nullptr) {
877 const StripElem *elem = strip->data->stripdata;
878 if (elem != nullptr) {
879 int paths_count = 1;
880 if (strip->type == STRIP_TYPE_IMAGE) {
881 /* Image strip has array of file names. */
882 paths_count = strip->data->stripdata_num;
883 }
884 char filepath[FILE_MAX];
885 const char *basepath = ID_BLEND_PATH_FROM_GLOBAL(&scene->id);
886 for (int i = 0; i < paths_count; i++, elem++) {
887 BLI_path_join(filepath, sizeof(filepath), strip->data->dirpath, elem->filename);
888 BLI_path_abs(filepath, basepath);
889 invalidated |= cache->invalidate_entry(ThumbnailCache::SourceKey(filepath));
890 }
891 }
892 }
893 else if (strip->type == STRIP_TYPE_MOVIECLIP && strip->clip) {
894 invalidated |= cache->invalidate_entry(ThumbnailCache::SourceKey(&strip->clip->id));
895 }
896 else if (strip->type == STRIP_TYPE_MASK && strip->mask) {
897 invalidated |= cache->invalidate_entry(ThumbnailCache::SourceKey(&strip->mask->id));
898 }
899 else if (strip->type == STRIP_TYPE_SCENE && strip->scene) {
900 invalidated |= cache->invalidate_entry(get_key_from_scene_strip(strip));
901 }
902 }
903 }
904
905 if (invalidated) {
907 }
908}
909
911{
912 std::scoped_lock lock(thumb_cache_mutex);
914 if (cache != nullptr) {
915 cache->logical_time_++;
916
917 /* Count total number of thumbnails, and track which one is the least recently used file. */
918 int64_t entries = 0;
919 ThumbnailCache::SourceKey oldest_key;
920 /* Do not remove thumbnails for files used within last 10 updates. */
921 int64_t oldest_time = cache->logical_time_ - 10;
922 int64_t oldest_entries = 0;
923 for (const auto &item : cache->map_.items()) {
924 entries += item.value.frames.size();
925 if (item.value.used_at < oldest_time) {
926 oldest_key = item.key;
927 oldest_time = item.value.used_at;
928 oldest_entries = item.value.frames.size();
929 }
930 }
931
932 /* If we're beyond capacity and have a long-unused file, remove that. */
933 if (entries > MAX_THUMBNAILS && oldest_key.is_valid()) {
934 cache->remove_entry(oldest_key);
935 entries -= oldest_entries;
936 }
937
938 /* If we're still beyond capacity, remove individual long-unused (but not within
939 * last 100 updates) individual frames. */
940 if (entries > MAX_THUMBNAILS) {
941 for (const auto &item : cache->map_.items()) {
942 for (int64_t i = 0; i < item.value.frames.size(); i++) {
943 if (item.value.frames[i].used_at < cache->logical_time_ - 100) {
944 IMB_freeImBuf(item.value.frames[i].thumb);
945 item.value.frames.remove_and_reorder(i);
946 i--;
947 }
948 }
949 }
950 }
951 }
952}
953
955{
956 std::scoped_lock lock(thumb_cache_mutex);
958 if (cache != nullptr) {
959 const auto is_outside = [&](const ThumbnailCache::Request &request) {
960 return request.timeline_frame < rect.xmin || request.timeline_frame > rect.xmax ||
961 request.channel < rect.ymin || request.channel > rect.ymax;
962 };
963 cache->requests_.remove_if(is_outside);
964 cache->scene_requests_.remove_if(is_outside);
965 }
966}
967
969{
970 std::scoped_lock lock(thumb_cache_mutex);
972 if (cache != nullptr) {
973 scene->ed->runtime->thumbnail_cache->clear();
974 }
975}
976
978{
979 std::scoped_lock lock(thumb_cache_mutex);
981 if (cache != nullptr) {
982 BLI_assert(cache == scene->ed->runtime->thumbnail_cache);
983 MEM_delete(scene->ed->runtime->thumbnail_cache);
984 scene->ed->runtime->thumbnail_cache = nullptr;
985 }
986}
987
988} // namespace blender::seq
#define FILE_MAX
#define BLI_path_join(...)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
#define C
Definition RandGen.cpp:29
#define ND_SEQUENCER
Definition WM_types.hh:456
#define NC_SCENE
Definition WM_types.hh:391
volatile int lock
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
const T * data() const
Definition BLI_array.hh:323
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
ValueIterator values() const &
Definition BLI_map.hh:884
void remove_contained(const Key &key)
Definition BLI_map.hh:387
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
ItemIterator items() const &
Definition BLI_map.hh:902
int64_t remove_if(Predicate &&predicate)
Definition BLI_set.hh:534
void clear()
Definition BLI_set.hh:570
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
void append(const T &value)
bool is_empty() const
void reserve(const int64_t min_capacity)
const T & data(const Action &owning_action) const
static void ensure_job(const bContext *C, ThumbnailCache *cache)
ThumbGenerationJob(Scene *scene, ThumbnailCache *cache)
#define INT_MAX
T max(const T &a, const T &b)
T abs(const T &a)
T round(const T &a)
void ensure_ibuf_is_sequencer_space(const Scene *scene, ImBuf *ibuf, bool make_float)
Definition render.cc:170
static ThumbnailCache::SourceKey get_key_from_strip(const Scene *scene, const Strip *strip, float timeline_frame)
ImBuf * render_scene_strip_thumbnail(Main *bmain, Scene *timeline_scene, const Strip *strip, float frame_index, int size)
Definition render.cc:1695
void thumbnail_cache_invalidate_strip(Scene *scene, const Strip *strip)
Span< Strip * > lookup_strips_by_scene(Editing *ed, const Scene *key)
float give_frame_index(const Scene *scene, const Strip *strip, float timeline_frame)
Definition strip_time.cc:43
bool strip_can_have_thumbnail(const Scene *scene, const Strip *strip)
void thumbnail_cache_clear(Scene *scene)
static ImBuf * make_thumb_for_image(const Scene *scene, const ThumbnailCache::Request &request)
static ImBuf * render_mask_thumb(Mask *mask, float frame_index)
ImBuf * thumbnail_cache_get(const bContext *C, Scene *scene, const Strip *strip, float timeline_frame)
static ImBuf * query_thumbnail(ThumbnailCache &cache, const ThumbnailCache::SourceKey &key, int frame_index, float timeline_frame, const bContext *C, const Strip *strip)
static Mutex thumb_cache_mutex
static ThumbnailCache::SourceKey get_key_from_scene_strip(const Strip *strip)
static constexpr int MAX_THUMBNAILS
void image_size_to_thumb_size(int &r_width, int &r_height, int size)
static void scale_to_thumbnail_size(ImBuf *ibuf)
static void entry_add_or_replace_frame(ThumbnailCache::SourceEntry &entry, int frame_index, int stream_index, ImBuf *thumb, int64_t used_at)
void thumbnail_cache_discard_requests_outside(Scene *scene, const rctf &rect)
static ID * get_id_copy(ThumbnailCache *cache, const ThumbnailCache::Request &request, ID *&cur_id_copy, uint64_t &cur_source_generation)
static ThumbnailCache * query_thumbnail_cache(Scene *scene)
static ThumbnailCache * ensure_thumbnail_cache(Scene *scene)
void thumbnail_cache_destroy(Scene *scene)
bool thumbnail_cache_update_scene_thumbs(const bContext *C, Scene *scene)
bool thumbnail_cache_has_pending_scene_requests(Scene *scene)
StripElem * render_give_stripelem(const Scene *scene, const Strip *strip, int timeline_frame)
Definition render.cc:220
void thumbnail_cache_maintain_capacity(Scene *scene)
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
void IMB_free_byte_pixels(ImBuf *ibuf)
Scene * CTX_data_sequencer_scene(const bContext *C)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
MaskRasterHandle * BKE_maskrasterize_handle_new()
unsigned int uint
Definition BKE_text.h:55
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
MINLINE int round_fl_to_int(float a)
ID * BKE_libblock_find_session_uid(Main *bmain, short type, uint32_t session_uid)
Definition lib_id.cc:1826
void MOV_close(MovieReader *anim)
Definition movie_read.cc:70
@ WM_JOB_TYPE_SEQ_DRAW_THUMBNAIL
Definition WM_api.hh:2047
ImBuf * MOV_decode_frame(MovieReader *anim, int frame_index, IMB_Proxy_Size preview_size)
void IMB_refImBuf(ImBuf *ibuf)
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
@ MCLIP_PROXY_RENDER_USE_FALLBACK_RENDER
@ WM_JOB_BACKGROUND
Definition WM_api.hh:1999
void BKE_mask_evaluate(Mask *mask, float ctime, bool do_newframe)
MINLINE int bitscan_forward_i(int a)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
unsigned int uint32_t
Definition BKE_text.h:81
MovieReader * MOV_open_file(const char *filepath, const ImBufFlags ib_flags, const int streamindex, const bool keep_original_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
wmWindowManager * CTX_wm_manager(const bContext *C)
void BKE_maskrasterize_handle_init(MaskRasterHandle *mr_handle, Mask *mask, int width, int height, bool do_aspect_correct, bool do_mask_aa, bool do_feather)
void BKE_maskrasterize_handle_free(MaskRasterHandle *mr_handle)
void BKE_id_free(Main *bmain, void *idv)
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
Main * CTX_data_main(const bContext *C)
void IMB_byte_from_float(ImBuf *ibuf)
ImBuf * BKE_movieclip_get_ibuf_flag(MovieClip *clip, const MovieClipUser *user, MovieClipFlag flag, MovieClipCacheFlag cache_flag)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
std::mutex Mutex
Definition BLI_mutex.hh:51
int MOV_get_existing_proxies(const MovieReader *anim)
constexpr uint64_t hash_string(StringRef str)
Definition BLI_hash.hh:183
ImBuf * IMB_thumb_load_image(const char *filepath, size_t max_thumb_size, ColorManagedColorspaceSettings *colorspace_settings, IMBThumbLoadFlags load_flags=IMBThumbLoadFlags::Zero)
Definition readimage.cc:226
void BKE_maskrasterize_buffer(MaskRasterHandle *mr_handle, unsigned int width, unsigned int height, float *buffer)
Rasterize a buffer from a single mask (threaded execution).
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
void IMB_free_float_pixels(ImBuf *ibuf)
bool WM_jobs_is_running(const wmJob *wm_job)
Definition wm_jobs.cc:358
void mask(PageMask &srt, TileMaps &tilemaps, const uint3 global_id, const uint local_tile_index)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
seq::EditingRuntime * runtime
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
struct Editing * ed
Request(const SourceKey &source_key, int frame, int stream, StripType type, int64_t logical_time, uint64_t source_generation, float time_frame, int ch)
friend bool operator==(const Request &a, const Request &b)
friend auto operator<=>(const SourceKey &a, const SourceKey &b)=default
bool remove_entry(const SourceKey &key)
bool invalidate_entry(const SourceKey &key)
Map< SourceKey, SourceEntry > map_
Map< uint64_t, ID * > id_copies_