Blender V5.3
sequencer_thumbnails.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10
11#include "BLI_array.hh"
12
13#include "IMB_imbuf.hh"
14
15#include "DNA_sequence_types.h"
16#include "DNA_space_types.h"
17#include "DNA_userdef_types.h"
18
19#include "GPU_batch.hh"
20#include "GPU_batch_presets.hh"
21#include "GPU_matrix.hh"
22#include "GPU_shader_shared.hh"
23#include "GPU_texture.hh"
24#include "GPU_uniform_buffer.hh"
25
27
28#include "PRF_profile.hh"
29
31
32#include "UI_view2d.hh"
33
34#include "WM_api.hh"
35
36#include "sequencer_intern.hh"
38
39namespace blender::ed::vse {
40
41/* Information for one thumbnail picture in the timeline. Note that a single
42 * strip could have multiple thumbnails. */
45 /* Strip coordinates in timeline space (X: frames, Y: channels). */
47 /* Thumbnail coordinates in timeline space. */
48 float x1, x2, y1, y2;
49 /* Horizontal cropping of thumbnail image, in pixels. Often a thumbnail
50 * does not have to be cropped, in which case these are 0 and ibuf->x-1. */
53};
54
55static void strip_get_thumb_image_dimensions(const Strip *strip,
56 float pixelx,
57 float pixely,
58 float *r_thumb_width,
59 float thumb_height,
60 float *r_image_width,
61 float *r_image_height)
62{
63 int image_width = seq::THUMB_SIZE, image_height = seq::THUMB_SIZE;
65 image_width = strip->data->stripdata->orig_width;
66 image_height = strip->data->stripdata->orig_height;
67 }
68 else if (strip->type == STRIP_TYPE_MOVIECLIP && strip->clip) {
69 image_width = strip->clip->lastsize[0];
70 image_height = strip->clip->lastsize[1];
71 }
72 else if (strip->type == STRIP_TYPE_SCENE && strip->scene) {
73 image_width = strip->scene->r.xsch;
74 image_height = strip->scene->r.ysch;
75 }
76
77 seq::image_size_to_thumb_size(image_width, image_height);
78
79 /* Calculate thumb dimensions. */
80 const float aspect_ratio = float(image_width) / float(image_height);
81 float thumb_h_px = thumb_height / pixely;
82 float thumb_width = aspect_ratio * thumb_h_px * pixelx;
83
84 *r_thumb_width = thumb_width;
85 *r_image_width = image_width;
86 *r_image_height = image_height;
87}
88
89static bool add_thumbnail_at_frame(float timeline_frame,
90 const bContext *C,
91 const View2D *v2d,
92 const StripDrawContext &strip,
93 Scene *scene,
94 float thumb_width,
95 float crop_left,
96 float crop_right,
97 float crop_x_multiplier,
98 float upper_thumb_bound,
99 float display_offset,
100 bool is_muted,
101 Vector<SeqThumbInfo> &r_thumbs)
102{
103 /* Frame at which the thumb at `timeline_frame` will be drawn. */
104 const float display_frame = timeline_frame + display_offset;
105
106 float thumb_x_end = display_frame + thumb_width;
107 bool clipped = false;
108
109 /* Reached end of view, no more thumbnails needed. */
110 if (display_frame > v2d->cur.xmax) {
111 return false;
112 }
113
114 /* Clip if full thumbnail cannot be displayed. */
115 if (thumb_x_end > upper_thumb_bound) {
116 thumb_x_end = upper_thumb_bound;
117 clipped = true;
118 }
119
120 crop_left = max_ff(crop_left, 0.0f);
121 crop_right = max_ff(crop_right, 0.0f);
122
123 if (crop_left > 0.0f || crop_right > 0.0f) {
124 clipped = true;
125 }
126
127 float cropx_min = crop_left * crop_x_multiplier;
128 float cropx_max = min_ff(thumb_width - crop_right, (thumb_x_end - display_frame)) *
129 crop_x_multiplier;
130 if (cropx_max - cropx_min < 1.0f) {
131 return false;
132 }
133
134 /* Get the thumbnail image. */
135 ImBuf *ibuf = seq::thumbnail_cache_get(C, scene, strip.strip, timeline_frame);
136 if (ibuf == nullptr) {
137 /* Thumbnail is not in cache but still other frames have to request for thumbnails. */
138 return true;
139 }
140
141 SeqThumbInfo thumb = {};
142 thumb.ibuf = ibuf;
143 thumb.cropx_min = 0;
144 thumb.cropx_max = ibuf->x - 1;
145 if (clipped) {
146 thumb.cropx_min = clamp_f(cropx_min, 0, ibuf->x - 1);
147 thumb.cropx_max = clamp_f(cropx_max, 0, ibuf->x - 1);
148 }
149 thumb.left_handle = strip.left_handle;
150 thumb.right_handle = strip.right_handle;
151 thumb.is_muted = is_muted;
152 thumb.bottom = strip.bottom;
153 thumb.top = strip.top;
154 thumb.x1 = display_frame + crop_left;
155 thumb.x2 = min_ff(thumb_x_end, display_frame + thumb_width - crop_right);
156 thumb.y1 = strip.bottom;
157 thumb.y2 = strip.strip_content_top;
158 r_thumbs.append(thumb);
159
160 return true;
161};
162
163static bool is_thumbnail_in_view(const float timeline_frame,
164 const float thumb_width,
165 const View2D *v2d)
166{
167 if (timeline_frame < v2d->cur.xmax && timeline_frame + thumb_width > v2d->cur.xmin) {
168 return true;
169 }
170 return false;
171}
172
174 const bContext *C,
175 const StripDrawContext &strip,
176 Scene *scene,
177 const float thumb_width,
178 const float crop_x_multiplier,
179 const float upper_thumb_bound,
180 bool is_muted,
181 Vector<SeqThumbInfo> &r_thumbs)
182{
183 const float left_frame = max_ff(strip.content_start, strip.left_handle);
184 const float right_frame = strip.is_single_image ? left_frame :
185 min_ff(strip.content_end, strip.right_handle);
186 const float strip_width = strip.is_single_image ? (strip.right_handle - strip.left_handle) :
187 (right_frame - left_frame);
188 const float middle_frame = roundf((left_frame + right_frame) / 2.0f);
189
190 /* To center the thumbnail, offset it by half its width to the left. */
191 const float display_offset = (strip.is_single_image ? strip_width / 2.0f : 0.0f) -
192 (thumb_width / 2.0f);
193
194 /* Thumbnail is centered, so same crop used for left and right. */
195 const float crop = max_ff(0.0f, thumb_width - strip_width) / 2.0f;
196
197 if (is_thumbnail_in_view(middle_frame + display_offset, thumb_width, v2d)) {
198 add_thumbnail_at_frame(middle_frame,
199 C,
200 v2d,
201 strip,
202 scene,
203 thumb_width,
204 crop,
205 crop,
206 crop_x_multiplier,
207 upper_thumb_bound,
208 display_offset,
209 is_muted,
210 r_thumbs);
211 }
212}
213
215 const bContext *C,
216 const StripDrawContext &strip,
217 Scene *scene,
218 const float thumb_width,
219 const float crop_x_multiplier,
220 const float upper_thumb_bound,
221 bool is_muted,
222 Vector<SeqThumbInfo> &r_thumbs)
223{
224 const float left_frame = max_ff(strip.content_start, strip.left_handle);
225 const float right_frame = strip.is_single_image ? left_frame :
226 min_ff(strip.content_end, strip.right_handle);
227 const float strip_width = strip.is_single_image ? (strip.right_handle - strip.left_handle) :
228 (right_frame - left_frame);
229 const float overlap = max_ff(0.0f, 2.0f * thumb_width - strip_width);
230 const bool only_right_handle_selected = ((strip.strip->flag & SEQ_RIGHTSEL) &&
231 !(strip.strip->flag & SEQ_LEFTSEL));
232 /* Offset the start of last thumbnail. */
233 const float display_offset = (strip.is_single_image ? strip_width : 0.0f) - thumb_width;
234 const float gap = 1.5f * ui::view2d_pixel_size_get_x(v2d) * UI_SCALE_FAC;
235
236 float crop_left = 0.0;
237 float crop_right = 0.0;
238
239 if (overlap > 0.0f && only_right_handle_selected) {
240 /* Crop left thumbnail from right. */
241 crop_right = overlap + gap;
242 }
243 else if (overlap > 0.0f) {
244 /* Crop right thumbnail from left. */
245 crop_left = overlap + gap;
246 }
247
248 if (is_thumbnail_in_view(left_frame, thumb_width, v2d)) {
249 /* Draw left thumbnail. */
250 add_thumbnail_at_frame(left_frame,
251 C,
252 v2d,
253 strip,
254 scene,
255 thumb_width,
256 0.0f,
257 crop_right,
258 crop_x_multiplier,
259 upper_thumb_bound,
260 0.0f,
261 is_muted,
262 r_thumbs);
263 }
264
265 if (is_thumbnail_in_view(right_frame + display_offset, thumb_width, v2d)) {
266 /* Draw right thumbnail. */
267 add_thumbnail_at_frame(right_frame,
268 C,
269 v2d,
270 strip,
271 scene,
272 thumb_width,
273 crop_left,
274 0.0f,
275 crop_x_multiplier,
276 upper_thumb_bound,
277 display_offset,
278 is_muted,
279 r_thumbs);
280 }
281}
282
283static void get_seq_strip_thumbnails(const View2D *v2d,
284 const bContext *C,
285 Scene *scene,
286 const StripDrawContext &strip,
287 bool is_muted,
288 bool show_only_middle,
289 bool show_only_at_strip_ends,
290 Vector<SeqThumbInfo> &r_thumbs)
291{
292 if (!seq::strip_can_have_thumbnail(scene, strip.strip)) {
293 return;
294 }
295
296 const float pixelx = ui::view2d_pixel_size_get_x(v2d);
297 const float pixely = ui::view2d_pixel_size_get_y(v2d);
298
299 /* No thumbnails if height of the strip is too small. */
300 const float thumb_height = strip.strip_content_top - strip.bottom;
301 if (thumb_height / pixely <= 20 * UI_SCALE_FAC) {
302 return;
303 }
304
305 float thumb_width, image_width, image_height;
307 strip.strip, pixelx, pixely, &thumb_width, thumb_height, &image_width, &image_height);
308
309 const float crop_x_multiplier = 1.0f / pixelx / (thumb_height / image_height / pixely);
310
311 float upper_thumb_bound = min_ff(strip.right_handle, strip.content_end);
312 if (strip.is_single_image) {
313 upper_thumb_bound = strip.right_handle;
314 }
315
316 if (show_only_middle) {
318 C,
319 strip,
320 scene,
321 thumb_width,
322 crop_x_multiplier,
323 upper_thumb_bound,
324 is_muted,
325 r_thumbs);
326 return;
327 }
328
329 if (show_only_at_strip_ends) {
331 C,
332 strip,
333 scene,
334 thumb_width,
335 crop_x_multiplier,
336 upper_thumb_bound,
337 is_muted,
338 r_thumbs);
339 return;
340 }
341
342 int first_drawable_frame = std::max({strip.left_handle, strip.strip->start, v2d->cur.xmin});
343 /* Calculate how many thumbnails should we skip over to get to the first visible thumbnail. */
344 float aligned_frame_offset = int((first_drawable_frame - strip.strip->start) / thumb_width) *
345 thumb_width;
346
347 /* If the first frame should correspond to the left handle position,
348 * we want to make it slide under the other thumbs when moving
349 * the left handle. This is so that we don't shift around the rest of the
350 * thumbnails.
351 */
352 bool draw_next_frame_ontop = first_drawable_frame == strip.left_handle;
353 float timeline_frame;
354 if (draw_next_frame_ontop) {
355 timeline_frame = first_drawable_frame;
356 }
357 else {
358 timeline_frame = strip.strip->start + aligned_frame_offset;
359 }
360
361 /* Start going over the strip length. */
362 while (timeline_frame < upper_thumb_bound) {
363
364 const bool should_add_next_thumbnail = add_thumbnail_at_frame(timeline_frame,
365 C,
366 v2d,
367 strip,
368 scene,
369 thumb_width,
370 0.0f,
371 0.0f,
372 crop_x_multiplier,
373 upper_thumb_bound,
374 0.0f,
375 is_muted,
376 r_thumbs);
377
378 if (!should_add_next_thumbnail) {
379 break;
380 }
381
382 if (draw_next_frame_ontop) {
383 timeline_frame = strip.strip->start + aligned_frame_offset + thumb_width;
384 draw_next_frame_ontop = false;
385 }
386 else {
387 timeline_frame += thumb_width;
388 }
389 }
390}
391
397 gpu::Batch *batch_ = nullptr;
403
405 : strips_batch_(strips_batch), thumbs_(GPU_SEQ_STRIP_DRAW_DATA_LEN), atlas_(atlas)
406 {
411
412 ubo_thumbs_ = GPU_uniformbuf_create(sizeof(SeqStripThumbData) * GPU_SEQ_STRIP_DRAW_DATA_LEN);
413
415 }
416
423
425 const SeqThumbInfo &info, float width, const rcti &rect, int tex_width, int tex_height)
426 {
427 if (thumbs_count_ == GPU_SEQ_STRIP_DRAW_DATA_LEN) {
428 flush_batch();
429 }
430
431 SeqStripThumbData &res = thumbs_[thumbs_count_];
433
436 res.bottom = strips_batch_.pos_to_pixel_space_y(info.bottom);
437 res.top = strips_batch_.pos_to_pixel_space_y(info.top);
438 res.tint_color = float4(1.0f, 1.0f, 1.0f, info.is_muted ? 0.47f : 1.0f);
443 res.u1 = float(rect.xmin) / float(tex_width);
444 res.u2 = float(rect.xmin + width) / float(tex_width);
445 res.v1 = float(rect.ymin) / float(tex_height);
446 res.v2 = float(rect.ymax) / float(tex_height);
447 }
448
466};
467
469 StripsDrawBatch &strips_batch,
470 const Vector<StripDrawContext> &strips)
471{
472 const bool show_thumbnails = (ctx.sseq->timeline_overlay.flag & SEQ_TIMELINE_SHOW_THUMBNAILS);
473 /* Nothing to do if we're not showing thumbnails overall. */
474 if ((ctx.sseq->flag & SEQ_SHOW_OVERLAY) == 0 || !show_thumbnails) {
475 return;
476 }
477
478 PRF_scope_with_name("SeqTimelineThumbs", ProfileCategory::Draw);
479
480 /* Gather information for all thumbnails. */
482 /* Thumbnail display mode (Strip ends / Continuous). */
483 const bool show_only_middle = (ctx.sseq->timeline_overlay.flag & SEQ_TIMELINE_MIDDLE_THUMBNAILS);
484 const bool show_only_at_strip_ends = (ctx.sseq->timeline_overlay.flag &
486
487 for (const StripDrawContext &strip : strips) {
489 ctx.C,
490 ctx.scene,
491 strip,
492 strip.is_muted,
493 show_only_middle,
494 show_only_at_strip_ends,
495 thumbs);
496 }
497 if (thumbs.is_empty()) {
498 return;
499 }
500
501 /* Arrange thumbnail images into a texture atlas, using a simple
502 * "add to current row until end, then start a new row". Thumbnail
503 * images are most often same height (but varying width due to horizontal
504 * cropping), so this simple algorithm works well enough. */
505 constexpr int ATLAS_WIDTH = 4096;
506 constexpr int ATLAS_MAX_HEIGHT = 4096;
507 int cur_row_x = 0;
508 int cur_row_y = 0;
509 int cur_row_height = 0;
510 Vector<rcti> rects;
511 rects.reserve(thumbs.size());
512 for (const SeqThumbInfo &info : thumbs) {
513 int cropx_min = int(info.cropx_min);
514 int cropx_max = int(math::ceil(info.cropx_max));
515 int width = cropx_max - cropx_min + 1;
516 int height = info.ibuf->y;
517 cur_row_height = math::max(cur_row_height, height);
518
519 /* If this thumb would not fit onto current row, start a new row. */
520 if (cur_row_x + width > ATLAS_WIDTH) {
521 cur_row_y += cur_row_height + 1; /* +1 empty pixel for bilinear filter. */
522 cur_row_height = height;
523 cur_row_x = 0;
524 if (cur_row_y > ATLAS_MAX_HEIGHT) {
525 break;
526 }
527 }
528
529 /* Record our rect. */
530 rcti rect{cur_row_x, cur_row_x + width, cur_row_y, cur_row_y + height};
531 rects.append(rect);
532
533 /* Advance to next item inside row. */
534 cur_row_x += width + 1; /* +1 empty pixel for bilinear filter. */
535 }
536
537 /* Create the atlas GPU texture. */
538 const int tex_width = ATLAS_WIDTH;
539 const int tex_height = cur_row_y + cur_row_height;
540 Array<uchar> tex_data(tex_width * tex_height * 4, 0);
541 for (int64_t i = 0; i < rects.size(); i++) {
542 /* Copy one thumbnail into atlas. */
543 const rcti &rect = rects[i];
544 SeqThumbInfo &info = thumbs[i];
545
546 const uchar *thumb_image = info.ibuf->byte_data();
547 BLI_assert_msg(thumb_image != nullptr, "Sequencer thumbnails are expected to be byte images");
548 int cropx_min = int(info.cropx_min);
549 int cropx_max = int(math::ceil(info.cropx_max));
550 int width = cropx_max - cropx_min + 1;
551 int height = info.ibuf->y;
552 const uchar *src = thumb_image + cropx_min * 4;
553 uchar *dst = &tex_data[(rect.ymin * ATLAS_WIDTH + rect.xmin) * 4];
554 for (int y = 0; y < height; y++) {
555 memcpy(dst, src, width * 4);
556 src += info.ibuf->x * 4;
557 dst += ATLAS_WIDTH * 4;
558 }
559
560 /* Release thumb image reference. */
561 IMB_freeImBuf(info.ibuf);
562 info.ibuf = nullptr;
563 }
564 gpu::Texture *atlas = GPU_texture_create_2d("thumb_atlas",
565 tex_width,
566 tex_height,
567 1,
568 gpu::TextureFormat::UNORM_8_8_8_8,
570 nullptr);
571 GPU_texture_update(atlas, GPU_DATA_UBYTE, tex_data.data());
572 GPU_texture_filter_mode(atlas, true);
574
575 /* Draw all thumbnails. */
578
579 ThumbsDrawBatch batch(strips_batch, atlas);
580 for (int64_t i = 0; i < rects.size(); i++) {
581 const rcti &rect = rects[i];
582 const SeqThumbInfo &info = thumbs[i];
583 batch.add_thumb(info, info.cropx_max - info.cropx_min + 1, rect, tex_width, tex_height);
584 }
585 batch.flush_batch();
586
588
589 GPU_texture_unbind(atlas);
590 GPU_texture_free(atlas);
591}
592
593} // namespace blender::ed::vse
#define UI_SCALE_FAC
#define GPU_uniformbuf_create(size)
#define PRF_scope_with_name(ui_name, category)
#define C
Definition RandGen.cpp:29
#define ELEM(...)
#define BLI_assert_msg(a, msg)
const T * data() const
Definition BLI_array.hh:323
int64_t size() const
void append(const T &value)
bool is_empty() const
void reserve(const int64_t min_capacity)
const T & data(const Action &owning_action) const
gpu::UniformBuf * get_ubo_context() const
#define roundf(x)
static void get_seq_strip_middle_thumbnail(const View2D *v2d, const bContext *C, const StripDrawContext &strip, Scene *scene, const float thumb_width, const float crop_x_multiplier, const float upper_thumb_bound, bool is_muted, Vector< SeqThumbInfo > &r_thumbs)
void draw_strip_thumbnails(const TimelineDrawContext &ctx, StripsDrawBatch &strips_batch, const Vector< StripDrawContext > &strips)
static bool is_thumbnail_in_view(const float timeline_frame, const float thumb_width, const View2D *v2d)
static void strip_get_thumb_image_dimensions(const Strip *strip, float pixelx, float pixely, float *r_thumb_width, float thumb_height, float *r_image_width, float *r_image_height)
static bool add_thumbnail_at_frame(float timeline_frame, const bContext *C, const View2D *v2d, const StripDrawContext &strip, Scene *scene, float thumb_width, float crop_left, float crop_right, float crop_x_multiplier, float upper_thumb_bound, float display_offset, bool is_muted, Vector< SeqThumbInfo > &r_thumbs)
static void get_seq_strip_ends_thumbnails(const View2D *v2d, const bContext *C, const StripDrawContext &strip, Scene *scene, const float thumb_width, const float crop_x_multiplier, const float upper_thumb_bound, bool is_muted, Vector< SeqThumbInfo > &r_thumbs)
static void get_seq_strip_thumbnails(const View2D *v2d, const bContext *C, Scene *scene, const StripDrawContext &strip, bool is_muted, bool show_only_middle, bool show_only_at_strip_ends, Vector< SeqThumbInfo > &r_thumbs)
T ceil(const T &a)
T max(const T &a, const T &b)
bool strip_can_have_thumbnail(const Scene *scene, const Strip *strip)
ImBuf * thumbnail_cache_get(const bContext *C, Scene *scene, const Strip *strip, float timeline_frame)
void image_size_to_thumb_size(int &r_width, int &r_height, int size)
float view2d_pixel_size_get_y(const View2D *v2d)
Definition view2d.cc:1924
float view2d_pixel_size_get_x(const View2D *v2d)
Definition view2d.cc:1920
MINLINE float min_ff(float a, float b)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
__int64 int64_t
Definition BKE_text.h:90
void wmOrtho2_region_pixelspace(const ARegion *region)
@ SEQ_TIMELINE_MIDDLE_THUMBNAILS
@ SEQ_TIMELINE_STRIP_END_THUMBNAILS
@ SEQ_TIMELINE_SHOW_THUMBNAILS
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_uniformbuf_free(gpu::UniformBuf *ubo)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_SHADER_SEQUENCER_THUMBS
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_batch_draw_instance_range(gpu::Batch *batch, int instance_first, int instance_count)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
void GPU_uniformbuf_update(gpu::UniformBuf *ubo, const void *data)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
gpu::Batch * GPU_batch_preset_quad()
void GPU_matrix_pop_projection()
void GPU_texture_bind(gpu::Texture *texture, int unit)
MINLINE float max_ff(float a, float b)
void GPU_texture_free(gpu::Texture *texture)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
void GPU_texture_unbind(gpu::Texture *texture)
void GPU_uniformbuf_unbind(gpu::UniformBuf *ubo)
void GPU_matrix_push_projection()
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
MINLINE float clamp_f(float value, float min, float max)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
eSpaceSeq_SequencerTimelineOverlay_Flag flag
eSpaceSeq_Flag flag
struct SequencerTimelineOverlay timeline_overlay
ThumbsDrawBatch(StripsDrawBatch &strips_batch, gpu::Texture *atlas)
void add_thumb(const SeqThumbInfo &info, float width, const rcti &rect, int tex_width, int tex_height)