obs-scene.c 112 KB

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