1
0

obs-scene.c 69 KB

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