obs-scene.c 89 KB

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