Blender V5.3
final_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_hash.hh"
10#include "BLI_map.hh"
11#include "BLI_mutex.hh"
12
13#include "DNA_scene_types.h"
14#include "DNA_sequence_types.h"
15
16#include "BKE_scene.hh"
17
18#include "IMB_imbuf.hh"
19
20#include "SEQ_relations.hh"
21#include "SEQ_sequencer.hh"
22
23#include "final_image_cache.hh"
24#include "prefetch.hh"
25
26namespace blender::seq {
27
29
31 struct Key {
36
41
42 bool operator==(const Key &other) const
43 {
44 return timeline_frame == other.timeline_frame && view_id == other.view_id &&
46 }
47 };
49
51 {
52 clear();
53 }
54
55 void clear()
56 {
57 for (ImBuf *item : map_.values()) {
58 IMB_freeImBuf(item);
59 }
60 map_.clear();
61 }
62};
63
65{
66 FinalImageCache **cache = &scene->ed->runtime->final_image_cache;
67 if (*cache == nullptr) {
68 *cache = MEM_new<FinalImageCache>(__func__);
69 }
70 return *cache;
71}
72
74{
75 if (scene == nullptr || scene->ed == nullptr) {
76 return nullptr;
77 }
78 return scene->ed->runtime->final_image_cache;
79}
80
82 float timeline_frame,
83 int view_id,
84 int display_channel,
85 int2 image_size,
86 bool is_render)
87{
88 if (is_render) {
89 return nullptr;
90 }
91
92 const FinalImageCache::Key key = {
93 int(math::round(timeline_frame)), view_id, display_channel, image_size};
94
95 ImBuf *res = nullptr;
96 {
97 std::lock_guard lock(final_image_cache_mutex);
99 if (cache == nullptr) {
100 return nullptr;
101 }
102 res = cache->map_.lookup_default(key, nullptr);
103 }
104
105 if (res) {
106 IMB_refImBuf(res);
107 }
108 return res;
109}
110
112 float timeline_frame,
113 int view_id,
114 int display_channel,
115 int2 image_size,
116 bool is_render,
117 ImBuf *image)
118{
119 if (is_render) {
120 return;
121 }
122
123 const FinalImageCache::Key key = {
124 int(math::round(timeline_frame)), view_id, display_channel, image_size};
125
127
128 std::lock_guard lock(final_image_cache_mutex);
130
131 cache->map_.add_or_modify(
132 key,
133 [&](ImBuf **value) { *value = image; },
134 [&](ImBuf **existing) {
135 if (*existing) {
136 IMB_freeImBuf(*existing);
137 }
138 *existing = image;
139 });
140}
141
143 const float timeline_frame_start,
144 const float timeline_frame_end)
145{
146 std::lock_guard lock(final_image_cache_mutex);
148 if (cache == nullptr) {
149 return;
150 }
151
152 const int key_start = int(math::floor(timeline_frame_start));
153 const int key_end = int(math::ceil(timeline_frame_end));
154
155 for (auto it = cache->map_.items().begin(); it != cache->map_.items().end(); it++) {
156 const int key = (*it).key.timeline_frame;
157 if (key >= key_start && key <= key_end) {
158 IMB_freeImBuf((*it).value);
159 cache->map_.remove(it);
160 }
161 }
162}
163
165{
166 std::lock_guard lock(final_image_cache_mutex);
168 if (cache != nullptr) {
169 scene->ed->runtime->final_image_cache->clear();
170 }
171}
172
174{
175 std::lock_guard lock(final_image_cache_mutex);
177 if (cache != nullptr) {
178 BLI_assert(cache == scene->ed->runtime->final_image_cache);
179 MEM_delete(scene->ed->runtime->final_image_cache);
180 scene->ed->runtime->final_image_cache = nullptr;
181 }
182}
183
185 void *userdata,
186 void callback_iter(void *userdata, int timeline_frame))
187{
188 std::lock_guard lock(final_image_cache_mutex);
190 if (cache == nullptr) {
191 return;
192 }
193 for (const FinalImageCache::Key &frame_view : cache->map_.keys()) {
194 callback_iter(userdata, frame_view.timeline_frame);
195 }
196}
197
199{
200 std::lock_guard lock(final_image_cache_mutex);
202 if (cache == nullptr) {
203 return 0;
204 }
205 size_t size = 0;
206 for (ImBuf *frame : cache->map_.values()) {
207 size += IMB_get_size_in_memory(frame);
208 }
209 return size;
210}
211
213{
214 std::lock_guard lock(final_image_cache_mutex);
216 if (cache == nullptr) {
217 return 0;
218 }
219 return cache->map_.size();
220}
221
223{
224 std::lock_guard lock(final_image_cache_mutex);
226 if (cache == nullptr) {
227 return false;
228 }
229
230 /* Find which entry to remove -- we pick the one that is furthest from the current frame,
231 * biasing the ones that are behind the current frame.
232 *
233 * However, do not try to evict entries from the current prefetch job range -- we need to
234 * be able to fully fill the cache from prefetching, and then actually stop the job when it
235 * is full and no longer can evict anything. */
236 int cur_prefetch_start = std::numeric_limits<int>::min();
237 int cur_prefetch_end = std::numeric_limits<int>::min();
239 /* Only activate the prefetch guards if the cache is active. */
240 seq_prefetch_get_time_range(scene, &cur_prefetch_start, &cur_prefetch_end);
241 }
242 const bool prefetch_loops_around = cur_prefetch_start > cur_prefetch_end;
243
244 const int timeline_start = scene->playback_start();
245 const int timeline_end = scene->playback_end();
246 /* If we wrap around, treat the timeline start as the playback head position.
247 * This is to try to mitigate un-needed cache evictions. */
248 const int cur_frame = prefetch_loops_around ? timeline_start : scene->r.cfra;
249
250 FinalImageCache::Key best_key = {};
251 ImBuf *best_item = nullptr;
252 int best_score = 0;
253 for (const auto &item : cache->map_.items()) {
254 const int item_frame = item.key.timeline_frame;
255 if (prefetch_loops_around) {
256 if (item_frame >= timeline_start && item_frame <= cur_prefetch_end) {
257 continue; /* Within active prefetch range, do not try to remove it. */
258 }
259 if (item_frame >= cur_prefetch_start && item_frame <= timeline_end) {
260 continue; /* Within active prefetch range, do not try to remove it. */
261 }
262 }
263 else if (item_frame >= cur_prefetch_start && item_frame <= cur_prefetch_end) {
264 continue; /* Within active prefetch range, do not try to remove it. */
265 }
266
267 /* Score for removal is distance to current frame; 2x that if behind current frame. */
268 int score = 0;
269 if (item_frame < cur_frame) {
270 score = (cur_frame - item_frame) * 2;
271 }
272 else if (item_frame > cur_frame) {
273 score = item_frame - cur_frame;
274 }
275 if (score > best_score) {
276 best_key = item.key;
277 best_item = item.value;
278 best_score = score;
279 }
280 }
281
282 /* Remove if we found one. */
283 if (best_item != nullptr) {
284 IMB_freeImBuf(best_item);
285 cache->map_.remove(best_key);
286 return true;
287 }
288
289 /* Did not find anything to remove. */
290 return false;
291}
292
293} // namespace blender::seq
volatile int lock
void clear()
Definition BLI_map.hh:1038
ValueIterator values() const &
Definition BLI_map.hh:884
T floor(const T &a)
T ceil(const T &a)
T round(const T &a)
static Mutex final_image_cache_mutex
size_t final_image_cache_calc_memory_size(const Scene *scene)
static FinalImageCache * ensure_final_image_cache(Scene *scene)
void final_image_cache_invalidate_frame_range(Scene *scene, const float timeline_frame_start, const float timeline_frame_end)
void final_image_cache_iterate(Scene *scene, void *userdata, void callback_iter(void *userdata, int timeline_frame))
void final_image_cache_destroy(Scene *scene)
ImBuf * final_image_cache_get(Scene *scene, float timeline_frame, int view_id, int display_channel, int2 image_size, bool is_render)
void final_image_cache_clear(Scene *scene)
void final_image_cache_put(Scene *scene, float timeline_frame, int view_id, int display_channel, int2 image_size, bool is_render, ImBuf *image)
bool final_image_cache_evict(Scene *scene)
size_t final_image_cache_get_image_count(const Scene *scene)
static FinalImageCache * query_final_image_cache(const 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)
VecBase< int32_t, 2 > int2
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
FinalImageCache * final_image_cache
bool operator==(const Key &other) const