obs-scene.c 68 KB

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