obs-scene.c 111 KB

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