obs-scene.c 93 KB

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