obs-scene.c 69 KB

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