obs-scene.c 114 KB

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