From f7aad870d24b40470f7b6d0d1d098d740696b83c Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 13:29:04 -0800 Subject: [PATCH 1/2] test: pin scene-item order positions before extraction (Refs #5284) --- test/cmocka/build/test-graph.json | 30 ++++ test/cmocka/test_sceneitem_order_position.c | 165 ++++++++++++++++++ .../test_sceneitem_order_position.cmake | 1 + 3 files changed, 196 insertions(+) create mode 100644 test/cmocka/test_sceneitem_order_position.c create mode 100644 test/cmocka/tests.d/test_sceneitem_order_position.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 8d3d9c638a..d1b27586ab 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -15257,6 +15257,28 @@ "libobs/obs-scene-item-matrix-getters.c.h" ] }, + "test_sceneitem_order_position": { + "sources": [ + "test/cmocka/test_sceneitem_order_position.c" + ], + "includes": [ + "libobs", + "deps/libcaption" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "watch_inputs": [ + "libobs/obs-scene-item-order-position.c.h" + ] + }, "test_sceneitem_private_settings": { "sources": [ "test/cmocka/test_sceneitem_private_settings.c" @@ -27368,6 +27390,14 @@ "suite": "core", "timeout_seconds": 30 }, + "test_sceneitem_order_position": { + "target": "test_sceneitem_order_position", + "command": [ + "${target}" + ], + "suite": "core", + "timeout_seconds": 30 + }, "test_sceneitem_private_settings": { "target": "test_sceneitem_private_settings", "command": [ diff --git a/test/cmocka/test_sceneitem_order_position.c b/test/cmocka/test_sceneitem_order_position.c new file mode 100644 index 0000000000..57b9d33a9b --- /dev/null +++ b/test/cmocka/test_sceneitem_order_position.c @@ -0,0 +1,165 @@ +/* Characterize the public C entry point without OBS startup or worker threads. + * Stack-owned scenes/items use only their real parent list and scene mutexes. */ +#include +#include +#include +#include +#include "libobs/build/libobs.pch.h" + +/* #2899 intentionally retains the permissive legacy attached-item predicate + * in libobs-c-reference. NULL/detached calls there are undefined. Probe the + * exact getter body with the current hybrid predicate contract instead; valid + * list assertions below also exercise the real public shared-library symbol. */ +static bool position_attached(bool has_item, bool has_parent); +static void position_lock(obs_scene_t *scene); +static void position_unlock(obs_scene_t *scene); +#define obs_sceneitem_get_order_position position_body +#define obs_scene_item_order_item_attached position_attached +#define obs_scene_atomic_full_lock position_lock +#define obs_scene_atomic_full_unlock position_unlock +#include "libobs/obs-scene-item-order-position.c.h" +#undef obs_scene_atomic_full_unlock +#undef obs_scene_atomic_full_lock +#undef obs_scene_item_order_item_attached +#undef obs_sceneitem_get_order_position + +static unsigned lock_calls, unlock_calls; + +static bool position_attached(bool has_item, bool has_parent) +{ + return has_item && has_parent; +} + +static void position_lock(obs_scene_t *scene) +{ + lock_calls++; + assert_int_equal(pthread_mutex_lock(&scene->video_mutex), 0); + assert_int_equal(pthread_mutex_lock(&scene->audio_mutex), 0); +} + +static void position_unlock(obs_scene_t *scene) +{ + unlock_calls++; + assert_int_equal(pthread_mutex_unlock(&scene->audio_mutex), 0); + assert_int_equal(pthread_mutex_unlock(&scene->video_mutex), 0); +} + +static void assert_position(obs_sceneitem_t *item, int expected) +{ + lock_calls = unlock_calls = 0; + assert_int_equal(position_body(item), expected); + assert_int_equal(lock_calls, 1); + assert_int_equal(unlock_calls, 1); + assert_int_equal(obs_sceneitem_get_order_position(item), expected); +} + +static void init_scene(struct obs_scene *scene) +{ + assert_int_equal(pthread_mutex_init(&scene->video_mutex, NULL), 0); + assert_int_equal(pthread_mutex_init(&scene->audio_mutex, NULL), 0); +} + +static void free_scene(struct obs_scene *scene) +{ + assert_int_equal(pthread_mutex_destroy(&scene->audio_mutex), 0); + assert_int_equal(pthread_mutex_destroy(&scene->video_mutex), 0); +} + +static void link_items(struct obs_scene *scene, struct obs_scene_item *items, size_t count) +{ + scene->first_item = count ? items : NULL; + for (size_t i = 0; i < count; i++) { + items[i].parent = scene; + items[i].prev = i ? &items[i - 1] : NULL; + items[i].next = i + 1 < count ? &items[i + 1] : NULL; + } +} + +static void null_and_detached_return_zero(void **state) +{ + (void)state; + struct obs_scene_item detached = {0}; + lock_calls = unlock_calls = 0; + assert_int_equal(position_body(NULL), 0); + assert_int_equal(position_body(&detached), 0); + assert_int_equal(lock_calls, 0); + assert_int_equal(unlock_calls, 0); +} + +static void positions_count_siblings(void **state) +{ + (void)state; + struct obs_scene scene = {0}; + struct obs_scene_item items[6] = {0}; + init_scene(&scene); + for (size_t count = 1; count <= 6; count++) { + link_items(&scene, items, count); + for (size_t i = 0; i < count; i++) { + /* IDs and visibility do not affect position. */ + items[i].id = 99 - (int64_t)i; + items[i].user_visible = i % 2 == 0; + assert_position(&items[i], i); + } + } + /* Re-link the same handles in a different order. */ + scene.first_item = &items[5]; + for (size_t i = 0; i < 6; i++) { + items[i].next = i ? &items[i - 1] : NULL; + items[i].prev = i < 5 ? &items[i + 1] : NULL; + } + for (size_t i = 0; i < 6; i++) + assert_position(&items[5 - i], i); + free_scene(&scene); +} + +static void nested_groups_count_only_the_parent_list(void **state) +{ + (void)state; + struct obs_scene root = {0}, group = {0}, nested = {0}; + struct obs_scene_item roots[3] = {0}, children[4] = {0}, grandchildren[2] = {0}; + init_scene(&root); + init_scene(&group); + init_scene(&nested); + link_items(&root, roots, 3); + link_items(&group, children, 4); + link_items(&nested, grandchildren, 2); + roots[1].is_group = true; + children[2].is_group = true; + group.is_group = nested.is_group = true; + /* A group occupies one sibling slot; its descendants never inflate it. + * Each child's parent is the group's own scene, including nested groups. */ + for (size_t i = 0; i < 3; i++) + assert_position(&roots[i], i); + for (size_t i = 0; i < 4; i++) + assert_position(&children[i], i); + for (size_t i = 0; i < 2; i++) + assert_position(&grandchildren[i], i); + free_scene(&nested); + free_scene(&group); + free_scene(&root); +} + +static void absent_item_returns_list_length(void **state) +{ + (void)state; + struct obs_scene scene = {0}; + struct obs_scene_item items[3] = {0}, absent = {0}; + init_scene(&scene); + absent.parent = &scene; + /* Inconsistent membership is characterized, not endorsed: count until + * NULL when the parent list does not contain the requested item. */ + for (size_t count = 0; count <= 3; count++) { + link_items(&scene, items, count); + assert_position(&absent, count); + } + free_scene(&scene); +} + +int main(void) +{ + const struct CMUnitTest tests[] = {cmocka_unit_test(null_and_detached_return_zero), + cmocka_unit_test(positions_count_siblings), + cmocka_unit_test(nested_groups_count_only_the_parent_list), + cmocka_unit_test(absent_item_returns_list_length)}; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_sceneitem_order_position.cmake b/test/cmocka/tests.d/test_sceneitem_order_position.cmake new file mode 100644 index 0000000000..b420a62916 --- /dev/null +++ b/test/cmocka/tests.d/test_sceneitem_order_position.cmake @@ -0,0 +1 @@ +# Registered once in build/test-graph.json. From 06cb0d7f603d74b4b4eb01b5e6c2bb80a440aad5 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 13:49:28 -0800 Subject: [PATCH 2/2] libobs: extract scene-item order position verbatim (Refs #5284) Ci-Attestation: {"at":1795126859,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":673,"stamp":"d622e44afa15dabd0b4296b87b1e5ce3","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126859,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":673,"stamp":"237e11a549ccd9fae337ea13383f0312","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126859,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":673,"stamp":"69b7219fc330a424f4c7ce6ddc7b8476","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126859,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":673,"stamp":"42638a62d8e48f7143149fa55400147c","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126171,"gate":"general/all/client-lint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"620a954bae1b582da575b47f4f3b21d9","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126859,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":673,"stamp":"62caee4f052a2adbdc9cc28e4f35cf89","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126176,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"3ba2524ec358096e60640cca93f901af","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126176,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"780111b42dd53c8d37baf034d796ba21","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126174,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"93629964945fe2cde85de222b3174585","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126174,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"f43672d5ed11d5eb457d01617d31c20f","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126172,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"06d6ff0d8c74e4ac66f6ef24f69270fa","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126172,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"b687d7c4fe1cd3e2309e31840334ce37","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126175,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"fe59d24d14461e2977ff0bff063e58d9","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126175,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"e0bfd5419886f92e5d70379434df6532","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126173,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"a190269208cd9d65b873c262d7ed193b","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126173,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"9813ce13a9b78d6b85755591e767866c","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126172,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"81a9537911232f3ccd0a9a0254f31b09","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} Ci-Attestation: {"at":1795126172,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"97399461d404f95949d240f659a11cc670f1c8646f4d08c52327b8dc39055ac8","lane":"client-lint","parents":["f7aad870d24b40470f7b6d0d1d098d740696b83c"],"secs":183,"stamp":"3486dcfe718332c6e486788ad9bc2427","tree":"2c90cbb252aea43da69861dae3dcf7c4eb1f178d","v":1,"via":"run"} --- libobs/build/libobs.core-source+.h.c | 1 + libobs/obs-scene-item-order-position.c.h | 32 ++++++++++++++++++++++++ libobs/obs-scene.c.h | 21 ---------------- 3 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 libobs/obs-scene-item-order-position.c.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index d43b159069..d2d32bb004 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -36,6 +36,7 @@ #include "libobs/obs-scene-item-hotkeys-fields.c.h" #include "libobs/obs-scene-item-locked-fields.c.h" #include "libobs/obs-scene-item-matrix-getters.c.h" +#include "libobs/obs-scene-item-order-position.c.h" #include "libobs/obs-scene-item-parent-fields.c.h" #include "libobs/obs-scene-item-select-fields.c.h" #include "libobs/obs-scene-item-set-visibility-fields.c.h" diff --git a/libobs/obs-scene-item-order-position.c.h b/libobs/obs-scene-item-order-position.c.h new file mode 100644 index 0000000000..cc856dd9b1 --- /dev/null +++ b/libobs/obs-scene-item-order-position.c.h @@ -0,0 +1,32 @@ +#include "libobs/build/libobs.pch.h" + +/* Reuse the existing full scene lock boundary without changing the body. */ +void obs_scene_atomic_full_lock(obs_scene_t *scene); +void obs_scene_atomic_full_unlock(obs_scene_t *scene); + +#define full_lock obs_scene_atomic_full_lock +#define full_unlock obs_scene_atomic_full_unlock + +int obs_sceneitem_get_order_position(obs_sceneitem_t *item) +{ + if (!obs_scene_item_order_item_attached(item != NULL, item && item->parent)) + return 0; + + struct obs_scene *scene = item->parent; + struct obs_scene_item *next = scene->first_item; + + full_lock(scene); + + int index = 0; + while (next && next != item) { + next = next->next; + ++index; + } + + full_unlock(scene); + + return index; +} + +#undef full_unlock +#undef full_lock diff --git a/libobs/obs-scene.c.h b/libobs/obs-scene.c.h index 90ffe23923..7083461862 100644 --- a/libobs/obs-scene.c.h +++ b/libobs/obs-scene.c.h @@ -2274,27 +2274,6 @@ void obs_sceneitem_set_order(obs_sceneitem_t *item, enum obs_order_movement move obs_scene_release(scene); } -int obs_sceneitem_get_order_position(obs_sceneitem_t *item) -{ - if (!obs_scene_item_order_item_attached(item != NULL, item && item->parent)) - return 0; - - struct obs_scene *scene = item->parent; - struct obs_scene_item *next = scene->first_item; - - full_lock(scene); - - int index = 0; - while (next && next != item) { - next = next->next; - ++index; - } - - full_unlock(scene); - - return index; -} - void obs_sceneitem_set_order_position(obs_sceneitem_t *item, int position) { if (!item)