obs-scene.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604
  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. pthread_mutexattr_t attr;
  74. struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
  75. scene->source = source;
  76. if (strcmp(source->info.id, group_info.id) == 0) {
  77. scene->is_group = true;
  78. scene->custom_size = true;
  79. scene->cx = 0;
  80. scene->cy = 0;
  81. }
  82. signal_handler_add_array(obs_source_get_signal_handler(source),
  83. obs_scene_signals);
  84. if (pthread_mutexattr_init(&attr) != 0)
  85. goto fail;
  86. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  87. goto fail;
  88. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  89. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  90. "mutex");
  91. goto fail;
  92. }
  93. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  94. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  95. "mutex");
  96. goto fail;
  97. }
  98. UNUSED_PARAMETER(settings);
  99. return scene;
  100. fail:
  101. pthread_mutexattr_destroy(&attr);
  102. bfree(scene);
  103. return NULL;
  104. }
  105. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  106. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  107. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  108. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  109. static inline void full_lock(struct obs_scene *scene)
  110. {
  111. video_lock(scene);
  112. audio_lock(scene);
  113. }
  114. static inline void full_unlock(struct obs_scene *scene)
  115. {
  116. audio_unlock(scene);
  117. video_unlock(scene);
  118. }
  119. static void set_visibility(struct obs_scene_item *item, bool vis);
  120. static inline void detach_sceneitem(struct obs_scene_item *item);
  121. static inline void remove_without_release(struct obs_scene_item *item)
  122. {
  123. item->removed = true;
  124. set_visibility(item, false);
  125. signal_item_remove(item);
  126. detach_sceneitem(item);
  127. }
  128. static void remove_all_items(struct obs_scene *scene)
  129. {
  130. struct obs_scene_item *item;
  131. DARRAY(struct obs_scene_item *) items;
  132. da_init(items);
  133. full_lock(scene);
  134. item = scene->first_item;
  135. while (item) {
  136. struct obs_scene_item *del_item = item;
  137. item = item->next;
  138. remove_without_release(del_item);
  139. da_push_back(items, &del_item);
  140. }
  141. full_unlock(scene);
  142. for (size_t i = 0; i < items.num; i++)
  143. obs_sceneitem_release(items.array[i]);
  144. da_free(items);
  145. }
  146. static void scene_destroy(void *data)
  147. {
  148. struct obs_scene *scene = data;
  149. remove_all_items(scene);
  150. pthread_mutex_destroy(&scene->video_mutex);
  151. pthread_mutex_destroy(&scene->audio_mutex);
  152. bfree(scene);
  153. }
  154. static inline bool transition_active(obs_source_t *transition)
  155. {
  156. return transition && (transition->transitioning_audio ||
  157. transition->transitioning_video);
  158. }
  159. static void scene_enum_sources(void *data, obs_source_enum_proc_t enum_callback,
  160. void *param, bool active)
  161. {
  162. struct obs_scene *scene = data;
  163. struct obs_scene_item *item;
  164. struct obs_scene_item *next;
  165. full_lock(scene);
  166. item = scene->first_item;
  167. while (item) {
  168. next = item->next;
  169. obs_sceneitem_addref(item);
  170. if (active) {
  171. if (item->visible &&
  172. transition_active(item->show_transition))
  173. enum_callback(scene->source,
  174. item->show_transition, param);
  175. else if (!item->visible &&
  176. transition_active(item->hide_transition))
  177. enum_callback(scene->source,
  178. item->hide_transition, param);
  179. else if (os_atomic_load_long(&item->active_refs) > 0)
  180. enum_callback(scene->source, item->source,
  181. param);
  182. } else {
  183. if (item->show_transition)
  184. enum_callback(scene->source,
  185. item->show_transition, param);
  186. if (item->hide_transition)
  187. enum_callback(scene->source,
  188. item->hide_transition, param);
  189. enum_callback(scene->source, item->source, param);
  190. }
  191. obs_sceneitem_release(item);
  192. item = next;
  193. }
  194. full_unlock(scene);
  195. }
  196. static void scene_enum_active_sources(void *data,
  197. obs_source_enum_proc_t enum_callback,
  198. void *param)
  199. {
  200. scene_enum_sources(data, enum_callback, param, true);
  201. }
  202. static void scene_enum_all_sources(void *data,
  203. obs_source_enum_proc_t enum_callback,
  204. void *param)
  205. {
  206. scene_enum_sources(data, enum_callback, param, false);
  207. }
  208. static inline void detach_sceneitem(struct obs_scene_item *item)
  209. {
  210. if (item->prev)
  211. item->prev->next = item->next;
  212. else
  213. item->parent->first_item = item->next;
  214. if (item->next)
  215. item->next->prev = item->prev;
  216. item->parent = NULL;
  217. }
  218. static inline void attach_sceneitem(struct obs_scene *parent,
  219. struct obs_scene_item *item,
  220. struct obs_scene_item *prev)
  221. {
  222. item->prev = prev;
  223. item->parent = parent;
  224. if (prev) {
  225. item->next = prev->next;
  226. if (prev->next)
  227. prev->next->prev = item;
  228. prev->next = item;
  229. } else {
  230. item->next = parent->first_item;
  231. if (parent->first_item)
  232. parent->first_item->prev = item;
  233. parent->first_item = item;
  234. }
  235. }
  236. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  237. {
  238. if (align & OBS_ALIGN_RIGHT)
  239. v->x += (float)cx;
  240. else if ((align & OBS_ALIGN_LEFT) == 0)
  241. v->x += (float)(cx / 2);
  242. if (align & OBS_ALIGN_BOTTOM)
  243. v->y += (float)cy;
  244. else if ((align & OBS_ALIGN_TOP) == 0)
  245. v->y += (float)(cy / 2);
  246. }
  247. static void calculate_bounds_data(struct obs_scene_item *item,
  248. struct vec2 *origin, struct vec2 *scale,
  249. uint32_t *cx, uint32_t *cy)
  250. {
  251. float width = (float)(*cx) * fabsf(scale->x);
  252. float height = (float)(*cy) * fabsf(scale->y);
  253. float item_aspect = width / height;
  254. float bounds_aspect = item->bounds.x / item->bounds.y;
  255. uint32_t bounds_type = item->bounds_type;
  256. float width_diff, height_diff;
  257. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  258. if (width > item->bounds.x || height > item->bounds.y)
  259. bounds_type = OBS_BOUNDS_SCALE_INNER;
  260. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  261. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  262. bool use_width = (bounds_aspect < item_aspect);
  263. float mul;
  264. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  265. use_width = !use_width;
  266. mul = use_width ? item->bounds.x / width
  267. : item->bounds.y / height;
  268. vec2_mulf(scale, scale, mul);
  269. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  270. vec2_mulf(scale, scale, item->bounds.x / width);
  271. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  272. vec2_mulf(scale, scale, item->bounds.y / height);
  273. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  274. scale->x = item->bounds.x / (float)(*cx);
  275. scale->y = item->bounds.y / (float)(*cy);
  276. }
  277. width = (float)(*cx) * scale->x;
  278. height = (float)(*cy) * scale->y;
  279. width_diff = item->bounds.x - width;
  280. height_diff = item->bounds.y - height;
  281. *cx = (uint32_t)item->bounds.x;
  282. *cy = (uint32_t)item->bounds.y;
  283. add_alignment(origin, item->bounds_align, (int)-width_diff,
  284. (int)-height_diff);
  285. }
  286. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  287. uint32_t width)
  288. {
  289. uint32_t crop_cx = item->crop.left + item->crop.right;
  290. return (crop_cx > width) ? 2 : (width - crop_cx);
  291. }
  292. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  293. uint32_t height)
  294. {
  295. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  296. return (crop_cy > height) ? 2 : (height - crop_cy);
  297. }
  298. static void update_item_transform(struct obs_scene_item *item, bool update_tex)
  299. {
  300. uint32_t width;
  301. uint32_t height;
  302. uint32_t cx;
  303. uint32_t cy;
  304. struct vec2 base_origin;
  305. struct vec2 origin;
  306. struct vec2 scale;
  307. struct calldata params;
  308. uint8_t stack[128];
  309. if (os_atomic_load_long(&item->defer_update) > 0)
  310. return;
  311. width = obs_source_get_width(item->source);
  312. height = obs_source_get_height(item->source);
  313. cx = calc_cx(item, width);
  314. cy = calc_cy(item, height);
  315. scale = item->scale;
  316. item->last_width = width;
  317. item->last_height = height;
  318. width = cx;
  319. height = cy;
  320. vec2_zero(&base_origin);
  321. vec2_zero(&origin);
  322. /* ----------------------- */
  323. if (item->bounds_type != OBS_BOUNDS_NONE) {
  324. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  325. } else {
  326. cx = (uint32_t)((float)cx * scale.x);
  327. cy = (uint32_t)((float)cy * scale.y);
  328. }
  329. add_alignment(&origin, item->align, (int)cx, (int)cy);
  330. matrix4_identity(&item->draw_transform);
  331. matrix4_scale3f(&item->draw_transform, &item->draw_transform, scale.x,
  332. scale.y, 1.0f);
  333. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  334. -origin.x, -origin.y, 0.0f);
  335. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform, 0.0f,
  336. 0.0f, 1.0f, RAD(item->rot));
  337. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  338. item->pos.x, item->pos.y, 0.0f);
  339. item->output_scale = scale;
  340. /* ----------------------- */
  341. if (item->bounds_type != OBS_BOUNDS_NONE) {
  342. vec2_copy(&scale, &item->bounds);
  343. } else {
  344. scale.x = (float)width * item->scale.x;
  345. scale.y = (float)height * item->scale.y;
  346. }
  347. item->box_scale = scale;
  348. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  349. matrix4_identity(&item->box_transform);
  350. matrix4_scale3f(&item->box_transform, &item->box_transform, scale.x,
  351. scale.y, 1.0f);
  352. matrix4_translate3f(&item->box_transform, &item->box_transform,
  353. -base_origin.x, -base_origin.y, 0.0f);
  354. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform, 0.0f,
  355. 0.0f, 1.0f, RAD(item->rot));
  356. matrix4_translate3f(&item->box_transform, &item->box_transform,
  357. item->pos.x, item->pos.y, 0.0f);
  358. /* ----------------------- */
  359. calldata_init_fixed(&params, stack, sizeof(stack));
  360. calldata_set_ptr(&params, "item", item);
  361. signal_parent(item->parent, "item_transform", &params);
  362. if (!update_tex)
  363. return;
  364. if (item->item_render && !item_texture_enabled(item)) {
  365. obs_enter_graphics();
  366. gs_texrender_destroy(item->item_render);
  367. item->item_render = NULL;
  368. obs_leave_graphics();
  369. } else if (!item->item_render && item_texture_enabled(item)) {
  370. obs_enter_graphics();
  371. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  372. obs_leave_graphics();
  373. }
  374. os_atomic_set_bool(&item->update_transform, false);
  375. }
  376. static inline bool source_size_changed(struct obs_scene_item *item)
  377. {
  378. uint32_t width = obs_source_get_width(item->source);
  379. uint32_t height = obs_source_get_height(item->source);
  380. return item->last_width != width || item->last_height != height;
  381. }
  382. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  383. {
  384. return crop->left || crop->right || crop->top || crop->bottom;
  385. }
  386. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  387. {
  388. return item->scale_filter != OBS_SCALE_DISABLE;
  389. }
  390. static inline bool item_is_scene(const struct obs_scene_item *item)
  391. {
  392. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  393. }
  394. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  395. {
  396. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  397. (item_is_scene(item) && !item->is_group);
  398. }
  399. static void render_item_texture(struct obs_scene_item *item)
  400. {
  401. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  402. if (!tex) {
  403. return;
  404. }
  405. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_ITEM_TEXTURE,
  406. "render_item_texture");
  407. gs_effect_t *effect = obs->video.default_effect;
  408. enum obs_scale_type type = item->scale_filter;
  409. uint32_t cx = gs_texture_get_width(tex);
  410. uint32_t cy = gs_texture_get_height(tex);
  411. const char *tech = "Draw";
  412. if (type != OBS_SCALE_DISABLE) {
  413. if (type == OBS_SCALE_POINT) {
  414. gs_eparam_t *image =
  415. gs_effect_get_param_by_name(effect, "image");
  416. gs_effect_set_next_sampler(image,
  417. obs->video.point_sampler);
  418. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  419. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  420. gs_eparam_t *scale_param;
  421. gs_eparam_t *scale_i_param;
  422. if (item->output_scale.x < 0.5f ||
  423. item->output_scale.y < 0.5f) {
  424. effect = obs->video.bilinear_lowres_effect;
  425. } else if (type == OBS_SCALE_BICUBIC) {
  426. effect = obs->video.bicubic_effect;
  427. } else if (type == OBS_SCALE_LANCZOS) {
  428. effect = obs->video.lanczos_effect;
  429. } else if (type == OBS_SCALE_AREA) {
  430. effect = obs->video.area_effect;
  431. if ((item->output_scale.x >= 1.0f) &&
  432. (item->output_scale.y >= 1.0f))
  433. tech = "DrawUpscale";
  434. }
  435. scale_param = gs_effect_get_param_by_name(
  436. effect, "base_dimension");
  437. if (scale_param) {
  438. struct vec2 base_res = {(float)cx, (float)cy};
  439. gs_effect_set_vec2(scale_param, &base_res);
  440. }
  441. scale_i_param = gs_effect_get_param_by_name(
  442. effect, "base_dimension_i");
  443. if (scale_i_param) {
  444. struct vec2 base_res_i = {1.0f / (float)cx,
  445. 1.0f / (float)cy};
  446. gs_effect_set_vec2(scale_i_param, &base_res_i);
  447. }
  448. }
  449. }
  450. gs_blend_state_push();
  451. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  452. while (gs_effect_loop(effect, tech))
  453. obs_source_draw(tex, 0, 0, 0, 0, 0);
  454. gs_blend_state_pop();
  455. GS_DEBUG_MARKER_END();
  456. }
  457. static bool are_texcoords_centered(struct matrix4 *m)
  458. {
  459. static const struct matrix4 identity = {
  460. {1.0f, 0.0f, 0.0f, 0.0f},
  461. {0.0f, 1.0f, 0.0f, 0.0f},
  462. {0.0f, 0.0f, 1.0f, 0.0f},
  463. {0.0f, 0.0f, 0.0f, 1.0f},
  464. };
  465. struct matrix4 copy = identity;
  466. copy.t.x = floorf(m->t.x);
  467. copy.t.y = floorf(m->t.y);
  468. return memcmp(m, &copy, sizeof(*m)) == 0;
  469. }
  470. static inline void render_item(struct obs_scene_item *item)
  471. {
  472. GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_ITEM, "Item: %s",
  473. obs_source_get_name(item->source));
  474. if (item->item_render) {
  475. uint32_t width = obs_source_get_width(item->source);
  476. uint32_t height = obs_source_get_height(item->source);
  477. if (!width || !height) {
  478. goto cleanup;
  479. }
  480. uint32_t cx = calc_cx(item, width);
  481. uint32_t cy = calc_cy(item, height);
  482. if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
  483. float cx_scale = (float)width / (float)cx;
  484. float cy_scale = (float)height / (float)cy;
  485. struct vec4 clear_color;
  486. vec4_zero(&clear_color);
  487. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  488. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  489. -100.0f, 100.0f);
  490. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  491. gs_matrix_translate3f(-(float)item->crop.left,
  492. -(float)item->crop.top, 0.0f);
  493. if (item->user_visible &&
  494. transition_active(item->show_transition)) {
  495. const int cx =
  496. obs_source_get_width(item->source);
  497. const int cy =
  498. obs_source_get_height(item->source);
  499. obs_transition_set_size(item->show_transition,
  500. cx, cy);
  501. obs_source_video_render(item->show_transition);
  502. } else if (!item->user_visible &&
  503. transition_active(item->hide_transition)) {
  504. const int cx =
  505. obs_source_get_width(item->source);
  506. const int cy =
  507. obs_source_get_height(item->source);
  508. obs_transition_set_size(item->hide_transition,
  509. cx, cy);
  510. obs_source_video_render(item->hide_transition);
  511. } else {
  512. obs_source_set_texcoords_centered(item->source,
  513. true);
  514. obs_source_video_render(item->source);
  515. obs_source_set_texcoords_centered(item->source,
  516. false);
  517. }
  518. gs_texrender_end(item->item_render);
  519. }
  520. }
  521. const bool previous = gs_set_linear_srgb(true);
  522. gs_matrix_push();
  523. gs_matrix_mul(&item->draw_transform);
  524. if (item->item_render) {
  525. render_item_texture(item);
  526. } else if (item->user_visible &&
  527. transition_active(item->show_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->show_transition, cx, cy);
  531. obs_source_video_render(item->show_transition);
  532. } else if (!item->user_visible &&
  533. transition_active(item->hide_transition)) {
  534. const int cx = obs_source_get_width(item->source);
  535. const int cy = obs_source_get_height(item->source);
  536. obs_transition_set_size(item->hide_transition, cx, cy);
  537. obs_source_video_render(item->hide_transition);
  538. } else {
  539. const bool centered =
  540. are_texcoords_centered(&item->draw_transform);
  541. obs_source_set_texcoords_centered(item->source, centered);
  542. obs_source_video_render(item->source);
  543. obs_source_set_texcoords_centered(item->source, false);
  544. }
  545. gs_matrix_pop();
  546. gs_set_linear_srgb(previous);
  547. cleanup:
  548. GS_DEBUG_MARKER_END();
  549. }
  550. static void scene_video_tick(void *data, float seconds)
  551. {
  552. struct obs_scene *scene = data;
  553. struct obs_scene_item *item;
  554. video_lock(scene);
  555. item = scene->first_item;
  556. while (item) {
  557. if (item->item_render)
  558. gs_texrender_reset(item->item_render);
  559. item = item->next;
  560. }
  561. video_unlock(scene);
  562. UNUSED_PARAMETER(seconds);
  563. }
  564. /* assumes video lock */
  565. static void
  566. update_transforms_and_prune_sources(obs_scene_t *scene,
  567. struct darray *remove_items,
  568. obs_sceneitem_t *group_sceneitem)
  569. {
  570. struct obs_scene_item *item = scene->first_item;
  571. bool rebuild_group =
  572. group_sceneitem &&
  573. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  574. while (item) {
  575. if (obs_source_removed(item->source)) {
  576. struct obs_scene_item *del_item = item;
  577. item = item->next;
  578. remove_without_release(del_item);
  579. darray_push_back(sizeof(struct obs_scene_item *),
  580. remove_items, &del_item);
  581. rebuild_group = true;
  582. continue;
  583. }
  584. if (item->is_group) {
  585. obs_scene_t *group_scene = item->source->context.data;
  586. video_lock(group_scene);
  587. update_transforms_and_prune_sources(group_scene,
  588. remove_items, item);
  589. video_unlock(group_scene);
  590. }
  591. if (os_atomic_load_bool(&item->update_transform) ||
  592. source_size_changed(item)) {
  593. update_item_transform(item, true);
  594. rebuild_group = true;
  595. }
  596. item = item->next;
  597. }
  598. if (rebuild_group && group_sceneitem)
  599. resize_group(group_sceneitem);
  600. }
  601. static void scene_video_render(void *data, gs_effect_t *effect)
  602. {
  603. DARRAY(struct obs_scene_item *) remove_items;
  604. struct obs_scene *scene = data;
  605. struct obs_scene_item *item;
  606. da_init(remove_items);
  607. video_lock(scene);
  608. if (!scene->is_group) {
  609. update_transforms_and_prune_sources(scene, &remove_items.da,
  610. NULL);
  611. }
  612. gs_blend_state_push();
  613. gs_reset_blend_state();
  614. item = scene->first_item;
  615. while (item) {
  616. if (item->user_visible ||
  617. transition_active(item->hide_transition))
  618. render_item(item);
  619. item = item->next;
  620. }
  621. gs_blend_state_pop();
  622. video_unlock(scene);
  623. for (size_t i = 0; i < remove_items.num; i++)
  624. obs_sceneitem_release(remove_items.array[i]);
  625. da_free(remove_items);
  626. UNUSED_PARAMETER(effect);
  627. }
  628. static void set_visibility(struct obs_scene_item *item, bool vis)
  629. {
  630. pthread_mutex_lock(&item->actions_mutex);
  631. da_resize(item->audio_actions, 0);
  632. if (os_atomic_load_long(&item->active_refs) > 0) {
  633. if (!vis)
  634. obs_source_remove_active_child(item->parent->source,
  635. item->source);
  636. } else if (vis) {
  637. obs_source_add_active_child(item->parent->source, item->source);
  638. }
  639. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  640. item->visible = vis;
  641. item->user_visible = vis;
  642. pthread_mutex_unlock(&item->actions_mutex);
  643. }
  644. static void scene_load(void *data, obs_data_t *settings);
  645. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  646. {
  647. const char *name = obs_data_get_string(item_data, "name");
  648. obs_source_t *source;
  649. const char *scale_filter_str;
  650. struct obs_scene_item *item;
  651. bool visible;
  652. bool lock;
  653. if (obs_data_get_bool(item_data, "group_item_backup"))
  654. return;
  655. source = obs_get_source_by_name(name);
  656. if (!source) {
  657. blog(LOG_WARNING,
  658. "[scene_load_item] Source %s not "
  659. "found!",
  660. name);
  661. return;
  662. }
  663. item = obs_scene_add(scene, source);
  664. if (!item) {
  665. blog(LOG_WARNING,
  666. "[scene_load_item] Could not add source '%s' "
  667. "to scene '%s'!",
  668. name, obs_source_get_name(scene->source));
  669. obs_source_release(source);
  670. return;
  671. }
  672. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  673. obs_data_set_default_int(item_data, "align",
  674. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  675. if (obs_data_has_user_value(item_data, "id"))
  676. item->id = obs_data_get_int(item_data, "id");
  677. item->rot = (float)obs_data_get_double(item_data, "rot");
  678. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  679. visible = obs_data_get_bool(item_data, "visible");
  680. lock = obs_data_get_bool(item_data, "locked");
  681. obs_data_get_vec2(item_data, "pos", &item->pos);
  682. obs_data_get_vec2(item_data, "scale", &item->scale);
  683. obs_data_release(item->private_settings);
  684. item->private_settings =
  685. obs_data_get_obj(item_data, "private_settings");
  686. if (!item->private_settings)
  687. item->private_settings = obs_data_create();
  688. set_visibility(item, visible);
  689. obs_sceneitem_set_locked(item, lock);
  690. item->bounds_type = (enum obs_bounds_type)obs_data_get_int(
  691. item_data, "bounds_type");
  692. item->bounds_align =
  693. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  694. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  695. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  696. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  697. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  698. item->crop.bottom =
  699. (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  700. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  701. item->scale_filter = OBS_SCALE_DISABLE;
  702. if (scale_filter_str) {
  703. if (astrcmpi(scale_filter_str, "point") == 0)
  704. item->scale_filter = OBS_SCALE_POINT;
  705. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  706. item->scale_filter = OBS_SCALE_BILINEAR;
  707. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  708. item->scale_filter = OBS_SCALE_BICUBIC;
  709. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  710. item->scale_filter = OBS_SCALE_LANCZOS;
  711. else if (astrcmpi(scale_filter_str, "area") == 0)
  712. item->scale_filter = OBS_SCALE_AREA;
  713. }
  714. obs_data_t *show_data = obs_data_get_obj(item_data, "show_transition");
  715. if (show_data) {
  716. obs_sceneitem_transition_load(item, show_data, true);
  717. obs_data_release(show_data);
  718. }
  719. obs_data_t *hide_data = obs_data_get_obj(item_data, "hide_transition");
  720. if (hide_data) {
  721. obs_sceneitem_transition_load(item, hide_data, false);
  722. obs_data_release(hide_data);
  723. }
  724. if (item->item_render && !item_texture_enabled(item)) {
  725. obs_enter_graphics();
  726. gs_texrender_destroy(item->item_render);
  727. item->item_render = NULL;
  728. obs_leave_graphics();
  729. } else if (!item->item_render && item_texture_enabled(item)) {
  730. obs_enter_graphics();
  731. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  732. obs_leave_graphics();
  733. }
  734. obs_source_release(source);
  735. update_item_transform(item, false);
  736. }
  737. static void scene_load(void *data, obs_data_t *settings)
  738. {
  739. struct obs_scene *scene = data;
  740. obs_data_array_t *items = obs_data_get_array(settings, "items");
  741. size_t count, i;
  742. remove_all_items(scene);
  743. if (!items)
  744. return;
  745. count = obs_data_array_count(items);
  746. for (i = 0; i < count; i++) {
  747. obs_data_t *item_data = obs_data_array_item(items, i);
  748. scene_load_item(scene, item_data);
  749. obs_data_release(item_data);
  750. }
  751. if (obs_data_has_user_value(settings, "id_counter"))
  752. scene->id_counter = obs_data_get_int(settings, "id_counter");
  753. if (obs_data_get_bool(settings, "custom_size")) {
  754. scene->cx = (uint32_t)obs_data_get_int(settings, "cx");
  755. scene->cy = (uint32_t)obs_data_get_int(settings, "cy");
  756. scene->custom_size = true;
  757. }
  758. obs_data_array_release(items);
  759. }
  760. static void scene_save(void *data, obs_data_t *settings);
  761. static void scene_save_item(obs_data_array_t *array,
  762. struct obs_scene_item *item,
  763. struct obs_scene_item *backup_group)
  764. {
  765. obs_data_t *item_data = obs_data_create();
  766. const char *name = obs_source_get_name(item->source);
  767. const char *scale_filter;
  768. struct vec2 pos = item->pos;
  769. struct vec2 scale = item->scale;
  770. float rot = item->rot;
  771. if (backup_group) {
  772. get_ungrouped_transform(backup_group, &pos, &scale, &rot);
  773. }
  774. obs_data_set_string(item_data, "name", name);
  775. obs_data_set_bool(item_data, "visible", item->user_visible);
  776. obs_data_set_bool(item_data, "locked", item->locked);
  777. obs_data_set_double(item_data, "rot", rot);
  778. obs_data_set_vec2(item_data, "pos", &pos);
  779. obs_data_set_vec2(item_data, "scale", &scale);
  780. obs_data_set_int(item_data, "align", (int)item->align);
  781. obs_data_set_int(item_data, "bounds_type", (int)item->bounds_type);
  782. obs_data_set_int(item_data, "bounds_align", (int)item->bounds_align);
  783. obs_data_set_vec2(item_data, "bounds", &item->bounds);
  784. obs_data_set_int(item_data, "crop_left", (int)item->crop.left);
  785. obs_data_set_int(item_data, "crop_top", (int)item->crop.top);
  786. obs_data_set_int(item_data, "crop_right", (int)item->crop.right);
  787. obs_data_set_int(item_data, "crop_bottom", (int)item->crop.bottom);
  788. obs_data_set_int(item_data, "id", item->id);
  789. obs_data_set_bool(item_data, "group_item_backup", !!backup_group);
  790. if (item->is_group) {
  791. obs_scene_t *group_scene = item->source->context.data;
  792. obs_sceneitem_t *group_item;
  793. /* save group items as part of main scene, but ignored.
  794. * causes an automatic ungroup if scene collection file
  795. * is loaded in previous versions. */
  796. full_lock(group_scene);
  797. group_item = group_scene->first_item;
  798. while (group_item) {
  799. scene_save_item(array, group_item, item);
  800. group_item = group_item->next;
  801. }
  802. full_unlock(group_scene);
  803. }
  804. if (item->scale_filter == OBS_SCALE_POINT)
  805. scale_filter = "point";
  806. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  807. scale_filter = "bilinear";
  808. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  809. scale_filter = "bicubic";
  810. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  811. scale_filter = "lanczos";
  812. else if (item->scale_filter == OBS_SCALE_AREA)
  813. scale_filter = "area";
  814. else
  815. scale_filter = "disable";
  816. obs_data_set_string(item_data, "scale_filter", scale_filter);
  817. obs_data_t *show_data = obs_sceneitem_transition_save(item, true);
  818. obs_data_set_obj(item_data, "show_transition", show_data);
  819. obs_data_release(show_data);
  820. obs_data_t *hide_data = obs_sceneitem_transition_save(item, false);
  821. obs_data_set_obj(item_data, "hide_transition", hide_data);
  822. obs_data_release(hide_data);
  823. obs_data_set_obj(item_data, "private_settings", item->private_settings);
  824. obs_data_array_push_back(array, item_data);
  825. obs_data_release(item_data);
  826. }
  827. static void scene_save(void *data, obs_data_t *settings)
  828. {
  829. struct obs_scene *scene = data;
  830. obs_data_array_t *array = obs_data_array_create();
  831. struct obs_scene_item *item;
  832. full_lock(scene);
  833. item = scene->first_item;
  834. while (item) {
  835. scene_save_item(array, item, NULL);
  836. item = item->next;
  837. }
  838. obs_data_set_int(settings, "id_counter", scene->id_counter);
  839. obs_data_set_bool(settings, "custom_size", scene->custom_size);
  840. if (scene->custom_size) {
  841. obs_data_set_int(settings, "cx", scene->cx);
  842. obs_data_set_int(settings, "cy", scene->cy);
  843. }
  844. full_unlock(scene);
  845. obs_data_set_array(settings, "items", array);
  846. obs_data_array_release(array);
  847. }
  848. static uint32_t scene_getwidth(void *data)
  849. {
  850. obs_scene_t *scene = data;
  851. return scene->custom_size ? scene->cx : obs->video.base_width;
  852. }
  853. static uint32_t scene_getheight(void *data)
  854. {
  855. obs_scene_t *scene = data;
  856. return scene->custom_size ? scene->cy : obs->video.base_height;
  857. }
  858. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  859. float *buf, uint64_t ts,
  860. size_t sample_rate)
  861. {
  862. bool cur_visible = item->visible;
  863. uint64_t frame_num = 0;
  864. size_t deref_count = 0;
  865. pthread_mutex_lock(&item->actions_mutex);
  866. for (size_t i = 0; i < item->audio_actions.num; i++) {
  867. struct item_action action = item->audio_actions.array[i];
  868. uint64_t timestamp = action.timestamp;
  869. uint64_t new_frame_num;
  870. if (timestamp < ts)
  871. timestamp = ts;
  872. new_frame_num = util_mul_div64(timestamp - ts, sample_rate,
  873. 1000000000ULL);
  874. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  875. break;
  876. da_erase(item->audio_actions, i--);
  877. item->visible = action.visible;
  878. if (!item->visible)
  879. deref_count++;
  880. if (buf && new_frame_num > frame_num) {
  881. for (; frame_num < new_frame_num; frame_num++)
  882. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  883. }
  884. cur_visible = item->visible;
  885. }
  886. if (buf) {
  887. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  888. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  889. }
  890. pthread_mutex_unlock(&item->actions_mutex);
  891. while (deref_count--) {
  892. if (os_atomic_dec_long(&item->active_refs) == 0) {
  893. obs_source_remove_active_child(item->parent->source,
  894. item->source);
  895. }
  896. }
  897. }
  898. static bool apply_scene_item_volume(struct obs_scene_item *item, float *buf,
  899. uint64_t ts, size_t sample_rate)
  900. {
  901. bool actions_pending;
  902. struct item_action action;
  903. pthread_mutex_lock(&item->actions_mutex);
  904. actions_pending = item->audio_actions.num > 0;
  905. if (actions_pending)
  906. action = item->audio_actions.array[0];
  907. pthread_mutex_unlock(&item->actions_mutex);
  908. if (actions_pending) {
  909. uint64_t duration = util_mul_div64(AUDIO_OUTPUT_FRAMES,
  910. 1000000000ULL, sample_rate);
  911. if (!ts || action.timestamp < (ts + duration)) {
  912. apply_scene_item_audio_actions(item, buf, ts,
  913. sample_rate);
  914. return true;
  915. }
  916. }
  917. return false;
  918. }
  919. static void process_all_audio_actions(struct obs_scene_item *item,
  920. size_t sample_rate)
  921. {
  922. while (apply_scene_item_volume(item, NULL, 0, sample_rate))
  923. ;
  924. }
  925. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  926. size_t pos, size_t count)
  927. {
  928. register float *out = p_out;
  929. register float *buf = buf_in + pos;
  930. register float *in = p_in + pos;
  931. register float *end = in + count;
  932. while (in < end)
  933. *(out++) += *(in++) * *(buf++);
  934. }
  935. static inline void mix_audio(float *p_out, float *p_in, size_t pos,
  936. size_t count)
  937. {
  938. register float *out = p_out;
  939. register float *in = p_in + pos;
  940. register float *end = in + count;
  941. while (in < end)
  942. *(out++) += *(in++);
  943. }
  944. static bool scene_audio_render(void *data, uint64_t *ts_out,
  945. struct obs_source_audio_mix *audio_output,
  946. uint32_t mixers, size_t channels,
  947. size_t sample_rate)
  948. {
  949. uint64_t timestamp = 0;
  950. float buf[AUDIO_OUTPUT_FRAMES];
  951. struct obs_source_audio_mix child_audio;
  952. struct obs_scene *scene = data;
  953. struct obs_scene_item *item;
  954. audio_lock(scene);
  955. item = scene->first_item;
  956. while (item) {
  957. struct obs_source *source;
  958. if (item->visible && transition_active(item->show_transition))
  959. source = item->show_transition;
  960. else if (!item->visible &&
  961. transition_active(item->hide_transition))
  962. source = item->hide_transition;
  963. else
  964. source = item->source;
  965. if (!obs_source_audio_pending(source) &&
  966. (item->visible ||
  967. transition_active(item->hide_transition))) {
  968. uint64_t source_ts =
  969. obs_source_get_audio_timestamp(source);
  970. if (source_ts && (!timestamp || source_ts < timestamp))
  971. timestamp = source_ts;
  972. }
  973. item = item->next;
  974. }
  975. if (!timestamp) {
  976. /* just process all pending audio actions if no audio playing,
  977. * otherwise audio actions will just never be processed */
  978. item = scene->first_item;
  979. while (item) {
  980. process_all_audio_actions(item, sample_rate);
  981. item = item->next;
  982. }
  983. audio_unlock(scene);
  984. return false;
  985. }
  986. item = scene->first_item;
  987. while (item) {
  988. uint64_t source_ts;
  989. size_t pos, count;
  990. bool apply_buf;
  991. struct obs_source *source;
  992. if (item->visible && transition_active(item->show_transition))
  993. source = item->show_transition;
  994. else if (!item->visible &&
  995. transition_active(item->hide_transition))
  996. source = item->hide_transition;
  997. else
  998. source = item->source;
  999. apply_buf = apply_scene_item_volume(item, buf, timestamp,
  1000. sample_rate);
  1001. if (obs_source_audio_pending(source)) {
  1002. item = item->next;
  1003. continue;
  1004. }
  1005. source_ts = obs_source_get_audio_timestamp(source);
  1006. if (!source_ts) {
  1007. item = item->next;
  1008. continue;
  1009. }
  1010. pos = (size_t)ns_to_audio_frames(sample_rate,
  1011. source_ts - timestamp);
  1012. count = AUDIO_OUTPUT_FRAMES - pos;
  1013. if (!apply_buf && !item->visible &&
  1014. !transition_active(item->hide_transition)) {
  1015. item = item->next;
  1016. continue;
  1017. }
  1018. obs_source_get_audio_mix(source, &child_audio);
  1019. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  1020. if ((mixers & (1 << mix)) == 0)
  1021. continue;
  1022. for (size_t ch = 0; ch < channels; ch++) {
  1023. float *out = audio_output->output[mix].data[ch];
  1024. float *in = child_audio.output[mix].data[ch];
  1025. if (apply_buf)
  1026. mix_audio_with_buf(out, in, buf, pos,
  1027. count);
  1028. else
  1029. mix_audio(out, in, pos, count);
  1030. }
  1031. }
  1032. item = item->next;
  1033. }
  1034. *ts_out = timestamp;
  1035. audio_unlock(scene);
  1036. return true;
  1037. }
  1038. const struct obs_source_info scene_info = {
  1039. .id = "scene",
  1040. .type = OBS_SOURCE_TYPE_SCENE,
  1041. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1042. OBS_SOURCE_COMPOSITE | OBS_SOURCE_DO_NOT_DUPLICATE |
  1043. OBS_SOURCE_SRGB,
  1044. .get_name = scene_getname,
  1045. .create = scene_create,
  1046. .destroy = scene_destroy,
  1047. .video_tick = scene_video_tick,
  1048. .video_render = scene_video_render,
  1049. .audio_render = scene_audio_render,
  1050. .get_width = scene_getwidth,
  1051. .get_height = scene_getheight,
  1052. .load = scene_load,
  1053. .save = scene_save,
  1054. .enum_active_sources = scene_enum_active_sources,
  1055. .enum_all_sources = scene_enum_all_sources};
  1056. const struct obs_source_info group_info = {
  1057. .id = "group",
  1058. .type = OBS_SOURCE_TYPE_SCENE,
  1059. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1060. OBS_SOURCE_COMPOSITE | OBS_SOURCE_SRGB,
  1061. .get_name = group_getname,
  1062. .create = scene_create,
  1063. .destroy = scene_destroy,
  1064. .video_tick = scene_video_tick,
  1065. .video_render = scene_video_render,
  1066. .audio_render = scene_audio_render,
  1067. .get_width = scene_getwidth,
  1068. .get_height = scene_getheight,
  1069. .load = scene_load,
  1070. .save = scene_save,
  1071. .enum_active_sources = scene_enum_active_sources,
  1072. .enum_all_sources = scene_enum_all_sources};
  1073. static inline obs_scene_t *create_id(const char *id, const char *name)
  1074. {
  1075. struct obs_source *source = obs_source_create(id, name, NULL, NULL);
  1076. return source->context.data;
  1077. }
  1078. static inline obs_scene_t *create_private_id(const char *id, const char *name)
  1079. {
  1080. struct obs_source *source = obs_source_create_private(id, name, NULL);
  1081. return source->context.data;
  1082. }
  1083. obs_scene_t *obs_scene_create(const char *name)
  1084. {
  1085. return create_id("scene", name);
  1086. }
  1087. obs_scene_t *obs_scene_create_private(const char *name)
  1088. {
  1089. return create_private_id("scene", name);
  1090. }
  1091. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  1092. {
  1093. struct obs_scene_item *item = scene->first_item;
  1094. while (item && idx--)
  1095. item = item->next;
  1096. return item ? item->source : NULL;
  1097. }
  1098. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  1099. obs_scene_t *new_scene, bool private)
  1100. {
  1101. DARRAY(struct obs_scene_item *) old_items;
  1102. obs_source_t *source;
  1103. old_items.da = *array;
  1104. source = old_items.array[idx]->source;
  1105. /* if the old item is referenced more than once in the old scene,
  1106. * make sure they're referenced similarly in the new scene to reduce
  1107. * load times */
  1108. for (size_t i = 0; i < idx; i++) {
  1109. struct obs_scene_item *item = old_items.array[i];
  1110. if (item->source == source) {
  1111. source = get_child_at_idx(new_scene, i);
  1112. obs_source_addref(source);
  1113. return source;
  1114. }
  1115. }
  1116. return obs_source_duplicate(
  1117. source, private ? obs_source_get_name(source) : NULL, private);
  1118. }
  1119. static inline obs_source_t *new_ref(obs_source_t *source)
  1120. {
  1121. obs_source_addref(source);
  1122. return source;
  1123. }
  1124. static inline void duplicate_item_data(struct obs_scene_item *dst,
  1125. struct obs_scene_item *src,
  1126. bool defer_texture_update,
  1127. bool duplicate_hotkeys,
  1128. bool duplicate_private_data)
  1129. {
  1130. struct obs_scene *dst_scene = dst->parent;
  1131. if (!src->user_visible)
  1132. set_visibility(dst, false);
  1133. dst->selected = src->selected;
  1134. dst->pos = src->pos;
  1135. dst->rot = src->rot;
  1136. dst->scale = src->scale;
  1137. dst->align = src->align;
  1138. dst->last_width = src->last_width;
  1139. dst->last_height = src->last_height;
  1140. dst->output_scale = src->output_scale;
  1141. dst->scale_filter = src->scale_filter;
  1142. dst->box_transform = src->box_transform;
  1143. dst->box_scale = src->box_scale;
  1144. dst->draw_transform = src->draw_transform;
  1145. dst->bounds_type = src->bounds_type;
  1146. dst->bounds_align = src->bounds_align;
  1147. dst->bounds = src->bounds;
  1148. if (src->show_transition) {
  1149. obs_source_t *transition = obs_source_duplicate(
  1150. src->show_transition,
  1151. obs_source_get_name(src->show_transition), true);
  1152. obs_sceneitem_set_show_transition(dst, transition);
  1153. obs_source_release(transition);
  1154. }
  1155. if (src->hide_transition) {
  1156. obs_source_t *transition = obs_source_duplicate(
  1157. src->hide_transition,
  1158. obs_source_get_name(src->hide_transition), true);
  1159. obs_sceneitem_set_hide_transition(dst, transition);
  1160. obs_source_release(transition);
  1161. }
  1162. dst->show_transition_duration = src->show_transition_duration;
  1163. dst->hide_transition_duration = src->hide_transition_duration;
  1164. if (duplicate_hotkeys && !dst_scene->source->context.private) {
  1165. obs_data_array_t *data0 = NULL;
  1166. obs_data_array_t *data1 = NULL;
  1167. obs_hotkey_pair_save(src->toggle_visibility, &data0, &data1);
  1168. obs_hotkey_pair_load(dst->toggle_visibility, data0, data1);
  1169. obs_data_array_release(data0);
  1170. obs_data_array_release(data1);
  1171. }
  1172. obs_sceneitem_set_crop(dst, &src->crop);
  1173. obs_sceneitem_set_locked(dst, src->locked);
  1174. if (defer_texture_update) {
  1175. os_atomic_set_bool(&dst->update_transform, true);
  1176. } else {
  1177. if (!dst->item_render && item_texture_enabled(dst)) {
  1178. obs_enter_graphics();
  1179. dst->item_render =
  1180. gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1181. obs_leave_graphics();
  1182. }
  1183. }
  1184. if (duplicate_private_data) {
  1185. obs_data_apply(dst->private_settings, src->private_settings);
  1186. }
  1187. }
  1188. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  1189. enum obs_scene_duplicate_type type)
  1190. {
  1191. bool make_unique = ((int)type & (1 << 0)) != 0;
  1192. bool make_private = ((int)type & (1 << 1)) != 0;
  1193. DARRAY(struct obs_scene_item *) items;
  1194. struct obs_scene *new_scene;
  1195. struct obs_scene_item *item;
  1196. struct obs_source *source;
  1197. da_init(items);
  1198. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  1199. return NULL;
  1200. /* --------------------------------- */
  1201. full_lock(scene);
  1202. item = scene->first_item;
  1203. while (item) {
  1204. da_push_back(items, &item);
  1205. obs_sceneitem_addref(item);
  1206. item = item->next;
  1207. }
  1208. full_unlock(scene);
  1209. /* --------------------------------- */
  1210. new_scene = make_private
  1211. ? create_private_id(scene->source->info.id, name)
  1212. : create_id(scene->source->info.id, name);
  1213. obs_source_copy_filters(new_scene->source, scene->source);
  1214. obs_data_apply(new_scene->source->private_settings,
  1215. scene->source->private_settings);
  1216. /* never duplicate sub-items for groups */
  1217. if (scene->is_group)
  1218. make_unique = false;
  1219. for (size_t i = 0; i < items.num; i++) {
  1220. item = items.array[i];
  1221. source = make_unique ? dup_child(&items.da, i, new_scene,
  1222. make_private)
  1223. : new_ref(item->source);
  1224. if (source) {
  1225. struct obs_scene_item *new_item =
  1226. obs_scene_add(new_scene, source);
  1227. if (!new_item) {
  1228. obs_source_release(source);
  1229. continue;
  1230. }
  1231. duplicate_item_data(new_item, item, false, false,
  1232. false);
  1233. obs_source_release(source);
  1234. }
  1235. }
  1236. for (size_t i = 0; i < items.num; i++)
  1237. obs_sceneitem_release(items.array[i]);
  1238. if (new_scene->is_group)
  1239. resize_scene(new_scene);
  1240. da_free(items);
  1241. return new_scene;
  1242. }
  1243. void obs_scene_addref(obs_scene_t *scene)
  1244. {
  1245. if (scene)
  1246. obs_source_addref(scene->source);
  1247. }
  1248. void obs_scene_release(obs_scene_t *scene)
  1249. {
  1250. if (scene)
  1251. obs_source_release(scene->source);
  1252. }
  1253. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  1254. {
  1255. return scene ? scene->source : NULL;
  1256. }
  1257. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  1258. {
  1259. if (!source || strcmp(source->info.id, scene_info.id) != 0)
  1260. return NULL;
  1261. return source->context.data;
  1262. }
  1263. obs_scene_t *obs_group_from_source(const obs_source_t *source)
  1264. {
  1265. if (!source || strcmp(source->info.id, group_info.id) != 0)
  1266. return NULL;
  1267. return source->context.data;
  1268. }
  1269. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  1270. {
  1271. struct obs_scene_item *item;
  1272. if (!scene)
  1273. return NULL;
  1274. full_lock(scene);
  1275. item = scene->first_item;
  1276. while (item) {
  1277. if (strcmp(item->source->context.name, name) == 0)
  1278. break;
  1279. item = item->next;
  1280. }
  1281. full_unlock(scene);
  1282. return item;
  1283. }
  1284. obs_sceneitem_t *obs_scene_find_source_recursive(obs_scene_t *scene,
  1285. const char *name)
  1286. {
  1287. struct obs_scene_item *item;
  1288. if (!scene)
  1289. return NULL;
  1290. full_lock(scene);
  1291. item = scene->first_item;
  1292. while (item) {
  1293. if (strcmp(item->source->context.name, name) == 0)
  1294. break;
  1295. if (item->is_group) {
  1296. obs_scene_t *group = item->source->context.data;
  1297. obs_sceneitem_t *child =
  1298. obs_scene_find_source(group, name);
  1299. if (child) {
  1300. item = child;
  1301. break;
  1302. }
  1303. }
  1304. item = item->next;
  1305. }
  1306. full_unlock(scene);
  1307. return item;
  1308. }
  1309. struct sceneitem_check {
  1310. obs_source_t *source_in;
  1311. obs_sceneitem_t *item_out;
  1312. };
  1313. bool check_sceneitem_exists(obs_scene_t *scene, obs_sceneitem_t *item,
  1314. void *vp_check)
  1315. {
  1316. UNUSED_PARAMETER(scene);
  1317. struct sceneitem_check *check = (struct sceneitem_check *)vp_check;
  1318. if (obs_sceneitem_get_source(item) == check->source_in) {
  1319. check->item_out = item;
  1320. obs_sceneitem_addref(item);
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. obs_sceneitem_t *obs_scene_sceneitem_from_source(obs_scene_t *scene,
  1326. obs_source_t *source)
  1327. {
  1328. struct sceneitem_check check = {source, NULL};
  1329. obs_scene_enum_items(scene, check_sceneitem_exists, (void *)&check);
  1330. return check.item_out;
  1331. }
  1332. obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
  1333. {
  1334. struct obs_scene_item *item;
  1335. if (!scene)
  1336. return NULL;
  1337. full_lock(scene);
  1338. item = scene->first_item;
  1339. while (item) {
  1340. if (item->id == id)
  1341. break;
  1342. item = item->next;
  1343. }
  1344. full_unlock(scene);
  1345. return item;
  1346. }
  1347. void obs_scene_enum_items(obs_scene_t *scene,
  1348. bool (*callback)(obs_scene_t *, obs_sceneitem_t *,
  1349. void *),
  1350. void *param)
  1351. {
  1352. struct obs_scene_item *item;
  1353. if (!scene || !callback)
  1354. return;
  1355. full_lock(scene);
  1356. item = scene->first_item;
  1357. while (item) {
  1358. struct obs_scene_item *next = item->next;
  1359. obs_sceneitem_addref(item);
  1360. if (!callback(scene, item, param)) {
  1361. obs_sceneitem_release(item);
  1362. break;
  1363. }
  1364. obs_sceneitem_release(item);
  1365. item = next;
  1366. }
  1367. full_unlock(scene);
  1368. }
  1369. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  1370. {
  1371. long owners = os_atomic_load_long(&si->ref);
  1372. while (owners > 0) {
  1373. if (os_atomic_compare_exchange_long(&si->ref, &owners,
  1374. owners + 1)) {
  1375. return si;
  1376. }
  1377. }
  1378. return NULL;
  1379. }
  1380. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  1381. obs_hotkey_t *hotkey, bool pressed)
  1382. {
  1383. UNUSED_PARAMETER(id);
  1384. UNUSED_PARAMETER(hotkey);
  1385. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1386. if (pressed && si && !si->user_visible) {
  1387. obs_sceneitem_set_visible(si, true);
  1388. obs_sceneitem_release(si);
  1389. return true;
  1390. }
  1391. obs_sceneitem_release(si);
  1392. return false;
  1393. }
  1394. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  1395. obs_hotkey_t *hotkey, bool pressed)
  1396. {
  1397. UNUSED_PARAMETER(id);
  1398. UNUSED_PARAMETER(hotkey);
  1399. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1400. if (pressed && si && si->user_visible) {
  1401. obs_sceneitem_set_visible(si, false);
  1402. obs_sceneitem_release(si);
  1403. return true;
  1404. }
  1405. obs_sceneitem_release(si);
  1406. return false;
  1407. }
  1408. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  1409. const char *name)
  1410. {
  1411. struct dstr show = {0};
  1412. struct dstr hide = {0};
  1413. struct dstr show_desc = {0};
  1414. struct dstr hide_desc = {0};
  1415. dstr_copy(&show, "libobs.show_scene_item.%1");
  1416. dstr_replace(&show, "%1", name);
  1417. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1418. dstr_replace(&hide, "%1", name);
  1419. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1420. dstr_replace(&show_desc, "%1", name);
  1421. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1422. dstr_replace(&hide_desc, "%1", name);
  1423. item->toggle_visibility = obs_hotkey_pair_register_source(
  1424. scene->source, show.array, show_desc.array, hide.array,
  1425. hide_desc.array, hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1426. item, item);
  1427. dstr_free(&show);
  1428. dstr_free(&hide);
  1429. dstr_free(&show_desc);
  1430. dstr_free(&hide_desc);
  1431. }
  1432. static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
  1433. const char *new_name)
  1434. {
  1435. struct dstr show = {0};
  1436. struct dstr hide = {0};
  1437. struct dstr show_desc = {0};
  1438. struct dstr hide_desc = {0};
  1439. dstr_copy(&show, "libobs.show_scene_item.%1");
  1440. dstr_replace(&show, "%1", new_name);
  1441. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1442. dstr_replace(&hide, "%1", new_name);
  1443. obs_hotkey_pair_set_names(scene_item->toggle_visibility, show.array,
  1444. hide.array);
  1445. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1446. dstr_replace(&show_desc, "%1", new_name);
  1447. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1448. dstr_replace(&hide_desc, "%1", new_name);
  1449. obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
  1450. show_desc.array, hide_desc.array);
  1451. dstr_free(&show);
  1452. dstr_free(&hide);
  1453. dstr_free(&show_desc);
  1454. dstr_free(&hide_desc);
  1455. }
  1456. static void sceneitem_renamed(void *param, calldata_t *data)
  1457. {
  1458. obs_sceneitem_t *scene_item = param;
  1459. const char *name = calldata_string(data, "new_name");
  1460. sceneitem_rename_hotkey(scene_item, name);
  1461. }
  1462. static inline bool source_has_audio(obs_source_t *source)
  1463. {
  1464. return (source->info.output_flags &
  1465. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1466. }
  1467. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  1468. obs_source_t *source,
  1469. obs_sceneitem_t *insert_after)
  1470. {
  1471. struct obs_scene_item *last;
  1472. struct obs_scene_item *item;
  1473. pthread_mutex_t mutex;
  1474. struct item_action action = {.visible = true,
  1475. .timestamp = os_gettime_ns()};
  1476. if (!scene)
  1477. return NULL;
  1478. if (!source) {
  1479. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1480. return NULL;
  1481. }
  1482. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1483. blog(LOG_WARNING, "Failed to create scene item mutex");
  1484. return NULL;
  1485. }
  1486. if (!obs_source_add_active_child(scene->source, source)) {
  1487. blog(LOG_WARNING, "Failed to add source to scene due to "
  1488. "infinite source recursion");
  1489. pthread_mutex_destroy(&mutex);
  1490. return NULL;
  1491. }
  1492. item = bzalloc(sizeof(struct obs_scene_item));
  1493. item->source = source;
  1494. item->id = ++scene->id_counter;
  1495. item->parent = scene;
  1496. item->ref = 1;
  1497. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1498. item->actions_mutex = mutex;
  1499. item->user_visible = true;
  1500. item->locked = false;
  1501. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  1502. item->private_settings = obs_data_create();
  1503. item->toggle_visibility = OBS_INVALID_HOTKEY_PAIR_ID;
  1504. os_atomic_set_long(&item->active_refs, 1);
  1505. vec2_set(&item->scale, 1.0f, 1.0f);
  1506. matrix4_identity(&item->draw_transform);
  1507. matrix4_identity(&item->box_transform);
  1508. obs_source_addref(source);
  1509. if (source_has_audio(source)) {
  1510. item->visible = false;
  1511. da_push_back(item->audio_actions, &action);
  1512. } else {
  1513. item->visible = true;
  1514. }
  1515. if (item_texture_enabled(item)) {
  1516. obs_enter_graphics();
  1517. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1518. obs_leave_graphics();
  1519. }
  1520. full_lock(scene);
  1521. if (insert_after) {
  1522. obs_sceneitem_t *next = insert_after->next;
  1523. if (next)
  1524. next->prev = item;
  1525. item->next = insert_after->next;
  1526. item->prev = insert_after;
  1527. insert_after->next = item;
  1528. } else {
  1529. last = scene->first_item;
  1530. if (!last) {
  1531. scene->first_item = item;
  1532. } else {
  1533. while (last->next)
  1534. last = last->next;
  1535. last->next = item;
  1536. item->prev = last;
  1537. }
  1538. }
  1539. full_unlock(scene);
  1540. if (!scene->source->context.private)
  1541. init_hotkeys(scene, item, obs_source_get_name(source));
  1542. signal_handler_connect(obs_source_get_signal_handler(source), "rename",
  1543. sceneitem_renamed, item);
  1544. return item;
  1545. }
  1546. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1547. {
  1548. obs_sceneitem_t *item = obs_scene_add_internal(scene, source, NULL);
  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 =
  2409. obs_scene_add_internal(scene, sub_scene->source, last_item);
  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. duplicate_item_data(dst, last, true, true, true);
  2499. apply_group_transform(last, item);
  2500. if (!last->next)
  2501. break;
  2502. insert_after = dst;
  2503. last = last->next;
  2504. }
  2505. full_unlock(subscene);
  2506. /* ------------------------- */
  2507. detach_sceneitem(item);
  2508. full_unlock(scene);
  2509. obs_sceneitem_release(item);
  2510. }
  2511. void obs_sceneitem_group_ungroup2(obs_sceneitem_t *item, bool signal)
  2512. {
  2513. obs_scene_t *scene = item->parent;
  2514. obs_sceneitem_group_ungroup(item);
  2515. if (signal)
  2516. signal_refresh(scene);
  2517. }
  2518. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2519. {
  2520. if (!group || !group->is_group || !item)
  2521. return;
  2522. obs_scene_t *scene = group->parent;
  2523. obs_scene_t *groupscene = group->source->context.data;
  2524. if (item->parent != scene)
  2525. return;
  2526. if (item->parent == groupscene)
  2527. return;
  2528. /* ------------------------- */
  2529. full_lock(scene);
  2530. full_lock(groupscene);
  2531. remove_group_transform(group, item);
  2532. detach_sceneitem(item);
  2533. attach_sceneitem(groupscene, item, NULL);
  2534. apply_group_transform(item, group);
  2535. resize_group(group);
  2536. full_unlock(groupscene);
  2537. full_unlock(scene);
  2538. /* ------------------------- */
  2539. signal_refresh(scene);
  2540. }
  2541. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2542. obs_sceneitem_t *item)
  2543. {
  2544. if (!item || !group || !group->is_group)
  2545. return;
  2546. obs_scene_t *groupscene = item->parent;
  2547. obs_scene_t *scene = group->parent;
  2548. /* ------------------------- */
  2549. full_lock(scene);
  2550. full_lock(groupscene);
  2551. remove_group_transform(group, item);
  2552. detach_sceneitem(item);
  2553. attach_sceneitem(scene, item, NULL);
  2554. resize_group(group);
  2555. full_unlock(groupscene);
  2556. full_unlock(scene);
  2557. /* ------------------------- */
  2558. signal_refresh(scene);
  2559. }
  2560. static void
  2561. build_current_order_info(obs_scene_t *scene,
  2562. struct obs_sceneitem_order_info **items_out,
  2563. size_t *size_out)
  2564. {
  2565. DARRAY(struct obs_sceneitem_order_info) items;
  2566. da_init(items);
  2567. obs_sceneitem_t *item = scene->first_item;
  2568. while (item) {
  2569. da_push_back(items, &item);
  2570. if (item->is_group) {
  2571. obs_scene_t *sub_scene = item->source->context.data;
  2572. full_lock(sub_scene);
  2573. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2574. while (sub_item) {
  2575. da_push_back(items, &item);
  2576. sub_item = sub_item->next;
  2577. }
  2578. full_unlock(sub_scene);
  2579. }
  2580. item = item->next;
  2581. }
  2582. *items_out = items.array;
  2583. *size_out = items.num;
  2584. }
  2585. static bool sceneitems_match2(obs_scene_t *scene,
  2586. struct obs_sceneitem_order_info *items,
  2587. size_t size)
  2588. {
  2589. struct obs_sceneitem_order_info *cur_items;
  2590. size_t cur_size;
  2591. build_current_order_info(scene, &cur_items, &cur_size);
  2592. if (cur_size != size) {
  2593. bfree(cur_items);
  2594. return false;
  2595. }
  2596. for (size_t i = 0; i < size; i++) {
  2597. struct obs_sceneitem_order_info *new = &items[i];
  2598. struct obs_sceneitem_order_info *old = &cur_items[i];
  2599. if (new->group != old->group || new->item != old->item) {
  2600. bfree(cur_items);
  2601. return false;
  2602. }
  2603. }
  2604. bfree(cur_items);
  2605. return true;
  2606. }
  2607. static obs_sceneitem_t *
  2608. get_sceneitem_parent_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem)
  2609. {
  2610. if (group_subitem->is_group)
  2611. return NULL;
  2612. obs_sceneitem_t *item = scene->first_item;
  2613. while (item) {
  2614. if (item->is_group &&
  2615. item->source->context.data == group_subitem->parent)
  2616. return item;
  2617. item = item->next;
  2618. }
  2619. return NULL;
  2620. }
  2621. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2622. struct obs_sceneitem_order_info *item_order,
  2623. size_t item_order_size)
  2624. {
  2625. if (!scene || !item_order_size || !item_order)
  2626. return false;
  2627. obs_scene_addref(scene);
  2628. full_lock(scene);
  2629. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2630. full_unlock(scene);
  2631. obs_scene_release(scene);
  2632. return false;
  2633. }
  2634. for (size_t i = 0; i < item_order_size; i++) {
  2635. struct obs_sceneitem_order_info *info = &item_order[i];
  2636. if (!info->item->is_group) {
  2637. obs_sceneitem_t *group =
  2638. get_sceneitem_parent_group(scene, info->item);
  2639. remove_group_transform(group, info->item);
  2640. }
  2641. }
  2642. scene->first_item = item_order[0].item;
  2643. obs_sceneitem_t *prev = NULL;
  2644. for (size_t i = 0; i < item_order_size; i++) {
  2645. struct obs_sceneitem_order_info *info = &item_order[i];
  2646. obs_sceneitem_t *item = info->item;
  2647. if (info->item->is_group) {
  2648. obs_sceneitem_t *sub_prev = NULL;
  2649. obs_scene_t *sub_scene =
  2650. info->item->source->context.data;
  2651. sub_scene->first_item = NULL;
  2652. obs_scene_addref(sub_scene);
  2653. full_lock(sub_scene);
  2654. for (i++; i < item_order_size; i++) {
  2655. struct obs_sceneitem_order_info *sub_info =
  2656. &item_order[i];
  2657. obs_sceneitem_t *sub_item = sub_info->item;
  2658. if (sub_info->group != info->item) {
  2659. i--;
  2660. break;
  2661. }
  2662. if (!sub_scene->first_item)
  2663. sub_scene->first_item = sub_item;
  2664. sub_item->prev = sub_prev;
  2665. sub_item->next = NULL;
  2666. sub_item->parent = sub_scene;
  2667. if (sub_prev)
  2668. sub_prev->next = sub_item;
  2669. apply_group_transform(sub_info->item,
  2670. sub_info->group);
  2671. sub_prev = sub_item;
  2672. }
  2673. resize_group(info->item);
  2674. full_unlock(sub_scene);
  2675. obs_scene_release(sub_scene);
  2676. }
  2677. item->prev = prev;
  2678. item->next = NULL;
  2679. item->parent = scene;
  2680. if (prev)
  2681. prev->next = item;
  2682. prev = item;
  2683. }
  2684. full_unlock(scene);
  2685. signal_reorder(scene->first_item);
  2686. obs_scene_release(scene);
  2687. return true;
  2688. }
  2689. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  2690. obs_sceneitem_t *group_subitem)
  2691. {
  2692. if (!scene || !group_subitem || group_subitem->is_group)
  2693. return NULL;
  2694. full_lock(scene);
  2695. obs_sceneitem_t *group =
  2696. get_sceneitem_parent_group(scene, group_subitem);
  2697. full_unlock(scene);
  2698. return group;
  2699. }
  2700. bool obs_source_is_group(const obs_source_t *source)
  2701. {
  2702. return source && strcmp(source->info.id, group_info.id) == 0;
  2703. }
  2704. bool obs_source_is_scene(const obs_source_t *source)
  2705. {
  2706. return source && strcmp(source->info.id, scene_info.id) == 0;
  2707. }
  2708. bool obs_scene_is_group(const obs_scene_t *scene)
  2709. {
  2710. return scene ? scene->is_group : false;
  2711. }
  2712. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  2713. bool (*callback)(obs_scene_t *,
  2714. obs_sceneitem_t *, void *),
  2715. void *param)
  2716. {
  2717. if (!group || !group->is_group)
  2718. return;
  2719. obs_scene_t *scene = group->source->context.data;
  2720. if (scene)
  2721. obs_scene_enum_items(scene, callback, param);
  2722. }
  2723. void obs_sceneitem_force_update_transform(obs_sceneitem_t *item)
  2724. {
  2725. if (!item)
  2726. return;
  2727. if (os_atomic_set_bool(&item->update_transform, false))
  2728. update_item_transform(item, false);
  2729. }
  2730. void obs_sceneitem_set_show_transition(obs_sceneitem_t *item,
  2731. obs_source_t *transition)
  2732. {
  2733. if (!item)
  2734. return;
  2735. if (item->show_transition)
  2736. obs_source_release(item->show_transition);
  2737. item->show_transition = transition;
  2738. if (item->show_transition)
  2739. obs_source_addref(item->show_transition);
  2740. }
  2741. void obs_sceneitem_set_show_transition_duration(obs_sceneitem_t *item,
  2742. uint32_t duration_ms)
  2743. {
  2744. if (!item)
  2745. return;
  2746. item->show_transition_duration = duration_ms;
  2747. }
  2748. obs_source_t *obs_sceneitem_get_show_transition(obs_sceneitem_t *item)
  2749. {
  2750. if (!item)
  2751. return NULL;
  2752. return item->show_transition;
  2753. }
  2754. uint32_t obs_sceneitem_get_show_transition_duration(obs_sceneitem_t *item)
  2755. {
  2756. if (!item)
  2757. return 0;
  2758. return item->show_transition_duration;
  2759. }
  2760. void obs_sceneitem_set_hide_transition(obs_sceneitem_t *item,
  2761. obs_source_t *transition)
  2762. {
  2763. if (!item)
  2764. return;
  2765. if (item->hide_transition)
  2766. obs_source_release(item->hide_transition);
  2767. item->hide_transition = transition;
  2768. if (item->hide_transition)
  2769. obs_source_addref(item->hide_transition);
  2770. }
  2771. void obs_sceneitem_set_hide_transition_duration(obs_sceneitem_t *item,
  2772. uint32_t duration_ms)
  2773. {
  2774. if (!item)
  2775. return;
  2776. item->hide_transition_duration = duration_ms;
  2777. }
  2778. obs_source_t *obs_sceneitem_get_hide_transition(obs_sceneitem_t *item)
  2779. {
  2780. if (!item)
  2781. return NULL;
  2782. return item->hide_transition;
  2783. }
  2784. uint32_t obs_sceneitem_get_hide_transition_duration(obs_sceneitem_t *item)
  2785. {
  2786. if (!item)
  2787. return 0;
  2788. return item->hide_transition_duration;
  2789. }
  2790. void obs_sceneitem_transition_stop(void *data, calldata_t *calldata)
  2791. {
  2792. obs_source_t *parent = data;
  2793. obs_source_t *transition;
  2794. calldata_get_ptr(calldata, "source", &transition);
  2795. obs_source_remove_active_child(parent, transition);
  2796. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2797. if (sh)
  2798. signal_handler_disconnect(sh, "transition_stop",
  2799. obs_sceneitem_transition_stop,
  2800. parent);
  2801. }
  2802. void obs_sceneitem_do_transition(obs_sceneitem_t *item, bool visible)
  2803. {
  2804. if (!item)
  2805. return;
  2806. if (transition_active(item->show_transition))
  2807. obs_transition_force_stop(item->show_transition);
  2808. if (transition_active(item->hide_transition))
  2809. obs_transition_force_stop(item->hide_transition);
  2810. obs_source_t *transition =
  2811. visible ? obs_sceneitem_get_show_transition(item)
  2812. : obs_sceneitem_get_hide_transition(item);
  2813. if (!transition)
  2814. return;
  2815. int duration =
  2816. (int)(visible ? obs_sceneitem_get_show_transition_duration(item)
  2817. : obs_sceneitem_get_hide_transition_duration(
  2818. item));
  2819. const int cx = obs_source_get_width(item->source);
  2820. const int cy = obs_source_get_height(item->source);
  2821. obs_transition_set_size(transition, cx, cy);
  2822. obs_transition_set_alignment(transition, OBS_ALIGN_CENTER);
  2823. obs_transition_set_scale_type(transition, OBS_TRANSITION_SCALE_ASPECT);
  2824. if (duration == 0)
  2825. duration = 300;
  2826. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  2827. obs_source_t *parent = obs_scene_get_source(scene);
  2828. obs_source_add_active_child(parent, transition);
  2829. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2830. if (sh)
  2831. signal_handler_connect(sh, "transition_stop",
  2832. obs_sceneitem_transition_stop, parent);
  2833. if (!visible) {
  2834. obs_transition_set(transition, item->source);
  2835. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2836. duration, NULL);
  2837. } else {
  2838. obs_transition_set(transition, NULL);
  2839. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2840. duration, item->source);
  2841. }
  2842. }
  2843. void obs_sceneitem_transition_load(struct obs_scene_item *item,
  2844. obs_data_t *data, bool show)
  2845. {
  2846. if (!item || !data)
  2847. return;
  2848. const char *id = obs_data_get_string(data, "id");
  2849. if (id && strlen(id)) {
  2850. const char *tn = obs_data_get_string(data, "name");
  2851. obs_data_t *s = obs_data_get_obj(data, "transition");
  2852. obs_source_t *t = obs_source_create_private(id, tn, s);
  2853. if (show)
  2854. obs_sceneitem_set_show_transition(item, t);
  2855. else
  2856. obs_sceneitem_set_hide_transition(item, t);
  2857. obs_source_release(t);
  2858. obs_data_release(s);
  2859. }
  2860. if (show)
  2861. item->show_transition_duration =
  2862. (uint32_t)obs_data_get_int(data, "duration");
  2863. else
  2864. item->hide_transition_duration =
  2865. (uint32_t)obs_data_get_int(data, "duration");
  2866. }
  2867. obs_data_t *obs_sceneitem_transition_save(struct obs_scene_item *item,
  2868. bool show)
  2869. {
  2870. obs_data_t *data = obs_data_create();
  2871. struct obs_source *transition = show ? item->show_transition
  2872. : item->hide_transition;
  2873. if (transition) {
  2874. obs_data_set_string(data, "id",
  2875. obs_source_get_unversioned_id(transition));
  2876. obs_data_set_string(data, "versioned_id",
  2877. obs_source_get_id(transition));
  2878. obs_data_set_string(data, "name",
  2879. obs_source_get_name(transition));
  2880. obs_data_t *s = obs_source_get_settings(transition);
  2881. obs_data_set_obj(data, "transition", s);
  2882. obs_data_release(s);
  2883. }
  2884. obs_data_set_int(data, "duration",
  2885. show ? item->show_transition_duration
  2886. : item->hide_transition_duration);
  2887. return data;
  2888. }