obs-scene.c 110 KB

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