Blender V5.3
source_image_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_map.hh"
10#include "BLI_mutex.hh"
11
12#include "DNA_scene_types.h"
13#include "DNA_sequence_types.h"
14
15#include "BKE_scene.hh"
16
17#include "IMB_imbuf.hh"
18
19#include "SEQ_relations.hh"
20#include "SEQ_render.hh"
21#include "SEQ_sequencer.hh"
22#include "SEQ_time.hh"
23
24#include "prefetch.hh"
25#include "source_image_cache.hh"
26
27namespace blender::seq {
28
30
32 struct FrameEntry {
40 float strip_frame = 0;
41 };
42
43 struct Key {
44 float source_frame = 0.0f;
45 int view_id = 0;
47
52
53 friend bool operator==(const Key &a, const Key &b) = default;
54 };
55
59
61
63 {
64 clear();
65 }
66
67 void clear()
68 {
69 for (const auto &item : map_.items()) {
70 for (const auto &frame : item.value.frames.values()) {
71 IMB_freeImBuf(frame.image.image);
72 }
73 }
74 map_.clear();
75 }
76
77 void remove_entry(const Strip *strip)
78 {
79 StripEntry *entry = map_.lookup_ptr(strip);
80 if (entry == nullptr) {
81 return;
82 }
83 for (const auto &frame : entry->frames.values()) {
84 IMB_freeImBuf(frame.image.image);
85 }
87 }
88};
89
91{
93 if (*cache == nullptr) {
94 *cache = MEM_new<SourceImageCache>(__func__);
95 }
96 return *cache;
97}
98
100{
101 if (scene == nullptr || scene->ed == nullptr) {
102 return nullptr;
103 }
104 return scene->ed->runtime->source_image_cache;
105}
106
107static float give_cache_frame_index(const Scene *scene, const Strip *strip, float timeline_frame)
108{
109 float frame_index = give_frame_index(scene, strip, timeline_frame);
110 if (strip->type != STRIP_TYPE_SCENE) {
111 /* Scene strips that are slowed down need fractional frame index for animation interpolation;
112 * for others use integer index for better cache hit rates. */
113 frame_index = std::trunc(frame_index);
114 }
115 if (strip->type == STRIP_TYPE_MOVIE) {
116 frame_index += strip->anim_startofs;
117 }
118 return frame_index;
119}
120
122 const Scene *scene,
123 const Strip *strip,
124 float timeline_frame)
125{
126 const float frame_index = give_cache_frame_index(scene, strip, timeline_frame);
127 eDrawType draw_type = OB_RENDER;
128 if (!context->render && strip->type == STRIP_TYPE_SCENE) {
129 draw_type = eDrawType(scene->r.seq_prev_type);
130 }
131 return {frame_index, context->view_id, draw_type};
132}
133
135 const Strip *strip,
136 float timeline_frame)
137{
138 if (context->skip_cache || strip == nullptr) {
139 return {};
140 }
141
142 Scene *scene = prefetch_get_original_scene_and_strip(context, strip);
143 timeline_frame = math::round(timeline_frame);
144 const SourceImageCache::Key key = get_key(context, scene, strip, timeline_frame);
145
146 SeqResult res;
147 {
148 std::lock_guard lock(source_image_cache_mutex);
150 if (cache == nullptr) {
151 return res;
152 }
153
154 SourceImageCache::StripEntry *val = cache->map_.lookup_ptr(strip);
155 if (val == nullptr) {
156 /* Nothing in cache for this strip yet. */
157 return res;
158 }
159 /* Search entries for the frame we want. */
160 SourceImageCache::FrameEntry *frame = val->frames.lookup_ptr(key);
161 if (frame != nullptr) {
162 res = frame->image;
163 }
164
165 /* For effect, meta, and scene strips, check if the cached result matches our current
166 * render resolution. If it does not, remove stale source entries for this strip. */
167 if (res.is_valid() &&
168 (strip->is_effect() || strip->type == STRIP_TYPE_SCENE || strip->type == STRIP_TYPE_META))
169 {
170 if (res.image->x != context->rectx || res.image->y != context->recty) {
171 cache->remove_entry(strip);
172 return {};
173 }
174 }
175 }
176
177 if (res.is_valid()) {
178 IMB_refImBuf(res.image);
179 }
180 return res;
181}
182
184 const Strip *strip,
185 float timeline_frame,
186 const SeqResult &image)
187{
188 if (context->skip_cache || strip == nullptr || !image.is_valid()) {
189 return;
190 }
191
192 Scene *scene = prefetch_get_original_scene_and_strip(context, strip);
193 timeline_frame = math::round(timeline_frame);
194 const SourceImageCache::Key key = get_key(context, scene, strip, timeline_frame);
195
196 IMB_refImBuf(image.image);
197
198 std::lock_guard lock(source_image_cache_mutex);
200
201 SourceImageCache::StripEntry *val = cache->map_.lookup_ptr(strip);
202
203 if (val == nullptr) {
204 /* Nothing in cache for this strip yet. */
205 cache->map_.add_new(strip, {});
206 val = cache->map_.lookup_ptr(strip);
207 }
208 BLI_assert_msg(val != nullptr, "Source image cache value should never be null here");
209
210 SourceImageCache::FrameEntry &frame = val->frames.lookup_or_add_default(key);
211 if (frame.image.is_valid()) {
213 }
214 frame.strip_frame = timeline_frame - strip->start;
215 frame.image = image;
216}
217
219{
220 std::lock_guard lock(source_image_cache_mutex);
222 if (cache != nullptr) {
223 cache->remove_entry(strip);
224 }
225}
226
228{
229 std::lock_guard lock(source_image_cache_mutex);
231 if (cache != nullptr) {
233 }
234}
235
237{
238 std::lock_guard lock(source_image_cache_mutex);
240 if (cache != nullptr) {
241 BLI_assert(cache == scene->ed->runtime->source_image_cache);
242 MEM_delete(scene->ed->runtime->source_image_cache);
243 scene->ed->runtime->source_image_cache = nullptr;
244 }
245}
246
248 void *userdata,
249 void callback_iter(void *userdata,
250 const Strip *strip,
251 int timeline_frame))
252{
253 std::lock_guard lock(source_image_cache_mutex);
255 if (cache == nullptr) {
256 return;
257 }
258
259 for (const auto &[strip, frames] : cache->map_.items()) {
260 for (const auto &[frame_key, frame] : frames.frames.items()) {
261 const float timeline_frame = strip->start + frame.strip_frame;
262 callback_iter(userdata, strip, int(timeline_frame));
263 }
264 }
265}
266
268{
269 std::lock_guard lock(source_image_cache_mutex);
271 if (cache == nullptr) {
272 return 0;
273 }
274 size_t size = 0;
275 for (const SourceImageCache::StripEntry &entry : cache->map_.values()) {
276 for (const SourceImageCache::FrameEntry &frame : entry.frames.values()) {
277 if (frame.image.is_valid()) {
278 size += IMB_get_size_in_memory(frame.image.image);
279 }
280 }
281 }
282 return size;
283}
284
286{
287 std::lock_guard lock(source_image_cache_mutex);
289 if (cache == nullptr) {
290 return 0;
291 }
292 size_t count = 0;
293 for (const SourceImageCache::StripEntry &entry : cache->map_.values()) {
294 count += entry.frames.size();
295 }
296 return count;
297}
298
300{
301 std::lock_guard lock(source_image_cache_mutex);
303 if (cache == nullptr) {
304 return false;
305 }
306 /* Find which entry to remove -- we pick the one that is furthest from the current frame,
307 * biasing the ones that are behind the current frame.
308 *
309 * However, do not try to evict entries from the current prefetch job range -- we need to
310 * be able to fully fill the cache from prefetching, and then actually stop the job when it
311 * is full and no longer can evict anything. */
312 int cur_prefetch_start = std::numeric_limits<int>::min();
313 int cur_prefetch_end = std::numeric_limits<int>::min();
314 if (scene->ed->cache_flag & SEQ_CACHE_STORE_RAW) {
315 /* Only activate the prefetch guards if the cache is active. */
316 seq_prefetch_get_time_range(scene, &cur_prefetch_start, &cur_prefetch_end);
317 }
318 const bool prefetch_loops_around = cur_prefetch_start > cur_prefetch_end;
319
320 const int timeline_start = scene->playback_start();
321 const int timeline_end = scene->playback_end();
322 /* If we wrap around, treat the timeline start as the playback head position.
323 * This is to try to mitigate un-needed cache evictions. */
324 const int cur_frame = prefetch_loops_around ? timeline_start : scene->r.cfra;
325
326 SourceImageCache::StripEntry *best_strip = nullptr;
327 SourceImageCache::Key best_key;
328 int best_score = 0;
329 for (const auto &strip : cache->map_.items()) {
330 for (const auto &entry : strip.value.frames.items()) {
331 const int item_frame = int(strip.key->start + entry.value.strip_frame);
332 if (prefetch_loops_around) {
333 if (item_frame >= timeline_start && item_frame <= cur_prefetch_end) {
334 continue; /* Within active prefetch range, do not try to remove it. */
335 }
336 if (item_frame >= cur_prefetch_start && item_frame <= timeline_end) {
337 continue; /* Within active prefetch range, do not try to remove it. */
338 }
339 }
340 else if (item_frame >= cur_prefetch_start && item_frame <= cur_prefetch_end) {
341 continue; /* Within active prefetch range, do not try to remove it. */
342 }
343
344 /* Score for removal is distance to current frame; 2x that if behind current frame. */
345 int score = 0;
346 if (item_frame < cur_frame) {
347 score = (cur_frame - item_frame) * 2;
348 }
349 else if (item_frame > cur_frame) {
350 score = item_frame - cur_frame;
351 }
352 if (score > best_score) {
353 best_strip = &strip.value;
354 best_key = entry.key;
355 best_score = score;
356 }
357 }
358 }
359
360 /* Remove if we found one. */
361 if (best_strip != nullptr) {
362 IMB_freeImBuf(best_strip->frames.lookup(best_key).image.image);
363 best_strip->frames.remove(best_key);
364 return true;
365 }
366
367 return false;
368}
369
370} // namespace blender::seq
volatile int lock
#define BLI_assert_msg(a, msg)
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
void remove_contained(const Key &key)
Definition BLI_map.hh:387
ItemIterator items() const &
Definition BLI_map.hh:902
T round(const T &a)
static SourceImageCache::Key get_key(const RenderData *context, const Scene *scene, const Strip *strip, float timeline_frame)
void source_image_cache_put(const RenderData *context, const Strip *strip, float timeline_frame, const SeqResult &image)
static float give_cache_frame_index(const Scene *scene, const Strip *strip, float timeline_frame)
void source_image_cache_iterate(Scene *scene, void *userdata, void callback_iter(void *userdata, const Strip *strip, int timeline_frame))
float give_frame_index(const Scene *scene, const Strip *strip, float timeline_frame)
Definition strip_time.cc:43
bool source_image_cache_evict(Scene *scene)
void source_image_cache_clear(Scene *scene)
void source_image_cache_invalidate_strip(Scene *scene, const Strip *strip)
size_t source_image_cache_get_image_count(const Scene *scene)
SeqResult source_image_cache_get(const RenderData *context, const Strip *strip, float timeline_frame)
static SourceImageCache * query_source_image_cache(const Scene *scene)
Scene * prefetch_get_original_scene_and_strip(const RenderData *context, const Strip *&strip)
Definition prefetch.cc:188
void source_image_cache_destroy(Scene *scene)
static Mutex source_image_cache_mutex
size_t source_image_cache_calc_memory_size(const Scene *scene)
static SourceImageCache * ensure_source_image_cache(Scene *scene)
void seq_prefetch_get_time_range(Scene *scene, int *r_start, int *r_end)
Definition prefetch.cc:222
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void IMB_refImBuf(ImBuf *ibuf)
size_t IMB_get_size_in_memory(const ImBuf *ibuf)
void IMB_freeImBuf(ImBuf *ibuf)
std::mutex Mutex
Definition BLI_mutex.hh:51
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
seq::EditingRuntime * runtime
eEditingCacheFlag cache_flag
struct Editing * ed
struct RenderData r
SourceImageCache * source_image_cache
bool is_valid() const
Definition render.hh:54
friend bool operator==(const Key &a, const Key &b)=default
void remove_entry(const Strip *strip)
Map< const Strip *, StripEntry > map_