obs-scene.c 71 KB

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