From 951c3ef794a7ad331c2567e9bcdd09a45d8135e6 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 29 Nov 2026 19:15:29 -0500 Subject: [PATCH 1/3] libobs: extract obs_source_main_render and obs_source_render_filters verbatim (Refs #5972) Move obs_source_render_filters() and obs_source_main_render(), the two static draws obs_source_video_render() makes, and their hidden wrappers obs_source_video_render_render_filters() and obs_source_video_render_main_render() (no EXPORT) unchanged out of obs-source.c.h into obs-source-video-render-draws.c.h (live group libobs.core-source). source_render() stays static in obs-source.c.h and is reached through its existing hidden wrapper obs_source_default_render_source_render(), mapped onto its original name for this file, as obs-source-default-render.c.h does. The other caller of obs_source_main_render(), obs-source.c.h's hidden wrapper for obs_source_skip_video_filter(), now calls obs_source_video_render_main_render(). The video render registration test finds the two wrappers in the new file. No behavior change. The new cmocka test_source_video_render_draws characterizes them through the real libobs, rendering on the test thread inside the graphics context and inside a technique of the solid effect: a plain source draws through obs_source_default_render() with the default effect and the linear sRGB flag cleared, restored afterwards; an sRGB-aware source keeps the flag either way; a custom draw source gets NULL; a filter draws with the current effect, holding one more reference than the chain's, with rendering_filter set on its parent; a source with filters reached as a filter target gets the current effect, not the default one; and a custom draw source without data, reached through a skipped filter, draws nothing and the flag is restored. 6/6 against hybrid and the C reference. Co-Authored-By: Claude Opus 5.5 --- libobs/build/libobs.core-source+.h.c | 1 + libobs/obs-source-video-render-draws.c.h | 74 +++ libobs/obs-source.c.h | 62 +-- ..._source_video_render_swap_is_registered.rs | 18 +- test/cmocka/build/test-graph.json | 45 ++ test/cmocka/test_source_video_render_draws.c | 463 ++++++++++++++++++ .../test_source_video_render_draws.cmake | 7 + 7 files changed, 608 insertions(+), 62 deletions(-) create mode 100644 libobs/obs-source-video-render-draws.c.h create mode 100644 test/cmocka/test_source_video_render_draws.c create mode 100644 test/cmocka/tests.d/test_source_video_render_draws.cmake diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index 9ad6c4c589..0454650f6d 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -144,6 +144,7 @@ #include "libobs/obs-source-type-info-fields.c.h" #include "libobs/obs-source-update-fields.c.h" #include "libobs/obs-source-update-properties-fields.c.h" +#include "libobs/obs-source-video-render-draws.c.h" #include "libobs/obs-source-video-render-fields.c.h" #include "libobs/obs-source-video-tick-callbacks-fields.c.h" #include "libobs/obs-source-video-tick-entry-fields.c.h" diff --git a/libobs/obs-source-video-render-draws.c.h b/libobs/obs-source-video-render-draws.c.h new file mode 100644 index 0000000000..62ab0fdfbf --- /dev/null +++ b/libobs/obs-source-video-render-draws.c.h @@ -0,0 +1,74 @@ +#include "libobs/build/libobs.pch.h" + +/* obs_source_render_filters() and obs_source_main_render(), the two draws + * obs_source_video_render() makes (obs-source-video-render.c.h), and their + * hidden wrappers (no EXPORT), moved verbatim out of obs-source.c.h (#5972). + * Every decision is in obs-source-render-route.c; this keeps the lock, the + * references, the source's fields and the draws. obs_source_main_render() is + * also the main draw of obs_source_skip_video_filter(), which obs-source.c.h + * reaches through obs_source_video_render_main_render(). + * + * obs-source.c.h's source_render() is static there; this extraction reaches + * it through its hidden wrapper (no EXPORT), under the original name, scoped + * to this file. */ +void obs_source_default_render_source_render(obs_source_t *source, gs_effect_t *effect); +#define source_render obs_source_default_render_source_render + +static inline void obs_source_render_filters(obs_source_t *source) +{ + obs_source_t *first_filter; + + pthread_mutex_lock(&source->filter_mutex); + first_filter = obs_source_get_ref(source->filters.array[0]); + pthread_mutex_unlock(&source->filter_mutex); + + source->rendering_filter = true; + obs_source_video_render(first_filter); + source->rendering_filter = false; + + obs_source_release(first_filter); +} + +static inline void obs_source_main_render(obs_source_t *source) +{ + uint32_t flags = source->info.output_flags; + bool custom_draw = obs_source_render_route_main_custom_draw(flags); + bool srgb_aware = obs_source_render_route_main_srgb_aware(flags); + const bool has_filter_parent = source->filter_parent != NULL; + bool default_effect = obs_source_render_route_main_default_effect( + flags, has_filter_parent, + obs_source_render_route_main_reads_filters(has_filter_parent) && source->filters.num != 0); + bool previous_srgb = false; + + if (!srgb_aware) { + previous_srgb = gs_get_linear_srgb(); + gs_set_linear_srgb(false); + } + + if (default_effect) { + obs_source_default_render(source); + } else if (source->context.data) { + source_render(source, custom_draw ? NULL : gs_get_effect()); + } + + if (!srgb_aware) + gs_set_linear_srgb(previous_srgb); +} + +/* The static draws obs_source_video_render() makes + * (obs-source-video-render.c.h). Hidden: no EXPORT. obs_source_video_render() + * and its source_render_video(), source_draw_route() and get_type_format() + * steps are there (#5962). */ +void obs_source_video_render_render_filters(obs_source_t *source); +void obs_source_video_render_render_filters(obs_source_t *source) +{ + obs_source_render_filters(source); +} + +void obs_source_video_render_main_render(obs_source_t *source); +void obs_source_video_render_main_render(obs_source_t *source) +{ + obs_source_main_render(source); +} + +#undef source_render diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index 8e3b6cd208..557f485c01 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -552,21 +552,6 @@ void obs_source_update_async_video(obs_source_t *source); * (#5941); no EXPORT. */ void obs_source_render_async_video(obs_source_t *source); -static inline void obs_source_render_filters(obs_source_t *source) -{ - obs_source_t *first_filter; - - pthread_mutex_lock(&source->filter_mutex); - first_filter = obs_source_get_ref(source->filters.array[0]); - pthread_mutex_unlock(&source->filter_mutex); - - source->rendering_filter = true; - obs_source_video_render(first_filter); - source->rendering_filter = false; - - obs_source_release(first_filter); -} - _Static_assert(OBS_SOURCE_SIZE_TYPE_FILTER == OBS_SOURCE_TYPE_FILTER, "OBS_SOURCE_TYPE_FILTER"); _Static_assert(OBS_SOURCE_SIZE_TYPE_TRANSITION == OBS_SOURCE_TYPE_TRANSITION, "OBS_SOURCE_TYPE_TRANSITION"); _Static_assert(sizeof(enum obs_source_type) == sizeof(int), "enum obs_source_type is passed as int"); @@ -664,47 +649,12 @@ _Static_assert(sizeof(enum obs_allow_direct_render) == sizeof(int), "enum obs_al _Static_assert(sizeof(enum gs_color_space) == sizeof(int), "enum gs_color_space is passed as int"); _Static_assert(sizeof(enum gs_color_format) == sizeof(int), "enum gs_color_format is passed as int"); -static inline void obs_source_main_render(obs_source_t *source) -{ - uint32_t flags = source->info.output_flags; - bool custom_draw = obs_source_render_route_main_custom_draw(flags); - bool srgb_aware = obs_source_render_route_main_srgb_aware(flags); - const bool has_filter_parent = source->filter_parent != NULL; - bool default_effect = obs_source_render_route_main_default_effect( - flags, has_filter_parent, - obs_source_render_route_main_reads_filters(has_filter_parent) && source->filters.num != 0); - bool previous_srgb = false; - - if (!srgb_aware) { - previous_srgb = gs_get_linear_srgb(); - gs_set_linear_srgb(false); - } - - if (default_effect) { - obs_source_default_render(source); - } else if (source->context.data) { - source_render(source, custom_draw ? NULL : gs_get_effect()); - } - - if (!srgb_aware) - gs_set_linear_srgb(previous_srgb); -} - -/* The static draws obs_source_video_render() makes - * (obs-source-video-render.c.h). Hidden: no EXPORT. obs_source_video_render() - * and its source_render_video(), source_draw_route() and get_type_format() - * steps are there (#5962). */ -void obs_source_video_render_render_filters(obs_source_t *source); -void obs_source_video_render_render_filters(obs_source_t *source) -{ - obs_source_render_filters(source); -} - +/* obs_source_render_filters() and obs_source_main_render(), the static draws + * obs_source_video_render() makes, and their hidden wrappers + * obs_source_video_render_render_filters() and + * obs_source_video_render_main_render() (no EXPORT) are in + * obs-source-video-render-draws.c.h (#5972). */ void obs_source_video_render_main_render(obs_source_t *source); -void obs_source_video_render_main_render(obs_source_t *source) -{ - obs_source_main_render(source); -} /* Where obs_source_get_width() and obs_source_get_height() start a source * that is not a filter. The caller holds its filter_mutex. */ @@ -890,7 +840,7 @@ _Static_assert(OBS_SOURCE_ASYNC_PACING_MAX_TS_VAR == MAX_TS_VAR, "MAX_TS_VAR"); void obs_source_skip_video_filter_main_render(obs_source_t *source); void obs_source_skip_video_filter_main_render(obs_source_t *source) { - obs_source_main_render(source); + obs_source_video_render_main_render(source); } void obs_source_skip_video_filter_async_video(obs_source_t *source); diff --git a/rust/obs-build/tests/native/registration/canonical_source_video_render_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_source_video_render_swap_is_registered.rs index 9effbc6c38..6198e5db2d 100644 --- a/rust/obs-build/tests/native/registration/canonical_source_video_render_swap_is_registered.rs +++ b/rust/obs-build/tests/native/registration/canonical_source_video_render_swap_is_registered.rs @@ -36,13 +36,19 @@ fn canonical_source_video_render_swap_is_registered() { let hybrid = fs::read_to_string(root.join("libobs/build/libobs.core-source+.h.c")).unwrap(); assert!(!hybrid.contains("#include \"libobs/obs-source-video-render.c.h\"")); assert!(hybrid.contains("#include \"libobs/obs-source-video-render-fields.c.h\"")); - // obs-source.c.h keeps the two hidden draw wrappers, and no copy of the - // render or its steps. + // The two hidden draw wrappers moved with their draws into + // obs-source-video-render-draws.c.h (#5972); obs-source.c.h keeps no + // copy of them, the render or its steps. + let draws = fs::read_to_string(root.join("libobs/obs-source-video-render-draws.c.h")).unwrap(); + assert!(draws.contains("void obs_source_video_render_render_filters(obs_source_t *source)\n{")); + assert!(draws.contains("void obs_source_video_render_main_render(obs_source_t *source)\n{")); let source = fs::read_to_string(root.join("libobs/obs-source.c.h")).unwrap(); - assert!( - source.contains("void obs_source_video_render_render_filters(obs_source_t *source)\n{") - ); - assert!(source.contains("void obs_source_video_render_main_render(obs_source_t *source)\n{")); + for wrapper in [ + "void obs_source_video_render_render_filters(obs_source_t *source)\n{", + "void obs_source_video_render_main_render(obs_source_t *source)\n{", + ] { + assert!(!source.contains(wrapper), "{wrapper}"); + } for step in [ "void obs_source_video_render(obs_source_t *source)\n{", "source_render_video(obs_source_t *source)", diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 1c8bf78e1b..cee666e4db 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -20205,6 +20205,34 @@ "libobs/data" ] }, + "test_source_video_render_draws": { + "sources": [ + "test/cmocka/test_source_video_render_draws.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + { + "name": "m", + "requires": { + "not": "MSVC" + } + }, + "OBS::native-test-cmocka" + ], + "requires": { + "all": [ + "OS_LINUX", + "HAVE_OBS_OPENGL" + ] + }, + "artifact_definitions": { + "GRAPHICS_MODULE_PATH": "OBS::libobs-opengl" + }, + "watch_inputs": [ + "libobs/data" + ] + }, "test_source_video_tick_callbacks": { "sources": [ "test/cmocka/test_source_video_tick_callbacks.c" @@ -31912,6 +31940,23 @@ "working_directory": "${runtime}", "timeout_seconds": 120 }, + "test_source_video_render_draws": { + "target": "test_source_video_render_draws", + "command": [ + "${target}" + ], + "suite": "graphics", + "working_directory": "${runtime}", + "timeout_seconds": 120, + "optional_prefixes": [ + [ + "xvfb-run", + "-a", + "-s", + "-screen 0 1280x1024x24 -noreset" + ] + ] + }, "test_source_video_tick_callbacks": { "target": "test_source_video_tick_callbacks", "command": [ diff --git a/test/cmocka/test_source_video_render_draws.c b/test/cmocka/test_source_video_render_draws.c new file mode 100644 index 0000000000..a7d88e2d2f --- /dev/null +++ b/test/cmocka/test_source_video_render_draws.c @@ -0,0 +1,463 @@ +/* Characterize obs_source_main_render() and obs_source_render_filters() + * (#5972), the two draws obs_source_video_render() makes, through the real + * libobs (hybrid and the C reference). Both are hidden, so each case reaches + * them through obs_source_video_render(). + * + * obs_source_main_render() gives a source that is not a filter, has no + * filters and is not OBS_SOURCE_CUSTOM_DRAW to obs_source_default_render(), + * which draws it with the default effect; any other source with data gets + * the current effect (gs_get_effect()), or NULL when it is custom draw. A + * source without OBS_SOURCE_SRGB draws with the linear sRGB flag cleared, + * and the flag is restored afterwards; an sRGB-aware source keeps it. A + * source without data draws nothing. obs_source_render_filters() draws the + * first filter holding one more reference to it, with rendering_filter set + * on its parent. + * + * Each case renders on the test thread inside the graphics context, inside + * a technique of the solid effect, so the current effect is that effect, + * and records the effect and the linear sRGB flag each video_render + * callback saw. + * + * Left to the Rust swap's Tier 1 and Tier 3: the order in which the fields + * are read and the lock. These are characterized, not endorsed. Rendering + * needs a display, so the cases run (under xvfb-run) on Linux and skip + * without one. */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#define PLAIN_ID "render_draws_plain" +#define SRGB_ID "render_draws_srgb" +#define CUSTOM_ID "render_draws_custom" +#define NO_DATA_ID "render_draws_no_data" +#define SKIP_FILTER_ID "render_draws_skip_filter" +#define PASS_FILTER_ID "render_draws_pass_filter" +#define FRAME_SIZE 16 +#define CANVAS_SIZE 64 +#define MAX_DRAWS 8 + +static bool obs_up; + +/* ------------------------------------------------------------------------- */ +/* what the callbacks saw */ + +struct draw { + /* 'P' a plain source, 'F' a filter */ + char who; + /* the effect the callback was given */ + gs_effect_t *effect; + /* gs_get_linear_srgb() inside the callback */ + bool linear_srgb; + /* the source's own reference count while it drew */ + long refs; + /* its filter parent's rendering_filter, false without a parent */ + bool parent_rendering_filter; +}; + +static struct { + int count; + struct draw draw[MAX_DRAWS]; +} drawn; + +static void record(char who, obs_source_t *source, gs_effect_t *effect) +{ + if (drawn.count >= MAX_DRAWS) + return; + struct draw *d = &drawn.draw[drawn.count++]; + d->who = who; + d->effect = effect; + d->linear_srgb = gs_get_linear_srgb(); + d->refs = os_atomic_load_long(&source->context.control->ref.refs); + d->parent_rendering_filter = source->filter_parent && source->filter_parent->rendering_filter; +} + +static long refs_of(obs_source_t *source) +{ + return os_atomic_load_long(&source->context.control->ref.refs); +} + +/* ------------------------------------------------------------------------- */ +/* sources */ + +static const char *any_name(void *type_data) +{ + (void)type_data; + return "video render draws"; +} + +static void *keep_source(obs_data_t *settings, obs_source_t *source) +{ + (void)settings; + return source; +} + +/* create fails: the source is kept, without data */ +static void *no_data(obs_data_t *settings, obs_source_t *source) +{ + (void)settings; + (void)source; + return NULL; +} + +static void no_destroy(void *data) +{ + (void)data; +} + +static uint32_t any_size(void *data) +{ + (void)data; + return FRAME_SIZE; +} + +static void plain_render(void *data, gs_effect_t *effect) +{ + record('P', data, effect); +} + +/* The filter records, then skips itself (draws its parent's main render). */ +static void skip_filter_render(void *data, gs_effect_t *effect) +{ + record('F', data, effect); + obs_source_skip_video_filter(data); +} + +/* Lets the video thread drop the references it takes while ticking, then + * waits for the deferred destroys. */ +static void settle(void) +{ + uint32_t start = obs_get_total_frames(); + for (int i = 0; i < 500 && obs_get_total_frames() - start < 2; i++) + os_sleep_ms(10); + obs_wait_for_destroy_queue(); +} + +/* What render() saw around the draw. */ +static struct { + gs_effect_t *solid; + gs_effect_t *base_default; + /* gs_get_linear_srgb() right after obs_source_video_render() */ + bool linear_srgb_after; +} seen; + +/* Renders the source into a CANVAS_SIZE texture on the calling thread, + * inside a technique of the solid effect and with the linear sRGB flag set + * to `linear_srgb`, and clears what the callbacks saw first. */ +static void render(obs_source_t *source, bool linear_srgb) +{ + memset(&drawn, 0, sizeof(drawn)); + obs_enter_graphics(); + seen.solid = obs_get_base_effect(OBS_EFFECT_SOLID); + seen.base_default = obs_get_base_effect(OBS_EFFECT_DEFAULT); + gs_texrender_t *tr = gs_texrender_create(GS_RGBA, GS_ZS_NONE); + + if (gs_texrender_begin(tr, CANVAS_SIZE, CANVAS_SIZE)) { + struct vec4 clear; + vec4_zero(&clear); + gs_clear(GS_CLEAR_COLOR, &clear, 0.0f, 0); + gs_ortho(0.0f, (float)CANVAS_SIZE, 0.0f, (float)CANVAS_SIZE, -100.0f, 100.0f); + gs_blend_state_push(); + gs_reset_blend_state(); + + const bool previous = gs_get_linear_srgb(); + gs_set_linear_srgb(linear_srgb); + gs_technique_t *tech = gs_effect_get_technique(seen.solid, "Solid"); + gs_technique_begin(tech); + obs_source_video_render(source); + gs_technique_end(tech); + seen.linear_srgb_after = gs_get_linear_srgb(); + gs_set_linear_srgb(previous); + + gs_blend_state_pop(); + gs_texrender_end(tr); + } + + gs_texrender_destroy(tr); + obs_leave_graphics(); +} + +static obs_source_t *create(const char *id) +{ + obs_source_t *source = obs_source_create_private(id, id, NULL); + assert_non_null(source); + return source; +} + +/* ------------------------------------------------------------------------- */ +/* obs_source_main_render() */ + +/* Not a filter, no filters, not custom draw: obs_source_default_render() + * draws it with the default effect. Without OBS_SOURCE_SRGB it draws with + * the linear sRGB flag cleared, and the flag is back afterwards. */ +static void plain_source_draws_with_the_default_effect(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *source = create(PLAIN_ID); + render(source, true); + assert_int_equal(drawn.count, 1); + assert_int_equal(drawn.draw[0].who, 'P'); + assert_ptr_equal(drawn.draw[0].effect, seen.base_default); + assert_false(drawn.draw[0].linear_srgb); + assert_true(seen.linear_srgb_after); + + render(source, false); + assert_int_equal(drawn.count, 1); + assert_false(drawn.draw[0].linear_srgb); + assert_false(seen.linear_srgb_after); + obs_source_release(source); + settle(); +} + +/* OBS_SOURCE_SRGB: the flag is left as the caller set it, either way. */ +static void srgb_aware_source_keeps_the_linear_srgb_flag(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *source = create(SRGB_ID); + render(source, true); + assert_int_equal(drawn.count, 1); + assert_ptr_equal(drawn.draw[0].effect, seen.base_default); + assert_true(drawn.draw[0].linear_srgb); + assert_true(seen.linear_srgb_after); + + render(source, false); + assert_int_equal(drawn.count, 1); + assert_false(drawn.draw[0].linear_srgb); + assert_false(seen.linear_srgb_after); + obs_source_release(source); + settle(); +} + +/* OBS_SOURCE_CUSTOM_DRAW: no default effect, and NULL instead of the + * current effect. */ +static void custom_draw_source_gets_no_effect(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *source = create(CUSTOM_ID); + render(source, true); + assert_int_equal(drawn.count, 1); + assert_int_equal(drawn.draw[0].who, 'P'); + assert_null(drawn.draw[0].effect); + assert_false(drawn.draw[0].linear_srgb); + assert_true(seen.linear_srgb_after); + obs_source_release(source); + settle(); +} + +/* A source with a filter: obs_source_render_filters() draws the filter + * with one more reference than the chain's own and rendering_filter set on + * the parent. The filter (it has a parent) draws through its main render + * with the current effect and the linear sRGB flag cleared; it skips + * itself, and obs_source_skip_video_filter() draws the parent (not custom + * draw) with obs_source_default_render(), so with the default effect. */ +static void filters_draw_with_the_current_effect(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *parent = create(PLAIN_ID); + obs_source_t *filter = create(SKIP_FILTER_ID); + obs_source_filter_add(parent, filter); + const long filter_refs = refs_of(filter); + + render(parent, true); + assert_int_equal(drawn.count, 2); + assert_int_equal(drawn.draw[0].who, 'F'); + assert_ptr_equal(drawn.draw[0].effect, seen.solid); + assert_false(drawn.draw[0].linear_srgb); + assert_true(drawn.draw[0].parent_rendering_filter); + /* the chain's reference and the filter's own render's */ + assert_int_equal(drawn.draw[0].refs, filter_refs + 2); + assert_int_equal(drawn.draw[1].who, 'P'); + assert_ptr_equal(drawn.draw[1].effect, seen.base_default); + assert_true(seen.linear_srgb_after); + assert_false(parent->rendering_filter); + assert_int_equal(refs_of(filter), filter_refs); + + obs_source_filter_remove(parent, filter); + obs_source_release(filter); + obs_source_release(parent); + settle(); +} + +/* A filter without video_render draws its target, the parent, which has + * filters: its main render gives it the current effect, not the default + * one, with the linear sRGB flag cleared. */ +static void a_source_with_filters_gets_the_current_effect(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *parent = create(PLAIN_ID); + obs_source_t *filter = create(PASS_FILTER_ID); + obs_source_filter_add(parent, filter); + + render(parent, true); + assert_int_equal(drawn.count, 1); + assert_int_equal(drawn.draw[0].who, 'P'); + assert_ptr_equal(drawn.draw[0].effect, seen.solid); + assert_false(drawn.draw[0].linear_srgb); + assert_true(seen.linear_srgb_after); + assert_false(parent->rendering_filter); + + obs_source_filter_remove(parent, filter); + obs_source_release(filter); + obs_source_release(parent); + settle(); +} + +/* A custom draw parent without data, reached through a filter rendered on + * its own: the filter draws with the current effect and skips itself, and + * obs_source_skip_video_filter() gives the parent (custom draw, with a + * video_render) to its main render, which draws nothing without data; the + * flag is back afterwards. */ +static void source_without_data_draws_nothing(void **state) +{ + (void)state; + if (!obs_up) + skip(); + + obs_source_t *parent = create(NO_DATA_ID); + assert_null(parent->context.data); + obs_source_t *filter = create(SKIP_FILTER_ID); + obs_source_filter_add(parent, filter); + + render(filter, true); + assert_int_equal(drawn.count, 1); + assert_int_equal(drawn.draw[0].who, 'F'); + assert_ptr_equal(drawn.draw[0].effect, seen.solid); + assert_false(drawn.draw[0].linear_srgb); + assert_false(drawn.draw[0].parent_rendering_filter); + assert_true(seen.linear_srgb_after); + + obs_source_filter_remove(parent, filter); + obs_source_release(filter); + obs_source_release(parent); + settle(); +} + +static int setup(void **state) +{ + (void)state; + /* obs_startup() needs a display on Linux (run under xvfb-run); the + * cases skip without one. */ + obs_up = obs_startup("en-US", NULL, NULL); + if (!obs_up) { + fprintf(stderr, "skipped: obs_startup failed (no display?)\n"); + return 0; + } + + struct obs_video_info ovi = { + .graphics_module = GRAPHICS_MODULE_PATH, + .fps_num = 30, + .fps_den = 1, + .base_width = CANVAS_SIZE, + .base_height = CANVAS_SIZE, + .output_width = CANVAS_SIZE, + .output_height = CANVAS_SIZE, + .output_format = VIDEO_FORMAT_NV12, + .colorspace = VIDEO_CS_709, + .range = VIDEO_RANGE_PARTIAL, + .scale_type = OBS_SCALE_BICUBIC, + }; + int ret = obs_reset_video(&ovi); + if (ret != OBS_VIDEO_SUCCESS) { + fprintf(stderr, "skipped: obs_reset_video failed: %d\n", ret); + obs_shutdown(); + obs_up = false; + return 0; + } + + struct obs_source_info plain = { + .id = PLAIN_ID, + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_VIDEO, + .get_name = any_name, + .create = keep_source, + .destroy = no_destroy, + .get_width = any_size, + .get_height = any_size, + .video_render = plain_render, + }; + obs_register_source(&plain); + + struct obs_source_info srgb = plain; + srgb.id = SRGB_ID; + srgb.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB; + obs_register_source(&srgb); + + struct obs_source_info custom = plain; + custom.id = CUSTOM_ID; + custom.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW; + obs_register_source(&custom); + + struct obs_source_info none = plain; + none.id = NO_DATA_ID; + none.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW; + none.create = no_data; + obs_register_source(&none); + + struct obs_source_info skip_filter = { + .id = SKIP_FILTER_ID, + .type = OBS_SOURCE_TYPE_FILTER, + .output_flags = OBS_SOURCE_VIDEO, + .get_name = any_name, + .create = keep_source, + .destroy = no_destroy, + .video_render = skip_filter_render, + }; + obs_register_source(&skip_filter); + + struct obs_source_info pass_filter = skip_filter; + pass_filter.id = PASS_FILTER_ID; + pass_filter.video_render = NULL; + obs_register_source(&pass_filter); + + /* Let the graphics thread get through its first frames before the + * cases render on its context. */ + for (int i = 0; i < 500 && obs_get_total_frames() < 3; i++) + os_sleep_ms(10); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + if (obs_up) + obs_shutdown(); + return 0; +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(plain_source_draws_with_the_default_effect), + cmocka_unit_test(srgb_aware_source_keeps_the_linear_srgb_flag), + cmocka_unit_test(custom_draw_source_gets_no_effect), + cmocka_unit_test(filters_draw_with_the_current_effect), + cmocka_unit_test(a_source_with_filters_gets_the_current_effect), + cmocka_unit_test(source_without_data_draws_nothing), + }; + + return cmocka_run_group_tests(tests, setup, teardown); +} diff --git a/test/cmocka/tests.d/test_source_video_render_draws.cmake b/test/cmocka/tests.d/test_source_video_render_draws.cmake new file mode 100644 index 0000000000..542b4bd7e0 --- /dev/null +++ b/test/cmocka/tests.d/test_source_video_render_draws.cmake @@ -0,0 +1,7 @@ +# obs_source_main_render() and obs_source_render_filters() (#5972), the two +# draws obs_source_video_render() makes, reached through it on the test +# thread inside the graphics context: the default effect, the current effect +# or none, the linear sRGB flag cleared and restored (kept for an sRGB-aware +# source), a source without data, and the first filter drawn inside one more +# reference. It runs (under xvfb-run) on Linux and skips without a display. +# Canonical target and test: test/cmocka/build/test-graph.json. From c0eaecdf0759e2caa969e6e883b5d31e19fca747 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 29 Nov 2026 19:18:48 -0500 Subject: [PATCH 2/3] obs-source: Tier 1 for obs_source_main_render and obs_source_render_filters, RED (Refs #5972) The safe core's Host trait (filter_mutex, filters.array[0], obs_source_get_ref() and obs_source_release(), rendering_filter, obs_source_video_render(), info.output_flags, filter_parent, filters.num, gs_get_linear_srgb() and gs_set_linear_srgb(), obs_source_default_render(), context.data, gs_get_effect() and source_render()) and todo!() steps, with 22 Tier 1 tests over a recording model whose hooks change the source and the graphics state from inside a call. render_filters: the first filter read and referenced under the lock, then drawn with rendering_filter set and the lock free, then released; the reference held while it draws; a filter being destroyed and a NULL first filter passed on as NULL; the first filter read once (a reorder after the unlock changes nothing); rendering_filter cleared after the draw; no other field read. main_render: a plain source through the default render with the flag cleared and restored; an sRGB-aware source leaves the flag alone; a custom draw gets NULL and never asks for the effect; a source with filters gets the current effect (NULL passed on); a filter never reads its filter count; without data nothing is drawn; the default render reads no data; the output flags read once; the route decided before the flag is saved; the data read after the flag is cleared; the effect read right before the draw; the saved flag set back whatever the draw did; and over 64 states at most one draw and the flag restored. soldr cargo test -p obs-data --test obs-tests-core source::video_render_draws test result: FAILED. 0 passed; 22 failed (not yet implemented: obs_source_main_render / obs_source_render_filters) Co-Authored-By: Claude Opus 5.5 --- rust/obs-source/src/lib.rs | 7 +- rust/obs-source/src/video_render_draws.rs | 108 +++ rust/obs-source/tests/source/main.rs | 1 + .../tests/source/video_render_draws.rs | 714 ++++++++++++++++++ 4 files changed, 829 insertions(+), 1 deletion(-) create mode 100644 rust/obs-source/src/video_render_draws.rs create mode 100644 rust/obs-source/tests/source/video_render_draws.rs diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index 45d942d60b..f94e4f270c 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -274,6 +274,10 @@ //! `source_render_video()`, `source_draw_route()` and `get_type_format()` //! (the validity check, the reference, the fields, the skip, the updates //! and the draws stay in a C fields adapter); +//! `libobs/obs-source-video-render-draws.c.h`, its two draws +//! `obs_source_render_filters()` and `obs_source_main_render()`, through +//! their hidden wrappers (the lock, the fields, the references and the +//! calls stay in a C fields adapter); //! `libobs/obs-source-video-tick.c.h`, the per-frame decisions of //! `obs_source_video_tick()` and `async_tick()` (which steps run, the //! deferred update, the show and activate edges, the plugin callback and @@ -302,7 +306,7 @@ //! [`transition_slots`], //! [`transition_start`], [`transition_stop`], [`transition_switch`], //! [`transition_video`], -//! [`video_render`], [`video_tick`], [`video_tick_callbacks`], [`video_tick_entry`], [`volume_apply`] and +//! [`video_render`], [`video_render_draws`], [`video_tick`], [`video_tick_callbacks`], [`video_tick_entry`], [`volume_apply`] and //! [`volume_mul`]; [`draw_technique`] holds the //! technique and colour space values [`deinterlace_draw`] and [`draw_plan`] //! share. The C @@ -451,6 +455,7 @@ pub mod type_info; pub mod update; pub mod update_properties; pub mod video_render; +pub mod video_render_draws; pub mod video_tick; pub mod video_tick_callbacks; pub mod video_tick_entry; diff --git a/rust/obs-source/src/video_render_draws.rs b/rust/obs-source/src/video_render_draws.rs new file mode 100644 index 0000000000..ab51883e43 --- /dev/null +++ b/rust/obs-source/src/video_render_draws.rs @@ -0,0 +1,108 @@ +//! Safe core of `libobs/obs-source-video-render-draws.c.h`: +//! `obs_source_render_filters()` and `obs_source_main_render()`, the two +//! draws `obs_source_video_render()` makes ([`crate::video_render`]), +//! reached through their hidden wrappers +//! `obs_source_video_render_render_filters()` and +//! `obs_source_video_render_main_render()` (no `EXPORT`). +//! `obs_source_main_render()` is also the main draw of +//! `obs_source_skip_video_filter()`. +//! +//! [`render_filters`] keeps the C's lock, reference and calls in their +//! order: +//! +//! - `filter_mutex` locked; `filters.array[0]` read and +//! `obs_source_get_ref()` taken on it; `filter_mutex` unlocked. +//! - `rendering_filter` set, `obs_source_video_render()` of that reference +//! (NULL when none could be taken), `rendering_filter` cleared. +//! - `obs_source_release()` of the reference. +//! +//! [`main_render`] keeps the C's reads and calls in their order: +//! +//! - `info.output_flags` read once, for +//! [`render_route::main_custom_draw`] and +//! [`render_route::main_srgb_aware`]; +//! - `filter_parent`, then `filters.num` only when +//! [`render_route::main_reads_filters`] says so, for +//! [`render_route::main_default_effect`]; +//! - when not sRGB aware, `gs_get_linear_srgb()` saved and +//! `gs_set_linear_srgb(false)`; +//! - the default effect: `obs_source_default_render()`; otherwise, only +//! when `context.data` is set, `source_render()` with NULL for a custom +//! draw or `gs_get_effect()`; +//! - when not sRGB aware, the saved flag set back. +//! +//! Every decision is [`render_route`]'s. The lock, the fields, the +//! references and every call stay C, reached through the [`Host`] trait one +//! access at a time. +//! +//! No intentional differences from C. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::unimplemented, + clippy::unreachable +)] + +/// The source's lock, fields and references and the calls the two draws +/// make, in the C's terms. +pub trait Host { + /// A non-null source as the C passes it. + type Source: Copy; + /// A non-null effect as the C passes it. + type Effect: Copy; + + /// `pthread_mutex_lock(&source->filter_mutex)`. + fn lock_filters(&self, source: Self::Source); + /// `source->filters.array[0]`; `None` is NULL. + fn first_filter(&self, source: Self::Source) -> Option; + /// `obs_source_get_ref(source)`: NULL (`None`) for NULL or a source + /// being destroyed. + fn get_ref(&self, source: Option) -> Option; + /// `pthread_mutex_unlock(&source->filter_mutex)`. + fn unlock_filters(&self, source: Self::Source); + /// `source->rendering_filter = rendering`. + fn set_rendering_filter(&self, source: Self::Source, rendering: bool); + /// `obs_source_video_render(source)`; `None` is NULL. + fn video_render(&self, source: Option); + /// `obs_source_release(source)`; `None` is NULL. + fn release(&self, source: Option); + /// `source->info.output_flags`. + fn output_flags(&self, source: Self::Source) -> u32; + /// `source->filter_parent != NULL`. + fn has_filter_parent(&self, source: Self::Source) -> bool; + /// `source->filters.num != 0`. + fn has_filters(&self, source: Self::Source) -> bool; + /// `gs_get_linear_srgb()`. + fn linear_srgb(&self) -> bool; + /// `gs_set_linear_srgb(linear_srgb)`. + fn set_linear_srgb(&self, linear_srgb: bool); + /// `obs_source_default_render(source)`. + fn default_render(&self, source: Self::Source); + /// `source->context.data != NULL`. + fn has_data(&self, source: Self::Source) -> bool; + /// `gs_get_effect()`; `None` is NULL. + fn current_effect(&self) -> Option; + /// `source_render(source, effect)`; `None` is NULL. + fn source_render(&self, source: Self::Source, effect: Option); +} + +/// `obs_source_render_filters(source)`: the first filter, referenced under +/// `filter_mutex`, drawn with `rendering_filter` set, then released. +pub fn render_filters(host: &H, source: H::Source) { + let _ = (host, source); + todo!("obs_source_render_filters") +} + +/// `obs_source_main_render(source)`: the default render, or the plugin's +/// draw with the current effect (none for a custom draw) when the source +/// has data, with the linear sRGB flag cleared around it unless the source +/// is sRGB aware. +pub fn main_render(host: &H, source: H::Source) { + let _ = (host, source); + todo!("obs_source_main_render") +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 2e1fc1145e..6b300e4ad2 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -264,6 +264,7 @@ mod update_parity; mod update_properties; mod update_properties_parity; mod video_render; +mod video_render_draws; mod video_render_parity; mod video_tick; mod video_tick_callbacks; diff --git a/rust/obs-source/tests/source/video_render_draws.rs b/rust/obs-source/tests/source/video_render_draws.rs new file mode 100644 index 0000000000..941e79fcf9 --- /dev/null +++ b/rust/obs-source/tests/source/video_render_draws.rs @@ -0,0 +1,714 @@ +//! Tier 1: `obs_source::video_render_draws` over a recording model of a +//! source, its filters and the graphics state: `filter_mutex`, +//! `filters.array[0]`, the references, `rendering_filter`, +//! `info.output_flags`, `filter_parent`, `filters.num`, `context.data`, the +//! linear sRGB flag and the current effect, and the calls +//! (`obs_source_video_render()`, `obs_source_default_render()` and +//! `source_render()`), one access at a time. Hooks change the model from +//! inside a call, as a plugin or another thread can, so a value read once +//! too often, too early or too late shows: the first filter read under the +//! lock, `rendering_filter` set while the filter draws, the output flags +//! read once, `filters.num` read only for a source that is not a filter, +//! `context.data` read only off the default route, and the linear sRGB +//! flag restored to what it was before the draw. +use obs_source::render_route::{FLAG_CUSTOM_DRAW, FLAG_SRGB, FLAG_VIDEO}; +use obs_source::video_render_draws::{Host, main_render, render_filters}; +use std::cell::RefCell; +use std::collections::BTreeMap; + +/// The source is token 0; its filters are 1, 2, ... +const SOURCE: u32 = 0; +/// The current effect's token. +const EFFECT: u32 = 7; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Event { + Lock(u32), + FirstFilter(u32), + GetRef(Option), + Unlock(u32), + SetRendering(u32, bool), + VideoRender(Option), + Release(Option), + Flags(u32), + HasFilterParent(u32), + HasFilters(u32), + LinearSrgb, + SetLinearSrgb(bool), + DefaultRender(u32), + HasData(u32), + CurrentEffect, + SourceRender(u32, Option), +} + +#[derive(Debug, Clone, Default)] +struct Fields { + flags: u32, + filter_parent: bool, + filters: Vec, + data: bool, + rendering_filter: bool, + /// `obs_source_get_ref()` gives NULL. + dying: bool, + refs: i64, +} + +#[derive(Debug, Clone, Default)] +struct Graphics { + linear_srgb: bool, + effect: Option, + locked: bool, +} + +/// What a hook does to the model when its call is made. +type Hook = Box, &mut Graphics)>; + +/// What the model saw at a draw: the source's parent's `rendering_filter`, +/// the linear sRGB flag and whether `filter_mutex` was held. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Seen { + rendering_filter: bool, + linear_srgb: bool, + locked: bool, +} + +struct Model { + sources: RefCell>, + graphics: RefCell, + log: RefCell>, + seen: RefCell>, + hooks: Vec<(Event, Hook)>, +} + +impl Model { + fn new(source: Fields, filters: usize) -> Self { + let mut sources = BTreeMap::new(); + let mut source = source; + source.filters = (1..=u32::try_from(filters).unwrap()).collect(); + for &f in &source.filters { + sources.insert( + f, + Fields { + flags: FLAG_VIDEO, + filter_parent: true, + data: true, + ..Fields::default() + }, + ); + } + sources.insert(SOURCE, source); + Self { + sources: RefCell::new(sources), + graphics: RefCell::new(Graphics { + linear_srgb: true, + effect: Some(EFFECT), + locked: false, + }), + log: RefCell::new(Vec::new()), + seen: RefCell::new(Vec::new()), + hooks: Vec::new(), + } + } + + fn hook( + mut self, + on: Event, + hook: impl Fn(&mut BTreeMap, &mut Graphics) + 'static, + ) -> Self { + self.hooks.push((on, Box::new(hook))); + self + } + + fn record(&self, event: Event) { + self.log.borrow_mut().push(event); + for (on, hook) in &self.hooks { + if *on == event { + hook( + &mut self.sources.borrow_mut(), + &mut self.graphics.borrow_mut(), + ); + } + } + } + + fn see(&self) { + let g = self.graphics.borrow(); + self.seen.borrow_mut().push(Seen { + rendering_filter: self.sources.borrow()[&SOURCE].rendering_filter, + linear_srgb: g.linear_srgb, + locked: g.locked, + }); + } + + fn get(&self, s: u32, read: impl Fn(&Fields) -> T) -> T { + read(&self.sources.borrow()[&s]) + } + + fn set(&self, s: u32, write: impl Fn(&mut Fields)) { + write(self.sources.borrow_mut().get_mut(&s).unwrap()); + } + + fn log(&self) -> Vec { + self.log.borrow().clone() + } + + fn seen(&self) -> Vec { + self.seen.borrow().clone() + } + + fn fields(&self, s: u32) -> Fields { + self.sources.borrow()[&s].clone() + } + + fn linear_srgb_now(&self) -> bool { + self.graphics.borrow().linear_srgb + } + + fn filters(&self) { + render_filters(self, SOURCE); + } + + fn main(&self) { + main_render(self, SOURCE); + } +} + +impl Host for Model { + type Source = u32; + type Effect = u32; + + fn lock_filters(&self, s: u32) { + assert!(!self.graphics.borrow().locked, "locked twice"); + self.graphics.borrow_mut().locked = true; + self.record(Event::Lock(s)); + } + fn first_filter(&self, s: u32) -> Option { + self.record(Event::FirstFilter(s)); + self.get(s, |f| f.filters.first().copied()) + } + fn get_ref(&self, s: Option) -> Option { + self.record(Event::GetRef(s)); + let s = s?; + if self.get(s, |f| f.dying) { + return None; + } + self.set(s, |f| f.refs += 1); + Some(s) + } + fn unlock_filters(&self, s: u32) { + assert!(self.graphics.borrow().locked, "unlocked without the lock"); + self.graphics.borrow_mut().locked = false; + self.record(Event::Unlock(s)); + } + fn set_rendering_filter(&self, s: u32, rendering: bool) { + self.set(s, |f| f.rendering_filter = rendering); + self.record(Event::SetRendering(s, rendering)); + } + fn video_render(&self, s: Option) { + self.see(); + self.record(Event::VideoRender(s)); + } + fn release(&self, s: Option) { + if let Some(s) = s { + self.set(s, |f| f.refs -= 1); + } + self.record(Event::Release(s)); + } + fn output_flags(&self, s: u32) -> u32 { + self.record(Event::Flags(s)); + self.get(s, |f| f.flags) + } + fn has_filter_parent(&self, s: u32) -> bool { + self.record(Event::HasFilterParent(s)); + self.get(s, |f| f.filter_parent) + } + fn has_filters(&self, s: u32) -> bool { + self.record(Event::HasFilters(s)); + self.get(s, |f| !f.filters.is_empty()) + } + fn linear_srgb(&self) -> bool { + let value = self.graphics.borrow().linear_srgb; + self.record(Event::LinearSrgb); + value + } + fn set_linear_srgb(&self, linear_srgb: bool) { + self.graphics.borrow_mut().linear_srgb = linear_srgb; + self.record(Event::SetLinearSrgb(linear_srgb)); + } + fn default_render(&self, s: u32) { + self.see(); + self.record(Event::DefaultRender(s)); + } + fn has_data(&self, s: u32) -> bool { + self.record(Event::HasData(s)); + self.get(s, |f| f.data) + } + fn current_effect(&self) -> Option { + let value = self.graphics.borrow().effect; + self.record(Event::CurrentEffect); + value + } + fn source_render(&self, s: u32, effect: Option) { + self.see(); + self.record(Event::SourceRender(s, effect)); + } +} + +fn plain() -> Fields { + Fields { + flags: FLAG_VIDEO, + data: true, + ..Fields::default() + } +} + +// --- obs_source_render_filters() ------------------------------------------ + +#[test] +fn the_first_filter_is_referenced_under_the_lock_and_drawn() { + let m = Model::new(plain(), 2); + m.filters(); + use Event::*; + assert_eq!( + m.log(), + [ + Lock(SOURCE), + FirstFilter(SOURCE), + GetRef(Some(1)), + Unlock(SOURCE), + SetRendering(SOURCE, true), + VideoRender(Some(1)), + SetRendering(SOURCE, false), + Release(Some(1)), + ] + ); +} + +#[test] +fn the_filter_draws_with_rendering_filter_set_and_the_lock_free() { + let m = Model::new(plain(), 1); + m.filters(); + assert_eq!( + m.seen(), + [Seen { + rendering_filter: true, + linear_srgb: true, + locked: false, + }] + ); + assert!(!m.fields(SOURCE).rendering_filter); +} + +#[test] +fn the_reference_is_held_while_the_filter_draws_and_given_back() { + // held during the draw (a hook sees it), given back after + let m = Model::new(plain(), 1).hook(Event::VideoRender(Some(1)), |s, _| { + assert_eq!(s[&1].refs, 1); + }); + m.filters(); + assert_eq!(m.fields(1).refs, 0); +} + +#[test] +fn a_filter_being_destroyed_draws_null() { + let m = Model::new(plain(), 1); + m.set(1, |f| f.dying = true); + m.filters(); + use Event::*; + assert_eq!( + m.log(), + [ + Lock(SOURCE), + FirstFilter(SOURCE), + GetRef(Some(1)), + Unlock(SOURCE), + SetRendering(SOURCE, true), + VideoRender(None), + SetRendering(SOURCE, false), + Release(None), + ] + ); + assert_eq!(m.fields(1).refs, 0); +} + +#[test] +fn a_null_first_filter_is_passed_on_as_null() { + let m = Model::new(plain(), 0); + m.filters(); + use Event::*; + assert_eq!( + m.log(), + [ + Lock(SOURCE), + FirstFilter(SOURCE), + GetRef(None), + Unlock(SOURCE), + SetRendering(SOURCE, true), + VideoRender(None), + SetRendering(SOURCE, false), + Release(None), + ] + ); +} + +#[test] +fn the_first_filter_is_read_once_under_the_lock() { + // Another filter moves to the front once the lock is free: the draw and + // the release keep the one referenced. + let m = Model::new(plain(), 2).hook(Event::Unlock(SOURCE), |s, _| { + s.get_mut(&SOURCE).unwrap().filters.reverse(); + }); + m.filters(); + use Event::*; + let log = m.log(); + assert_eq!(log.iter().filter(|e| **e == FirstFilter(SOURCE)).count(), 1); + assert!(log.contains(&VideoRender(Some(1)))); + assert_eq!(log.last(), Some(&Release(Some(1)))); + assert_eq!(m.fields(1).refs, 0); + assert_eq!(m.fields(2).refs, 0); +} + +#[test] +fn rendering_filter_is_cleared_even_when_the_draw_changed_it() { + let m = Model::new(plain(), 1).hook(Event::VideoRender(Some(1)), |s, _| { + s.get_mut(&SOURCE).unwrap().rendering_filter = true; + }); + m.set(SOURCE, |f| f.rendering_filter = true); + m.filters(); + assert!(!m.fields(SOURCE).rendering_filter); +} + +#[test] +fn render_filters_reads_no_other_field() { + let m = Model::new(plain(), 1); + m.filters(); + for e in m.log() { + assert!( + !matches!( + e, + Event::Flags(_) + | Event::HasFilterParent(_) + | Event::HasFilters(_) + | Event::HasData(_) + | Event::LinearSrgb + | Event::SetLinearSrgb(_) + | Event::CurrentEffect + ), + "{e:?}" + ); + } +} + +// --- obs_source_main_render() --------------------------------------------- + +#[test] +fn a_plain_source_draws_through_the_default_render() { + let m = Model::new(plain(), 0); + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + HasFilters(SOURCE), + LinearSrgb, + SetLinearSrgb(false), + DefaultRender(SOURCE), + SetLinearSrgb(true), + ] + ); + assert!(!m.seen()[0].linear_srgb); + assert!(m.linear_srgb_now()); +} + +#[test] +fn an_srgb_aware_source_leaves_the_flag_alone() { + for linear in [false, true] { + let m = Model::new( + Fields { + flags: FLAG_VIDEO | FLAG_SRGB, + ..plain() + }, + 0, + ); + m.graphics.borrow_mut().linear_srgb = linear; + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + HasFilters(SOURCE), + DefaultRender(SOURCE), + ] + ); + assert_eq!(m.seen()[0].linear_srgb, linear); + assert_eq!(m.linear_srgb_now(), linear); + } +} + +#[test] +fn a_custom_draw_gets_no_effect_and_never_asks_for_one() { + let m = Model::new( + Fields { + flags: FLAG_VIDEO | FLAG_CUSTOM_DRAW, + ..plain() + }, + 0, + ); + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + HasFilters(SOURCE), + LinearSrgb, + SetLinearSrgb(false), + HasData(SOURCE), + SourceRender(SOURCE, None), + SetLinearSrgb(true), + ] + ); +} + +#[test] +fn a_source_with_filters_gets_the_current_effect() { + let m = Model::new(plain(), 1); + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + HasFilters(SOURCE), + LinearSrgb, + SetLinearSrgb(false), + HasData(SOURCE), + CurrentEffect, + SourceRender(SOURCE, Some(EFFECT)), + SetLinearSrgb(true), + ] + ); +} + +#[test] +fn a_null_current_effect_is_passed_on() { + let m = Model::new(plain(), 1); + m.graphics.borrow_mut().effect = None; + m.main(); + assert!(m.log().contains(&Event::SourceRender(SOURCE, None))); +} + +#[test] +fn a_filter_never_reads_its_filter_count() { + let m = Model::new( + Fields { + filter_parent: true, + ..plain() + }, + 1, + ); + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + LinearSrgb, + SetLinearSrgb(false), + HasData(SOURCE), + CurrentEffect, + SourceRender(SOURCE, Some(EFFECT)), + SetLinearSrgb(true), + ] + ); +} + +#[test] +fn without_data_nothing_is_drawn_and_the_flag_is_restored() { + for custom in [false, true] { + let flags = if custom { + FLAG_VIDEO | FLAG_CUSTOM_DRAW + } else { + FLAG_VIDEO + }; + let m = Model::new( + Fields { + flags, + data: false, + ..plain() + }, + 1, + ); + m.main(); + use Event::*; + assert_eq!( + m.log(), + [ + Flags(SOURCE), + HasFilterParent(SOURCE), + HasFilters(SOURCE), + LinearSrgb, + SetLinearSrgb(false), + HasData(SOURCE), + SetLinearSrgb(true), + ] + ); + } +} + +#[test] +fn the_default_render_reads_no_data() { + // obs_source_default_render() checks the data itself. + let m = Model::new( + Fields { + data: false, + ..plain() + }, + 0, + ); + m.main(); + assert!(!m.log().contains(&Event::HasData(SOURCE))); + assert!(m.log().contains(&Event::DefaultRender(SOURCE))); +} + +#[test] +fn the_output_flags_are_read_once() { + // The flags turn sRGB aware and custom draw once saved: the draw and + // the restore still follow the first read. + let m = Model::new(plain(), 1).hook(Event::LinearSrgb, |s, _| { + s.get_mut(&SOURCE).unwrap().flags = FLAG_VIDEO | FLAG_SRGB | FLAG_CUSTOM_DRAW; + }); + m.main(); + use Event::*; + let log = m.log(); + assert_eq!(log.iter().filter(|e| **e == Flags(SOURCE)).count(), 1); + assert!(log.contains(&SourceRender(SOURCE, Some(EFFECT)))); + assert_eq!(log.last(), Some(&SetLinearSrgb(true))); +} + +#[test] +fn the_default_route_is_decided_before_the_flag_is_saved() { + // filter_parent and filters change once the flag is read: the route + // keeps the values read before. + let m = Model::new(plain(), 0).hook(Event::LinearSrgb, |s, _| { + let f = s.get_mut(&SOURCE).unwrap(); + f.filter_parent = true; + f.filters = vec![1]; + }); + m.main(); + assert!(m.log().contains(&Event::DefaultRender(SOURCE))); + let m = Model::new(plain(), 1).hook(Event::LinearSrgb, |s, _| { + let f = s.get_mut(&SOURCE).unwrap(); + f.filters.clear(); + }); + m.main(); + assert!(!m.log().contains(&Event::DefaultRender(SOURCE))); + assert!(m.log().contains(&Event::SourceRender(SOURCE, Some(EFFECT)))); +} + +#[test] +fn the_data_is_read_after_the_flag_is_cleared() { + // The data goes away while the flag is cleared: nothing is drawn. + let m = Model::new(plain(), 1).hook(Event::SetLinearSrgb(false), |s, _| { + s.get_mut(&SOURCE).unwrap().data = false; + }); + m.main(); + use Event::*; + assert!( + !m.log() + .iter() + .any(|e| matches!(e, SourceRender(..) | CurrentEffect)) + ); + assert_eq!(m.log().last(), Some(&SetLinearSrgb(true))); +} + +#[test] +fn the_effect_is_read_after_the_data_right_before_the_draw() { + let m = Model::new(plain(), 1).hook(Event::HasData(SOURCE), |_, g| { + g.effect = Some(EFFECT + 1); + }); + m.main(); + assert!( + m.log() + .contains(&Event::SourceRender(SOURCE, Some(EFFECT + 1))) + ); +} + +#[test] +fn the_flag_is_set_back_to_what_it_was_before_the_draw() { + for before in [false, true] { + for during in [false, true] { + // the draw sets the flag; the restore puts back the saved one + let m = Model::new(plain(), 1).hook( + Event::SourceRender(SOURCE, Some(EFFECT)), + move |_, g| { + g.linear_srgb = during; + }, + ); + m.graphics.borrow_mut().linear_srgb = before; + m.main(); + assert_eq!(m.linear_srgb_now(), before); + assert_eq!(m.log().last(), Some(&Event::SetLinearSrgb(before))); + } + } +} + +#[test] +fn every_state_draws_at_most_once_and_restores_the_flag() { + for bits in 0..64_u32 { + let bit = |i: u32| bits & (1 << i) != 0; + let mut flags = FLAG_VIDEO; + if bit(0) { + flags |= FLAG_CUSTOM_DRAW; + } + if bit(1) { + flags |= FLAG_SRGB; + } + let m = Model::new( + Fields { + flags, + filter_parent: bit(2), + data: bit(4), + ..plain() + }, + usize::from(bit(3)), + ); + m.graphics.borrow_mut().linear_srgb = bit(5); + m.main(); + let log = m.log(); + let draws: Vec<_> = log + .iter() + .filter(|e| matches!(e, Event::DefaultRender(_) | Event::SourceRender(..))) + .collect(); + let default = !bit(2) && !bit(3) && !bit(0); + let want = usize::from(default || bit(4)); + assert_eq!(draws.len(), want, "{bits:#b}: {log:?}"); + if default { + assert_eq!(draws[0], &Event::DefaultRender(SOURCE), "{bits:#b}"); + } else if bit(4) { + let effect = if bit(0) { None } else { Some(EFFECT) }; + assert_eq!(draws[0], &Event::SourceRender(SOURCE, effect), "{bits:#b}"); + } + assert_eq!( + log.contains(&Event::HasFilters(SOURCE)), + !bit(2), + "{bits:#b}" + ); + let sets = log + .iter() + .filter(|e| matches!(e, Event::SetLinearSrgb(_))) + .count(); + assert_eq!(sets, if bit(1) { 0 } else { 2 }, "{bits:#b}"); + assert_eq!(m.linear_srgb_now(), bit(5), "{bits:#b}"); + for seen in m.seen() { + assert_eq!(seen.linear_srgb, bit(1) && bit(5), "{bits:#b}"); + } + } +} From a507790354cef35a7188063254e2709267cce661 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 29 Nov 2026 19:37:52 -0500 Subject: [PATCH 3/3] obs-source: port obs_source_main_render and obs_source_render_filters to Rust, GREEN (Fixes #5972) The safe core in obs-source::video_render_draws, the two hidden exports obs_source_video_render_render_filters() and obs_source_video_render_main_render() in obs-source::ffi::source_video_render_draws (rust-hidden.d; neither has EXPORT), one-access adapters in libobs/obs-source-video-render-draws-fields.c.h (live group libobs.core-source), and the original moved to the reference-only group libobs.ported-source-video-render-draws. obs_source_skip_video_filter()'s main draw reaches the same export through obs-source.c.h's wrapper. Every decision is obs-source::render_route; filter_mutex, the fields, the references and every call (obs_source_video_render(), obs_source_default_render(), gs_get_linear_srgb(), gs_set_linear_srgb(), gs_get_effect() and source_render(), through its hidden wrapper obs_source_default_render_source_render()) stay C. The first filter is read and referenced under the lock, rendering_filter is set only around its draw, the output flags are read once, filters.num only for a source that is not a filter, context.data only off the default route, and the saved linear sRGB flag is set back. Tier 3 in obs-c-oracle (oracle/source_video_render_draws.c) runs the original C, with the oracle's original route, against the Rust exports on three noise-filled real-layout sources over shared stand-ins of the lock, the reference, the draws, the flag and the effect. Clobbers move another filter to the front from the lock and the unlock, flip rendering_filter from the filter's draw, flip the output flags, filter_parent and filters.num once the flag is read, flip context.data once the flag is cleared or the effect is read, and flip the flag and the output flags from the draws. It compares the log (with the references held and rendering_filter), the references and the flag left and every byte over a grid and a proptest, pins the order from the C's log, checks that every clobber that can reach the C does, and rejects twelve mutants. No intentional differences. Co-Authored-By: Claude Opus 5.5 Ci-Attestation: {"at":1795999252,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":76,"stamp":"9229610565a589ef4e7afd5a943a1095","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999252,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":76,"stamp":"d9558aca80a6ff2c5908e4d4cb28b9dd","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999252,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":76,"stamp":"2c5bab7fefa9141c12543a7dde711f0b","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999252,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":76,"stamp":"2ae0c207a24d19d6df28ea6c1da34ab6","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"general/all/client-lint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"dbee3930f58ea9d88826cfb9c6ce6080","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999252,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":76,"stamp":"025b393faba4bd576be302a5043972a6","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"85e9ec1dafa7eeb0420a5ed6be592a23","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"2357adbf2cc4ae56ae78abc3850a4ff9","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"cae5925532b3ca0e672cd5c755e9598b","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"e565d59f6d2be0b10cb84b952415e92c","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"3636f3552e510faefa2d2489c0a8c218","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"2a48e451e7821a9d85bd66dbdb00fcef","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"cdc4b486d54fe9c46118122b41adab30","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"d81b7dfbe6d98dbf4619397e78922522","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"73b655ea8b20cce24ba2eb885a951b34","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"03073ae71a53d7fce98d63051b0eed78","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"aeea27f40cc5bf53059fd9332f0f1389","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999175,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"4b55d162408623736e38873182d606f08e6648f5b3a559167f0748d69bf4425f","lane":"client-lint","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":92,"stamp":"c6236233426780832c96b5013a53f0e0","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Ci-Attestation: {"at":1795999558,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"6d29c2f1963cb57b273f31a1c1997dfe5eba29b3a6f4f44815acffffc6760272","lane":"client-test-x86_64-unknown-linux-gnu","parents":["c0eaecdf0759e2caa969e6e883b5d31e19fca747"],"secs":305,"stamp":"b72c3c81d49f78ffc8850c8413cac2ae","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f","v":1,"via":"run"} Client-Checks: {"checks":{"base":"68c8fae54d6f75f5ba28b4e93419b8762cffe241","coverage":"all-tracked-applicable-files","diff":["libobs/build/libobs.core-source+.h.c","libobs/build/libobs.ported-source-video-render-draws+.h.c","libobs/build/ported-source-sets/source-video-render-draws.json","libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt","libobs/obs-source-video-render-draws-fields.c.h","libobs/obs-source-video-render-draws.c.h","libobs/obs-source.c.h","rust/README.md","rust/obs-build/tests/native.rs","rust/obs-build/tests/native/registration/canonical_source_video_render_draws_swap_is_registered.rs","rust/obs-build/tests/native/registration/canonical_source_video_render_swap_is_registered.rs","rust/obs-c-oracle/oracle.d/source-video-render-draws.list","rust/obs-c-oracle/oracle/source_video_render_draws.c","rust/obs-source/src/ffi/mod.rs","rust/obs-source/src/ffi/source_video_render_draws.rs","rust/obs-source/src/lib.rs","rust/obs-source/src/video_render_draws.rs","rust/obs-source/tests/source/main.rs","rust/obs-source/tests/source/video_render_draws.rs","rust/obs-source/tests/source/video_render_draws_parity.rs","test/cmocka/build/test-graph.json","test/cmocka/test_source_video_render_draws.c","test/cmocka/tests.d/test_source_video_render_draws.cmake"],"head":"cc6c2c54863d3530e4e4417d440ef8711c79f1c6","tools":{"orchestrator-python":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]","schema-dependencies":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21504-GITv2.15.4\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ICU ISO8859X Regexps Automata RelaxNG Schemas Schematron Modules Debug Zlib"},"tracked-digest":"308f5d4425ce0b6ef038d1353bd53b16d0f53433494803f559c21e0248dd700e","tree":"2e2f14a56f2be5a83767618229d8380c21c2cc3f"}} --- libobs/build/libobs.core-source+.h.c | 2 +- ...bobs.ported-source-video-render-draws+.h.c | 4 + .../source-video-render-draws.json | 5 + .../obs-source-source_video_render_draws.txt | 5 + .../obs-source-video-render-draws-fields.c.h | 113 ++++ rust/README.md | 1 + rust/obs-build/tests/native.rs | 2 + ...e_video_render_draws_swap_is_registered.rs | 77 +++ .../oracle.d/source-video-render-draws.list | 1 + .../oracle/source_video_render_draws.c | 445 +++++++++++++ rust/obs-source/src/ffi/mod.rs | 1 + .../src/ffi/source_video_render_draws.rs | 181 ++++++ rust/obs-source/src/video_render_draws.rs | 44 +- rust/obs-source/tests/source/main.rs | 1 + .../tests/source/video_render_draws_parity.rs | 611 ++++++++++++++++++ 15 files changed, 1488 insertions(+), 5 deletions(-) create mode 100644 libobs/build/libobs.ported-source-video-render-draws+.h.c create mode 100644 libobs/build/ported-source-sets/source-video-render-draws.json create mode 100644 libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt create mode 100644 libobs/obs-source-video-render-draws-fields.c.h create mode 100644 rust/obs-build/tests/native/registration/canonical_source_video_render_draws_swap_is_registered.rs create mode 100644 rust/obs-c-oracle/oracle.d/source-video-render-draws.list create mode 100644 rust/obs-c-oracle/oracle/source_video_render_draws.c create mode 100644 rust/obs-source/src/ffi/source_video_render_draws.rs create mode 100644 rust/obs-source/tests/source/video_render_draws_parity.rs diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index 0454650f6d..a15d0ffd88 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -144,7 +144,7 @@ #include "libobs/obs-source-type-info-fields.c.h" #include "libobs/obs-source-update-fields.c.h" #include "libobs/obs-source-update-properties-fields.c.h" -#include "libobs/obs-source-video-render-draws.c.h" +#include "libobs/obs-source-video-render-draws-fields.c.h" #include "libobs/obs-source-video-render-fields.c.h" #include "libobs/obs-source-video-tick-callbacks-fields.c.h" #include "libobs/obs-source-video-tick-entry-fields.c.h" diff --git a/libobs/build/libobs.ported-source-video-render-draws+.h.c b/libobs/build/libobs.ported-source-video-render-draws+.h.c new file mode 100644 index 0000000000..fd566bf8ec --- /dev/null +++ b/libobs/build/libobs.ported-source-video-render-draws+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original obs_source_render_filters and obs_source_main_render for C reference. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-source-video-render-draws.c.h" diff --git a/libobs/build/ported-source-sets/source-video-render-draws.json b/libobs/build/ported-source-sets/source-video-render-draws.json new file mode 100644 index 0000000000..cdbd99efd1 --- /dev/null +++ b/libobs/build/ported-source-sets/source-video-render-draws.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-source-video-render-draws+.h.c" + ] +} diff --git a/libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt b/libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt new file mode 100644 index 0000000000..281199e2e7 --- /dev/null +++ b/libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt @@ -0,0 +1,5 @@ +# obs-source-video-render-draws.c.h (#5972): obs_source_render_filters() and +# obs_source_main_render(), the two draws obs_source_video_render() makes, +# through their wrappers. Declared without EXPORT, so the shims stay hidden. +obs_source_video_render_render_filters +obs_source_video_render_main_render diff --git a/libobs/obs-source-video-render-draws-fields.c.h b/libobs/obs-source-video-render-draws-fields.c.h new file mode 100644 index 0000000000..53e5785d15 --- /dev/null +++ b/libobs/obs-source-video-render-draws-fields.c.h @@ -0,0 +1,113 @@ +#include "libobs/build/libobs.pch.h" + +/* Private adapters of the Rust obs_source_render_filters() and + * obs_source_main_render() (obs-source::ffi::source_video_render_draws, + * #5972; exported as their hidden wrappers + * obs_source_video_render_render_filters() and + * obs_source_video_render_main_render(), no EXPORT), each one access of + * obs-source-video-render-draws.c.h: filter_mutex, filters.array[0], the + * reference and its release, rendering_filter, obs_source_video_render(), + * info.output_flags, filter_parent, filters.num, the linear sRGB flag, + * obs_source_default_render(), context.data, the current effect and + * source_render(). source_render() is static in obs-source.c.h and reached + * through its hidden wrapper, declared there, later in the group. Rust + * decides what runs, and in what order. Like the C, nothing checks the + * source: the render passes a live one, inside the graphics context. */ + +void obs_source_default_render_source_render(obs_source_t *source, gs_effect_t *effect); + +void obs_source_video_render_draws_lock(obs_source_t *source); +void obs_source_video_render_draws_lock(obs_source_t *source) +{ + pthread_mutex_lock(&source->filter_mutex); +} + +obs_source_t *obs_source_video_render_draws_first_filter(const obs_source_t *source); +obs_source_t *obs_source_video_render_draws_first_filter(const obs_source_t *source) +{ + return source->filters.array[0]; +} + +obs_source_t *obs_source_video_render_draws_get_ref(obs_source_t *source); +obs_source_t *obs_source_video_render_draws_get_ref(obs_source_t *source) +{ + return obs_source_get_ref(source); +} + +void obs_source_video_render_draws_unlock(obs_source_t *source); +void obs_source_video_render_draws_unlock(obs_source_t *source) +{ + pthread_mutex_unlock(&source->filter_mutex); +} + +void obs_source_video_render_draws_set_rendering_filter(obs_source_t *source, bool rendering); +void obs_source_video_render_draws_set_rendering_filter(obs_source_t *source, bool rendering) +{ + source->rendering_filter = rendering; +} + +void obs_source_video_render_draws_video_render(obs_source_t *source); +void obs_source_video_render_draws_video_render(obs_source_t *source) +{ + obs_source_video_render(source); +} + +void obs_source_video_render_draws_release(obs_source_t *source); +void obs_source_video_render_draws_release(obs_source_t *source) +{ + obs_source_release(source); +} + +uint32_t obs_source_video_render_draws_flags(const obs_source_t *source); +uint32_t obs_source_video_render_draws_flags(const obs_source_t *source) +{ + return source->info.output_flags; +} + +bool obs_source_video_render_draws_has_filter_parent(const obs_source_t *source); +bool obs_source_video_render_draws_has_filter_parent(const obs_source_t *source) +{ + return source->filter_parent != NULL; +} + +bool obs_source_video_render_draws_has_filters(const obs_source_t *source); +bool obs_source_video_render_draws_has_filters(const obs_source_t *source) +{ + return source->filters.num != 0; +} + +bool obs_source_video_render_draws_linear_srgb(void); +bool obs_source_video_render_draws_linear_srgb(void) +{ + return gs_get_linear_srgb(); +} + +void obs_source_video_render_draws_set_linear_srgb(bool linear_srgb); +void obs_source_video_render_draws_set_linear_srgb(bool linear_srgb) +{ + gs_set_linear_srgb(linear_srgb); +} + +void obs_source_video_render_draws_default_render(obs_source_t *source); +void obs_source_video_render_draws_default_render(obs_source_t *source) +{ + obs_source_default_render(source); +} + +bool obs_source_video_render_draws_has_data(const obs_source_t *source); +bool obs_source_video_render_draws_has_data(const obs_source_t *source) +{ + return source->context.data != NULL; +} + +gs_effect_t *obs_source_video_render_draws_effect(void); +gs_effect_t *obs_source_video_render_draws_effect(void) +{ + return gs_get_effect(); +} + +void obs_source_video_render_draws_source_render(obs_source_t *source, gs_effect_t *effect); +void obs_source_video_render_draws_source_render(obs_source_t *source, gs_effect_t *effect) +{ + obs_source_default_render_source_render(source, effect); +} diff --git a/rust/README.md b/rust/README.md index 6a75337e3d..dc92d8b92e 100644 --- a/rust/README.md +++ b/rust/README.md @@ -556,6 +556,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-source-transition-switch.c.h` | `obs-source::transition_switch`, shims in `obs-source-seam::ffi::source_transition_switch` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition-switch`). The start and set of a transition, moved out of `obs-source-transition.c.h` (#3794): `obs_transition_start()` and `obs_transition_set()`, both `EXPORT` through `obs.h`, with their file-local `set_source()`, `activate_transition()`, `write_start_clock()` and `check_transition_recursion()`. They are the safe core's, generic over its `Transition` (one method per seam call and per libobs call); the C shims in `obs-source-seam` implement it over the transition seam (`transition_valid()`, `transition_active()`, one accessor per field, the lock and unlock of `transition_mutex`, and the plugin's `transition_start` through `obs_source_transition_seam_call_start()`) and, through `Calls`, over `obs_source_get_ref()`, `obs_source_release()`, the active-child add and remove, `obs_source_get_name()`, `obs_source_enum_full_tree()`, `os_gettime_ns()`, the signals (`obs_source_transition_seam_dosignal()`) and the size port's `transition_recalculate_size()` and `_matrices()`, with the warnings through `blog()`. No `struct obs_source` is mirrored. The decisions are the start rules, each asked where the C asks it. The locking is the C's: the start reads the sources and the running state in three separate lock sections (finishing a running transition with `obs_transition_set()` to its source B between the first two), writes the mode under the lock, calls the plugin and reads the fixed duration without it, and `set_source()` adds the new child as an active child with `transition_mutex` held, after the recursion walk; the set empties both slots and fills slot A under the lock, then removes and releases the old children and adds the new one after the unlock. The plugin's `transition_start` can re-enter the start or set on the same transition: the shims hold nothing across it, and every field is read again afterwards. Kept, characterized, not endorsed (as in the start rules' row): a mode written by a re-entering start stays; a running transition started again to its destination finishes on it and is refused; and a destination whose last reference is gone starts the transition towards an empty slot B without running it. No intentional differences. `test_source_transition_switch` and the transition start, set and recursion tests see both through the real libobs; Tier 1 runs the core on a fake transition that records every read and write with whether the lock was held, every lock and every libobs call, and reaches the `DONE` admission with a start another thread makes between two lock calls. Tier 3 builds the C renamed, with its libobs calls going to recording stand-ins that the Rust shims are handed too, and runs both on the same graph (a transition with a plugin that can re-enter once, an input, three children and two scenes whose trees hold the transition), comparing after every call the result, the slots, flags, mode, manual fields and clock, the held mutexes, the reference counts, the active links and every call in order: the C test's sequences, every call on fixed graphs and a proptest over random graphs and call sequences; in a child process, the warning lines match the C. | | `libobs/obs-source-transition-video.c.h` | `obs-source::transition_video`, shims in `obs-source-seam::ffi::source_transition_video` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition-video`). The video render of a transition, moved out of `obs-source-transition.c.h` (#4061): `obs_transition_video_render()`, `obs_transition_video_render2()`, `obs_transition_video_get_color_space()` and `obs_transition_video_render_direct()`, all `EXPORT` through `obs.h`, which every bundled transition draws through, with the file-local `get_texture()`, `copy_transition_state()`, `render_child()` and `mix_spaces()` folded in. Under `transition_mutex`: the video time, the video stop when it is due (`transitioning_video` ends; the transition stops unless its audio runs on, and the direct render also ends the audio of a transition that is not active) and the state copy (both children referenced, both flags, both matrices); then the texture trylock while the video transitions and the draw the render rule (`obs-source::transition_render`) picks: both children into their texrenders (re-created when the format no longer fits the space) and the type's callback under a premultiplied blend, only at a non-zero size, with the placeholder for an empty slot or a missing texture; or one child with its matrix; then the texture unlock, the releases, the video stop signal and the stop handler. They are the safe core's, generic over its `Transition` (one method per seam call, per libobs call and per graphics call); the C shims in `obs-source-seam` implement it over the transition seam (`transition_valid()`, one accessor per field, the matrices, the texrenders, both locks and the context data) and, through `Calls`, over the clock read, stop and size helpers (the stop and size ports' Rust), `obs_source_active()`, the reference calls, the signal, `obs_source_get_color_space()`, `obs_source_video_render()`, the transparent texture and the graphics calls; `gs_get_format_from_space()`, a `graphics.h` inline with no symbol, is ported as `obs-source::transition_video::format_from_space`. No `struct obs_source` is mirrored. The locking is the C's: `transition_mutex` is let go before the texture trylock, and the texture lock, when it was taken, after the draw; the callback runs with no transition lock held and nothing of the call cached across it. Intentional differences: `obs_transition_video_get_color_space()` asks `transition_valid()` first (the C crashed on a NULL transition and mixed the unused slots of a source that is not one; both now get `GS_CS_SRGB`), and `obs_transition_video_render_direct()` hands `transition_valid()` its own name (the C's debug line for a NULL named `obs_transition_video_render`). `test_source_transition_video` (the characterization test the move added), `test_transition_render_steps` and the transition, stinger and scene tests see the render through the real libobs, and `transition_video_layout` checks the `struct vec4` and `struct matrix4` the shims hand libobs (size, alignment and every field offset) against the C headers; Tier 1 runs the core on a fake transition that records every read and write with whether the lock was held, every lock, `transition_valid()`, libobs and graphics call. Tier 3 builds the C renamed, with its libobs and graphics calls going to recording stand-ins that the Rust shims are handed too, and runs both on the same graph (a transition, an input and two children), comparing the result, the slots, flags and texrenders, the held mutexes, the reference counts and every call in order with its arguments (floats bit for bit) and the held locks: every call on each flag pair, due and early times, active or not and the texture lock free or held, every slot state, texrender formats, textures and begin results, zero sizes, each pair of child spaces, frame sequences through the stop and a proptest over random graphs and call sequences; the format inline is compared with the C on every int it is asked; the colour-space argument check is compared against the C's own outcome on a non-transition. | | `libobs/obs-source-transition.c.h` | `obs-source::transition_audio`, shims in `obs-source-seam::ffi::source_transition` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition`). The rest of the transition file, after the moves of its other units: `obs_transition_audio_render()` (`EXPORT` through `obs.h`, called by every transition plugin's `audio_render`), with its file-local `transition_live_sources()`, `transition_calc_min_ts()`, `transition_process_audio()` and `mix_child()`, and `obs_transition_init()` / `obs_transition_free()` (declared in `obs-internal.h`, called by a source's create and destroy). They are the safe core's, generic over its `Transition`, which extends `transition_stop`'s with one method per further field read or write, mutex call, child read, buffer access and libobs call; the shims implement it on the stop shims' `Seam` over `libobs/obs-source-transition-seam.h` and a `Calls` trait for `obs_source_removed()`, `obs_source_get_audio_timestamp()`, `obs_source_get_audio_mix()`, `obs_source_get_ref()`, the graphics context and the texrender calls, and mirror `struct obs_source_audio_mix`. The render takes `transition_mutex` around the children's reads, the clock at their earliest timestamp, the audio stop (`transition_stop`'s helpers, called in Rust), the second read of the children after a stop (#2185) and the state copy; it mixes each child with the type's callback from the copied clock only (#3499), or copies slot A's whole block, after the lock. Init returns on a failed mutex before it makes the texrenders; free destroys the mutexes before it enters the graphics context. The file's `_Static_assert`s on the Rust rules' constants moved verbatim into `obs-source-transition-seam.c.h`. No intentional differences. Tiers: 1 (`source::transition_audio`, a recording fake), 2 (`test_source_transition_audio`, the characterization test this port added, and the transition, slideshow, stinger and audio tests, on hybrid and C reference), 3 (`source_seam::transition_audio_parity`, the original file over the seam with recording stand-ins, every call compared with the output mix bit for bit; mutation-checked). | +| `libobs/obs-source-video-render-draws.c.h` | `obs-source::video_render_draws`, shim in `obs-source::ffi::source_video_render_draws` | #5972: `obs_source_render_filters()` and `obs_source_main_render()`, the two draws `obs_source_video_render()` makes, swapped in hybrid `libobs` (group `libobs.ported-source-video-render-draws`) as their hidden wrappers `obs_source_video_render_render_filters()` and `obs_source_video_render_main_render()` (`rust-hidden.d`, no `EXPORT`); `obs_source_skip_video_filter()`'s main draw goes through the same export. `render_filters()`: `filter_mutex` locked, `filters.array[0]` read and referenced, unlocked; `rendering_filter` set around `obs_source_video_render()` of that reference (NULL when none could be taken), then the reference released. `main_render()`: `info.output_flags` read once; `filter_parent`, and `filters.num` only for a source that is not a filter; when not sRGB aware, `gs_get_linear_srgb()` saved and cleared, set back after the draw; the default effect draws through `obs_source_default_render()`, otherwise, only with `context.data`, `source_render()` with NULL for a custom draw or `gs_get_effect()`. Every decision is `render_route`'s; safe core over a `Host` trait; the lock, the fields, the references and the calls stay C in `obs-source-video-render-draws-fields.c.h` (live group `libobs.core-source`), `source_render()` through its hidden wrapper `obs_source_default_render_source_render()`. The new `test_source_video_render_draws` (Linux with OpenGL; rendered on the test thread in the graphics context, inside a technique of the solid effect) runs against hybrid and reference libraries: 6 cases of the default effect with the flag cleared and restored, an sRGB-aware source keeping it, a custom draw getting NULL, a filter and a source with filters getting the current effect, the filter's extra reference with `rendering_filter` set, and a source without data drawing nothing. Tier 3 runs the original C (with the oracle's original route) and the Rust exports on three noise-filled real-layout sources through shared recorders of the lock, the reference, the draws, the flag and the effect, with clobbers that move another filter to the front from the lock and the unlock, flip the output flags, `filter_parent`, `filters.num` and `context.data` mid-call and flip the flag from the draws, over a grid and a proptest; it compares the log (with the references held and `rendering_filter`), the references and the flag left and every byte, and rejects twelve mutants. No intentional differences; Tier 4 does not apply. | | `libobs/obs-source-video-render.c.h` | `obs-source::video_render`, shim in `obs-source::ffi::source_video_render` | #5962: `obs_source_video_render()` swapped in hybrid `libobs` (group `libobs.ported-source-video-render`, the export in `rust-exports.d`), with its steps `source_render_video()`, `source_draw_route()` and `get_type_format()`. Every scene item, projector, preview and filter target draws a source through it. A NULL source logs the `obs_source_valid()` line; otherwise the source is drawn between `obs_source_get_ref()` and `obs_source_release()`, and not at all when no reference can be taken (a source being destroyed). `source_render_video()`: the type, and the output flags unless it is a filter, for the "no video" check (a filter with a parent skips itself through `obs_source_skip_video_filter()`); the type and flags read again for the async check (flags only for an input), `rendering_filter` only for an async input, then `deinterlace_update_async_video()` (deinterlacing on) and `obs_source_update_async_video()`; `context.data`, and `enabled` only with data, for the inactive check (a filter with a parent skips); then the one draw `source_draw_route()` picks, reading `filters.num`, `rendering_filter`, `video_render`, `filter_target` and the deinterlace mode one at a time and only when `render_route::draw_next()` asks: the filter chain, the main render, the filter target (read again for the draw, which goes through the export again), the deinterlacer or the async frame. `get_type_format()` and the debug marker exist only with `GS_USE_DEBUG_MARKERS`, which is 0 (`video_render::DEBUG_MARKERS`, checked by a `_Static_assert` in the fields adapter). Safe core over a `Host` trait; every route decision is `render_route`'s; the validity check, the reference, the fields, the skip, the updates, the draws and the marker stay C in `obs-source-video-render-fields.c.h` (live group `libobs.core-source`, no `EXPORT`), the static `obs_source_render_filters()` and `obs_source_main_render()` through new hidden wrappers in `obs-source.c.h`. The new `test_source_video_render` (Linux with OpenGL; rendered on the test thread in the graphics context) runs against hybrid and reference libraries: 12 cases of the NULL line, the main render inside one more reference, a source being destroyed, no video, disabled, video without `video_render`, the filter chain (`rendering_filter` set, two references), a disabled filter skipping, a filter drawing its target, two filters, the async frame and the deinterlacer. Tier 3 runs the original C (with the oracle's original route) and the Rust export on four noise-filled real-layout sources through shared recorders of the check, the reference, the skip, the updates and the draws, the filter chain and the target drawing through each side's own entry again, with clobbers that flip `enabled`, `context.data`, `filters.num`, `rendering_filter`, `video_render`, `filter_target`, the deinterlace mode and `filter_parent` mid-call, over a grid of types, flags and node states and a proptest; it compares the log (with the references held and every `rendering_filter`), the references left and every byte, and rejects eleven mutants. The view oracle's render double became a hook of the new validity stand-in. No intentional differences; Tier 4 does not apply. | | `libobs/obs-source-video-tick.c.h` | `obs-source::video_tick` | Swapped in hybrid `libobs` (group `libobs.ported-source-video-tick`). The per-frame decisions of `obs_source_video_tick()` and `async_tick()`, which every source runs once per rendered frame, as seven functions on plain values: `obs_source_video_tick_transition()`, `_async()` and `_media()` (which steps run: the transition tick for `OBS_SOURCE_TYPE_TRANSITION`, the async tick for `OBS_SOURCE_ASYNC` and the media actions for `OBS_SOURCE_CONTROLLABLE_MEDIA`, each asked at its own point), `obs_source_video_tick_deferred_update()` (a loaded `defer_update_count` above 0), `obs_source_video_tick_edge()` (the show/hide and activate/deactivate edge: none when `refs != 0` equals the stored flag, else on or off), `obs_source_video_tick_calls_tick()` (data and a `video_tick` callback) and `obs_source_video_tick_async_route()` (deinterlace the last frame or take the closest one; asked again for the previous frame's filter). They are declared in `obs-source-video-tick.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every read and call where it was: the three unlocked `os_atomic_load_long()` reads (`defer_update_count` after the media actions, `show_refs` before the show edge, `activate_refs` only after it, since a show callback can change it), the output flags read again for the media step, `obs_transition_tick()`, `process_media_actions()`, `obs_source_deferred_update()`, `gs_texrender_reset()`, the show, hide, activate and deactivate calls, the filter walks in reverse, the stores of `showing` and `active`, the plugin callback, the per-tick resets, `async_mutex`, both `deinterlacing_enabled()` calls, the frame calls and `set_async_texture_size()` with their null checks. `info.video_tick` is now read also when the source has no data (it is set when the source is created). The header mirrors the type and the two flags, and `obs-source.c.h` checks them. No pointer crosses; the counts are the C's `long`, widened to `i64`, and the two enums go back as their values. Kept, characterized, not endorsed: the type is not checked against the flags (a filter or scene flagged async gets the async tick); a negative defer count is not due; a negative reference count (one release too many) is not 0, so it shows or activates the source and keeps it so. No intentional differences. `test_source_video_tick_steps` compiles the C into itself, and `test_source_video_tick_steps_rust` runs the same test source against `libobs_rust-static`; `test_visibility_transition`, `test_preview_only_hide`, `test_studio_mode_swap`, `test_studio_mode_exit`, `test_canvas_destroy_scene_refs` and `test_item_transition_empty_source` see the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: the header's values and enum sizes, the transition tick for every type from -100,000 to 100,000 and every 65,521st `int`, the async and media steps for every low 16-bit flag word under 16 high words and every 65,521st `uint32_t`, the deferred update and both edges for every count from -100,000 to 100,000 and 38 special counts, the callback rule and the route over every input, `obs_source_video_tick()`'s order of steps copied onto a stand-in source over every type, flag pair, count pair around 0 and stored flag (131,072 ticks) and over proptest runs of ticks, and full-range proptests. | | `libobs/obs-source-video-tick-callbacks.c.h` | `obs-source::video_tick_callbacks`, shims in `obs-source::ffi::source_video_tick_callbacks` | #5842: swapped in hybrid `libobs` (group `libobs.ported-source-video-tick-callbacks`). The five per-source callback steps `obs_source_video_tick()` runs, moved out of `obs-source.c.h` (their only caller, which stays C): `obs_source_deferred_update()`, `activate_source()`, `deactivate_source()`, `show_source()` and `hide_source()`, no longer `static`, declared in `obs-internal.h` without `EXPORT`, so the shims stay hidden through `rust-hidden.d`. The deferred update does nothing at all, signal included, unless the source has data and its type an `update` callback; then it loads `defer_update_count`, calls `update` with the data and settings, swaps the count to 0 only if it still holds the value loaded before the call (an update deferred again from inside the callback survives), and sends `source_update`, then `update`. Each edge step calls the type's `activate`, `deactivate`, `show` or `hide` with the data when both are set, then always sends `source_` (public sources only) and ``. Safe core over a `Host` trait; the callback checks and calls, the count's address and `obs_source_dosignal()` stay C in `obs-source-video-tick-callbacks-fields.c.h` (live group `libobs.core-source`, no `EXPORT`), and the shims load and swap the count with sequentially consistent atomics, as the C does. The new `test_source_video_tick_callbacks` (Linux with OpenGL; the steps and `obs_source_video_tick()` are hidden, so the core's video thread ticks a public source with two private filters) runs against hybrid and reference libraries: the four edges on the source and then its filters from the last entry, callback before signals, signals without a callback or data, show before activate, the deferred update with and without `update` or data, and an update deferred again surviving the swap. Tier 3 runs the original C and the Rust exports on real-layout sources with recording callbacks and real source and core signal handlers under a private core, over every combination of data, the five callbacks and privacy with edge counts and a re-deferral from inside `update`, the characterized sequence and a proptest of call sequences, comparing every event in order, the arguments seen and the count; it rejects six mutants. No intentional differences; Tier 4 does not apply. | None. | diff --git a/rust/obs-build/tests/native.rs b/rust/obs-build/tests/native.rs index 6c93f01206..9e93b00687 100644 --- a/rust/obs-build/tests/native.rs +++ b/rust/obs-build/tests/native.rs @@ -5686,6 +5686,8 @@ pub(crate) mod core_module_manifest; mod canonical_source_update_properties_swap_is_registered; #[path = "native/registration/canonical_source_update_swap_is_registered.rs"] mod canonical_source_update_swap_is_registered; +#[path = "native/registration/canonical_source_video_render_draws_swap_is_registered.rs"] +mod canonical_source_video_render_draws_swap_is_registered; #[path = "native/registration/canonical_source_video_render_swap_is_registered.rs"] mod canonical_source_video_render_swap_is_registered; #[path = "native/registration/canonical_source_video_tick_callbacks_swap_is_registered.rs"] diff --git a/rust/obs-build/tests/native/registration/canonical_source_video_render_draws_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_source_video_render_draws_swap_is_registered.rs new file mode 100644 index 0000000000..09be171dd1 --- /dev/null +++ b/rust/obs-build/tests/native/registration/canonical_source_video_render_draws_swap_is_registered.rs @@ -0,0 +1,77 @@ +use super::*; + +#[test] +fn canonical_source_video_render_draws_swap_is_registered() { + use obs_build::validation::graph::{Graph, Source}; + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let mut graph = Graph::load(&root.join("test/cmocka/build/test-graph.json")).unwrap(); + let name = "test_source_video_render_draws"; + graph.targets.retain(|key, _| key == name); + graph.tests.retain(|key, _| key == name); + // The test compiles no copy of the draws: obs_source_video_render() of + // the real libobs reaches the hidden symbols (Rust in hybrid, the + // original C in the reference) in the graphics context. The render + // needs the OpenGL core, so only Linux selects it. + let selected = graph + .select(&[("OS_LINUX".into(), true), ("HAVE_OBS_OPENGL".into(), true)].into()) + .unwrap(); + assert!( + matches!(selected.targets[name].sources.as_slice(), [Source::Path(path)] + if path == "test/cmocka/test_source_video_render_draws.c") + ); + assert!(selected.targets[name].definitions.is_empty()); + assert_eq!(selected.tests[name].target, name); + for os in ["OS_MACOS", "OS_WINDOWS"] { + let facts = [ + (os.into(), true), + ("OS_LINUX".into(), false), + ("HAVE_OBS_OPENGL".into(), true), + ]; + let selected = graph.select(&facts.into()).unwrap(); + assert!(selected.targets.is_empty(), "{os}"); + } + let ported = fs::read_to_string( + root.join("libobs/build/ported-source-sets/source-video-render-draws.json"), + ) + .unwrap(); + assert!(ported.contains("build/libobs.ported-source-video-render-draws+.h.c")); + let group = + fs::read_to_string(root.join("libobs/build/libobs.ported-source-video-render-draws+.h.c")) + .unwrap(); + assert!(group.contains("#include \"libobs/obs-source-video-render-draws.c.h\"")); + let hybrid = fs::read_to_string(root.join("libobs/build/libobs.core-source+.h.c")).unwrap(); + assert!(!hybrid.contains("#include \"libobs/obs-source-video-render-draws.c.h\"")); + assert!(hybrid.contains("#include \"libobs/obs-source-video-render-draws-fields.c.h\"")); + // obs-source.c.h keeps no copy of the draws; its skip wrapper calls the + // export. + let source = fs::read_to_string(root.join("libobs/obs-source.c.h")).unwrap(); + for step in [ + "obs_source_render_filters(obs_source_t *source)\n{", + "obs_source_main_render(obs_source_t *source)\n{", + ] { + assert!(!source.contains(step), "{step}"); + } + assert!(source.contains("\tobs_source_video_render_main_render(source);\n")); + let hidden = fs::read_to_string( + root.join("libobs/cmake/rust-hidden.d/obs-source-source_video_render_draws.txt"), + ) + .unwrap(); + let steps = [ + "obs_source_video_render_render_filters", + "obs_source_video_render_main_render", + ]; + assert_eq!( + hidden + .lines() + .filter(|line| !line.starts_with('#')) + .collect::>(), + steps + ); + // Hidden, not exported: neither has EXPORT. + for entry in fs::read_dir(root.join("libobs/cmake/rust-exports.d")).unwrap() { + let text = fs::read_to_string(entry.unwrap().path()).unwrap(); + for step in steps { + assert!(!text.lines().any(|line| line == step), "{step}"); + } + } +} diff --git a/rust/obs-c-oracle/oracle.d/source-video-render-draws.list b/rust/obs-c-oracle/oracle.d/source-video-render-draws.list new file mode 100644 index 0000000000..4c472dba76 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/source-video-render-draws.list @@ -0,0 +1 @@ +source-video-render-draws.inc|libobs/obs-source-video-render-draws.c.h|auto diff --git a/rust/obs-c-oracle/oracle/source_video_render_draws.c b/rust/obs-c-oracle/oracle/source_video_render_draws.c new file mode 100644 index 0000000000..42dff2a32c --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_video_render_draws.c @@ -0,0 +1,445 @@ +/* Tier 3: the unchanged extracted obs_source_render_filters() and + * obs_source_main_render() (libobs/obs-source-video-render-draws.c.h), with + * their hidden wrappers renamed oracle_obs_source_video_render_render_filters + * and oracle_obs_source_video_render_main_render, against the Rust exports, + * over the real source layout. The route decisions they ask are the + * oracle's original C (source_render_route.c). + * + * The production adapters the Rust exports call + * (libobs/obs-source-video-render-draws-fields.c.h) are compiled here under + * their real names, the one definition of each in the test binaries. Both + * they and the original go through the same stand-ins: filter_mutex's lock + * and unlock, the reference and its release, obs_source_video_render(), + * the linear sRGB flag, obs_source_default_render(), gs_get_effect() and + * source_render() log one event each, with the node they were given (or + * the value), the references held and node 0's rendering_filter at that + * moment. The fields are the real ones of noise-filled sources. With the + * clobber bits set the stand-ins also rewrite fields and the flag, so a + * field read in another place than the C's shows. The log is per thread. + * + * Each side of a case (0 the C, 1 the Rust) has its own nodes, so the two + * can be compared by token and byte for byte afterwards. */ +#define _NO_CRT_STDIO_INLINE +#include +#include +#include "libobs/build/libobs.pch.h" + +#define VRD_NODES 3 +#define VRD_EVENTS 32 +/* the token of NULL, and of a pointer that is none of the side's nodes */ +#define VRD_NULL -1 +#define VRD_FOREIGN 99 + +enum vrd_kind { + VRD_LOCK = 1, + VRD_UNLOCK, + VRD_GET_REF, + VRD_RELEASE, + VRD_VIDEO_RENDER, + VRD_GET_LINEAR_SRGB, + VRD_SET_LINEAR_SRGB, + VRD_DEFAULT_RENDER, + VRD_GET_EFFECT, + VRD_SOURCE_RENDER, +}; + +struct oracle_vrd_event { + uint32_t kind; + int32_t node; + /* the references held, over every node */ + int32_t refs; + /* node 0's rendering_filter */ + int32_t rendering; + /* the linear sRGB flag, or for source_render() the effect's token + * (0 NULL, 1 the effect, VRD_FOREIGN another) */ + int32_t a; +}; + +struct oracle_vrd_log { + uint32_t events; + struct oracle_vrd_event event[VRD_EVENTS]; +}; + +/* A case: node 0 is the source drawn, nodes 1 and 2 its filters (node 2 + * also its filter parent when it has one). Mirrored by the parity test. */ +struct oracle_vrd_spec { + uint32_t flags; + /* filters.num of node 0, 0 to 2; filters.array is NULL for 0 */ + uint32_t filters; + bool filter_parent; + /* filters.array[0] is NULL */ + bool null_first; + bool has_data; + bool rendering_filter; + /* obs_source_get_ref() gives NULL for node i */ + bool dying[VRD_NODES]; + /* gs_get_linear_srgb() before the draw */ + bool linear_srgb; + /* gs_get_effect() gives an effect, else NULL */ + bool effect; + uint32_t noise; + /* bit 0: the lock moves node 2 to filters.array[0]; bit 1: the unlock + * does; bit 2: obs_source_video_render() flips node 0's + * rendering_filter; bits 3 to 5: gs_get_linear_srgb() flips node 0's + * OBS_SOURCE_SRGB and OBS_SOURCE_CUSTOM_DRAW flags (3), filter_parent + * (4) and filters.num between 0 and 1 (5); bit 6: + * gs_set_linear_srgb(false) flips context.data; bit 7: the draws flip + * the linear sRGB flag; bit 8: the draws flip node 0's + * OBS_SOURCE_SRGB; bit 9: gs_get_effect() flips context.data */ + uint32_t clobber; +}; + +typedef void (*vrd_entry_fn)(obs_source_t *source); + +struct vrd_side { + struct obs_source *nodes; + obs_source_t *filters[2]; + int32_t refs[VRD_NODES]; + bool linear_srgb; +}; + +static THREAD_LOCAL struct vrd_side vrd_sides[2]; +static THREAD_LOCAL struct oracle_vrd_spec vrd_spec; +static THREAD_LOCAL struct oracle_vrd_log vrd_log; +static THREAD_LOCAL struct vrd_side *vrd_watched; + +/* context.data of a node with data, and the current effect: never + * dereferenced */ +static char vrd_data; +static char vrd_effect; + +static int32_t vrd_token(const void *p) +{ + if (!p) + return VRD_NULL; + if (!vrd_watched) + return VRD_FOREIGN; + const struct obs_source *s = p; + if (s >= vrd_watched->nodes && s < vrd_watched->nodes + VRD_NODES) + return (int32_t)(s - vrd_watched->nodes); + return VRD_FOREIGN; +} + +static obs_source_t *vrd_node(int32_t token) +{ + if (!vrd_watched || token < 0 || token >= VRD_NODES) + return NULL; + return &vrd_watched->nodes[token]; +} + +static void vrd_record(uint32_t kind, int32_t node, int32_t a) +{ + if (vrd_log.events >= VRD_EVENTS) + return; + struct oracle_vrd_event *e = &vrd_log.event[vrd_log.events++]; + e->kind = kind; + e->node = node; + e->refs = 0; + for (int32_t i = 0; vrd_watched && i < VRD_NODES; i++) + e->refs += vrd_watched->refs[i]; + e->rendering = vrd_watched && vrd_watched->nodes[0].rendering_filter ? 1 : 0; + e->a = a; +} + +/* ------------------------------------------------------------------------ */ +/* the stand-ins */ + +static int32_t vrd_mutex_token(pthread_mutex_t *mutex) +{ + for (int32_t i = 0; vrd_watched && i < VRD_NODES; i++) { + if (mutex == &vrd_watched->nodes[i].filter_mutex) + return i; + } + return VRD_FOREIGN; +} + +static void vrd_move_node_2_first(void) +{ + struct obs_source *source = vrd_node(0); + if (source && source->filters.array) + source->filters.array[0] = vrd_node(2); +} + +static int vrd_lock(pthread_mutex_t *mutex) +{ + vrd_record(VRD_LOCK, vrd_mutex_token(mutex), 0); + if (vrd_spec.clobber & 1) + vrd_move_node_2_first(); + return 0; +} + +static int vrd_unlock(pthread_mutex_t *mutex) +{ + vrd_record(VRD_UNLOCK, vrd_mutex_token(mutex), 0); + if (vrd_spec.clobber & 2) + vrd_move_node_2_first(); + return 0; +} + +static obs_source_t *vrd_get_ref(obs_source_t *source) +{ + const int32_t n = vrd_token(source); + vrd_record(VRD_GET_REF, n, 0); + if (n < 0 || n >= VRD_NODES || vrd_spec.dying[n]) + return NULL; + vrd_watched->refs[n]++; + return source; +} + +static void vrd_release(obs_source_t *source) +{ + const int32_t n = vrd_token(source); + if (n >= 0 && n < VRD_NODES) + vrd_watched->refs[n]--; + vrd_record(VRD_RELEASE, n, 0); +} + +static void vrd_video_render(obs_source_t *source) +{ + vrd_record(VRD_VIDEO_RENDER, vrd_token(source), 0); + struct obs_source *s = vrd_node(0); + if (s && (vrd_spec.clobber & 4)) + s->rendering_filter = !s->rendering_filter; +} + +static bool vrd_get_linear_srgb(void) +{ + const bool value = vrd_watched && vrd_watched->linear_srgb; + vrd_record(VRD_GET_LINEAR_SRGB, VRD_NULL, value); + struct obs_source *s = vrd_node(0); + if (s) { + const uint32_t c = vrd_spec.clobber; + if (c & 8) + s->info.output_flags ^= OBS_SOURCE_SRGB | OBS_SOURCE_CUSTOM_DRAW; + if (c & 16) + s->filter_parent = s->filter_parent ? NULL : vrd_node(2); + if (c & 32) + s->filters.num = s->filters.num ? 0 : 1; + } + return value; +} + +static void vrd_set_linear_srgb(bool linear_srgb) +{ + if (vrd_watched) + vrd_watched->linear_srgb = linear_srgb; + vrd_record(VRD_SET_LINEAR_SRGB, VRD_NULL, linear_srgb); + struct obs_source *s = vrd_node(0); + if (s && !linear_srgb && (vrd_spec.clobber & 64)) + s->context.data = s->context.data ? NULL : &vrd_data; +} + +/* What both draws do to the state with bits 7 and 8. */ +static void vrd_draw_clobbers(void) +{ + struct obs_source *s = vrd_node(0); + if (vrd_watched && (vrd_spec.clobber & 128)) + vrd_watched->linear_srgb = !vrd_watched->linear_srgb; + if (s && (vrd_spec.clobber & 256)) + s->info.output_flags ^= OBS_SOURCE_SRGB; +} + +static void vrd_default_render(obs_source_t *source) +{ + vrd_record(VRD_DEFAULT_RENDER, vrd_token(source), vrd_watched && vrd_watched->linear_srgb); + vrd_draw_clobbers(); +} + +static gs_effect_t *vrd_get_effect(void) +{ + vrd_record(VRD_GET_EFFECT, VRD_NULL, 0); + struct obs_source *s = vrd_node(0); + if (s && (vrd_spec.clobber & 512)) + s->context.data = s->context.data ? NULL : &vrd_data; + return vrd_spec.effect ? (gs_effect_t *)&vrd_effect : NULL; +} + +static void vrd_source_render(obs_source_t *source, gs_effect_t *effect) +{ + const int32_t token = !effect ? 0 : (void *)effect == (void *)&vrd_effect ? 1 : VRD_FOREIGN; + vrd_record(VRD_SOURCE_RENDER, vrd_token(source), token); + vrd_draw_clobbers(); +} + +/* ------------------------------------------------------------------------ */ +/* the original and the production adapters, over the stand-ins */ + +/* The mingw build renames every pthread function on the command line + * (w32-pthreads): keep that name for the rest of the file and use the + * recorders only for the original and the adapters. */ +#pragma push_macro("pthread_mutex_lock") +#pragma push_macro("pthread_mutex_unlock") +#undef pthread_mutex_lock +#undef pthread_mutex_unlock +#define pthread_mutex_lock vrd_lock +#define pthread_mutex_unlock vrd_unlock +#define obs_source_get_ref vrd_get_ref +#define obs_source_release vrd_release +#define obs_source_video_render vrd_video_render +#define gs_get_linear_srgb vrd_get_linear_srgb +#define gs_set_linear_srgb vrd_set_linear_srgb +#define obs_source_default_render vrd_default_render +#define gs_get_effect vrd_get_effect +#define obs_source_default_render_source_render vrd_source_render + +#define obs_source_video_render_render_filters oracle_obs_source_video_render_render_filters +#define obs_source_video_render_main_render oracle_obs_source_video_render_main_render +#define obs_source_render_route_main_custom_draw oracle_obs_source_render_route_main_custom_draw +#define obs_source_render_route_main_srgb_aware oracle_obs_source_render_route_main_srgb_aware +#define obs_source_render_route_main_reads_filters oracle_obs_source_render_route_main_reads_filters +#define obs_source_render_route_main_default_effect oracle_obs_source_render_route_main_default_effect + +bool oracle_obs_source_render_route_main_custom_draw(uint32_t flags); +bool oracle_obs_source_render_route_main_srgb_aware(uint32_t flags); +bool oracle_obs_source_render_route_main_reads_filters(bool has_filter_parent); +bool oracle_obs_source_render_route_main_default_effect(uint32_t flags, bool has_filter_parent, bool has_filters); + +#include "source-video-render-draws.inc" + +#undef obs_source_render_route_main_default_effect +#undef obs_source_render_route_main_reads_filters +#undef obs_source_render_route_main_srgb_aware +#undef obs_source_render_route_main_custom_draw +#undef obs_source_video_render_main_render +#undef obs_source_video_render_render_filters + +#include "source-video-render-draws-fields.inc" + +#undef obs_source_default_render_source_render +#undef gs_get_effect +#undef obs_source_default_render +#undef gs_set_linear_srgb +#undef gs_get_linear_srgb +#undef obs_source_video_render +#undef obs_source_release +#undef obs_source_get_ref +#undef pthread_mutex_unlock +#undef pthread_mutex_lock +#pragma pop_macro("pthread_mutex_unlock") +#pragma pop_macro("pthread_mutex_lock") + +/* ------------------------------------------------------------------------ */ +/* the fixtures */ + +/* Side `side`'s nodes, noise-filled and set to `spec` (which also becomes + * the stand-ins' script). */ +void oracle_vrd_new(const struct oracle_vrd_spec *spec, int side); +void oracle_vrd_new(const struct oracle_vrd_spec *spec, int side) +{ + struct vrd_side *s = &vrd_sides[side ? 1 : 0]; + struct vrd_side *watched = vrd_watched; + + vrd_spec = *spec; + s->nodes = bmalloc(sizeof(struct obs_source) * VRD_NODES); + memset(s->nodes, (int)(spec->noise & 0xff), sizeof(struct obs_source) * VRD_NODES); + memset(s->refs, 0, sizeof(s->refs)); + s->linear_srgb = spec->linear_srgb; + vrd_watched = s; + s->filters[0] = spec->null_first ? NULL : vrd_node(1); + s->filters[1] = vrd_node(2); + for (int32_t i = 0; i < VRD_NODES; i++) { + struct obs_source *source = &s->nodes[i]; + source->info.output_flags = OBS_SOURCE_VIDEO; + source->filter_parent = vrd_node(0); + source->filters.array = NULL; + source->filters.num = 0; + source->context.data = &vrd_data; + source->rendering_filter = false; + } + struct obs_source *source = &s->nodes[0]; + source->info.output_flags = spec->flags; + source->filter_parent = spec->filter_parent ? vrd_node(2) : NULL; + source->filters.num = spec->filters > 2 ? 2 : spec->filters; + source->filters.array = source->filters.num ? s->filters : NULL; + source->context.data = spec->has_data ? &vrd_data : NULL; + source->rendering_filter = spec->rendering_filter; + vrd_watched = watched; +} + +void oracle_vrd_free(int side); +void oracle_vrd_free(int side) +{ + struct vrd_side *s = &vrd_sides[side ? 1 : 0]; + if (!s->nodes) + return; + if (vrd_watched == s) + vrd_watched = NULL; + bfree(s->nodes); + s->nodes = NULL; +} + +/* Run side `side` on node 0 through `entry`, the log cleared first. */ +void oracle_vrd_run(int side, vrd_entry_fn entry); +void oracle_vrd_run(int side, vrd_entry_fn entry) +{ + vrd_watched = &vrd_sides[side ? 1 : 0]; + memset(&vrd_log, 0, sizeof(vrd_log)); + entry(vrd_node(0)); + vrd_watched = NULL; +} + +/* The originals, as the C reference runs them. */ +vrd_entry_fn oracle_vrd_original_filters(void); +vrd_entry_fn oracle_vrd_original_filters(void) +{ + return oracle_obs_source_video_render_render_filters; +} + +vrd_entry_fn oracle_vrd_original_main(void); +vrd_entry_fn oracle_vrd_original_main(void) +{ + return oracle_obs_source_video_render_main_render; +} + +void oracle_vrd_log(struct oracle_vrd_log *out); +void oracle_vrd_log(struct oracle_vrd_log *out) +{ + *out = vrd_log; +} + +size_t oracle_vrd_events(void); +size_t oracle_vrd_events(void) +{ + return VRD_EVENTS; +} + +/* A side's references still held per node (0 when balanced), and its + * linear sRGB flag. */ +bool oracle_vrd_state(int side, int32_t *refs); +bool oracle_vrd_state(int side, int32_t *refs) +{ + const struct vrd_side *s = &vrd_sides[side ? 1 : 0]; + memcpy(refs, s->refs, sizeof(s->refs)); + return s->linear_srgb; +} + +/* Whether the two sides' nodes and filter arrays are byte for byte the + * same, their links compared by token. */ +bool oracle_vrd_same(void); +bool oracle_vrd_same(void) +{ + const size_t size = sizeof(struct obs_source) * VRD_NODES; + struct obs_source *x = bmemdup(vrd_sides[0].nodes, size); + struct obs_source *y = bmemdup(vrd_sides[1].nodes, size); + struct vrd_side *watched = vrd_watched; + intptr_t fx[2], fy[2]; + for (int32_t i = 0; i < VRD_NODES; i++) { + vrd_watched = &vrd_sides[0]; + x[i].filter_parent = (obs_source_t *)(intptr_t)vrd_token(x[i].filter_parent); + x[i].filters.array = (obs_source_t **)(intptr_t)(x[i].filters.array != NULL); + vrd_watched = &vrd_sides[1]; + y[i].filter_parent = (obs_source_t *)(intptr_t)vrd_token(y[i].filter_parent); + y[i].filters.array = (obs_source_t **)(intptr_t)(y[i].filters.array != NULL); + } + for (int32_t i = 0; i < 2; i++) { + vrd_watched = &vrd_sides[0]; + fx[i] = vrd_token(vrd_sides[0].filters[i]); + vrd_watched = &vrd_sides[1]; + fy[i] = vrd_token(vrd_sides[1].filters[i]); + } + vrd_watched = watched; + bool same = memcmp(x, y, size) == 0 && memcmp(fx, fy, sizeof(fx)) == 0; + bfree(x); + bfree(y); + return same; +} diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index bd8f63dd60..d62f46445b 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -127,6 +127,7 @@ pub mod source_type_lookup; pub mod source_update; pub mod source_update_properties; pub mod source_video_render; +pub mod source_video_render_draws; pub mod source_video_tick; pub mod source_video_tick_callbacks; pub mod source_video_tick_entry; diff --git a/rust/obs-source/src/ffi/source_video_render_draws.rs b/rust/obs-source/src/ffi/source_video_render_draws.rs new file mode 100644 index 0000000000..99ab975ca8 --- /dev/null +++ b/rust/obs-source/src/ffi/source_video_render_draws.rs @@ -0,0 +1,181 @@ +//! C ABI for `libobs/obs-source-video-render-draws.c.h`: +//! `obs_source_render_filters()` and `obs_source_main_render()`, the two +//! draws `obs_source_video_render()` makes, exported as their hidden +//! wrappers `obs_source_video_render_render_filters()` and +//! `obs_source_video_render_main_render()`. Neither has `EXPORT`, so the +//! shims stay hidden (`rust-hidden.d`). `obs_source_main_render()` is also +//! the main draw of `obs_source_skip_video_filter()`, whose wrapper in +//! `obs-source.c.h` calls the export. +//! +//! Runs the safe core ([`crate::video_render_draws`]) over the C-owned +//! source: `filter_mutex`, every field read and store, the reference and +//! its release, the linear sRGB flag, the current effect and every draw +//! (`obs_source_video_render()`, `obs_source_default_render()` and +//! `source_render()`) are one access each through the adapters of +//! `obs-source-video-render-draws-fields.c.h`. Like the C, nothing checks +//! the source: the render passes a live one, inside the graphics context. +//! The source and effect pointers cross as they are. +//! +//! No intentional differences from C. + +use core::ffi::c_void; + +use crate::video_render_draws::{self, Host}; + +unsafe extern "C" { + fn obs_source_video_render_draws_lock(source: *mut c_void); + fn obs_source_video_render_draws_first_filter(source: *const c_void) -> *mut c_void; + fn obs_source_video_render_draws_get_ref(source: *mut c_void) -> *mut c_void; + fn obs_source_video_render_draws_unlock(source: *mut c_void); + fn obs_source_video_render_draws_set_rendering_filter(source: *mut c_void, rendering: bool); + fn obs_source_video_render_draws_video_render(source: *mut c_void); + fn obs_source_video_render_draws_release(source: *mut c_void); + fn obs_source_video_render_draws_flags(source: *const c_void) -> u32; + fn obs_source_video_render_draws_has_filter_parent(source: *const c_void) -> bool; + fn obs_source_video_render_draws_has_filters(source: *const c_void) -> bool; + fn obs_source_video_render_draws_linear_srgb() -> bool; + fn obs_source_video_render_draws_set_linear_srgb(linear_srgb: bool); + fn obs_source_video_render_draws_default_render(source: *mut c_void); + fn obs_source_video_render_draws_has_data(source: *const c_void) -> bool; + fn obs_source_video_render_draws_effect() -> *mut c_void; + fn obs_source_video_render_draws_source_render(source: *mut c_void, effect: *mut c_void); +} + +/// A source as the C passes it; only the adapters dereference it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Source(*mut c_void); + +impl Source { + /// A C source. + pub fn from_raw(ptr: *mut c_void) -> Self { + Self(ptr) + } + + /// The C source. + pub fn as_raw(self) -> *mut c_void { + self.0 + } +} + +/// A non-NULL effect as `gs_get_effect()` gives it; never dereferenced. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Effect(*mut c_void); + +impl Effect { + /// The C effect. + pub fn as_raw(self) -> *mut c_void { + self.0 + } +} + +/// `Some` for a non-NULL source. +fn some(ptr: *mut c_void) -> Option { + (!ptr.is_null()).then_some(Source(ptr)) +} + +/// NULL for `None`. +fn raw(source: Option) -> *mut c_void { + source.map_or(core::ptr::null_mut(), Source::as_raw) +} + +/// libobs. +#[derive(Clone, Copy, Debug, Default)] +pub struct Libobs; + +// SAFETY (every method): `source` is the caller's live source, or the +// reference taken on its first filter (NULL where the C passes NULL), as +// the core calls them; in the graphics context. +impl Host for Libobs { + type Source = Source; + type Effect = Effect; + + fn lock_filters(&self, source: Source) { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_lock(source.0) } + } + fn first_filter(&self, source: Source) -> Option { + // SAFETY: a live source, its filter_mutex held. + some(unsafe { obs_source_video_render_draws_first_filter(source.0) }) + } + fn get_ref(&self, source: Option) -> Option { + // SAFETY: obs_source_get_ref() takes NULL. + some(unsafe { obs_source_video_render_draws_get_ref(raw(source)) }) + } + fn unlock_filters(&self, source: Source) { + // SAFETY: a live source whose filter_mutex the core locked. + unsafe { obs_source_video_render_draws_unlock(source.0) } + } + fn set_rendering_filter(&self, source: Source, rendering: bool) { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_set_rendering_filter(source.0, rendering) } + } + fn video_render(&self, source: Option) { + // SAFETY: obs_source_video_render() takes NULL. + unsafe { obs_source_video_render_draws_video_render(raw(source)) } + } + fn release(&self, source: Option) { + // SAFETY: obs_source_release() takes NULL; otherwise the reference + // the core took. + unsafe { obs_source_video_render_draws_release(raw(source)) } + } + fn output_flags(&self, source: Source) -> u32 { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_flags(source.0) } + } + fn has_filter_parent(&self, source: Source) -> bool { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_has_filter_parent(source.0) } + } + fn has_filters(&self, source: Source) -> bool { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_has_filters(source.0) } + } + fn linear_srgb(&self) -> bool { + // SAFETY: in the graphics context. + unsafe { obs_source_video_render_draws_linear_srgb() } + } + fn set_linear_srgb(&self, linear_srgb: bool) { + // SAFETY: in the graphics context. + unsafe { obs_source_video_render_draws_set_linear_srgb(linear_srgb) } + } + fn default_render(&self, source: Source) { + // SAFETY: a live source, in the graphics context. + unsafe { obs_source_video_render_draws_default_render(source.0) } + } + fn has_data(&self, source: Source) -> bool { + // SAFETY: a live source. + unsafe { obs_source_video_render_draws_has_data(source.0) } + } + fn current_effect(&self) -> Option { + // SAFETY: in the graphics context. + let effect = unsafe { obs_source_video_render_draws_effect() }; + (!effect.is_null()).then_some(Effect(effect)) + } + fn source_render(&self, source: Source, effect: Option) { + let effect = effect.map_or(core::ptr::null_mut(), Effect::as_raw); + // SAFETY: a live source with data, in the graphics context; the + // effect is NULL or the current one. + unsafe { obs_source_video_render_draws_source_render(source.0, effect) } + } +} + +/// `obs_source_video_render_render_filters(source)`: +/// `obs_source_render_filters(source)`. +/// +/// # Safety +/// `source` is a live source with at least one filter, drawn inside the +/// graphics context. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_video_render_render_filters(source: *mut c_void) { + video_render_draws::render_filters(&Libobs, Source(source)); +} + +/// `obs_source_video_render_main_render(source)`: +/// `obs_source_main_render(source)`. +/// +/// # Safety +/// `source` is a live source, drawn inside the graphics context. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_video_render_main_render(source: *mut c_void) { + video_render_draws::main_render(&Libobs, Source(source)); +} diff --git a/rust/obs-source/src/video_render_draws.rs b/rust/obs-source/src/video_render_draws.rs index ab51883e43..219f6f8b5c 100644 --- a/rust/obs-source/src/video_render_draws.rs +++ b/rust/obs-source/src/video_render_draws.rs @@ -44,10 +44,13 @@ clippy::unwrap_used, clippy::expect_used, clippy::panic, + clippy::todo, clippy::unimplemented, clippy::unreachable )] +use crate::render_route; + /// The source's lock, fields and references and the calls the two draws /// make, in the C's terms. pub trait Host { @@ -94,8 +97,15 @@ pub trait Host { /// `obs_source_render_filters(source)`: the first filter, referenced under /// `filter_mutex`, drawn with `rendering_filter` set, then released. pub fn render_filters(host: &H, source: H::Source) { - let _ = (host, source); - todo!("obs_source_render_filters") + host.lock_filters(source); + let first_filter = host.get_ref(host.first_filter(source)); + host.unlock_filters(source); + + host.set_rendering_filter(source, true); + host.video_render(first_filter); + host.set_rendering_filter(source, false); + + host.release(first_filter); } /// `obs_source_main_render(source)`: the default render, or the plugin's @@ -103,6 +113,32 @@ pub fn render_filters(host: &H, source: H::Source) { /// has data, with the linear sRGB flag cleared around it unless the source /// is sRGB aware. pub fn main_render(host: &H, source: H::Source) { - let _ = (host, source); - todo!("obs_source_main_render") + let flags = host.output_flags(source); + let custom_draw = render_route::main_custom_draw(flags); + let srgb_aware = render_route::main_srgb_aware(flags); + let has_filter_parent = host.has_filter_parent(source); + let has_filters = + render_route::main_reads_filters(has_filter_parent) && host.has_filters(source); + let default_effect = render_route::main_default_effect(flags, has_filter_parent, has_filters); + let mut previous_srgb = false; + + if !srgb_aware { + previous_srgb = host.linear_srgb(); + host.set_linear_srgb(false); + } + + if default_effect { + host.default_render(source); + } else if host.has_data(source) { + let effect = if custom_draw { + None + } else { + host.current_effect() + }; + host.source_render(source, effect); + } + + if !srgb_aware { + host.set_linear_srgb(previous_srgb); + } } diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 6b300e4ad2..a17e4341b7 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -265,6 +265,7 @@ mod update_properties; mod update_properties_parity; mod video_render; mod video_render_draws; +mod video_render_draws_parity; mod video_render_parity; mod video_tick; mod video_tick_callbacks; diff --git a/rust/obs-source/tests/source/video_render_draws_parity.rs b/rust/obs-source/tests/source/video_render_draws_parity.rs new file mode 100644 index 0000000000..2fb4c9b542 --- /dev/null +++ b/rust/obs-source/tests/source/video_render_draws_parity.rs @@ -0,0 +1,611 @@ +//! Tier 3: the original C `obs_source_render_filters()` and +//! `obs_source_main_render()` (`source_video_render_draws.c`, the extracted +//! file compiled with its wrappers renamed, and the oracle's original route +//! decisions) against the Rust exports, each side on its own three +//! noise-filled real-layout sources set up the same way. Both go through +//! the oracle's stand-ins for `filter_mutex`, the reference and its +//! release, `obs_source_video_render()`, the linear sRGB flag, +//! `obs_source_default_render()`, `gs_get_effect()` and `source_render()` +//! (the Rust side through the production adapters), so one log holds every +//! call in order with its node or value, the references held and the +//! source's `rendering_filter`. With the clobber bits the stand-ins move +//! another filter to the front from the lock and the unlock, flip +//! `rendering_filter` from the filter's draw, flip the output flags, +//! `filter_parent` and `filters.num` once the flag is read, flip +//! `context.data` once the flag is cleared or the effect is read, and flip +//! the flag and the output flags from the draws, so a field read out of +//! place shows. The log, the references and the flag left and every byte +//! of the sources must match over a grid and a proptest. Twelve mutants, +//! each the core with one change on the same production adapters, must +//! not match. No intentional differences or exclusions. +use core::cell::Cell; +use core::ffi::{c_int, c_void}; +use obs_source::ffi::source_video_render_draws::{self as rust, Libobs, Source}; +use obs_source::render_route::{self, FLAG_ASYNC, FLAG_CUSTOM_DRAW, FLAG_SRGB, FLAG_VIDEO}; +use obs_source::video_render_draws::Host; +use proptest::prelude::*; + +const NODES: usize = 3; +const EVENTS: usize = 32; + +#[repr(C)] +#[derive(Debug, Clone, Copy)] +struct Spec { + flags: u32, + filters: u32, + filter_parent: bool, + null_first: bool, + has_data: bool, + rendering_filter: bool, + dying: [bool; NODES], + linear_srgb: bool, + effect: bool, + noise: u32, + clobber: u32, +} + +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct RawEvent { + kind: u32, + node: i32, + refs: i32, + rendering: i32, + a: i32, +} + +#[repr(C)] +struct Log { + events: u32, + event: [RawEvent; EVENTS], +} + +type Entry = unsafe extern "C" fn(*mut c_void); + +unsafe extern "C" { + fn oracle_vrd_new(spec: *const Spec, side: c_int); + fn oracle_vrd_free(side: c_int); + fn oracle_vrd_run(side: c_int, entry: Entry); + fn oracle_vrd_original_filters() -> Entry; + fn oracle_vrd_original_main() -> Entry; + fn oracle_vrd_log(out: *mut Log); + fn oracle_vrd_events() -> usize; + fn oracle_vrd_state(side: c_int, refs: *mut i32) -> bool; + fn oracle_vrd_same() -> bool; +} + +/// The core with one change each, on the production adapters. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Mutant { + /// The first filter is referenced after the unlock. + RefAfterUnlock, + /// The release reads the first filter again. + ReleaseRereads, + /// `rendering_filter` is never set. + NoRenderingFilter, + /// `rendering_filter` is left set. + RenderingFilterLeftSet, + /// The reference is never given back. + NoRelease, + /// The restore reads the output flags again. + FlagsReadTwice, + /// `filters.num` is read after the flag is saved. + FiltersLate, + /// The default route ignores the filters. + DefaultIgnoresFilters, + /// The plugin's draw runs without data. + DataUnchecked, + /// A custom draw gets the current effect. + EffectForCustom, + /// The flag is set back to true. + RestoresTrue, + /// An sRGB-aware source has the flag cleared too. + SrgbIgnored, +} + +const MUTANTS: [Mutant; 12] = [ + Mutant::RefAfterUnlock, + Mutant::ReleaseRereads, + Mutant::NoRenderingFilter, + Mutant::RenderingFilterLeftSet, + Mutant::NoRelease, + Mutant::FlagsReadTwice, + Mutant::FiltersLate, + Mutant::DefaultIgnoresFilters, + Mutant::DataUnchecked, + Mutant::EffectForCustom, + Mutant::RestoresTrue, + Mutant::SrgbIgnored, +]; + +thread_local! { + static MUTANT: Cell = const { Cell::new(Mutant::RefAfterUnlock) }; +} + +fn mutant() -> Mutant { + MUTANT.with(Cell::get) +} + +/// `obs_source_render_filters()` with the mutant's change. +unsafe extern "C" fn mutant_filters(raw: *mut c_void) { + let m = mutant(); + let host = Libobs; + let source = Source::from_raw(raw); + host.lock_filters(source); + let first = if m == Mutant::RefAfterUnlock { + let first = host.first_filter(source); + host.unlock_filters(source); + host.get_ref(first) + } else { + let first = host.get_ref(host.first_filter(source)); + host.unlock_filters(source); + first + }; + if m != Mutant::NoRenderingFilter { + host.set_rendering_filter(source, true); + } + host.video_render(first); + if m != Mutant::RenderingFilterLeftSet && m != Mutant::NoRenderingFilter { + host.set_rendering_filter(source, false); + } + match m { + Mutant::NoRelease => {} + Mutant::ReleaseRereads => { + host.lock_filters(source); + let again = host.first_filter(source); + host.unlock_filters(source); + host.release(again); + } + _ => host.release(first), + } +} + +/// `obs_source_main_render()` with the mutant's change. +unsafe extern "C" fn mutant_main(raw: *mut c_void) { + let m = mutant(); + let host = Libobs; + let source = Source::from_raw(raw); + let flags = host.output_flags(source); + let custom_draw = render_route::main_custom_draw(flags); + let srgb_aware = render_route::main_srgb_aware(flags) && m != Mutant::SrgbIgnored; + let has_filter_parent = host.has_filter_parent(source); + let mut has_filters = m != Mutant::FiltersLate + && render_route::main_reads_filters(has_filter_parent) + && host.has_filters(source); + let mut previous = false; + if !srgb_aware { + previous = host.linear_srgb(); + host.set_linear_srgb(false); + } + if m == Mutant::FiltersLate { + has_filters = + render_route::main_reads_filters(has_filter_parent) && host.has_filters(source); + } + if m == Mutant::DefaultIgnoresFilters { + has_filters = false; + } + if render_route::main_default_effect(flags, has_filter_parent, has_filters) { + host.default_render(source); + } else if m == Mutant::DataUnchecked || host.has_data(source) { + let effect = if custom_draw && m != Mutant::EffectForCustom { + None + } else { + host.current_effect() + }; + host.source_render(source, effect); + } + let restore = if m == Mutant::FlagsReadTwice { + !render_route::main_srgb_aware(host.output_flags(source)) + } else { + !srgb_aware + }; + if restore { + host.set_linear_srgb(previous || m == Mutant::RestoresTrue); + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Draw { + Filters, + Main, +} + +#[derive(Debug, Clone, Copy)] +enum Side { + C, + Rust, + Mutant(Mutant), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Run { + log: Vec, + refs: [i32; NODES], + linear_srgb: bool, +} + +fn take_log() -> Vec { + let mut log = Box::new(Log { + events: 0, + event: [RawEvent { + kind: 0, + node: 0, + refs: 0, + rendering: 0, + a: 0, + }; EVENTS], + }); + unsafe { oracle_vrd_log(&mut *log) }; + let n = usize::try_from(log.events).unwrap().min(EVENTS); + log.event[..n].to_vec() +} + +/// One draw of `side` on its own nodes (side 0 for the C, 1 for the +/// others), the nodes left for [`compare`]. +fn run(spec: &Spec, draw: Draw, side: Side) -> Run { + let k = c_int::from(!matches!(side, Side::C)); + run_on(spec, k, entry(draw, side)) +} + +/// The entry of `side` for `draw`. +fn entry(draw: Draw, side: Side) -> Entry { + match (side, draw) { + (Side::C, Draw::Filters) => unsafe { oracle_vrd_original_filters() }, + (Side::C, Draw::Main) => unsafe { oracle_vrd_original_main() }, + (Side::Rust, Draw::Filters) => rust::obs_source_video_render_render_filters, + (Side::Rust, Draw::Main) => rust::obs_source_video_render_main_render, + (Side::Mutant(m), d) => { + MUTANT.with(|c| c.set(m)); + match d { + Draw::Filters => mutant_filters, + Draw::Main => mutant_main, + } + } + } +} + +/// One draw through `entry` on side `k`'s own nodes. +fn run_on(spec: &Spec, k: c_int, entry: Entry) -> Run { + unsafe { oracle_vrd_new(spec, k) }; + unsafe { oracle_vrd_run(k, entry) }; + let mut refs = [0_i32; NODES]; + let linear_srgb = unsafe { oracle_vrd_state(k, refs.as_mut_ptr()) }; + Run { + log: take_log(), + refs, + linear_srgb, + } +} + +/// The C and `side` over `spec`: both runs, and whether the two sides' +/// nodes are byte for byte the same. +fn compare(spec: &Spec, draw: Draw, side: Side) -> (Run, Run, bool) { + let c = run(spec, draw, Side::C); + let other = run(spec, draw, side); + let same = unsafe { oracle_vrd_same() }; + unsafe { + oracle_vrd_free(0); + oracle_vrd_free(1); + } + (c, other, same) +} + +fn matches(spec: &Spec, draw: Draw, side: Side) -> bool { + let (c, other, same) = compare(spec, draw, side); + c == other && same +} + +// ---------------------------------------------------------------- cases + +/// A plain video source with data and two filters, the flag set and an +/// effect current. +const fn spec() -> Spec { + Spec { + flags: FLAG_VIDEO, + filters: 2, + filter_parent: false, + null_first: false, + has_data: true, + rendering_filter: false, + dying: [false; NODES], + linear_srgb: true, + effect: true, + noise: 0xa5, + clobber: 0, + } +} + +const CLOBBERS: [u32; 12] = [0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023]; + +/// Visits every case of the grid with its index, built one at a time; +/// `visit` returns false to stop. +fn for_each_case(mut visit: impl FnMut(u64, Draw, &Spec) -> bool) { + let mut index = 0_u64; + for flags in [ + FLAG_VIDEO, + FLAG_VIDEO | FLAG_SRGB, + FLAG_VIDEO | FLAG_CUSTOM_DRAW, + FLAG_VIDEO | FLAG_CUSTOM_DRAW | FLAG_SRGB, + FLAG_VIDEO | FLAG_ASYNC, + ] { + for filters in 0..3_u32 { + for bits in 0..256_u32 { + for clobber in CLOBBERS { + let bit = |i: u32| bits & (1 << i) != 0; + let mut s = spec(); + s.flags = flags; + s.filters = filters; + s.filter_parent = bit(0); + s.null_first = bit(1); + s.has_data = bit(2); + s.rendering_filter = bit(3); + s.dying = [false, bit(4), bit(5)]; + s.linear_srgb = bit(6); + s.effect = bit(7); + s.clobber = clobber; + for draw in [Draw::Main, Draw::Filters] { + // the filter chain is drawn only with a filter + if draw == Draw::Filters && filters == 0 { + continue; + } + if !visit(index, draw, &s) { + return; + } + index += 1; + } + } + } + } + } +} + +/// Whether the parity level runs case `index`: the cases without a clobber +/// always, the rest sampled. +fn sampled(index: u64, s: &Spec) -> bool { + s.clobber == 0 || obs_c_oracle::parity::keep(index) +} + +#[test] +fn the_oracle_logs_the_events_the_test_mirrors() { + assert_eq!(unsafe { oracle_vrd_events() }, EVENTS); +} + +#[test] +fn matches_c_over_the_grid() { + let mut ran = 0_u32; + for_each_case(|index, draw, s| { + if sampled(index, s) { + let (c, r, same) = compare(s, draw, Side::Rust); + assert_eq!(c, r, "{draw:?} {s:?}"); + assert!(same, "bytes differ: {draw:?} {s:?}"); + ran += 1; + } + true + }); + assert!(ran > 0); +} + +/// One event as (kind, node, references held, rendering_filter, a). +type Row = (u32, i32, i32, i32, i32); + +fn rows(run: &Run) -> Vec { + run.log + .iter() + .map(|e| (e.kind, e.node, e.refs, e.rendering, e.a)) + .collect() +} + +/// The logs of the main cases: every call, its node and the state it ran +/// in, in the C's order. +#[test] +fn the_logs_see_every_call_in_order() { + const LOCK: u32 = 1; + const UNLOCK: u32 = 2; + const GET_REF: u32 = 3; + const RELEASE: u32 = 4; + const VIDEO_RENDER: u32 = 5; + const GET_LINEAR_SRGB: u32 = 6; + const SET_LINEAR_SRGB: u32 = 7; + const DEFAULT_RENDER: u32 = 8; + const GET_EFFECT: u32 = 9; + const SOURCE_RENDER: u32 = 10; + + let check = |s: &Spec, draw: Draw, want: &[Row]| { + let (c, r, same) = compare(s, draw, Side::Rust); + assert_eq!(c, r, "{s:?}"); + assert!(same); + assert_eq!(rows(&c), want, "{s:?}"); + assert_eq!(c.refs, [0; NODES]); + assert_eq!(c.linear_srgb, s.linear_srgb); + }; + + // The filter chain: node 1 referenced under node 0's lock, drawn with + // rendering_filter set, released. + check( + &spec(), + Draw::Filters, + &[ + (LOCK, 0, 0, 0, 0), + (GET_REF, 1, 0, 0, 0), + (UNLOCK, 0, 1, 0, 0), + (VIDEO_RENDER, 1, 1, 1, 0), + (RELEASE, 1, 0, 0, 0), + ], + ); + // A filter being destroyed: NULL drawn and released. + let mut s = spec(); + s.dying[1] = true; + check( + &s, + Draw::Filters, + &[ + (LOCK, 0, 0, 0, 0), + (GET_REF, 1, 0, 0, 0), + (UNLOCK, 0, 0, 0, 0), + (VIDEO_RENDER, -1, 0, 1, 0), + (RELEASE, -1, 0, 0, 0), + ], + ); + + // A plain source without filters: the default render, the flag + // cleared around it. + let mut s = spec(); + s.filters = 0; + check( + &s, + Draw::Main, + &[ + (GET_LINEAR_SRGB, -1, 0, 0, 1), + (SET_LINEAR_SRGB, -1, 0, 0, 0), + (DEFAULT_RENDER, 0, 0, 0, 0), + (SET_LINEAR_SRGB, -1, 0, 0, 1), + ], + ); + // sRGB aware: the flag untouched. + s.flags |= FLAG_SRGB; + check(&s, Draw::Main, &[(DEFAULT_RENDER, 0, 0, 0, 1)]); + + // With filters: the current effect. + check( + &spec(), + Draw::Main, + &[ + (GET_LINEAR_SRGB, -1, 0, 0, 1), + (SET_LINEAR_SRGB, -1, 0, 0, 0), + (GET_EFFECT, -1, 0, 0, 0), + (SOURCE_RENDER, 0, 0, 0, 1), + (SET_LINEAR_SRGB, -1, 0, 0, 1), + ], + ); + // Custom draw: NULL, the effect never asked for. + let mut s = spec(); + s.flags |= FLAG_CUSTOM_DRAW; + check( + &s, + Draw::Main, + &[ + (GET_LINEAR_SRGB, -1, 0, 0, 1), + (SET_LINEAR_SRGB, -1, 0, 0, 0), + (SOURCE_RENDER, 0, 0, 0, 0), + (SET_LINEAR_SRGB, -1, 0, 0, 1), + ], + ); + // Without data: nothing drawn. + let mut s = spec(); + s.has_data = false; + s.linear_srgb = false; + check( + &s, + Draw::Main, + &[ + (GET_LINEAR_SRGB, -1, 0, 0, 0), + (SET_LINEAR_SRGB, -1, 0, 0, 0), + (SET_LINEAR_SRGB, -1, 0, 0, 0), + ], + ); +} + +/// Each clobber but two reaches the C: its log, the references, the flag +/// or the bytes left change against the same case without it, so the +/// grid's clobbers are live. Bits 2 (`rendering_filter` flipped by the +/// filter's draw) and 7 (the flag flipped by the draws) leave the C as it +/// was, since it stores both after the draw; they show only a core that +/// skips or reads back those stores. +#[test] +fn every_clobber_changes_the_c() { + let live = |draw: Draw, s: Spec, clobber: u32| { + let c = entry(draw, Side::C); + let a = run_on(&s, 0, c); + let mut t = s; + t.clobber = clobber; + let b = run_on(&t, 1, c); + let same = unsafe { oracle_vrd_same() }; + unsafe { + oracle_vrd_free(0); + oracle_vrd_free(1); + } + a != b || !same + }; + let mut parent = spec(); + parent.filters = 0; + let mut srgb_default = spec(); + srgb_default.filters = 0; + srgb_default.flags |= FLAG_SRGB; + for (draw, s, clobber) in [ + (Draw::Filters, spec(), 1), + (Draw::Filters, spec(), 2), + (Draw::Main, spec(), 8), + (Draw::Main, parent, 16), + (Draw::Main, parent, 32), + (Draw::Main, spec(), 64), + (Draw::Main, srgb_default, 256), + (Draw::Main, spec(), 512), + ] { + assert!(live(draw, s, clobber), "{draw:?} {clobber}"); + } + for (draw, clobber) in [(Draw::Filters, 4), (Draw::Main, 128)] { + assert!(!live(draw, spec(), clobber), "{draw:?} {clobber}"); + } +} + +#[test] +fn every_mutant_is_caught() { + for m in MUTANTS { + let mut caught = false; + for_each_case(|_, draw, s| { + if !matches(s, draw, Side::Mutant(m)) { + caught = true; + return false; + } + true + }); + assert!(caught, "{m:?} survives"); + } +} + +fn arb_spec() -> impl Strategy { + ( + any::(), + 0..4_u32, + any::<[bool; 4]>(), + any::<[bool; NODES]>(), + any::<[bool; 2]>(), + any::(), + 0..1024_u32, + ) + .prop_map(|(flags, filters, b, dying, g, noise, clobber)| Spec { + flags, + filters, + filter_parent: b[0], + null_first: b[1], + has_data: b[2], + rendering_filter: b[3], + dying, + linear_srgb: g[0], + effect: g[1], + noise: u32::from(noise), + clobber, + }) +} + +#[test] +fn matches_c_on_random_states() { + let mut runner = proptest::test_runner::TestRunner::new(ProptestConfig { + cases: obs_c_oracle::parity::cases(1024), + failure_persistence: None, + ..ProptestConfig::default() + }); + runner + .run(&(arb_spec(), any::()), |(s, filters)| { + let draw = if filters && s.filters != 0 { + Draw::Filters + } else { + Draw::Main + }; + let (c, r, same) = compare(&s, draw, Side::Rust); + prop_assert_eq!(c, r); + prop_assert!(same); + Ok(()) + }) + .unwrap(); +}