Blender V5.3
sequencer_retiming_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string_utf8.hh"
10
11#include "DNA_sequence_types.h"
12
13#include "BKE_context.hh"
14#include "BKE_fcurve.hh"
15
16#include "BLF_api.hh"
17
18#include "GPU_immediate.hh"
19#include "GPU_matrix.hh"
20#include "GPU_state.hh"
21#include "GPU_vertex_format.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "ED_keyframes_draw.hh"
28#include "ED_screen.hh"
29
30#include "UI_view2d.hh"
31
32#include "SEQ_retiming.hh"
33
34#include "sequencer_intern.hh"
36
37namespace blender::ed::vse {
38
39/* -------------------------------------------------------------------- */
42
48
49static bool can_draw_retiming(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx)
50{
51 if (ctx.ed == nullptr) {
52 return false;
53 }
54
56 return false;
57 }
58
59 if (!strip_ctx.can_draw_retiming_overlay) {
60 return false;
61 }
62
63 if (!seq::retiming_is_allowed(strip_ctx.strip)) {
64 return false;
65 }
66
67 if (!seq::retiming_show_keys(strip_ctx.strip)) {
68 return false;
69 }
70
71 return true;
72}
73
74static inline float retiming_key_size()
75{
76 /* Pixel size of whole retiming key, from left side to right side or top to bottom. */
77 return 10.0f * U.pixelsize;
78}
79
80static inline float retiming_key_center(const View2D *v2d, const Strip *strip)
81{
82 return (ui::view2d_view_to_region_y(v2d, strip->channel + STRIP_OFSBOTTOM) + 4 +
83 retiming_key_size() / 2);
84}
85
86rcti strip_retiming_keys_box_get(const Scene *scene, const View2D *v2d, const Strip *strip)
87{
88 rctf strip_bounds = strip_bounds_get(scene, strip);
89 rcti key_bounds;
90 ui::view2d_view_to_region_rcti(v2d, &strip_bounds, &key_bounds);
91
92 key_bounds.ymax = retiming_key_center(v2d, strip) + retiming_key_size() / 2;
93 key_bounds.ymin = retiming_key_center(v2d, strip) - retiming_key_size() / 2;
94 return key_bounds;
95}
96
98
99/* -------------------------------------------------------------------- */
102
104 const StripDrawContext &strip_ctx,
105 const SeqRetimingKey *key,
106 const KeyframeShaderBindings &sh_bindings)
107{
108 const Scene *scene = ctx.scene;
109 const View2D *v2d = ctx.v2d;
110 Strip *strip = strip_ctx.strip;
111
112 const float key_frame = seq::retiming_key_frame_get(scene, strip, key);
113 const rctf strip_bounds = strip_bounds_get(scene, strip);
114 if (!BLI_rctf_isect_x(&strip_bounds, key_frame)) {
115 return; /* Key out of the strip bounds. */
116 }
117
120 key_type = BEZT_KEYTYPE_BREAKDOWN;
121 }
123 key_type = BEZT_KEYTYPE_MOVEHOLD;
124 }
125
126 const bool is_selected = ctx.retiming_selection.contains(const_cast<SeqRetimingKey *>(key));
127 const int size = retiming_key_size();
128 const float bottom = retiming_key_center(v2d, strip);
129
130 /* For keys on the edge of the strip, ensure that their entire extent is drawn with a shift. */
131 const float right_max = ui::view2d_view_to_region_x(v2d, strip_ctx.right_handle) - (size / 2);
132 const float left_min = ui::view2d_view_to_region_x(v2d, strip_ctx.left_handle) + (size / 2);
133 float key_position = ui::view2d_view_to_region_x(v2d, key_frame);
134 CLAMP(key_position, left_min, right_max);
135
136 draw_keyframe_shape(key_position,
137 bottom,
138 size,
139 is_selected,
140 key_type,
142 1.0,
143 &sh_bindings,
144 0,
145 0);
146}
147
148/* If there are no keys, draw fake keys and create real key when they are selected. */
149/* TODO: would be nice to draw segments between fake keys. */
150static bool fake_keys_draw(const TimelineDrawContext &ctx,
151 const StripDrawContext &strip_ctx,
152 const KeyframeShaderBindings &sh_bindings)
153{
154 const Strip *strip = strip_ctx.strip;
155 const Scene *scene = ctx.scene;
156
157 const int left_key_frame = seq::left_fake_key_frame_get(scene, strip);
158 if (seq::retiming_key_get_by_frame(scene, strip, left_key_frame) == nullptr) {
159 SeqRetimingKey fake_key = seq::fake_retiming_key_init(scene, strip, left_key_frame);
160 retiming_key_draw(ctx, strip_ctx, &fake_key, sh_bindings);
161 }
162
163 int right_key_frame = seq::right_fake_key_frame_get(scene, strip);
164 if (seq::retiming_key_get_by_frame(scene, strip, right_key_frame) == nullptr) {
165 SeqRetimingKey fake_key = seq::fake_retiming_key_init(scene, strip, right_key_frame);
166 retiming_key_draw(ctx, strip_ctx, &fake_key, sh_bindings);
167 }
168 return true;
169}
170
172{
173 if (strips.is_empty()) {
174 return;
175 }
176 if (ctx.ed == nullptr || !retiming_overlay_enabled(ctx.sseq)) {
177 return;
178 }
179
182
183 const View2D *v2d = ctx.v2d;
184
185 GPUVertFormat *format = immVertexFormat();
186 KeyframeShaderBindings sh_bindings;
187 sh_bindings.pos_id = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
188 sh_bindings.size_id = GPU_vertformat_attr_add(format, "size", gpu::VertAttrType::SFLOAT_32);
189 sh_bindings.color_id = GPU_vertformat_attr_add(
190 format, "color", gpu::VertAttrType::UNORM_8_8_8_8);
192 format, "outlineColor", gpu::VertAttrType::UNORM_8_8_8_8);
193 sh_bindings.flags_id = GPU_vertformat_attr_add(format, "flags", gpu::VertAttrType::UINT_32);
194
197 immUniform1f("outline_scale", 1.0f);
198 immUniform2f("ViewportSize", BLI_rcti_size_x(&v2d->mask) + 1, BLI_rcti_size_y(&v2d->mask) + 1);
199
200 constexpr int MAX_KEYS_IN_BATCH = 1024;
201 int point_counter = 0;
202 immBeginAtMost(GPU_PRIM_POINTS, MAX_KEYS_IN_BATCH);
203
204 for (const StripDrawContext &strip_ctx : strips) {
205 if (!can_draw_retiming(ctx, strip_ctx)) {
206 continue;
207 }
208 if (fake_keys_draw(ctx, strip_ctx, sh_bindings)) {
209 point_counter += 2;
210 }
211
212 for (const SeqRetimingKey &key : seq::retiming_keys_get(strip_ctx.strip)) {
213 retiming_key_draw(ctx, strip_ctx, &key, sh_bindings);
214 point_counter++;
215
216 /* Next key plus possible two fake keys for next strip would need at
217 * most 3 points, so restart the batch if we're close to that. */
218 if (point_counter + 3 >= MAX_KEYS_IN_BATCH) {
219 immEnd();
220 immBeginAtMost(GPU_PRIM_POINTS, MAX_KEYS_IN_BATCH);
221 point_counter = 0;
222 }
223 }
224 }
225
226 immEnd();
229
231}
232
234
235/* -------------------------------------------------------------------- */
238
240 const StripDrawContext &strip_ctx)
241{
242 if (!can_draw_retiming(ctx, strip_ctx) || seq::retiming_keys_count(strip_ctx.strip) == 0) {
243 return;
244 }
245
246 const Strip *strip = strip_ctx.strip;
247 const View2D *v2d = ctx.v2d;
248 const Scene *scene = ctx.scene;
249 const float left_handle_pos = ui::view2d_view_to_region_x(v2d, strip_ctx.left_handle);
250 const float right_handle_pos = ui::view2d_view_to_region_x(v2d, strip_ctx.right_handle);
251
252 for (const SeqRetimingKey &key : seq::retiming_keys_get(strip)) {
253 const int key_frame = seq::retiming_key_frame_get(scene, strip, &key);
254 if (key_frame == strip_ctx.left_handle || key.strip_frame_index == 0) {
255 continue;
256 }
257
258 float key_pos = ui::view2d_view_to_region_x(v2d, key_frame);
259 float prev_key_pos = ui::view2d_view_to_region_x(
260 v2d, seq::retiming_key_frame_get(scene, strip, &key - 1));
261 if (prev_key_pos > right_handle_pos || key_pos < left_handle_pos) {
262 /* Don't draw highlights for out of bounds retiming keys. */
263 continue;
264 }
265 prev_key_pos = max_ff(prev_key_pos, left_handle_pos);
266 key_pos = min_ff(key_pos, right_handle_pos);
267
268 const int size = retiming_key_size();
269 const float y_center = retiming_key_center(v2d, strip);
270
271 const float width_fac = 0.5f;
272 const float bottom = y_center - size * width_fac;
273 const float top = y_center + size * width_fac;
274
275 uchar color[4];
276 if ((ctx.retiming_selection.contains(const_cast<SeqRetimingKey *>(&key)) ||
277 ctx.retiming_selection.contains(const_cast<SeqRetimingKey *>(&key - 1))))
278 {
279 color[0] = 166;
280 color[1] = 127;
281 color[2] = 51;
282 color[3] = 255;
283 }
284 else {
285 color[0] = 0;
286 color[1] = 0;
287 color[2] = 0;
288 color[3] = 25;
289 }
290 ctx.quads->add_quad(prev_key_pos, bottom, key_pos, top, color);
291 }
292}
293
294static size_t speed_label_str_get(const Strip *strip,
295 const SeqRetimingKey *key,
296 char *r_label_str,
297 const size_t label_str_maxncpy)
298{
299 const SeqRetimingKey *next_key = key + 1;
301 const float prev_speed = seq::retiming_key_speed_get(strip, key);
302 const float next_speed = seq::retiming_key_speed_get(strip, next_key + 1);
303 return BLI_snprintf_utf8_rlen(r_label_str,
304 label_str_maxncpy,
305 "%d%% - %d%%",
306 round_fl_to_int(prev_speed * 100.0f),
307 round_fl_to_int(next_speed * 100.0f));
308 }
309 const float speed = seq::retiming_key_speed_get(strip, next_key);
311 r_label_str, label_str_maxncpy, "%d%%", round_fl_to_int(speed * 100.0f));
312}
313
314static std::optional<float2> speed_label_pos_get(const TimelineDrawContext &ctx,
315 const StripDrawContext &strip_ctx,
316 const SeqRetimingKey *key,
317 const char *label_str,
318 const size_t label_len)
319{
320 const Scene *scene = ctx.scene;
321
322 const float key_x = max_ff(strip_ctx.left_handle,
323 seq::retiming_key_frame_get(scene, strip_ctx.strip, key));
324 const float next_x = min_ff(strip_ctx.right_handle,
325 seq::retiming_key_frame_get(scene, strip_ctx.strip, key + 1));
326
327 const float label_width = ctx.pixelx * BLF_width(BLF_default(), label_str, label_len);
328
329 /* Available space for text is segment width minus two "half" keys (one key width in total). */
330 const float available_width = (next_x - key_x) - (ctx.pixelx * retiming_key_size());
331 if (available_width < label_width) {
332 return std::nullopt;
333 }
334
335 /* Label rests 5px above bottom of strip. */
336 const float bottom_pad = (ctx.pixely * 5.0f);
337
338 const float x = 0.5f * (key_x + next_x - label_width); /* Left edge of centered label. */
339 const float y = (strip_ctx.strip->channel + STRIP_OFSBOTTOM) + bottom_pad;
340 return float2{x, y};
341}
342
344 const StripDrawContext &strip_ctx,
345 const SeqRetimingKey *key)
346{
347 const Strip *strip = strip_ctx.strip;
348 const Scene *scene = ctx.scene;
349
350 if (seq::retiming_is_last_key(strip, key)) {
351 return;
352 }
353
354 const SeqRetimingKey *next_key = key + 1;
355 if (seq::retiming_key_frame_get(scene, strip, next_key) < strip_ctx.left_handle ||
356 seq::retiming_key_frame_get(scene, strip, key) > strip_ctx.right_handle)
357 {
358 return; /* Label out of strip bounds. */
359 }
360
361 char label_str[40];
362 size_t label_len = speed_label_str_get(strip, key, label_str, sizeof(label_str));
363
364 const std::optional<float2> pos = speed_label_pos_get(ctx, strip_ctx, key, label_str, label_len);
365 if (!pos) {
366 return; /* Not enough space to draw the label. */
367 }
368
369 /* Keep speed percentage text black regardless of selection state to maintain
370 * legibility over bright waveforms and strip colors (#164310). */
371 constexpr uchar col[4] = {0, 0, 0, 255};
372
373 ui::view2d_text_cache_add(ctx.v2d, pos->x, pos->y, label_str, label_len, col);
374}
375
377 const StripDrawContext &strip_ctx)
378{
379 if (!can_draw_retiming(ctx, strip_ctx)) {
380 return;
381 }
382
383 for (const SeqRetimingKey &key : seq::retiming_keys_get(strip_ctx.strip)) {
384 speed_label_draw(ctx, strip_ctx, &key);
385 }
386
388}
389
391
392} // namespace blender::ed::vse
#define STRIP_OFSBOTTOM
#define CLAMP(a, b, c)
constexpr bool is_empty() const
Definition BLI_span.hh:264
void add_quad(float x1, float y1, float x2, float y2, const uchar color[4])
bool retiming_overlay_enabled(const SpaceSeq *sseq)
static bool can_draw_retiming(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx)
rctf strip_bounds_get(const Scene *scene, const Strip *strip)
static size_t speed_label_str_get(const Strip *strip, const SeqRetimingKey *key, char *r_label_str, const size_t label_str_maxncpy)
static void retiming_key_draw(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx, const SeqRetimingKey *key, const KeyframeShaderBindings &sh_bindings)
static float retiming_key_center(const View2D *v2d, const Strip *strip)
void sequencer_retiming_draw_segments(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx)
static void speed_label_draw(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx, const SeqRetimingKey *key)
static bool fake_keys_draw(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx, const KeyframeShaderBindings &sh_bindings)
void sequencer_retiming_keys_draw(const TimelineDrawContext &ctx, Span< StripDrawContext > strips)
rcti strip_retiming_keys_box_get(const Scene *scene, const View2D *v2d, const Strip *strip)
void sequencer_retiming_speed_labels_draw(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx)
static std::optional< float2 > speed_label_pos_get(const TimelineDrawContext &ctx, const StripDrawContext &strip_ctx, const SeqRetimingKey *key, const char *label_str, const size_t label_len)
static float retiming_key_size()
MutableSpan< SeqRetimingKey > retiming_keys_get(const Strip *strip)
bool retiming_key_is_transition_start(const SeqRetimingKey *key)
int retiming_keys_count(const Strip *strip)
SeqRetimingKey fake_retiming_key_init(const Scene *scene, const Strip *strip, int frame)
int retiming_key_frame_get(const Scene *scene, const Strip *strip, const SeqRetimingKey *key)
bool retiming_key_is_transition_type(const SeqRetimingKey *key)
bool retiming_key_is_freeze_frame(const SeqRetimingKey *key)
int right_fake_key_frame_get(const Scene *scene, const Strip *strip)
float retiming_key_speed_get(const Strip *strip, const SeqRetimingKey *key)
bool retiming_is_allowed(const Strip *strip)
bool retiming_show_keys(const Strip *strip)
int left_fake_key_frame_get(const Scene *scene, const Strip *strip)
SeqRetimingKey * retiming_key_get_by_frame(const Scene *scene, const Strip *strip, const int frame)
bool retiming_is_last_key(const Strip *strip, const SeqRetimingKey *key)
char char char char void view2d_text_cache_add(View2D *v2d, float x, float y, const char *str, size_t str_len, const unsigned char col[4])
Definition view2d.cc:2079
void view2d_view_ortho(const View2D *v2d)
Definition view2d.cc:1080
float view2d_view_to_region_y(const View2D *v2d, float y)
Definition view2d.cc:1678
void view2d_view_to_region_rcti(const View2D *v2d, const rctf *rect_src, rcti *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1767
float view2d_view_to_region_x(const View2D *v2d, float x)
Definition view2d.cc:1673
MINLINE float min_ff(float a, float b)
UserDef U
Definition blender.cc:58
@ BEZT_KEYTYPE_MOVEHOLD
@ BEZT_KEYTYPE_KEYFRAME
@ BEZT_KEYTYPE_BREAKDOWN
void wmOrtho2_region_pixelspace(const ARegion *region)
@ SEQ_TIMELINE_SHOW_STRIP_RETIMING
void immEnd()
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:835
MINLINE int round_fl_to_int(float a)
void draw_keyframe_shape(const float x, const float y, float size, const bool sel, const eBezTriple_KeyframeType key_type, const eKeyframeShapeDrawOpts mode, const float alpha, const KeyframeShaderBindings *sh_bindings, const short handle_type, const short extreme_type)
void immUniform2f(const char *name, float x, float y)
void immBeginAtMost(GPUPrimType, uint max_vertex_len)
GPUVertFormat * immVertexFormat()
void immUnbindProgram()
bool BLI_rctf_isect_x(const rctf *rect, float x)
Definition rct.cc:95
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
unsigned char uchar
Definition BKE_text.h:58
int BLF_default()
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void GPU_matrix_pop_projection()
MINLINE float max_ff(float a, float b)
void GPU_matrix_push_projection()
size_t BLI_snprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
eSpaceSeq_SequencerTimelineOverlay_Flag flag
eSpaceSeq_Flag flag
struct SequencerTimelineOverlay timeline_overlay
Map< SeqRetimingKey *, Strip * > retiming_selection