obs-scene.c 95 KB

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