obs-scene.c 70 KB

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