obs-scene.c 116 KB

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