obs-scene.c 114 KB

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