obs-scene.c 70 KB

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