-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathscope_window.cpp
More file actions
616 lines (521 loc) · 21.7 KB
/
Copy pathscope_window.cpp
File metadata and controls
616 lines (521 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "scope_window.h"
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include "sdl_event_info.h"
#include "string_utils.h"
extern "C" {
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
}
#include <SDL2/SDL.h>
namespace {
constexpr int kFrameThickness = 2;
constexpr int kDividerThickness = 3;
constexpr int kOuterPad = kFrameThickness + kDividerThickness; // frame + breathing room
constexpr int kHalfDividerThickness = kDividerThickness / 2;
constexpr int kHalfOuterPad = kOuterPad / 2;
constexpr int kDividerGap = kOuterPad * 2; // total empty gap between panes
struct ScopeInfoEntry {
ScopeWindow::Type type;
const char* title;
const char* filter_name; // FFmpeg runtime filter name
const char* type_string; // used for logging/CLI-facing messages
};
static constexpr ScopeInfoEntry kScopeInfos[] = {
{ScopeWindow::Type::Histogram, "Histogram", "histogram", "histogram"},
{ScopeWindow::Type::Vectorscope, "Vectorscope", "vectorscope", "vectorscope"},
{ScopeWindow::Type::Waveform, "Waveform", "waveform", "waveform"},
};
inline const ScopeInfoEntry* find_scope_info(const ScopeWindow::Type type) {
for (const auto& entry : kScopeInfos) {
if (entry.type == type) {
return &entry;
}
}
return nullptr;
}
inline const ScopeInfoEntry& get_scope_info(const ScopeWindow::Type type) {
if (const auto* info = find_scope_info(type)) {
return *info;
}
return kScopeInfos[0];
}
inline ScopeWindow::Roi clamp_roi_to_frame(const ScopeWindow::Roi& roi, const int frame_w, const int frame_h) {
if (frame_w <= 0 || frame_h <= 0) {
return {0, 0, 0, 0};
}
// Make sure crop width/height are positive and within bounds.
const int x = std::max(0, std::min(roi.x, frame_w - 1));
const int y = std::max(0, std::min(roi.y, frame_h - 1));
const int w = std::max(1, std::min(roi.w, frame_w - x));
const int h = std::max(1, std::min(roi.h, frame_h - y));
return {x, y, w, h};
}
static void draw_rect_thickness(SDL_Renderer* renderer, const SDL_Rect& rect, const int thickness) {
if (!renderer || thickness <= 0) {
return;
}
SDL_Rect r = rect;
for (int i = 0; i < thickness; ++i) {
SDL_RenderDrawRect(renderer, &r);
r.x += 1;
r.y += 1;
r.w = std::max(0, r.w - 2);
r.h = std::max(0, r.h - 2);
if (r.w <= 0 || r.h <= 0) {
break;
}
}
}
static void draw_left_right_overlay(SDL_Renderer* renderer, const int w, const int h, const int divider_x) {
if (!renderer || w <= 1 || h <= 1) {
return;
}
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
const int x = std::min(std::max(divider_x, 0), w - 1);
SDL_Rect left_rect{0, 0, x, h};
SDL_Rect right_rect{x, 0, w - x, h};
SDL_SetRenderDrawColor(renderer, 255, 128, 128, 255);
draw_rect_thickness(renderer, left_rect, kFrameThickness);
SDL_SetRenderDrawColor(renderer, 128, 128, 255, 255);
draw_rect_thickness(renderer, right_rect, kFrameThickness);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 140);
for (int dx = -kHalfOuterPad; dx <= kHalfOuterPad; ++dx) {
const int xi = x + dx;
if (xi >= 0 && xi < w) {
SDL_RenderDrawLine(renderer, xi, 0, xi, h - 1);
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 220);
for (int dx = -kHalfDividerThickness; dx <= kHalfDividerThickness; ++dx) {
const int xi = x + dx;
if (xi >= 0 && xi < w) {
SDL_RenderDrawLine(renderer, xi, 0, xi, h - 1);
}
}
}
} // namespace
static void sdl_check_bool(const bool ok, const char* what) {
if (!ok) {
throw std::runtime_error(std::string("SDL error in ") + what + ": " + SDL_GetError());
}
}
static void* sdl_check_ptr(void* ptr, const char* what) {
if (!ptr) {
throw std::runtime_error(std::string("SDL error in ") + what + ": " + SDL_GetError());
}
return ptr;
}
static void ffmpeg_check(const int error_code, const char* what) {
if (error_code < 0) {
throw std::runtime_error(std::string("FFmpeg error in ") + what);
}
}
ScopeWindow::ScopeWindow(const Type type, const int pane_width, const int pane_height, const bool always_on_top, const int display_number, const bool use_10_bpc, const std::string& filter_options)
: type_(type), pane_width_(pane_width), pane_height_(pane_height), always_on_top_(always_on_top), display_number_(display_number), use_10_bpc_(use_10_bpc), filter_options_(filter_options) {
const int window_width = pane_width_ * 2;
const int window_height = pane_height_;
Uint32 window_flags = SDL_WINDOW_SHOWN;
window_flags |= SDL_WINDOW_RESIZABLE;
if (always_on_top_) {
window_flags |= SDL_WINDOW_ALWAYS_ON_TOP;
}
// Determine initial position based on display usable bounds and tool type index to avoid overlap.
int initial_position_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display_number_);
int initial_position_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display_number_);
SDL_Rect usable_bounds;
if (SDL_GetDisplayUsableBounds(display_number_, &usable_bounds) == 0) {
const int margin_pixels = 32;
const int offset_step_pixels = 64;
const int tool_type_index = static_cast<int>(ScopeWindow::index(type_));
int proposed_x = usable_bounds.x + margin_pixels + tool_type_index * offset_step_pixels;
int proposed_y = usable_bounds.y + margin_pixels + tool_type_index * offset_step_pixels;
// Clamp to ensure the window is entirely within the usable bounds
const int max_x = usable_bounds.x + std::max(0, usable_bounds.w - window_width - margin_pixels);
const int max_y = usable_bounds.y + std::max(0, usable_bounds.h - window_height - margin_pixels);
initial_position_x = std::min(std::max(proposed_x, usable_bounds.x + margin_pixels), max_x);
initial_position_y = std::min(std::max(proposed_y, usable_bounds.y + margin_pixels), max_y);
}
const auto& scope_info_title = get_scope_info(type_);
const char* window_title = scope_info_title.title;
base_title_ = window_title;
last_window_title_ = window_title;
window_ = static_cast<SDL_Window*>(sdl_check_ptr(SDL_CreateWindow(window_title, initial_position_x, initial_position_y, window_width, window_height, window_flags), "SDL_CreateWindow"));
renderer_ = static_cast<SDL_Renderer*>(sdl_check_ptr(SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), "SDL_CreateRenderer"));
present_frame(nullptr, false);
window_width_ = window_width;
window_height_ = window_height;
window_id_ = SDL_GetWindowID(window_);
}
ScopeWindow::~ScopeWindow() {
destroy_graph();
if (texture_ != nullptr) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
}
if (renderer_ != nullptr) {
SDL_DestroyRenderer(renderer_);
renderer_ = nullptr;
}
if (window_ != nullptr) {
SDL_DestroyWindow(window_);
window_ = nullptr;
}
}
const char* ScopeWindow::type_to_string(const Type t) {
if (const auto* info = find_scope_info(t)) {
return info->type_string;
}
return "scope";
}
size_t ScopeWindow::index(const Type t) {
for (size_t i = 0; i < kNumScopes; ++i) {
if (kScopeInfos[i].type == t) {
return i;
}
}
return 0;
}
ScopeWindow::Type ScopeWindow::type_for_index(const size_t idx) {
if (idx < kNumScopes) {
return kScopeInfos[idx].type;
}
return Type::Histogram;
}
std::array<ScopeWindow::Type, ScopeWindow::kNumScopes> ScopeWindow::all_types() {
std::array<Type, kNumScopes> result{};
for (size_t i = 0; i < kNumScopes; ++i) {
result[i] = kScopeInfos[i].type;
}
return result;
}
std::string ScopeWindow::format_filter_args(const AVFrame* frame) {
#if (LIBAVFILTER_VERSION_INT < AV_VERSION_INT(10, 1, 100))
return std::string("video_size=") + std::to_string(frame->width) + "x" + std::to_string(frame->height) + ":pix_fmt=" + std::to_string(frame->format) + ":time_base=1/1:pixel_aspect=0/1";
#else
return std::string("video_size=") + std::to_string(frame->width) + "x" + std::to_string(frame->height) + ":pix_fmt=" + std::to_string(frame->format) + ":time_base=1/1:pixel_aspect=0/1:colorspace=" + std::to_string(frame->colorspace) +
":range=" + std::to_string(frame->color_range);
#endif
}
std::string ScopeWindow::build_filter_description(const int pane_width, const int pane_height, const int left_colorspace, const int left_range, const int right_colorspace, const int right_range, const bool roi_enabled, const Roi& roi)
const {
const std::string setparams_left = string_sprintf("setparams=colorspace=%d:range=%d", left_colorspace, left_range);
const std::string setparams_right = string_sprintf("setparams=colorspace=%d:range=%d", right_colorspace, right_range);
const int content_w = std::max(1, pane_width - kDividerGap);
const int content_h = std::max(1, pane_height - kDividerGap);
const std::string left_scale = string_sprintf("scale=%d:%d", content_w, content_h);
const std::string right_scale = string_sprintf("scale=%d:%d", content_w, content_h);
const std::string left_pad = string_sprintf("pad=%d:%d:%d:%d:color=black", pane_width, pane_height, kOuterPad, kOuterPad);
const std::string right_pad = string_sprintf("pad=%d:%d:%d:%d:color=black", pane_width, pane_height, kOuterPad, kOuterPad);
const std::string crop_left = roi_enabled ? string_sprintf("crop=%d:%d:%d:%d,", roi.w, roi.h, roi.x, roi.y) : "";
const std::string crop_right = crop_left;
const char* pre_format_filter;
if (type_ == Type::Histogram) {
pre_format_filter = use_10_bpc_ ? "format=gbrp10" : "format=gbrp";
} else {
pre_format_filter = use_10_bpc_ ? "format=yuv444p10le" : "format=yuv444p";
}
const char* tool_name = get_scope_info(type_).filter_name;
const std::string tool_spec = filter_options_.empty() ? std::string(tool_name) : string_sprintf("%s=%s", tool_name, filter_options_.c_str());
std::string filter_description = string_sprintf(
"[in_left]%s,%s%s,%s,%s,%s[left_scope];"
"[in_right]%s,%s%s,%s,%s,%s[right_scope];"
"[left_scope][right_scope]hstack=inputs=2,%s[out]",
setparams_left.c_str(), crop_left.c_str(), pre_format_filter, tool_spec.c_str(), left_scale.c_str(), left_pad.c_str(), setparams_right.c_str(), crop_right.c_str(), pre_format_filter, tool_spec.c_str(), right_scale.c_str(),
right_pad.c_str(), "format=rgb24");
return filter_description;
}
void ScopeWindow::destroy_graph() {
if (filter_graph_ != nullptr) {
avfilter_graph_free(&filter_graph_);
filter_graph_ = nullptr;
buffersrc_left_ctx_ = nullptr;
buffersrc_right_ctx_ = nullptr;
buffersink_ctx_ = nullptr;
}
}
void ScopeWindow::ensure_graph(const AVFrame* left_frame, const AVFrame* right_frame) {
Roi roi_local;
bool roi_enabled_local = false;
{
std::lock_guard<std::mutex> lock(state_mutex_);
roi_local = roi_;
roi_enabled_local = roi_enabled_;
}
Roi roi_effective{0, 0, 0, 0};
if (roi_enabled_local) {
roi_effective = clamp_roi_to_frame(roi_local, left_frame->width, left_frame->height);
roi_enabled_local = roi_effective.w > 0 && roi_effective.h > 0;
}
bool must_reinitialize = left_frame->width != left_input_.width || left_frame->height != left_input_.height || left_frame->format != left_input_.format || right_frame->width != right_input_.width ||
right_frame->height != right_input_.height || right_frame->format != right_input_.format || left_frame->colorspace != left_input_.colorspace || left_frame->color_range != left_input_.range ||
right_frame->colorspace != right_input_.colorspace || right_frame->color_range != right_input_.range;
// Trigger rebuild when ROI changes
const bool roi_changed = roi_enabled_local && (prev_roi_.x != roi_effective.x || prev_roi_.y != roi_effective.y || prev_roi_.w != roi_effective.w || prev_roi_.h != roi_effective.h);
must_reinitialize = must_reinitialize || roi_changed;
if (!must_reinitialize && filter_graph_ != nullptr) {
return;
}
destroy_graph();
left_input_.width = left_frame->width;
left_input_.height = left_frame->height;
left_input_.format = left_frame->format;
right_input_.width = right_frame->width;
right_input_.height = right_frame->height;
right_input_.format = right_frame->format;
left_input_.colorspace = left_frame->colorspace;
left_input_.range = left_frame->color_range;
right_input_.colorspace = right_frame->colorspace;
right_input_.range = right_frame->color_range;
prev_roi_ = roi_enabled_local ? roi_effective : Roi{-1, -1, -1, -1};
{
std::lock_guard<std::mutex> lock(state_mutex_);
source_width_ = left_frame->width;
source_height_ = left_frame->height;
}
const AVFilter* buffersrc = avfilter_get_by_name("buffer");
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
filter_graph_ = avfilter_graph_alloc();
if (!filter_graph_) {
throw std::runtime_error("avfilter_graph_alloc failed");
}
ffmpeg_check(avfilter_graph_create_filter(&buffersrc_left_ctx_, buffersrc, "in_left", format_filter_args(left_frame).c_str(), nullptr, filter_graph_), "create left buffer");
ffmpeg_check(avfilter_graph_create_filter(&buffersrc_right_ctx_, buffersrc, "in_right", format_filter_args(right_frame).c_str(), nullptr, filter_graph_), "create right buffer");
ffmpeg_check(avfilter_graph_create_filter(&buffersink_ctx_, buffersink, "out", nullptr, nullptr, filter_graph_), "create sink");
std::string filter_description = build_filter_description(pane_width_, pane_height_, left_input_.colorspace, left_input_.range, right_input_.colorspace, right_input_.range, roi_enabled_local, roi_effective);
AVFilterInOut* inputs = avfilter_inout_alloc();
AVFilterInOut* outputs_left = avfilter_inout_alloc();
AVFilterInOut* outputs_right = avfilter_inout_alloc();
if (!inputs || !outputs_left || !outputs_right) {
throw std::runtime_error("avfilter_inout_alloc failed");
}
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx_;
inputs->pad_idx = 0;
inputs->next = nullptr;
outputs_left->name = av_strdup("in_left");
outputs_left->filter_ctx = buffersrc_left_ctx_;
outputs_left->pad_idx = 0;
outputs_left->next = outputs_right;
outputs_right->name = av_strdup("in_right");
outputs_right->filter_ctx = buffersrc_right_ctx_;
outputs_right->pad_idx = 0;
outputs_right->next = nullptr;
ffmpeg_check(avfilter_graph_parse_ptr(filter_graph_, filter_description.c_str(), &inputs, &outputs_left, nullptr), "graph parse");
ffmpeg_check(avfilter_graph_config(filter_graph_, nullptr), "graph config");
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs_left);
// outputs_right is freed via the chained free of outputs_left->next
}
void ScopeWindow::ensure_texture() {
if (texture_ == nullptr) {
texture_ = static_cast<SDL_Texture*>(sdl_check_ptr(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, pane_width_ * 2, pane_height_), "SDL_CreateTexture"));
}
}
void ScopeWindow::present_frame(const AVFrame* filtered_frame, const bool allow_cached) {
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 100);
sdl_check_bool(SDL_RenderClear(renderer_) == 0, "SDL_RenderClear");
if (filtered_frame != nullptr) {
sdl_check_bool(SDL_UpdateTexture(texture_, nullptr, filtered_frame->data[0], filtered_frame->linesize[0]) == 0, "SDL_UpdateTexture");
has_valid_texture_ = true;
}
if ((filtered_frame != nullptr) || (allow_cached && has_valid_texture_)) {
sdl_check_bool(SDL_RenderCopy(renderer_, texture_, nullptr, nullptr) == 0, "SDL_RenderCopy");
}
draw_left_right_overlay(renderer_, window_width_, window_height_, window_width_ / 2);
SDL_RenderPresent(renderer_);
}
bool ScopeWindow::prepare(const AVFrame* left_frame, const AVFrame* right_frame) {
if (!left_frame || !right_frame) {
return false;
}
bool roi_enabled_local = false;
{
std::lock_guard<std::mutex> lock(state_mutex_);
roi_enabled_local = roi_enabled_;
if (graph_reset_pending_) {
destroy_graph();
graph_reset_pending_ = false;
}
}
// If ROI is disabled, we still want to blank in render, but skip compute here.
if (!roi_enabled_local) {
std::lock_guard<std::mutex> lock(state_mutex_);
if (pending_frame_ != nullptr) {
av_frame_free(&pending_frame_);
pending_frame_ = nullptr;
}
return true;
}
ensure_graph(left_frame, right_frame);
// Drain any pending frames to prevent lag and ensure one-to-one updates
for (;;) {
AVFrame* pending = av_frame_alloc();
if (!pending) {
throw std::runtime_error("av_frame_alloc failed (pending)");
}
const int pending_result = av_buffersink_get_frame(buffersink_ctx_, pending);
av_frame_free(&pending);
if (pending_result != 0) {
break;
}
}
// Use synthetic, matched PTS for framesync to pair both inputs
AVFrame* left_clone = av_frame_clone(const_cast<AVFrame*>(left_frame));
AVFrame* right_clone = av_frame_clone(const_cast<AVFrame*>(right_frame));
if (!left_clone || !right_clone) {
if (left_clone) {
av_frame_free(&left_clone);
}
if (right_clone) {
av_frame_free(&right_clone);
}
throw std::runtime_error("av_frame_clone failed");
}
left_clone->pts = frame_counter_;
right_clone->pts = frame_counter_;
ffmpeg_check(av_buffersrc_add_frame_flags(buffersrc_left_ctx_, left_clone, AV_BUFFERSRC_FLAG_KEEP_REF), "feed left frame");
ffmpeg_check(av_buffersrc_add_frame_flags(buffersrc_right_ctx_, right_clone, AV_BUFFERSRC_FLAG_KEEP_REF), "feed right frame");
av_frame_free(&left_clone);
av_frame_free(&right_clone);
// Retrieve the freshest available frame, if any
AVFrame* latest_frame = nullptr;
for (;;) {
AVFrame* candidate = av_frame_alloc();
if (!candidate) {
throw std::runtime_error("av_frame_alloc failed");
}
const int receive_result = av_buffersink_get_frame(buffersink_ctx_, candidate);
if (receive_result != 0) {
av_frame_free(&candidate);
break;
}
if (latest_frame != nullptr) {
av_frame_free(&latest_frame);
}
latest_frame = candidate;
}
if (latest_frame != nullptr) {
std::lock_guard<std::mutex> lock(state_mutex_);
if (pending_frame_ != nullptr) {
av_frame_free(&pending_frame_);
pending_frame_ = nullptr;
}
pending_frame_ = latest_frame;
frame_counter_++;
last_pts_left_ = left_frame->pts;
last_pts_right_ = right_frame->pts;
return true;
}
return false;
}
void ScopeWindow::render() {
Roi roi_local;
bool roi_enabled_local = false;
int source_width = 0;
int source_height = 0;
{
std::lock_guard<std::mutex> lock(state_mutex_);
roi_local = roi_;
roi_enabled_local = roi_enabled_;
source_width = source_width_;
source_height = source_height_;
}
// Update title (main thread only) when ROI is smaller than the full frame.
std::string title = base_title_;
if (roi_enabled_local && source_width > 0 && source_height > 0) {
const Roi effective_roi = clamp_roi_to_frame(roi_local, source_width, source_height);
const bool is_full = (effective_roi.w == source_width && effective_roi.h == source_height);
if (!is_full) {
title += string_sprintf(" (%d,%d)-(%d,%d)", effective_roi.x, effective_roi.y, effective_roi.x + effective_roi.w - 1, effective_roi.y + effective_roi.h - 1);
}
}
if (title != last_window_title_) {
SDL_SetWindowTitle(window_, title.c_str());
last_window_title_ = title;
}
// Always clear/present to keep the window responsive.
if (!roi_enabled_local) {
ensure_texture();
// Blank the scope when ROI is disabled.
has_valid_texture_ = false;
present_frame(nullptr, false);
return;
}
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (texture_reset_pending_) {
if (texture_ != nullptr) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
}
texture_reset_pending_ = false;
}
}
ensure_texture();
AVFrame* to_present = nullptr;
{
std::lock_guard<std::mutex> lock(state_mutex_);
to_present = pending_frame_;
pending_frame_ = nullptr;
}
present_frame(to_present, true);
if (to_present != nullptr) {
av_frame_free(&to_present);
}
}
bool ScopeWindow::update(const AVFrame* left_frame, const AVFrame* right_frame) {
const bool prepared = prepare(left_frame, right_frame);
render();
return prepared;
}
bool ScopeWindow::handle_event(const SDL_Event& event) {
const Uint32 event_window_id = SDLEventInfo::window_id(event);
// Only process events directed to this window
if (event_window_id == 0 || event_window_id != window_id_) {
return false;
}
switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
close_requested_ = true;
} else if (event.window.event == SDL_WINDOWEVENT_RESIZED || event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
// Update internal sizing and reinitialize resources to match
const int new_width = event.window.data1;
const int new_height = event.window.data2;
if (new_width > 0 && new_height > 0) {
window_width_ = new_width;
window_height_ = new_height;
pane_width_ = std::max(1, window_width_ / 2);
pane_height_ = std::max(1, window_height_);
// Recreate resources on next cycle to avoid cross-thread SDL calls
{
std::lock_guard<std::mutex> lock(state_mutex_);
texture_reset_pending_ = true;
graph_reset_pending_ = true;
}
refresh_requested_.store(true, std::memory_order_relaxed);
}
}
return true;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
return true;
}
// For other events to this window that we do not explicitly handle, do not consume by default
return false;
}
bool ScopeWindow::consume_refresh_request() {
return refresh_requested_.exchange(false, std::memory_order_relaxed);
}
void ScopeWindow::set_roi(const Roi& roi) {
std::lock_guard<std::mutex> lock(state_mutex_);
roi_ = roi;
roi_enabled_ = roi.w > 0 && roi.h > 0;
graph_reset_pending_ = true;
}