obs-scene.c 116 KB

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