obs-scene.c 110 KB

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