obs-scene.c 71 KB

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