obs-scene.c 98 KB

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