obs-scene.c 117 KB

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