1
0

obs-scene.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 "util/util_uint64.h"
  17. #include "graphics/math-defs.h"
  18. #include "obs-scene.h"
  19. #include "obs-internal.h"
  20. const struct obs_source_info group_info;
  21. static void resize_group(obs_sceneitem_t *group);
  22. static void resize_scene(obs_scene_t *scene);
  23. static void signal_parent(obs_scene_t *parent, const char *name,
  24. calldata_t *params);
  25. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  26. struct vec2 *scale, 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. typedef DARRAY(struct obs_scene_item *) obs_scene_item_ptr_array_t;
  32. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  33. * lock the graphics mutex inside either of the scene mutexes.
  34. *
  35. * Another thing that must be done to prevent that cross-lock (and improve
  36. * performance), is to not create/release/update sources within the scene
  37. * mutexes.
  38. *
  39. * It's okay to lock the graphics mutex before locking either of the scene
  40. * mutexes, but not after.
  41. */
  42. static const char *obs_scene_signals[] = {
  43. "void item_add(ptr scene, ptr item)",
  44. "void item_remove(ptr scene, ptr item)",
  45. "void reorder(ptr scene)",
  46. "void refresh(ptr scene)",
  47. "void item_visible(ptr scene, ptr item, bool visible)",
  48. "void item_select(ptr scene, ptr item)",
  49. "void item_deselect(ptr scene, ptr item)",
  50. "void item_transform(ptr scene, ptr item)",
  51. "void item_locked(ptr scene, ptr item, bool locked)",
  52. NULL,
  53. };
  54. static const struct {
  55. enum gs_blend_type src_color;
  56. enum gs_blend_type src_alpha;
  57. enum gs_blend_type dst_color;
  58. enum gs_blend_type dst_alpha;
  59. enum gs_blend_op_type op;
  60. } obs_blend_mode_params[] = {
  61. /* clang-format off */
  62. // OBS_BLEND_NORMAL
  63. {
  64. GS_BLEND_ONE,
  65. GS_BLEND_ONE,
  66. GS_BLEND_INVSRCALPHA,
  67. GS_BLEND_INVSRCALPHA,
  68. GS_BLEND_OP_ADD,
  69. },
  70. // OBS_BLEND_ADDITIVE
  71. {
  72. GS_BLEND_ONE,
  73. GS_BLEND_ONE,
  74. GS_BLEND_ONE,
  75. GS_BLEND_ONE,
  76. GS_BLEND_OP_ADD,
  77. },
  78. // OBS_BLEND_SUBTRACT
  79. {
  80. GS_BLEND_ONE,
  81. GS_BLEND_ONE,
  82. GS_BLEND_ONE,
  83. GS_BLEND_ONE,
  84. GS_BLEND_OP_REVERSE_SUBTRACT,
  85. },
  86. // OBS_BLEND_SCREEN
  87. {
  88. GS_BLEND_ONE,
  89. GS_BLEND_ONE,
  90. GS_BLEND_INVSRCCOLOR,
  91. GS_BLEND_INVSRCALPHA,
  92. GS_BLEND_OP_ADD
  93. },
  94. // OBS_BLEND_MULTIPLY
  95. {
  96. GS_BLEND_DSTCOLOR,
  97. GS_BLEND_DSTALPHA,
  98. GS_BLEND_INVSRCALPHA,
  99. GS_BLEND_INVSRCALPHA,
  100. GS_BLEND_OP_ADD
  101. },
  102. // OBS_BLEND_LIGHTEN
  103. {
  104. GS_BLEND_ONE,
  105. GS_BLEND_ONE,
  106. GS_BLEND_ONE,
  107. GS_BLEND_ONE,
  108. GS_BLEND_OP_MAX,
  109. },
  110. // OBS_BLEND_DARKEN
  111. {
  112. GS_BLEND_ONE,
  113. GS_BLEND_ONE,
  114. GS_BLEND_ONE,
  115. GS_BLEND_ONE,
  116. GS_BLEND_OP_MIN,
  117. },
  118. /* clang-format on */
  119. };
  120. static inline void signal_item_remove(struct obs_scene_item *item)
  121. {
  122. struct calldata params;
  123. uint8_t stack[128];
  124. calldata_init_fixed(&params, stack, sizeof(stack));
  125. calldata_set_ptr(&params, "item", item);
  126. signal_parent(item->parent, "item_remove", &params);
  127. }
  128. static const char *scene_getname(void *unused)
  129. {
  130. UNUSED_PARAMETER(unused);
  131. return "Scene";
  132. }
  133. static const char *group_getname(void *unused)
  134. {
  135. UNUSED_PARAMETER(unused);
  136. return "Group";
  137. }
  138. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  139. {
  140. struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
  141. scene->source = source;
  142. if (strcmp(source->info.id, group_info.id) == 0) {
  143. scene->is_group = true;
  144. scene->custom_size = true;
  145. scene->cx = 0;
  146. scene->cy = 0;
  147. }
  148. signal_handler_add_array(obs_source_get_signal_handler(source),
  149. obs_scene_signals);
  150. if (pthread_mutex_init_recursive(&scene->audio_mutex) != 0) {
  151. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  152. "mutex");
  153. goto fail;
  154. }
  155. if (pthread_mutex_init_recursive(&scene->video_mutex) != 0) {
  156. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  157. "mutex");
  158. goto fail;
  159. }
  160. UNUSED_PARAMETER(settings);
  161. return scene;
  162. fail:
  163. bfree(scene);
  164. return NULL;
  165. }
  166. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  167. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  168. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  169. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  170. static inline void full_lock(struct obs_scene *scene)
  171. {
  172. video_lock(scene);
  173. audio_lock(scene);
  174. }
  175. static inline void full_unlock(struct obs_scene *scene)
  176. {
  177. audio_unlock(scene);
  178. video_unlock(scene);
  179. }
  180. static void set_visibility(struct obs_scene_item *item, bool vis);
  181. static inline void detach_sceneitem(struct obs_scene_item *item);
  182. static inline void remove_without_release(struct obs_scene_item *item)
  183. {
  184. item->removed = true;
  185. set_visibility(item, false);
  186. signal_item_remove(item);
  187. detach_sceneitem(item);
  188. }
  189. static void remove_all_items(struct obs_scene *scene)
  190. {
  191. struct obs_scene_item *item;
  192. obs_scene_item_ptr_array_t items;
  193. da_init(items);
  194. full_lock(scene);
  195. item = scene->first_item;
  196. while (item) {
  197. struct obs_scene_item *del_item = item;
  198. item = item->next;
  199. remove_without_release(del_item);
  200. da_push_back(items, &del_item);
  201. }
  202. full_unlock(scene);
  203. for (size_t i = 0; i < items.num; i++)
  204. obs_sceneitem_release(items.array[i]);
  205. da_free(items);
  206. }
  207. static void scene_destroy(void *data)
  208. {
  209. struct obs_scene *scene = data;
  210. remove_all_items(scene);
  211. pthread_mutex_destroy(&scene->video_mutex);
  212. pthread_mutex_destroy(&scene->audio_mutex);
  213. bfree(scene);
  214. }
  215. static inline bool transition_active(obs_source_t *transition)
  216. {
  217. return transition && (transition->transitioning_audio ||
  218. transition->transitioning_video);
  219. }
  220. static void scene_enum_sources(void *data, obs_source_enum_proc_t enum_callback,
  221. void *param, bool active)
  222. {
  223. struct obs_scene *scene = data;
  224. struct obs_scene_item *item;
  225. struct obs_scene_item *next;
  226. full_lock(scene);
  227. item = scene->first_item;
  228. while (item) {
  229. next = item->next;
  230. obs_sceneitem_addref(item);
  231. if (active) {
  232. if (item->visible &&
  233. transition_active(item->show_transition))
  234. enum_callback(scene->source,
  235. item->show_transition, param);
  236. else if (!item->visible &&
  237. transition_active(item->hide_transition))
  238. enum_callback(scene->source,
  239. item->hide_transition, param);
  240. else if (os_atomic_load_long(&item->active_refs) > 0)
  241. enum_callback(scene->source, item->source,
  242. param);
  243. } else {
  244. if (item->show_transition)
  245. enum_callback(scene->source,
  246. item->show_transition, param);
  247. if (item->hide_transition)
  248. enum_callback(scene->source,
  249. item->hide_transition, param);
  250. enum_callback(scene->source, item->source, param);
  251. }
  252. obs_sceneitem_release(item);
  253. item = next;
  254. }
  255. full_unlock(scene);
  256. }
  257. static void scene_enum_active_sources(void *data,
  258. obs_source_enum_proc_t enum_callback,
  259. void *param)
  260. {
  261. scene_enum_sources(data, enum_callback, param, true);
  262. }
  263. static void scene_enum_all_sources(void *data,
  264. obs_source_enum_proc_t enum_callback,
  265. void *param)
  266. {
  267. scene_enum_sources(data, enum_callback, param, false);
  268. }
  269. static inline void detach_sceneitem(struct obs_scene_item *item)
  270. {
  271. if (item->prev)
  272. item->prev->next = item->next;
  273. else
  274. item->parent->first_item = item->next;
  275. if (item->next)
  276. item->next->prev = item->prev;
  277. item->parent = NULL;
  278. }
  279. static inline void attach_sceneitem(struct obs_scene *parent,
  280. struct obs_scene_item *item,
  281. struct obs_scene_item *prev)
  282. {
  283. item->prev = prev;
  284. item->parent = parent;
  285. if (prev) {
  286. item->next = prev->next;
  287. if (prev->next)
  288. prev->next->prev = item;
  289. prev->next = item;
  290. } else {
  291. item->next = parent->first_item;
  292. if (parent->first_item)
  293. parent->first_item->prev = item;
  294. parent->first_item = item;
  295. }
  296. }
  297. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  298. {
  299. if (align & OBS_ALIGN_RIGHT)
  300. v->x += (float)cx;
  301. else if ((align & OBS_ALIGN_LEFT) == 0)
  302. v->x += (float)(cx / 2);
  303. if (align & OBS_ALIGN_BOTTOM)
  304. v->y += (float)cy;
  305. else if ((align & OBS_ALIGN_TOP) == 0)
  306. v->y += (float)(cy / 2);
  307. }
  308. static void calculate_bounds_data(struct obs_scene_item *item,
  309. struct vec2 *origin, struct vec2 *scale,
  310. uint32_t *cx, uint32_t *cy)
  311. {
  312. float width = (float)(*cx) * fabsf(scale->x);
  313. float height = (float)(*cy) * fabsf(scale->y);
  314. float item_aspect = width / height;
  315. float bounds_aspect = item->bounds.x / item->bounds.y;
  316. uint32_t bounds_type = item->bounds_type;
  317. float width_diff, height_diff;
  318. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  319. if (width > item->bounds.x || height > item->bounds.y)
  320. bounds_type = OBS_BOUNDS_SCALE_INNER;
  321. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  322. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  323. bool use_width = (bounds_aspect < item_aspect);
  324. float mul;
  325. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  326. use_width = !use_width;
  327. mul = use_width ? item->bounds.x / width
  328. : item->bounds.y / height;
  329. vec2_mulf(scale, scale, mul);
  330. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  331. vec2_mulf(scale, scale, item->bounds.x / width);
  332. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  333. vec2_mulf(scale, scale, item->bounds.y / height);
  334. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  335. scale->x = copysignf(item->bounds.x / (float)(*cx), scale->x);
  336. scale->y = copysignf(item->bounds.y / (float)(*cy), scale->y);
  337. }
  338. width = (float)(*cx) * scale->x;
  339. height = (float)(*cy) * scale->y;
  340. /* Disregards flip when calculating size diff */
  341. width_diff = item->bounds.x - fabsf(width);
  342. height_diff = item->bounds.y - fabsf(height);
  343. *cx = (uint32_t)item->bounds.x;
  344. *cy = (uint32_t)item->bounds.y;
  345. add_alignment(origin, item->bounds_align, (int)-width_diff,
  346. (int)-height_diff);
  347. /* Makes the item stay in-place in the box if flipped */
  348. origin->x += (width < 0.0f) ? width : 0.0f;
  349. origin->y += (height < 0.0f) ? height : 0.0f;
  350. }
  351. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  352. uint32_t width)
  353. {
  354. uint32_t crop_cx = item->crop.left + item->crop.right;
  355. return (crop_cx > width) ? 2 : (width - crop_cx);
  356. }
  357. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  358. uint32_t height)
  359. {
  360. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  361. return (crop_cy > height) ? 2 : (height - crop_cy);
  362. }
  363. static void update_item_transform(struct obs_scene_item *item, bool update_tex)
  364. {
  365. uint32_t width;
  366. uint32_t height;
  367. uint32_t cx;
  368. uint32_t cy;
  369. struct vec2 base_origin;
  370. struct vec2 origin;
  371. struct vec2 scale;
  372. struct calldata params;
  373. uint8_t stack[128];
  374. if (os_atomic_load_long(&item->defer_update) > 0)
  375. return;
  376. width = obs_source_get_width(item->source);
  377. height = obs_source_get_height(item->source);
  378. cx = calc_cx(item, width);
  379. cy = calc_cy(item, height);
  380. scale = item->scale;
  381. item->last_width = width;
  382. item->last_height = height;
  383. width = cx;
  384. height = cy;
  385. vec2_zero(&base_origin);
  386. vec2_zero(&origin);
  387. /* ----------------------- */
  388. if (item->bounds_type != OBS_BOUNDS_NONE) {
  389. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  390. } else {
  391. cx = (uint32_t)((float)cx * scale.x);
  392. cy = (uint32_t)((float)cy * scale.y);
  393. }
  394. add_alignment(&origin, item->align, (int)cx, (int)cy);
  395. matrix4_identity(&item->draw_transform);
  396. matrix4_scale3f(&item->draw_transform, &item->draw_transform, scale.x,
  397. scale.y, 1.0f);
  398. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  399. -origin.x, -origin.y, 0.0f);
  400. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform, 0.0f,
  401. 0.0f, 1.0f, RAD(item->rot));
  402. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  403. item->pos.x, item->pos.y, 0.0f);
  404. item->output_scale = scale;
  405. /* ----------------------- */
  406. if (item->bounds_type != OBS_BOUNDS_NONE) {
  407. vec2_copy(&scale, &item->bounds);
  408. } else {
  409. scale.x = (float)width * item->scale.x;
  410. scale.y = (float)height * item->scale.y;
  411. }
  412. item->box_scale = scale;
  413. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  414. matrix4_identity(&item->box_transform);
  415. matrix4_scale3f(&item->box_transform, &item->box_transform, scale.x,
  416. scale.y, 1.0f);
  417. matrix4_translate3f(&item->box_transform, &item->box_transform,
  418. -base_origin.x, -base_origin.y, 0.0f);
  419. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform, 0.0f,
  420. 0.0f, 1.0f, RAD(item->rot));
  421. matrix4_translate3f(&item->box_transform, &item->box_transform,
  422. item->pos.x, item->pos.y, 0.0f);
  423. /* ----------------------- */
  424. calldata_init_fixed(&params, stack, sizeof(stack));
  425. calldata_set_ptr(&params, "item", item);
  426. signal_parent(item->parent, "item_transform", &params);
  427. if (!update_tex)
  428. return;
  429. os_atomic_set_bool(&item->update_transform, false);
  430. }
  431. static inline bool source_size_changed(struct obs_scene_item *item)
  432. {
  433. uint32_t width = obs_source_get_width(item->source);
  434. uint32_t height = obs_source_get_height(item->source);
  435. return item->last_width != width || item->last_height != height;
  436. }
  437. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  438. {
  439. return crop->left || crop->right || crop->top || crop->bottom;
  440. }
  441. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  442. {
  443. return item->scale_filter != OBS_SCALE_DISABLE;
  444. }
  445. static inline bool default_blending_enabled(const struct obs_scene_item *item)
  446. {
  447. return item->blend_type == OBS_BLEND_NORMAL;
  448. }
  449. static inline bool item_is_scene(const struct obs_scene_item *item)
  450. {
  451. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  452. }
  453. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  454. {
  455. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  456. (item->blend_method == OBS_BLEND_METHOD_SRGB_OFF) ||
  457. !default_blending_enabled(item) ||
  458. (item_is_scene(item) && !item->is_group);
  459. }
  460. static void render_item_texture(struct obs_scene_item *item,
  461. enum gs_color_space current_space,
  462. enum gs_color_space source_space)
  463. {
  464. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  465. if (!tex) {
  466. return;
  467. }
  468. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_ITEM_TEXTURE,
  469. "render_item_texture");
  470. gs_effect_t *effect = obs->video.default_effect;
  471. enum obs_scale_type type = item->scale_filter;
  472. uint32_t cx = gs_texture_get_width(tex);
  473. uint32_t cy = gs_texture_get_height(tex);
  474. bool upscale = false;
  475. if (type != OBS_SCALE_DISABLE) {
  476. if (type == OBS_SCALE_POINT) {
  477. gs_eparam_t *image =
  478. gs_effect_get_param_by_name(effect, "image");
  479. gs_effect_set_next_sampler(image,
  480. obs->video.point_sampler);
  481. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  482. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  483. if (item->output_scale.x < 0.5f ||
  484. item->output_scale.y < 0.5f) {
  485. effect = obs->video.bilinear_lowres_effect;
  486. } else if (type == OBS_SCALE_BICUBIC) {
  487. effect = obs->video.bicubic_effect;
  488. } else if (type == OBS_SCALE_LANCZOS) {
  489. effect = obs->video.lanczos_effect;
  490. } else if (type == OBS_SCALE_AREA) {
  491. effect = obs->video.area_effect;
  492. upscale = (item->output_scale.x >= 1.0f) &&
  493. (item->output_scale.y >= 1.0f);
  494. }
  495. gs_eparam_t *const scale_param =
  496. gs_effect_get_param_by_name(effect,
  497. "base_dimension");
  498. if (scale_param) {
  499. struct vec2 base_res = {(float)cx, (float)cy};
  500. gs_effect_set_vec2(scale_param, &base_res);
  501. }
  502. gs_eparam_t *const scale_i_param =
  503. gs_effect_get_param_by_name(effect,
  504. "base_dimension_i");
  505. if (scale_i_param) {
  506. struct vec2 base_res_i = {1.0f / (float)cx,
  507. 1.0f / (float)cy};
  508. gs_effect_set_vec2(scale_i_param, &base_res_i);
  509. }
  510. }
  511. }
  512. float multiplier = 1.f;
  513. if (current_space == GS_CS_709_SCRGB) {
  514. switch (source_space) {
  515. case GS_CS_SRGB:
  516. case GS_CS_SRGB_16F:
  517. case GS_CS_709_EXTENDED:
  518. multiplier = obs_get_video_sdr_white_level() / 80.f;
  519. break;
  520. case GS_CS_709_SCRGB:
  521. break;
  522. }
  523. }
  524. if (source_space == GS_CS_709_SCRGB) {
  525. switch (current_space) {
  526. case GS_CS_SRGB:
  527. case GS_CS_SRGB_16F:
  528. case GS_CS_709_EXTENDED:
  529. multiplier = 80.f / obs_get_video_sdr_white_level();
  530. break;
  531. case GS_CS_709_SCRGB:
  532. break;
  533. }
  534. }
  535. const char *tech_name = "Draw";
  536. if (upscale) {
  537. tech_name = "DrawUpscale";
  538. switch (source_space) {
  539. case GS_CS_SRGB:
  540. case GS_CS_SRGB_16F:
  541. if (current_space == GS_CS_709_SCRGB)
  542. tech_name = "DrawUpscaleMultiply";
  543. break;
  544. case GS_CS_709_EXTENDED:
  545. if (current_space == GS_CS_SRGB ||
  546. current_space == GS_CS_SRGB_16F)
  547. tech_name = "DrawUpscaleTonemap";
  548. else if (current_space == GS_CS_709_SCRGB)
  549. tech_name = "DrawUpscaleMultiply";
  550. break;
  551. case GS_CS_709_SCRGB:
  552. if (current_space == GS_CS_SRGB ||
  553. current_space == GS_CS_SRGB_16F)
  554. tech_name = "DrawUpscaleMultiplyTonemap";
  555. else if (current_space == GS_CS_709_EXTENDED)
  556. tech_name = "DrawUpscaleMultiply";
  557. break;
  558. }
  559. } else {
  560. switch (source_space) {
  561. case GS_CS_SRGB:
  562. case GS_CS_SRGB_16F:
  563. if (current_space == GS_CS_709_SCRGB)
  564. tech_name = "DrawMultiply";
  565. break;
  566. case GS_CS_709_EXTENDED:
  567. if (current_space == GS_CS_SRGB ||
  568. current_space == GS_CS_SRGB_16F)
  569. tech_name = "DrawTonemap";
  570. else if (current_space == GS_CS_709_SCRGB)
  571. tech_name = "DrawMultiply";
  572. break;
  573. case GS_CS_709_SCRGB:
  574. if (current_space == GS_CS_SRGB ||
  575. current_space == GS_CS_SRGB_16F)
  576. tech_name = "DrawMultiplyTonemap";
  577. else if (current_space == GS_CS_709_EXTENDED)
  578. tech_name = "DrawMultiply";
  579. break;
  580. }
  581. }
  582. gs_eparam_t *const multiplier_param =
  583. gs_effect_get_param_by_name(effect, "multiplier");
  584. if (multiplier_param)
  585. gs_effect_set_float(multiplier_param, multiplier);
  586. gs_blend_state_push();
  587. gs_blend_function_separate(
  588. obs_blend_mode_params[item->blend_type].src_color,
  589. obs_blend_mode_params[item->blend_type].dst_color,
  590. obs_blend_mode_params[item->blend_type].src_alpha,
  591. obs_blend_mode_params[item->blend_type].dst_alpha);
  592. gs_blend_op(obs_blend_mode_params[item->blend_type].op);
  593. while (gs_effect_loop(effect, tech_name))
  594. obs_source_draw(tex, 0, 0, 0, 0, 0);
  595. gs_blend_state_pop();
  596. GS_DEBUG_MARKER_END();
  597. }
  598. static bool are_texcoords_centered(struct matrix4 *m)
  599. {
  600. static const struct matrix4 identity = {
  601. {1.0f, 0.0f, 0.0f, 0.0f},
  602. {0.0f, 1.0f, 0.0f, 0.0f},
  603. {0.0f, 0.0f, 1.0f, 0.0f},
  604. {0.0f, 0.0f, 0.0f, 1.0f},
  605. };
  606. struct matrix4 copy = identity;
  607. copy.t.x = floorf(m->t.x);
  608. copy.t.y = floorf(m->t.y);
  609. return memcmp(m, &copy, sizeof(*m)) == 0;
  610. }
  611. static inline void render_item(struct obs_scene_item *item)
  612. {
  613. GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_ITEM, "Item: %s",
  614. obs_source_get_name(item->source));
  615. const bool use_texrender = item_texture_enabled(item);
  616. obs_source_t *const source = item->source;
  617. const enum gs_color_space current_space = gs_get_color_space();
  618. const enum gs_color_space source_space =
  619. obs_source_get_color_space(source, 1, &current_space);
  620. const enum gs_color_format format =
  621. gs_get_format_from_space(source_space);
  622. if (item->item_render &&
  623. (!use_texrender ||
  624. (gs_texrender_get_format(item->item_render) != format))) {
  625. gs_texrender_destroy(item->item_render);
  626. item->item_render = NULL;
  627. }
  628. if (!item->item_render && use_texrender) {
  629. item->item_render = gs_texrender_create(format, GS_ZS_NONE);
  630. }
  631. if (item->item_render) {
  632. uint32_t width = obs_source_get_width(item->source);
  633. uint32_t height = obs_source_get_height(item->source);
  634. if (!width || !height) {
  635. goto cleanup;
  636. }
  637. uint32_t cx = calc_cx(item, width);
  638. uint32_t cy = calc_cy(item, height);
  639. if (cx && cy &&
  640. gs_texrender_begin_with_color_space(item->item_render, cx,
  641. cy, source_space)) {
  642. float cx_scale = (float)width / (float)cx;
  643. float cy_scale = (float)height / (float)cy;
  644. struct vec4 clear_color;
  645. vec4_zero(&clear_color);
  646. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  647. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  648. -100.0f, 100.0f);
  649. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  650. gs_matrix_translate3f(-(float)item->crop.left,
  651. -(float)item->crop.top, 0.0f);
  652. if (item->user_visible &&
  653. transition_active(item->show_transition)) {
  654. const int cx =
  655. obs_source_get_width(item->source);
  656. const int cy =
  657. obs_source_get_height(item->source);
  658. obs_transition_set_size(item->show_transition,
  659. cx, cy);
  660. obs_source_video_render(item->show_transition);
  661. } else if (!item->user_visible &&
  662. transition_active(item->hide_transition)) {
  663. const int cx =
  664. obs_source_get_width(item->source);
  665. const int cy =
  666. obs_source_get_height(item->source);
  667. obs_transition_set_size(item->hide_transition,
  668. cx, cy);
  669. obs_source_video_render(item->hide_transition);
  670. } else {
  671. obs_source_set_texcoords_centered(item->source,
  672. true);
  673. obs_source_video_render(item->source);
  674. obs_source_set_texcoords_centered(item->source,
  675. false);
  676. }
  677. gs_texrender_end(item->item_render);
  678. }
  679. }
  680. const bool linear_srgb =
  681. !item->item_render ||
  682. (item->blend_method != OBS_BLEND_METHOD_SRGB_OFF);
  683. const bool previous = gs_set_linear_srgb(linear_srgb);
  684. gs_matrix_push();
  685. gs_matrix_mul(&item->draw_transform);
  686. if (item->item_render) {
  687. render_item_texture(item, current_space, source_space);
  688. } else if (item->user_visible &&
  689. transition_active(item->show_transition)) {
  690. const int cx = obs_source_get_width(item->source);
  691. const int cy = obs_source_get_height(item->source);
  692. obs_transition_set_size(item->show_transition, cx, cy);
  693. obs_source_video_render(item->show_transition);
  694. } else if (!item->user_visible &&
  695. transition_active(item->hide_transition)) {
  696. const int cx = obs_source_get_width(item->source);
  697. const int cy = obs_source_get_height(item->source);
  698. obs_transition_set_size(item->hide_transition, cx, cy);
  699. obs_source_video_render(item->hide_transition);
  700. } else {
  701. const bool centered =
  702. are_texcoords_centered(&item->draw_transform);
  703. obs_source_set_texcoords_centered(item->source, centered);
  704. obs_source_video_render(item->source);
  705. obs_source_set_texcoords_centered(item->source, false);
  706. }
  707. gs_matrix_pop();
  708. gs_set_linear_srgb(previous);
  709. cleanup:
  710. GS_DEBUG_MARKER_END();
  711. }
  712. static void scene_video_tick(void *data, float seconds)
  713. {
  714. struct obs_scene *scene = data;
  715. struct obs_scene_item *item;
  716. video_lock(scene);
  717. item = scene->first_item;
  718. while (item) {
  719. if (item->item_render)
  720. gs_texrender_reset(item->item_render);
  721. item = item->next;
  722. }
  723. video_unlock(scene);
  724. UNUSED_PARAMETER(seconds);
  725. }
  726. /* assumes video lock */
  727. static void
  728. update_transforms_and_prune_sources(obs_scene_t *scene,
  729. obs_scene_item_ptr_array_t *remove_items,
  730. obs_sceneitem_t *group_sceneitem)
  731. {
  732. struct obs_scene_item *item = scene->first_item;
  733. bool rebuild_group =
  734. group_sceneitem &&
  735. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  736. while (item) {
  737. if (obs_source_removed(item->source)) {
  738. struct obs_scene_item *del_item = item;
  739. item = item->next;
  740. remove_without_release(del_item);
  741. da_push_back(*remove_items, &del_item);
  742. rebuild_group = true;
  743. continue;
  744. }
  745. if (item->is_group) {
  746. obs_scene_t *group_scene = item->source->context.data;
  747. video_lock(group_scene);
  748. update_transforms_and_prune_sources(group_scene,
  749. remove_items, item);
  750. video_unlock(group_scene);
  751. }
  752. if (os_atomic_load_bool(&item->update_transform) ||
  753. source_size_changed(item)) {
  754. update_item_transform(item, true);
  755. rebuild_group = true;
  756. }
  757. item = item->next;
  758. }
  759. if (rebuild_group && group_sceneitem)
  760. resize_group(group_sceneitem);
  761. }
  762. static void scene_video_render(void *data, gs_effect_t *effect)
  763. {
  764. obs_scene_item_ptr_array_t remove_items;
  765. struct obs_scene *scene = data;
  766. struct obs_scene_item *item;
  767. da_init(remove_items);
  768. video_lock(scene);
  769. if (!scene->is_group) {
  770. update_transforms_and_prune_sources(scene, &remove_items, NULL);
  771. }
  772. gs_blend_state_push();
  773. gs_reset_blend_state();
  774. item = scene->first_item;
  775. while (item) {
  776. if (item->user_visible ||
  777. transition_active(item->hide_transition))
  778. render_item(item);
  779. item = item->next;
  780. }
  781. gs_blend_state_pop();
  782. video_unlock(scene);
  783. for (size_t i = 0; i < remove_items.num; i++)
  784. obs_sceneitem_release(remove_items.array[i]);
  785. da_free(remove_items);
  786. UNUSED_PARAMETER(effect);
  787. }
  788. static void set_visibility(struct obs_scene_item *item, bool vis)
  789. {
  790. pthread_mutex_lock(&item->actions_mutex);
  791. da_resize(item->audio_actions, 0);
  792. if (os_atomic_load_long(&item->active_refs) > 0) {
  793. if (!vis)
  794. obs_source_remove_active_child(item->parent->source,
  795. item->source);
  796. } else if (vis) {
  797. obs_source_add_active_child(item->parent->source, item->source);
  798. }
  799. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  800. item->visible = vis;
  801. item->user_visible = vis;
  802. pthread_mutex_unlock(&item->actions_mutex);
  803. }
  804. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  805. obs_source_t *source,
  806. obs_sceneitem_t *insert_after,
  807. int64_t id);
  808. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  809. {
  810. const char *name = obs_data_get_string(item_data, "name");
  811. const char *src_uuid = obs_data_get_string(item_data, "source_uuid");
  812. obs_source_t *source = NULL;
  813. const char *scale_filter_str;
  814. const char *blend_method_str;
  815. const char *blend_str;
  816. struct obs_scene_item *item;
  817. struct calldata params;
  818. uint8_t stack[128];
  819. bool visible;
  820. bool lock;
  821. if (obs_data_get_bool(item_data, "group_item_backup"))
  822. return;
  823. if (src_uuid && strlen(src_uuid) == UUID_STR_LENGTH)
  824. source = obs_get_source_by_uuid(src_uuid);
  825. /* Fall back to name if UUID was not found or is not set. */
  826. if (!source)
  827. source = obs_get_source_by_name(name);
  828. if (!source) {
  829. blog(LOG_WARNING,
  830. "[scene_load_item] Source %s not "
  831. "found!",
  832. name);
  833. return;
  834. }
  835. item = obs_scene_add_internal(scene, source, NULL,
  836. obs_data_get_int(item_data, "id"));
  837. if (!item) {
  838. blog(LOG_WARNING,
  839. "[scene_load_item] Could not add source '%s' "
  840. "to scene '%s'!",
  841. name, obs_source_get_name(scene->source));
  842. obs_source_release(source);
  843. return;
  844. }
  845. calldata_init_fixed(&params, stack, sizeof(stack));
  846. calldata_set_ptr(&params, "scene", scene);
  847. calldata_set_ptr(&params, "item", item);
  848. signal_handler_signal(scene->source->context.signals, "item_add",
  849. &params);
  850. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  851. obs_data_set_default_int(item_data, "align",
  852. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  853. item->rot = (float)obs_data_get_double(item_data, "rot");
  854. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  855. visible = obs_data_get_bool(item_data, "visible");
  856. lock = obs_data_get_bool(item_data, "locked");
  857. obs_data_get_vec2(item_data, "pos", &item->pos);
  858. obs_data_get_vec2(item_data, "scale", &item->scale);
  859. obs_data_release(item->private_settings);
  860. item->private_settings =
  861. obs_data_get_obj(item_data, "private_settings");
  862. if (!item->private_settings)
  863. item->private_settings = obs_data_create();
  864. set_visibility(item, visible);
  865. obs_sceneitem_set_locked(item, lock);
  866. item->bounds_type = (enum obs_bounds_type)obs_data_get_int(
  867. item_data, "bounds_type");
  868. item->bounds_align =
  869. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  870. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  871. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  872. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  873. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  874. item->crop.bottom =
  875. (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  876. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  877. item->scale_filter = OBS_SCALE_DISABLE;
  878. if (scale_filter_str) {
  879. if (astrcmpi(scale_filter_str, "point") == 0)
  880. item->scale_filter = OBS_SCALE_POINT;
  881. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  882. item->scale_filter = OBS_SCALE_BILINEAR;
  883. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  884. item->scale_filter = OBS_SCALE_BICUBIC;
  885. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  886. item->scale_filter = OBS_SCALE_LANCZOS;
  887. else if (astrcmpi(scale_filter_str, "area") == 0)
  888. item->scale_filter = OBS_SCALE_AREA;
  889. }
  890. blend_method_str = obs_data_get_string(item_data, "blend_method");
  891. item->blend_method = OBS_BLEND_METHOD_DEFAULT;
  892. if (blend_method_str) {
  893. if (astrcmpi(blend_method_str, "srgb_off") == 0)
  894. item->blend_method = OBS_BLEND_METHOD_SRGB_OFF;
  895. }
  896. blend_str = obs_data_get_string(item_data, "blend_type");
  897. item->blend_type = OBS_BLEND_NORMAL;
  898. if (blend_str) {
  899. if (astrcmpi(blend_str, "additive") == 0)
  900. item->blend_type = OBS_BLEND_ADDITIVE;
  901. else if (astrcmpi(blend_str, "subtract") == 0)
  902. item->blend_type = OBS_BLEND_SUBTRACT;
  903. else if (astrcmpi(blend_str, "screen") == 0)
  904. item->blend_type = OBS_BLEND_SCREEN;
  905. else if (astrcmpi(blend_str, "multiply") == 0)
  906. item->blend_type = OBS_BLEND_MULTIPLY;
  907. else if (astrcmpi(blend_str, "lighten") == 0)
  908. item->blend_type = OBS_BLEND_LIGHTEN;
  909. else if (astrcmpi(blend_str, "darken") == 0)
  910. item->blend_type = OBS_BLEND_DARKEN;
  911. }
  912. obs_data_t *show_data = obs_data_get_obj(item_data, "show_transition");
  913. if (show_data) {
  914. obs_sceneitem_transition_load(item, show_data, true);
  915. obs_data_release(show_data);
  916. }
  917. obs_data_t *hide_data = obs_data_get_obj(item_data, "hide_transition");
  918. if (hide_data) {
  919. obs_sceneitem_transition_load(item, hide_data, false);
  920. obs_data_release(hide_data);
  921. }
  922. obs_source_release(source);
  923. update_item_transform(item, false);
  924. }
  925. static void scene_load(void *data, obs_data_t *settings)
  926. {
  927. struct obs_scene *scene = data;
  928. obs_data_array_t *items = obs_data_get_array(settings, "items");
  929. size_t count, i;
  930. remove_all_items(scene);
  931. if (obs_data_get_bool(settings, "custom_size")) {
  932. scene->cx = (uint32_t)obs_data_get_int(settings, "cx");
  933. scene->cy = (uint32_t)obs_data_get_int(settings, "cy");
  934. scene->custom_size = true;
  935. }
  936. if (obs_data_has_user_value(settings, "id_counter"))
  937. scene->id_counter = obs_data_get_int(settings, "id_counter");
  938. if (!items)
  939. return;
  940. count = obs_data_array_count(items);
  941. for (i = 0; i < count; i++) {
  942. obs_data_t *item_data = obs_data_array_item(items, i);
  943. scene_load_item(scene, item_data);
  944. obs_data_release(item_data);
  945. }
  946. obs_data_array_release(items);
  947. }
  948. static void scene_save(void *data, obs_data_t *settings);
  949. static void scene_save_item(obs_data_array_t *array,
  950. struct obs_scene_item *item,
  951. struct obs_scene_item *backup_group)
  952. {
  953. obs_data_t *item_data = obs_data_create();
  954. const char *name = obs_source_get_name(item->source);
  955. const char *src_uuid = obs_source_get_uuid(item->source);
  956. const char *scale_filter;
  957. const char *blend_method;
  958. const char *blend_type;
  959. struct vec2 pos = item->pos;
  960. struct vec2 scale = item->scale;
  961. float rot = item->rot;
  962. if (backup_group) {
  963. get_ungrouped_transform(backup_group, &pos, &scale, &rot);
  964. }
  965. obs_data_set_string(item_data, "name", name);
  966. obs_data_set_string(item_data, "source_uuid", src_uuid);
  967. obs_data_set_bool(item_data, "visible", item->user_visible);
  968. obs_data_set_bool(item_data, "locked", item->locked);
  969. obs_data_set_double(item_data, "rot", rot);
  970. obs_data_set_vec2(item_data, "pos", &pos);
  971. obs_data_set_vec2(item_data, "scale", &scale);
  972. obs_data_set_int(item_data, "align", (int)item->align);
  973. obs_data_set_int(item_data, "bounds_type", (int)item->bounds_type);
  974. obs_data_set_int(item_data, "bounds_align", (int)item->bounds_align);
  975. obs_data_set_vec2(item_data, "bounds", &item->bounds);
  976. obs_data_set_int(item_data, "crop_left", (int)item->crop.left);
  977. obs_data_set_int(item_data, "crop_top", (int)item->crop.top);
  978. obs_data_set_int(item_data, "crop_right", (int)item->crop.right);
  979. obs_data_set_int(item_data, "crop_bottom", (int)item->crop.bottom);
  980. obs_data_set_int(item_data, "id", item->id);
  981. obs_data_set_bool(item_data, "group_item_backup", !!backup_group);
  982. if (item->is_group) {
  983. obs_scene_t *group_scene = item->source->context.data;
  984. obs_sceneitem_t *group_item;
  985. /* save group items as part of main scene, but ignored.
  986. * causes an automatic ungroup if scene collection file
  987. * is loaded in previous versions. */
  988. full_lock(group_scene);
  989. group_item = group_scene->first_item;
  990. while (group_item) {
  991. scene_save_item(array, group_item, item);
  992. group_item = group_item->next;
  993. }
  994. full_unlock(group_scene);
  995. }
  996. if (item->scale_filter == OBS_SCALE_POINT)
  997. scale_filter = "point";
  998. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  999. scale_filter = "bilinear";
  1000. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  1001. scale_filter = "bicubic";
  1002. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  1003. scale_filter = "lanczos";
  1004. else if (item->scale_filter == OBS_SCALE_AREA)
  1005. scale_filter = "area";
  1006. else
  1007. scale_filter = "disable";
  1008. obs_data_set_string(item_data, "scale_filter", scale_filter);
  1009. if (item->blend_method == OBS_BLEND_METHOD_SRGB_OFF)
  1010. blend_method = "srgb_off";
  1011. else
  1012. blend_method = "default";
  1013. obs_data_set_string(item_data, "blend_method", blend_method);
  1014. if (item->blend_type == OBS_BLEND_NORMAL)
  1015. blend_type = "normal";
  1016. else if (item->blend_type == OBS_BLEND_ADDITIVE)
  1017. blend_type = "additive";
  1018. else if (item->blend_type == OBS_BLEND_SUBTRACT)
  1019. blend_type = "subtract";
  1020. else if (item->blend_type == OBS_BLEND_SCREEN)
  1021. blend_type = "screen";
  1022. else if (item->blend_type == OBS_BLEND_MULTIPLY)
  1023. blend_type = "multiply";
  1024. else if (item->blend_type == OBS_BLEND_LIGHTEN)
  1025. blend_type = "lighten";
  1026. else if (item->blend_type == OBS_BLEND_DARKEN)
  1027. blend_type = "darken";
  1028. else
  1029. blend_type = "normal";
  1030. obs_data_set_string(item_data, "blend_type", blend_type);
  1031. obs_data_t *show_data = obs_sceneitem_transition_save(item, true);
  1032. obs_data_set_obj(item_data, "show_transition", show_data);
  1033. obs_data_release(show_data);
  1034. obs_data_t *hide_data = obs_sceneitem_transition_save(item, false);
  1035. obs_data_set_obj(item_data, "hide_transition", hide_data);
  1036. obs_data_release(hide_data);
  1037. obs_data_set_obj(item_data, "private_settings", item->private_settings);
  1038. obs_data_array_push_back(array, item_data);
  1039. obs_data_release(item_data);
  1040. }
  1041. static void scene_save(void *data, obs_data_t *settings)
  1042. {
  1043. struct obs_scene *scene = data;
  1044. obs_data_array_t *array = obs_data_array_create();
  1045. struct obs_scene_item *item;
  1046. full_lock(scene);
  1047. item = scene->first_item;
  1048. while (item) {
  1049. scene_save_item(array, item, NULL);
  1050. item = item->next;
  1051. }
  1052. obs_data_set_int(settings, "id_counter", scene->id_counter);
  1053. obs_data_set_bool(settings, "custom_size", scene->custom_size);
  1054. if (scene->custom_size) {
  1055. obs_data_set_int(settings, "cx", scene->cx);
  1056. obs_data_set_int(settings, "cy", scene->cy);
  1057. }
  1058. full_unlock(scene);
  1059. obs_data_set_array(settings, "items", array);
  1060. obs_data_array_release(array);
  1061. }
  1062. static uint32_t scene_getwidth(void *data)
  1063. {
  1064. obs_scene_t *scene = data;
  1065. if (scene->custom_size)
  1066. return scene->cx;
  1067. if (obs->video.main_mix)
  1068. return obs->video.main_mix->ovi.base_width;
  1069. return 0;
  1070. }
  1071. static uint32_t scene_getheight(void *data)
  1072. {
  1073. obs_scene_t *scene = data;
  1074. if (scene->custom_size)
  1075. return scene->cy;
  1076. if (obs->video.main_mix)
  1077. return obs->video.main_mix->ovi.base_height;
  1078. return 0;
  1079. }
  1080. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  1081. float *buf, uint64_t ts,
  1082. size_t sample_rate)
  1083. {
  1084. bool cur_visible = item->visible;
  1085. uint64_t frame_num = 0;
  1086. size_t deref_count = 0;
  1087. pthread_mutex_lock(&item->actions_mutex);
  1088. for (size_t i = 0; i < item->audio_actions.num; i++) {
  1089. struct item_action action = item->audio_actions.array[i];
  1090. uint64_t timestamp = action.timestamp;
  1091. uint64_t new_frame_num;
  1092. if (timestamp < ts)
  1093. timestamp = ts;
  1094. new_frame_num = util_mul_div64(timestamp - ts, sample_rate,
  1095. 1000000000ULL);
  1096. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  1097. break;
  1098. da_erase(item->audio_actions, i--);
  1099. item->visible = action.visible;
  1100. if (!item->visible)
  1101. deref_count++;
  1102. if (buf && new_frame_num > frame_num) {
  1103. for (; frame_num < new_frame_num; frame_num++)
  1104. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  1105. }
  1106. cur_visible = item->visible;
  1107. }
  1108. if (buf) {
  1109. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  1110. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  1111. }
  1112. pthread_mutex_unlock(&item->actions_mutex);
  1113. while (deref_count--) {
  1114. if (os_atomic_dec_long(&item->active_refs) == 0) {
  1115. obs_source_remove_active_child(item->parent->source,
  1116. item->source);
  1117. }
  1118. }
  1119. }
  1120. static bool apply_scene_item_volume(struct obs_scene_item *item, float *buf,
  1121. uint64_t ts, size_t sample_rate)
  1122. {
  1123. bool actions_pending;
  1124. struct item_action action;
  1125. pthread_mutex_lock(&item->actions_mutex);
  1126. actions_pending = item->audio_actions.num > 0;
  1127. if (actions_pending)
  1128. action = item->audio_actions.array[0];
  1129. pthread_mutex_unlock(&item->actions_mutex);
  1130. if (actions_pending) {
  1131. uint64_t duration = util_mul_div64(AUDIO_OUTPUT_FRAMES,
  1132. 1000000000ULL, sample_rate);
  1133. if (!ts || action.timestamp < (ts + duration)) {
  1134. apply_scene_item_audio_actions(item, buf, ts,
  1135. sample_rate);
  1136. return true;
  1137. }
  1138. }
  1139. return false;
  1140. }
  1141. static void process_all_audio_actions(struct obs_scene_item *item,
  1142. size_t sample_rate)
  1143. {
  1144. while (apply_scene_item_volume(item, NULL, 0, sample_rate))
  1145. ;
  1146. }
  1147. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  1148. size_t pos, size_t count)
  1149. {
  1150. register float *out = p_out + pos;
  1151. register float *buf = buf_in;
  1152. register float *in = p_in;
  1153. register float *end = in + count;
  1154. while (in < end)
  1155. *(out++) += *(in++) * *(buf++);
  1156. }
  1157. static inline void mix_audio(float *p_out, float *p_in, size_t pos,
  1158. size_t count)
  1159. {
  1160. register float *out = p_out + pos;
  1161. register float *in = p_in;
  1162. register float *end = in + count;
  1163. while (in < end)
  1164. *(out++) += *(in++);
  1165. }
  1166. static bool scene_audio_render(void *data, uint64_t *ts_out,
  1167. struct obs_source_audio_mix *audio_output,
  1168. uint32_t mixers, size_t channels,
  1169. size_t sample_rate)
  1170. {
  1171. uint64_t timestamp = 0;
  1172. float buf[AUDIO_OUTPUT_FRAMES];
  1173. struct obs_source_audio_mix child_audio;
  1174. struct obs_scene *scene = data;
  1175. struct obs_scene_item *item;
  1176. audio_lock(scene);
  1177. item = scene->first_item;
  1178. while (item) {
  1179. struct obs_source *source;
  1180. if (item->visible && transition_active(item->show_transition))
  1181. source = item->show_transition;
  1182. else if (!item->visible &&
  1183. transition_active(item->hide_transition))
  1184. source = item->hide_transition;
  1185. else
  1186. source = item->source;
  1187. if (!obs_source_audio_pending(source) &&
  1188. (item->visible ||
  1189. transition_active(item->hide_transition))) {
  1190. uint64_t source_ts =
  1191. obs_source_get_audio_timestamp(source);
  1192. if (source_ts && (!timestamp || source_ts < timestamp))
  1193. timestamp = source_ts;
  1194. }
  1195. item = item->next;
  1196. }
  1197. if (!timestamp) {
  1198. /* just process all pending audio actions if no audio playing,
  1199. * otherwise audio actions will just never be processed */
  1200. item = scene->first_item;
  1201. while (item) {
  1202. process_all_audio_actions(item, sample_rate);
  1203. item = item->next;
  1204. }
  1205. audio_unlock(scene);
  1206. return false;
  1207. }
  1208. item = scene->first_item;
  1209. while (item) {
  1210. uint64_t source_ts;
  1211. size_t pos, count;
  1212. bool apply_buf;
  1213. struct obs_source *source;
  1214. if (item->visible && transition_active(item->show_transition))
  1215. source = item->show_transition;
  1216. else if (!item->visible &&
  1217. transition_active(item->hide_transition))
  1218. source = item->hide_transition;
  1219. else
  1220. source = item->source;
  1221. apply_buf = apply_scene_item_volume(item, buf, timestamp,
  1222. sample_rate);
  1223. if (obs_source_audio_pending(source)) {
  1224. item = item->next;
  1225. continue;
  1226. }
  1227. source_ts = obs_source_get_audio_timestamp(source);
  1228. if (!source_ts) {
  1229. item = item->next;
  1230. continue;
  1231. }
  1232. pos = (size_t)ns_to_audio_frames(sample_rate,
  1233. source_ts - timestamp);
  1234. if (pos >= AUDIO_OUTPUT_FRAMES) {
  1235. item = item->next;
  1236. continue;
  1237. }
  1238. count = AUDIO_OUTPUT_FRAMES - pos;
  1239. if (!apply_buf && !item->visible &&
  1240. !transition_active(item->hide_transition)) {
  1241. item = item->next;
  1242. continue;
  1243. }
  1244. obs_source_get_audio_mix(source, &child_audio);
  1245. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  1246. if ((mixers & (1 << mix)) == 0)
  1247. continue;
  1248. for (size_t ch = 0; ch < channels; ch++) {
  1249. float *out = audio_output->output[mix].data[ch];
  1250. float *in = child_audio.output[mix].data[ch];
  1251. if (apply_buf)
  1252. mix_audio_with_buf(out, in, buf, pos,
  1253. count);
  1254. else
  1255. mix_audio(out, in, pos, count);
  1256. }
  1257. }
  1258. item = item->next;
  1259. }
  1260. *ts_out = timestamp;
  1261. audio_unlock(scene);
  1262. return true;
  1263. }
  1264. enum gs_color_space
  1265. scene_video_get_color_space(void *data, size_t count,
  1266. const enum gs_color_space *preferred_spaces)
  1267. {
  1268. UNUSED_PARAMETER(data);
  1269. UNUSED_PARAMETER(count);
  1270. UNUSED_PARAMETER(preferred_spaces);
  1271. enum gs_color_space space = GS_CS_SRGB;
  1272. struct obs_video_info ovi;
  1273. if (obs_get_video_info(&ovi)) {
  1274. if (ovi.colorspace == VIDEO_CS_2100_PQ ||
  1275. ovi.colorspace == VIDEO_CS_2100_HLG)
  1276. space = GS_CS_709_EXTENDED;
  1277. }
  1278. return space;
  1279. }
  1280. const struct obs_source_info scene_info = {
  1281. .id = "scene",
  1282. .type = OBS_SOURCE_TYPE_SCENE,
  1283. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1284. OBS_SOURCE_COMPOSITE | OBS_SOURCE_DO_NOT_DUPLICATE |
  1285. OBS_SOURCE_SRGB,
  1286. .get_name = scene_getname,
  1287. .create = scene_create,
  1288. .destroy = scene_destroy,
  1289. .video_tick = scene_video_tick,
  1290. .video_render = scene_video_render,
  1291. .audio_render = scene_audio_render,
  1292. .get_width = scene_getwidth,
  1293. .get_height = scene_getheight,
  1294. .load = scene_load,
  1295. .save = scene_save,
  1296. .enum_active_sources = scene_enum_active_sources,
  1297. .enum_all_sources = scene_enum_all_sources,
  1298. .video_get_color_space = scene_video_get_color_space,
  1299. };
  1300. const struct obs_source_info group_info = {
  1301. .id = "group",
  1302. .type = OBS_SOURCE_TYPE_SCENE,
  1303. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1304. OBS_SOURCE_COMPOSITE | OBS_SOURCE_SRGB,
  1305. .get_name = group_getname,
  1306. .create = scene_create,
  1307. .destroy = scene_destroy,
  1308. .video_tick = scene_video_tick,
  1309. .video_render = scene_video_render,
  1310. .audio_render = scene_audio_render,
  1311. .get_width = scene_getwidth,
  1312. .get_height = scene_getheight,
  1313. .load = scene_load,
  1314. .save = scene_save,
  1315. .enum_active_sources = scene_enum_active_sources,
  1316. .enum_all_sources = scene_enum_all_sources,
  1317. .video_get_color_space = scene_video_get_color_space,
  1318. };
  1319. static inline obs_scene_t *create_id(const char *id, const char *name)
  1320. {
  1321. struct obs_source *source = obs_source_create(id, name, NULL, NULL);
  1322. return source->context.data;
  1323. }
  1324. static inline obs_scene_t *create_private_id(const char *id, const char *name)
  1325. {
  1326. struct obs_source *source = obs_source_create_private(id, name, NULL);
  1327. return source->context.data;
  1328. }
  1329. obs_scene_t *obs_scene_create(const char *name)
  1330. {
  1331. return create_id("scene", name);
  1332. }
  1333. obs_scene_t *obs_scene_create_private(const char *name)
  1334. {
  1335. return create_private_id("scene", name);
  1336. }
  1337. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  1338. {
  1339. struct obs_scene_item *item = scene->first_item;
  1340. while (item && idx--)
  1341. item = item->next;
  1342. return item ? item->source : NULL;
  1343. }
  1344. static inline obs_source_t *dup_child(obs_scene_item_ptr_array_t *old_items,
  1345. size_t idx, obs_scene_t *new_scene,
  1346. bool private)
  1347. {
  1348. obs_source_t *source;
  1349. source = old_items->array[idx]->source;
  1350. /* if the old item is referenced more than once in the old scene,
  1351. * make sure they're referenced similarly in the new scene to reduce
  1352. * load times */
  1353. for (size_t i = 0; i < idx; i++) {
  1354. struct obs_scene_item *item = old_items->array[i];
  1355. if (item->source == source) {
  1356. source = get_child_at_idx(new_scene, i);
  1357. return obs_source_get_ref(source);
  1358. }
  1359. }
  1360. return obs_source_duplicate(
  1361. source, private ? obs_source_get_name(source) : NULL, private);
  1362. }
  1363. static inline obs_source_t *new_ref(obs_source_t *source)
  1364. {
  1365. return obs_source_get_ref(source);
  1366. }
  1367. static inline void duplicate_item_data(struct obs_scene_item *dst,
  1368. struct obs_scene_item *src,
  1369. bool defer_texture_update,
  1370. bool duplicate_hotkeys)
  1371. {
  1372. struct obs_scene *dst_scene = dst->parent;
  1373. if (!src->user_visible)
  1374. set_visibility(dst, false);
  1375. dst->selected = src->selected;
  1376. dst->pos = src->pos;
  1377. dst->rot = src->rot;
  1378. dst->scale = src->scale;
  1379. dst->align = src->align;
  1380. dst->last_width = src->last_width;
  1381. dst->last_height = src->last_height;
  1382. dst->output_scale = src->output_scale;
  1383. dst->scale_filter = src->scale_filter;
  1384. dst->blend_method = src->blend_method;
  1385. dst->blend_type = src->blend_type;
  1386. dst->box_transform = src->box_transform;
  1387. dst->box_scale = src->box_scale;
  1388. dst->draw_transform = src->draw_transform;
  1389. dst->bounds_type = src->bounds_type;
  1390. dst->bounds_align = src->bounds_align;
  1391. dst->bounds = src->bounds;
  1392. if (src->show_transition) {
  1393. obs_source_t *transition = obs_source_duplicate(
  1394. src->show_transition,
  1395. obs_source_get_name(src->show_transition), true);
  1396. obs_sceneitem_set_transition(dst, true, transition);
  1397. obs_source_release(transition);
  1398. }
  1399. if (src->hide_transition) {
  1400. obs_source_t *transition = obs_source_duplicate(
  1401. src->hide_transition,
  1402. obs_source_get_name(src->hide_transition), true);
  1403. obs_sceneitem_set_transition(dst, false, transition);
  1404. obs_source_release(transition);
  1405. }
  1406. dst->show_transition_duration = src->show_transition_duration;
  1407. dst->hide_transition_duration = src->hide_transition_duration;
  1408. if (duplicate_hotkeys && !dst_scene->source->context.private) {
  1409. struct dstr show = {0};
  1410. struct dstr hide = {0};
  1411. obs_data_array_t *data0 = NULL;
  1412. obs_data_array_t *data1 = NULL;
  1413. obs_hotkey_pair_save(src->toggle_visibility, &data0, &data1);
  1414. obs_hotkey_pair_load(dst->toggle_visibility, data0, data1);
  1415. /* Fix scene item ID */
  1416. dstr_printf(&show, "libobs.show_scene_item.%" PRIi64, dst->id);
  1417. dstr_printf(&hide, "libobs.hide_scene_item.%" PRIi64, dst->id);
  1418. obs_hotkey_pair_set_names(dst->toggle_visibility, show.array,
  1419. hide.array);
  1420. obs_data_array_release(data0);
  1421. obs_data_array_release(data1);
  1422. dstr_free(&show);
  1423. dstr_free(&hide);
  1424. }
  1425. obs_sceneitem_set_crop(dst, &src->crop);
  1426. obs_sceneitem_set_locked(dst, src->locked);
  1427. if (defer_texture_update) {
  1428. os_atomic_set_bool(&dst->update_transform, true);
  1429. }
  1430. obs_data_apply(dst->private_settings, src->private_settings);
  1431. }
  1432. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  1433. enum obs_scene_duplicate_type type)
  1434. {
  1435. bool make_unique = ((int)type & (1 << 0)) != 0;
  1436. bool make_private = ((int)type & (1 << 1)) != 0;
  1437. obs_scene_item_ptr_array_t items;
  1438. struct obs_scene *new_scene;
  1439. struct obs_scene_item *item;
  1440. struct obs_source *source;
  1441. da_init(items);
  1442. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  1443. return NULL;
  1444. /* --------------------------------- */
  1445. full_lock(scene);
  1446. item = scene->first_item;
  1447. while (item) {
  1448. da_push_back(items, &item);
  1449. obs_sceneitem_addref(item);
  1450. item = item->next;
  1451. }
  1452. full_unlock(scene);
  1453. /* --------------------------------- */
  1454. new_scene = make_private
  1455. ? create_private_id(scene->source->info.id, name)
  1456. : create_id(scene->source->info.id, name);
  1457. obs_source_copy_filters(new_scene->source, scene->source);
  1458. obs_data_apply(new_scene->source->private_settings,
  1459. scene->source->private_settings);
  1460. /* never duplicate sub-items for groups */
  1461. if (scene->is_group)
  1462. make_unique = false;
  1463. for (size_t i = 0; i < items.num; i++) {
  1464. item = items.array[i];
  1465. source = make_unique
  1466. ? dup_child(&items, i, new_scene, make_private)
  1467. : new_ref(item->source);
  1468. if (source) {
  1469. struct obs_scene_item *new_item =
  1470. obs_scene_add(new_scene, source);
  1471. if (!new_item) {
  1472. obs_source_release(source);
  1473. continue;
  1474. }
  1475. duplicate_item_data(new_item, item, false, false);
  1476. obs_source_release(source);
  1477. }
  1478. }
  1479. for (size_t i = 0; i < items.num; i++)
  1480. obs_sceneitem_release(items.array[i]);
  1481. if (new_scene->is_group)
  1482. resize_scene(new_scene);
  1483. da_free(items);
  1484. return new_scene;
  1485. }
  1486. void obs_scene_addref(obs_scene_t *scene)
  1487. {
  1488. if (scene)
  1489. obs_source_addref(scene->source);
  1490. }
  1491. obs_scene_t *obs_scene_get_ref(obs_scene_t *scene)
  1492. {
  1493. if (!scene)
  1494. return NULL;
  1495. if (obs_source_get_ref(scene->source) != NULL)
  1496. return scene;
  1497. return NULL;
  1498. }
  1499. void obs_scene_release(obs_scene_t *scene)
  1500. {
  1501. if (scene)
  1502. obs_source_release(scene->source);
  1503. }
  1504. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  1505. {
  1506. return scene ? scene->source : NULL;
  1507. }
  1508. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  1509. {
  1510. if (!source || strcmp(source->info.id, scene_info.id) != 0)
  1511. return NULL;
  1512. return source->context.data;
  1513. }
  1514. obs_scene_t *obs_group_from_source(const obs_source_t *source)
  1515. {
  1516. if (!source || strcmp(source->info.id, group_info.id) != 0)
  1517. return NULL;
  1518. return source->context.data;
  1519. }
  1520. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  1521. {
  1522. struct obs_scene_item *item;
  1523. if (!scene)
  1524. return NULL;
  1525. full_lock(scene);
  1526. item = scene->first_item;
  1527. while (item) {
  1528. if (strcmp(item->source->context.name, name) == 0)
  1529. break;
  1530. item = item->next;
  1531. }
  1532. full_unlock(scene);
  1533. return item;
  1534. }
  1535. obs_sceneitem_t *obs_scene_find_source_recursive(obs_scene_t *scene,
  1536. const char *name)
  1537. {
  1538. struct obs_scene_item *item;
  1539. if (!scene)
  1540. return NULL;
  1541. full_lock(scene);
  1542. item = scene->first_item;
  1543. while (item) {
  1544. if (strcmp(item->source->context.name, name) == 0)
  1545. break;
  1546. if (item->is_group) {
  1547. obs_scene_t *group = item->source->context.data;
  1548. obs_sceneitem_t *child =
  1549. obs_scene_find_source(group, name);
  1550. if (child) {
  1551. item = child;
  1552. break;
  1553. }
  1554. }
  1555. item = item->next;
  1556. }
  1557. full_unlock(scene);
  1558. return item;
  1559. }
  1560. struct sceneitem_check {
  1561. obs_source_t *source_in;
  1562. obs_sceneitem_t *item_out;
  1563. };
  1564. bool check_sceneitem_exists(obs_scene_t *scene, obs_sceneitem_t *item,
  1565. void *vp_check)
  1566. {
  1567. UNUSED_PARAMETER(scene);
  1568. struct sceneitem_check *check = (struct sceneitem_check *)vp_check;
  1569. if (obs_sceneitem_get_source(item) == check->source_in) {
  1570. check->item_out = item;
  1571. obs_sceneitem_addref(item);
  1572. return false;
  1573. }
  1574. return true;
  1575. }
  1576. obs_sceneitem_t *obs_scene_sceneitem_from_source(obs_scene_t *scene,
  1577. obs_source_t *source)
  1578. {
  1579. struct sceneitem_check check = {source, NULL};
  1580. obs_scene_enum_items(scene, check_sceneitem_exists, (void *)&check);
  1581. return check.item_out;
  1582. }
  1583. obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
  1584. {
  1585. struct obs_scene_item *item;
  1586. if (!scene)
  1587. return NULL;
  1588. full_lock(scene);
  1589. item = scene->first_item;
  1590. while (item) {
  1591. if (item->id == id)
  1592. break;
  1593. item = item->next;
  1594. }
  1595. full_unlock(scene);
  1596. return item;
  1597. }
  1598. void obs_scene_enum_items(obs_scene_t *scene,
  1599. bool (*callback)(obs_scene_t *, obs_sceneitem_t *,
  1600. void *),
  1601. void *param)
  1602. {
  1603. struct obs_scene_item *item;
  1604. if (!scene || !callback)
  1605. return;
  1606. full_lock(scene);
  1607. item = scene->first_item;
  1608. while (item) {
  1609. struct obs_scene_item *next = item->next;
  1610. obs_sceneitem_addref(item);
  1611. if (!callback(scene, item, param)) {
  1612. obs_sceneitem_release(item);
  1613. break;
  1614. }
  1615. obs_sceneitem_release(item);
  1616. item = next;
  1617. }
  1618. full_unlock(scene);
  1619. }
  1620. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  1621. {
  1622. long owners = os_atomic_load_long(&si->ref);
  1623. while (owners > 0) {
  1624. if (os_atomic_compare_exchange_long(&si->ref, &owners,
  1625. owners + 1)) {
  1626. return si;
  1627. }
  1628. }
  1629. return NULL;
  1630. }
  1631. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  1632. obs_hotkey_t *hotkey, bool pressed)
  1633. {
  1634. UNUSED_PARAMETER(id);
  1635. UNUSED_PARAMETER(hotkey);
  1636. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1637. if (pressed && si && !si->user_visible) {
  1638. obs_sceneitem_set_visible(si, true);
  1639. obs_sceneitem_release(si);
  1640. return true;
  1641. }
  1642. obs_sceneitem_release(si);
  1643. return false;
  1644. }
  1645. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  1646. obs_hotkey_t *hotkey, bool pressed)
  1647. {
  1648. UNUSED_PARAMETER(id);
  1649. UNUSED_PARAMETER(hotkey);
  1650. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1651. if (pressed && si && si->user_visible) {
  1652. obs_sceneitem_set_visible(si, false);
  1653. obs_sceneitem_release(si);
  1654. return true;
  1655. }
  1656. obs_sceneitem_release(si);
  1657. return false;
  1658. }
  1659. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  1660. const char *name)
  1661. {
  1662. struct obs_data_array *hotkey_array;
  1663. obs_data_t *hotkey_data = scene->source->context.hotkey_data;
  1664. struct dstr show = {0};
  1665. struct dstr hide = {0};
  1666. struct dstr legacy = {0};
  1667. struct dstr show_desc = {0};
  1668. struct dstr hide_desc = {0};
  1669. dstr_printf(&show, "libobs.show_scene_item.%" PRIi64, item->id);
  1670. dstr_printf(&hide, "libobs.hide_scene_item.%" PRIi64, item->id);
  1671. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1672. dstr_replace(&show_desc, "%1", name);
  1673. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1674. dstr_replace(&hide_desc, "%1", name);
  1675. /* Check if legacy keys exists, migrate if necessary */
  1676. dstr_printf(&legacy, "libobs.show_scene_item.%s", name);
  1677. hotkey_array = obs_data_get_array(hotkey_data, legacy.array);
  1678. if (hotkey_array) {
  1679. obs_data_set_array(hotkey_data, show.array, hotkey_array);
  1680. obs_data_array_release(hotkey_array);
  1681. }
  1682. dstr_printf(&legacy, "libobs.hide_scene_item.%s", name);
  1683. hotkey_array = obs_data_get_array(hotkey_data, legacy.array);
  1684. if (hotkey_array) {
  1685. obs_data_set_array(hotkey_data, hide.array, hotkey_array);
  1686. obs_data_array_release(hotkey_array);
  1687. }
  1688. item->toggle_visibility = obs_hotkey_pair_register_source(
  1689. scene->source, show.array, show_desc.array, hide.array,
  1690. hide_desc.array, hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1691. item, item);
  1692. dstr_free(&show);
  1693. dstr_free(&hide);
  1694. dstr_free(&legacy);
  1695. dstr_free(&show_desc);
  1696. dstr_free(&hide_desc);
  1697. }
  1698. static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
  1699. const char *new_name)
  1700. {
  1701. struct dstr show_desc = {0};
  1702. struct dstr hide_desc = {0};
  1703. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1704. dstr_replace(&show_desc, "%1", new_name);
  1705. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1706. dstr_replace(&hide_desc, "%1", new_name);
  1707. obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
  1708. show_desc.array, hide_desc.array);
  1709. dstr_free(&show_desc);
  1710. dstr_free(&hide_desc);
  1711. }
  1712. static void sceneitem_renamed(void *param, calldata_t *data)
  1713. {
  1714. obs_sceneitem_t *scene_item = param;
  1715. const char *name = calldata_string(data, "new_name");
  1716. sceneitem_rename_hotkey(scene_item, name);
  1717. }
  1718. static inline bool source_has_audio(obs_source_t *source)
  1719. {
  1720. return (source->info.output_flags &
  1721. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1722. }
  1723. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  1724. obs_source_t *source,
  1725. obs_sceneitem_t *insert_after,
  1726. int64_t id)
  1727. {
  1728. struct obs_scene_item *last;
  1729. struct obs_scene_item *item;
  1730. pthread_mutex_t mutex;
  1731. struct item_action action = {.visible = true,
  1732. .timestamp = os_gettime_ns()};
  1733. if (!scene)
  1734. return NULL;
  1735. source = obs_source_get_ref(source);
  1736. if (!source) {
  1737. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1738. return NULL;
  1739. }
  1740. if (source->removed) {
  1741. blog(LOG_WARNING, "Tried to add a removed source to a scene");
  1742. goto release_source_and_fail;
  1743. }
  1744. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1745. blog(LOG_WARNING, "Failed to create scene item mutex");
  1746. goto release_source_and_fail;
  1747. }
  1748. if (!obs_source_add_active_child(scene->source, source)) {
  1749. blog(LOG_WARNING, "Failed to add source to scene due to "
  1750. "infinite source recursion");
  1751. pthread_mutex_destroy(&mutex);
  1752. goto release_source_and_fail;
  1753. }
  1754. item = bzalloc(sizeof(struct obs_scene_item));
  1755. item->source = source;
  1756. item->id = id ? id : ++scene->id_counter;
  1757. item->parent = scene;
  1758. item->ref = 1;
  1759. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1760. item->actions_mutex = mutex;
  1761. item->user_visible = true;
  1762. item->locked = false;
  1763. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  1764. item->private_settings = obs_data_create();
  1765. item->toggle_visibility = OBS_INVALID_HOTKEY_PAIR_ID;
  1766. os_atomic_set_long(&item->active_refs, 1);
  1767. vec2_set(&item->scale, 1.0f, 1.0f);
  1768. matrix4_identity(&item->draw_transform);
  1769. matrix4_identity(&item->box_transform);
  1770. if (source_has_audio(source)) {
  1771. item->visible = false;
  1772. da_push_back(item->audio_actions, &action);
  1773. } else {
  1774. item->visible = true;
  1775. }
  1776. full_lock(scene);
  1777. if (insert_after) {
  1778. obs_sceneitem_t *next = insert_after->next;
  1779. if (next)
  1780. next->prev = item;
  1781. item->next = insert_after->next;
  1782. item->prev = insert_after;
  1783. insert_after->next = item;
  1784. } else {
  1785. last = scene->first_item;
  1786. if (!last) {
  1787. scene->first_item = item;
  1788. } else {
  1789. while (last->next)
  1790. last = last->next;
  1791. last->next = item;
  1792. item->prev = last;
  1793. }
  1794. }
  1795. full_unlock(scene);
  1796. if (!scene->source->context.private)
  1797. init_hotkeys(scene, item, obs_source_get_name(source));
  1798. signal_handler_connect(obs_source_get_signal_handler(source), "rename",
  1799. sceneitem_renamed, item);
  1800. return item;
  1801. release_source_and_fail:
  1802. obs_source_release(source);
  1803. return NULL;
  1804. }
  1805. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1806. {
  1807. obs_sceneitem_t *item = obs_scene_add_internal(scene, source, NULL, 0);
  1808. struct calldata params;
  1809. uint8_t stack[128];
  1810. if (!item)
  1811. return NULL;
  1812. calldata_init_fixed(&params, stack, sizeof(stack));
  1813. calldata_set_ptr(&params, "scene", scene);
  1814. calldata_set_ptr(&params, "item", item);
  1815. signal_handler_signal(scene->source->context.signals, "item_add",
  1816. &params);
  1817. return item;
  1818. }
  1819. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  1820. {
  1821. if (item) {
  1822. if (item->item_render) {
  1823. obs_enter_graphics();
  1824. gs_texrender_destroy(item->item_render);
  1825. obs_leave_graphics();
  1826. }
  1827. obs_data_release(item->private_settings);
  1828. obs_hotkey_pair_unregister(item->toggle_visibility);
  1829. pthread_mutex_destroy(&item->actions_mutex);
  1830. signal_handler_disconnect(
  1831. obs_source_get_signal_handler(item->source), "rename",
  1832. sceneitem_renamed, item);
  1833. if (item->show_transition)
  1834. obs_source_release(item->show_transition);
  1835. if (item->hide_transition)
  1836. obs_source_release(item->hide_transition);
  1837. if (item->source)
  1838. obs_source_release(item->source);
  1839. da_free(item->audio_actions);
  1840. bfree(item);
  1841. }
  1842. }
  1843. void obs_sceneitem_addref(obs_sceneitem_t *item)
  1844. {
  1845. if (item)
  1846. os_atomic_inc_long(&item->ref);
  1847. }
  1848. void obs_sceneitem_release(obs_sceneitem_t *item)
  1849. {
  1850. if (!item)
  1851. return;
  1852. if (os_atomic_dec_long(&item->ref) == 0)
  1853. obs_sceneitem_destroy(item);
  1854. }
  1855. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1856. {
  1857. obs_scene_t *scene;
  1858. if (!item || item->removed)
  1859. return;
  1860. scene = item->parent;
  1861. full_lock(scene);
  1862. item->removed = true;
  1863. assert(scene != NULL);
  1864. assert(scene->source != NULL);
  1865. set_visibility(item, false);
  1866. signal_item_remove(item);
  1867. detach_sceneitem(item);
  1868. full_unlock(scene);
  1869. obs_sceneitem_set_transition(item, true, NULL);
  1870. obs_sceneitem_set_transition(item, false, NULL);
  1871. obs_sceneitem_release(item);
  1872. }
  1873. void obs_sceneitem_save(obs_sceneitem_t *item, obs_data_array_t *arr)
  1874. {
  1875. scene_save_item(arr, item, NULL);
  1876. }
  1877. void sceneitem_restore(obs_data_t *data, void *vp)
  1878. {
  1879. obs_scene_t *scene = (obs_scene_t *)vp;
  1880. scene_load_item(scene, data);
  1881. }
  1882. void obs_sceneitems_add(obs_scene_t *scene, obs_data_array_t *data)
  1883. {
  1884. obs_data_array_enum(data, sceneitem_restore, scene);
  1885. }
  1886. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1887. {
  1888. return item ? item->parent : NULL;
  1889. }
  1890. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1891. {
  1892. return item ? item->source : NULL;
  1893. }
  1894. static void signal_parent(obs_scene_t *parent, const char *command,
  1895. calldata_t *params)
  1896. {
  1897. calldata_set_ptr(params, "scene", parent);
  1898. signal_handler_signal(parent->source->context.signals, command, params);
  1899. }
  1900. struct passthrough {
  1901. obs_data_array_t *ids;
  1902. obs_data_array_t *scenes_and_groups;
  1903. bool all_items;
  1904. };
  1905. bool save_transform_states(obs_scene_t *scene, obs_sceneitem_t *item,
  1906. void *vp_pass)
  1907. {
  1908. struct passthrough *pass = (struct passthrough *)vp_pass;
  1909. if (obs_sceneitem_selected(item) || pass->all_items) {
  1910. obs_data_t *temp = obs_data_create();
  1911. obs_data_array_t *item_ids = (obs_data_array_t *)pass->ids;
  1912. struct obs_transform_info info;
  1913. struct obs_sceneitem_crop crop;
  1914. obs_sceneitem_get_info(item, &info);
  1915. obs_sceneitem_get_crop(item, &crop);
  1916. struct vec2 pos = info.pos;
  1917. struct vec2 scale = info.scale;
  1918. float rot = info.rot;
  1919. uint32_t alignment = info.alignment;
  1920. uint32_t bounds_type = info.bounds_type;
  1921. uint32_t bounds_alignment = info.bounds_alignment;
  1922. struct vec2 bounds = info.bounds;
  1923. obs_data_set_int(temp, "id", obs_sceneitem_get_id(item));
  1924. obs_data_set_vec2(temp, "pos", &pos);
  1925. obs_data_set_vec2(temp, "scale", &scale);
  1926. obs_data_set_double(temp, "rot", rot);
  1927. obs_data_set_int(temp, "alignment", alignment);
  1928. obs_data_set_int(temp, "bounds_type", bounds_type);
  1929. obs_data_set_vec2(temp, "bounds", &bounds);
  1930. obs_data_set_int(temp, "bounds_alignment", bounds_alignment);
  1931. obs_data_set_int(temp, "top", crop.top);
  1932. obs_data_set_int(temp, "bottom", crop.bottom);
  1933. obs_data_set_int(temp, "left", crop.left);
  1934. obs_data_set_int(temp, "right", crop.right);
  1935. obs_data_array_push_back(item_ids, temp);
  1936. obs_data_release(temp);
  1937. }
  1938. obs_source_t *item_source = obs_sceneitem_get_source(item);
  1939. if (obs_source_is_group(item_source)) {
  1940. obs_data_t *temp = obs_data_create();
  1941. obs_data_array_t *nids = obs_data_array_create();
  1942. obs_data_set_string(temp, "scene_name",
  1943. obs_source_get_name(item_source));
  1944. obs_data_set_bool(temp, "is_group", true);
  1945. obs_data_set_string(
  1946. temp, "group_parent",
  1947. obs_source_get_name(obs_scene_get_source(scene)));
  1948. struct passthrough npass = {nids, pass->scenes_and_groups,
  1949. pass->all_items};
  1950. obs_sceneitem_group_enum_items(item, save_transform_states,
  1951. (void *)&npass);
  1952. obs_data_set_array(temp, "items", nids);
  1953. obs_data_array_push_back(pass->scenes_and_groups, temp);
  1954. obs_data_release(temp);
  1955. obs_data_array_release(nids);
  1956. }
  1957. return true;
  1958. }
  1959. obs_data_t *obs_scene_save_transform_states(obs_scene_t *scene, bool all_items)
  1960. {
  1961. obs_data_t *wrapper = obs_data_create();
  1962. obs_data_array_t *scenes_and_groups = obs_data_array_create();
  1963. obs_data_array_t *item_ids = obs_data_array_create();
  1964. struct passthrough pass = {item_ids, scenes_and_groups, all_items};
  1965. obs_data_t *temp = obs_data_create();
  1966. obs_data_set_string(temp, "scene_name",
  1967. obs_source_get_name(obs_scene_get_source(scene)));
  1968. obs_data_set_string(temp, "scene_uuid",
  1969. obs_source_get_uuid(obs_scene_get_source(scene)));
  1970. obs_data_set_bool(temp, "is_group", false);
  1971. obs_scene_enum_items(scene, save_transform_states, (void *)&pass);
  1972. obs_data_set_array(temp, "items", item_ids);
  1973. obs_data_array_push_back(scenes_and_groups, temp);
  1974. obs_data_set_array(wrapper, "scenes_and_groups", scenes_and_groups);
  1975. obs_data_array_release(item_ids);
  1976. obs_data_array_release(scenes_and_groups);
  1977. obs_data_release(temp);
  1978. return wrapper;
  1979. }
  1980. void load_transform_states(obs_data_t *temp, void *vp_scene)
  1981. {
  1982. obs_scene_t *scene = (obs_scene_t *)vp_scene;
  1983. int64_t id = obs_data_get_int(temp, "id");
  1984. obs_sceneitem_t *item = obs_scene_find_sceneitem_by_id(scene, id);
  1985. struct obs_transform_info info;
  1986. struct obs_sceneitem_crop crop;
  1987. obs_data_get_vec2(temp, "pos", &info.pos);
  1988. obs_data_get_vec2(temp, "scale", &info.scale);
  1989. info.rot = (float)obs_data_get_double(temp, "rot");
  1990. info.alignment = (uint32_t)obs_data_get_int(temp, "alignment");
  1991. info.bounds_type =
  1992. (enum obs_bounds_type)obs_data_get_int(temp, "bounds_type");
  1993. info.bounds_alignment =
  1994. (uint32_t)obs_data_get_int(temp, "bounds_alignment");
  1995. obs_data_get_vec2(temp, "bounds", &info.bounds);
  1996. crop.top = (int)obs_data_get_int(temp, "top");
  1997. crop.bottom = (int)obs_data_get_int(temp, "bottom");
  1998. crop.left = (int)obs_data_get_int(temp, "left");
  1999. crop.right = (int)obs_data_get_int(temp, "right");
  2000. obs_sceneitem_defer_update_begin(item);
  2001. obs_sceneitem_set_info(item, &info);
  2002. obs_sceneitem_set_crop(item, &crop);
  2003. obs_sceneitem_defer_update_end(item);
  2004. }
  2005. void iterate_scenes_and_groups_transform_states(obs_data_t *data, void *vp)
  2006. {
  2007. obs_data_array_t *items = obs_data_get_array(data, "items");
  2008. obs_source_t *scene_source =
  2009. obs_get_source_by_name(obs_data_get_string(data, "scene_name"));
  2010. obs_scene_t *scene = obs_scene_from_source(scene_source);
  2011. if (obs_data_get_bool(data, "is_group")) {
  2012. obs_source_t *parent_source = obs_get_source_by_name(
  2013. obs_data_get_string(data, "group_parent"));
  2014. obs_scene_t *parent = obs_scene_from_source(parent_source);
  2015. obs_sceneitem_t *group = obs_scene_get_group(
  2016. parent, obs_data_get_string(data, "scene_name"));
  2017. scene = obs_sceneitem_group_get_scene(group);
  2018. obs_source_release(parent_source);
  2019. }
  2020. obs_data_array_enum(items, load_transform_states, (void *)scene);
  2021. UNUSED_PARAMETER(vp);
  2022. obs_data_array_release(items);
  2023. obs_source_release(scene_source);
  2024. }
  2025. void obs_scene_load_transform_states(const char *data)
  2026. {
  2027. obs_data_t *dat = obs_data_create_from_json(data);
  2028. obs_data_array_t *scenes_and_groups =
  2029. obs_data_get_array(dat, "scenes_and_groups");
  2030. obs_data_array_enum(scenes_and_groups,
  2031. iterate_scenes_and_groups_transform_states, NULL);
  2032. obs_data_release(dat);
  2033. obs_data_array_release(scenes_and_groups);
  2034. }
  2035. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  2036. {
  2037. struct calldata params;
  2038. uint8_t stack[128];
  2039. const char *command = select ? "item_select" : "item_deselect";
  2040. if (!item || item->selected == select || !item->parent)
  2041. return;
  2042. item->selected = select;
  2043. calldata_init_fixed(&params, stack, sizeof(stack));
  2044. calldata_set_ptr(&params, "item", item);
  2045. signal_parent(item->parent, command, &params);
  2046. }
  2047. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  2048. {
  2049. return item ? item->selected : false;
  2050. }
  2051. #define do_update_transform(item) \
  2052. do { \
  2053. if (!item->parent || item->parent->is_group) \
  2054. os_atomic_set_bool(&item->update_transform, true); \
  2055. else \
  2056. update_item_transform(item, false); \
  2057. } while (false)
  2058. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  2059. {
  2060. if (item) {
  2061. vec2_copy(&item->pos, pos);
  2062. do_update_transform(item);
  2063. }
  2064. }
  2065. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  2066. {
  2067. if (item) {
  2068. item->rot = rot;
  2069. do_update_transform(item);
  2070. }
  2071. }
  2072. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  2073. {
  2074. if (item) {
  2075. vec2_copy(&item->scale, scale);
  2076. do_update_transform(item);
  2077. }
  2078. }
  2079. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  2080. {
  2081. if (item) {
  2082. item->align = alignment;
  2083. do_update_transform(item);
  2084. }
  2085. }
  2086. static inline void signal_reorder(struct obs_scene_item *item)
  2087. {
  2088. const char *command = NULL;
  2089. struct calldata params;
  2090. uint8_t stack[128];
  2091. command = "reorder";
  2092. calldata_init_fixed(&params, stack, sizeof(stack));
  2093. signal_parent(item->parent, command, &params);
  2094. }
  2095. static inline void signal_refresh(obs_scene_t *scene)
  2096. {
  2097. const char *command = NULL;
  2098. struct calldata params;
  2099. uint8_t stack[128];
  2100. command = "refresh";
  2101. calldata_init_fixed(&params, stack, sizeof(stack));
  2102. signal_parent(scene, command, &params);
  2103. }
  2104. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  2105. enum obs_order_movement movement)
  2106. {
  2107. if (!item)
  2108. return;
  2109. struct obs_scene_item *next, *prev;
  2110. struct obs_scene *scene = obs_scene_get_ref(item->parent);
  2111. if (!scene)
  2112. return;
  2113. full_lock(scene);
  2114. next = item->next;
  2115. prev = item->prev;
  2116. detach_sceneitem(item);
  2117. if (movement == OBS_ORDER_MOVE_DOWN) {
  2118. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  2119. } else if (movement == OBS_ORDER_MOVE_UP) {
  2120. attach_sceneitem(scene, item, next ? next : prev);
  2121. } else if (movement == OBS_ORDER_MOVE_TOP) {
  2122. struct obs_scene_item *last = next;
  2123. if (!last) {
  2124. last = prev;
  2125. } else {
  2126. while (last->next)
  2127. last = last->next;
  2128. }
  2129. attach_sceneitem(scene, item, last);
  2130. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  2131. attach_sceneitem(scene, item, NULL);
  2132. }
  2133. full_unlock(scene);
  2134. signal_reorder(item);
  2135. obs_scene_release(scene);
  2136. }
  2137. int obs_sceneitem_get_order_position(obs_sceneitem_t *item)
  2138. {
  2139. struct obs_scene *scene = item->parent;
  2140. struct obs_scene_item *next = scene->first_item;
  2141. full_lock(scene);
  2142. int index = 0;
  2143. while (next && next != item) {
  2144. next = next->next;
  2145. ++index;
  2146. }
  2147. full_unlock(scene);
  2148. return index;
  2149. }
  2150. void obs_sceneitem_set_order_position(obs_sceneitem_t *item, int position)
  2151. {
  2152. if (!item)
  2153. return;
  2154. struct obs_scene *scene = obs_scene_get_ref(item->parent);
  2155. struct obs_scene_item *next;
  2156. if (!scene)
  2157. return;
  2158. full_lock(scene);
  2159. detach_sceneitem(item);
  2160. next = scene->first_item;
  2161. if (position == 0) {
  2162. attach_sceneitem(scene, item, NULL);
  2163. } else {
  2164. for (int i = position; i > 1; --i) {
  2165. if (next->next == NULL)
  2166. break;
  2167. next = next->next;
  2168. }
  2169. attach_sceneitem(scene, item, next);
  2170. }
  2171. full_unlock(scene);
  2172. signal_reorder(item);
  2173. obs_scene_release(scene);
  2174. }
  2175. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  2176. enum obs_bounds_type type)
  2177. {
  2178. if (item) {
  2179. item->bounds_type = type;
  2180. do_update_transform(item);
  2181. }
  2182. }
  2183. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  2184. uint32_t alignment)
  2185. {
  2186. if (item) {
  2187. item->bounds_align = alignment;
  2188. do_update_transform(item);
  2189. }
  2190. }
  2191. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  2192. {
  2193. if (item) {
  2194. item->bounds = *bounds;
  2195. do_update_transform(item);
  2196. }
  2197. }
  2198. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  2199. {
  2200. if (item)
  2201. vec2_copy(pos, &item->pos);
  2202. }
  2203. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  2204. {
  2205. return item ? item->rot : 0.0f;
  2206. }
  2207. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  2208. {
  2209. if (item)
  2210. vec2_copy(scale, &item->scale);
  2211. }
  2212. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  2213. {
  2214. return item ? item->align : 0;
  2215. }
  2216. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  2217. {
  2218. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  2219. }
  2220. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  2221. {
  2222. return item ? item->bounds_align : 0;
  2223. }
  2224. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  2225. {
  2226. if (item)
  2227. *bounds = item->bounds;
  2228. }
  2229. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  2230. struct obs_transform_info *info)
  2231. {
  2232. if (item && info) {
  2233. info->pos = item->pos;
  2234. info->rot = item->rot;
  2235. info->scale = item->scale;
  2236. info->alignment = item->align;
  2237. info->bounds_type = item->bounds_type;
  2238. info->bounds_alignment = item->bounds_align;
  2239. info->bounds = item->bounds;
  2240. }
  2241. }
  2242. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  2243. const struct obs_transform_info *info)
  2244. {
  2245. if (item && info) {
  2246. item->pos = info->pos;
  2247. item->rot = info->rot;
  2248. if (isfinite(info->scale.x) && isfinite(info->scale.y)) {
  2249. item->scale = info->scale;
  2250. }
  2251. item->align = info->alignment;
  2252. item->bounds_type = info->bounds_type;
  2253. item->bounds_align = info->bounds_alignment;
  2254. item->bounds = info->bounds;
  2255. do_update_transform(item);
  2256. }
  2257. }
  2258. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  2259. struct matrix4 *transform)
  2260. {
  2261. if (item)
  2262. matrix4_copy(transform, &item->draw_transform);
  2263. }
  2264. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  2265. struct matrix4 *transform)
  2266. {
  2267. if (item)
  2268. matrix4_copy(transform, &item->box_transform);
  2269. }
  2270. void obs_sceneitem_get_box_scale(const obs_sceneitem_t *item,
  2271. struct vec2 *scale)
  2272. {
  2273. if (item)
  2274. *scale = item->box_scale;
  2275. }
  2276. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  2277. {
  2278. return item ? item->user_visible : false;
  2279. }
  2280. static bool group_item_transition(obs_scene_t *scene, obs_sceneitem_t *item,
  2281. void *param)
  2282. {
  2283. if (!param || !item)
  2284. return true;
  2285. const bool visible = *(bool *)param;
  2286. if (obs_sceneitem_visible(item))
  2287. obs_sceneitem_do_transition(item, visible);
  2288. UNUSED_PARAMETER(scene);
  2289. return true;
  2290. }
  2291. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  2292. {
  2293. struct calldata cd;
  2294. uint8_t stack[256];
  2295. struct item_action action = {.visible = visible,
  2296. .timestamp = os_gettime_ns()};
  2297. if (!item)
  2298. return false;
  2299. if (item->user_visible == visible)
  2300. return false;
  2301. if (!item->parent)
  2302. return false;
  2303. obs_sceneitem_do_transition(item, visible);
  2304. if (obs_sceneitem_is_group(item))
  2305. obs_sceneitem_group_enum_items(item, group_item_transition,
  2306. &visible);
  2307. item->user_visible = visible;
  2308. if (visible) {
  2309. if (os_atomic_inc_long(&item->active_refs) == 1) {
  2310. if (!obs_source_add_active_child(item->parent->source,
  2311. item->source)) {
  2312. os_atomic_dec_long(&item->active_refs);
  2313. return false;
  2314. }
  2315. }
  2316. }
  2317. calldata_init_fixed(&cd, stack, sizeof(stack));
  2318. calldata_set_ptr(&cd, "item", item);
  2319. calldata_set_bool(&cd, "visible", visible);
  2320. signal_parent(item->parent, "item_visible", &cd);
  2321. if (source_has_audio(item->source)) {
  2322. pthread_mutex_lock(&item->actions_mutex);
  2323. da_push_back(item->audio_actions, &action);
  2324. pthread_mutex_unlock(&item->actions_mutex);
  2325. } else {
  2326. set_visibility(item, visible);
  2327. }
  2328. return true;
  2329. }
  2330. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  2331. {
  2332. return item ? item->locked : false;
  2333. }
  2334. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  2335. {
  2336. struct calldata cd;
  2337. uint8_t stack[256];
  2338. if (!item)
  2339. return false;
  2340. if (item->locked == lock)
  2341. return false;
  2342. if (!item->parent)
  2343. return false;
  2344. item->locked = lock;
  2345. calldata_init_fixed(&cd, stack, sizeof(stack));
  2346. calldata_set_ptr(&cd, "item", item);
  2347. calldata_set_bool(&cd, "locked", lock);
  2348. signal_parent(item->parent, "item_locked", &cd);
  2349. return true;
  2350. }
  2351. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t *const *items,
  2352. size_t size, bool *order_matches)
  2353. {
  2354. obs_sceneitem_t *item = scene->first_item;
  2355. size_t count = 0;
  2356. while (item) {
  2357. bool found = false;
  2358. for (size_t i = 0; i < size; i++) {
  2359. if (items[i] != item)
  2360. continue;
  2361. if (count != i)
  2362. *order_matches = false;
  2363. found = true;
  2364. break;
  2365. }
  2366. if (!found)
  2367. return false;
  2368. item = item->next;
  2369. count += 1;
  2370. }
  2371. return count == size;
  2372. }
  2373. bool obs_scene_reorder_items(obs_scene_t *scene,
  2374. obs_sceneitem_t *const *item_order,
  2375. size_t item_order_size)
  2376. {
  2377. if (!scene || !item_order_size)
  2378. return false;
  2379. scene = obs_scene_get_ref(scene);
  2380. if (!scene)
  2381. return false;
  2382. full_lock(scene);
  2383. bool order_matches = true;
  2384. if (!sceneitems_match(scene, item_order, item_order_size,
  2385. &order_matches) ||
  2386. order_matches) {
  2387. full_unlock(scene);
  2388. obs_scene_release(scene);
  2389. return false;
  2390. }
  2391. scene->first_item = item_order[0];
  2392. obs_sceneitem_t *prev = NULL;
  2393. for (size_t i = 0; i < item_order_size; i++) {
  2394. item_order[i]->prev = prev;
  2395. item_order[i]->next = NULL;
  2396. if (prev)
  2397. prev->next = item_order[i];
  2398. prev = item_order[i];
  2399. }
  2400. full_unlock(scene);
  2401. signal_reorder(scene->first_item);
  2402. obs_scene_release(scene);
  2403. return true;
  2404. }
  2405. void obs_scene_atomic_update(obs_scene_t *scene,
  2406. obs_scene_atomic_update_func func, void *data)
  2407. {
  2408. if (!scene)
  2409. return;
  2410. scene = obs_scene_get_ref(scene);
  2411. if (!scene)
  2412. return;
  2413. full_lock(scene);
  2414. func(data, scene);
  2415. full_unlock(scene);
  2416. obs_scene_release(scene);
  2417. }
  2418. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  2419. const struct obs_sceneitem_crop *crop2)
  2420. {
  2421. return crop1->left == crop2->left && crop1->right == crop2->right &&
  2422. crop1->top == crop2->top && crop1->bottom == crop2->bottom;
  2423. }
  2424. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  2425. const struct obs_sceneitem_crop *crop)
  2426. {
  2427. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  2428. return;
  2429. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  2430. return;
  2431. if (crop_equal(crop, &item->crop))
  2432. return;
  2433. memcpy(&item->crop, crop, sizeof(*crop));
  2434. if (item->crop.left < 0)
  2435. item->crop.left = 0;
  2436. if (item->crop.right < 0)
  2437. item->crop.right = 0;
  2438. if (item->crop.top < 0)
  2439. item->crop.top = 0;
  2440. if (item->crop.bottom < 0)
  2441. item->crop.bottom = 0;
  2442. os_atomic_set_bool(&item->update_transform, true);
  2443. }
  2444. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  2445. struct obs_sceneitem_crop *crop)
  2446. {
  2447. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  2448. return;
  2449. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  2450. return;
  2451. memcpy(crop, &item->crop, sizeof(*crop));
  2452. }
  2453. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  2454. enum obs_scale_type filter)
  2455. {
  2456. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  2457. return;
  2458. item->scale_filter = filter;
  2459. os_atomic_set_bool(&item->update_transform, true);
  2460. }
  2461. enum obs_scale_type obs_sceneitem_get_scale_filter(obs_sceneitem_t *item)
  2462. {
  2463. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter")
  2464. ? item->scale_filter
  2465. : OBS_SCALE_DISABLE;
  2466. }
  2467. void obs_sceneitem_set_blending_method(obs_sceneitem_t *item,
  2468. enum obs_blending_method method)
  2469. {
  2470. if (!obs_ptr_valid(item, "obs_sceneitem_set_blending_method"))
  2471. return;
  2472. item->blend_method = method;
  2473. }
  2474. enum obs_blending_method
  2475. obs_sceneitem_get_blending_method(obs_sceneitem_t *item)
  2476. {
  2477. return obs_ptr_valid(item, "obs_sceneitem_get_blending_method")
  2478. ? item->blend_method
  2479. : OBS_BLEND_METHOD_DEFAULT;
  2480. }
  2481. void obs_sceneitem_set_blending_mode(obs_sceneitem_t *item,
  2482. enum obs_blending_type type)
  2483. {
  2484. if (!obs_ptr_valid(item, "obs_sceneitem_set_blending_mode"))
  2485. return;
  2486. item->blend_type = type;
  2487. os_atomic_set_bool(&item->update_transform, true);
  2488. }
  2489. enum obs_blending_type obs_sceneitem_get_blending_mode(obs_sceneitem_t *item)
  2490. {
  2491. return obs_ptr_valid(item, "obs_sceneitem_get_blending_mode")
  2492. ? item->blend_type
  2493. : OBS_BLEND_NORMAL;
  2494. }
  2495. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  2496. {
  2497. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  2498. return;
  2499. os_atomic_inc_long(&item->defer_update);
  2500. }
  2501. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  2502. {
  2503. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  2504. return;
  2505. if (os_atomic_dec_long(&item->defer_update) == 0)
  2506. do_update_transform(item);
  2507. }
  2508. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  2509. {
  2510. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  2511. return;
  2512. os_atomic_inc_long(&item->defer_group_resize);
  2513. }
  2514. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  2515. {
  2516. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  2517. return;
  2518. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  2519. os_atomic_set_bool(&item->update_group_resize, true);
  2520. }
  2521. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  2522. {
  2523. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  2524. return 0;
  2525. return item->id;
  2526. }
  2527. void obs_sceneitem_set_id(obs_sceneitem_t *item, int64_t id)
  2528. {
  2529. item->id = id;
  2530. }
  2531. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  2532. {
  2533. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  2534. return NULL;
  2535. obs_data_addref(item->private_settings);
  2536. return item->private_settings;
  2537. }
  2538. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  2539. {
  2540. struct vec3 v;
  2541. vec3_set(&v, v2->x, v2->y, 0.0f);
  2542. vec3_transform(&v, &v, transform);
  2543. v2->x = v.x;
  2544. v2->y = v.y;
  2545. }
  2546. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  2547. struct vec2 *scale, float *rot)
  2548. {
  2549. struct matrix4 transform;
  2550. struct matrix4 mat;
  2551. struct vec4 x_base;
  2552. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2553. matrix4_copy(&transform, &group->draw_transform);
  2554. transform_val(pos, &transform);
  2555. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2556. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  2557. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  2558. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2559. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2560. matrix4_mul(&mat, &mat, &transform);
  2561. scale->x = vec4_len(&mat.x) * (scale->x > 0.0f ? 1.0f : -1.0f);
  2562. scale->y = vec4_len(&mat.y) * (scale->y > 0.0f ? 1.0f : -1.0f);
  2563. *rot += group->rot;
  2564. }
  2565. static void remove_group_transform(obs_sceneitem_t *group,
  2566. obs_sceneitem_t *item)
  2567. {
  2568. obs_scene_t *parent = item->parent;
  2569. if (!parent || !group)
  2570. return;
  2571. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  2572. update_item_transform(item, false);
  2573. }
  2574. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  2575. {
  2576. struct matrix4 transform;
  2577. struct matrix4 mat;
  2578. struct vec4 x_base;
  2579. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2580. matrix4_inv(&transform, &group->draw_transform);
  2581. transform_val(&item->pos, &transform);
  2582. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2583. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  2584. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  2585. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2586. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2587. matrix4_mul(&mat, &mat, &transform);
  2588. item->scale.x =
  2589. vec4_len(&mat.x) * (item->scale.x > 0.0f ? 1.0f : -1.0f);
  2590. item->scale.y =
  2591. vec4_len(&mat.y) * (item->scale.y > 0.0f ? 1.0f : -1.0f);
  2592. item->rot -= group->rot;
  2593. update_item_transform(item, false);
  2594. }
  2595. static bool resize_scene_base(obs_scene_t *scene, struct vec2 *minv,
  2596. struct vec2 *maxv, struct vec2 *scale)
  2597. {
  2598. vec2_set(minv, M_INFINITE, M_INFINITE);
  2599. vec2_set(maxv, -M_INFINITE, -M_INFINITE);
  2600. obs_sceneitem_t *item = scene->first_item;
  2601. if (!item) {
  2602. scene->cx = 0;
  2603. scene->cy = 0;
  2604. return false;
  2605. }
  2606. while (item) {
  2607. #define get_min_max(x_val, y_val) \
  2608. do { \
  2609. struct vec3 v; \
  2610. vec3_set(&v, x_val, y_val, 0.0f); \
  2611. vec3_transform(&v, &v, &item->box_transform); \
  2612. if (v.x < minv->x) \
  2613. minv->x = v.x; \
  2614. if (v.y < minv->y) \
  2615. minv->y = v.y; \
  2616. if (v.x > maxv->x) \
  2617. maxv->x = v.x; \
  2618. if (v.y > maxv->y) \
  2619. maxv->y = v.y; \
  2620. } while (false)
  2621. get_min_max(0.0f, 0.0f);
  2622. get_min_max(1.0f, 0.0f);
  2623. get_min_max(0.0f, 1.0f);
  2624. get_min_max(1.0f, 1.0f);
  2625. #undef get_min_max
  2626. item = item->next;
  2627. }
  2628. item = scene->first_item;
  2629. while (item) {
  2630. vec2_sub(&item->pos, &item->pos, minv);
  2631. update_item_transform(item, false);
  2632. item = item->next;
  2633. }
  2634. vec2_sub(scale, maxv, minv);
  2635. scene->cx = (uint32_t)ceilf(scale->x);
  2636. scene->cy = (uint32_t)ceilf(scale->y);
  2637. return true;
  2638. }
  2639. static void resize_scene(obs_scene_t *scene)
  2640. {
  2641. struct vec2 minv;
  2642. struct vec2 maxv;
  2643. struct vec2 scale;
  2644. resize_scene_base(scene, &minv, &maxv, &scale);
  2645. }
  2646. /* assumes group scene and parent scene is locked */
  2647. static void resize_group(obs_sceneitem_t *group)
  2648. {
  2649. obs_scene_t *scene = group->source->context.data;
  2650. struct vec2 minv;
  2651. struct vec2 maxv;
  2652. struct vec2 scale;
  2653. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  2654. return;
  2655. if (!resize_scene_base(scene, &minv, &maxv, &scale))
  2656. return;
  2657. if (group->bounds_type == OBS_BOUNDS_NONE) {
  2658. struct vec2 new_pos;
  2659. if ((group->align & OBS_ALIGN_LEFT) != 0)
  2660. new_pos.x = minv.x;
  2661. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  2662. new_pos.x = maxv.x;
  2663. else
  2664. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  2665. if ((group->align & OBS_ALIGN_TOP) != 0)
  2666. new_pos.y = minv.y;
  2667. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  2668. new_pos.y = maxv.y;
  2669. else
  2670. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  2671. transform_val(&new_pos, &group->draw_transform);
  2672. vec2_copy(&group->pos, &new_pos);
  2673. }
  2674. os_atomic_set_bool(&group->update_group_resize, false);
  2675. update_item_transform(group, false);
  2676. }
  2677. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  2678. {
  2679. return obs_scene_insert_group(scene, name, NULL, 0);
  2680. }
  2681. obs_sceneitem_t *obs_scene_add_group2(obs_scene_t *scene, const char *name,
  2682. bool signal)
  2683. {
  2684. return obs_scene_insert_group2(scene, name, NULL, 0, signal);
  2685. }
  2686. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene, const char *name,
  2687. obs_sceneitem_t **items, size_t count)
  2688. {
  2689. if (!scene)
  2690. return NULL;
  2691. /* don't allow groups or sub-items of other groups */
  2692. for (size_t i = count; i > 0; i--) {
  2693. obs_sceneitem_t *item = items[i - 1];
  2694. if (item->parent != scene || item->is_group)
  2695. return NULL;
  2696. }
  2697. obs_scene_t *sub_scene = create_id("group", name);
  2698. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  2699. obs_sceneitem_t *item =
  2700. obs_scene_add_internal(scene, sub_scene->source, last_item, 0);
  2701. if (!items || !count) {
  2702. obs_scene_release(sub_scene);
  2703. return item;
  2704. }
  2705. /* ------------------------- */
  2706. full_lock(scene);
  2707. full_lock(sub_scene);
  2708. sub_scene->first_item = items[0];
  2709. for (size_t i = count; i > 0; i--) {
  2710. size_t idx = i - 1;
  2711. remove_group_transform(item, items[idx]);
  2712. detach_sceneitem(items[idx]);
  2713. }
  2714. for (size_t i = 0; i < count; i++) {
  2715. size_t idx = i;
  2716. if (idx != (count - 1)) {
  2717. size_t next_idx = idx + 1;
  2718. items[idx]->next = items[next_idx];
  2719. items[next_idx]->prev = items[idx];
  2720. } else {
  2721. items[idx]->next = NULL;
  2722. }
  2723. items[idx]->parent = sub_scene;
  2724. apply_group_transform(items[idx], item);
  2725. }
  2726. items[0]->prev = NULL;
  2727. resize_group(item);
  2728. full_unlock(sub_scene);
  2729. full_unlock(scene);
  2730. struct calldata params;
  2731. uint8_t stack[128];
  2732. calldata_init_fixed(&params, stack, sizeof(stack));
  2733. calldata_set_ptr(&params, "scene", scene);
  2734. calldata_set_ptr(&params, "item", item);
  2735. signal_handler_signal(scene->source->context.signals, "item_add",
  2736. &params);
  2737. /* ------------------------- */
  2738. obs_scene_release(sub_scene);
  2739. return item;
  2740. }
  2741. obs_sceneitem_t *obs_scene_insert_group2(obs_scene_t *scene, const char *name,
  2742. obs_sceneitem_t **items, size_t count,
  2743. bool signal)
  2744. {
  2745. obs_sceneitem_t *item =
  2746. obs_scene_insert_group(scene, name, items, count);
  2747. if (signal && item)
  2748. signal_refresh(scene);
  2749. return item;
  2750. }
  2751. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  2752. {
  2753. if (!scene || !name || !*name) {
  2754. return NULL;
  2755. }
  2756. obs_sceneitem_t *group = NULL;
  2757. obs_sceneitem_t *item;
  2758. full_lock(scene);
  2759. item = scene->first_item;
  2760. while (item) {
  2761. if (item->is_group && item->source->context.name) {
  2762. if (strcmp(item->source->context.name, name) == 0) {
  2763. group = item;
  2764. break;
  2765. }
  2766. }
  2767. item = item->next;
  2768. }
  2769. full_unlock(scene);
  2770. return group;
  2771. }
  2772. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  2773. {
  2774. return item && item->is_group;
  2775. }
  2776. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  2777. {
  2778. return (item && item->is_group) ? item->source->context.data : NULL;
  2779. }
  2780. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  2781. {
  2782. if (!item || !item->is_group)
  2783. return;
  2784. obs_scene_t *scene = item->parent;
  2785. obs_scene_t *subscene = item->source->context.data;
  2786. obs_sceneitem_t *insert_after = item;
  2787. obs_sceneitem_t *first;
  2788. obs_sceneitem_t *last;
  2789. signal_item_remove(item);
  2790. full_lock(scene);
  2791. /* ------------------------- */
  2792. full_lock(subscene);
  2793. first = subscene->first_item;
  2794. last = first;
  2795. while (last) {
  2796. obs_sceneitem_t *dst;
  2797. remove_group_transform(item, last);
  2798. dst = obs_scene_add_internal(scene, last->source, insert_after,
  2799. 0);
  2800. duplicate_item_data(dst, last, true, true);
  2801. apply_group_transform(last, item);
  2802. if (!last->next)
  2803. break;
  2804. insert_after = dst;
  2805. last = last->next;
  2806. }
  2807. full_unlock(subscene);
  2808. /* ------------------------- */
  2809. detach_sceneitem(item);
  2810. full_unlock(scene);
  2811. obs_sceneitem_release(item);
  2812. }
  2813. void obs_sceneitem_group_ungroup2(obs_sceneitem_t *item, bool signal)
  2814. {
  2815. obs_scene_t *scene = item->parent;
  2816. obs_sceneitem_group_ungroup(item);
  2817. if (signal)
  2818. signal_refresh(scene);
  2819. }
  2820. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2821. {
  2822. if (!group || !group->is_group || !item)
  2823. return;
  2824. obs_scene_t *scene = group->parent;
  2825. obs_scene_t *groupscene = group->source->context.data;
  2826. if (item->parent != scene)
  2827. return;
  2828. if (item->parent == groupscene)
  2829. return;
  2830. /* ------------------------- */
  2831. full_lock(scene);
  2832. full_lock(groupscene);
  2833. remove_group_transform(group, item);
  2834. detach_sceneitem(item);
  2835. attach_sceneitem(groupscene, item, NULL);
  2836. apply_group_transform(item, group);
  2837. resize_group(group);
  2838. full_unlock(groupscene);
  2839. full_unlock(scene);
  2840. /* ------------------------- */
  2841. signal_refresh(scene);
  2842. }
  2843. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2844. obs_sceneitem_t *item)
  2845. {
  2846. if (!item || !group || !group->is_group)
  2847. return;
  2848. obs_scene_t *groupscene = item->parent;
  2849. obs_scene_t *scene = group->parent;
  2850. /* ------------------------- */
  2851. full_lock(scene);
  2852. full_lock(groupscene);
  2853. remove_group_transform(group, item);
  2854. detach_sceneitem(item);
  2855. attach_sceneitem(scene, item, NULL);
  2856. resize_group(group);
  2857. full_unlock(groupscene);
  2858. full_unlock(scene);
  2859. /* ------------------------- */
  2860. signal_refresh(scene);
  2861. }
  2862. static void
  2863. build_current_order_info(obs_scene_t *scene,
  2864. struct obs_sceneitem_order_info **items_out,
  2865. size_t *size_out)
  2866. {
  2867. DARRAY(struct obs_sceneitem_order_info) items;
  2868. da_init(items);
  2869. obs_sceneitem_t *item = scene->first_item;
  2870. while (item) {
  2871. struct obs_sceneitem_order_info info = {0};
  2872. info.item = item;
  2873. da_push_back(items, &info);
  2874. if (item->is_group) {
  2875. obs_scene_t *sub_scene = item->source->context.data;
  2876. full_lock(sub_scene);
  2877. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2878. while (sub_item) {
  2879. info.group = item;
  2880. info.item = sub_item;
  2881. da_push_back(items, &info);
  2882. sub_item = sub_item->next;
  2883. }
  2884. full_unlock(sub_scene);
  2885. }
  2886. item = item->next;
  2887. }
  2888. *items_out = items.array;
  2889. *size_out = items.num;
  2890. }
  2891. static bool sceneitems_match2(obs_scene_t *scene,
  2892. struct obs_sceneitem_order_info *items,
  2893. size_t size)
  2894. {
  2895. struct obs_sceneitem_order_info *cur_items;
  2896. size_t cur_size;
  2897. build_current_order_info(scene, &cur_items, &cur_size);
  2898. if (cur_size != size) {
  2899. bfree(cur_items);
  2900. return false;
  2901. }
  2902. for (size_t i = 0; i < size; i++) {
  2903. struct obs_sceneitem_order_info *new = &items[i];
  2904. struct obs_sceneitem_order_info *old = &cur_items[i];
  2905. if (new->group != old->group || new->item != old->item) {
  2906. bfree(cur_items);
  2907. return false;
  2908. }
  2909. }
  2910. bfree(cur_items);
  2911. return true;
  2912. }
  2913. static obs_sceneitem_t *
  2914. get_sceneitem_parent_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem)
  2915. {
  2916. if (group_subitem->is_group)
  2917. return NULL;
  2918. obs_sceneitem_t *item = scene->first_item;
  2919. while (item) {
  2920. if (item->is_group &&
  2921. item->source->context.data == group_subitem->parent)
  2922. return item;
  2923. item = item->next;
  2924. }
  2925. return NULL;
  2926. }
  2927. static void obs_sceneitem_move_hotkeys(obs_scene_t *parent,
  2928. obs_sceneitem_t *item)
  2929. {
  2930. obs_data_array_t *data0 = NULL;
  2931. obs_data_array_t *data1 = NULL;
  2932. obs_hotkey_pair_save(item->toggle_visibility, &data0, &data1);
  2933. obs_hotkey_pair_unregister(item->toggle_visibility);
  2934. init_hotkeys(parent, item, obs_source_get_name(item->source));
  2935. obs_hotkey_pair_load(item->toggle_visibility, data0, data1);
  2936. obs_data_array_release(data0);
  2937. obs_data_array_release(data1);
  2938. }
  2939. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2940. struct obs_sceneitem_order_info *item_order,
  2941. size_t item_order_size)
  2942. {
  2943. if (!scene || !item_order_size || !item_order)
  2944. return false;
  2945. scene = obs_scene_get_ref(scene);
  2946. if (!scene)
  2947. return false;
  2948. full_lock(scene);
  2949. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2950. full_unlock(scene);
  2951. obs_scene_release(scene);
  2952. return false;
  2953. }
  2954. for (size_t i = 0; i < item_order_size; i++) {
  2955. struct obs_sceneitem_order_info *info = &item_order[i];
  2956. if (!info->item->is_group) {
  2957. obs_sceneitem_t *group =
  2958. get_sceneitem_parent_group(scene, info->item);
  2959. remove_group_transform(group, info->item);
  2960. }
  2961. }
  2962. scene->first_item = item_order[0].item;
  2963. obs_sceneitem_t *prev = NULL;
  2964. for (size_t i = 0; i < item_order_size; i++) {
  2965. struct obs_sceneitem_order_info *info = &item_order[i];
  2966. obs_sceneitem_t *item = info->item;
  2967. if (info->item->is_group) {
  2968. obs_sceneitem_t *sub_prev = NULL;
  2969. obs_scene_t *sub_scene =
  2970. info->item->source->context.data;
  2971. sub_scene->first_item = NULL;
  2972. obs_scene_addref(sub_scene);
  2973. full_lock(sub_scene);
  2974. for (i++; i < item_order_size; i++) {
  2975. struct obs_sceneitem_order_info *sub_info =
  2976. &item_order[i];
  2977. obs_sceneitem_t *sub_item = sub_info->item;
  2978. if (sub_info->group != info->item) {
  2979. i--;
  2980. break;
  2981. }
  2982. if (!sub_scene->first_item)
  2983. sub_scene->first_item = sub_item;
  2984. /* Move hotkeys into group */
  2985. obs_sceneitem_move_hotkeys(sub_scene, sub_item);
  2986. sub_item->prev = sub_prev;
  2987. sub_item->next = NULL;
  2988. sub_item->parent = sub_scene;
  2989. if (sub_prev)
  2990. sub_prev->next = sub_item;
  2991. apply_group_transform(sub_info->item,
  2992. sub_info->group);
  2993. sub_prev = sub_item;
  2994. }
  2995. resize_group(info->item);
  2996. full_unlock(sub_scene);
  2997. obs_scene_release(sub_scene);
  2998. }
  2999. /* Move item hotkeys out of group */
  3000. if (item->parent && obs_scene_is_group(item->parent))
  3001. obs_sceneitem_move_hotkeys(scene, item);
  3002. item->prev = prev;
  3003. item->next = NULL;
  3004. item->parent = scene;
  3005. if (prev)
  3006. prev->next = item;
  3007. prev = item;
  3008. }
  3009. full_unlock(scene);
  3010. signal_reorder(scene->first_item);
  3011. obs_scene_release(scene);
  3012. return true;
  3013. }
  3014. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  3015. obs_sceneitem_t *group_subitem)
  3016. {
  3017. if (!scene || !group_subitem || group_subitem->is_group)
  3018. return NULL;
  3019. full_lock(scene);
  3020. obs_sceneitem_t *group =
  3021. get_sceneitem_parent_group(scene, group_subitem);
  3022. full_unlock(scene);
  3023. return group;
  3024. }
  3025. bool obs_source_is_group(const obs_source_t *source)
  3026. {
  3027. return source && strcmp(source->info.id, group_info.id) == 0;
  3028. }
  3029. bool obs_source_is_scene(const obs_source_t *source)
  3030. {
  3031. return source && strcmp(source->info.id, scene_info.id) == 0;
  3032. }
  3033. bool obs_scene_is_group(const obs_scene_t *scene)
  3034. {
  3035. return scene ? scene->is_group : false;
  3036. }
  3037. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  3038. bool (*callback)(obs_scene_t *,
  3039. obs_sceneitem_t *, void *),
  3040. void *param)
  3041. {
  3042. if (!group || !group->is_group)
  3043. return;
  3044. obs_scene_t *scene = group->source->context.data;
  3045. if (scene)
  3046. obs_scene_enum_items(scene, callback, param);
  3047. }
  3048. void obs_sceneitem_force_update_transform(obs_sceneitem_t *item)
  3049. {
  3050. if (!item)
  3051. return;
  3052. if (os_atomic_set_bool(&item->update_transform, false))
  3053. update_item_transform(item, false);
  3054. }
  3055. void obs_sceneitem_set_show_transition(obs_sceneitem_t *item,
  3056. obs_source_t *transition)
  3057. {
  3058. if (!item)
  3059. return;
  3060. if (item->show_transition)
  3061. obs_source_release(item->show_transition);
  3062. item->show_transition = obs_source_get_ref(transition);
  3063. }
  3064. void obs_sceneitem_set_show_transition_duration(obs_sceneitem_t *item,
  3065. uint32_t duration_ms)
  3066. {
  3067. if (!item)
  3068. return;
  3069. item->show_transition_duration = duration_ms;
  3070. }
  3071. obs_source_t *obs_sceneitem_get_show_transition(obs_sceneitem_t *item)
  3072. {
  3073. if (!item)
  3074. return NULL;
  3075. return item->show_transition;
  3076. }
  3077. uint32_t obs_sceneitem_get_show_transition_duration(obs_sceneitem_t *item)
  3078. {
  3079. if (!item)
  3080. return 0;
  3081. return item->show_transition_duration;
  3082. }
  3083. void obs_sceneitem_set_hide_transition(obs_sceneitem_t *item,
  3084. obs_source_t *transition)
  3085. {
  3086. if (!item)
  3087. return;
  3088. if (item->hide_transition)
  3089. obs_source_release(item->hide_transition);
  3090. item->hide_transition = obs_source_get_ref(transition);
  3091. }
  3092. void obs_sceneitem_set_hide_transition_duration(obs_sceneitem_t *item,
  3093. uint32_t duration_ms)
  3094. {
  3095. if (!item)
  3096. return;
  3097. item->hide_transition_duration = duration_ms;
  3098. }
  3099. obs_source_t *obs_sceneitem_get_hide_transition(obs_sceneitem_t *item)
  3100. {
  3101. if (!item)
  3102. return NULL;
  3103. return item->hide_transition;
  3104. }
  3105. uint32_t obs_sceneitem_get_hide_transition_duration(obs_sceneitem_t *item)
  3106. {
  3107. if (!item)
  3108. return 0;
  3109. return item->hide_transition_duration;
  3110. }
  3111. void obs_sceneitem_set_transition(obs_sceneitem_t *item, bool show,
  3112. obs_source_t *transition)
  3113. {
  3114. if (!item)
  3115. return;
  3116. obs_source_t **target = show ? &item->show_transition
  3117. : &item->hide_transition;
  3118. if (*target)
  3119. obs_source_release(*target);
  3120. *target = obs_source_get_ref(transition);
  3121. }
  3122. obs_source_t *obs_sceneitem_get_transition(obs_sceneitem_t *item, bool show)
  3123. {
  3124. if (!item)
  3125. return NULL;
  3126. return show ? item->show_transition : item->hide_transition;
  3127. }
  3128. void obs_sceneitem_set_transition_duration(obs_sceneitem_t *item, bool show,
  3129. uint32_t duration_ms)
  3130. {
  3131. if (!item)
  3132. return;
  3133. if (show)
  3134. item->show_transition_duration = duration_ms;
  3135. else
  3136. item->hide_transition_duration = duration_ms;
  3137. }
  3138. uint32_t obs_sceneitem_get_transition_duration(obs_sceneitem_t *item, bool show)
  3139. {
  3140. if (!item)
  3141. return 0;
  3142. return show ? item->show_transition_duration
  3143. : item->hide_transition_duration;
  3144. }
  3145. void obs_sceneitem_transition_stop(void *data, calldata_t *calldata)
  3146. {
  3147. obs_source_t *parent = data;
  3148. obs_source_t *transition;
  3149. calldata_get_ptr(calldata, "source", &transition);
  3150. obs_source_remove_active_child(parent, transition);
  3151. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  3152. if (sh)
  3153. signal_handler_disconnect(sh, "transition_stop",
  3154. obs_sceneitem_transition_stop,
  3155. parent);
  3156. }
  3157. void obs_sceneitem_do_transition(obs_sceneitem_t *item, bool visible)
  3158. {
  3159. if (!item)
  3160. return;
  3161. if (transition_active(item->show_transition))
  3162. obs_transition_force_stop(item->show_transition);
  3163. if (transition_active(item->hide_transition))
  3164. obs_transition_force_stop(item->hide_transition);
  3165. obs_source_t *transition = obs_sceneitem_get_transition(item, visible);
  3166. if (!transition)
  3167. return;
  3168. int duration =
  3169. (int)obs_sceneitem_get_transition_duration(item, visible);
  3170. const int cx = obs_source_get_width(item->source);
  3171. const int cy = obs_source_get_height(item->source);
  3172. obs_transition_set_size(transition, cx, cy);
  3173. obs_transition_set_alignment(transition, OBS_ALIGN_CENTER);
  3174. obs_transition_set_scale_type(transition, OBS_TRANSITION_SCALE_ASPECT);
  3175. if (duration == 0)
  3176. duration = 300;
  3177. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  3178. obs_source_t *parent = obs_scene_get_source(scene);
  3179. obs_source_add_active_child(parent, transition);
  3180. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  3181. if (sh)
  3182. signal_handler_connect(sh, "transition_stop",
  3183. obs_sceneitem_transition_stop, parent);
  3184. if (!visible) {
  3185. obs_transition_set(transition, item->source);
  3186. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  3187. duration, NULL);
  3188. } else {
  3189. obs_transition_set(transition, NULL);
  3190. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  3191. duration, item->source);
  3192. }
  3193. }
  3194. void obs_sceneitem_transition_load(struct obs_scene_item *item,
  3195. obs_data_t *data, bool show)
  3196. {
  3197. if (!item || !data)
  3198. return;
  3199. const char *id = obs_data_get_string(data, "id");
  3200. if (id && strlen(id)) {
  3201. const char *tn = obs_data_get_string(data, "name");
  3202. obs_data_t *s = obs_data_get_obj(data, "transition");
  3203. obs_source_t *t = obs_source_create_private(id, tn, s);
  3204. obs_sceneitem_set_transition(item, show, t);
  3205. obs_source_release(t);
  3206. obs_data_release(s);
  3207. } else {
  3208. obs_sceneitem_set_transition(item, show, NULL);
  3209. }
  3210. obs_sceneitem_set_transition_duration(
  3211. item, show, (uint32_t)obs_data_get_int(data, "duration"));
  3212. }
  3213. obs_data_t *obs_sceneitem_transition_save(struct obs_scene_item *item,
  3214. bool show)
  3215. {
  3216. obs_data_t *data = obs_data_create();
  3217. struct obs_source *transition = show ? item->show_transition
  3218. : item->hide_transition;
  3219. if (transition) {
  3220. obs_data_set_string(data, "id",
  3221. obs_source_get_unversioned_id(transition));
  3222. obs_data_set_string(data, "versioned_id",
  3223. obs_source_get_id(transition));
  3224. obs_data_set_string(data, "name",
  3225. obs_source_get_name(transition));
  3226. obs_data_t *s = obs_source_get_settings(transition);
  3227. obs_data_set_obj(data, "transition", s);
  3228. obs_data_release(s);
  3229. }
  3230. obs_data_set_int(data, "duration",
  3231. show ? item->show_transition_duration
  3232. : item->hide_transition_duration);
  3233. return data;
  3234. }
  3235. void obs_scene_prune_sources(obs_scene_t *scene)
  3236. {
  3237. obs_scene_item_ptr_array_t remove_items;
  3238. da_init(remove_items);
  3239. video_lock(scene);
  3240. update_transforms_and_prune_sources(scene, &remove_items, NULL);
  3241. video_unlock(scene);
  3242. for (size_t i = 0; i < remove_items.num; i++)
  3243. obs_sceneitem_release(remove_items.array[i]);
  3244. da_free(remove_items);
  3245. }