obs-scene.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. static void resize_group(obs_sceneitem_t *group);
  19. static void signal_parent(obs_scene_t *parent, const char *name,
  20. calldata_t *params);
  21. static void get_ungrouped_transform(obs_sceneitem_t *group,
  22. struct vec2 *pos,
  23. struct vec2 *scale,
  24. float *rot);
  25. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  26. * lock the graphics mutex inside either of the scene mutexes.
  27. *
  28. * Another thing that must be done to prevent that cross-lock (and improve
  29. * performance), is to not create/release/update sources within the scene
  30. * mutexes.
  31. *
  32. * It's okay to lock the graphics mutex before locking either of the scene
  33. * mutexes, but not after.
  34. */
  35. static const char *obs_scene_signals[] = {
  36. "void item_add(ptr scene, ptr item)",
  37. "void item_remove(ptr scene, ptr item)",
  38. "void reorder(ptr scene)",
  39. "void item_visible(ptr scene, ptr item, bool visible)",
  40. "void item_select(ptr scene, ptr item)",
  41. "void item_deselect(ptr scene, ptr item)",
  42. "void item_transform(ptr scene, ptr item)",
  43. NULL
  44. };
  45. static inline void signal_item_remove(struct obs_scene_item *item)
  46. {
  47. struct calldata params;
  48. uint8_t stack[128];
  49. calldata_init_fixed(&params, stack, sizeof(stack));
  50. calldata_set_ptr(&params, "item", item);
  51. signal_parent(item->parent, "item_remove", &params);
  52. }
  53. static const char *scene_getname(void *unused)
  54. {
  55. UNUSED_PARAMETER(unused);
  56. return "Scene";
  57. }
  58. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  59. {
  60. pthread_mutexattr_t attr;
  61. struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
  62. scene->source = source;
  63. signal_handler_add_array(obs_source_get_signal_handler(source),
  64. obs_scene_signals);
  65. if (pthread_mutexattr_init(&attr) != 0)
  66. goto fail;
  67. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  68. goto fail;
  69. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  70. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  71. "mutex");
  72. goto fail;
  73. }
  74. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  75. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  76. "mutex");
  77. goto fail;
  78. }
  79. UNUSED_PARAMETER(settings);
  80. return scene;
  81. fail:
  82. pthread_mutexattr_destroy(&attr);
  83. bfree(scene);
  84. return NULL;
  85. }
  86. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  87. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  88. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  89. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  90. static inline void full_lock(struct obs_scene *scene)
  91. {
  92. video_lock(scene);
  93. audio_lock(scene);
  94. }
  95. static inline void full_unlock(struct obs_scene *scene)
  96. {
  97. audio_unlock(scene);
  98. video_unlock(scene);
  99. }
  100. static void set_visibility(struct obs_scene_item *item, bool vis);
  101. static inline void detach_sceneitem(struct obs_scene_item *item);
  102. static inline void remove_without_release(struct obs_scene_item *item)
  103. {
  104. item->removed = true;
  105. set_visibility(item, false);
  106. signal_item_remove(item);
  107. detach_sceneitem(item);
  108. }
  109. static void remove_all_items(struct obs_scene *scene)
  110. {
  111. struct obs_scene_item *item;
  112. DARRAY(struct obs_scene_item*) items;
  113. da_init(items);
  114. full_lock(scene);
  115. item = scene->first_item;
  116. while (item) {
  117. struct obs_scene_item *del_item = item;
  118. item = item->next;
  119. remove_without_release(del_item);
  120. da_push_back(items, &del_item);
  121. }
  122. full_unlock(scene);
  123. for (size_t i = 0; i < items.num; i++)
  124. obs_sceneitem_release(items.array[i]);
  125. da_free(items);
  126. }
  127. static void scene_destroy(void *data)
  128. {
  129. struct obs_scene *scene = data;
  130. remove_all_items(scene);
  131. pthread_mutex_destroy(&scene->video_mutex);
  132. pthread_mutex_destroy(&scene->audio_mutex);
  133. bfree(scene);
  134. }
  135. static void scene_enum_sources(void *data,
  136. obs_source_enum_proc_t enum_callback,
  137. void *param, bool active)
  138. {
  139. struct obs_scene *scene = data;
  140. struct obs_scene_item *item;
  141. struct obs_scene_item *next;
  142. full_lock(scene);
  143. item = scene->first_item;
  144. while (item) {
  145. next = item->next;
  146. obs_sceneitem_addref(item);
  147. if (!active || os_atomic_load_long(&item->active_refs) > 0)
  148. enum_callback(scene->source, item->source, param);
  149. obs_sceneitem_release(item);
  150. item = next;
  151. }
  152. full_unlock(scene);
  153. }
  154. static void scene_enum_active_sources(void *data,
  155. obs_source_enum_proc_t enum_callback,
  156. void *param)
  157. {
  158. scene_enum_sources(data, enum_callback, param, true);
  159. }
  160. static void scene_enum_all_sources(void *data,
  161. obs_source_enum_proc_t enum_callback,
  162. void *param)
  163. {
  164. scene_enum_sources(data, enum_callback, param, false);
  165. }
  166. static inline void detach_sceneitem(struct obs_scene_item *item)
  167. {
  168. if (item->prev)
  169. item->prev->next = item->next;
  170. else
  171. item->parent->first_item = item->next;
  172. if (item->next)
  173. item->next->prev = item->prev;
  174. item->parent = NULL;
  175. }
  176. static inline void attach_sceneitem(struct obs_scene *parent,
  177. struct obs_scene_item *item, struct obs_scene_item *prev)
  178. {
  179. item->prev = prev;
  180. item->parent = parent;
  181. if (prev) {
  182. item->next = prev->next;
  183. if (prev->next)
  184. prev->next->prev = item;
  185. prev->next = item;
  186. } else {
  187. item->next = parent->first_item;
  188. if (parent->first_item)
  189. parent->first_item->prev = item;
  190. parent->first_item = item;
  191. }
  192. }
  193. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  194. {
  195. if (align & OBS_ALIGN_RIGHT)
  196. v->x += (float)cx;
  197. else if ((align & OBS_ALIGN_LEFT) == 0)
  198. v->x += (float)(cx / 2);
  199. if (align & OBS_ALIGN_BOTTOM)
  200. v->y += (float)cy;
  201. else if ((align & OBS_ALIGN_TOP) == 0)
  202. v->y += (float)(cy / 2);
  203. }
  204. static void calculate_bounds_data(struct obs_scene_item *item,
  205. struct vec2 *origin, struct vec2 *scale,
  206. uint32_t *cx, uint32_t *cy)
  207. {
  208. float width = (float)(*cx) * fabsf(scale->x);
  209. float height = (float)(*cy) * fabsf(scale->y);
  210. float item_aspect = width / height;
  211. float bounds_aspect = item->bounds.x / item->bounds.y;
  212. uint32_t bounds_type = item->bounds_type;
  213. float width_diff, height_diff;
  214. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  215. if (width > item->bounds.x || height > item->bounds.y)
  216. bounds_type = OBS_BOUNDS_SCALE_INNER;
  217. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  218. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  219. bool use_width = (bounds_aspect < item_aspect);
  220. float mul;
  221. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  222. use_width = !use_width;
  223. mul = use_width ?
  224. item->bounds.x / width :
  225. item->bounds.y / height;
  226. vec2_mulf(scale, scale, mul);
  227. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  228. vec2_mulf(scale, scale, item->bounds.x / width);
  229. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  230. vec2_mulf(scale, scale, item->bounds.y / height);
  231. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  232. scale->x = item->bounds.x / (float)(*cx);
  233. scale->y = item->bounds.y / (float)(*cy);
  234. }
  235. width = (float)(*cx) * scale->x;
  236. height = (float)(*cy) * scale->y;
  237. width_diff = item->bounds.x - width;
  238. height_diff = item->bounds.y - height;
  239. *cx = (uint32_t)item->bounds.x;
  240. *cy = (uint32_t)item->bounds.y;
  241. add_alignment(origin, item->bounds_align,
  242. (int)-width_diff, (int)-height_diff);
  243. }
  244. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  245. uint32_t width)
  246. {
  247. uint32_t crop_cx = item->crop.left + item->crop.right;
  248. return (crop_cx > width) ? 2 : (width - crop_cx);
  249. }
  250. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  251. uint32_t height)
  252. {
  253. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  254. return (crop_cy > height) ? 2 : (height - crop_cy);
  255. }
  256. static void update_item_transform(struct obs_scene_item *item)
  257. {
  258. uint32_t width = obs_source_get_width(item->source);
  259. uint32_t height = obs_source_get_height(item->source);
  260. uint32_t cx = calc_cx(item, width);
  261. uint32_t cy = calc_cy(item, height);
  262. struct vec2 base_origin;
  263. struct vec2 origin;
  264. struct vec2 scale = item->scale;
  265. struct calldata params;
  266. uint8_t stack[128];
  267. if (os_atomic_load_long(&item->defer_update) > 0)
  268. return;
  269. item->last_width = width;
  270. item->last_height = height;
  271. width = cx;
  272. height = cy;
  273. vec2_zero(&base_origin);
  274. vec2_zero(&origin);
  275. /* ----------------------- */
  276. if (item->bounds_type != OBS_BOUNDS_NONE) {
  277. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  278. } else {
  279. cx = (uint32_t)((float)cx * scale.x);
  280. cy = (uint32_t)((float)cy * scale.y);
  281. }
  282. add_alignment(&origin, item->align, (int)cx, (int)cy);
  283. matrix4_identity(&item->draw_transform);
  284. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  285. scale.x, scale.y, 1.0f);
  286. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  287. -origin.x, -origin.y, 0.0f);
  288. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  289. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  290. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  291. item->pos.x, item->pos.y, 0.0f);
  292. item->output_scale = scale;
  293. /* ----------------------- */
  294. if (item->bounds_type != OBS_BOUNDS_NONE) {
  295. vec2_copy(&scale, &item->bounds);
  296. } else {
  297. scale.x = (float)width * item->scale.x;
  298. scale.y = (float)height * item->scale.y;
  299. }
  300. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  301. matrix4_identity(&item->box_transform);
  302. matrix4_scale3f(&item->box_transform, &item->box_transform,
  303. scale.x, scale.y, 1.0f);
  304. matrix4_translate3f(&item->box_transform, &item->box_transform,
  305. -base_origin.x, -base_origin.y, 0.0f);
  306. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  307. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  308. matrix4_translate3f(&item->box_transform, &item->box_transform,
  309. item->pos.x, item->pos.y, 0.0f);
  310. /* ----------------------- */
  311. calldata_init_fixed(&params, stack, sizeof(stack));
  312. calldata_set_ptr(&params, "item", item);
  313. signal_parent(item->parent, "item_transform", &params);
  314. os_atomic_set_bool(&item->update_transform, false);
  315. }
  316. static inline bool source_size_changed(struct obs_scene_item *item)
  317. {
  318. uint32_t width = obs_source_get_width(item->source);
  319. uint32_t height = obs_source_get_height(item->source);
  320. return item->last_width != width || item->last_height != height;
  321. }
  322. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  323. {
  324. return crop->left || crop->right || crop->top || crop->bottom;
  325. }
  326. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  327. {
  328. return item->scale_filter != OBS_SCALE_DISABLE;
  329. }
  330. static inline bool item_is_scene(const struct obs_scene_item *item)
  331. {
  332. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  333. }
  334. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  335. {
  336. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  337. (item_is_scene(item) && !item->is_group);
  338. }
  339. static void render_item_texture(struct obs_scene_item *item)
  340. {
  341. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  342. gs_effect_t *effect = obs->video.default_effect;
  343. enum obs_scale_type type = item->scale_filter;
  344. uint32_t cx = gs_texture_get_width(tex);
  345. uint32_t cy = gs_texture_get_height(tex);
  346. if (type != OBS_SCALE_DISABLE) {
  347. if (type == OBS_SCALE_POINT) {
  348. gs_eparam_t *image = gs_effect_get_param_by_name(
  349. effect, "image");
  350. gs_effect_set_next_sampler(image,
  351. obs->video.point_sampler);
  352. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  353. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  354. gs_eparam_t *scale_param;
  355. if (item->output_scale.x < 0.5f ||
  356. item->output_scale.y < 0.5f) {
  357. effect = obs->video.bilinear_lowres_effect;
  358. } else if (type == OBS_SCALE_BICUBIC) {
  359. effect = obs->video.bicubic_effect;
  360. } else if (type == OBS_SCALE_LANCZOS) {
  361. effect = obs->video.lanczos_effect;
  362. }
  363. scale_param = gs_effect_get_param_by_name(effect,
  364. "base_dimension_i");
  365. if (scale_param) {
  366. struct vec2 base_res_i = {
  367. 1.0f / (float)cx,
  368. 1.0f / (float)cy
  369. };
  370. gs_effect_set_vec2(scale_param, &base_res_i);
  371. }
  372. }
  373. }
  374. while (gs_effect_loop(effect, "Draw"))
  375. obs_source_draw(tex, 0, 0, 0, 0, 0);
  376. }
  377. static inline void render_item(struct obs_scene_item *item)
  378. {
  379. if (item->item_render) {
  380. uint32_t width = obs_source_get_width(item->source);
  381. uint32_t height = obs_source_get_height(item->source);
  382. if (!width || !height)
  383. return;
  384. uint32_t cx = calc_cx(item, width);
  385. uint32_t cy = calc_cy(item, height);
  386. if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
  387. float cx_scale = (float)width / (float)cx;
  388. float cy_scale = (float)height / (float)cy;
  389. struct vec4 clear_color;
  390. vec4_zero(&clear_color);
  391. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  392. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  393. -100.0f, 100.0f);
  394. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  395. gs_matrix_translate3f(
  396. -(float)item->crop.left,
  397. -(float)item->crop.top,
  398. 0.0f);
  399. gs_blend_state_push();
  400. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  401. obs_source_video_render(item->source);
  402. gs_blend_state_pop();
  403. gs_texrender_end(item->item_render);
  404. }
  405. }
  406. gs_matrix_push();
  407. gs_matrix_mul(&item->draw_transform);
  408. if (item->item_render) {
  409. render_item_texture(item);
  410. } else {
  411. obs_source_video_render(item->source);
  412. }
  413. gs_matrix_pop();
  414. }
  415. static void scene_video_tick(void *data, float seconds)
  416. {
  417. struct obs_scene *scene = data;
  418. struct obs_scene_item *item;
  419. video_lock(scene);
  420. item = scene->first_item;
  421. while (item) {
  422. if (item->item_render)
  423. gs_texrender_reset(item->item_render);
  424. item = item->next;
  425. }
  426. video_unlock(scene);
  427. UNUSED_PARAMETER(seconds);
  428. }
  429. /* assumes video lock */
  430. static void update_transforms_and_prune_sources(obs_scene_t *scene,
  431. struct darray *remove_items)
  432. {
  433. obs_sceneitem_t *group_sceneitem = scene->group_sceneitem;
  434. struct obs_scene_item *item = scene->first_item;
  435. bool rebuild_group = group_sceneitem &&
  436. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  437. while (item) {
  438. if (obs_source_removed(item->source)) {
  439. struct obs_scene_item *del_item = item;
  440. item = item->next;
  441. remove_without_release(del_item);
  442. darray_push_back(sizeof(struct obs_scene_item*),
  443. remove_items, &del_item);
  444. rebuild_group = true;
  445. continue;
  446. }
  447. if (item->is_group) {
  448. obs_scene_t *group_scene = item->source->context.data;
  449. video_lock(group_scene);
  450. update_transforms_and_prune_sources(group_scene,
  451. remove_items);
  452. video_unlock(group_scene);
  453. }
  454. if (os_atomic_load_bool(&item->update_transform) ||
  455. source_size_changed(item)) {
  456. update_item_transform(item);
  457. rebuild_group = true;
  458. }
  459. item = item->next;
  460. }
  461. if (rebuild_group && scene->group_sceneitem)
  462. resize_group(scene->group_sceneitem);
  463. }
  464. static void scene_video_render(void *data, gs_effect_t *effect)
  465. {
  466. DARRAY(struct obs_scene_item*) remove_items;
  467. struct obs_scene *scene = data;
  468. struct obs_scene_item *item;
  469. da_init(remove_items);
  470. video_lock(scene);
  471. if (!scene->group_sceneitem) {
  472. update_transforms_and_prune_sources(scene, &remove_items.da);
  473. }
  474. gs_blend_state_push();
  475. gs_reset_blend_state();
  476. item = scene->first_item;
  477. while (item) {
  478. if (item->user_visible)
  479. render_item(item);
  480. item = item->next;
  481. }
  482. gs_blend_state_pop();
  483. video_unlock(scene);
  484. for (size_t i = 0; i < remove_items.num; i++)
  485. obs_sceneitem_release(remove_items.array[i]);
  486. da_free(remove_items);
  487. UNUSED_PARAMETER(effect);
  488. }
  489. static void set_visibility(struct obs_scene_item *item, bool vis)
  490. {
  491. pthread_mutex_lock(&item->actions_mutex);
  492. da_resize(item->audio_actions, 0);
  493. if (os_atomic_load_long(&item->active_refs) > 0) {
  494. if (!vis)
  495. obs_source_remove_active_child(item->parent->source,
  496. item->source);
  497. } else if (vis) {
  498. obs_source_add_active_child(item->parent->source, item->source);
  499. }
  500. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  501. item->visible = vis;
  502. item->user_visible = vis;
  503. pthread_mutex_unlock(&item->actions_mutex);
  504. }
  505. static void scene_load(void *data, obs_data_t *settings);
  506. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  507. {
  508. const char *name = obs_data_get_string(item_data, "name");
  509. obs_source_t *source;
  510. const char *scale_filter_str;
  511. struct obs_scene_item *item;
  512. bool is_group;
  513. bool visible;
  514. bool lock;
  515. if (obs_data_get_bool(item_data, "group_item_backup"))
  516. return;
  517. is_group = obs_data_get_bool(item_data, "is_group");
  518. if (is_group) {
  519. obs_scene_t *sub_scene = obs_scene_create_private(name);
  520. source = sub_scene->source;
  521. } else {
  522. source = obs_get_source_by_name(name);
  523. if (!source) {
  524. blog(LOG_WARNING, "[scene_load_item] Source %s not "
  525. "found!", name);
  526. return;
  527. }
  528. }
  529. item = obs_scene_add(scene, source);
  530. if (!item) {
  531. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  532. "to scene '%s'!",
  533. name, obs_source_get_name(scene->source));
  534. obs_source_release(source);
  535. return;
  536. }
  537. if (is_group) {
  538. item->is_group = true;
  539. ((obs_scene_t *)source->context.data)->group_sceneitem = item;
  540. }
  541. obs_data_set_default_int(item_data, "align",
  542. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  543. if (obs_data_has_user_value(item_data, "id"))
  544. item->id = obs_data_get_int(item_data, "id");
  545. item->rot = (float)obs_data_get_double(item_data, "rot");
  546. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  547. visible = obs_data_get_bool(item_data, "visible");
  548. lock = obs_data_get_bool(item_data, "locked");
  549. obs_data_get_vec2(item_data, "pos", &item->pos);
  550. obs_data_get_vec2(item_data, "scale", &item->scale);
  551. obs_data_release(item->private_settings);
  552. item->private_settings =
  553. obs_data_get_obj(item_data, "private_settings");
  554. if (!item->private_settings)
  555. item->private_settings = obs_data_create();
  556. set_visibility(item, visible);
  557. obs_sceneitem_set_locked(item, lock);
  558. item->bounds_type =
  559. (enum obs_bounds_type)obs_data_get_int(item_data,
  560. "bounds_type");
  561. item->bounds_align =
  562. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  563. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  564. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  565. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  566. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  567. item->crop.bottom = (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  568. if (item->is_group) {
  569. obs_data_t *group_data = obs_data_get_obj(item_data,
  570. "group_source");
  571. scene_load(source->context.data, group_data);
  572. obs_data_release(group_data);
  573. }
  574. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  575. item->scale_filter = OBS_SCALE_DISABLE;
  576. if (scale_filter_str) {
  577. if (astrcmpi(scale_filter_str, "point") == 0)
  578. item->scale_filter = OBS_SCALE_POINT;
  579. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  580. item->scale_filter = OBS_SCALE_BILINEAR;
  581. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  582. item->scale_filter = OBS_SCALE_BICUBIC;
  583. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  584. item->scale_filter = OBS_SCALE_LANCZOS;
  585. }
  586. if (item->item_render && !item_texture_enabled(item)) {
  587. obs_enter_graphics();
  588. gs_texrender_destroy(item->item_render);
  589. item->item_render = NULL;
  590. obs_leave_graphics();
  591. } else if (!item->item_render && item_texture_enabled(item)) {
  592. obs_enter_graphics();
  593. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  594. obs_leave_graphics();
  595. }
  596. obs_source_release(source);
  597. update_item_transform(item);
  598. }
  599. static void scene_load(void *data, obs_data_t *settings)
  600. {
  601. struct obs_scene *scene = data;
  602. obs_data_array_t *items = obs_data_get_array(settings, "items");
  603. size_t count, i;
  604. remove_all_items(scene);
  605. if (!items) return;
  606. count = obs_data_array_count(items);
  607. for (i = 0; i < count; i++) {
  608. obs_data_t *item_data = obs_data_array_item(items, i);
  609. scene_load_item(scene, item_data);
  610. obs_data_release(item_data);
  611. }
  612. if (obs_data_has_user_value(settings, "id_counter"))
  613. scene->id_counter = obs_data_get_int(settings, "id_counter");
  614. if (obs_data_get_bool(settings, "custom_size")) {
  615. scene->cx = (uint32_t)obs_data_get_int(settings, "cx");
  616. scene->cy = (uint32_t)obs_data_get_int(settings, "cy");
  617. scene->custom_size = true;
  618. }
  619. obs_data_array_release(items);
  620. }
  621. static void scene_save(void *data, obs_data_t *settings);
  622. static void scene_save_item(obs_data_array_t *array,
  623. struct obs_scene_item *item, bool group_item_backup)
  624. {
  625. obs_data_t *item_data = obs_data_create();
  626. const char *name = obs_source_get_name(item->source);
  627. const char *scale_filter;
  628. struct vec2 pos = item->pos;
  629. struct vec2 scale = item->scale;
  630. float rot = item->rot;
  631. if (group_item_backup) {
  632. obs_sceneitem_t *group = item->parent->group_sceneitem;
  633. get_ungrouped_transform(group, &pos, &scale, &rot);
  634. }
  635. obs_data_set_string(item_data, "name", name);
  636. obs_data_set_bool (item_data, "visible", item->user_visible);
  637. obs_data_set_bool (item_data, "locked", item->locked);
  638. obs_data_set_double(item_data, "rot", rot);
  639. obs_data_set_vec2 (item_data, "pos", &pos);
  640. obs_data_set_vec2 (item_data, "scale", &scale);
  641. obs_data_set_int (item_data, "align", (int)item->align);
  642. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  643. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  644. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  645. obs_data_set_int (item_data, "crop_left", (int)item->crop.left);
  646. obs_data_set_int (item_data, "crop_top", (int)item->crop.top);
  647. obs_data_set_int (item_data, "crop_right", (int)item->crop.right);
  648. obs_data_set_int (item_data, "crop_bottom", (int)item->crop.bottom);
  649. obs_data_set_int (item_data, "id", item->id);
  650. obs_data_set_bool (item_data, "is_group", item->is_group);
  651. obs_data_set_bool (item_data, "group_item_backup", group_item_backup);
  652. if (item->is_group) {
  653. obs_data_t *group_data = obs_data_create();
  654. obs_scene_t *group_scene = item->source->context.data;
  655. obs_sceneitem_t *group_item;
  656. scene_save(group_scene, group_data);
  657. /* save group items as part of main scene, but ignored.
  658. * causes an automatic ungroup if scene collection file
  659. * is loaded in previous versions. */
  660. full_lock(group_scene);
  661. group_item = group_scene->first_item;
  662. while (group_item) {
  663. scene_save_item(array, group_item, true);
  664. group_item = group_item->next;
  665. }
  666. full_unlock(group_scene);
  667. obs_data_set_obj(item_data, "group_source", group_data);
  668. obs_data_release(group_data);
  669. }
  670. if (item->scale_filter == OBS_SCALE_POINT)
  671. scale_filter = "point";
  672. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  673. scale_filter = "bilinear";
  674. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  675. scale_filter = "bicubic";
  676. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  677. scale_filter = "lanczos";
  678. else
  679. scale_filter = "disable";
  680. obs_data_set_string(item_data, "scale_filter", scale_filter);
  681. obs_data_set_obj(item_data, "private_settings",
  682. item->private_settings);
  683. obs_data_array_push_back(array, item_data);
  684. obs_data_release(item_data);
  685. }
  686. static void scene_save(void *data, obs_data_t *settings)
  687. {
  688. struct obs_scene *scene = data;
  689. obs_data_array_t *array = obs_data_array_create();
  690. struct obs_scene_item *item;
  691. full_lock(scene);
  692. item = scene->first_item;
  693. while (item) {
  694. scene_save_item(array, item, false);
  695. item = item->next;
  696. }
  697. obs_data_set_int(settings, "id_counter", scene->id_counter);
  698. obs_data_set_bool(settings, "custom_size", scene->custom_size);
  699. if (scene->custom_size) {
  700. obs_data_set_int(settings, "cx", scene->cx);
  701. obs_data_set_int(settings, "cy", scene->cy);
  702. }
  703. full_unlock(scene);
  704. obs_data_set_array(settings, "items", array);
  705. obs_data_array_release(array);
  706. }
  707. static uint32_t scene_getwidth(void *data)
  708. {
  709. obs_scene_t *scene = data;
  710. return scene->custom_size ? scene->cx : obs->video.base_width;
  711. }
  712. static uint32_t scene_getheight(void *data)
  713. {
  714. obs_scene_t *scene = data;
  715. return scene->custom_size ? scene->cy : obs->video.base_height;
  716. }
  717. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  718. float **p_buf, uint64_t ts, size_t sample_rate)
  719. {
  720. bool cur_visible = item->visible;
  721. uint64_t frame_num = 0;
  722. size_t deref_count = 0;
  723. float *buf = NULL;
  724. if (p_buf) {
  725. if (!*p_buf)
  726. *p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
  727. buf = *p_buf;
  728. }
  729. pthread_mutex_lock(&item->actions_mutex);
  730. for (size_t i = 0; i < item->audio_actions.num; i++) {
  731. struct item_action action = item->audio_actions.array[i];
  732. uint64_t timestamp = action.timestamp;
  733. uint64_t new_frame_num;
  734. if (timestamp < ts)
  735. timestamp = ts;
  736. new_frame_num = (timestamp - ts) * (uint64_t)sample_rate /
  737. 1000000000ULL;
  738. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  739. break;
  740. da_erase(item->audio_actions, i--);
  741. item->visible = action.visible;
  742. if (!item->visible)
  743. deref_count++;
  744. if (buf && new_frame_num > frame_num) {
  745. for (; frame_num < new_frame_num; frame_num++)
  746. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  747. }
  748. cur_visible = item->visible;
  749. }
  750. if (buf) {
  751. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  752. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  753. }
  754. pthread_mutex_unlock(&item->actions_mutex);
  755. while (deref_count--) {
  756. if (os_atomic_dec_long(&item->active_refs) == 0) {
  757. obs_source_remove_active_child(item->parent->source,
  758. item->source);
  759. }
  760. }
  761. }
  762. static bool apply_scene_item_volume(struct obs_scene_item *item,
  763. float **buf, uint64_t ts, size_t sample_rate)
  764. {
  765. bool actions_pending;
  766. struct item_action action;
  767. pthread_mutex_lock(&item->actions_mutex);
  768. actions_pending = item->audio_actions.num > 0;
  769. if (actions_pending)
  770. action = item->audio_actions.array[0];
  771. pthread_mutex_unlock(&item->actions_mutex);
  772. if (actions_pending) {
  773. uint64_t duration = (uint64_t)AUDIO_OUTPUT_FRAMES *
  774. 1000000000ULL / (uint64_t)sample_rate;
  775. if (!ts || action.timestamp < (ts + duration)) {
  776. apply_scene_item_audio_actions(item, buf, ts,
  777. sample_rate);
  778. return true;
  779. }
  780. }
  781. return false;
  782. }
  783. static void process_all_audio_actions(struct obs_scene_item *item,
  784. size_t sample_rate)
  785. {
  786. while (apply_scene_item_volume(item, NULL, 0, sample_rate));
  787. }
  788. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  789. size_t pos, size_t count)
  790. {
  791. register float *out = p_out;
  792. register float *buf = buf_in + pos;
  793. register float *in = p_in + pos;
  794. register float *end = in + count;
  795. while (in < end)
  796. *(out++) += *(in++) * *(buf++);
  797. }
  798. static inline void mix_audio(float *p_out, float *p_in,
  799. size_t pos, size_t count)
  800. {
  801. register float *out = p_out;
  802. register float *in = p_in + pos;
  803. register float *end = in + count;
  804. while (in < end)
  805. *(out++) += *(in++);
  806. }
  807. static bool scene_audio_render(void *data, uint64_t *ts_out,
  808. struct obs_source_audio_mix *audio_output, uint32_t mixers,
  809. size_t channels, size_t sample_rate)
  810. {
  811. uint64_t timestamp = 0;
  812. float *buf = NULL;
  813. struct obs_source_audio_mix child_audio;
  814. struct obs_scene *scene = data;
  815. struct obs_scene_item *item;
  816. audio_lock(scene);
  817. item = scene->first_item;
  818. while (item) {
  819. if (!obs_source_audio_pending(item->source) && item->visible) {
  820. uint64_t source_ts =
  821. obs_source_get_audio_timestamp(item->source);
  822. if (source_ts && (!timestamp || source_ts < timestamp))
  823. timestamp = source_ts;
  824. }
  825. item = item->next;
  826. }
  827. if (!timestamp) {
  828. /* just process all pending audio actions if no audio playing,
  829. * otherwise audio actions will just never be processed */
  830. item = scene->first_item;
  831. while (item) {
  832. process_all_audio_actions(item, sample_rate);
  833. item = item->next;
  834. }
  835. audio_unlock(scene);
  836. return false;
  837. }
  838. item = scene->first_item;
  839. while (item) {
  840. uint64_t source_ts;
  841. size_t pos, count;
  842. bool apply_buf;
  843. apply_buf = apply_scene_item_volume(item, &buf, timestamp,
  844. sample_rate);
  845. if (obs_source_audio_pending(item->source)) {
  846. item = item->next;
  847. continue;
  848. }
  849. source_ts = obs_source_get_audio_timestamp(item->source);
  850. if (!source_ts) {
  851. item = item->next;
  852. continue;
  853. }
  854. pos = (size_t)ns_to_audio_frames(sample_rate,
  855. source_ts - timestamp);
  856. count = AUDIO_OUTPUT_FRAMES - pos;
  857. if (!apply_buf && !item->visible) {
  858. item = item->next;
  859. continue;
  860. }
  861. obs_source_get_audio_mix(item->source, &child_audio);
  862. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  863. if ((mixers & (1 << mix)) == 0)
  864. continue;
  865. for (size_t ch = 0; ch < channels; ch++) {
  866. float *out = audio_output->output[mix].data[ch];
  867. float *in = child_audio.output[mix].data[ch];
  868. if (apply_buf)
  869. mix_audio_with_buf(out, in, buf, pos,
  870. count);
  871. else
  872. mix_audio(out, in, pos, count);
  873. }
  874. }
  875. item = item->next;
  876. }
  877. *ts_out = timestamp;
  878. audio_unlock(scene);
  879. free(buf);
  880. return true;
  881. }
  882. const struct obs_source_info scene_info =
  883. {
  884. .id = "scene",
  885. .type = OBS_SOURCE_TYPE_SCENE,
  886. .output_flags = OBS_SOURCE_VIDEO |
  887. OBS_SOURCE_CUSTOM_DRAW |
  888. OBS_SOURCE_COMPOSITE,
  889. .get_name = scene_getname,
  890. .create = scene_create,
  891. .destroy = scene_destroy,
  892. .video_tick = scene_video_tick,
  893. .video_render = scene_video_render,
  894. .audio_render = scene_audio_render,
  895. .get_width = scene_getwidth,
  896. .get_height = scene_getheight,
  897. .load = scene_load,
  898. .save = scene_save,
  899. .enum_active_sources = scene_enum_active_sources,
  900. .enum_all_sources = scene_enum_all_sources
  901. };
  902. obs_scene_t *obs_scene_create(const char *name)
  903. {
  904. struct obs_source *source = obs_source_create("scene", name, NULL,
  905. NULL);
  906. return source->context.data;
  907. }
  908. obs_scene_t *obs_scene_create_private(const char *name)
  909. {
  910. struct obs_source *source = obs_source_create_private("scene", name,
  911. NULL);
  912. return source->context.data;
  913. }
  914. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  915. {
  916. struct obs_scene_item *item = scene->first_item;
  917. while (item && idx--)
  918. item = item->next;
  919. return item ? item->source : NULL;
  920. }
  921. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  922. obs_scene_t *new_scene, bool private)
  923. {
  924. DARRAY(struct obs_scene_item*) old_items;
  925. obs_source_t *source;
  926. old_items.da = *array;
  927. source = old_items.array[idx]->source;
  928. /* if the old item is referenced more than once in the old scene,
  929. * make sure they're referenced similarly in the new scene to reduce
  930. * load times */
  931. for (size_t i = 0; i < idx; i++) {
  932. struct obs_scene_item *item = old_items.array[i];
  933. if (item->source == source) {
  934. source = get_child_at_idx(new_scene, i);
  935. obs_source_addref(source);
  936. return source;
  937. }
  938. }
  939. return obs_source_duplicate(source, NULL, private);
  940. }
  941. static inline obs_source_t *new_ref(obs_source_t *source)
  942. {
  943. obs_source_addref(source);
  944. return source;
  945. }
  946. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  947. enum obs_scene_duplicate_type type)
  948. {
  949. bool make_unique = ((int)type & (1<<0)) != 0;
  950. bool make_private = ((int)type & (1<<1)) != 0;
  951. DARRAY(struct obs_scene_item*) items;
  952. struct obs_scene *new_scene;
  953. struct obs_scene_item *item;
  954. struct obs_source *source;
  955. da_init(items);
  956. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  957. return NULL;
  958. /* --------------------------------- */
  959. full_lock(scene);
  960. item = scene->first_item;
  961. while (item) {
  962. da_push_back(items, &item);
  963. obs_sceneitem_addref(item);
  964. item = item->next;
  965. }
  966. full_unlock(scene);
  967. /* --------------------------------- */
  968. new_scene = make_private ?
  969. obs_scene_create_private(name) : obs_scene_create(name);
  970. obs_source_copy_filters(new_scene->source, scene->source);
  971. obs_data_apply(new_scene->source->private_settings,
  972. scene->source->private_settings);
  973. for (size_t i = 0; i < items.num; i++) {
  974. item = items.array[i];
  975. source = make_unique ?
  976. dup_child(&items.da, i, new_scene, make_private) :
  977. new_ref(item->source);
  978. if (source) {
  979. struct obs_scene_item *new_item =
  980. obs_scene_add(new_scene, source);
  981. if (!new_item) {
  982. obs_source_release(source);
  983. continue;
  984. }
  985. if (!item->user_visible)
  986. set_visibility(new_item, false);
  987. new_item->selected = item->selected;
  988. new_item->pos = item->pos;
  989. new_item->rot = item->rot;
  990. new_item->scale = item->scale;
  991. new_item->align = item->align;
  992. new_item->last_width = item->last_width;
  993. new_item->last_height = item->last_height;
  994. new_item->output_scale = item->output_scale;
  995. new_item->scale_filter = item->scale_filter;
  996. new_item->box_transform = item->box_transform;
  997. new_item->draw_transform = item->draw_transform;
  998. new_item->bounds_type = item->bounds_type;
  999. new_item->bounds_align = item->bounds_align;
  1000. new_item->bounds = item->bounds;
  1001. new_item->toggle_visibility =
  1002. OBS_INVALID_HOTKEY_PAIR_ID;
  1003. obs_sceneitem_set_crop(new_item, &item->crop);
  1004. if (!new_item->item_render &&
  1005. item_texture_enabled(new_item)) {
  1006. obs_enter_graphics();
  1007. new_item->item_render = gs_texrender_create(
  1008. GS_RGBA, GS_ZS_NONE);
  1009. obs_leave_graphics();
  1010. }
  1011. obs_source_release(source);
  1012. }
  1013. }
  1014. for (size_t i = 0; i < items.num; i++)
  1015. obs_sceneitem_release(items.array[i]);
  1016. da_free(items);
  1017. return new_scene;
  1018. }
  1019. void obs_scene_addref(obs_scene_t *scene)
  1020. {
  1021. if (scene)
  1022. obs_source_addref(scene->source);
  1023. }
  1024. void obs_scene_release(obs_scene_t *scene)
  1025. {
  1026. if (scene)
  1027. obs_source_release(scene->source);
  1028. }
  1029. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  1030. {
  1031. return scene ? scene->source : NULL;
  1032. }
  1033. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  1034. {
  1035. if (!source || source->info.id != scene_info.id)
  1036. return NULL;
  1037. return source->context.data;
  1038. }
  1039. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  1040. {
  1041. struct obs_scene_item *item;
  1042. if (!scene)
  1043. return NULL;
  1044. full_lock(scene);
  1045. item = scene->first_item;
  1046. while (item) {
  1047. if (strcmp(item->source->context.name, name) == 0)
  1048. break;
  1049. item = item->next;
  1050. }
  1051. full_unlock(scene);
  1052. return item;
  1053. }
  1054. obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
  1055. {
  1056. struct obs_scene_item *item;
  1057. if (!scene)
  1058. return NULL;
  1059. full_lock(scene);
  1060. item = scene->first_item;
  1061. while (item) {
  1062. if (item->id == id)
  1063. break;
  1064. item = item->next;
  1065. }
  1066. full_unlock(scene);
  1067. return item;
  1068. }
  1069. void obs_scene_enum_items(obs_scene_t *scene,
  1070. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  1071. void *param)
  1072. {
  1073. struct obs_scene_item *item;
  1074. if (!scene || !callback)
  1075. return;
  1076. full_lock(scene);
  1077. item = scene->first_item;
  1078. while (item) {
  1079. struct obs_scene_item *next = item->next;
  1080. obs_sceneitem_addref(item);
  1081. if (!callback(scene, item, param)) {
  1082. obs_sceneitem_release(item);
  1083. break;
  1084. }
  1085. obs_sceneitem_release(item);
  1086. item = next;
  1087. }
  1088. full_unlock(scene);
  1089. }
  1090. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  1091. {
  1092. long owners = si->ref;
  1093. while (owners > 0) {
  1094. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  1095. return si;
  1096. owners = si->ref;
  1097. }
  1098. return NULL;
  1099. }
  1100. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  1101. obs_hotkey_t *hotkey, bool pressed)
  1102. {
  1103. UNUSED_PARAMETER(id);
  1104. UNUSED_PARAMETER(hotkey);
  1105. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1106. if (pressed && si && !si->user_visible) {
  1107. obs_sceneitem_set_visible(si, true);
  1108. obs_sceneitem_release(si);
  1109. return true;
  1110. }
  1111. obs_sceneitem_release(si);
  1112. return false;
  1113. }
  1114. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  1115. obs_hotkey_t *hotkey, bool pressed)
  1116. {
  1117. UNUSED_PARAMETER(id);
  1118. UNUSED_PARAMETER(hotkey);
  1119. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1120. if (pressed && si && si->user_visible) {
  1121. obs_sceneitem_set_visible(si, false);
  1122. obs_sceneitem_release(si);
  1123. return true;
  1124. }
  1125. obs_sceneitem_release(si);
  1126. return false;
  1127. }
  1128. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  1129. const char *name)
  1130. {
  1131. struct dstr show = {0};
  1132. struct dstr hide = {0};
  1133. struct dstr show_desc = {0};
  1134. struct dstr hide_desc = {0};
  1135. dstr_copy(&show, "libobs.show_scene_item.%1");
  1136. dstr_replace(&show, "%1", name);
  1137. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1138. dstr_replace(&hide, "%1", name);
  1139. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1140. dstr_replace(&show_desc, "%1", name);
  1141. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1142. dstr_replace(&hide_desc, "%1", name);
  1143. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  1144. show.array, show_desc.array,
  1145. hide.array, hide_desc.array,
  1146. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1147. item, item);
  1148. dstr_free(&show);
  1149. dstr_free(&hide);
  1150. dstr_free(&show_desc);
  1151. dstr_free(&hide_desc);
  1152. }
  1153. static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
  1154. const char *new_name)
  1155. {
  1156. struct dstr show = { 0 };
  1157. struct dstr hide = { 0 };
  1158. struct dstr show_desc = { 0 };
  1159. struct dstr hide_desc = { 0 };
  1160. dstr_copy(&show, "libobs.show_scene_item.%1");
  1161. dstr_replace(&show, "%1", new_name);
  1162. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1163. dstr_replace(&hide, "%1", new_name);
  1164. obs_hotkey_pair_set_names(scene_item->toggle_visibility,
  1165. show.array, hide.array);
  1166. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1167. dstr_replace(&show_desc, "%1", new_name);
  1168. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1169. dstr_replace(&hide_desc, "%1", new_name);
  1170. obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
  1171. show_desc.array, hide_desc.array);
  1172. dstr_free(&show);
  1173. dstr_free(&hide);
  1174. dstr_free(&show_desc);
  1175. dstr_free(&hide_desc);
  1176. }
  1177. static void sceneitem_renamed(void *param, calldata_t *data)
  1178. {
  1179. obs_sceneitem_t *scene_item = param;
  1180. const char *name = calldata_string(data, "new_name");
  1181. sceneitem_rename_hotkey(scene_item, name);
  1182. }
  1183. static inline bool source_has_audio(obs_source_t *source)
  1184. {
  1185. return (source->info.output_flags &
  1186. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1187. }
  1188. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  1189. obs_source_t *source, obs_sceneitem_t *insert_after,
  1190. bool is_group)
  1191. {
  1192. struct obs_scene_item *last;
  1193. struct obs_scene_item *item;
  1194. pthread_mutex_t mutex;
  1195. struct item_action action = {
  1196. .visible = true,
  1197. .timestamp = os_gettime_ns()
  1198. };
  1199. if (!scene)
  1200. return NULL;
  1201. if (!source) {
  1202. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1203. return NULL;
  1204. }
  1205. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1206. blog(LOG_WARNING, "Failed to create scene item mutex");
  1207. return NULL;
  1208. }
  1209. if (!obs_source_add_active_child(scene->source, source)) {
  1210. blog(LOG_WARNING, "Failed to add source to scene due to "
  1211. "infinite source recursion");
  1212. pthread_mutex_destroy(&mutex);
  1213. return NULL;
  1214. }
  1215. item = bzalloc(sizeof(struct obs_scene_item));
  1216. item->source = source;
  1217. item->id = ++scene->id_counter;
  1218. item->parent = scene;
  1219. item->ref = 1;
  1220. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1221. item->actions_mutex = mutex;
  1222. item->user_visible = true;
  1223. item->locked = false;
  1224. item->is_group = is_group;
  1225. item->private_settings = obs_data_create();
  1226. os_atomic_set_long(&item->active_refs, 1);
  1227. vec2_set(&item->scale, 1.0f, 1.0f);
  1228. matrix4_identity(&item->draw_transform);
  1229. matrix4_identity(&item->box_transform);
  1230. obs_source_addref(source);
  1231. if (source_has_audio(source)) {
  1232. item->visible = false;
  1233. da_push_back(item->audio_actions, &action);
  1234. } else {
  1235. item->visible = true;
  1236. }
  1237. if (item_texture_enabled(item)) {
  1238. obs_enter_graphics();
  1239. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1240. obs_leave_graphics();
  1241. }
  1242. full_lock(scene);
  1243. if (insert_after) {
  1244. obs_sceneitem_t *next = insert_after->next;
  1245. if (next) next->prev = item;
  1246. item->next = insert_after->next;
  1247. item->prev = insert_after;
  1248. insert_after->next = item;
  1249. } else {
  1250. last = scene->first_item;
  1251. if (!last) {
  1252. scene->first_item = item;
  1253. } else {
  1254. while (last->next)
  1255. last = last->next;
  1256. last->next = item;
  1257. item->prev = last;
  1258. }
  1259. }
  1260. full_unlock(scene);
  1261. if (!scene->source->context.private)
  1262. init_hotkeys(scene, item, obs_source_get_name(source));
  1263. signal_handler_connect(obs_source_get_signal_handler(source), "rename",
  1264. sceneitem_renamed, item);
  1265. return item;
  1266. }
  1267. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1268. {
  1269. obs_sceneitem_t *item = obs_scene_add_internal(scene, source, NULL,
  1270. false);
  1271. struct calldata params;
  1272. uint8_t stack[128];
  1273. calldata_init_fixed(&params, stack, sizeof(stack));
  1274. calldata_set_ptr(&params, "scene", scene);
  1275. calldata_set_ptr(&params, "item", item);
  1276. signal_handler_signal(scene->source->context.signals, "item_add",
  1277. &params);
  1278. return item;
  1279. }
  1280. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  1281. {
  1282. if (item) {
  1283. if (item->item_render) {
  1284. obs_enter_graphics();
  1285. gs_texrender_destroy(item->item_render);
  1286. obs_leave_graphics();
  1287. }
  1288. obs_data_release(item->private_settings);
  1289. obs_hotkey_pair_unregister(item->toggle_visibility);
  1290. pthread_mutex_destroy(&item->actions_mutex);
  1291. signal_handler_disconnect(
  1292. obs_source_get_signal_handler(item->source),
  1293. "rename", sceneitem_renamed, item);
  1294. if (item->source)
  1295. obs_source_release(item->source);
  1296. da_free(item->audio_actions);
  1297. bfree(item);
  1298. }
  1299. }
  1300. void obs_sceneitem_addref(obs_sceneitem_t *item)
  1301. {
  1302. if (item)
  1303. os_atomic_inc_long(&item->ref);
  1304. }
  1305. void obs_sceneitem_release(obs_sceneitem_t *item)
  1306. {
  1307. if (!item)
  1308. return;
  1309. if (os_atomic_dec_long(&item->ref) == 0)
  1310. obs_sceneitem_destroy(item);
  1311. }
  1312. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1313. {
  1314. obs_scene_t *scene;
  1315. if (!item)
  1316. return;
  1317. scene = item->parent;
  1318. full_lock(scene);
  1319. if (item->removed) {
  1320. if (scene)
  1321. full_unlock(scene);
  1322. return;
  1323. }
  1324. item->removed = true;
  1325. assert(scene != NULL);
  1326. assert(scene->source != NULL);
  1327. set_visibility(item, false);
  1328. signal_item_remove(item);
  1329. detach_sceneitem(item);
  1330. full_unlock(scene);
  1331. obs_sceneitem_release(item);
  1332. }
  1333. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1334. {
  1335. return item ? item->parent : NULL;
  1336. }
  1337. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1338. {
  1339. return item ? item->source : NULL;
  1340. }
  1341. static void signal_parent(obs_scene_t *parent, const char *command,
  1342. calldata_t *params)
  1343. {
  1344. calldata_set_ptr(params, "scene", parent);
  1345. signal_handler_signal(parent->source->context.signals, command, params);
  1346. if (parent->group_sceneitem) {
  1347. parent = parent->group_sceneitem->parent;
  1348. if (!parent)
  1349. return;
  1350. calldata_set_ptr(params, "scene", parent);
  1351. signal_handler_signal(parent->source->context.signals, command,
  1352. params);
  1353. }
  1354. }
  1355. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1356. {
  1357. struct calldata params;
  1358. uint8_t stack[128];
  1359. const char *command = select ? "item_select" : "item_deselect";
  1360. if (!item || item->selected == select || !item->parent)
  1361. return;
  1362. item->selected = select;
  1363. calldata_init_fixed(&params, stack, sizeof(stack));
  1364. calldata_set_ptr(&params, "item", item);
  1365. signal_parent(item->parent, command, &params);
  1366. }
  1367. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1368. {
  1369. return item ? item->selected : false;
  1370. }
  1371. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1372. {
  1373. if (item) {
  1374. vec2_copy(&item->pos, pos);
  1375. os_atomic_set_bool(&item->update_transform, true);
  1376. }
  1377. }
  1378. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1379. {
  1380. if (item) {
  1381. item->rot = rot;
  1382. os_atomic_set_bool(&item->update_transform, true);
  1383. }
  1384. }
  1385. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1386. {
  1387. if (item) {
  1388. vec2_copy(&item->scale, scale);
  1389. os_atomic_set_bool(&item->update_transform, true);
  1390. }
  1391. }
  1392. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1393. {
  1394. if (item) {
  1395. item->align = alignment;
  1396. os_atomic_set_bool(&item->update_transform, true);
  1397. }
  1398. }
  1399. static inline void signal_reorder(struct obs_scene_item *item)
  1400. {
  1401. const char *command = NULL;
  1402. struct calldata params;
  1403. uint8_t stack[128];
  1404. command = "reorder";
  1405. calldata_init_fixed(&params, stack, sizeof(stack));
  1406. signal_parent(item->parent, command, &params);
  1407. }
  1408. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1409. enum obs_order_movement movement)
  1410. {
  1411. if (!item) return;
  1412. struct obs_scene_item *next, *prev;
  1413. struct obs_scene *scene = item->parent;
  1414. obs_scene_addref(scene);
  1415. full_lock(scene);
  1416. next = item->next;
  1417. prev = item->prev;
  1418. detach_sceneitem(item);
  1419. if (movement == OBS_ORDER_MOVE_DOWN) {
  1420. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1421. } else if (movement == OBS_ORDER_MOVE_UP) {
  1422. attach_sceneitem(scene, item, next ? next : prev);
  1423. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1424. struct obs_scene_item *last = next;
  1425. if (!last) {
  1426. last = prev;
  1427. } else {
  1428. while (last->next)
  1429. last = last->next;
  1430. }
  1431. attach_sceneitem(scene, item, last);
  1432. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1433. attach_sceneitem(scene, item, NULL);
  1434. }
  1435. full_unlock(scene);
  1436. signal_reorder(item);
  1437. obs_scene_release(scene);
  1438. }
  1439. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1440. int position)
  1441. {
  1442. if (!item) return;
  1443. struct obs_scene *scene = item->parent;
  1444. struct obs_scene_item *next;
  1445. obs_scene_addref(scene);
  1446. full_lock(scene);
  1447. detach_sceneitem(item);
  1448. next = scene->first_item;
  1449. if (position == 0) {
  1450. attach_sceneitem(scene, item, NULL);
  1451. } else {
  1452. for (int i = position; i > 1; --i) {
  1453. if (next->next == NULL)
  1454. break;
  1455. next = next->next;
  1456. }
  1457. attach_sceneitem(scene, item, next);
  1458. }
  1459. full_unlock(scene);
  1460. signal_reorder(item);
  1461. obs_scene_release(scene);
  1462. }
  1463. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1464. enum obs_bounds_type type)
  1465. {
  1466. if (item) {
  1467. item->bounds_type = type;
  1468. os_atomic_set_bool(&item->update_transform, true);
  1469. }
  1470. }
  1471. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1472. uint32_t alignment)
  1473. {
  1474. if (item) {
  1475. item->bounds_align = alignment;
  1476. os_atomic_set_bool(&item->update_transform, true);
  1477. }
  1478. }
  1479. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1480. {
  1481. if (item) {
  1482. item->bounds = *bounds;
  1483. os_atomic_set_bool(&item->update_transform, true);
  1484. }
  1485. }
  1486. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1487. {
  1488. if (item)
  1489. vec2_copy(pos, &item->pos);
  1490. }
  1491. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1492. {
  1493. return item ? item->rot : 0.0f;
  1494. }
  1495. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1496. {
  1497. if (item)
  1498. vec2_copy(scale, &item->scale);
  1499. }
  1500. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1501. {
  1502. return item ? item->align : 0;
  1503. }
  1504. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1505. {
  1506. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1507. }
  1508. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1509. {
  1510. return item ? item->bounds_align : 0;
  1511. }
  1512. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1513. {
  1514. if (item)
  1515. *bounds = item->bounds;
  1516. }
  1517. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1518. struct obs_transform_info *info)
  1519. {
  1520. if (item && info) {
  1521. info->pos = item->pos;
  1522. info->rot = item->rot;
  1523. info->scale = item->scale;
  1524. info->alignment = item->align;
  1525. info->bounds_type = item->bounds_type;
  1526. info->bounds_alignment = item->bounds_align;
  1527. info->bounds = item->bounds;
  1528. }
  1529. }
  1530. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1531. const struct obs_transform_info *info)
  1532. {
  1533. if (item && info) {
  1534. item->pos = info->pos;
  1535. item->rot = info->rot;
  1536. item->scale = info->scale;
  1537. item->align = info->alignment;
  1538. item->bounds_type = info->bounds_type;
  1539. item->bounds_align = info->bounds_alignment;
  1540. item->bounds = info->bounds;
  1541. os_atomic_set_bool(&item->update_transform, true);
  1542. }
  1543. }
  1544. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1545. struct matrix4 *transform)
  1546. {
  1547. if (item)
  1548. matrix4_copy(transform, &item->draw_transform);
  1549. }
  1550. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1551. struct matrix4 *transform)
  1552. {
  1553. if (item)
  1554. matrix4_copy(transform, &item->box_transform);
  1555. }
  1556. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1557. {
  1558. return item ? item->user_visible : false;
  1559. }
  1560. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1561. {
  1562. struct calldata cd;
  1563. uint8_t stack[256];
  1564. struct item_action action = {
  1565. .visible = visible,
  1566. .timestamp = os_gettime_ns()
  1567. };
  1568. if (!item)
  1569. return false;
  1570. if (item->user_visible == visible)
  1571. return false;
  1572. if (!item->parent)
  1573. return false;
  1574. if (visible) {
  1575. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1576. if (!obs_source_add_active_child(item->parent->source,
  1577. item->source)) {
  1578. os_atomic_dec_long(&item->active_refs);
  1579. return false;
  1580. }
  1581. }
  1582. }
  1583. item->user_visible = visible;
  1584. calldata_init_fixed(&cd, stack, sizeof(stack));
  1585. calldata_set_ptr(&cd, "item", item);
  1586. calldata_set_bool(&cd, "visible", visible);
  1587. signal_parent(item->parent, "item_visible", &cd);
  1588. if (source_has_audio(item->source)) {
  1589. pthread_mutex_lock(&item->actions_mutex);
  1590. da_push_back(item->audio_actions, &action);
  1591. pthread_mutex_unlock(&item->actions_mutex);
  1592. } else {
  1593. set_visibility(item, visible);
  1594. }
  1595. return true;
  1596. }
  1597. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  1598. {
  1599. return item ? item->locked : false;
  1600. }
  1601. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  1602. {
  1603. if (!item)
  1604. return false;
  1605. if (item->locked == lock)
  1606. return false;
  1607. if (!item->parent)
  1608. return false;
  1609. item->locked = lock;
  1610. return true;
  1611. }
  1612. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1613. size_t size, bool *order_matches)
  1614. {
  1615. obs_sceneitem_t *item = scene->first_item;
  1616. size_t count = 0;
  1617. while (item) {
  1618. bool found = false;
  1619. for (size_t i = 0; i < size; i++) {
  1620. if (items[i] != item)
  1621. continue;
  1622. if (count != i)
  1623. *order_matches = false;
  1624. found = true;
  1625. break;
  1626. }
  1627. if (!found)
  1628. return false;
  1629. item = item->next;
  1630. count += 1;
  1631. }
  1632. return count == size;
  1633. }
  1634. bool obs_scene_reorder_items(obs_scene_t *scene,
  1635. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1636. {
  1637. if (!scene || !item_order_size)
  1638. return false;
  1639. obs_scene_addref(scene);
  1640. full_lock(scene);
  1641. bool order_matches = true;
  1642. if (!sceneitems_match(scene, item_order, item_order_size,
  1643. &order_matches) || order_matches) {
  1644. full_unlock(scene);
  1645. obs_scene_release(scene);
  1646. return false;
  1647. }
  1648. scene->first_item = item_order[0];
  1649. obs_sceneitem_t *prev = NULL;
  1650. for (size_t i = 0; i < item_order_size; i++) {
  1651. item_order[i]->prev = prev;
  1652. item_order[i]->next = NULL;
  1653. if (prev)
  1654. prev->next = item_order[i];
  1655. prev = item_order[i];
  1656. }
  1657. full_unlock(scene);
  1658. signal_reorder(scene->first_item);
  1659. obs_scene_release(scene);
  1660. return true;
  1661. }
  1662. void obs_scene_atomic_update(obs_scene_t *scene,
  1663. obs_scene_atomic_update_func func, void *data)
  1664. {
  1665. if (!scene)
  1666. return;
  1667. obs_scene_addref(scene);
  1668. full_lock(scene);
  1669. func(data, scene);
  1670. full_unlock(scene);
  1671. obs_scene_release(scene);
  1672. }
  1673. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  1674. const struct obs_sceneitem_crop *crop2)
  1675. {
  1676. return crop1->left == crop2->left &&
  1677. crop1->right == crop2->right &&
  1678. crop1->top == crop2->top &&
  1679. crop1->bottom == crop2->bottom;
  1680. }
  1681. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  1682. const struct obs_sceneitem_crop *crop)
  1683. {
  1684. bool item_tex_now_enabled;
  1685. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  1686. return;
  1687. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  1688. return;
  1689. if (crop_equal(crop, &item->crop))
  1690. return;
  1691. item_tex_now_enabled = crop_enabled(crop) ||
  1692. scale_filter_enabled(item) || item_is_scene(item);
  1693. obs_enter_graphics();
  1694. if (!item_tex_now_enabled) {
  1695. gs_texrender_destroy(item->item_render);
  1696. item->item_render = NULL;
  1697. } else if (!item->item_render) {
  1698. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1699. }
  1700. memcpy(&item->crop, crop, sizeof(*crop));
  1701. if (item->crop.left < 0) item->crop.left = 0;
  1702. if (item->crop.right < 0) item->crop.right = 0;
  1703. if (item->crop.top < 0) item->crop.top = 0;
  1704. if (item->crop.bottom < 0) item->crop.bottom = 0;
  1705. obs_leave_graphics();
  1706. os_atomic_set_bool(&item->update_transform, true);
  1707. }
  1708. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  1709. struct obs_sceneitem_crop *crop)
  1710. {
  1711. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  1712. return;
  1713. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  1714. return;
  1715. memcpy(crop, &item->crop, sizeof(*crop));
  1716. }
  1717. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  1718. enum obs_scale_type filter)
  1719. {
  1720. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  1721. return;
  1722. item->scale_filter = filter;
  1723. obs_enter_graphics();
  1724. if (!item_texture_enabled(item)) {
  1725. gs_texrender_destroy(item->item_render);
  1726. item->item_render = NULL;
  1727. } else if (!item->item_render) {
  1728. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1729. }
  1730. obs_leave_graphics();
  1731. os_atomic_set_bool(&item->update_transform, true);
  1732. }
  1733. enum obs_scale_type obs_sceneitem_get_scale_filter(
  1734. obs_sceneitem_t *item)
  1735. {
  1736. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter") ?
  1737. item->scale_filter : OBS_SCALE_DISABLE;
  1738. }
  1739. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  1740. {
  1741. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  1742. return;
  1743. os_atomic_inc_long(&item->defer_update);
  1744. }
  1745. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  1746. {
  1747. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  1748. return;
  1749. if (os_atomic_dec_long(&item->defer_update) == 0)
  1750. os_atomic_set_bool(&item->update_transform, true);
  1751. }
  1752. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  1753. {
  1754. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  1755. return;
  1756. os_atomic_inc_long(&item->defer_group_resize);
  1757. }
  1758. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  1759. {
  1760. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  1761. return;
  1762. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  1763. os_atomic_set_bool(&item->update_group_resize, true);
  1764. }
  1765. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  1766. {
  1767. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  1768. return 0;
  1769. return item->id;
  1770. }
  1771. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  1772. {
  1773. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  1774. return NULL;
  1775. obs_data_addref(item->private_settings);
  1776. return item->private_settings;
  1777. }
  1778. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  1779. {
  1780. struct vec3 v;
  1781. vec3_set(&v, v2->x, v2->y, 0.0f);
  1782. vec3_transform(&v, &v, transform);
  1783. v2->x = v.x;
  1784. v2->y = v.y;
  1785. }
  1786. static void get_ungrouped_transform(obs_sceneitem_t *group,
  1787. struct vec2 *pos,
  1788. struct vec2 *scale,
  1789. float *rot)
  1790. {
  1791. struct matrix4 transform;
  1792. struct matrix4 mat;
  1793. struct vec4 x_base;
  1794. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  1795. matrix4_copy(&transform, &group->draw_transform);
  1796. transform_val(pos, &transform);
  1797. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1798. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  1799. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  1800. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  1801. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1802. matrix4_mul(&mat, &mat, &transform);
  1803. scale->x = vec4_len(&mat.x);
  1804. scale->y = vec4_len(&mat.y);
  1805. *rot += group->rot;
  1806. }
  1807. static void remove_group_transform(obs_sceneitem_t *item)
  1808. {
  1809. obs_scene_t *parent = item->parent;
  1810. if (!parent || !parent->group_sceneitem)
  1811. return;
  1812. obs_sceneitem_t *group = parent->group_sceneitem;
  1813. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  1814. update_item_transform(item);
  1815. }
  1816. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  1817. {
  1818. struct matrix4 transform;
  1819. struct matrix4 mat;
  1820. struct vec4 x_base;
  1821. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  1822. matrix4_inv(&transform, &group->draw_transform);
  1823. transform_val(&item->pos, &transform);
  1824. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1825. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  1826. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  1827. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  1828. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1829. matrix4_mul(&mat, &mat, &transform);
  1830. item->scale.x = vec4_len(&mat.x);
  1831. item->scale.y = vec4_len(&mat.y);
  1832. item->rot -= group->rot;
  1833. update_item_transform(item);
  1834. }
  1835. /* assumes group scene and parent scene is locked */
  1836. static void resize_group(obs_sceneitem_t *group)
  1837. {
  1838. obs_scene_t *scene = group->source->context.data;
  1839. struct vec2 minv;
  1840. struct vec2 maxv;
  1841. struct vec2 scale;
  1842. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  1843. return;
  1844. vec2_set(&minv, M_INFINITE, M_INFINITE);
  1845. vec2_set(&maxv, -M_INFINITE, -M_INFINITE);
  1846. obs_sceneitem_t *item = scene->first_item;
  1847. if (!item) {
  1848. scene->cx = 0;
  1849. scene->cy = 0;
  1850. return;
  1851. }
  1852. while (item) {
  1853. #define get_min_max(x_val, y_val) \
  1854. do { \
  1855. struct vec3 v; \
  1856. vec3_set(&v, x_val, y_val, 0.0f); \
  1857. vec3_transform(&v, &v, &item->box_transform); \
  1858. if (v.x < minv.x) minv.x = v.x; \
  1859. if (v.y < minv.y) minv.y = v.y; \
  1860. if (v.x > maxv.x) maxv.x = v.x; \
  1861. if (v.y > maxv.y) maxv.y = v.y; \
  1862. } while (false)
  1863. get_min_max(0.0f, 0.0f);
  1864. get_min_max(1.0f, 0.0f);
  1865. get_min_max(0.0f, 1.0f);
  1866. get_min_max(1.0f, 1.0f);
  1867. #undef get_min_max
  1868. item = item->next;
  1869. }
  1870. item = scene->first_item;
  1871. while (item) {
  1872. vec2_sub(&item->pos, &item->pos, &minv);
  1873. update_item_transform(item);
  1874. item = item->next;
  1875. }
  1876. vec2_sub(&scale, &maxv, &minv);
  1877. scene->cx = (uint32_t)ceilf(scale.x);
  1878. scene->cy = (uint32_t)ceilf(scale.y);
  1879. if (group->bounds_type == OBS_BOUNDS_NONE) {
  1880. struct vec2 new_pos;
  1881. if ((group->align & OBS_ALIGN_LEFT) != 0)
  1882. new_pos.x = minv.x;
  1883. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  1884. new_pos.x = maxv.x;
  1885. else
  1886. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  1887. if ((group->align & OBS_ALIGN_TOP) != 0)
  1888. new_pos.y = minv.y;
  1889. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  1890. new_pos.y = maxv.y;
  1891. else
  1892. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  1893. transform_val(&new_pos, &group->draw_transform);
  1894. vec2_copy(&group->pos, &new_pos);
  1895. }
  1896. os_atomic_set_bool(&group->update_group_resize, false);
  1897. update_item_transform(group);
  1898. }
  1899. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  1900. {
  1901. return obs_scene_insert_group(scene, name, NULL, 0);
  1902. }
  1903. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene,
  1904. const char *name, obs_sceneitem_t **items, size_t count)
  1905. {
  1906. if (!scene)
  1907. return NULL;
  1908. /* don't allow groups or sub-items of other groups */
  1909. for (size_t i = count; i > 0; i--) {
  1910. obs_sceneitem_t *item = items[i - 1];
  1911. if (item->parent != scene || item->is_group)
  1912. return NULL;
  1913. }
  1914. obs_scene_t *sub_scene = obs_scene_create_private(name);
  1915. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  1916. obs_sceneitem_t *item = obs_scene_add_internal(
  1917. scene, sub_scene->source, last_item, true);
  1918. sub_scene->group_sceneitem = item;
  1919. sub_scene->custom_size = true;
  1920. obs_scene_release(sub_scene);
  1921. if (!items || !count)
  1922. return item;
  1923. /* ------------------------- */
  1924. full_lock(scene);
  1925. full_lock(sub_scene);
  1926. sub_scene->first_item = items ? items[0] : NULL;
  1927. for (size_t i = count; i > 0; i--) {
  1928. size_t idx = i - 1;
  1929. remove_group_transform(items[idx]);
  1930. detach_sceneitem(items[idx]);
  1931. }
  1932. for (size_t i = 0; i < count; i++) {
  1933. size_t idx = i;
  1934. if (idx != (count - 1)) {
  1935. size_t next_idx = idx + 1;
  1936. items[idx]->next = items[next_idx];
  1937. items[next_idx]->prev = items[idx];
  1938. } else {
  1939. items[idx]->next = NULL;
  1940. }
  1941. items[idx]->parent = sub_scene;
  1942. apply_group_transform(items[idx], item);
  1943. }
  1944. items[0]->prev = NULL;
  1945. resize_group(item);
  1946. full_unlock(sub_scene);
  1947. full_unlock(scene);
  1948. /* ------------------------- */
  1949. return item;
  1950. }
  1951. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  1952. {
  1953. if (!scene || !name || !*name) {
  1954. return NULL;
  1955. }
  1956. obs_sceneitem_t *group = NULL;
  1957. obs_sceneitem_t *item;
  1958. full_lock(scene);
  1959. item = scene->first_item;
  1960. while (item) {
  1961. if (item->is_group && item->source->context.name) {
  1962. if (strcmp(item->source->context.name, name) == 0) {
  1963. group = item;
  1964. break;
  1965. }
  1966. }
  1967. item = item->next;
  1968. }
  1969. full_unlock(scene);
  1970. return group;
  1971. }
  1972. obs_sceneitem_t *obs_sceneitem_group_from_scene(obs_scene_t *scene)
  1973. {
  1974. return scene ? scene->group_sceneitem : NULL;
  1975. }
  1976. obs_sceneitem_t *obs_sceneitem_group_from_source(obs_source_t *source)
  1977. {
  1978. obs_scene_t *scene = obs_scene_from_source(source);
  1979. return obs_sceneitem_group_from_scene(scene);
  1980. }
  1981. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  1982. {
  1983. return item && item->is_group;
  1984. }
  1985. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  1986. {
  1987. return (item && item->is_group) ? item->source->context.data : NULL;
  1988. }
  1989. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  1990. {
  1991. if (!item || !item->is_group)
  1992. return;
  1993. obs_scene_t *scene = item->parent;
  1994. obs_scene_t *subscene = item->source->context.data;
  1995. obs_sceneitem_t *first;
  1996. obs_sceneitem_t *last;
  1997. /* ------------------------- */
  1998. full_lock(subscene);
  1999. first = subscene->first_item;
  2000. last = first;
  2001. while (last) {
  2002. remove_group_transform(last);
  2003. last->parent = scene;
  2004. if (!last->next)
  2005. break;
  2006. last = last->next;
  2007. }
  2008. subscene->first_item = NULL;
  2009. full_unlock(subscene);
  2010. /* ------------------------- */
  2011. full_lock(scene);
  2012. if (last) {
  2013. if (item->prev) {
  2014. first->prev = item->prev;
  2015. item->prev->next = first;
  2016. } else {
  2017. scene->first_item = first;
  2018. first->prev = NULL;
  2019. }
  2020. last->next = item->next;
  2021. if (last->next)
  2022. last->next->prev = last;
  2023. item->next = item->prev = NULL;
  2024. item->parent = NULL;
  2025. } else {
  2026. detach_sceneitem(item);
  2027. }
  2028. full_unlock(scene);
  2029. /* ------------------------- */
  2030. obs_sceneitem_release(item);
  2031. }
  2032. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2033. {
  2034. if (!group || !group->is_group || !item)
  2035. return;
  2036. obs_scene_t *scene = group->parent;
  2037. obs_scene_t *groupscene = group->source->context.data;
  2038. obs_sceneitem_t *last;
  2039. if (item->parent != scene)
  2040. return;
  2041. /* ------------------------- */
  2042. full_lock(scene);
  2043. remove_group_transform(item);
  2044. detach_sceneitem(item);
  2045. /* ------------------------- */
  2046. full_lock(groupscene);
  2047. last = groupscene->first_item;
  2048. if (last) {
  2049. for (;;) {
  2050. if (!last->next)
  2051. break;
  2052. last = last->next;
  2053. }
  2054. last->next = item;
  2055. item->prev = last;
  2056. } else {
  2057. groupscene->first_item = item;
  2058. }
  2059. item->parent = groupscene;
  2060. item->next = NULL;
  2061. apply_group_transform(item, group);
  2062. resize_group(group);
  2063. full_unlock(groupscene);
  2064. /* ------------------------- */
  2065. full_unlock(scene);
  2066. }
  2067. void obs_sceneitem_group_remove_item(obs_sceneitem_t *item)
  2068. {
  2069. if (!item)
  2070. return;
  2071. obs_scene_t *groupscene = item->parent;
  2072. obs_sceneitem_t *groupitem = groupscene->group_sceneitem;
  2073. if (!groupitem)
  2074. return;
  2075. obs_scene_t *scene = groupitem->parent;
  2076. /* ------------------------- */
  2077. full_lock(scene);
  2078. full_lock(groupscene);
  2079. remove_group_transform(item);
  2080. detach_sceneitem(item);
  2081. /* ------------------------- */
  2082. if (groupitem->prev) {
  2083. groupitem->prev->next = item;
  2084. item->prev = groupitem->prev;
  2085. } else {
  2086. scene->first_item = item;
  2087. item->prev = NULL;
  2088. }
  2089. groupitem->prev = item;
  2090. item->next = groupitem;
  2091. item->parent = scene;
  2092. /* ------------------------- */
  2093. resize_group(groupitem);
  2094. full_unlock(groupscene);
  2095. full_unlock(scene);
  2096. }
  2097. static void build_current_order_info(obs_scene_t *scene,
  2098. struct obs_sceneitem_order_info **items_out,
  2099. size_t *size_out)
  2100. {
  2101. DARRAY(struct obs_sceneitem_order_info) items;
  2102. da_init(items);
  2103. obs_sceneitem_t *item = scene->first_item;
  2104. while (item) {
  2105. da_push_back(items, &item);
  2106. if (item->is_group) {
  2107. obs_scene_t *sub_scene = item->source->context.data;
  2108. full_lock(sub_scene);
  2109. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2110. while (sub_item) {
  2111. da_push_back(items, &item);
  2112. sub_item = sub_item->next;
  2113. }
  2114. full_unlock(sub_scene);
  2115. }
  2116. item = item->next;
  2117. }
  2118. *items_out = items.array;
  2119. *size_out = items.num;
  2120. }
  2121. static bool sceneitems_match2(obs_scene_t *scene,
  2122. struct obs_sceneitem_order_info *items, size_t size)
  2123. {
  2124. struct obs_sceneitem_order_info *cur_items;
  2125. size_t cur_size;
  2126. build_current_order_info(scene, &cur_items, &cur_size);
  2127. if (cur_size != size) {
  2128. bfree(cur_items);
  2129. return false;
  2130. }
  2131. for (size_t i = 0; i < size; i++) {
  2132. struct obs_sceneitem_order_info *new = &items[i];
  2133. struct obs_sceneitem_order_info *old = &cur_items[i];
  2134. if (new->group != old->group || new->item != old->item) {
  2135. bfree(cur_items);
  2136. return false;
  2137. }
  2138. }
  2139. bfree(cur_items);
  2140. return true;
  2141. }
  2142. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2143. struct obs_sceneitem_order_info *item_order,
  2144. size_t item_order_size)
  2145. {
  2146. if (!scene || !item_order_size || !item_order)
  2147. return false;
  2148. obs_scene_addref(scene);
  2149. full_lock(scene);
  2150. bool order_matches = true;
  2151. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2152. full_unlock(scene);
  2153. obs_scene_release(scene);
  2154. return false;
  2155. }
  2156. for (size_t i = 0; i < item_order_size; i++) {
  2157. struct obs_sceneitem_order_info *info = &item_order[i];
  2158. if (!info->item->is_group) {
  2159. remove_group_transform(info->item);
  2160. }
  2161. }
  2162. scene->first_item = item_order[0].item;
  2163. obs_sceneitem_t *prev = NULL;
  2164. for (size_t i = 0; i < item_order_size; i++) {
  2165. struct obs_sceneitem_order_info *info = &item_order[i];
  2166. obs_sceneitem_t *item = info->item;
  2167. if (info->item->is_group) {
  2168. obs_sceneitem_t *sub_prev = NULL;
  2169. obs_scene_t *sub_scene =
  2170. info->item->source->context.data;
  2171. sub_scene->first_item = NULL;
  2172. obs_scene_addref(sub_scene);
  2173. full_lock(sub_scene);
  2174. for (i++; i < item_order_size; i++) {
  2175. struct obs_sceneitem_order_info *sub_info =
  2176. &item_order[i];
  2177. obs_sceneitem_t *sub_item = sub_info->item;
  2178. if (sub_info->group != info->item) {
  2179. i--;
  2180. break;
  2181. }
  2182. if (!sub_scene->first_item)
  2183. sub_scene->first_item = sub_item;
  2184. sub_item->prev = sub_prev;
  2185. sub_item->next = NULL;
  2186. sub_item->parent = sub_scene;
  2187. if (sub_prev)
  2188. sub_prev->next = sub_item;
  2189. apply_group_transform(sub_info->item,
  2190. sub_info->group);
  2191. sub_prev = sub_item;
  2192. }
  2193. resize_group(info->item);
  2194. full_unlock(sub_scene);
  2195. obs_scene_release(sub_scene);
  2196. }
  2197. item->prev = prev;
  2198. item->next = NULL;
  2199. item->parent = scene;
  2200. if (prev)
  2201. prev->next = item;
  2202. prev = item;
  2203. }
  2204. full_unlock(scene);
  2205. signal_reorder(scene->first_item);
  2206. obs_scene_release(scene);
  2207. return true;
  2208. }
  2209. obs_sceneitem_t *obs_sceneitem_get_group(obs_sceneitem_t *item)
  2210. {
  2211. return item && item->parent ? item->parent->group_sceneitem : NULL;
  2212. }
  2213. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  2214. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  2215. void *param)
  2216. {
  2217. if (!group)
  2218. return;
  2219. obs_scene_t *scene = obs_scene_from_source(group->source);
  2220. if (scene)
  2221. obs_scene_enum_items(scene, callback, param);
  2222. }