obs-scene.c 68 KB

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