obs-scene.c 71 KB

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