obs-scene.c 71 KB

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