obs-scene.c 88 KB

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