diff --git a/libobs/build/libobs.graphics+.h.c b/libobs/build/libobs.graphics+.h.c index 8c40f13883..852403d661 100644 --- a/libobs/build/libobs.graphics+.h.c +++ b/libobs/build/libobs.graphics+.h.c @@ -9,6 +9,7 @@ #include "libobs/graphics/graphics-device-queries-fields.c.h" #include "libobs/graphics/graphics-imports.c.h" #include "libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h" +#include "libobs/graphics/graphics-planar-capability-queries.c.h" #include "libobs/graphics/graphics-render-target-setters-fields.c.h" #include "libobs/graphics/graphics-samplerstate-lifecycle-fields.c.h" #include "libobs/graphics/graphics-shader-matrix-queries-fields.c.h" diff --git a/libobs/graphics/graphics-planar-capability-queries.c.h b/libobs/graphics/graphics-planar-capability-queries.c.h new file mode 100644 index 0000000000..239aa04951 --- /dev/null +++ b/libobs/graphics/graphics-planar-capability-queries.c.h @@ -0,0 +1,27 @@ +#include "libobs/build/libobs-graphics.pch.h" + +/* Private dependencies defined in graphics.c.h, later in the unity group. */ +static THREAD_LOCAL graphics_t *thread_graphics; +static inline bool gs_valid(const char *f); + +bool gs_nv12_available(void) +{ + if (!gs_valid("gs_nv12_available")) + return false; + + if (!thread_graphics->exports.device_nv12_available) + return false; + + return thread_graphics->exports.device_nv12_available(thread_graphics->device); +} + +bool gs_p010_available(void) +{ + if (!gs_valid("gs_p010_available")) + return false; + + if (!thread_graphics->exports.device_p010_available) + return false; + + return thread_graphics->exports.device_p010_available(thread_graphics->device); +} diff --git a/libobs/graphics/graphics.c.h b/libobs/graphics/graphics.c.h index bc9e72434b..9e18b18404 100644 --- a/libobs/graphics/graphics.c.h +++ b/libobs/graphics/graphics.c.h @@ -1317,28 +1317,6 @@ enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer) return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer); } -bool gs_nv12_available(void) -{ - if (!gs_valid("gs_nv12_available")) - return false; - - if (!thread_graphics->exports.device_nv12_available) - return false; - - return thread_graphics->exports.device_nv12_available(thread_graphics->device); -} - -bool gs_p010_available(void) -{ - if (!gs_valid("gs_p010_available")) - return false; - - if (!thread_graphics->exports.device_p010_available) - return false; - - return thread_graphics->exports.device_p010_available(thread_graphics->device); -} - bool gs_is_monitor_hdr(void *monitor) { if (!gs_valid("gs_is_monitor_hdr")) diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 2028cfc8f8..be17f55591 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -5226,6 +5226,22 @@ } ] }, + "test_graphics_planar_capability_queries": { + "sources": [ + "test/cmocka/test_graphics_planar_capability_queries.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "header_libraries": [ + "SIMDe::SIMDe" + ] + }, "test_graphics_render_target_setters": { "sources": [ "test/cmocka/test_graphics_render_target_setters.c" @@ -23332,6 +23348,12 @@ "${target}" ] }, + "test_graphics_planar_capability_queries": { + "target": "test_graphics_planar_capability_queries", + "command": [ + "${target}" + ] + }, "test_graphics_render_target_setters": { "target": "test_graphics_render_target_setters", "command": [ diff --git a/test/cmocka/test_graphics_planar_capability_queries.c b/test/cmocka/test_graphics_planar_capability_queries.c new file mode 100644 index 0000000000..51aacc538f --- /dev/null +++ b/test/cmocka/test_graphics_planar_capability_queries.c @@ -0,0 +1,162 @@ +/* Characterize optional planar capability callbacks without a GPU. */ +#include +#include +#include +#include +#include +#include +#include "graphics/graphics-internal.h" +#include "util/base.h" + +/* This characterization lands with the extraction, before the Rust swap. */ +#if !__has_include("graphics/graphics-planar-capability-queries.c.h") +#error "graphics planar capability query extraction seam is missing" +#endif + +static struct graphics_subsystem graphics; +static int device_tokens[2]; +static gs_device_t *expected_device; +static bool nv12_result, p010_result; +static unsigned nv12_calls, p010_calls, log_count; +static char message[160]; +static log_handler_t previous_handler; +static void *previous_param; + +#ifdef __GNUC__ +__attribute__((format(printf, 2, 0))) +#endif +static void capture(int level, const char *format, va_list args, void *param) +{ + (void)param; + assert_int_equal(level, LOG_DEBUG); + log_count++; + vsnprintf(message, sizeof(message), format, args); +} + +static void context(gs_device_t *device) +{ + assert_ptr_equal(device, expected_device); +} + +static bool nv12(gs_device_t *device) +{ + assert_ptr_equal(gs_get_context(), &graphics); + assert_ptr_equal(device, expected_device); + nv12_calls++; + return nv12_result; +} + +static bool p010(gs_device_t *device) +{ + assert_ptr_equal(gs_get_context(), &graphics); + assert_ptr_equal(device, expected_device); + p010_calls++; + return p010_result; +} + +static int setup(void **state) +{ + (void)state; + assert_null(gs_get_context()); + memset(&graphics, 0, sizeof(graphics)); + nv12_calls = p010_calls = log_count = 0; + nv12_result = p010_result = false; + message[0] = '\0'; + expected_device = (gs_device_t *)&device_tokens[0]; + graphics.device = expected_device; + graphics.exports.device_enter_context = context; + graphics.exports.device_leave_context = context; + graphics.exports.device_nv12_available = nv12; + graphics.exports.device_p010_available = p010; + assert_int_equal(pthread_mutex_init(&graphics.mutex, NULL), 0); + base_get_log_handler(&previous_handler, &previous_param); + base_set_log_handler(capture, NULL); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + if (gs_get_context()) + gs_leave_context(); + base_set_log_handler(previous_handler, previous_param); + assert_int_equal(pthread_mutex_destroy(&graphics.mutex), 0); + return 0; +} + +static void no_context(void **state) +{ + (void)state; + /* Context rejection precedes optional callback inspection. */ + for (unsigned i = 0; i < 2; i++) { + assert_false(gs_nv12_available()); + assert_string_equal(message, "gs_nv12_available: called while not in a graphics context"); + assert_int_equal(log_count, 2 * i + 1); + assert_false(gs_p010_available()); + assert_string_equal(message, "gs_p010_available: called while not in a graphics context"); + assert_int_equal(log_count, 2 * i + 2); + assert_int_equal(nv12_calls, 0); + assert_int_equal(p010_calls, 0); + graphics.exports.device_nv12_available = NULL; + graphics.exports.device_p010_available = NULL; + } +} + +static void optional_callbacks_are_independent(void **state) +{ + (void)state; + gs_enter_context(&graphics); + nv12_result = p010_result = true; + graphics.exports.device_nv12_available = NULL; + assert_false(gs_nv12_available()); + assert_true(gs_p010_available()); + assert_int_equal(nv12_calls, 0); + assert_int_equal(p010_calls, 1); + graphics.exports.device_nv12_available = nv12; + graphics.exports.device_p010_available = NULL; + assert_false(gs_p010_available()); + assert_true(gs_nv12_available()); + assert_int_equal(nv12_calls, 1); + assert_int_equal(p010_calls, 1); + graphics.exports.device_nv12_available = NULL; + assert_false(gs_nv12_available()); + assert_false(gs_p010_available()); + assert_int_equal(nv12_calls, 1); + assert_int_equal(p010_calls, 1); + assert_int_equal(log_count, 0); +} + +static void callback_results_and_device_routing(void **state) +{ + (void)state; + /* Every device and result here is fake; host capabilities are irrelevant. */ + for (unsigned device = 0; device < 2; device++) { + expected_device = (gs_device_t *)&device_tokens[device]; + graphics.device = expected_device; + gs_enter_context(&graphics); + for (unsigned results = 0; results < 4; results++) { + nv12_result = (results & 1) != 0; + p010_result = (results & 2) != 0; + unsigned calls = device * 4 + results; + assert_int_equal(gs_nv12_available(), nv12_result); + assert_int_equal(nv12_calls, calls + 1); + assert_int_equal(p010_calls, calls); + assert_int_equal(gs_p010_available(), p010_result); + assert_int_equal(nv12_calls, calls + 1); + assert_int_equal(p010_calls, calls + 1); + assert_int_equal(log_count, 0); + } + gs_leave_context(); + assert_null(gs_get_context()); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(no_context, setup, teardown), + cmocka_unit_test_setup_teardown(optional_callbacks_are_independent, setup, teardown), + cmocka_unit_test_setup_teardown(callback_results_and_device_routing, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_graphics_planar_capability_queries.cmake b/test/cmocka/tests.d/test_graphics_planar_capability_queries.cmake new file mode 100644 index 0000000000..097d1362a9 --- /dev/null +++ b/test/cmocka/tests.d/test_graphics_planar_capability_queries.cmake @@ -0,0 +1 @@ +# Both target and test are declared in ../build/test-graph.json.