1
0

obs-scene.c 69 KB

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