obs-scene.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 "util/util_uint64.h"
  17. #include "graphics/math-defs.h"
  18. #include "obs-scene.h"
  19. #include "obs-internal.h"
  20. const struct obs_source_info group_info;
  21. static void resize_group(obs_sceneitem_t *group);
  22. static void resize_scene(obs_scene_t *scene);
  23. static void signal_parent(obs_scene_t *parent, const char *name,
  24. calldata_t *params);
  25. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  26. struct vec2 *scale, float *rot);
  27. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop);
  28. static inline bool item_texture_enabled(const struct obs_scene_item *item);
  29. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  30. const char *name);
  31. typedef DARRAY(struct obs_scene_item *) obs_scene_item_ptr_array_t;
  32. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  33. * lock the graphics mutex inside either of the scene mutexes.
  34. *
  35. * Another thing that must be done to prevent that cross-lock (and improve
  36. * performance), is to not create/release/update sources within the scene
  37. * mutexes.
  38. *
  39. * It's okay to lock the graphics mutex before locking either of the scene
  40. * mutexes, but not after.
  41. */
  42. static const char *obs_scene_signals[] = {
  43. "void item_add(ptr scene, ptr item)",
  44. "void item_remove(ptr scene, ptr item)",
  45. "void reorder(ptr scene)",
  46. "void refresh(ptr scene)",
  47. "void item_visible(ptr scene, ptr item, bool visible)",
  48. "void item_select(ptr scene, ptr item)",
  49. "void item_deselect(ptr scene, ptr item)",
  50. "void item_transform(ptr scene, ptr item)",
  51. "void item_locked(ptr scene, ptr item, bool locked)",
  52. NULL,
  53. };
  54. static const struct {
  55. enum gs_blend_type src_color;
  56. enum gs_blend_type src_alpha;
  57. enum gs_blend_type dst_color;
  58. enum gs_blend_type dst_alpha;
  59. enum gs_blend_op_type op;
  60. } obs_blend_mode_params[] = {
  61. /* clang-format off */
  62. // OBS_BLEND_NORMAL
  63. {
  64. GS_BLEND_ONE,
  65. GS_BLEND_ONE,
  66. GS_BLEND_INVSRCALPHA,
  67. GS_BLEND_INVSRCALPHA,
  68. GS_BLEND_OP_ADD,
  69. },
  70. // OBS_BLEND_ADDITIVE
  71. {
  72. GS_BLEND_ONE,
  73. GS_BLEND_ONE,
  74. GS_BLEND_ONE,
  75. GS_BLEND_ONE,
  76. GS_BLEND_OP_ADD,
  77. },
  78. // OBS_BLEND_SUBTRACT
  79. {
  80. GS_BLEND_ONE,
  81. GS_BLEND_ONE,
  82. GS_BLEND_ONE,
  83. GS_BLEND_ONE,
  84. GS_BLEND_OP_REVERSE_SUBTRACT,
  85. },
  86. // OBS_BLEND_SCREEN
  87. {
  88. GS_BLEND_ONE,
  89. GS_BLEND_ONE,
  90. GS_BLEND_INVSRCCOLOR,
  91. GS_BLEND_INVSRCALPHA,
  92. GS_BLEND_OP_ADD
  93. },
  94. // OBS_BLEND_MULTIPLY
  95. {
  96. GS_BLEND_DSTCOLOR,
  97. GS_BLEND_DSTALPHA,
  98. GS_BLEND_INVSRCALPHA,
  99. GS_BLEND_INVSRCALPHA,
  100. GS_BLEND_OP_ADD
  101. },
  102. // OBS_BLEND_LIGHTEN
  103. {
  104. GS_BLEND_ONE,
  105. GS_BLEND_ONE,
  106. GS_BLEND_ONE,
  107. GS_BLEND_ONE,
  108. GS_BLEND_OP_MAX,
  109. },
  110. // OBS_BLEND_DARKEN
  111. {
  112. GS_BLEND_ONE,
  113. GS_BLEND_ONE,
  114. GS_BLEND_ONE,
  115. GS_BLEND_ONE,
  116. GS_BLEND_OP_MIN,
  117. },
  118. /* clang-format on */
  119. };
  120. static inline void signal_item_remove(struct obs_scene_item *item)
  121. {
  122. struct calldata params;
  123. uint8_t stack[128];
  124. calldata_init_fixed(&params, stack, sizeof(stack));
  125. calldata_set_ptr(&params, "item", item);
  126. signal_parent(item->parent, "item_remove", &params);
  127. }
  128. static const char *scene_getname(void *unused)
  129. {
  130. UNUSED_PARAMETER(unused);
  131. return "Scene";
  132. }
  133. static const char *group_getname(void *unused)
  134. {
  135. UNUSED_PARAMETER(unused);
  136. return "Group";
  137. }
  138. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  139. {
  140. struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
  141. scene->source = source;
  142. if (strcmp(source->info.id, group_info.id) == 0) {
  143. scene->is_group = true;
  144. scene->custom_size = true;
  145. scene->cx = 0;
  146. scene->cy = 0;
  147. }
  148. signal_handler_add_array(obs_source_get_signal_handler(source),
  149. obs_scene_signals);
  150. if (pthread_mutex_init_recursive(&scene->audio_mutex) != 0) {
  151. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  152. "mutex");
  153. goto fail;
  154. }
  155. if (pthread_mutex_init_recursive(&scene->video_mutex) != 0) {
  156. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  157. "mutex");
  158. goto fail;
  159. }
  160. UNUSED_PARAMETER(settings);
  161. return scene;
  162. fail:
  163. bfree(scene);
  164. return NULL;
  165. }
  166. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  167. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  168. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  169. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  170. static inline void full_lock(struct obs_scene *scene)
  171. {
  172. video_lock(scene);
  173. audio_lock(scene);
  174. }
  175. static inline void full_unlock(struct obs_scene *scene)
  176. {
  177. audio_unlock(scene);
  178. video_unlock(scene);
  179. }
  180. static void set_visibility(struct obs_scene_item *item, bool vis);
  181. static inline void detach_sceneitem(struct obs_scene_item *item);
  182. static inline void remove_without_release(struct obs_scene_item *item)
  183. {
  184. item->removed = true;
  185. set_visibility(item, false);
  186. signal_item_remove(item);
  187. detach_sceneitem(item);
  188. }
  189. static void remove_all_items(struct obs_scene *scene)
  190. {
  191. struct obs_scene_item *item;
  192. obs_scene_item_ptr_array_t items;
  193. da_init(items);
  194. full_lock(scene);
  195. item = scene->first_item;
  196. while (item) {
  197. struct obs_scene_item *del_item = item;
  198. item = item->next;
  199. remove_without_release(del_item);
  200. da_push_back(items, &del_item);
  201. }
  202. full_unlock(scene);
  203. for (size_t i = 0; i < items.num; i++)
  204. obs_sceneitem_release(items.array[i]);
  205. da_free(items);
  206. }
  207. static void scene_destroy(void *data)
  208. {
  209. struct obs_scene *scene = data;
  210. remove_all_items(scene);
  211. pthread_mutex_destroy(&scene->video_mutex);
  212. pthread_mutex_destroy(&scene->audio_mutex);
  213. bfree(scene);
  214. }
  215. static inline bool transition_active(obs_source_t *transition)
  216. {
  217. return transition && (transition->transitioning_audio ||
  218. transition->transitioning_video);
  219. }
  220. static void scene_enum_sources(void *data, obs_source_enum_proc_t enum_callback,
  221. void *param, bool active)
  222. {
  223. struct obs_scene *scene = data;
  224. struct obs_scene_item *item;
  225. struct obs_scene_item *next;
  226. full_lock(scene);
  227. item = scene->first_item;
  228. while (item) {
  229. next = item->next;
  230. obs_sceneitem_addref(item);
  231. if (active) {
  232. if (item->visible &&
  233. transition_active(item->show_transition))
  234. enum_callback(scene->source,
  235. item->show_transition, param);
  236. else if (!item->visible &&
  237. transition_active(item->hide_transition))
  238. enum_callback(scene->source,
  239. item->hide_transition, param);
  240. else if (os_atomic_load_long(&item->active_refs) > 0)
  241. enum_callback(scene->source, item->source,
  242. param);
  243. } else {
  244. if (item->show_transition)
  245. enum_callback(scene->source,
  246. item->show_transition, param);
  247. if (item->hide_transition)
  248. enum_callback(scene->source,
  249. item->hide_transition, param);
  250. enum_callback(scene->source, item->source, param);
  251. }
  252. obs_sceneitem_release(item);
  253. item = next;
  254. }
  255. full_unlock(scene);
  256. }
  257. static void scene_enum_active_sources(void *data,
  258. obs_source_enum_proc_t enum_callback,
  259. void *param)
  260. {
  261. scene_enum_sources(data, enum_callback, param, true);
  262. }
  263. static void scene_enum_all_sources(void *data,
  264. obs_source_enum_proc_t enum_callback,
  265. void *param)
  266. {
  267. scene_enum_sources(data, enum_callback, param, false);
  268. }
  269. static inline void detach_sceneitem(struct obs_scene_item *item)
  270. {
  271. if (item->prev)
  272. item->prev->next = item->next;
  273. else
  274. item->parent->first_item = item->next;
  275. if (item->next)
  276. item->next->prev = item->prev;
  277. item->parent = NULL;
  278. }
  279. static inline void attach_sceneitem(struct obs_scene *parent,
  280. struct obs_scene_item *item,
  281. struct obs_scene_item *prev)
  282. {
  283. item->prev = prev;
  284. item->parent = parent;
  285. if (prev) {
  286. item->next = prev->next;
  287. if (prev->next)
  288. prev->next->prev = item;
  289. prev->next = item;
  290. } else {
  291. item->next = parent->first_item;
  292. if (parent->first_item)
  293. parent->first_item->prev = item;
  294. parent->first_item = item;
  295. }
  296. }
  297. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  298. {
  299. if (align & OBS_ALIGN_RIGHT)
  300. v->x += (float)cx;
  301. else if ((align & OBS_ALIGN_LEFT) == 0)
  302. v->x += (float)(cx / 2);
  303. if (align & OBS_ALIGN_BOTTOM)
  304. v->y += (float)cy;
  305. else if ((align & OBS_ALIGN_TOP) == 0)
  306. v->y += (float)(cy / 2);
  307. }
  308. static void calculate_bounds_data(struct obs_scene_item *item,
  309. struct vec2 *origin, struct vec2 *scale,
  310. uint32_t *cx, uint32_t *cy)
  311. {
  312. float width = (float)(*cx) * fabsf(scale->x);
  313. float height = (float)(*cy) * fabsf(scale->y);
  314. float item_aspect = width / height;
  315. float bounds_aspect = item->bounds.x / item->bounds.y;
  316. uint32_t bounds_type = item->bounds_type;
  317. float width_diff, height_diff;
  318. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  319. if (width > item->bounds.x || height > item->bounds.y)
  320. bounds_type = OBS_BOUNDS_SCALE_INNER;
  321. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  322. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  323. bool use_width = (bounds_aspect < item_aspect);
  324. float mul;
  325. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  326. use_width = !use_width;
  327. mul = use_width ? item->bounds.x / width
  328. : item->bounds.y / height;
  329. vec2_mulf(scale, scale, mul);
  330. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  331. vec2_mulf(scale, scale, item->bounds.x / width);
  332. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  333. vec2_mulf(scale, scale, item->bounds.y / height);
  334. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  335. scale->x = copysignf(item->bounds.x / (float)(*cx), scale->x);
  336. scale->y = copysignf(item->bounds.y / (float)(*cy), scale->y);
  337. }
  338. width = (float)(*cx) * scale->x;
  339. height = (float)(*cy) * scale->y;
  340. /* Disregards flip when calculating size diff */
  341. width_diff = item->bounds.x - fabsf(width);
  342. height_diff = item->bounds.y - fabsf(height);
  343. *cx = (uint32_t)item->bounds.x;
  344. *cy = (uint32_t)item->bounds.y;
  345. add_alignment(origin, item->bounds_align, (int)-width_diff,
  346. (int)-height_diff);
  347. /* Makes the item stay in-place in the box if flipped */
  348. origin->x += (width < 0.0f) ? width : 0.0f;
  349. origin->y += (height < 0.0f) ? height : 0.0f;
  350. }
  351. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  352. uint32_t width)
  353. {
  354. uint32_t crop_cx = item->crop.left + item->crop.right;
  355. return (crop_cx > width) ? 2 : (width - crop_cx);
  356. }
  357. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  358. uint32_t height)
  359. {
  360. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  361. return (crop_cy > height) ? 2 : (height - crop_cy);
  362. }
  363. static void update_item_transform(struct obs_scene_item *item, bool update_tex)
  364. {
  365. uint32_t width;
  366. uint32_t height;
  367. uint32_t cx;
  368. uint32_t cy;
  369. struct vec2 base_origin;
  370. struct vec2 origin;
  371. struct vec2 scale;
  372. struct calldata params;
  373. uint8_t stack[128];
  374. if (os_atomic_load_long(&item->defer_update) > 0)
  375. return;
  376. width = obs_source_get_width(item->source);
  377. height = obs_source_get_height(item->source);
  378. cx = calc_cx(item, width);
  379. cy = calc_cy(item, height);
  380. scale = item->scale;
  381. item->last_width = width;
  382. item->last_height = height;
  383. width = cx;
  384. height = cy;
  385. vec2_zero(&base_origin);
  386. vec2_zero(&origin);
  387. /* ----------------------- */
  388. if (item->bounds_type != OBS_BOUNDS_NONE) {
  389. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  390. } else {
  391. cx = (uint32_t)((float)cx * scale.x);
  392. cy = (uint32_t)((float)cy * scale.y);
  393. }
  394. add_alignment(&origin, item->align, (int)cx, (int)cy);
  395. matrix4_identity(&item->draw_transform);
  396. matrix4_scale3f(&item->draw_transform, &item->draw_transform, scale.x,
  397. scale.y, 1.0f);
  398. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  399. -origin.x, -origin.y, 0.0f);
  400. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform, 0.0f,
  401. 0.0f, 1.0f, RAD(item->rot));
  402. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  403. item->pos.x, item->pos.y, 0.0f);
  404. item->output_scale = scale;
  405. /* ----------------------- */
  406. if (item->bounds_type != OBS_BOUNDS_NONE) {
  407. vec2_copy(&scale, &item->bounds);
  408. } else {
  409. scale.x = (float)width * item->scale.x;
  410. scale.y = (float)height * item->scale.y;
  411. }
  412. item->box_scale = scale;
  413. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  414. matrix4_identity(&item->box_transform);
  415. matrix4_scale3f(&item->box_transform, &item->box_transform, scale.x,
  416. scale.y, 1.0f);
  417. matrix4_translate3f(&item->box_transform, &item->box_transform,
  418. -base_origin.x, -base_origin.y, 0.0f);
  419. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform, 0.0f,
  420. 0.0f, 1.0f, RAD(item->rot));
  421. matrix4_translate3f(&item->box_transform, &item->box_transform,
  422. item->pos.x, item->pos.y, 0.0f);
  423. /* ----------------------- */
  424. calldata_init_fixed(&params, stack, sizeof(stack));
  425. calldata_set_ptr(&params, "item", item);
  426. signal_parent(item->parent, "item_transform", &params);
  427. if (!update_tex)
  428. return;
  429. os_atomic_set_bool(&item->update_transform, false);
  430. }
  431. static inline bool source_size_changed(struct obs_scene_item *item)
  432. {
  433. uint32_t width = obs_source_get_width(item->source);
  434. uint32_t height = obs_source_get_height(item->source);
  435. return item->last_width != width || item->last_height != height;
  436. }
  437. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  438. {
  439. return crop->left || crop->right || crop->top || crop->bottom;
  440. }
  441. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  442. {
  443. return item->scale_filter != OBS_SCALE_DISABLE;
  444. }
  445. static inline bool default_blending_enabled(const struct obs_scene_item *item)
  446. {
  447. return item->blend_type == OBS_BLEND_NORMAL;
  448. }
  449. static inline bool item_is_scene(const struct obs_scene_item *item)
  450. {
  451. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  452. }
  453. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  454. {
  455. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  456. (item->blend_method == OBS_BLEND_METHOD_SRGB_OFF) ||
  457. !default_blending_enabled(item) ||
  458. (item_is_scene(item) && !item->is_group);
  459. }
  460. static void render_item_texture(struct obs_scene_item *item,
  461. enum gs_color_space current_space,
  462. enum gs_color_space source_space)
  463. {
  464. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  465. if (!tex) {
  466. return;
  467. }
  468. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_ITEM_TEXTURE,
  469. "render_item_texture");
  470. gs_effect_t *effect = obs->video.default_effect;
  471. enum obs_scale_type type = item->scale_filter;
  472. uint32_t cx = gs_texture_get_width(tex);
  473. uint32_t cy = gs_texture_get_height(tex);
  474. bool upscale = false;
  475. if (type != OBS_SCALE_DISABLE) {
  476. if (type == OBS_SCALE_POINT) {
  477. gs_eparam_t *image =
  478. gs_effect_get_param_by_name(effect, "image");
  479. gs_effect_set_next_sampler(image,
  480. obs->video.point_sampler);
  481. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  482. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  483. if (item->output_scale.x < 0.5f ||
  484. item->output_scale.y < 0.5f) {
  485. effect = obs->video.bilinear_lowres_effect;
  486. } else if (type == OBS_SCALE_BICUBIC) {
  487. effect = obs->video.bicubic_effect;
  488. } else if (type == OBS_SCALE_LANCZOS) {
  489. effect = obs->video.lanczos_effect;
  490. } else if (type == OBS_SCALE_AREA) {
  491. effect = obs->video.area_effect;
  492. upscale = (item->output_scale.x >= 1.0f) &&
  493. (item->output_scale.y >= 1.0f);
  494. }
  495. gs_eparam_t *const scale_param =
  496. gs_effect_get_param_by_name(effect,
  497. "base_dimension");
  498. if (scale_param) {
  499. struct vec2 base_res = {(float)cx, (float)cy};
  500. gs_effect_set_vec2(scale_param, &base_res);
  501. }
  502. gs_eparam_t *const scale_i_param =
  503. gs_effect_get_param_by_name(effect,
  504. "base_dimension_i");
  505. if (scale_i_param) {
  506. struct vec2 base_res_i = {1.0f / (float)cx,
  507. 1.0f / (float)cy};
  508. gs_effect_set_vec2(scale_i_param, &base_res_i);
  509. }
  510. }
  511. }
  512. float multiplier = 1.f;
  513. if (current_space == GS_CS_709_SCRGB) {
  514. switch (source_space) {
  515. case GS_CS_SRGB:
  516. case GS_CS_SRGB_16F:
  517. case GS_CS_709_EXTENDED:
  518. multiplier = obs_get_video_sdr_white_level() / 80.f;
  519. break;
  520. case GS_CS_709_SCRGB:
  521. break;
  522. }
  523. }
  524. if (source_space == GS_CS_709_SCRGB) {
  525. switch (current_space) {
  526. case GS_CS_SRGB:
  527. case GS_CS_SRGB_16F:
  528. case GS_CS_709_EXTENDED:
  529. multiplier = 80.f / obs_get_video_sdr_white_level();
  530. break;
  531. case GS_CS_709_SCRGB:
  532. break;
  533. }
  534. }
  535. const char *tech_name = "Draw";
  536. if (upscale) {
  537. tech_name = "DrawUpscale";
  538. switch (source_space) {
  539. case GS_CS_SRGB:
  540. case GS_CS_SRGB_16F:
  541. if (current_space == GS_CS_709_SCRGB)
  542. tech_name = "DrawUpscaleMultiply";
  543. break;
  544. case GS_CS_709_EXTENDED:
  545. if (current_space == GS_CS_SRGB ||
  546. current_space == GS_CS_SRGB_16F)
  547. tech_name = "DrawUpscaleTonemap";
  548. else if (current_space == GS_CS_709_SCRGB)
  549. tech_name = "DrawUpscaleMultiply";
  550. break;
  551. case GS_CS_709_SCRGB:
  552. if (current_space == GS_CS_SRGB ||
  553. current_space == GS_CS_SRGB_16F)
  554. tech_name = "DrawUpscaleMultiplyTonemap";
  555. else if (current_space == GS_CS_709_EXTENDED)
  556. tech_name = "DrawUpscaleMultiply";
  557. break;
  558. }
  559. } else {
  560. switch (source_space) {
  561. case GS_CS_SRGB:
  562. case GS_CS_SRGB_16F:
  563. if (current_space == GS_CS_709_SCRGB)
  564. tech_name = "DrawMultiply";
  565. break;
  566. case GS_CS_709_EXTENDED:
  567. if (current_space == GS_CS_SRGB ||
  568. current_space == GS_CS_SRGB_16F)
  569. tech_name = "DrawTonemap";
  570. else if (current_space == GS_CS_709_SCRGB)
  571. tech_name = "DrawMultiply";
  572. break;
  573. case GS_CS_709_SCRGB:
  574. if (current_space == GS_CS_SRGB ||
  575. current_space == GS_CS_SRGB_16F)
  576. tech_name = "DrawMultiplyTonemap";
  577. else if (current_space == GS_CS_709_EXTENDED)
  578. tech_name = "DrawMultiply";
  579. break;
  580. }
  581. }
  582. gs_eparam_t *const multiplier_param =
  583. gs_effect_get_param_by_name(effect, "multiplier");
  584. if (multiplier_param)
  585. gs_effect_set_float(multiplier_param, multiplier);
  586. gs_blend_state_push();
  587. gs_blend_function_separate(
  588. obs_blend_mode_params[item->blend_type].src_color,
  589. obs_blend_mode_params[item->blend_type].dst_color,
  590. obs_blend_mode_params[item->blend_type].src_alpha,
  591. obs_blend_mode_params[item->blend_type].dst_alpha);
  592. gs_blend_op(obs_blend_mode_params[item->blend_type].op);
  593. while (gs_effect_loop(effect, tech_name))
  594. obs_source_draw(tex, 0, 0, 0, 0, 0);
  595. gs_blend_state_pop();
  596. GS_DEBUG_MARKER_END();
  597. }
  598. static bool are_texcoords_centered(struct matrix4 *m)
  599. {
  600. static const struct matrix4 identity = {
  601. {1.0f, 0.0f, 0.0f, 0.0f},
  602. {0.0f, 1.0f, 0.0f, 0.0f},
  603. {0.0f, 0.0f, 1.0f, 0.0f},
  604. {0.0f, 0.0f, 0.0f, 1.0f},
  605. };
  606. struct matrix4 copy = identity;
  607. copy.t.x = floorf(m->t.x);
  608. copy.t.y = floorf(m->t.y);
  609. return memcmp(m, &copy, sizeof(*m)) == 0;
  610. }
  611. static inline void render_item(struct obs_scene_item *item)
  612. {
  613. GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_ITEM, "Item: %s",
  614. obs_source_get_name(item->source));
  615. const bool use_texrender = item_texture_enabled(item);
  616. obs_source_t *const source = item->source;
  617. const enum gs_color_space current_space = gs_get_color_space();
  618. const enum gs_color_space source_space =
  619. obs_source_get_color_space(source, 1, &current_space);
  620. const enum gs_color_format format =
  621. gs_get_format_from_space(source_space);
  622. if (item->item_render &&
  623. (!use_texrender ||
  624. (gs_texrender_get_format(item->item_render) != format))) {
  625. gs_texrender_destroy(item->item_render);
  626. item->item_render = NULL;
  627. }
  628. if (!item->item_render && use_texrender) {
  629. item->item_render = gs_texrender_create(format, GS_ZS_NONE);
  630. }
  631. if (item->item_render) {
  632. uint32_t width = obs_source_get_width(item->source);
  633. uint32_t height = obs_source_get_height(item->source);
  634. if (!width || !height) {
  635. goto cleanup;
  636. }
  637. uint32_t cx = calc_cx(item, width);
  638. uint32_t cy = calc_cy(item, height);
  639. if (cx && cy &&
  640. gs_texrender_begin_with_color_space(item->item_render, cx,
  641. cy, source_space)) {
  642. float cx_scale = (float)width / (float)cx;
  643. float cy_scale = (float)height / (float)cy;
  644. struct vec4 clear_color;
  645. vec4_zero(&clear_color);
  646. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  647. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  648. -100.0f, 100.0f);
  649. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  650. gs_matrix_translate3f(-(float)item->crop.left,
  651. -(float)item->crop.top, 0.0f);
  652. if (item->user_visible &&
  653. transition_active(item->show_transition)) {
  654. const int cx =
  655. obs_source_get_width(item->source);
  656. const int cy =
  657. obs_source_get_height(item->source);
  658. obs_transition_set_size(item->show_transition,
  659. cx, cy);
  660. obs_source_video_render(item->show_transition);
  661. } else if (!item->user_visible &&
  662. transition_active(item->hide_transition)) {
  663. const int cx =
  664. obs_source_get_width(item->source);
  665. const int cy =
  666. obs_source_get_height(item->source);
  667. obs_transition_set_size(item->hide_transition,
  668. cx, cy);
  669. obs_source_video_render(item->hide_transition);
  670. } else {
  671. obs_source_set_texcoords_centered(item->source,
  672. true);
  673. obs_source_video_render(item->source);
  674. obs_source_set_texcoords_centered(item->source,
  675. false);
  676. }
  677. gs_texrender_end(item->item_render);
  678. }
  679. }
  680. const bool linear_srgb =
  681. !item->item_render ||
  682. (item->blend_method != OBS_BLEND_METHOD_SRGB_OFF);
  683. const bool previous = gs_set_linear_srgb(linear_srgb);
  684. gs_matrix_push();
  685. gs_matrix_mul(&item->draw_transform);
  686. if (item->item_render) {
  687. render_item_texture(item, current_space, source_space);
  688. } else if (item->user_visible &&
  689. transition_active(item->show_transition)) {
  690. const int cx = obs_source_get_width(item->source);
  691. const int cy = obs_source_get_height(item->source);
  692. obs_transition_set_size(item->show_transition, cx, cy);
  693. obs_source_video_render(item->show_transition);
  694. } else if (!item->user_visible &&
  695. transition_active(item->hide_transition)) {
  696. const int cx = obs_source_get_width(item->source);
  697. const int cy = obs_source_get_height(item->source);
  698. obs_transition_set_size(item->hide_transition, cx, cy);
  699. obs_source_video_render(item->hide_transition);
  700. } else {
  701. const bool centered =
  702. are_texcoords_centered(&item->draw_transform);
  703. obs_source_set_texcoords_centered(item->source, centered);
  704. obs_source_video_render(item->source);
  705. obs_source_set_texcoords_centered(item->source, false);
  706. }
  707. gs_matrix_pop();
  708. gs_set_linear_srgb(previous);
  709. cleanup:
  710. GS_DEBUG_MARKER_END();
  711. }
  712. static void scene_video_tick(void *data, float seconds)
  713. {
  714. struct obs_scene *scene = data;
  715. struct obs_scene_item *item;
  716. video_lock(scene);
  717. item = scene->first_item;
  718. while (item) {
  719. if (item->item_render)
  720. gs_texrender_reset(item->item_render);
  721. item = item->next;
  722. }
  723. video_unlock(scene);
  724. UNUSED_PARAMETER(seconds);
  725. }
  726. /* assumes video lock */
  727. static void
  728. update_transforms_and_prune_sources(obs_scene_t *scene,
  729. obs_scene_item_ptr_array_t *remove_items,
  730. obs_sceneitem_t *group_sceneitem)
  731. {
  732. struct obs_scene_item *item = scene->first_item;
  733. bool rebuild_group =
  734. group_sceneitem &&
  735. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  736. while (item) {
  737. if (obs_source_removed(item->source)) {
  738. struct obs_scene_item *del_item = item;
  739. item = item->next;
  740. remove_without_release(del_item);
  741. da_push_back(*remove_items, &del_item);
  742. rebuild_group = true;
  743. continue;
  744. }
  745. if (item->is_group) {
  746. obs_scene_t *group_scene = item->source->context.data;
  747. video_lock(group_scene);
  748. update_transforms_and_prune_sources(group_scene,
  749. remove_items, item);
  750. video_unlock(group_scene);
  751. }
  752. if (os_atomic_load_bool(&item->update_transform) ||
  753. source_size_changed(item)) {
  754. update_item_transform(item, true);
  755. rebuild_group = true;
  756. }
  757. item = item->next;
  758. }
  759. if (rebuild_group && group_sceneitem)
  760. resize_group(group_sceneitem);
  761. }
  762. static void scene_video_render(void *data, gs_effect_t *effect)
  763. {
  764. obs_scene_item_ptr_array_t remove_items;
  765. struct obs_scene *scene = data;
  766. struct obs_scene_item *item;
  767. da_init(remove_items);
  768. video_lock(scene);
  769. if (!scene->is_group) {
  770. update_transforms_and_prune_sources(scene, &remove_items, NULL);
  771. }
  772. gs_blend_state_push();
  773. gs_reset_blend_state();
  774. item = scene->first_item;
  775. while (item) {
  776. if (item->user_visible ||
  777. transition_active(item->hide_transition))
  778. render_item(item);
  779. item = item->next;
  780. }
  781. gs_blend_state_pop();
  782. video_unlock(scene);
  783. for (size_t i = 0; i < remove_items.num; i++)
  784. obs_sceneitem_release(remove_items.array[i]);
  785. da_free(remove_items);
  786. UNUSED_PARAMETER(effect);
  787. }
  788. static void set_visibility(struct obs_scene_item *item, bool vis)
  789. {
  790. pthread_mutex_lock(&item->actions_mutex);
  791. da_resize(item->audio_actions, 0);
  792. if (os_atomic_load_long(&item->active_refs) > 0) {
  793. if (!vis)
  794. obs_source_remove_active_child(item->parent->source,
  795. item->source);
  796. } else if (vis) {
  797. obs_source_add_active_child(item->parent->source, item->source);
  798. }
  799. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  800. item->visible = vis;
  801. item->user_visible = vis;
  802. pthread_mutex_unlock(&item->actions_mutex);
  803. }
  804. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  805. obs_source_t *source,
  806. obs_sceneitem_t *insert_after,
  807. int64_t id);
  808. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  809. {
  810. const char *name = obs_data_get_string(item_data, "name");
  811. const char *src_uuid = obs_data_get_string(item_data, "source_uuid");
  812. obs_source_t *source = NULL;
  813. const char *scale_filter_str;
  814. const char *blend_method_str;
  815. const char *blend_str;
  816. struct obs_scene_item *item;
  817. struct calldata params;
  818. uint8_t stack[128];
  819. bool visible;
  820. bool lock;
  821. if (obs_data_get_bool(item_data, "group_item_backup"))
  822. return;
  823. if (src_uuid && strlen(src_uuid) == UUID_STR_LENGTH)
  824. source = obs_get_source_by_uuid(src_uuid);
  825. /* Fall back to name if UUID was not found or is not set. */
  826. if (!source)
  827. source = obs_get_source_by_name(name);
  828. if (!source) {
  829. blog(LOG_WARNING,
  830. "[scene_load_item] Source %s not "
  831. "found!",
  832. name);
  833. return;
  834. }
  835. item = obs_scene_add_internal(scene, source, NULL,
  836. obs_data_get_int(item_data, "id"));
  837. if (!item) {
  838. blog(LOG_WARNING,
  839. "[scene_load_item] Could not add source '%s' "
  840. "to scene '%s'!",
  841. name, obs_source_get_name(scene->source));
  842. obs_source_release(source);
  843. return;
  844. }
  845. calldata_init_fixed(&params, stack, sizeof(stack));
  846. calldata_set_ptr(&params, "scene", scene);
  847. calldata_set_ptr(&params, "item", item);
  848. signal_handler_signal(scene->source->context.signals, "item_add",
  849. &params);
  850. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  851. obs_data_set_default_int(item_data, "align",
  852. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  853. item->rot = (float)obs_data_get_double(item_data, "rot");
  854. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  855. visible = obs_data_get_bool(item_data, "visible");
  856. lock = obs_data_get_bool(item_data, "locked");
  857. obs_data_get_vec2(item_data, "pos", &item->pos);
  858. obs_data_get_vec2(item_data, "scale", &item->scale);
  859. obs_data_release(item->private_settings);
  860. item->private_settings =
  861. obs_data_get_obj(item_data, "private_settings");
  862. if (!item->private_settings)
  863. item->private_settings = obs_data_create();
  864. set_visibility(item, visible);
  865. obs_sceneitem_set_locked(item, lock);
  866. item->bounds_type = (enum obs_bounds_type)obs_data_get_int(
  867. item_data, "bounds_type");
  868. item->bounds_align =
  869. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  870. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  871. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  872. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  873. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  874. item->crop.bottom =
  875. (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  876. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  877. item->scale_filter = OBS_SCALE_DISABLE;
  878. if (scale_filter_str) {
  879. if (astrcmpi(scale_filter_str, "point") == 0)
  880. item->scale_filter = OBS_SCALE_POINT;
  881. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  882. item->scale_filter = OBS_SCALE_BILINEAR;
  883. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  884. item->scale_filter = OBS_SCALE_BICUBIC;
  885. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  886. item->scale_filter = OBS_SCALE_LANCZOS;
  887. else if (astrcmpi(scale_filter_str, "area") == 0)
  888. item->scale_filter = OBS_SCALE_AREA;
  889. }
  890. blend_method_str = obs_data_get_string(item_data, "blend_method");
  891. item->blend_method = OBS_BLEND_METHOD_DEFAULT;
  892. if (blend_method_str) {
  893. if (astrcmpi(blend_method_str, "srgb_off") == 0)
  894. item->blend_method = OBS_BLEND_METHOD_SRGB_OFF;
  895. }
  896. blend_str = obs_data_get_string(item_data, "blend_type");
  897. item->blend_type = OBS_BLEND_NORMAL;
  898. if (blend_str) {
  899. if (astrcmpi(blend_str, "additive") == 0)
  900. item->blend_type = OBS_BLEND_ADDITIVE;
  901. else if (astrcmpi(blend_str, "subtract") == 0)
  902. item->blend_type = OBS_BLEND_SUBTRACT;
  903. else if (astrcmpi(blend_str, "screen") == 0)
  904. item->blend_type = OBS_BLEND_SCREEN;
  905. else if (astrcmpi(blend_str, "multiply") == 0)
  906. item->blend_type = OBS_BLEND_MULTIPLY;
  907. else if (astrcmpi(blend_str, "lighten") == 0)
  908. item->blend_type = OBS_BLEND_LIGHTEN;
  909. else if (astrcmpi(blend_str, "darken") == 0)
  910. item->blend_type = OBS_BLEND_DARKEN;
  911. }
  912. obs_data_t *show_data = obs_data_get_obj(item_data, "show_transition");
  913. if (show_data) {
  914. obs_sceneitem_transition_load(item, show_data, true);
  915. obs_data_release(show_data);
  916. }
  917. obs_data_t *hide_data = obs_data_get_obj(item_data, "hide_transition");
  918. if (hide_data) {
  919. obs_sceneitem_transition_load(item, hide_data, false);
  920. obs_data_release(hide_data);
  921. }
  922. obs_source_release(source);
  923. update_item_transform(item, false);
  924. }
  925. static void scene_load(void *data, obs_data_t *settings)
  926. {
  927. struct obs_scene *scene = data;
  928. obs_data_array_t *items = obs_data_get_array(settings, "items");
  929. size_t count, i;
  930. remove_all_items(scene);
  931. if (obs_data_get_bool(settings, "custom_size")) {
  932. scene->cx = (uint32_t)obs_data_get_int(settings, "cx");
  933. scene->cy = (uint32_t)obs_data_get_int(settings, "cy");
  934. scene->custom_size = true;
  935. }
  936. if (obs_data_has_user_value(settings, "id_counter"))
  937. scene->id_counter = obs_data_get_int(settings, "id_counter");
  938. if (!items)
  939. return;
  940. count = obs_data_array_count(items);
  941. for (i = 0; i < count; i++) {
  942. obs_data_t *item_data = obs_data_array_item(items, i);
  943. scene_load_item(scene, item_data);
  944. obs_data_release(item_data);
  945. }
  946. obs_data_array_release(items);
  947. }
  948. static void scene_save(void *data, obs_data_t *settings);
  949. static void scene_save_item(obs_data_array_t *array,
  950. struct obs_scene_item *item,
  951. struct obs_scene_item *backup_group)
  952. {
  953. obs_data_t *item_data = obs_data_create();
  954. const char *name = obs_source_get_name(item->source);
  955. const char *src_uuid = obs_source_get_uuid(item->source);
  956. const char *scale_filter;
  957. const char *blend_method;
  958. const char *blend_type;
  959. struct vec2 pos = item->pos;
  960. struct vec2 scale = item->scale;
  961. float rot = item->rot;
  962. if (backup_group) {
  963. get_ungrouped_transform(backup_group, &pos, &scale, &rot);
  964. }
  965. obs_data_set_string(item_data, "name", name);
  966. obs_data_set_string(item_data, "source_uuid", src_uuid);
  967. obs_data_set_bool(item_data, "visible", item->user_visible);
  968. obs_data_set_bool(item_data, "locked", item->locked);
  969. obs_data_set_double(item_data, "rot", rot);
  970. obs_data_set_vec2(item_data, "pos", &pos);
  971. obs_data_set_vec2(item_data, "scale", &scale);
  972. obs_data_set_int(item_data, "align", (int)item->align);
  973. obs_data_set_int(item_data, "bounds_type", (int)item->bounds_type);
  974. obs_data_set_int(item_data, "bounds_align", (int)item->bounds_align);
  975. obs_data_set_vec2(item_data, "bounds", &item->bounds);
  976. obs_data_set_int(item_data, "crop_left", (int)item->crop.left);
  977. obs_data_set_int(item_data, "crop_top", (int)item->crop.top);
  978. obs_data_set_int(item_data, "crop_right", (int)item->crop.right);
  979. obs_data_set_int(item_data, "crop_bottom", (int)item->crop.bottom);
  980. obs_data_set_int(item_data, "id", item->id);
  981. obs_data_set_bool(item_data, "group_item_backup", !!backup_group);
  982. if (item->is_group) {
  983. obs_scene_t *group_scene = item->source->context.data;
  984. obs_sceneitem_t *group_item;
  985. /* save group items as part of main scene, but ignored.
  986. * causes an automatic ungroup if scene collection file
  987. * is loaded in previous versions. */
  988. full_lock(group_scene);
  989. group_item = group_scene->first_item;
  990. while (group_item) {
  991. scene_save_item(array, group_item, item);
  992. group_item = group_item->next;
  993. }
  994. full_unlock(group_scene);
  995. }
  996. if (item->scale_filter == OBS_SCALE_POINT)
  997. scale_filter = "point";
  998. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  999. scale_filter = "bilinear";
  1000. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  1001. scale_filter = "bicubic";
  1002. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  1003. scale_filter = "lanczos";
  1004. else if (item->scale_filter == OBS_SCALE_AREA)
  1005. scale_filter = "area";
  1006. else
  1007. scale_filter = "disable";
  1008. obs_data_set_string(item_data, "scale_filter", scale_filter);
  1009. if (item->blend_method == OBS_BLEND_METHOD_SRGB_OFF)
  1010. blend_method = "srgb_off";
  1011. else
  1012. blend_method = "default";
  1013. obs_data_set_string(item_data, "blend_method", blend_method);
  1014. if (item->blend_type == OBS_BLEND_NORMAL)
  1015. blend_type = "normal";
  1016. else if (item->blend_type == OBS_BLEND_ADDITIVE)
  1017. blend_type = "additive";
  1018. else if (item->blend_type == OBS_BLEND_SUBTRACT)
  1019. blend_type = "subtract";
  1020. else if (item->blend_type == OBS_BLEND_SCREEN)
  1021. blend_type = "screen";
  1022. else if (item->blend_type == OBS_BLEND_MULTIPLY)
  1023. blend_type = "multiply";
  1024. else if (item->blend_type == OBS_BLEND_LIGHTEN)
  1025. blend_type = "lighten";
  1026. else if (item->blend_type == OBS_BLEND_DARKEN)
  1027. blend_type = "darken";
  1028. else
  1029. blend_type = "normal";
  1030. obs_data_set_string(item_data, "blend_type", blend_type);
  1031. obs_data_t *show_data = obs_sceneitem_transition_save(item, true);
  1032. obs_data_set_obj(item_data, "show_transition", show_data);
  1033. obs_data_release(show_data);
  1034. obs_data_t *hide_data = obs_sceneitem_transition_save(item, false);
  1035. obs_data_set_obj(item_data, "hide_transition", hide_data);
  1036. obs_data_release(hide_data);
  1037. obs_data_set_obj(item_data, "private_settings", item->private_settings);
  1038. obs_data_array_push_back(array, item_data);
  1039. obs_data_release(item_data);
  1040. }
  1041. static void scene_save(void *data, obs_data_t *settings)
  1042. {
  1043. struct obs_scene *scene = data;
  1044. obs_data_array_t *array = obs_data_array_create();
  1045. struct obs_scene_item *item;
  1046. full_lock(scene);
  1047. item = scene->first_item;
  1048. while (item) {
  1049. scene_save_item(array, item, NULL);
  1050. item = item->next;
  1051. }
  1052. obs_data_set_int(settings, "id_counter", scene->id_counter);
  1053. obs_data_set_bool(settings, "custom_size", scene->custom_size);
  1054. if (scene->custom_size) {
  1055. obs_data_set_int(settings, "cx", scene->cx);
  1056. obs_data_set_int(settings, "cy", scene->cy);
  1057. }
  1058. full_unlock(scene);
  1059. obs_data_set_array(settings, "items", array);
  1060. obs_data_array_release(array);
  1061. }
  1062. static uint32_t scene_getwidth(void *data)
  1063. {
  1064. obs_scene_t *scene = data;
  1065. if (scene->custom_size)
  1066. return scene->cx;
  1067. if (obs->video.main_mix)
  1068. return obs->video.main_mix->ovi.base_width;
  1069. return 0;
  1070. }
  1071. static uint32_t scene_getheight(void *data)
  1072. {
  1073. obs_scene_t *scene = data;
  1074. if (scene->custom_size)
  1075. return scene->cy;
  1076. if (obs->video.main_mix)
  1077. return obs->video.main_mix->ovi.base_height;
  1078. return 0;
  1079. }
  1080. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  1081. float *buf, uint64_t ts,
  1082. size_t sample_rate)
  1083. {
  1084. bool cur_visible = item->visible;
  1085. uint64_t frame_num = 0;
  1086. size_t deref_count = 0;
  1087. pthread_mutex_lock(&item->actions_mutex);
  1088. for (size_t i = 0; i < item->audio_actions.num; i++) {
  1089. struct item_action action = item->audio_actions.array[i];
  1090. uint64_t timestamp = action.timestamp;
  1091. uint64_t new_frame_num;
  1092. if (timestamp < ts)
  1093. timestamp = ts;
  1094. new_frame_num = util_mul_div64(timestamp - ts, sample_rate,
  1095. 1000000000ULL);
  1096. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  1097. break;
  1098. da_erase(item->audio_actions, i--);
  1099. item->visible = action.visible;
  1100. if (!item->visible)
  1101. deref_count++;
  1102. if (buf && new_frame_num > frame_num) {
  1103. for (; frame_num < new_frame_num; frame_num++)
  1104. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  1105. }
  1106. cur_visible = item->visible;
  1107. }
  1108. if (buf) {
  1109. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  1110. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  1111. }
  1112. pthread_mutex_unlock(&item->actions_mutex);
  1113. while (deref_count--) {
  1114. if (os_atomic_dec_long(&item->active_refs) == 0) {
  1115. obs_source_remove_active_child(item->parent->source,
  1116. item->source);
  1117. }
  1118. }
  1119. }
  1120. static bool apply_scene_item_volume(struct obs_scene_item *item, float *buf,
  1121. uint64_t ts, size_t sample_rate)
  1122. {
  1123. bool actions_pending;
  1124. struct item_action action;
  1125. pthread_mutex_lock(&item->actions_mutex);
  1126. actions_pending = item->audio_actions.num > 0;
  1127. if (actions_pending)
  1128. action = item->audio_actions.array[0];
  1129. pthread_mutex_unlock(&item->actions_mutex);
  1130. if (actions_pending) {
  1131. uint64_t duration = util_mul_div64(AUDIO_OUTPUT_FRAMES,
  1132. 1000000000ULL, sample_rate);
  1133. if (!ts || action.timestamp < (ts + duration)) {
  1134. apply_scene_item_audio_actions(item, buf, ts,
  1135. sample_rate);
  1136. return true;
  1137. }
  1138. }
  1139. return false;
  1140. }
  1141. static void process_all_audio_actions(struct obs_scene_item *item,
  1142. size_t sample_rate)
  1143. {
  1144. while (apply_scene_item_volume(item, NULL, 0, sample_rate))
  1145. ;
  1146. }
  1147. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  1148. size_t pos, size_t count)
  1149. {
  1150. register float *out = p_out + pos;
  1151. register float *buf = buf_in;
  1152. register float *in = p_in;
  1153. register float *end = in + count;
  1154. while (in < end)
  1155. *(out++) += *(in++) * *(buf++);
  1156. }
  1157. static inline void mix_audio(float *p_out, float *p_in, size_t pos,
  1158. size_t count)
  1159. {
  1160. register float *out = p_out + pos;
  1161. register float *in = p_in;
  1162. register float *end = in + count;
  1163. while (in < end)
  1164. *(out++) += *(in++);
  1165. }
  1166. static bool scene_audio_render(void *data, uint64_t *ts_out,
  1167. struct obs_source_audio_mix *audio_output,
  1168. uint32_t mixers, size_t channels,
  1169. size_t sample_rate)
  1170. {
  1171. uint64_t timestamp = 0;
  1172. float buf[AUDIO_OUTPUT_FRAMES];
  1173. struct obs_source_audio_mix child_audio;
  1174. struct obs_scene *scene = data;
  1175. struct obs_scene_item *item;
  1176. audio_lock(scene);
  1177. item = scene->first_item;
  1178. while (item) {
  1179. struct obs_source *source;
  1180. if (item->visible && transition_active(item->show_transition))
  1181. source = item->show_transition;
  1182. else if (!item->visible &&
  1183. transition_active(item->hide_transition))
  1184. source = item->hide_transition;
  1185. else
  1186. source = item->source;
  1187. if (!obs_source_audio_pending(source) &&
  1188. (item->visible ||
  1189. transition_active(item->hide_transition))) {
  1190. uint64_t source_ts =
  1191. obs_source_get_audio_timestamp(source);
  1192. if (source_ts && (!timestamp || source_ts < timestamp))
  1193. timestamp = source_ts;
  1194. }
  1195. item = item->next;
  1196. }
  1197. if (!timestamp) {
  1198. /* just process all pending audio actions if no audio playing,
  1199. * otherwise audio actions will just never be processed */
  1200. item = scene->first_item;
  1201. while (item) {
  1202. process_all_audio_actions(item, sample_rate);
  1203. item = item->next;
  1204. }
  1205. audio_unlock(scene);
  1206. return false;
  1207. }
  1208. item = scene->first_item;
  1209. while (item) {
  1210. uint64_t source_ts;
  1211. size_t pos, count;
  1212. bool apply_buf;
  1213. struct obs_source *source;
  1214. if (item->visible && transition_active(item->show_transition))
  1215. source = item->show_transition;
  1216. else if (!item->visible &&
  1217. transition_active(item->hide_transition))
  1218. source = item->hide_transition;
  1219. else
  1220. source = item->source;
  1221. apply_buf = apply_scene_item_volume(item, buf, timestamp,
  1222. sample_rate);
  1223. if (obs_source_audio_pending(source)) {
  1224. item = item->next;
  1225. continue;
  1226. }
  1227. source_ts = obs_source_get_audio_timestamp(source);
  1228. if (!source_ts) {
  1229. item = item->next;
  1230. continue;
  1231. }
  1232. pos = (size_t)ns_to_audio_frames(sample_rate,
  1233. source_ts - timestamp);
  1234. if (pos >= AUDIO_OUTPUT_FRAMES) {
  1235. item = item->next;
  1236. continue;
  1237. }
  1238. count = AUDIO_OUTPUT_FRAMES - pos;
  1239. if (!apply_buf && !item->visible &&
  1240. !transition_active(item->hide_transition)) {
  1241. item = item->next;
  1242. continue;
  1243. }
  1244. obs_source_get_audio_mix(source, &child_audio);
  1245. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  1246. if ((mixers & (1 << mix)) == 0)
  1247. continue;
  1248. for (size_t ch = 0; ch < channels; ch++) {
  1249. float *out = audio_output->output[mix].data[ch];
  1250. float *in = child_audio.output[mix].data[ch];
  1251. if (apply_buf)
  1252. mix_audio_with_buf(out, in, buf, pos,
  1253. count);
  1254. else
  1255. mix_audio(out, in, pos, count);
  1256. }
  1257. }
  1258. item = item->next;
  1259. }
  1260. *ts_out = timestamp;
  1261. audio_unlock(scene);
  1262. return true;
  1263. }
  1264. enum gs_color_space
  1265. scene_video_get_color_space(void *data, size_t count,
  1266. const enum gs_color_space *preferred_spaces)
  1267. {
  1268. UNUSED_PARAMETER(data);
  1269. UNUSED_PARAMETER(count);
  1270. UNUSED_PARAMETER(preferred_spaces);
  1271. enum gs_color_space space = GS_CS_SRGB;
  1272. struct obs_video_info ovi;
  1273. if (obs_get_video_info(&ovi)) {
  1274. if (ovi.colorspace == VIDEO_CS_2100_PQ ||
  1275. ovi.colorspace == VIDEO_CS_2100_HLG)
  1276. space = GS_CS_709_EXTENDED;
  1277. }
  1278. return space;
  1279. }
  1280. const struct obs_source_info scene_info = {
  1281. .id = "scene",
  1282. .type = OBS_SOURCE_TYPE_SCENE,
  1283. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1284. OBS_SOURCE_COMPOSITE | OBS_SOURCE_DO_NOT_DUPLICATE |
  1285. OBS_SOURCE_SRGB,
  1286. .get_name = scene_getname,
  1287. .create = scene_create,
  1288. .destroy = scene_destroy,
  1289. .video_tick = scene_video_tick,
  1290. .video_render = scene_video_render,
  1291. .audio_render = scene_audio_render,
  1292. .get_width = scene_getwidth,
  1293. .get_height = scene_getheight,
  1294. .load = scene_load,
  1295. .save = scene_save,
  1296. .enum_active_sources = scene_enum_active_sources,
  1297. .enum_all_sources = scene_enum_all_sources,
  1298. .video_get_color_space = scene_video_get_color_space,
  1299. };
  1300. const struct obs_source_info group_info = {
  1301. .id = "group",
  1302. .type = OBS_SOURCE_TYPE_SCENE,
  1303. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1304. OBS_SOURCE_COMPOSITE | OBS_SOURCE_SRGB,
  1305. .get_name = group_getname,
  1306. .create = scene_create,
  1307. .destroy = scene_destroy,
  1308. .video_tick = scene_video_tick,
  1309. .video_render = scene_video_render,
  1310. .audio_render = scene_audio_render,
  1311. .get_width = scene_getwidth,
  1312. .get_height = scene_getheight,
  1313. .load = scene_load,
  1314. .save = scene_save,
  1315. .enum_active_sources = scene_enum_active_sources,
  1316. .enum_all_sources = scene_enum_all_sources,
  1317. .video_get_color_space = scene_video_get_color_space,
  1318. };
  1319. static inline obs_scene_t *create_id(const char *id, const char *name)
  1320. {
  1321. struct obs_source *source = obs_source_create(id, name, NULL, NULL);
  1322. return source->context.data;
  1323. }
  1324. static inline obs_scene_t *create_private_id(const char *id, const char *name)
  1325. {
  1326. struct obs_source *source = obs_source_create_private(id, name, NULL);
  1327. return source->context.data;
  1328. }
  1329. obs_scene_t *obs_scene_create(const char *name)
  1330. {
  1331. return create_id("scene", name);
  1332. }
  1333. obs_scene_t *obs_scene_create_private(const char *name)
  1334. {
  1335. return create_private_id("scene", name);
  1336. }
  1337. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  1338. {
  1339. struct obs_scene_item *item = scene->first_item;
  1340. while (item && idx--)
  1341. item = item->next;
  1342. return item ? item->source : NULL;
  1343. }
  1344. static inline obs_source_t *dup_child(obs_scene_item_ptr_array_t *old_items,
  1345. size_t idx, obs_scene_t *new_scene,
  1346. bool private)
  1347. {
  1348. obs_source_t *source;
  1349. source = old_items->array[idx]->source;
  1350. /* if the old item is referenced more than once in the old scene,
  1351. * make sure they're referenced similarly in the new scene to reduce
  1352. * load times */
  1353. for (size_t i = 0; i < idx; i++) {
  1354. struct obs_scene_item *item = old_items->array[i];
  1355. if (item->source == source) {
  1356. source = get_child_at_idx(new_scene, i);
  1357. return obs_source_get_ref(source);
  1358. }
  1359. }
  1360. return obs_source_duplicate(
  1361. source, private ? obs_source_get_name(source) : NULL, private);
  1362. }
  1363. static inline obs_source_t *new_ref(obs_source_t *source)
  1364. {
  1365. return obs_source_get_ref(source);
  1366. }
  1367. static inline void duplicate_item_data(struct obs_scene_item *dst,
  1368. struct obs_scene_item *src,
  1369. bool defer_texture_update,
  1370. bool duplicate_hotkeys)
  1371. {
  1372. struct obs_scene *dst_scene = dst->parent;
  1373. if (!src->user_visible)
  1374. set_visibility(dst, false);
  1375. dst->selected = src->selected;
  1376. dst->pos = src->pos;
  1377. dst->rot = src->rot;
  1378. dst->scale = src->scale;
  1379. dst->align = src->align;
  1380. dst->last_width = src->last_width;
  1381. dst->last_height = src->last_height;
  1382. dst->output_scale = src->output_scale;
  1383. dst->scale_filter = src->scale_filter;
  1384. dst->blend_method = src->blend_method;
  1385. dst->blend_type = src->blend_type;
  1386. dst->box_transform = src->box_transform;
  1387. dst->box_scale = src->box_scale;
  1388. dst->draw_transform = src->draw_transform;
  1389. dst->bounds_type = src->bounds_type;
  1390. dst->bounds_align = src->bounds_align;
  1391. dst->bounds = src->bounds;
  1392. if (src->show_transition) {
  1393. obs_source_t *transition = obs_source_duplicate(
  1394. src->show_transition,
  1395. obs_source_get_name(src->show_transition), true);
  1396. obs_sceneitem_set_transition(dst, true, transition);
  1397. obs_source_release(transition);
  1398. }
  1399. if (src->hide_transition) {
  1400. obs_source_t *transition = obs_source_duplicate(
  1401. src->hide_transition,
  1402. obs_source_get_name(src->hide_transition), true);
  1403. obs_sceneitem_set_transition(dst, false, transition);
  1404. obs_source_release(transition);
  1405. }
  1406. dst->show_transition_duration = src->show_transition_duration;
  1407. dst->hide_transition_duration = src->hide_transition_duration;
  1408. if (duplicate_hotkeys && !dst_scene->source->context.private) {
  1409. struct dstr show = {0};
  1410. struct dstr hide = {0};
  1411. obs_data_array_t *data0 = NULL;
  1412. obs_data_array_t *data1 = NULL;
  1413. obs_hotkey_pair_save(src->toggle_visibility, &data0, &data1);
  1414. obs_hotkey_pair_load(dst->toggle_visibility, data0, data1);
  1415. /* Fix scene item ID */
  1416. dstr_printf(&show, "libobs.show_scene_item.%" PRIi64, dst->id);
  1417. dstr_printf(&hide, "libobs.hide_scene_item.%" PRIi64, dst->id);
  1418. obs_hotkey_pair_set_names(dst->toggle_visibility, show.array,
  1419. hide.array);
  1420. obs_data_array_release(data0);
  1421. obs_data_array_release(data1);
  1422. dstr_free(&show);
  1423. dstr_free(&hide);
  1424. }
  1425. obs_sceneitem_set_crop(dst, &src->crop);
  1426. obs_sceneitem_set_locked(dst, src->locked);
  1427. if (defer_texture_update) {
  1428. os_atomic_set_bool(&dst->update_transform, true);
  1429. }
  1430. obs_data_apply(dst->private_settings, src->private_settings);
  1431. }
  1432. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  1433. enum obs_scene_duplicate_type type)
  1434. {
  1435. bool make_unique = ((int)type & (1 << 0)) != 0;
  1436. bool make_private = ((int)type & (1 << 1)) != 0;
  1437. obs_scene_item_ptr_array_t items;
  1438. struct obs_scene *new_scene;
  1439. struct obs_scene_item *item;
  1440. struct obs_source *source;
  1441. da_init(items);
  1442. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  1443. return NULL;
  1444. /* --------------------------------- */
  1445. full_lock(scene);
  1446. item = scene->first_item;
  1447. while (item) {
  1448. da_push_back(items, &item);
  1449. obs_sceneitem_addref(item);
  1450. item = item->next;
  1451. }
  1452. full_unlock(scene);
  1453. /* --------------------------------- */
  1454. new_scene = make_private
  1455. ? create_private_id(scene->source->info.id, name)
  1456. : create_id(scene->source->info.id, name);
  1457. obs_source_copy_filters(new_scene->source, scene->source);
  1458. obs_data_apply(new_scene->source->private_settings,
  1459. scene->source->private_settings);
  1460. /* never duplicate sub-items for groups */
  1461. if (scene->is_group)
  1462. make_unique = false;
  1463. for (size_t i = 0; i < items.num; i++) {
  1464. item = items.array[i];
  1465. source = make_unique
  1466. ? dup_child(&items, i, new_scene, make_private)
  1467. : new_ref(item->source);
  1468. if (source) {
  1469. struct obs_scene_item *new_item =
  1470. obs_scene_add(new_scene, source);
  1471. if (!new_item) {
  1472. obs_source_release(source);
  1473. continue;
  1474. }
  1475. duplicate_item_data(new_item, item, false, false);
  1476. obs_source_release(source);
  1477. }
  1478. }
  1479. for (size_t i = 0; i < items.num; i++)
  1480. obs_sceneitem_release(items.array[i]);
  1481. if (new_scene->is_group)
  1482. resize_scene(new_scene);
  1483. da_free(items);
  1484. return new_scene;
  1485. }
  1486. void obs_scene_addref(obs_scene_t *scene)
  1487. {
  1488. if (scene)
  1489. obs_source_addref(scene->source);
  1490. }
  1491. obs_scene_t *obs_scene_get_ref(obs_scene_t *scene)
  1492. {
  1493. if (!scene)
  1494. return NULL;
  1495. if (obs_source_get_ref(scene->source) != NULL)
  1496. return scene;
  1497. return NULL;
  1498. }
  1499. void obs_scene_release(obs_scene_t *scene)
  1500. {
  1501. if (scene)
  1502. obs_source_release(scene->source);
  1503. }
  1504. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  1505. {
  1506. return scene ? scene->source : NULL;
  1507. }
  1508. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  1509. {
  1510. if (!source || strcmp(source->info.id, scene_info.id) != 0)
  1511. return NULL;
  1512. return source->context.data;
  1513. }
  1514. obs_scene_t *obs_group_from_source(const obs_source_t *source)
  1515. {
  1516. if (!source || strcmp(source->info.id, group_info.id) != 0)
  1517. return NULL;
  1518. return source->context.data;
  1519. }
  1520. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  1521. {
  1522. struct obs_scene_item *item;
  1523. if (!scene)
  1524. return NULL;
  1525. full_lock(scene);
  1526. item = scene->first_item;
  1527. while (item) {
  1528. if (strcmp(item->source->context.name, name) == 0)
  1529. break;
  1530. item = item->next;
  1531. }
  1532. full_unlock(scene);
  1533. return item;
  1534. }
  1535. obs_sceneitem_t *obs_scene_find_source_recursive(obs_scene_t *scene,
  1536. const char *name)
  1537. {
  1538. struct obs_scene_item *item;
  1539. if (!scene)
  1540. return NULL;
  1541. full_lock(scene);
  1542. item = scene->first_item;
  1543. while (item) {
  1544. if (strcmp(item->source->context.name, name) == 0)
  1545. break;
  1546. if (item->is_group) {
  1547. obs_scene_t *group = item->source->context.data;
  1548. obs_sceneitem_t *child =
  1549. obs_scene_find_source(group, name);
  1550. if (child) {
  1551. item = child;
  1552. break;
  1553. }
  1554. }
  1555. item = item->next;
  1556. }
  1557. full_unlock(scene);
  1558. return item;
  1559. }
  1560. struct sceneitem_check {
  1561. obs_source_t *source_in;
  1562. obs_sceneitem_t *item_out;
  1563. };
  1564. bool check_sceneitem_exists(obs_scene_t *scene, obs_sceneitem_t *item,
  1565. void *vp_check)
  1566. {
  1567. UNUSED_PARAMETER(scene);
  1568. struct sceneitem_check *check = (struct sceneitem_check *)vp_check;
  1569. if (obs_sceneitem_get_source(item) == check->source_in) {
  1570. check->item_out = item;
  1571. obs_sceneitem_addref(item);
  1572. return false;
  1573. }
  1574. return true;
  1575. }
  1576. obs_sceneitem_t *obs_scene_sceneitem_from_source(obs_scene_t *scene,
  1577. obs_source_t *source)
  1578. {
  1579. struct sceneitem_check check = {source, NULL};
  1580. obs_scene_enum_items(scene, check_sceneitem_exists, (void *)&check);
  1581. return check.item_out;
  1582. }
  1583. obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
  1584. {
  1585. struct obs_scene_item *item;
  1586. if (!scene)
  1587. return NULL;
  1588. full_lock(scene);
  1589. item = scene->first_item;
  1590. while (item) {
  1591. if (item->id == id)
  1592. break;
  1593. item = item->next;
  1594. }
  1595. full_unlock(scene);
  1596. return item;
  1597. }
  1598. void obs_scene_enum_items(obs_scene_t *scene,
  1599. bool (*callback)(obs_scene_t *, obs_sceneitem_t *,
  1600. void *),
  1601. void *param)
  1602. {
  1603. struct obs_scene_item *item;
  1604. if (!scene || !callback)
  1605. return;
  1606. full_lock(scene);
  1607. item = scene->first_item;
  1608. while (item) {
  1609. struct obs_scene_item *next = item->next;
  1610. obs_sceneitem_addref(item);
  1611. if (!callback(scene, item, param)) {
  1612. obs_sceneitem_release(item);
  1613. break;
  1614. }
  1615. obs_sceneitem_release(item);
  1616. item = next;
  1617. }
  1618. full_unlock(scene);
  1619. }
  1620. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  1621. {
  1622. long owners = os_atomic_load_long(&si->ref);
  1623. while (owners > 0) {
  1624. if (os_atomic_compare_exchange_long(&si->ref, &owners,
  1625. owners + 1)) {
  1626. return si;
  1627. }
  1628. }
  1629. return NULL;
  1630. }
  1631. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  1632. obs_hotkey_t *hotkey, bool pressed)
  1633. {
  1634. UNUSED_PARAMETER(id);
  1635. UNUSED_PARAMETER(hotkey);
  1636. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1637. if (pressed && si && !si->user_visible) {
  1638. obs_sceneitem_set_visible(si, true);
  1639. obs_sceneitem_release(si);
  1640. return true;
  1641. }
  1642. obs_sceneitem_release(si);
  1643. return false;
  1644. }
  1645. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  1646. obs_hotkey_t *hotkey, bool pressed)
  1647. {
  1648. UNUSED_PARAMETER(id);
  1649. UNUSED_PARAMETER(hotkey);
  1650. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1651. if (pressed && si && si->user_visible) {
  1652. obs_sceneitem_set_visible(si, false);
  1653. obs_sceneitem_release(si);
  1654. return true;
  1655. }
  1656. obs_sceneitem_release(si);
  1657. return false;
  1658. }
  1659. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  1660. const char *name)
  1661. {
  1662. struct obs_data_array *hotkey_array;
  1663. obs_data_t *hotkey_data = scene->source->context.hotkey_data;
  1664. struct dstr show = {0};
  1665. struct dstr hide = {0};
  1666. struct dstr legacy = {0};
  1667. struct dstr show_desc = {0};
  1668. struct dstr hide_desc = {0};
  1669. dstr_printf(&show, "libobs.show_scene_item.%" PRIi64, item->id);
  1670. dstr_printf(&hide, "libobs.hide_scene_item.%" PRIi64, item->id);
  1671. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1672. dstr_replace(&show_desc, "%1", name);
  1673. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1674. dstr_replace(&hide_desc, "%1", name);
  1675. /* Check if legacy keys exists, migrate if necessary */
  1676. dstr_printf(&legacy, "libobs.show_scene_item.%s", name);
  1677. hotkey_array = obs_data_get_array(hotkey_data, legacy.array);
  1678. if (hotkey_array)
  1679. obs_data_set_array(hotkey_data, show.array, hotkey_array);
  1680. dstr_printf(&legacy, "libobs.hide_scene_item.%s", name);
  1681. hotkey_array = obs_data_get_array(hotkey_data, legacy.array);
  1682. if (hotkey_array)
  1683. obs_data_set_array(hotkey_data, hide.array, hotkey_array);
  1684. item->toggle_visibility = obs_hotkey_pair_register_source(
  1685. scene->source, show.array, show_desc.array, hide.array,
  1686. hide_desc.array, hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1687. item, item);
  1688. dstr_free(&show);
  1689. dstr_free(&hide);
  1690. dstr_free(&legacy);
  1691. dstr_free(&show_desc);
  1692. dstr_free(&hide_desc);
  1693. }
  1694. static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
  1695. const char *new_name)
  1696. {
  1697. struct dstr show_desc = {0};
  1698. struct dstr hide_desc = {0};
  1699. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1700. dstr_replace(&show_desc, "%1", new_name);
  1701. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1702. dstr_replace(&hide_desc, "%1", new_name);
  1703. obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
  1704. show_desc.array, hide_desc.array);
  1705. dstr_free(&show_desc);
  1706. dstr_free(&hide_desc);
  1707. }
  1708. static void sceneitem_renamed(void *param, calldata_t *data)
  1709. {
  1710. obs_sceneitem_t *scene_item = param;
  1711. const char *name = calldata_string(data, "new_name");
  1712. sceneitem_rename_hotkey(scene_item, name);
  1713. }
  1714. static inline bool source_has_audio(obs_source_t *source)
  1715. {
  1716. return (source->info.output_flags &
  1717. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1718. }
  1719. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  1720. obs_source_t *source,
  1721. obs_sceneitem_t *insert_after,
  1722. int64_t id)
  1723. {
  1724. struct obs_scene_item *last;
  1725. struct obs_scene_item *item;
  1726. pthread_mutex_t mutex;
  1727. struct item_action action = {.visible = true,
  1728. .timestamp = os_gettime_ns()};
  1729. if (!scene)
  1730. return NULL;
  1731. source = obs_source_get_ref(source);
  1732. if (!source) {
  1733. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1734. return NULL;
  1735. }
  1736. if (source->removed) {
  1737. blog(LOG_WARNING, "Tried to add a removed source to a scene");
  1738. goto release_source_and_fail;
  1739. }
  1740. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1741. blog(LOG_WARNING, "Failed to create scene item mutex");
  1742. goto release_source_and_fail;
  1743. }
  1744. if (!obs_source_add_active_child(scene->source, source)) {
  1745. blog(LOG_WARNING, "Failed to add source to scene due to "
  1746. "infinite source recursion");
  1747. pthread_mutex_destroy(&mutex);
  1748. goto release_source_and_fail;
  1749. }
  1750. item = bzalloc(sizeof(struct obs_scene_item));
  1751. item->source = source;
  1752. item->id = id ? id : ++scene->id_counter;
  1753. item->parent = scene;
  1754. item->ref = 1;
  1755. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1756. item->actions_mutex = mutex;
  1757. item->user_visible = true;
  1758. item->locked = false;
  1759. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  1760. item->private_settings = obs_data_create();
  1761. item->toggle_visibility = OBS_INVALID_HOTKEY_PAIR_ID;
  1762. os_atomic_set_long(&item->active_refs, 1);
  1763. vec2_set(&item->scale, 1.0f, 1.0f);
  1764. matrix4_identity(&item->draw_transform);
  1765. matrix4_identity(&item->box_transform);
  1766. if (source_has_audio(source)) {
  1767. item->visible = false;
  1768. da_push_back(item->audio_actions, &action);
  1769. } else {
  1770. item->visible = true;
  1771. }
  1772. full_lock(scene);
  1773. if (insert_after) {
  1774. obs_sceneitem_t *next = insert_after->next;
  1775. if (next)
  1776. next->prev = item;
  1777. item->next = insert_after->next;
  1778. item->prev = insert_after;
  1779. insert_after->next = item;
  1780. } else {
  1781. last = scene->first_item;
  1782. if (!last) {
  1783. scene->first_item = item;
  1784. } else {
  1785. while (last->next)
  1786. last = last->next;
  1787. last->next = item;
  1788. item->prev = last;
  1789. }
  1790. }
  1791. full_unlock(scene);
  1792. if (!scene->source->context.private)
  1793. init_hotkeys(scene, item, obs_source_get_name(source));
  1794. signal_handler_connect(obs_source_get_signal_handler(source), "rename",
  1795. sceneitem_renamed, item);
  1796. return item;
  1797. release_source_and_fail:
  1798. obs_source_release(source);
  1799. return NULL;
  1800. }
  1801. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1802. {
  1803. obs_sceneitem_t *item = obs_scene_add_internal(scene, source, NULL, 0);
  1804. struct calldata params;
  1805. uint8_t stack[128];
  1806. if (!item)
  1807. return NULL;
  1808. calldata_init_fixed(&params, stack, sizeof(stack));
  1809. calldata_set_ptr(&params, "scene", scene);
  1810. calldata_set_ptr(&params, "item", item);
  1811. signal_handler_signal(scene->source->context.signals, "item_add",
  1812. &params);
  1813. return item;
  1814. }
  1815. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  1816. {
  1817. if (item) {
  1818. if (item->item_render) {
  1819. obs_enter_graphics();
  1820. gs_texrender_destroy(item->item_render);
  1821. obs_leave_graphics();
  1822. }
  1823. obs_data_release(item->private_settings);
  1824. obs_hotkey_pair_unregister(item->toggle_visibility);
  1825. pthread_mutex_destroy(&item->actions_mutex);
  1826. signal_handler_disconnect(
  1827. obs_source_get_signal_handler(item->source), "rename",
  1828. sceneitem_renamed, item);
  1829. if (item->show_transition)
  1830. obs_source_release(item->show_transition);
  1831. if (item->hide_transition)
  1832. obs_source_release(item->hide_transition);
  1833. if (item->source)
  1834. obs_source_release(item->source);
  1835. da_free(item->audio_actions);
  1836. bfree(item);
  1837. }
  1838. }
  1839. void obs_sceneitem_addref(obs_sceneitem_t *item)
  1840. {
  1841. if (item)
  1842. os_atomic_inc_long(&item->ref);
  1843. }
  1844. void obs_sceneitem_release(obs_sceneitem_t *item)
  1845. {
  1846. if (!item)
  1847. return;
  1848. if (os_atomic_dec_long(&item->ref) == 0)
  1849. obs_sceneitem_destroy(item);
  1850. }
  1851. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1852. {
  1853. obs_scene_t *scene;
  1854. if (!item || item->removed)
  1855. return;
  1856. scene = item->parent;
  1857. full_lock(scene);
  1858. item->removed = true;
  1859. assert(scene != NULL);
  1860. assert(scene->source != NULL);
  1861. set_visibility(item, false);
  1862. signal_item_remove(item);
  1863. detach_sceneitem(item);
  1864. full_unlock(scene);
  1865. obs_sceneitem_set_transition(item, true, NULL);
  1866. obs_sceneitem_set_transition(item, false, NULL);
  1867. obs_sceneitem_release(item);
  1868. }
  1869. void obs_sceneitem_save(obs_sceneitem_t *item, obs_data_array_t *arr)
  1870. {
  1871. scene_save_item(arr, item, NULL);
  1872. }
  1873. void sceneitem_restore(obs_data_t *data, void *vp)
  1874. {
  1875. obs_scene_t *scene = (obs_scene_t *)vp;
  1876. scene_load_item(scene, data);
  1877. }
  1878. void obs_sceneitems_add(obs_scene_t *scene, obs_data_array_t *data)
  1879. {
  1880. obs_data_array_enum(data, sceneitem_restore, scene);
  1881. }
  1882. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1883. {
  1884. return item ? item->parent : NULL;
  1885. }
  1886. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1887. {
  1888. return item ? item->source : NULL;
  1889. }
  1890. static void signal_parent(obs_scene_t *parent, const char *command,
  1891. calldata_t *params)
  1892. {
  1893. calldata_set_ptr(params, "scene", parent);
  1894. signal_handler_signal(parent->source->context.signals, command, params);
  1895. }
  1896. struct passthrough {
  1897. obs_data_array_t *ids;
  1898. obs_data_array_t *scenes_and_groups;
  1899. bool all_items;
  1900. };
  1901. bool save_transform_states(obs_scene_t *scene, obs_sceneitem_t *item,
  1902. void *vp_pass)
  1903. {
  1904. struct passthrough *pass = (struct passthrough *)vp_pass;
  1905. if (obs_sceneitem_selected(item) || pass->all_items) {
  1906. obs_data_t *temp = obs_data_create();
  1907. obs_data_array_t *item_ids = (obs_data_array_t *)pass->ids;
  1908. struct obs_transform_info info;
  1909. struct obs_sceneitem_crop crop;
  1910. obs_sceneitem_get_info(item, &info);
  1911. obs_sceneitem_get_crop(item, &crop);
  1912. struct vec2 pos = info.pos;
  1913. struct vec2 scale = info.scale;
  1914. float rot = info.rot;
  1915. uint32_t alignment = info.alignment;
  1916. uint32_t bounds_type = info.bounds_type;
  1917. uint32_t bounds_alignment = info.bounds_alignment;
  1918. struct vec2 bounds = info.bounds;
  1919. obs_data_set_int(temp, "id", obs_sceneitem_get_id(item));
  1920. obs_data_set_vec2(temp, "pos", &pos);
  1921. obs_data_set_vec2(temp, "scale", &scale);
  1922. obs_data_set_double(temp, "rot", rot);
  1923. obs_data_set_int(temp, "alignment", alignment);
  1924. obs_data_set_int(temp, "bounds_type", bounds_type);
  1925. obs_data_set_vec2(temp, "bounds", &bounds);
  1926. obs_data_set_int(temp, "bounds_alignment", bounds_alignment);
  1927. obs_data_set_int(temp, "top", crop.top);
  1928. obs_data_set_int(temp, "bottom", crop.bottom);
  1929. obs_data_set_int(temp, "left", crop.left);
  1930. obs_data_set_int(temp, "right", crop.right);
  1931. obs_data_array_push_back(item_ids, temp);
  1932. obs_data_release(temp);
  1933. }
  1934. obs_source_t *item_source = obs_sceneitem_get_source(item);
  1935. if (obs_source_is_group(item_source)) {
  1936. obs_data_t *temp = obs_data_create();
  1937. obs_data_array_t *nids = obs_data_array_create();
  1938. obs_data_set_string(temp, "scene_name",
  1939. obs_source_get_name(item_source));
  1940. obs_data_set_bool(temp, "is_group", true);
  1941. obs_data_set_string(
  1942. temp, "group_parent",
  1943. obs_source_get_name(obs_scene_get_source(scene)));
  1944. struct passthrough npass = {nids, pass->scenes_and_groups,
  1945. pass->all_items};
  1946. obs_sceneitem_group_enum_items(item, save_transform_states,
  1947. (void *)&npass);
  1948. obs_data_set_array(temp, "items", nids);
  1949. obs_data_array_push_back(pass->scenes_and_groups, temp);
  1950. obs_data_release(temp);
  1951. obs_data_array_release(nids);
  1952. }
  1953. return true;
  1954. }
  1955. obs_data_t *obs_scene_save_transform_states(obs_scene_t *scene, bool all_items)
  1956. {
  1957. obs_data_t *wrapper = obs_data_create();
  1958. obs_data_array_t *scenes_and_groups = obs_data_array_create();
  1959. obs_data_array_t *item_ids = obs_data_array_create();
  1960. struct passthrough pass = {item_ids, scenes_and_groups, all_items};
  1961. obs_data_t *temp = obs_data_create();
  1962. obs_data_set_string(temp, "scene_name",
  1963. obs_source_get_name(obs_scene_get_source(scene)));
  1964. obs_data_set_string(temp, "scene_uuid",
  1965. obs_source_get_uuid(obs_scene_get_source(scene)));
  1966. obs_data_set_bool(temp, "is_group", false);
  1967. obs_scene_enum_items(scene, save_transform_states, (void *)&pass);
  1968. obs_data_set_array(temp, "items", item_ids);
  1969. obs_data_array_push_back(scenes_and_groups, temp);
  1970. obs_data_set_array(wrapper, "scenes_and_groups", scenes_and_groups);
  1971. obs_data_array_release(item_ids);
  1972. obs_data_array_release(scenes_and_groups);
  1973. obs_data_release(temp);
  1974. return wrapper;
  1975. }
  1976. void load_transform_states(obs_data_t *temp, void *vp_scene)
  1977. {
  1978. obs_scene_t *scene = (obs_scene_t *)vp_scene;
  1979. int64_t id = obs_data_get_int(temp, "id");
  1980. obs_sceneitem_t *item = obs_scene_find_sceneitem_by_id(scene, id);
  1981. struct obs_transform_info info;
  1982. struct obs_sceneitem_crop crop;
  1983. obs_data_get_vec2(temp, "pos", &info.pos);
  1984. obs_data_get_vec2(temp, "scale", &info.scale);
  1985. info.rot = (float)obs_data_get_double(temp, "rot");
  1986. info.alignment = (uint32_t)obs_data_get_int(temp, "alignment");
  1987. info.bounds_type =
  1988. (enum obs_bounds_type)obs_data_get_int(temp, "bounds_type");
  1989. info.bounds_alignment =
  1990. (uint32_t)obs_data_get_int(temp, "bounds_alignment");
  1991. obs_data_get_vec2(temp, "bounds", &info.bounds);
  1992. crop.top = (int)obs_data_get_int(temp, "top");
  1993. crop.bottom = (int)obs_data_get_int(temp, "bottom");
  1994. crop.left = (int)obs_data_get_int(temp, "left");
  1995. crop.right = (int)obs_data_get_int(temp, "right");
  1996. obs_sceneitem_defer_update_begin(item);
  1997. obs_sceneitem_set_info(item, &info);
  1998. obs_sceneitem_set_crop(item, &crop);
  1999. obs_sceneitem_defer_update_end(item);
  2000. }
  2001. void iterate_scenes_and_groups_transform_states(obs_data_t *data, void *vp)
  2002. {
  2003. obs_data_array_t *items = obs_data_get_array(data, "items");
  2004. obs_source_t *scene_source =
  2005. obs_get_source_by_name(obs_data_get_string(data, "scene_name"));
  2006. obs_scene_t *scene = obs_scene_from_source(scene_source);
  2007. if (obs_data_get_bool(data, "is_group")) {
  2008. obs_source_t *parent_source = obs_get_source_by_name(
  2009. obs_data_get_string(data, "group_parent"));
  2010. obs_scene_t *parent = obs_scene_from_source(parent_source);
  2011. obs_sceneitem_t *group = obs_scene_get_group(
  2012. parent, obs_data_get_string(data, "scene_name"));
  2013. scene = obs_sceneitem_group_get_scene(group);
  2014. obs_source_release(parent_source);
  2015. }
  2016. obs_data_array_enum(items, load_transform_states, (void *)scene);
  2017. UNUSED_PARAMETER(vp);
  2018. obs_data_array_release(items);
  2019. obs_source_release(scene_source);
  2020. }
  2021. void obs_scene_load_transform_states(const char *data)
  2022. {
  2023. obs_data_t *dat = obs_data_create_from_json(data);
  2024. obs_data_array_t *scenes_and_groups =
  2025. obs_data_get_array(dat, "scenes_and_groups");
  2026. obs_data_array_enum(scenes_and_groups,
  2027. iterate_scenes_and_groups_transform_states, NULL);
  2028. obs_data_release(dat);
  2029. obs_data_array_release(scenes_and_groups);
  2030. }
  2031. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  2032. {
  2033. struct calldata params;
  2034. uint8_t stack[128];
  2035. const char *command = select ? "item_select" : "item_deselect";
  2036. if (!item || item->selected == select || !item->parent)
  2037. return;
  2038. item->selected = select;
  2039. calldata_init_fixed(&params, stack, sizeof(stack));
  2040. calldata_set_ptr(&params, "item", item);
  2041. signal_parent(item->parent, command, &params);
  2042. }
  2043. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  2044. {
  2045. return item ? item->selected : false;
  2046. }
  2047. #define do_update_transform(item) \
  2048. do { \
  2049. if (!item->parent || item->parent->is_group) \
  2050. os_atomic_set_bool(&item->update_transform, true); \
  2051. else \
  2052. update_item_transform(item, false); \
  2053. } while (false)
  2054. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  2055. {
  2056. if (item) {
  2057. vec2_copy(&item->pos, pos);
  2058. do_update_transform(item);
  2059. }
  2060. }
  2061. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  2062. {
  2063. if (item) {
  2064. item->rot = rot;
  2065. do_update_transform(item);
  2066. }
  2067. }
  2068. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  2069. {
  2070. if (item) {
  2071. vec2_copy(&item->scale, scale);
  2072. do_update_transform(item);
  2073. }
  2074. }
  2075. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  2076. {
  2077. if (item) {
  2078. item->align = alignment;
  2079. do_update_transform(item);
  2080. }
  2081. }
  2082. static inline void signal_reorder(struct obs_scene_item *item)
  2083. {
  2084. const char *command = NULL;
  2085. struct calldata params;
  2086. uint8_t stack[128];
  2087. command = "reorder";
  2088. calldata_init_fixed(&params, stack, sizeof(stack));
  2089. signal_parent(item->parent, command, &params);
  2090. }
  2091. static inline void signal_refresh(obs_scene_t *scene)
  2092. {
  2093. const char *command = NULL;
  2094. struct calldata params;
  2095. uint8_t stack[128];
  2096. command = "refresh";
  2097. calldata_init_fixed(&params, stack, sizeof(stack));
  2098. signal_parent(scene, command, &params);
  2099. }
  2100. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  2101. enum obs_order_movement movement)
  2102. {
  2103. if (!item)
  2104. return;
  2105. struct obs_scene_item *next, *prev;
  2106. struct obs_scene *scene = obs_scene_get_ref(item->parent);
  2107. if (!scene)
  2108. return;
  2109. full_lock(scene);
  2110. next = item->next;
  2111. prev = item->prev;
  2112. detach_sceneitem(item);
  2113. if (movement == OBS_ORDER_MOVE_DOWN) {
  2114. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  2115. } else if (movement == OBS_ORDER_MOVE_UP) {
  2116. attach_sceneitem(scene, item, next ? next : prev);
  2117. } else if (movement == OBS_ORDER_MOVE_TOP) {
  2118. struct obs_scene_item *last = next;
  2119. if (!last) {
  2120. last = prev;
  2121. } else {
  2122. while (last->next)
  2123. last = last->next;
  2124. }
  2125. attach_sceneitem(scene, item, last);
  2126. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  2127. attach_sceneitem(scene, item, NULL);
  2128. }
  2129. full_unlock(scene);
  2130. signal_reorder(item);
  2131. obs_scene_release(scene);
  2132. }
  2133. int obs_sceneitem_get_order_position(obs_sceneitem_t *item)
  2134. {
  2135. struct obs_scene *scene = item->parent;
  2136. struct obs_scene_item *next = scene->first_item;
  2137. full_lock(scene);
  2138. int index = 0;
  2139. while (next && next != item) {
  2140. next = next->next;
  2141. ++index;
  2142. }
  2143. full_unlock(scene);
  2144. return index;
  2145. }
  2146. void obs_sceneitem_set_order_position(obs_sceneitem_t *item, int position)
  2147. {
  2148. if (!item)
  2149. return;
  2150. struct obs_scene *scene = obs_scene_get_ref(item->parent);
  2151. struct obs_scene_item *next;
  2152. if (!scene)
  2153. return;
  2154. full_lock(scene);
  2155. detach_sceneitem(item);
  2156. next = scene->first_item;
  2157. if (position == 0) {
  2158. attach_sceneitem(scene, item, NULL);
  2159. } else {
  2160. for (int i = position; i > 1; --i) {
  2161. if (next->next == NULL)
  2162. break;
  2163. next = next->next;
  2164. }
  2165. attach_sceneitem(scene, item, next);
  2166. }
  2167. full_unlock(scene);
  2168. signal_reorder(item);
  2169. obs_scene_release(scene);
  2170. }
  2171. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  2172. enum obs_bounds_type type)
  2173. {
  2174. if (item) {
  2175. item->bounds_type = type;
  2176. do_update_transform(item);
  2177. }
  2178. }
  2179. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  2180. uint32_t alignment)
  2181. {
  2182. if (item) {
  2183. item->bounds_align = alignment;
  2184. do_update_transform(item);
  2185. }
  2186. }
  2187. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  2188. {
  2189. if (item) {
  2190. item->bounds = *bounds;
  2191. do_update_transform(item);
  2192. }
  2193. }
  2194. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  2195. {
  2196. if (item)
  2197. vec2_copy(pos, &item->pos);
  2198. }
  2199. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  2200. {
  2201. return item ? item->rot : 0.0f;
  2202. }
  2203. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  2204. {
  2205. if (item)
  2206. vec2_copy(scale, &item->scale);
  2207. }
  2208. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  2209. {
  2210. return item ? item->align : 0;
  2211. }
  2212. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  2213. {
  2214. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  2215. }
  2216. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  2217. {
  2218. return item ? item->bounds_align : 0;
  2219. }
  2220. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  2221. {
  2222. if (item)
  2223. *bounds = item->bounds;
  2224. }
  2225. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  2226. struct obs_transform_info *info)
  2227. {
  2228. if (item && info) {
  2229. info->pos = item->pos;
  2230. info->rot = item->rot;
  2231. info->scale = item->scale;
  2232. info->alignment = item->align;
  2233. info->bounds_type = item->bounds_type;
  2234. info->bounds_alignment = item->bounds_align;
  2235. info->bounds = item->bounds;
  2236. }
  2237. }
  2238. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  2239. const struct obs_transform_info *info)
  2240. {
  2241. if (item && info) {
  2242. item->pos = info->pos;
  2243. item->rot = info->rot;
  2244. if (isfinite(info->scale.x) && isfinite(info->scale.y)) {
  2245. item->scale = info->scale;
  2246. }
  2247. item->align = info->alignment;
  2248. item->bounds_type = info->bounds_type;
  2249. item->bounds_align = info->bounds_alignment;
  2250. item->bounds = info->bounds;
  2251. do_update_transform(item);
  2252. }
  2253. }
  2254. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  2255. struct matrix4 *transform)
  2256. {
  2257. if (item)
  2258. matrix4_copy(transform, &item->draw_transform);
  2259. }
  2260. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  2261. struct matrix4 *transform)
  2262. {
  2263. if (item)
  2264. matrix4_copy(transform, &item->box_transform);
  2265. }
  2266. void obs_sceneitem_get_box_scale(const obs_sceneitem_t *item,
  2267. struct vec2 *scale)
  2268. {
  2269. if (item)
  2270. *scale = item->box_scale;
  2271. }
  2272. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  2273. {
  2274. return item ? item->user_visible : false;
  2275. }
  2276. static bool group_item_transition(obs_scene_t *scene, obs_sceneitem_t *item,
  2277. void *param)
  2278. {
  2279. if (!param || !item)
  2280. return true;
  2281. const bool visible = *(bool *)param;
  2282. if (obs_sceneitem_visible(item))
  2283. obs_sceneitem_do_transition(item, visible);
  2284. UNUSED_PARAMETER(scene);
  2285. return true;
  2286. }
  2287. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  2288. {
  2289. struct calldata cd;
  2290. uint8_t stack[256];
  2291. struct item_action action = {.visible = visible,
  2292. .timestamp = os_gettime_ns()};
  2293. if (!item)
  2294. return false;
  2295. if (item->user_visible == visible)
  2296. return false;
  2297. if (!item->parent)
  2298. return false;
  2299. obs_sceneitem_do_transition(item, visible);
  2300. if (obs_sceneitem_is_group(item))
  2301. obs_sceneitem_group_enum_items(item, group_item_transition,
  2302. &visible);
  2303. item->user_visible = visible;
  2304. if (visible) {
  2305. if (os_atomic_inc_long(&item->active_refs) == 1) {
  2306. if (!obs_source_add_active_child(item->parent->source,
  2307. item->source)) {
  2308. os_atomic_dec_long(&item->active_refs);
  2309. return false;
  2310. }
  2311. }
  2312. }
  2313. calldata_init_fixed(&cd, stack, sizeof(stack));
  2314. calldata_set_ptr(&cd, "item", item);
  2315. calldata_set_bool(&cd, "visible", visible);
  2316. signal_parent(item->parent, "item_visible", &cd);
  2317. if (source_has_audio(item->source)) {
  2318. pthread_mutex_lock(&item->actions_mutex);
  2319. da_push_back(item->audio_actions, &action);
  2320. pthread_mutex_unlock(&item->actions_mutex);
  2321. } else {
  2322. set_visibility(item, visible);
  2323. }
  2324. return true;
  2325. }
  2326. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  2327. {
  2328. return item ? item->locked : false;
  2329. }
  2330. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  2331. {
  2332. struct calldata cd;
  2333. uint8_t stack[256];
  2334. if (!item)
  2335. return false;
  2336. if (item->locked == lock)
  2337. return false;
  2338. if (!item->parent)
  2339. return false;
  2340. item->locked = lock;
  2341. calldata_init_fixed(&cd, stack, sizeof(stack));
  2342. calldata_set_ptr(&cd, "item", item);
  2343. calldata_set_bool(&cd, "locked", lock);
  2344. signal_parent(item->parent, "item_locked", &cd);
  2345. return true;
  2346. }
  2347. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t *const *items,
  2348. size_t size, bool *order_matches)
  2349. {
  2350. obs_sceneitem_t *item = scene->first_item;
  2351. size_t count = 0;
  2352. while (item) {
  2353. bool found = false;
  2354. for (size_t i = 0; i < size; i++) {
  2355. if (items[i] != item)
  2356. continue;
  2357. if (count != i)
  2358. *order_matches = false;
  2359. found = true;
  2360. break;
  2361. }
  2362. if (!found)
  2363. return false;
  2364. item = item->next;
  2365. count += 1;
  2366. }
  2367. return count == size;
  2368. }
  2369. bool obs_scene_reorder_items(obs_scene_t *scene,
  2370. obs_sceneitem_t *const *item_order,
  2371. size_t item_order_size)
  2372. {
  2373. if (!scene || !item_order_size)
  2374. return false;
  2375. scene = obs_scene_get_ref(scene);
  2376. if (!scene)
  2377. return false;
  2378. full_lock(scene);
  2379. bool order_matches = true;
  2380. if (!sceneitems_match(scene, item_order, item_order_size,
  2381. &order_matches) ||
  2382. order_matches) {
  2383. full_unlock(scene);
  2384. obs_scene_release(scene);
  2385. return false;
  2386. }
  2387. scene->first_item = item_order[0];
  2388. obs_sceneitem_t *prev = NULL;
  2389. for (size_t i = 0; i < item_order_size; i++) {
  2390. item_order[i]->prev = prev;
  2391. item_order[i]->next = NULL;
  2392. if (prev)
  2393. prev->next = item_order[i];
  2394. prev = item_order[i];
  2395. }
  2396. full_unlock(scene);
  2397. signal_reorder(scene->first_item);
  2398. obs_scene_release(scene);
  2399. return true;
  2400. }
  2401. void obs_scene_atomic_update(obs_scene_t *scene,
  2402. obs_scene_atomic_update_func func, void *data)
  2403. {
  2404. if (!scene)
  2405. return;
  2406. scene = obs_scene_get_ref(scene);
  2407. if (!scene)
  2408. return;
  2409. full_lock(scene);
  2410. func(data, scene);
  2411. full_unlock(scene);
  2412. obs_scene_release(scene);
  2413. }
  2414. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  2415. const struct obs_sceneitem_crop *crop2)
  2416. {
  2417. return crop1->left == crop2->left && crop1->right == crop2->right &&
  2418. crop1->top == crop2->top && crop1->bottom == crop2->bottom;
  2419. }
  2420. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  2421. const struct obs_sceneitem_crop *crop)
  2422. {
  2423. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  2424. return;
  2425. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  2426. return;
  2427. if (crop_equal(crop, &item->crop))
  2428. return;
  2429. memcpy(&item->crop, crop, sizeof(*crop));
  2430. if (item->crop.left < 0)
  2431. item->crop.left = 0;
  2432. if (item->crop.right < 0)
  2433. item->crop.right = 0;
  2434. if (item->crop.top < 0)
  2435. item->crop.top = 0;
  2436. if (item->crop.bottom < 0)
  2437. item->crop.bottom = 0;
  2438. os_atomic_set_bool(&item->update_transform, true);
  2439. }
  2440. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  2441. struct obs_sceneitem_crop *crop)
  2442. {
  2443. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  2444. return;
  2445. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  2446. return;
  2447. memcpy(crop, &item->crop, sizeof(*crop));
  2448. }
  2449. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  2450. enum obs_scale_type filter)
  2451. {
  2452. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  2453. return;
  2454. item->scale_filter = filter;
  2455. os_atomic_set_bool(&item->update_transform, true);
  2456. }
  2457. enum obs_scale_type obs_sceneitem_get_scale_filter(obs_sceneitem_t *item)
  2458. {
  2459. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter")
  2460. ? item->scale_filter
  2461. : OBS_SCALE_DISABLE;
  2462. }
  2463. void obs_sceneitem_set_blending_method(obs_sceneitem_t *item,
  2464. enum obs_blending_method method)
  2465. {
  2466. if (!obs_ptr_valid(item, "obs_sceneitem_set_blending_method"))
  2467. return;
  2468. item->blend_method = method;
  2469. }
  2470. enum obs_blending_method
  2471. obs_sceneitem_get_blending_method(obs_sceneitem_t *item)
  2472. {
  2473. return obs_ptr_valid(item, "obs_sceneitem_get_blending_method")
  2474. ? item->blend_method
  2475. : OBS_BLEND_METHOD_DEFAULT;
  2476. }
  2477. void obs_sceneitem_set_blending_mode(obs_sceneitem_t *item,
  2478. enum obs_blending_type type)
  2479. {
  2480. if (!obs_ptr_valid(item, "obs_sceneitem_set_blending_mode"))
  2481. return;
  2482. item->blend_type = type;
  2483. os_atomic_set_bool(&item->update_transform, true);
  2484. }
  2485. enum obs_blending_type obs_sceneitem_get_blending_mode(obs_sceneitem_t *item)
  2486. {
  2487. return obs_ptr_valid(item, "obs_sceneitem_get_blending_mode")
  2488. ? item->blend_type
  2489. : OBS_BLEND_NORMAL;
  2490. }
  2491. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  2492. {
  2493. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  2494. return;
  2495. os_atomic_inc_long(&item->defer_update);
  2496. }
  2497. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  2498. {
  2499. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  2500. return;
  2501. if (os_atomic_dec_long(&item->defer_update) == 0)
  2502. do_update_transform(item);
  2503. }
  2504. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  2505. {
  2506. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  2507. return;
  2508. os_atomic_inc_long(&item->defer_group_resize);
  2509. }
  2510. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  2511. {
  2512. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  2513. return;
  2514. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  2515. os_atomic_set_bool(&item->update_group_resize, true);
  2516. }
  2517. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  2518. {
  2519. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  2520. return 0;
  2521. return item->id;
  2522. }
  2523. void obs_sceneitem_set_id(obs_sceneitem_t *item, int64_t id)
  2524. {
  2525. item->id = id;
  2526. }
  2527. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  2528. {
  2529. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  2530. return NULL;
  2531. obs_data_addref(item->private_settings);
  2532. return item->private_settings;
  2533. }
  2534. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  2535. {
  2536. struct vec3 v;
  2537. vec3_set(&v, v2->x, v2->y, 0.0f);
  2538. vec3_transform(&v, &v, transform);
  2539. v2->x = v.x;
  2540. v2->y = v.y;
  2541. }
  2542. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  2543. struct vec2 *scale, float *rot)
  2544. {
  2545. struct matrix4 transform;
  2546. struct matrix4 mat;
  2547. struct vec4 x_base;
  2548. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2549. matrix4_copy(&transform, &group->draw_transform);
  2550. transform_val(pos, &transform);
  2551. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2552. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  2553. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  2554. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2555. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2556. matrix4_mul(&mat, &mat, &transform);
  2557. scale->x = vec4_len(&mat.x) * (scale->x > 0.0f ? 1.0f : -1.0f);
  2558. scale->y = vec4_len(&mat.y) * (scale->y > 0.0f ? 1.0f : -1.0f);
  2559. *rot += group->rot;
  2560. }
  2561. static void remove_group_transform(obs_sceneitem_t *group,
  2562. obs_sceneitem_t *item)
  2563. {
  2564. obs_scene_t *parent = item->parent;
  2565. if (!parent || !group)
  2566. return;
  2567. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  2568. update_item_transform(item, false);
  2569. }
  2570. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  2571. {
  2572. struct matrix4 transform;
  2573. struct matrix4 mat;
  2574. struct vec4 x_base;
  2575. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2576. matrix4_inv(&transform, &group->draw_transform);
  2577. transform_val(&item->pos, &transform);
  2578. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2579. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  2580. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  2581. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2582. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2583. matrix4_mul(&mat, &mat, &transform);
  2584. item->scale.x =
  2585. vec4_len(&mat.x) * (item->scale.x > 0.0f ? 1.0f : -1.0f);
  2586. item->scale.y =
  2587. vec4_len(&mat.y) * (item->scale.y > 0.0f ? 1.0f : -1.0f);
  2588. item->rot -= group->rot;
  2589. update_item_transform(item, false);
  2590. }
  2591. static bool resize_scene_base(obs_scene_t *scene, struct vec2 *minv,
  2592. struct vec2 *maxv, struct vec2 *scale)
  2593. {
  2594. vec2_set(minv, M_INFINITE, M_INFINITE);
  2595. vec2_set(maxv, -M_INFINITE, -M_INFINITE);
  2596. obs_sceneitem_t *item = scene->first_item;
  2597. if (!item) {
  2598. scene->cx = 0;
  2599. scene->cy = 0;
  2600. return false;
  2601. }
  2602. while (item) {
  2603. #define get_min_max(x_val, y_val) \
  2604. do { \
  2605. struct vec3 v; \
  2606. vec3_set(&v, x_val, y_val, 0.0f); \
  2607. vec3_transform(&v, &v, &item->box_transform); \
  2608. if (v.x < minv->x) \
  2609. minv->x = v.x; \
  2610. if (v.y < minv->y) \
  2611. minv->y = v.y; \
  2612. if (v.x > maxv->x) \
  2613. maxv->x = v.x; \
  2614. if (v.y > maxv->y) \
  2615. maxv->y = v.y; \
  2616. } while (false)
  2617. get_min_max(0.0f, 0.0f);
  2618. get_min_max(1.0f, 0.0f);
  2619. get_min_max(0.0f, 1.0f);
  2620. get_min_max(1.0f, 1.0f);
  2621. #undef get_min_max
  2622. item = item->next;
  2623. }
  2624. item = scene->first_item;
  2625. while (item) {
  2626. vec2_sub(&item->pos, &item->pos, minv);
  2627. update_item_transform(item, false);
  2628. item = item->next;
  2629. }
  2630. vec2_sub(scale, maxv, minv);
  2631. scene->cx = (uint32_t)ceilf(scale->x);
  2632. scene->cy = (uint32_t)ceilf(scale->y);
  2633. return true;
  2634. }
  2635. static void resize_scene(obs_scene_t *scene)
  2636. {
  2637. struct vec2 minv;
  2638. struct vec2 maxv;
  2639. struct vec2 scale;
  2640. resize_scene_base(scene, &minv, &maxv, &scale);
  2641. }
  2642. /* assumes group scene and parent scene is locked */
  2643. static void resize_group(obs_sceneitem_t *group)
  2644. {
  2645. obs_scene_t *scene = group->source->context.data;
  2646. struct vec2 minv;
  2647. struct vec2 maxv;
  2648. struct vec2 scale;
  2649. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  2650. return;
  2651. if (!resize_scene_base(scene, &minv, &maxv, &scale))
  2652. return;
  2653. if (group->bounds_type == OBS_BOUNDS_NONE) {
  2654. struct vec2 new_pos;
  2655. if ((group->align & OBS_ALIGN_LEFT) != 0)
  2656. new_pos.x = minv.x;
  2657. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  2658. new_pos.x = maxv.x;
  2659. else
  2660. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  2661. if ((group->align & OBS_ALIGN_TOP) != 0)
  2662. new_pos.y = minv.y;
  2663. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  2664. new_pos.y = maxv.y;
  2665. else
  2666. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  2667. transform_val(&new_pos, &group->draw_transform);
  2668. vec2_copy(&group->pos, &new_pos);
  2669. }
  2670. os_atomic_set_bool(&group->update_group_resize, false);
  2671. update_item_transform(group, false);
  2672. }
  2673. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  2674. {
  2675. return obs_scene_insert_group(scene, name, NULL, 0);
  2676. }
  2677. obs_sceneitem_t *obs_scene_add_group2(obs_scene_t *scene, const char *name,
  2678. bool signal)
  2679. {
  2680. return obs_scene_insert_group2(scene, name, NULL, 0, signal);
  2681. }
  2682. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene, const char *name,
  2683. obs_sceneitem_t **items, size_t count)
  2684. {
  2685. if (!scene)
  2686. return NULL;
  2687. /* don't allow groups or sub-items of other groups */
  2688. for (size_t i = count; i > 0; i--) {
  2689. obs_sceneitem_t *item = items[i - 1];
  2690. if (item->parent != scene || item->is_group)
  2691. return NULL;
  2692. }
  2693. obs_scene_t *sub_scene = create_id("group", name);
  2694. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  2695. obs_sceneitem_t *item =
  2696. obs_scene_add_internal(scene, sub_scene->source, last_item, 0);
  2697. if (!items || !count) {
  2698. obs_scene_release(sub_scene);
  2699. return item;
  2700. }
  2701. /* ------------------------- */
  2702. full_lock(scene);
  2703. full_lock(sub_scene);
  2704. sub_scene->first_item = items[0];
  2705. for (size_t i = count; i > 0; i--) {
  2706. size_t idx = i - 1;
  2707. remove_group_transform(item, items[idx]);
  2708. detach_sceneitem(items[idx]);
  2709. }
  2710. for (size_t i = 0; i < count; i++) {
  2711. size_t idx = i;
  2712. if (idx != (count - 1)) {
  2713. size_t next_idx = idx + 1;
  2714. items[idx]->next = items[next_idx];
  2715. items[next_idx]->prev = items[idx];
  2716. } else {
  2717. items[idx]->next = NULL;
  2718. }
  2719. items[idx]->parent = sub_scene;
  2720. apply_group_transform(items[idx], item);
  2721. }
  2722. items[0]->prev = NULL;
  2723. resize_group(item);
  2724. full_unlock(sub_scene);
  2725. full_unlock(scene);
  2726. struct calldata params;
  2727. uint8_t stack[128];
  2728. calldata_init_fixed(&params, stack, sizeof(stack));
  2729. calldata_set_ptr(&params, "scene", scene);
  2730. calldata_set_ptr(&params, "item", item);
  2731. signal_handler_signal(scene->source->context.signals, "item_add",
  2732. &params);
  2733. /* ------------------------- */
  2734. obs_scene_release(sub_scene);
  2735. return item;
  2736. }
  2737. obs_sceneitem_t *obs_scene_insert_group2(obs_scene_t *scene, const char *name,
  2738. obs_sceneitem_t **items, size_t count,
  2739. bool signal)
  2740. {
  2741. obs_sceneitem_t *item =
  2742. obs_scene_insert_group(scene, name, items, count);
  2743. if (signal && item)
  2744. signal_refresh(scene);
  2745. return item;
  2746. }
  2747. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  2748. {
  2749. if (!scene || !name || !*name) {
  2750. return NULL;
  2751. }
  2752. obs_sceneitem_t *group = NULL;
  2753. obs_sceneitem_t *item;
  2754. full_lock(scene);
  2755. item = scene->first_item;
  2756. while (item) {
  2757. if (item->is_group && item->source->context.name) {
  2758. if (strcmp(item->source->context.name, name) == 0) {
  2759. group = item;
  2760. break;
  2761. }
  2762. }
  2763. item = item->next;
  2764. }
  2765. full_unlock(scene);
  2766. return group;
  2767. }
  2768. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  2769. {
  2770. return item && item->is_group;
  2771. }
  2772. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  2773. {
  2774. return (item && item->is_group) ? item->source->context.data : NULL;
  2775. }
  2776. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  2777. {
  2778. if (!item || !item->is_group)
  2779. return;
  2780. obs_scene_t *scene = item->parent;
  2781. obs_scene_t *subscene = item->source->context.data;
  2782. obs_sceneitem_t *insert_after = item;
  2783. obs_sceneitem_t *first;
  2784. obs_sceneitem_t *last;
  2785. signal_item_remove(item);
  2786. full_lock(scene);
  2787. /* ------------------------- */
  2788. full_lock(subscene);
  2789. first = subscene->first_item;
  2790. last = first;
  2791. while (last) {
  2792. obs_sceneitem_t *dst;
  2793. remove_group_transform(item, last);
  2794. dst = obs_scene_add_internal(scene, last->source, insert_after,
  2795. 0);
  2796. duplicate_item_data(dst, last, true, true);
  2797. apply_group_transform(last, item);
  2798. if (!last->next)
  2799. break;
  2800. insert_after = dst;
  2801. last = last->next;
  2802. }
  2803. full_unlock(subscene);
  2804. /* ------------------------- */
  2805. detach_sceneitem(item);
  2806. full_unlock(scene);
  2807. obs_sceneitem_release(item);
  2808. }
  2809. void obs_sceneitem_group_ungroup2(obs_sceneitem_t *item, bool signal)
  2810. {
  2811. obs_scene_t *scene = item->parent;
  2812. obs_sceneitem_group_ungroup(item);
  2813. if (signal)
  2814. signal_refresh(scene);
  2815. }
  2816. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2817. {
  2818. if (!group || !group->is_group || !item)
  2819. return;
  2820. obs_scene_t *scene = group->parent;
  2821. obs_scene_t *groupscene = group->source->context.data;
  2822. if (item->parent != scene)
  2823. return;
  2824. if (item->parent == groupscene)
  2825. return;
  2826. /* ------------------------- */
  2827. full_lock(scene);
  2828. full_lock(groupscene);
  2829. remove_group_transform(group, item);
  2830. detach_sceneitem(item);
  2831. attach_sceneitem(groupscene, item, NULL);
  2832. apply_group_transform(item, group);
  2833. resize_group(group);
  2834. full_unlock(groupscene);
  2835. full_unlock(scene);
  2836. /* ------------------------- */
  2837. signal_refresh(scene);
  2838. }
  2839. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2840. obs_sceneitem_t *item)
  2841. {
  2842. if (!item || !group || !group->is_group)
  2843. return;
  2844. obs_scene_t *groupscene = item->parent;
  2845. obs_scene_t *scene = group->parent;
  2846. /* ------------------------- */
  2847. full_lock(scene);
  2848. full_lock(groupscene);
  2849. remove_group_transform(group, item);
  2850. detach_sceneitem(item);
  2851. attach_sceneitem(scene, item, NULL);
  2852. resize_group(group);
  2853. full_unlock(groupscene);
  2854. full_unlock(scene);
  2855. /* ------------------------- */
  2856. signal_refresh(scene);
  2857. }
  2858. static void
  2859. build_current_order_info(obs_scene_t *scene,
  2860. struct obs_sceneitem_order_info **items_out,
  2861. size_t *size_out)
  2862. {
  2863. DARRAY(struct obs_sceneitem_order_info) items;
  2864. da_init(items);
  2865. obs_sceneitem_t *item = scene->first_item;
  2866. while (item) {
  2867. struct obs_sceneitem_order_info info = {0};
  2868. info.item = item;
  2869. da_push_back(items, &info);
  2870. if (item->is_group) {
  2871. obs_scene_t *sub_scene = item->source->context.data;
  2872. full_lock(sub_scene);
  2873. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2874. while (sub_item) {
  2875. info.group = item;
  2876. info.item = sub_item;
  2877. da_push_back(items, &info);
  2878. sub_item = sub_item->next;
  2879. }
  2880. full_unlock(sub_scene);
  2881. }
  2882. item = item->next;
  2883. }
  2884. *items_out = items.array;
  2885. *size_out = items.num;
  2886. }
  2887. static bool sceneitems_match2(obs_scene_t *scene,
  2888. struct obs_sceneitem_order_info *items,
  2889. size_t size)
  2890. {
  2891. struct obs_sceneitem_order_info *cur_items;
  2892. size_t cur_size;
  2893. build_current_order_info(scene, &cur_items, &cur_size);
  2894. if (cur_size != size) {
  2895. bfree(cur_items);
  2896. return false;
  2897. }
  2898. for (size_t i = 0; i < size; i++) {
  2899. struct obs_sceneitem_order_info *new = &items[i];
  2900. struct obs_sceneitem_order_info *old = &cur_items[i];
  2901. if (new->group != old->group || new->item != old->item) {
  2902. bfree(cur_items);
  2903. return false;
  2904. }
  2905. }
  2906. bfree(cur_items);
  2907. return true;
  2908. }
  2909. static obs_sceneitem_t *
  2910. get_sceneitem_parent_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem)
  2911. {
  2912. if (group_subitem->is_group)
  2913. return NULL;
  2914. obs_sceneitem_t *item = scene->first_item;
  2915. while (item) {
  2916. if (item->is_group &&
  2917. item->source->context.data == group_subitem->parent)
  2918. return item;
  2919. item = item->next;
  2920. }
  2921. return NULL;
  2922. }
  2923. static void obs_sceneitem_move_hotkeys(obs_scene_t *parent,
  2924. obs_sceneitem_t *item)
  2925. {
  2926. obs_data_array_t *data0 = NULL;
  2927. obs_data_array_t *data1 = NULL;
  2928. obs_hotkey_pair_save(item->toggle_visibility, &data0, &data1);
  2929. obs_hotkey_pair_unregister(item->toggle_visibility);
  2930. init_hotkeys(parent, item, obs_source_get_name(item->source));
  2931. obs_hotkey_pair_load(item->toggle_visibility, data0, data1);
  2932. obs_data_array_release(data0);
  2933. obs_data_array_release(data1);
  2934. }
  2935. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2936. struct obs_sceneitem_order_info *item_order,
  2937. size_t item_order_size)
  2938. {
  2939. if (!scene || !item_order_size || !item_order)
  2940. return false;
  2941. scene = obs_scene_get_ref(scene);
  2942. if (!scene)
  2943. return false;
  2944. full_lock(scene);
  2945. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2946. full_unlock(scene);
  2947. obs_scene_release(scene);
  2948. return false;
  2949. }
  2950. for (size_t i = 0; i < item_order_size; i++) {
  2951. struct obs_sceneitem_order_info *info = &item_order[i];
  2952. if (!info->item->is_group) {
  2953. obs_sceneitem_t *group =
  2954. get_sceneitem_parent_group(scene, info->item);
  2955. remove_group_transform(group, info->item);
  2956. }
  2957. }
  2958. scene->first_item = item_order[0].item;
  2959. obs_sceneitem_t *prev = NULL;
  2960. for (size_t i = 0; i < item_order_size; i++) {
  2961. struct obs_sceneitem_order_info *info = &item_order[i];
  2962. obs_sceneitem_t *item = info->item;
  2963. if (info->item->is_group) {
  2964. obs_sceneitem_t *sub_prev = NULL;
  2965. obs_scene_t *sub_scene =
  2966. info->item->source->context.data;
  2967. sub_scene->first_item = NULL;
  2968. obs_scene_addref(sub_scene);
  2969. full_lock(sub_scene);
  2970. for (i++; i < item_order_size; i++) {
  2971. struct obs_sceneitem_order_info *sub_info =
  2972. &item_order[i];
  2973. obs_sceneitem_t *sub_item = sub_info->item;
  2974. if (sub_info->group != info->item) {
  2975. i--;
  2976. break;
  2977. }
  2978. if (!sub_scene->first_item)
  2979. sub_scene->first_item = sub_item;
  2980. /* Move hotkeys into group */
  2981. obs_sceneitem_move_hotkeys(sub_scene, sub_item);
  2982. sub_item->prev = sub_prev;
  2983. sub_item->next = NULL;
  2984. sub_item->parent = sub_scene;
  2985. if (sub_prev)
  2986. sub_prev->next = sub_item;
  2987. apply_group_transform(sub_info->item,
  2988. sub_info->group);
  2989. sub_prev = sub_item;
  2990. }
  2991. resize_group(info->item);
  2992. full_unlock(sub_scene);
  2993. obs_scene_release(sub_scene);
  2994. }
  2995. /* Move item hotkeys out of group */
  2996. if (item->parent && obs_scene_is_group(item->parent))
  2997. obs_sceneitem_move_hotkeys(scene, item);
  2998. item->prev = prev;
  2999. item->next = NULL;
  3000. item->parent = scene;
  3001. if (prev)
  3002. prev->next = item;
  3003. prev = item;
  3004. }
  3005. full_unlock(scene);
  3006. signal_reorder(scene->first_item);
  3007. obs_scene_release(scene);
  3008. return true;
  3009. }
  3010. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  3011. obs_sceneitem_t *group_subitem)
  3012. {
  3013. if (!scene || !group_subitem || group_subitem->is_group)
  3014. return NULL;
  3015. full_lock(scene);
  3016. obs_sceneitem_t *group =
  3017. get_sceneitem_parent_group(scene, group_subitem);
  3018. full_unlock(scene);
  3019. return group;
  3020. }
  3021. bool obs_source_is_group(const obs_source_t *source)
  3022. {
  3023. return source && strcmp(source->info.id, group_info.id) == 0;
  3024. }
  3025. bool obs_source_is_scene(const obs_source_t *source)
  3026. {
  3027. return source && strcmp(source->info.id, scene_info.id) == 0;
  3028. }
  3029. bool obs_scene_is_group(const obs_scene_t *scene)
  3030. {
  3031. return scene ? scene->is_group : false;
  3032. }
  3033. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  3034. bool (*callback)(obs_scene_t *,
  3035. obs_sceneitem_t *, void *),
  3036. void *param)
  3037. {
  3038. if (!group || !group->is_group)
  3039. return;
  3040. obs_scene_t *scene = group->source->context.data;
  3041. if (scene)
  3042. obs_scene_enum_items(scene, callback, param);
  3043. }
  3044. void obs_sceneitem_force_update_transform(obs_sceneitem_t *item)
  3045. {
  3046. if (!item)
  3047. return;
  3048. if (os_atomic_set_bool(&item->update_transform, false))
  3049. update_item_transform(item, false);
  3050. }
  3051. void obs_sceneitem_set_show_transition(obs_sceneitem_t *item,
  3052. obs_source_t *transition)
  3053. {
  3054. if (!item)
  3055. return;
  3056. if (item->show_transition)
  3057. obs_source_release(item->show_transition);
  3058. item->show_transition = obs_source_get_ref(transition);
  3059. }
  3060. void obs_sceneitem_set_show_transition_duration(obs_sceneitem_t *item,
  3061. uint32_t duration_ms)
  3062. {
  3063. if (!item)
  3064. return;
  3065. item->show_transition_duration = duration_ms;
  3066. }
  3067. obs_source_t *obs_sceneitem_get_show_transition(obs_sceneitem_t *item)
  3068. {
  3069. if (!item)
  3070. return NULL;
  3071. return item->show_transition;
  3072. }
  3073. uint32_t obs_sceneitem_get_show_transition_duration(obs_sceneitem_t *item)
  3074. {
  3075. if (!item)
  3076. return 0;
  3077. return item->show_transition_duration;
  3078. }
  3079. void obs_sceneitem_set_hide_transition(obs_sceneitem_t *item,
  3080. obs_source_t *transition)
  3081. {
  3082. if (!item)
  3083. return;
  3084. if (item->hide_transition)
  3085. obs_source_release(item->hide_transition);
  3086. item->hide_transition = obs_source_get_ref(transition);
  3087. }
  3088. void obs_sceneitem_set_hide_transition_duration(obs_sceneitem_t *item,
  3089. uint32_t duration_ms)
  3090. {
  3091. if (!item)
  3092. return;
  3093. item->hide_transition_duration = duration_ms;
  3094. }
  3095. obs_source_t *obs_sceneitem_get_hide_transition(obs_sceneitem_t *item)
  3096. {
  3097. if (!item)
  3098. return NULL;
  3099. return item->hide_transition;
  3100. }
  3101. uint32_t obs_sceneitem_get_hide_transition_duration(obs_sceneitem_t *item)
  3102. {
  3103. if (!item)
  3104. return 0;
  3105. return item->hide_transition_duration;
  3106. }
  3107. void obs_sceneitem_set_transition(obs_sceneitem_t *item, bool show,
  3108. obs_source_t *transition)
  3109. {
  3110. if (!item)
  3111. return;
  3112. obs_source_t **target = show ? &item->show_transition
  3113. : &item->hide_transition;
  3114. if (*target)
  3115. obs_source_release(*target);
  3116. *target = obs_source_get_ref(transition);
  3117. }
  3118. obs_source_t *obs_sceneitem_get_transition(obs_sceneitem_t *item, bool show)
  3119. {
  3120. if (!item)
  3121. return NULL;
  3122. return show ? item->show_transition : item->hide_transition;
  3123. }
  3124. void obs_sceneitem_set_transition_duration(obs_sceneitem_t *item, bool show,
  3125. uint32_t duration_ms)
  3126. {
  3127. if (!item)
  3128. return;
  3129. if (show)
  3130. item->show_transition_duration = duration_ms;
  3131. else
  3132. item->hide_transition_duration = duration_ms;
  3133. }
  3134. uint32_t obs_sceneitem_get_transition_duration(obs_sceneitem_t *item, bool show)
  3135. {
  3136. if (!item)
  3137. return 0;
  3138. return show ? item->show_transition_duration
  3139. : item->hide_transition_duration;
  3140. }
  3141. void obs_sceneitem_transition_stop(void *data, calldata_t *calldata)
  3142. {
  3143. obs_source_t *parent = data;
  3144. obs_source_t *transition;
  3145. calldata_get_ptr(calldata, "source", &transition);
  3146. obs_source_remove_active_child(parent, transition);
  3147. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  3148. if (sh)
  3149. signal_handler_disconnect(sh, "transition_stop",
  3150. obs_sceneitem_transition_stop,
  3151. parent);
  3152. }
  3153. void obs_sceneitem_do_transition(obs_sceneitem_t *item, bool visible)
  3154. {
  3155. if (!item)
  3156. return;
  3157. if (transition_active(item->show_transition))
  3158. obs_transition_force_stop(item->show_transition);
  3159. if (transition_active(item->hide_transition))
  3160. obs_transition_force_stop(item->hide_transition);
  3161. obs_source_t *transition = obs_sceneitem_get_transition(item, visible);
  3162. if (!transition)
  3163. return;
  3164. int duration =
  3165. (int)obs_sceneitem_get_transition_duration(item, visible);
  3166. const int cx = obs_source_get_width(item->source);
  3167. const int cy = obs_source_get_height(item->source);
  3168. obs_transition_set_size(transition, cx, cy);
  3169. obs_transition_set_alignment(transition, OBS_ALIGN_CENTER);
  3170. obs_transition_set_scale_type(transition, OBS_TRANSITION_SCALE_ASPECT);
  3171. if (duration == 0)
  3172. duration = 300;
  3173. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  3174. obs_source_t *parent = obs_scene_get_source(scene);
  3175. obs_source_add_active_child(parent, transition);
  3176. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  3177. if (sh)
  3178. signal_handler_connect(sh, "transition_stop",
  3179. obs_sceneitem_transition_stop, parent);
  3180. if (!visible) {
  3181. obs_transition_set(transition, item->source);
  3182. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  3183. duration, NULL);
  3184. } else {
  3185. obs_transition_set(transition, NULL);
  3186. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  3187. duration, item->source);
  3188. }
  3189. }
  3190. void obs_sceneitem_transition_load(struct obs_scene_item *item,
  3191. obs_data_t *data, bool show)
  3192. {
  3193. if (!item || !data)
  3194. return;
  3195. const char *id = obs_data_get_string(data, "id");
  3196. if (id && strlen(id)) {
  3197. const char *tn = obs_data_get_string(data, "name");
  3198. obs_data_t *s = obs_data_get_obj(data, "transition");
  3199. obs_source_t *t = obs_source_create_private(id, tn, s);
  3200. obs_sceneitem_set_transition(item, show, t);
  3201. obs_source_release(t);
  3202. obs_data_release(s);
  3203. } else {
  3204. obs_sceneitem_set_transition(item, show, NULL);
  3205. }
  3206. obs_sceneitem_set_transition_duration(
  3207. item, show, (uint32_t)obs_data_get_int(data, "duration"));
  3208. }
  3209. obs_data_t *obs_sceneitem_transition_save(struct obs_scene_item *item,
  3210. bool show)
  3211. {
  3212. obs_data_t *data = obs_data_create();
  3213. struct obs_source *transition = show ? item->show_transition
  3214. : item->hide_transition;
  3215. if (transition) {
  3216. obs_data_set_string(data, "id",
  3217. obs_source_get_unversioned_id(transition));
  3218. obs_data_set_string(data, "versioned_id",
  3219. obs_source_get_id(transition));
  3220. obs_data_set_string(data, "name",
  3221. obs_source_get_name(transition));
  3222. obs_data_t *s = obs_source_get_settings(transition);
  3223. obs_data_set_obj(data, "transition", s);
  3224. obs_data_release(s);
  3225. }
  3226. obs_data_set_int(data, "duration",
  3227. show ? item->show_transition_duration
  3228. : item->hide_transition_duration);
  3229. return data;
  3230. }
  3231. void obs_scene_prune_sources(obs_scene_t *scene)
  3232. {
  3233. obs_scene_item_ptr_array_t remove_items;
  3234. da_init(remove_items);
  3235. video_lock(scene);
  3236. update_transforms_and_prune_sources(scene, &remove_items, NULL);
  3237. video_unlock(scene);
  3238. for (size_t i = 0; i < remove_items.num; i++)
  3239. obs_sceneitem_release(remove_items.array[i]);
  3240. da_free(remove_items);
  3241. }