obs-scene.c 69 KB

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