obs-scene.c 103 KB

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