obs-scene.c 105 KB

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