obs-scene.c 106 KB

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