diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index 774f481b9a..f4f2984f44 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -31,6 +31,7 @@ #include "libobs/obs-scene-item-blending-method-fields.c.h" #include "libobs/obs-scene-item-blending-mode-fields.c.h" #include "libobs/obs-scene-item-bounds-setters-fields.c.h" +#include "libobs/obs-scene-item-get-group.c.h" #include "libobs/obs-scene-item-group-scene-fields.c.h" #include "libobs/obs-scene-item-hotkey-bridge.c.h" #include "libobs/obs-scene-item-hotkeys-fields.c.h" diff --git a/libobs/obs-scene-item-get-group.c.h b/libobs/obs-scene-item-get-group.c.h new file mode 100644 index 0000000000..0b05ca0553 --- /dev/null +++ b/libobs/obs-scene-item-get-group.c.h @@ -0,0 +1,26 @@ +#include "libobs/build/libobs.pch.h" + +/* Keep the original wrapper body; C owns the scene locks and shared walk. */ +void obs_scene_atomic_full_lock(obs_scene_t *scene); +void obs_scene_atomic_full_unlock(obs_scene_t *scene); +obs_sceneitem_t *obs_sceneitem_parent_group_lookup(obs_scene_t *scene, obs_sceneitem_t *group_subitem); + +#define full_lock obs_scene_atomic_full_lock +#define full_unlock obs_scene_atomic_full_unlock +#define get_sceneitem_parent_group obs_sceneitem_parent_group_lookup + +obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem) +{ + if (!scene || !group_subitem || group_subitem->is_group) + return NULL; + + full_lock(scene); + obs_sceneitem_t *group = get_sceneitem_parent_group(scene, group_subitem); + full_unlock(scene); + + return group; +} + +#undef get_sceneitem_parent_group +#undef full_unlock +#undef full_lock diff --git a/libobs/obs-scene.c.h b/libobs/obs-scene.c.h index 90ffe23923..3834427876 100644 --- a/libobs/obs-scene.c.h +++ b/libobs/obs-scene.c.h @@ -2960,6 +2960,14 @@ static obs_sceneitem_t *get_sceneitem_parent_group(obs_scene_t *scene, obs_scene return NULL; } +/* Private callable seam: the shared walk stays here for reorder callers. */ +obs_sceneitem_t *obs_sceneitem_parent_group_lookup(obs_scene_t *scene, obs_sceneitem_t *group_subitem); + +obs_sceneitem_t *obs_sceneitem_parent_group_lookup(obs_scene_t *scene, obs_sceneitem_t *group_subitem) +{ + return get_sceneitem_parent_group(scene, group_subitem); +} + static void obs_sceneitem_move_hotkeys(obs_scene_t *parent, obs_sceneitem_t *item) { obs_data_array_t *data0 = NULL; @@ -3081,18 +3089,6 @@ bool obs_scene_reorder_items2(obs_scene_t *scene, struct obs_sceneitem_order_inf return true; } -obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem) -{ - if (!scene || !group_subitem || group_subitem->is_group) - return NULL; - - full_lock(scene); - obs_sceneitem_t *group = get_sceneitem_parent_group(scene, group_subitem); - full_unlock(scene); - - return group; -} - void obs_sceneitem_force_update_transform(obs_sceneitem_t *item) { if (!item) diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index c9b5daa111..ee8e25826c 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -15172,6 +15172,21 @@ "libobs/obs-scene-item-alignment-setter.c.h" ] }, + "test_sceneitem_get_group": { + "sources": [ + "test/cmocka/test_sceneitem_get_group.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_sceneitem_matrix_getters": { "sources": [ "test/cmocka/test_sceneitem_matrix_getters.c" @@ -27221,6 +27236,12 @@ "suite": "core", "timeout_seconds": 30 }, + "test_sceneitem_get_group": { + "target": "test_sceneitem_get_group", + "command": [ + "${target}" + ] + }, "test_sceneitem_matrix_getters": { "target": "test_sceneitem_matrix_getters", "command": [ diff --git a/test/cmocka/test_sceneitem_get_group.c b/test/cmocka/test_sceneitem_get_group.c new file mode 100644 index 0000000000..f7a2081ed5 --- /dev/null +++ b/test/cmocka/test_sceneitem_get_group.c @@ -0,0 +1,179 @@ +/* Characterize wrapper locking and the real linked parent-group walk (#5276). + * Native layouts need no OBS startup, devices or worker threads. */ +#include +#include +#include +#include +#include +#include "libobs/build/libobs.pch.h" + +static struct obs_scene scenes[3]; +static struct obs_scene_item items[5], child; +static struct obs_source sources[3]; +static unsigned locks, lookups, unlocks; +static bool locked; +static obs_sceneitem_t *lookup_result; + +void sceneitem_group_record_lock(obs_scene_t *scene) +{ + assert_ptr_equal(scene, &scenes[0]); + assert_false(locked); + assert_int_equal(locks, lookups); + assert_int_equal(locks, unlocks); + locked = true; + locks++; +} + +obs_sceneitem_t *sceneitem_group_record_lookup(obs_scene_t *scene, obs_sceneitem_t *item) +{ + assert_ptr_equal(scene, &scenes[0]); + assert_ptr_equal(item, &child); + assert_true(locked); + assert_int_equal(locks, lookups + 1); + lookups++; + return lookup_result; +} + +void sceneitem_group_record_unlock(obs_scene_t *scene) +{ + assert_ptr_equal(scene, &scenes[0]); + assert_true(locked); + assert_int_equal(locks, lookups); + assert_int_equal(locks, unlocks + 1); + locked = false; + unlocks++; +} + +#ifndef SCENEITEM_GET_GROUP_EXTERNAL +#define obs_sceneitem_get_group sceneitem_group_recorded +#define obs_scene_atomic_full_lock sceneitem_group_record_lock +#define obs_scene_atomic_full_unlock sceneitem_group_record_unlock +#define obs_sceneitem_parent_group_lookup sceneitem_group_record_lookup +#include "libobs/obs-scene-item-get-group.c.h" +#undef obs_sceneitem_parent_group_lookup +#undef obs_scene_atomic_full_unlock +#undef obs_scene_atomic_full_lock +#undef obs_sceneitem_get_group +#else +obs_sceneitem_t *sceneitem_group_recorded(obs_scene_t *scene, obs_sceneitem_t *item); +#endif + +static int setup(void **state) +{ + (void)state; + memset(scenes, 0, sizeof(scenes)); + memset(items, 0, sizeof(items)); + memset(sources, 0, sizeof(sources)); + memset(&child, 0, sizeof(child)); + locks = lookups = unlocks = 0; + locked = false; + lookup_result = NULL; + assert_int_equal(pthread_mutex_init(&scenes[0].video_mutex, NULL), 0); + assert_int_equal(pthread_mutex_init(&scenes[0].audio_mutex, NULL), 0); + child.ref = 11; + child.parent = &scenes[1]; + for (size_t i = 0; i < 5; i++) { + items[i].ref = 17 + (long)i; + items[i].next = i + 1 < 5 ? &items[i + 1] : NULL; + } + return 0; +} + +static int teardown(void **state) +{ + (void)state; + assert_false(locked); + assert_int_equal(locks, lookups); + assert_int_equal(locks, unlocks); + assert_int_equal(pthread_mutex_destroy(&scenes[0].audio_mutex), 0); + assert_int_equal(pthread_mutex_destroy(&scenes[0].video_mutex), 0); + return 0; +} + +static void rejects_before_lock(void **state) +{ + (void)state; + assert_null(sceneitem_group_recorded(NULL, NULL)); + assert_null(sceneitem_group_recorded(NULL, &child)); + assert_null(sceneitem_group_recorded(&scenes[0], NULL)); + child.is_group = true; + assert_null(sceneitem_group_recorded(NULL, &child)); + assert_null(sceneitem_group_recorded(&scenes[0], &child)); + assert_int_equal(locks, 0); + assert_int_equal(lookups, 0); + assert_int_equal(unlocks, 0); + /* Real library must also reject before touching uninitialized locks. */ + assert_null(obs_sceneitem_get_group(NULL, NULL)); + assert_null(obs_sceneitem_get_group(NULL, &child)); + assert_null(obs_sceneitem_get_group(&scenes[2], NULL)); + assert_null(obs_sceneitem_get_group(&scenes[2], &child)); +} + +static void balanced_lookup_returns_borrowed_pointer(void **state) +{ + (void)state; + for (size_t i = 0; i < 6; i++) { + lookup_result = i < 5 ? &items[i] : NULL; + assert_ptr_equal(sceneitem_group_recorded(&scenes[0], &child), lookup_result); + assert_int_equal(locks, i + 1); + assert_int_equal(lookups, locks); + assert_int_equal(unlocks, locks); + assert_false(locked); + assert_int_equal(child.ref, 11); + for (size_t j = 0; j < 5; j++) + assert_int_equal(items[j].ref, 17 + j); + } +} + +static void linked_query(obs_sceneitem_t *expected) +{ + unsigned char before_items[sizeof(items)], before_sources[sizeof(sources)], before_child[sizeof(child)]; + memcpy(before_items, items, sizeof(items)); + memcpy(before_sources, sources, sizeof(sources)); + memcpy(before_child, &child, sizeof(child)); + obs_sceneitem_t *first = scenes[0].first_item; + for (unsigned repeat = 0; repeat < 2; repeat++) { + assert_ptr_equal(obs_sceneitem_get_group(&scenes[0], &child), expected); + assert_ptr_equal(scenes[0].first_item, first); + /* Includes every item/source reference count: result is borrowed. */ + assert_memory_equal(items, before_items, sizeof(items)); + assert_memory_equal(sources, before_sources, sizeof(sources)); + assert_memory_equal(&child, before_child, sizeof(child)); + } +} + +static void linked_first_matching_parent_group(void **state) +{ + (void)state; + linked_query(NULL); /* empty scene */ + scenes[0].first_item = &items[0]; + linked_query(NULL); /* non-groups need no source, even with matching parent */ + items[0].parent = child.parent; + items[1].is_group = items[2].is_group = items[3].is_group = true; + items[1].source = &sources[0]; + items[2].source = &sources[1]; + items[3].source = &sources[2]; + sources[0].context.data = &scenes[2]; + sources[1].context.data = sources[2].context.data = child.parent; + linked_query(&items[2]); /* skip ordinary item and foreign group */ + items[2].removed = true; + linked_query(&items[2]); /* characterized: removed flag is not consulted */ + sources[1].context.data = &scenes[2]; + linked_query(&items[3]); + child.parent = &scenes[0]; + linked_query(NULL); /* no matching group / foreign child */ + child.parent = NULL; + linked_query(NULL); + sources[0].context.data = NULL; + linked_query(&items[1]); /* characterized: NULL parent matches NULL context */ +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(rejects_before_lock, setup, teardown), + cmocka_unit_test_setup_teardown(balanced_lookup_returns_borrowed_pointer, setup, teardown), + cmocka_unit_test_setup_teardown(linked_first_matching_parent_group, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_sceneitem_get_group.cmake b/test/cmocka/tests.d/test_sceneitem_get_group.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_sceneitem_get_group.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).