1
0

obs-scene.c 97 KB

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