1
0

obs-scene.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  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, obs_sceneitem_t *group_sceneitem)
  432. {
  433. struct obs_scene_item *item = scene->first_item;
  434. bool rebuild_group = group_sceneitem &&
  435. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  436. while (item) {
  437. if (obs_source_removed(item->source)) {
  438. struct obs_scene_item *del_item = item;
  439. item = item->next;
  440. remove_without_release(del_item);
  441. darray_push_back(sizeof(struct obs_scene_item*),
  442. remove_items, &del_item);
  443. rebuild_group = true;
  444. continue;
  445. }
  446. if (item->is_group) {
  447. obs_scene_t *group_scene = item->source->context.data;
  448. video_lock(group_scene);
  449. update_transforms_and_prune_sources(group_scene,
  450. remove_items, item);
  451. video_unlock(group_scene);
  452. }
  453. if (os_atomic_load_bool(&item->update_transform) ||
  454. source_size_changed(item)) {
  455. update_item_transform(item);
  456. rebuild_group = true;
  457. }
  458. item = item->next;
  459. }
  460. if (rebuild_group && group_sceneitem)
  461. resize_group(group_sceneitem);
  462. }
  463. static void scene_video_render(void *data, gs_effect_t *effect)
  464. {
  465. DARRAY(struct obs_scene_item*) remove_items;
  466. struct obs_scene *scene = data;
  467. struct obs_scene_item *item;
  468. da_init(remove_items);
  469. video_lock(scene);
  470. if (!scene->is_group) {
  471. update_transforms_and_prune_sources(scene, &remove_items.da,
  472. NULL);
  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)->is_group = true;
  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,
  624. struct obs_scene_item *backup_group)
  625. {
  626. obs_data_t *item_data = obs_data_create();
  627. const char *name = obs_source_get_name(item->source);
  628. const char *scale_filter;
  629. struct vec2 pos = item->pos;
  630. struct vec2 scale = item->scale;
  631. float rot = item->rot;
  632. if (backup_group) {
  633. get_ungrouped_transform(backup_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", !!backup_group);
  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, item);
  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, NULL);
  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. }
  1347. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1348. {
  1349. struct calldata params;
  1350. uint8_t stack[128];
  1351. const char *command = select ? "item_select" : "item_deselect";
  1352. if (!item || item->selected == select || !item->parent)
  1353. return;
  1354. item->selected = select;
  1355. calldata_init_fixed(&params, stack, sizeof(stack));
  1356. calldata_set_ptr(&params, "item", item);
  1357. signal_parent(item->parent, command, &params);
  1358. }
  1359. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1360. {
  1361. return item ? item->selected : false;
  1362. }
  1363. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1364. {
  1365. if (item) {
  1366. vec2_copy(&item->pos, pos);
  1367. os_atomic_set_bool(&item->update_transform, true);
  1368. }
  1369. }
  1370. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1371. {
  1372. if (item) {
  1373. item->rot = rot;
  1374. os_atomic_set_bool(&item->update_transform, true);
  1375. }
  1376. }
  1377. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1378. {
  1379. if (item) {
  1380. vec2_copy(&item->scale, scale);
  1381. os_atomic_set_bool(&item->update_transform, true);
  1382. }
  1383. }
  1384. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1385. {
  1386. if (item) {
  1387. item->align = alignment;
  1388. os_atomic_set_bool(&item->update_transform, true);
  1389. }
  1390. }
  1391. static inline void signal_reorder(struct obs_scene_item *item)
  1392. {
  1393. const char *command = NULL;
  1394. struct calldata params;
  1395. uint8_t stack[128];
  1396. command = "reorder";
  1397. calldata_init_fixed(&params, stack, sizeof(stack));
  1398. signal_parent(item->parent, command, &params);
  1399. }
  1400. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1401. enum obs_order_movement movement)
  1402. {
  1403. if (!item) return;
  1404. struct obs_scene_item *next, *prev;
  1405. struct obs_scene *scene = item->parent;
  1406. obs_scene_addref(scene);
  1407. full_lock(scene);
  1408. next = item->next;
  1409. prev = item->prev;
  1410. detach_sceneitem(item);
  1411. if (movement == OBS_ORDER_MOVE_DOWN) {
  1412. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1413. } else if (movement == OBS_ORDER_MOVE_UP) {
  1414. attach_sceneitem(scene, item, next ? next : prev);
  1415. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1416. struct obs_scene_item *last = next;
  1417. if (!last) {
  1418. last = prev;
  1419. } else {
  1420. while (last->next)
  1421. last = last->next;
  1422. }
  1423. attach_sceneitem(scene, item, last);
  1424. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1425. attach_sceneitem(scene, item, NULL);
  1426. }
  1427. full_unlock(scene);
  1428. signal_reorder(item);
  1429. obs_scene_release(scene);
  1430. }
  1431. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1432. int position)
  1433. {
  1434. if (!item) return;
  1435. struct obs_scene *scene = item->parent;
  1436. struct obs_scene_item *next;
  1437. obs_scene_addref(scene);
  1438. full_lock(scene);
  1439. detach_sceneitem(item);
  1440. next = scene->first_item;
  1441. if (position == 0) {
  1442. attach_sceneitem(scene, item, NULL);
  1443. } else {
  1444. for (int i = position; i > 1; --i) {
  1445. if (next->next == NULL)
  1446. break;
  1447. next = next->next;
  1448. }
  1449. attach_sceneitem(scene, item, next);
  1450. }
  1451. full_unlock(scene);
  1452. signal_reorder(item);
  1453. obs_scene_release(scene);
  1454. }
  1455. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1456. enum obs_bounds_type type)
  1457. {
  1458. if (item) {
  1459. item->bounds_type = type;
  1460. os_atomic_set_bool(&item->update_transform, true);
  1461. }
  1462. }
  1463. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1464. uint32_t alignment)
  1465. {
  1466. if (item) {
  1467. item->bounds_align = alignment;
  1468. os_atomic_set_bool(&item->update_transform, true);
  1469. }
  1470. }
  1471. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1472. {
  1473. if (item) {
  1474. item->bounds = *bounds;
  1475. os_atomic_set_bool(&item->update_transform, true);
  1476. }
  1477. }
  1478. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1479. {
  1480. if (item)
  1481. vec2_copy(pos, &item->pos);
  1482. }
  1483. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1484. {
  1485. return item ? item->rot : 0.0f;
  1486. }
  1487. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1488. {
  1489. if (item)
  1490. vec2_copy(scale, &item->scale);
  1491. }
  1492. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1493. {
  1494. return item ? item->align : 0;
  1495. }
  1496. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1497. {
  1498. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1499. }
  1500. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1501. {
  1502. return item ? item->bounds_align : 0;
  1503. }
  1504. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1505. {
  1506. if (item)
  1507. *bounds = item->bounds;
  1508. }
  1509. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1510. struct obs_transform_info *info)
  1511. {
  1512. if (item && info) {
  1513. info->pos = item->pos;
  1514. info->rot = item->rot;
  1515. info->scale = item->scale;
  1516. info->alignment = item->align;
  1517. info->bounds_type = item->bounds_type;
  1518. info->bounds_alignment = item->bounds_align;
  1519. info->bounds = item->bounds;
  1520. }
  1521. }
  1522. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1523. const struct obs_transform_info *info)
  1524. {
  1525. if (item && info) {
  1526. item->pos = info->pos;
  1527. item->rot = info->rot;
  1528. item->scale = info->scale;
  1529. item->align = info->alignment;
  1530. item->bounds_type = info->bounds_type;
  1531. item->bounds_align = info->bounds_alignment;
  1532. item->bounds = info->bounds;
  1533. os_atomic_set_bool(&item->update_transform, true);
  1534. }
  1535. }
  1536. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1537. struct matrix4 *transform)
  1538. {
  1539. if (item)
  1540. matrix4_copy(transform, &item->draw_transform);
  1541. }
  1542. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1543. struct matrix4 *transform)
  1544. {
  1545. if (item)
  1546. matrix4_copy(transform, &item->box_transform);
  1547. }
  1548. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1549. {
  1550. return item ? item->user_visible : false;
  1551. }
  1552. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1553. {
  1554. struct calldata cd;
  1555. uint8_t stack[256];
  1556. struct item_action action = {
  1557. .visible = visible,
  1558. .timestamp = os_gettime_ns()
  1559. };
  1560. if (!item)
  1561. return false;
  1562. if (item->user_visible == visible)
  1563. return false;
  1564. if (!item->parent)
  1565. return false;
  1566. if (visible) {
  1567. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1568. if (!obs_source_add_active_child(item->parent->source,
  1569. item->source)) {
  1570. os_atomic_dec_long(&item->active_refs);
  1571. return false;
  1572. }
  1573. }
  1574. }
  1575. item->user_visible = visible;
  1576. calldata_init_fixed(&cd, stack, sizeof(stack));
  1577. calldata_set_ptr(&cd, "item", item);
  1578. calldata_set_bool(&cd, "visible", visible);
  1579. signal_parent(item->parent, "item_visible", &cd);
  1580. if (source_has_audio(item->source)) {
  1581. pthread_mutex_lock(&item->actions_mutex);
  1582. da_push_back(item->audio_actions, &action);
  1583. pthread_mutex_unlock(&item->actions_mutex);
  1584. } else {
  1585. set_visibility(item, visible);
  1586. }
  1587. return true;
  1588. }
  1589. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  1590. {
  1591. return item ? item->locked : false;
  1592. }
  1593. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  1594. {
  1595. if (!item)
  1596. return false;
  1597. if (item->locked == lock)
  1598. return false;
  1599. if (!item->parent)
  1600. return false;
  1601. item->locked = lock;
  1602. return true;
  1603. }
  1604. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1605. size_t size, bool *order_matches)
  1606. {
  1607. obs_sceneitem_t *item = scene->first_item;
  1608. size_t count = 0;
  1609. while (item) {
  1610. bool found = false;
  1611. for (size_t i = 0; i < size; i++) {
  1612. if (items[i] != item)
  1613. continue;
  1614. if (count != i)
  1615. *order_matches = false;
  1616. found = true;
  1617. break;
  1618. }
  1619. if (!found)
  1620. return false;
  1621. item = item->next;
  1622. count += 1;
  1623. }
  1624. return count == size;
  1625. }
  1626. bool obs_scene_reorder_items(obs_scene_t *scene,
  1627. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1628. {
  1629. if (!scene || !item_order_size)
  1630. return false;
  1631. obs_scene_addref(scene);
  1632. full_lock(scene);
  1633. bool order_matches = true;
  1634. if (!sceneitems_match(scene, item_order, item_order_size,
  1635. &order_matches) || order_matches) {
  1636. full_unlock(scene);
  1637. obs_scene_release(scene);
  1638. return false;
  1639. }
  1640. scene->first_item = item_order[0];
  1641. obs_sceneitem_t *prev = NULL;
  1642. for (size_t i = 0; i < item_order_size; i++) {
  1643. item_order[i]->prev = prev;
  1644. item_order[i]->next = NULL;
  1645. if (prev)
  1646. prev->next = item_order[i];
  1647. prev = item_order[i];
  1648. }
  1649. full_unlock(scene);
  1650. signal_reorder(scene->first_item);
  1651. obs_scene_release(scene);
  1652. return true;
  1653. }
  1654. void obs_scene_atomic_update(obs_scene_t *scene,
  1655. obs_scene_atomic_update_func func, void *data)
  1656. {
  1657. if (!scene)
  1658. return;
  1659. obs_scene_addref(scene);
  1660. full_lock(scene);
  1661. func(data, scene);
  1662. full_unlock(scene);
  1663. obs_scene_release(scene);
  1664. }
  1665. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  1666. const struct obs_sceneitem_crop *crop2)
  1667. {
  1668. return crop1->left == crop2->left &&
  1669. crop1->right == crop2->right &&
  1670. crop1->top == crop2->top &&
  1671. crop1->bottom == crop2->bottom;
  1672. }
  1673. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  1674. const struct obs_sceneitem_crop *crop)
  1675. {
  1676. bool item_tex_now_enabled;
  1677. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  1678. return;
  1679. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  1680. return;
  1681. if (crop_equal(crop, &item->crop))
  1682. return;
  1683. item_tex_now_enabled = crop_enabled(crop) ||
  1684. scale_filter_enabled(item) || item_is_scene(item);
  1685. obs_enter_graphics();
  1686. if (!item_tex_now_enabled) {
  1687. gs_texrender_destroy(item->item_render);
  1688. item->item_render = NULL;
  1689. } else if (!item->item_render) {
  1690. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1691. }
  1692. memcpy(&item->crop, crop, sizeof(*crop));
  1693. if (item->crop.left < 0) item->crop.left = 0;
  1694. if (item->crop.right < 0) item->crop.right = 0;
  1695. if (item->crop.top < 0) item->crop.top = 0;
  1696. if (item->crop.bottom < 0) item->crop.bottom = 0;
  1697. obs_leave_graphics();
  1698. os_atomic_set_bool(&item->update_transform, true);
  1699. }
  1700. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  1701. struct obs_sceneitem_crop *crop)
  1702. {
  1703. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  1704. return;
  1705. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  1706. return;
  1707. memcpy(crop, &item->crop, sizeof(*crop));
  1708. }
  1709. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  1710. enum obs_scale_type filter)
  1711. {
  1712. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  1713. return;
  1714. item->scale_filter = filter;
  1715. obs_enter_graphics();
  1716. if (!item_texture_enabled(item)) {
  1717. gs_texrender_destroy(item->item_render);
  1718. item->item_render = NULL;
  1719. } else if (!item->item_render) {
  1720. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1721. }
  1722. obs_leave_graphics();
  1723. os_atomic_set_bool(&item->update_transform, true);
  1724. }
  1725. enum obs_scale_type obs_sceneitem_get_scale_filter(
  1726. obs_sceneitem_t *item)
  1727. {
  1728. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter") ?
  1729. item->scale_filter : OBS_SCALE_DISABLE;
  1730. }
  1731. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  1732. {
  1733. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  1734. return;
  1735. os_atomic_inc_long(&item->defer_update);
  1736. }
  1737. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  1738. {
  1739. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  1740. return;
  1741. if (os_atomic_dec_long(&item->defer_update) == 0)
  1742. os_atomic_set_bool(&item->update_transform, true);
  1743. }
  1744. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  1745. {
  1746. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  1747. return;
  1748. os_atomic_inc_long(&item->defer_group_resize);
  1749. }
  1750. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  1751. {
  1752. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  1753. return;
  1754. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  1755. os_atomic_set_bool(&item->update_group_resize, true);
  1756. }
  1757. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  1758. {
  1759. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  1760. return 0;
  1761. return item->id;
  1762. }
  1763. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  1764. {
  1765. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  1766. return NULL;
  1767. obs_data_addref(item->private_settings);
  1768. return item->private_settings;
  1769. }
  1770. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  1771. {
  1772. struct vec3 v;
  1773. vec3_set(&v, v2->x, v2->y, 0.0f);
  1774. vec3_transform(&v, &v, transform);
  1775. v2->x = v.x;
  1776. v2->y = v.y;
  1777. }
  1778. static void get_ungrouped_transform(obs_sceneitem_t *group,
  1779. struct vec2 *pos,
  1780. struct vec2 *scale,
  1781. float *rot)
  1782. {
  1783. struct matrix4 transform;
  1784. struct matrix4 mat;
  1785. struct vec4 x_base;
  1786. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  1787. matrix4_copy(&transform, &group->draw_transform);
  1788. transform_val(pos, &transform);
  1789. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1790. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  1791. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  1792. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  1793. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1794. matrix4_mul(&mat, &mat, &transform);
  1795. scale->x = vec4_len(&mat.x);
  1796. scale->y = vec4_len(&mat.y);
  1797. *rot += group->rot;
  1798. }
  1799. static void remove_group_transform(obs_sceneitem_t *group,
  1800. obs_sceneitem_t *item)
  1801. {
  1802. obs_scene_t *parent = item->parent;
  1803. if (!parent || !group)
  1804. return;
  1805. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  1806. update_item_transform(item);
  1807. }
  1808. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  1809. {
  1810. struct matrix4 transform;
  1811. struct matrix4 mat;
  1812. struct vec4 x_base;
  1813. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  1814. matrix4_inv(&transform, &group->draw_transform);
  1815. transform_val(&item->pos, &transform);
  1816. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1817. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  1818. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  1819. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  1820. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  1821. matrix4_mul(&mat, &mat, &transform);
  1822. item->scale.x = vec4_len(&mat.x);
  1823. item->scale.y = vec4_len(&mat.y);
  1824. item->rot -= group->rot;
  1825. update_item_transform(item);
  1826. }
  1827. /* assumes group scene and parent scene is locked */
  1828. static void resize_group(obs_sceneitem_t *group)
  1829. {
  1830. obs_scene_t *scene = group->source->context.data;
  1831. struct vec2 minv;
  1832. struct vec2 maxv;
  1833. struct vec2 scale;
  1834. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  1835. return;
  1836. vec2_set(&minv, M_INFINITE, M_INFINITE);
  1837. vec2_set(&maxv, -M_INFINITE, -M_INFINITE);
  1838. obs_sceneitem_t *item = scene->first_item;
  1839. if (!item) {
  1840. scene->cx = 0;
  1841. scene->cy = 0;
  1842. return;
  1843. }
  1844. while (item) {
  1845. #define get_min_max(x_val, y_val) \
  1846. do { \
  1847. struct vec3 v; \
  1848. vec3_set(&v, x_val, y_val, 0.0f); \
  1849. vec3_transform(&v, &v, &item->box_transform); \
  1850. if (v.x < minv.x) minv.x = v.x; \
  1851. if (v.y < minv.y) minv.y = v.y; \
  1852. if (v.x > maxv.x) maxv.x = v.x; \
  1853. if (v.y > maxv.y) maxv.y = v.y; \
  1854. } while (false)
  1855. get_min_max(0.0f, 0.0f);
  1856. get_min_max(1.0f, 0.0f);
  1857. get_min_max(0.0f, 1.0f);
  1858. get_min_max(1.0f, 1.0f);
  1859. #undef get_min_max
  1860. item = item->next;
  1861. }
  1862. item = scene->first_item;
  1863. while (item) {
  1864. vec2_sub(&item->pos, &item->pos, &minv);
  1865. update_item_transform(item);
  1866. item = item->next;
  1867. }
  1868. vec2_sub(&scale, &maxv, &minv);
  1869. scene->cx = (uint32_t)ceilf(scale.x);
  1870. scene->cy = (uint32_t)ceilf(scale.y);
  1871. if (group->bounds_type == OBS_BOUNDS_NONE) {
  1872. struct vec2 new_pos;
  1873. if ((group->align & OBS_ALIGN_LEFT) != 0)
  1874. new_pos.x = minv.x;
  1875. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  1876. new_pos.x = maxv.x;
  1877. else
  1878. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  1879. if ((group->align & OBS_ALIGN_TOP) != 0)
  1880. new_pos.y = minv.y;
  1881. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  1882. new_pos.y = maxv.y;
  1883. else
  1884. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  1885. transform_val(&new_pos, &group->draw_transform);
  1886. vec2_copy(&group->pos, &new_pos);
  1887. }
  1888. os_atomic_set_bool(&group->update_group_resize, false);
  1889. update_item_transform(group);
  1890. }
  1891. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  1892. {
  1893. return obs_scene_insert_group(scene, name, NULL, 0);
  1894. }
  1895. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene,
  1896. const char *name, obs_sceneitem_t **items, size_t count)
  1897. {
  1898. if (!scene)
  1899. return NULL;
  1900. /* don't allow groups or sub-items of other groups */
  1901. for (size_t i = count; i > 0; i--) {
  1902. obs_sceneitem_t *item = items[i - 1];
  1903. if (item->parent != scene || item->is_group)
  1904. return NULL;
  1905. }
  1906. obs_scene_t *sub_scene = obs_scene_create_private(name);
  1907. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  1908. obs_sceneitem_t *item = obs_scene_add_internal(
  1909. scene, sub_scene->source, last_item, true);
  1910. sub_scene->custom_size = true;
  1911. obs_scene_release(sub_scene);
  1912. if (!items || !count)
  1913. return item;
  1914. /* ------------------------- */
  1915. full_lock(scene);
  1916. full_lock(sub_scene);
  1917. sub_scene->first_item = items ? items[0] : NULL;
  1918. for (size_t i = count; i > 0; i--) {
  1919. size_t idx = i - 1;
  1920. remove_group_transform(item, items[idx]);
  1921. detach_sceneitem(items[idx]);
  1922. }
  1923. for (size_t i = 0; i < count; i++) {
  1924. size_t idx = i;
  1925. if (idx != (count - 1)) {
  1926. size_t next_idx = idx + 1;
  1927. items[idx]->next = items[next_idx];
  1928. items[next_idx]->prev = items[idx];
  1929. } else {
  1930. items[idx]->next = NULL;
  1931. }
  1932. items[idx]->parent = sub_scene;
  1933. apply_group_transform(items[idx], item);
  1934. }
  1935. items[0]->prev = NULL;
  1936. resize_group(item);
  1937. full_unlock(sub_scene);
  1938. full_unlock(scene);
  1939. /* ------------------------- */
  1940. return item;
  1941. }
  1942. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  1943. {
  1944. if (!scene || !name || !*name) {
  1945. return NULL;
  1946. }
  1947. obs_sceneitem_t *group = NULL;
  1948. obs_sceneitem_t *item;
  1949. full_lock(scene);
  1950. item = scene->first_item;
  1951. while (item) {
  1952. if (item->is_group && item->source->context.name) {
  1953. if (strcmp(item->source->context.name, name) == 0) {
  1954. group = item;
  1955. break;
  1956. }
  1957. }
  1958. item = item->next;
  1959. }
  1960. full_unlock(scene);
  1961. return group;
  1962. }
  1963. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  1964. {
  1965. return item && item->is_group;
  1966. }
  1967. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  1968. {
  1969. return (item && item->is_group) ? item->source->context.data : NULL;
  1970. }
  1971. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  1972. {
  1973. if (!item || !item->is_group)
  1974. return;
  1975. obs_scene_t *scene = item->parent;
  1976. obs_scene_t *subscene = item->source->context.data;
  1977. obs_sceneitem_t *first;
  1978. obs_sceneitem_t *last;
  1979. /* ------------------------- */
  1980. full_lock(subscene);
  1981. first = subscene->first_item;
  1982. last = first;
  1983. while (last) {
  1984. remove_group_transform(item, last);
  1985. last->parent = scene;
  1986. if (!last->next)
  1987. break;
  1988. last = last->next;
  1989. }
  1990. subscene->first_item = NULL;
  1991. full_unlock(subscene);
  1992. /* ------------------------- */
  1993. full_lock(scene);
  1994. if (last) {
  1995. if (item->prev) {
  1996. first->prev = item->prev;
  1997. item->prev->next = first;
  1998. } else {
  1999. scene->first_item = first;
  2000. first->prev = NULL;
  2001. }
  2002. last->next = item->next;
  2003. if (last->next)
  2004. last->next->prev = last;
  2005. item->next = item->prev = NULL;
  2006. item->parent = NULL;
  2007. } else {
  2008. detach_sceneitem(item);
  2009. }
  2010. full_unlock(scene);
  2011. /* ------------------------- */
  2012. obs_sceneitem_release(item);
  2013. }
  2014. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2015. {
  2016. if (!group || !group->is_group || !item)
  2017. return;
  2018. obs_scene_t *scene = group->parent;
  2019. obs_scene_t *groupscene = group->source->context.data;
  2020. obs_sceneitem_t *last;
  2021. if (item->parent != scene)
  2022. return;
  2023. /* ------------------------- */
  2024. full_lock(scene);
  2025. remove_group_transform(group, item);
  2026. detach_sceneitem(item);
  2027. /* ------------------------- */
  2028. full_lock(groupscene);
  2029. last = groupscene->first_item;
  2030. if (last) {
  2031. for (;;) {
  2032. if (!last->next)
  2033. break;
  2034. last = last->next;
  2035. }
  2036. last->next = item;
  2037. item->prev = last;
  2038. } else {
  2039. groupscene->first_item = item;
  2040. }
  2041. item->parent = groupscene;
  2042. item->next = NULL;
  2043. apply_group_transform(item, group);
  2044. resize_group(group);
  2045. full_unlock(groupscene);
  2046. /* ------------------------- */
  2047. full_unlock(scene);
  2048. }
  2049. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2050. obs_sceneitem_t *item)
  2051. {
  2052. if (!item || !group || !group->is_group)
  2053. return;
  2054. obs_scene_t *groupscene = item->parent;
  2055. obs_scene_t *scene = group->parent;
  2056. /* ------------------------- */
  2057. full_lock(scene);
  2058. full_lock(groupscene);
  2059. remove_group_transform(group, item);
  2060. detach_sceneitem(item);
  2061. /* ------------------------- */
  2062. if (group->prev) {
  2063. group->prev->next = item;
  2064. item->prev = group->prev;
  2065. } else {
  2066. scene->first_item = item;
  2067. item->prev = NULL;
  2068. }
  2069. group->prev = item;
  2070. item->next = group;
  2071. item->parent = scene;
  2072. /* ------------------------- */
  2073. resize_group(group);
  2074. full_unlock(groupscene);
  2075. full_unlock(scene);
  2076. }
  2077. static void build_current_order_info(obs_scene_t *scene,
  2078. struct obs_sceneitem_order_info **items_out,
  2079. size_t *size_out)
  2080. {
  2081. DARRAY(struct obs_sceneitem_order_info) items;
  2082. da_init(items);
  2083. obs_sceneitem_t *item = scene->first_item;
  2084. while (item) {
  2085. da_push_back(items, &item);
  2086. if (item->is_group) {
  2087. obs_scene_t *sub_scene = item->source->context.data;
  2088. full_lock(sub_scene);
  2089. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2090. while (sub_item) {
  2091. da_push_back(items, &item);
  2092. sub_item = sub_item->next;
  2093. }
  2094. full_unlock(sub_scene);
  2095. }
  2096. item = item->next;
  2097. }
  2098. *items_out = items.array;
  2099. *size_out = items.num;
  2100. }
  2101. static bool sceneitems_match2(obs_scene_t *scene,
  2102. struct obs_sceneitem_order_info *items, size_t size)
  2103. {
  2104. struct obs_sceneitem_order_info *cur_items;
  2105. size_t cur_size;
  2106. build_current_order_info(scene, &cur_items, &cur_size);
  2107. if (cur_size != size) {
  2108. bfree(cur_items);
  2109. return false;
  2110. }
  2111. for (size_t i = 0; i < size; i++) {
  2112. struct obs_sceneitem_order_info *new = &items[i];
  2113. struct obs_sceneitem_order_info *old = &cur_items[i];
  2114. if (new->group != old->group || new->item != old->item) {
  2115. bfree(cur_items);
  2116. return false;
  2117. }
  2118. }
  2119. bfree(cur_items);
  2120. return true;
  2121. }
  2122. static obs_sceneitem_t *get_sceneitem_parent_group(obs_scene_t *scene,
  2123. obs_sceneitem_t *group_subitem)
  2124. {
  2125. if (group_subitem->is_group)
  2126. return NULL;
  2127. obs_sceneitem_t *item = scene->first_item;
  2128. while (item) {
  2129. if (item->is_group &&
  2130. item->source->context.data == group_subitem->parent)
  2131. return item;
  2132. item = item->next;
  2133. }
  2134. return NULL;
  2135. }
  2136. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2137. struct obs_sceneitem_order_info *item_order,
  2138. size_t item_order_size)
  2139. {
  2140. if (!scene || !item_order_size || !item_order)
  2141. return false;
  2142. obs_scene_addref(scene);
  2143. full_lock(scene);
  2144. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2145. full_unlock(scene);
  2146. obs_scene_release(scene);
  2147. return false;
  2148. }
  2149. for (size_t i = 0; i < item_order_size; i++) {
  2150. struct obs_sceneitem_order_info *info = &item_order[i];
  2151. if (!info->item->is_group) {
  2152. obs_sceneitem_t *group =
  2153. get_sceneitem_parent_group(scene, info->item);
  2154. remove_group_transform(group, info->item);
  2155. }
  2156. }
  2157. scene->first_item = item_order[0].item;
  2158. obs_sceneitem_t *prev = NULL;
  2159. for (size_t i = 0; i < item_order_size; i++) {
  2160. struct obs_sceneitem_order_info *info = &item_order[i];
  2161. obs_sceneitem_t *item = info->item;
  2162. if (info->item->is_group) {
  2163. obs_sceneitem_t *sub_prev = NULL;
  2164. obs_scene_t *sub_scene =
  2165. info->item->source->context.data;
  2166. sub_scene->first_item = NULL;
  2167. obs_scene_addref(sub_scene);
  2168. full_lock(sub_scene);
  2169. for (i++; i < item_order_size; i++) {
  2170. struct obs_sceneitem_order_info *sub_info =
  2171. &item_order[i];
  2172. obs_sceneitem_t *sub_item = sub_info->item;
  2173. if (sub_info->group != info->item) {
  2174. i--;
  2175. break;
  2176. }
  2177. if (!sub_scene->first_item)
  2178. sub_scene->first_item = sub_item;
  2179. sub_item->prev = sub_prev;
  2180. sub_item->next = NULL;
  2181. sub_item->parent = sub_scene;
  2182. if (sub_prev)
  2183. sub_prev->next = sub_item;
  2184. apply_group_transform(sub_info->item,
  2185. sub_info->group);
  2186. sub_prev = sub_item;
  2187. }
  2188. resize_group(info->item);
  2189. full_unlock(sub_scene);
  2190. obs_scene_release(sub_scene);
  2191. }
  2192. item->prev = prev;
  2193. item->next = NULL;
  2194. item->parent = scene;
  2195. if (prev)
  2196. prev->next = item;
  2197. prev = item;
  2198. }
  2199. full_unlock(scene);
  2200. signal_reorder(scene->first_item);
  2201. obs_scene_release(scene);
  2202. return true;
  2203. }
  2204. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  2205. obs_sceneitem_t *group_subitem)
  2206. {
  2207. if (!scene || !group_subitem || group_subitem->is_group)
  2208. return NULL;
  2209. full_lock(scene);
  2210. obs_sceneitem_t *group = get_sceneitem_parent_group(scene,
  2211. group_subitem);
  2212. full_unlock(scene);
  2213. return group;
  2214. }
  2215. bool obs_source_is_group(const obs_source_t *source)
  2216. {
  2217. if (!source || source->info.id != scene_info.id)
  2218. return false;
  2219. return ((obs_scene_t *)source->context.data)->is_group;
  2220. }
  2221. bool obs_scene_is_group(const obs_scene_t *scene)
  2222. {
  2223. return scene ? scene->is_group : false;
  2224. }
  2225. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  2226. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  2227. void *param)
  2228. {
  2229. if (!group || !group->is_group)
  2230. return;
  2231. obs_scene_t *scene = obs_scene_from_source(group->source);
  2232. if (scene)
  2233. obs_scene_enum_items(scene, callback, param);
  2234. }