Blender V5.3
preview_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 "DNA_scene_types.h"
10#include "DNA_sequence_types.h"
11
12#include "GPU_texture.hh"
13
14#include "SEQ_preview_cache.hh"
15#include "SEQ_sequencer.hh"
16
17namespace blender::seq {
18
22 int view_id = -1;
24 int width = -1;
25 int height = -1;
26
29
30 void clear()
31 {
32 last_used = -1;
33 timeline_frame = -1;
34 width = -1;
35 height = -1;
38 }
39};
40
42 static constexpr int cache_size = 4;
45
47 {
48 clear();
49 }
50
51 void clear()
52 {
53 for (PreviewCacheItem &item : this->items) {
54 item.clear();
55 }
56 }
57};
58
60{
61 if (scene == nullptr || scene->ed == nullptr) {
62 return nullptr;
63 }
64 return scene->ed->runtime->preview_cache;
65}
66
68{
69 if (scene == nullptr || scene->ed == nullptr) {
70 return nullptr;
71 }
72 PreviewCache *&cache = scene->ed->runtime->preview_cache;
73 if (cache == nullptr) {
74 cache = MEM_new<PreviewCache>(__func__);
75 }
76 return cache;
77}
78
80 Scene *scene, int timeline_frame, int view_id, int display_channel, int width, int height)
81{
82 PreviewCache *cache = query_preview_cache(scene);
83 if (cache == nullptr) {
84 return nullptr;
85 }
86 cache->tick_count++;
87 for (PreviewCacheItem &item : cache->items) {
88 if (item.timeline_frame == timeline_frame && item.view_id == view_id &&
89 item.display_channel == display_channel && item.width == width && item.height == height &&
90 item.texture != nullptr)
91 {
92 item.last_used = cache->tick_count;
93 return item.texture;
94 }
95 }
96 return nullptr;
97}
98
100 Scene *scene, int timeline_frame, int view_id, int display_channel, int width, int height)
101{
102 PreviewCache *cache = query_preview_cache(scene);
103 if (cache == nullptr) {
104 return nullptr;
105 }
106 cache->tick_count++;
107 for (PreviewCacheItem &item : cache->items) {
108 if (item.timeline_frame == timeline_frame && item.view_id == view_id &&
109 item.display_channel == display_channel && item.width == width && item.height == height &&
110 item.scope_texture != nullptr)
111 {
112 item.last_used = cache->tick_count;
113 return item.scope_texture;
114 }
115 }
116 return nullptr;
117}
118
120 int timeline_frame,
121 int view_id,
122 int display_channel,
123 int width,
124 int height)
125{
126 cache->tick_count++;
127
128 /* Try to find an exact frame match. */
129 for (PreviewCacheItem &item : cache->items) {
130 if (item.timeline_frame == timeline_frame && item.view_id == view_id &&
131 item.display_channel == display_channel && item.width == width && item.height == height)
132 {
133 return &item;
134 }
135 }
136
137 /* Find unused or least recently used slot. */
138 PreviewCacheItem *best_slot = nullptr;
139 int64_t best_score = -1;
140 for (PreviewCacheItem &item : cache->items) {
141 if (item.texture == nullptr && item.scope_texture == nullptr) {
142 return &item;
143 }
144 int64_t score = cache->tick_count - item.last_used;
145 if (score >= best_score) {
146 best_score = score;
147 best_slot = &item;
148 }
149 }
150
151 return best_slot;
152}
153
155 Scene *scene, int timeline_frame, int view_id, int display_channel, gpu::Texture *texture)
156{
157 PreviewCache *cache = ensure_preview_cache(scene);
158 if (cache == nullptr || texture == nullptr) {
159 return;
160 }
161 const int width = GPU_texture_width(texture);
162 const int height = GPU_texture_height(texture);
164 cache, timeline_frame, view_id, display_channel, width, height);
165 if (slot == nullptr) {
166 return;
167 }
168
169 slot->timeline_frame = timeline_frame;
170 slot->view_id = view_id;
171 slot->display_channel = display_channel;
172 slot->width = width;
173 slot->height = height;
174 slot->last_used = cache->tick_count;
176 /* Free the display-space texture of this slot too. */
178 slot->texture = texture;
179}
180
182 Scene *scene, int timeline_frame, int view_id, int display_channel, gpu::Texture *texture)
183{
184 PreviewCache *cache = ensure_preview_cache(scene);
185 if (cache == nullptr || texture == nullptr) {
186 return;
187 }
188 const int width = GPU_texture_width(texture);
189 const int height = GPU_texture_height(texture);
191 cache, timeline_frame, view_id, display_channel, width, height);
192 if (slot == nullptr) {
193 return;
194 }
195
196 slot->timeline_frame = timeline_frame;
197 slot->view_id = view_id;
198 slot->display_channel = display_channel;
199 slot->width = width;
200 slot->height = height;
201 slot->last_used = cache->tick_count;
203 slot->scope_texture = texture;
204}
205
207{
208 PreviewCache *cache = query_preview_cache(scene);
209 if (cache != nullptr) {
210 cache->clear();
211 }
212}
213
215{
216 PreviewCache *cache = query_preview_cache(scene);
217 if (cache != nullptr) {
218 MEM_SAFE_DELETE(scene->ed->runtime->preview_cache);
219 }
220}
221
222} // namespace blender::seq
#define GPU_TEXTURE_FREE_SAFE(texture)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
gpu::Texture * preview_cache_get_gpu_texture(Scene *scene, int timeline_frame, int view_id, int display_channel, int width, int height)
void preview_cache_set_gpu_texture(Scene *scene, int timeline_frame, int view_id, int display_channel, gpu::Texture *texture)
void preview_cache_set_gpu_scope_texture(Scene *scene, int timeline_frame, int view_id, int display_channel, gpu::Texture *texture)
void preview_cache_destroy(Scene *scene)
static PreviewCacheItem * find_slot(PreviewCache *cache, int timeline_frame, int view_id, int display_channel, int width, int height)
gpu::Texture * preview_cache_get_gpu_scope_texture(Scene *scene, int timeline_frame, int view_id, int display_channel, int width, int height)
static PreviewCache * ensure_preview_cache(Scene *scene)
static PreviewCache * query_preview_cache(Scene *scene)
void preview_cache_invalidate(Scene *scene)
__int64 int64_t
Definition BKE_text.h:90
int GPU_texture_width(const gpu::Texture *texture)
int GPU_texture_height(const gpu::Texture *texture)
seq::EditingRuntime * runtime
struct Editing * ed
PreviewCacheItem items[cache_size]
static constexpr int cache_size