obs-scene.c 68 KB

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