1
0

obs-scene.c 97 KB

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