SDL_render.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // The SDL 2D rendering system
  20. #include "SDL_sysrender.h"
  21. #include "SDL_render_debug_font.h"
  22. #include "software/SDL_render_sw_c.h"
  23. #include "../events/SDL_windowevents_c.h"
  24. #include "../video/SDL_pixels_c.h"
  25. #include "../video/SDL_video_c.h"
  26. #ifdef SDL_PLATFORM_ANDROID
  27. #include "../core/android/SDL_android.h"
  28. #include "../video/android/SDL_androidevents.h"
  29. #endif
  30. /* as a courtesy to iOS apps, we don't try to draw when in the background, as
  31. that will crash the app. However, these apps _should_ have used
  32. SDL_AddEventWatch to catch SDL_EVENT_WILL_ENTER_BACKGROUND events and stopped
  33. drawing themselves. Other platforms still draw, as the compositor can use it,
  34. and more importantly: drawing to render targets isn't lost. But I still think
  35. this should probably be removed at some point in the future. --ryan. */
  36. #if defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) || defined(SDL_PLATFORM_ANDROID)
  37. #define DONT_DRAW_WHILE_HIDDEN 1
  38. #else
  39. #define DONT_DRAW_WHILE_HIDDEN 0
  40. #endif
  41. #define SDL_PROP_WINDOW_RENDERER_POINTER "SDL.internal.window.renderer"
  42. #define SDL_PROP_TEXTURE_PARENT_POINTER "SDL.internal.texture.parent"
  43. #define CHECK_RENDERER_MAGIC_BUT_NOT_DESTROYED_FLAG(renderer, result) \
  44. if (!SDL_ObjectValid(renderer, SDL_OBJECT_TYPE_RENDERER)) { \
  45. SDL_InvalidParamError("renderer"); \
  46. return result; \
  47. }
  48. #define CHECK_RENDERER_MAGIC(renderer, result) \
  49. CHECK_RENDERER_MAGIC_BUT_NOT_DESTROYED_FLAG(renderer, result); \
  50. if ((renderer)->destroyed) { \
  51. SDL_SetError("Renderer's window has been destroyed, can't use further"); \
  52. return result; \
  53. }
  54. #define CHECK_TEXTURE_MAGIC(texture, result) \
  55. if (!SDL_ObjectValid(texture, SDL_OBJECT_TYPE_TEXTURE)) { \
  56. SDL_InvalidParamError("texture"); \
  57. return result; \
  58. }
  59. // Predefined blend modes
  60. #define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
  61. srcAlphaFactor, dstAlphaFactor, alphaOperation) \
  62. (SDL_BlendMode)(((Uint32)(colorOperation) << 0) | \
  63. ((Uint32)(srcColorFactor) << 4) | \
  64. ((Uint32)(dstColorFactor) << 8) | \
  65. ((Uint32)(alphaOperation) << 16) | \
  66. ((Uint32)(srcAlphaFactor) << 20) | \
  67. ((Uint32)(dstAlphaFactor) << 24))
  68. #define SDL_BLENDMODE_NONE_FULL \
  69. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
  70. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
  71. #define SDL_BLENDMODE_BLEND_FULL \
  72. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  73. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  74. #define SDL_BLENDMODE_BLEND_PREMULTIPLIED_FULL \
  75. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  76. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  77. #define SDL_BLENDMODE_ADD_FULL \
  78. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
  79. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  80. #define SDL_BLENDMODE_ADD_PREMULTIPLIED_FULL \
  81. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
  82. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  83. #define SDL_BLENDMODE_MOD_FULL \
  84. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
  85. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  86. #define SDL_BLENDMODE_MUL_FULL \
  87. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_DST_COLOR, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  88. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  89. #ifndef SDL_RENDER_DISABLED
  90. static const SDL_RenderDriver *render_drivers[] = {
  91. #ifdef SDL_VIDEO_RENDER_D3D11
  92. &D3D11_RenderDriver,
  93. #endif
  94. #ifdef SDL_VIDEO_RENDER_D3D12
  95. &D3D12_RenderDriver,
  96. #endif
  97. #ifdef SDL_VIDEO_RENDER_D3D
  98. &D3D_RenderDriver,
  99. #endif
  100. #ifdef SDL_VIDEO_RENDER_METAL
  101. &METAL_RenderDriver,
  102. #endif
  103. #ifdef SDL_VIDEO_RENDER_OGL
  104. &GL_RenderDriver,
  105. #endif
  106. #ifdef SDL_VIDEO_RENDER_OGL_ES2
  107. &GLES2_RenderDriver,
  108. #endif
  109. #ifdef SDL_VIDEO_RENDER_PS2
  110. &PS2_RenderDriver,
  111. #endif
  112. #ifdef SDL_VIDEO_RENDER_PSP
  113. &PSP_RenderDriver,
  114. #endif
  115. #ifdef SDL_VIDEO_RENDER_VITA_GXM
  116. &VITA_GXM_RenderDriver,
  117. #endif
  118. #ifdef SDL_VIDEO_RENDER_VULKAN
  119. &VULKAN_RenderDriver,
  120. #endif
  121. #ifdef SDL_VIDEO_RENDER_GPU
  122. &GPU_RenderDriver,
  123. #endif
  124. #ifdef SDL_VIDEO_RENDER_SW
  125. &SW_RenderDriver,
  126. #endif
  127. NULL
  128. };
  129. #endif // !SDL_RENDER_DISABLED
  130. static SDL_Renderer *SDL_renderers;
  131. static const int rect_index_order[] = { 0, 1, 2, 0, 2, 3 };
  132. void SDL_QuitRender(void)
  133. {
  134. while (SDL_renderers) {
  135. SDL_DestroyRenderer(SDL_renderers);
  136. }
  137. }
  138. bool SDL_AddSupportedTextureFormat(SDL_Renderer *renderer, SDL_PixelFormat format)
  139. {
  140. SDL_PixelFormat *texture_formats = (SDL_PixelFormat *)SDL_realloc((void *)renderer->texture_formats, (renderer->num_texture_formats + 2) * sizeof(SDL_PixelFormat));
  141. if (!texture_formats) {
  142. return false;
  143. }
  144. texture_formats[renderer->num_texture_formats++] = format;
  145. texture_formats[renderer->num_texture_formats] = SDL_PIXELFORMAT_UNKNOWN;
  146. renderer->texture_formats = texture_formats;
  147. SDL_SetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, texture_formats);
  148. return true;
  149. }
  150. void SDL_SetupRendererColorspace(SDL_Renderer *renderer, SDL_PropertiesID props)
  151. {
  152. renderer->output_colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB);
  153. }
  154. bool SDL_RenderingLinearSpace(SDL_Renderer *renderer)
  155. {
  156. SDL_Colorspace colorspace;
  157. if (renderer->target) {
  158. colorspace = renderer->target->colorspace;
  159. } else {
  160. colorspace = renderer->output_colorspace;
  161. }
  162. if (colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. void SDL_ConvertToLinear(SDL_FColor *color)
  168. {
  169. color->r = SDL_sRGBtoLinear(color->r);
  170. color->g = SDL_sRGBtoLinear(color->g);
  171. color->b = SDL_sRGBtoLinear(color->b);
  172. }
  173. void SDL_ConvertFromLinear(SDL_FColor *color)
  174. {
  175. color->r = SDL_sRGBfromLinear(color->r);
  176. color->g = SDL_sRGBfromLinear(color->g);
  177. color->b = SDL_sRGBfromLinear(color->b);
  178. }
  179. static SDL_INLINE void DebugLogRenderCommands(const SDL_RenderCommand *cmd)
  180. {
  181. #if 0
  182. unsigned int i = 1;
  183. SDL_Log("Render commands to flush:");
  184. while (cmd) {
  185. switch (cmd->command) {
  186. case SDL_RENDERCMD_NO_OP:
  187. SDL_Log(" %u. no-op", i++);
  188. break;
  189. case SDL_RENDERCMD_SETVIEWPORT:
  190. SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
  191. (unsigned int) cmd->data.viewport.first,
  192. cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
  193. cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
  194. break;
  195. case SDL_RENDERCMD_SETCLIPRECT:
  196. SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
  197. cmd->data.cliprect.enabled ? "true" : "false",
  198. cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
  199. cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
  200. break;
  201. case SDL_RENDERCMD_SETDRAWCOLOR:
  202. SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d, color_scale=%g)", i++,
  203. (unsigned int) cmd->data.color.first,
  204. (int) cmd->data.color.color.r, (int) cmd->data.color.color.g,
  205. (int) cmd->data.color.color.b, (int) cmd->data.color.color.a, cmd->data.color.color_scale);
  206. break;
  207. case SDL_RENDERCMD_CLEAR:
  208. SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d, color_scale=%g)", i++,
  209. (unsigned int) cmd->data.color.first,
  210. (int) cmd->data.color.color.r, (int) cmd->data.color.color.g,
  211. (int) cmd->data.color.color.b, (int) cmd->data.color.color.a, cmd->data.color.color_scale);
  212. break;
  213. case SDL_RENDERCMD_DRAW_POINTS:
  214. SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g)", i++,
  215. (unsigned int) cmd->data.draw.first,
  216. (unsigned int) cmd->data.draw.count,
  217. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  218. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  219. (int) cmd->data.draw.blend, cmd->data.draw.color_scale);
  220. break;
  221. case SDL_RENDERCMD_DRAW_LINES:
  222. SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g)", i++,
  223. (unsigned int) cmd->data.draw.first,
  224. (unsigned int) cmd->data.draw.count,
  225. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  226. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  227. (int) cmd->data.draw.blend, cmd->data.draw.color_scale);
  228. break;
  229. case SDL_RENDERCMD_FILL_RECTS:
  230. SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g)", i++,
  231. (unsigned int) cmd->data.draw.first,
  232. (unsigned int) cmd->data.draw.count,
  233. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  234. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  235. (int) cmd->data.draw.blend, cmd->data.draw.color_scale);
  236. break;
  237. case SDL_RENDERCMD_COPY:
  238. SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g, tex=%p)", i++,
  239. (unsigned int) cmd->data.draw.first,
  240. (unsigned int) cmd->data.draw.count,
  241. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  242. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  243. (int) cmd->data.draw.blend, cmd->data.draw.color_scale, cmd->data.draw.texture);
  244. break;
  245. case SDL_RENDERCMD_COPY_EX:
  246. SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g, tex=%p)", i++,
  247. (unsigned int) cmd->data.draw.first,
  248. (unsigned int) cmd->data.draw.count,
  249. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  250. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  251. (int) cmd->data.draw.blend, cmd->data.draw.color_scale, cmd->data.draw.texture);
  252. break;
  253. case SDL_RENDERCMD_GEOMETRY:
  254. SDL_Log(" %u. geometry (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, color_scale=%g, tex=%p)", i++,
  255. (unsigned int) cmd->data.draw.first,
  256. (unsigned int) cmd->data.draw.count,
  257. (int) cmd->data.draw.color.r, (int) cmd->data.draw.color.g,
  258. (int) cmd->data.draw.color.b, (int) cmd->data.draw.color.a,
  259. (int) cmd->data.draw.blend, cmd->data.draw.color_scale, cmd->data.draw.texture);
  260. break;
  261. }
  262. cmd = cmd->next;
  263. }
  264. #endif
  265. }
  266. static bool FlushRenderCommands(SDL_Renderer *renderer)
  267. {
  268. bool result;
  269. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  270. if (!renderer->render_commands) { // nothing to do!
  271. SDL_assert(renderer->vertex_data_used == 0);
  272. return true;
  273. }
  274. DebugLogRenderCommands(renderer->render_commands);
  275. result = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
  276. // Move the whole render command queue to the unused pool so we can reuse them next time.
  277. if (renderer->render_commands_tail) {
  278. renderer->render_commands_tail->next = renderer->render_commands_pool;
  279. renderer->render_commands_pool = renderer->render_commands;
  280. renderer->render_commands_tail = NULL;
  281. renderer->render_commands = NULL;
  282. }
  283. renderer->vertex_data_used = 0;
  284. renderer->render_command_generation++;
  285. renderer->color_queued = false;
  286. renderer->viewport_queued = false;
  287. renderer->cliprect_queued = false;
  288. return result;
  289. }
  290. static bool FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
  291. {
  292. SDL_Renderer *renderer = texture->renderer;
  293. if (texture->last_command_generation == renderer->render_command_generation) {
  294. // the current command queue depends on this texture, flush the queue now before it changes
  295. return FlushRenderCommands(renderer);
  296. }
  297. return true;
  298. }
  299. static bool FlushRenderCommandsIfGPURenderStateNeeded(SDL_GPURenderState *state)
  300. {
  301. SDL_Renderer *renderer = state->renderer;
  302. if (state->last_command_generation == renderer->render_command_generation) {
  303. // the current command queue depends on this state, flush the queue now before it changes
  304. return FlushRenderCommands(renderer);
  305. }
  306. return true;
  307. }
  308. bool SDL_FlushRenderer(SDL_Renderer *renderer)
  309. {
  310. if (!FlushRenderCommands(renderer)) {
  311. return false;
  312. }
  313. renderer->InvalidateCachedState(renderer);
  314. return true;
  315. }
  316. void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, size_t numbytes, size_t alignment, size_t *offset)
  317. {
  318. const size_t needed = renderer->vertex_data_used + numbytes + alignment;
  319. const size_t current_offset = renderer->vertex_data_used;
  320. const size_t aligner = (alignment && ((current_offset & (alignment - 1)) != 0)) ? (alignment - (current_offset & (alignment - 1))) : 0;
  321. const size_t aligned = current_offset + aligner;
  322. if (renderer->vertex_data_allocation < needed) {
  323. const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
  324. size_t newsize = current_allocation * 2;
  325. void *ptr;
  326. while (newsize < needed) {
  327. newsize *= 2;
  328. }
  329. ptr = SDL_realloc(renderer->vertex_data, newsize);
  330. if (!ptr) {
  331. return NULL;
  332. }
  333. renderer->vertex_data = ptr;
  334. renderer->vertex_data_allocation = newsize;
  335. }
  336. if (offset) {
  337. *offset = aligned;
  338. }
  339. renderer->vertex_data_used += aligner + numbytes;
  340. return ((Uint8 *)renderer->vertex_data) + aligned;
  341. }
  342. static SDL_RenderCommand *AllocateRenderCommand(SDL_Renderer *renderer)
  343. {
  344. SDL_RenderCommand *result = NULL;
  345. result = renderer->render_commands_pool;
  346. if (result) {
  347. renderer->render_commands_pool = result->next;
  348. result->next = NULL;
  349. } else {
  350. result = (SDL_RenderCommand *)SDL_calloc(1, sizeof(*result));
  351. if (!result) {
  352. return NULL;
  353. }
  354. }
  355. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  356. if (renderer->render_commands_tail) {
  357. renderer->render_commands_tail->next = result;
  358. } else {
  359. renderer->render_commands = result;
  360. }
  361. renderer->render_commands_tail = result;
  362. return result;
  363. }
  364. static void UpdatePixelViewport(SDL_Renderer *renderer, SDL_RenderViewState *view)
  365. {
  366. view->pixel_viewport.x = (int)SDL_floorf((view->viewport.x * view->current_scale.x) + view->logical_offset.x);
  367. view->pixel_viewport.y = (int)SDL_floorf((view->viewport.y * view->current_scale.y) + view->logical_offset.y);
  368. if (view->viewport.w >= 0) {
  369. view->pixel_viewport.w = (int)SDL_ceilf(view->viewport.w * view->current_scale.x);
  370. } else {
  371. view->pixel_viewport.w = view->pixel_w;
  372. }
  373. if (view->viewport.h >= 0) {
  374. view->pixel_viewport.h = (int)SDL_ceilf(view->viewport.h * view->current_scale.y);
  375. } else {
  376. view->pixel_viewport.h = view->pixel_h;
  377. }
  378. }
  379. static bool QueueCmdSetViewport(SDL_Renderer *renderer)
  380. {
  381. bool result = true;
  382. SDL_Rect viewport = renderer->view->pixel_viewport;
  383. if (!renderer->viewport_queued ||
  384. SDL_memcmp(&viewport, &renderer->last_queued_viewport, sizeof(viewport)) != 0) {
  385. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  386. if (cmd) {
  387. cmd->command = SDL_RENDERCMD_SETVIEWPORT;
  388. cmd->data.viewport.first = 0; // render backend will fill this in.
  389. SDL_copyp(&cmd->data.viewport.rect, &viewport);
  390. result = renderer->QueueSetViewport(renderer, cmd);
  391. if (!result) {
  392. cmd->command = SDL_RENDERCMD_NO_OP;
  393. } else {
  394. SDL_copyp(&renderer->last_queued_viewport, &viewport);
  395. renderer->viewport_queued = true;
  396. }
  397. } else {
  398. result = false;
  399. }
  400. }
  401. return result;
  402. }
  403. static void UpdatePixelClipRect(SDL_Renderer *renderer, SDL_RenderViewState *view)
  404. {
  405. const float scale_x = view->current_scale.x;
  406. const float scale_y = view->current_scale.y;
  407. view->pixel_clip_rect.x = (int)SDL_floorf(view->clip_rect.x * scale_x);
  408. view->pixel_clip_rect.y = (int)SDL_floorf(view->clip_rect.y * scale_y);
  409. view->pixel_clip_rect.w = (int)SDL_ceilf(view->clip_rect.w * scale_x);
  410. view->pixel_clip_rect.h = (int)SDL_ceilf(view->clip_rect.h * scale_y);
  411. }
  412. static bool QueueCmdSetClipRect(SDL_Renderer *renderer)
  413. {
  414. bool result = true;
  415. const SDL_RenderViewState *view = renderer->view;
  416. SDL_Rect clip_rect = view->pixel_clip_rect;
  417. if (!renderer->cliprect_queued ||
  418. view->clipping_enabled != renderer->last_queued_cliprect_enabled ||
  419. SDL_memcmp(&clip_rect, &renderer->last_queued_cliprect, sizeof(clip_rect)) != 0) {
  420. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  421. if (cmd) {
  422. cmd->command = SDL_RENDERCMD_SETCLIPRECT;
  423. cmd->data.cliprect.enabled = view->clipping_enabled;
  424. SDL_copyp(&cmd->data.cliprect.rect, &clip_rect);
  425. SDL_copyp(&renderer->last_queued_cliprect, &clip_rect);
  426. renderer->last_queued_cliprect_enabled = view->clipping_enabled;
  427. renderer->cliprect_queued = true;
  428. } else {
  429. result = false;
  430. }
  431. }
  432. return result;
  433. }
  434. static bool QueueCmdSetDrawColor(SDL_Renderer *renderer, SDL_FColor *color)
  435. {
  436. bool result = true;
  437. if (!renderer->color_queued ||
  438. color->r != renderer->last_queued_color.r ||
  439. color->g != renderer->last_queued_color.g ||
  440. color->b != renderer->last_queued_color.b ||
  441. color->a != renderer->last_queued_color.a) {
  442. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  443. result = false;
  444. if (cmd) {
  445. cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
  446. cmd->data.color.first = 0; // render backend will fill this in.
  447. cmd->data.color.color_scale = renderer->color_scale;
  448. cmd->data.color.color = *color;
  449. result = renderer->QueueSetDrawColor(renderer, cmd);
  450. if (!result) {
  451. cmd->command = SDL_RENDERCMD_NO_OP;
  452. } else {
  453. renderer->last_queued_color = *color;
  454. renderer->color_queued = true;
  455. }
  456. }
  457. }
  458. return result;
  459. }
  460. static bool QueueCmdClear(SDL_Renderer *renderer)
  461. {
  462. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  463. if (!cmd) {
  464. return false;
  465. }
  466. cmd->command = SDL_RENDERCMD_CLEAR;
  467. cmd->data.color.first = 0;
  468. cmd->data.color.color_scale = renderer->color_scale;
  469. cmd->data.color.color = renderer->color;
  470. return true;
  471. }
  472. static SDL_RenderCommand *PrepQueueCmdDraw(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype, SDL_Texture *texture)
  473. {
  474. SDL_RenderCommand *cmd = NULL;
  475. bool result = true;
  476. SDL_FColor *color;
  477. SDL_BlendMode blendMode;
  478. if (texture) {
  479. color = &texture->color;
  480. blendMode = texture->blendMode;
  481. } else {
  482. color = &renderer->color;
  483. blendMode = renderer->blendMode;
  484. }
  485. if (cmdtype != SDL_RENDERCMD_GEOMETRY) {
  486. result = QueueCmdSetDrawColor(renderer, color);
  487. }
  488. /* Set the viewport and clip rect directly before draws, so the backends
  489. * don't have to worry about that state not being valid at draw time. */
  490. if (result && !renderer->viewport_queued) {
  491. result = QueueCmdSetViewport(renderer);
  492. }
  493. if (result && !renderer->cliprect_queued) {
  494. result = QueueCmdSetClipRect(renderer);
  495. }
  496. if (result) {
  497. cmd = AllocateRenderCommand(renderer);
  498. if (cmd) {
  499. cmd->command = cmdtype;
  500. cmd->data.draw.first = 0; // render backend will fill this in.
  501. cmd->data.draw.count = 0; // render backend will fill this in.
  502. cmd->data.draw.color_scale = renderer->color_scale;
  503. cmd->data.draw.color = *color;
  504. cmd->data.draw.blend = blendMode;
  505. cmd->data.draw.texture = texture;
  506. if (texture) {
  507. cmd->data.draw.texture_scale_mode = texture->scaleMode;
  508. }
  509. cmd->data.draw.texture_address_mode = SDL_TEXTURE_ADDRESS_CLAMP;
  510. cmd->data.draw.gpu_render_state = renderer->gpu_render_state;
  511. if (renderer->gpu_render_state) {
  512. renderer->gpu_render_state->last_command_generation = renderer->render_command_generation;
  513. }
  514. }
  515. }
  516. return cmd;
  517. }
  518. static bool QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, const int count)
  519. {
  520. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_POINTS, NULL);
  521. bool result = false;
  522. if (cmd) {
  523. result = renderer->QueueDrawPoints(renderer, cmd, points, count);
  524. if (!result) {
  525. cmd->command = SDL_RENDERCMD_NO_OP;
  526. }
  527. }
  528. return result;
  529. }
  530. static bool QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint *points, const int count)
  531. {
  532. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_LINES, NULL);
  533. bool result = false;
  534. if (cmd) {
  535. result = renderer->QueueDrawLines(renderer, cmd, points, count);
  536. if (!result) {
  537. cmd->command = SDL_RENDERCMD_NO_OP;
  538. }
  539. }
  540. return result;
  541. }
  542. static bool QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, const int count)
  543. {
  544. SDL_RenderCommand *cmd;
  545. bool result = false;
  546. const int use_rendergeometry = (!renderer->QueueFillRects);
  547. cmd = PrepQueueCmdDraw(renderer, (use_rendergeometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS), NULL);
  548. if (cmd) {
  549. if (use_rendergeometry) {
  550. bool isstack1;
  551. bool isstack2;
  552. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  553. int *indices = SDL_small_alloc(int, 6 * count, &isstack2);
  554. if (xy && indices) {
  555. int i;
  556. float *ptr_xy = xy;
  557. int *ptr_indices = indices;
  558. const int xy_stride = 2 * sizeof(float);
  559. const int num_vertices = 4 * count;
  560. const int num_indices = 6 * count;
  561. const int size_indices = 4;
  562. int cur_index = 0;
  563. for (i = 0; i < count; ++i) {
  564. float minx, miny, maxx, maxy;
  565. minx = rects[i].x;
  566. miny = rects[i].y;
  567. maxx = rects[i].x + rects[i].w;
  568. maxy = rects[i].y + rects[i].h;
  569. *ptr_xy++ = minx;
  570. *ptr_xy++ = miny;
  571. *ptr_xy++ = maxx;
  572. *ptr_xy++ = miny;
  573. *ptr_xy++ = maxx;
  574. *ptr_xy++ = maxy;
  575. *ptr_xy++ = minx;
  576. *ptr_xy++ = maxy;
  577. *ptr_indices++ = cur_index + rect_index_order[0];
  578. *ptr_indices++ = cur_index + rect_index_order[1];
  579. *ptr_indices++ = cur_index + rect_index_order[2];
  580. *ptr_indices++ = cur_index + rect_index_order[3];
  581. *ptr_indices++ = cur_index + rect_index_order[4];
  582. *ptr_indices++ = cur_index + rect_index_order[5];
  583. cur_index += 4;
  584. }
  585. result = renderer->QueueGeometry(renderer, cmd, NULL,
  586. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  587. num_vertices, indices, num_indices, size_indices,
  588. 1.0f, 1.0f);
  589. if (!result) {
  590. cmd->command = SDL_RENDERCMD_NO_OP;
  591. }
  592. }
  593. SDL_small_free(xy, isstack1);
  594. SDL_small_free(indices, isstack2);
  595. } else {
  596. result = renderer->QueueFillRects(renderer, cmd, rects, count);
  597. if (!result) {
  598. cmd->command = SDL_RENDERCMD_NO_OP;
  599. }
  600. }
  601. }
  602. return result;
  603. }
  604. static bool QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect)
  605. {
  606. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
  607. bool result = false;
  608. if (cmd) {
  609. result = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
  610. if (!result) {
  611. cmd->command = SDL_RENDERCMD_NO_OP;
  612. }
  613. }
  614. return result;
  615. }
  616. static bool QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture *texture,
  617. const SDL_FRect *srcquad, const SDL_FRect *dstrect,
  618. const double angle, const SDL_FPoint *center, const SDL_FlipMode flip, float scale_x, float scale_y)
  619. {
  620. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
  621. bool result = false;
  622. if (cmd) {
  623. result = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip, scale_x, scale_y);
  624. if (!result) {
  625. cmd->command = SDL_RENDERCMD_NO_OP;
  626. }
  627. }
  628. return result;
  629. }
  630. static bool QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
  631. const float *xy, int xy_stride,
  632. const SDL_FColor *color, int color_stride,
  633. const float *uv, int uv_stride,
  634. int num_vertices,
  635. const void *indices, int num_indices, int size_indices,
  636. float scale_x, float scale_y, SDL_TextureAddressMode texture_address_mode)
  637. {
  638. SDL_RenderCommand *cmd;
  639. bool result = false;
  640. cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
  641. if (cmd) {
  642. cmd->data.draw.texture_address_mode = texture_address_mode;
  643. result = renderer->QueueGeometry(renderer, cmd, texture,
  644. xy, xy_stride,
  645. color, color_stride, uv, uv_stride,
  646. num_vertices, indices, num_indices, size_indices,
  647. scale_x, scale_y);
  648. if (!result) {
  649. cmd->command = SDL_RENDERCMD_NO_OP;
  650. }
  651. }
  652. return result;
  653. }
  654. static void UpdateMainViewDimensions(SDL_Renderer *renderer)
  655. {
  656. int window_w = 0, window_h = 0;
  657. if (renderer->window) {
  658. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  659. }
  660. SDL_GetRenderOutputSize(renderer, &renderer->main_view.pixel_w, &renderer->main_view.pixel_h);
  661. if (window_w > 0 && window_h > 0) {
  662. renderer->dpi_scale.x = (float)renderer->main_view.pixel_w / window_w;
  663. renderer->dpi_scale.y = (float)renderer->main_view.pixel_h / window_h;
  664. } else {
  665. renderer->dpi_scale.x = 1.0f;
  666. renderer->dpi_scale.y = 1.0f;
  667. }
  668. UpdatePixelViewport(renderer, &renderer->main_view);
  669. }
  670. static void UpdateColorScale(SDL_Renderer *renderer)
  671. {
  672. float SDR_white_point;
  673. if (renderer->target) {
  674. SDR_white_point = renderer->target->SDR_white_point;
  675. } else {
  676. SDR_white_point = renderer->SDR_white_point;
  677. }
  678. renderer->color_scale = renderer->desired_color_scale * SDR_white_point;
  679. }
  680. static void UpdateHDRProperties(SDL_Renderer *renderer)
  681. {
  682. SDL_PropertiesID window_props;
  683. SDL_PropertiesID renderer_props;
  684. window_props = SDL_GetWindowProperties(renderer->window);
  685. if (!window_props) {
  686. return;
  687. }
  688. renderer_props = SDL_GetRendererProperties(renderer);
  689. if (!renderer_props) {
  690. return;
  691. }
  692. if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
  693. renderer->SDR_white_point = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_SDR_WHITE_LEVEL_FLOAT, 1.0f);
  694. renderer->HDR_headroom = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_HDR_HEADROOM_FLOAT, 1.0f);
  695. } else {
  696. renderer->SDR_white_point = 1.0f;
  697. renderer->HDR_headroom = 1.0f;
  698. }
  699. if (renderer->HDR_headroom > 1.0f) {
  700. SDL_SetBooleanProperty(renderer_props, SDL_PROP_RENDERER_HDR_ENABLED_BOOLEAN, true);
  701. } else {
  702. SDL_SetBooleanProperty(renderer_props, SDL_PROP_RENDERER_HDR_ENABLED_BOOLEAN, false);
  703. }
  704. SDL_SetFloatProperty(renderer_props, SDL_PROP_RENDERER_SDR_WHITE_POINT_FLOAT, renderer->SDR_white_point);
  705. SDL_SetFloatProperty(renderer_props, SDL_PROP_RENDERER_HDR_HEADROOM_FLOAT, renderer->HDR_headroom);
  706. UpdateColorScale(renderer);
  707. }
  708. static void UpdateLogicalPresentation(SDL_Renderer *renderer);
  709. int SDL_GetNumRenderDrivers(void)
  710. {
  711. #ifndef SDL_RENDER_DISABLED
  712. return SDL_arraysize(render_drivers) - 1;
  713. #else
  714. return 0;
  715. #endif
  716. }
  717. const char *SDL_GetRenderDriver(int index)
  718. {
  719. #ifndef SDL_RENDER_DISABLED
  720. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  721. SDL_InvalidParamError("index");
  722. return NULL;
  723. }
  724. return render_drivers[index]->name;
  725. #else
  726. SDL_SetError("SDL not built with rendering support");
  727. return NULL;
  728. #endif
  729. }
  730. static bool SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  731. {
  732. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  733. SDL_Window *window = renderer->window;
  734. if (renderer->WindowEvent) {
  735. renderer->WindowEvent(renderer, &event->window);
  736. }
  737. if (event->type == SDL_EVENT_WINDOW_RESIZED ||
  738. event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED ||
  739. event->type == SDL_EVENT_WINDOW_METAL_VIEW_RESIZED) {
  740. SDL_RenderViewState *view = renderer->view;
  741. renderer->view = &renderer->main_view; // only update the main_view (the window framebuffer) for window changes.
  742. UpdateLogicalPresentation(renderer);
  743. renderer->view = view; // put us back on whatever the current render target's actual view is.
  744. } else if (event->type == SDL_EVENT_WINDOW_HIDDEN) {
  745. renderer->hidden = true;
  746. } else if (event->type == SDL_EVENT_WINDOW_SHOWN) {
  747. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  748. renderer->hidden = false;
  749. }
  750. } else if (event->type == SDL_EVENT_WINDOW_MINIMIZED) {
  751. renderer->hidden = true;
  752. } else if (event->type == SDL_EVENT_WINDOW_RESTORED ||
  753. event->type == SDL_EVENT_WINDOW_MAXIMIZED) {
  754. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  755. renderer->hidden = false;
  756. }
  757. } else if (event->type == SDL_EVENT_WINDOW_DISPLAY_CHANGED ||
  758. event->type == SDL_EVENT_WINDOW_HDR_STATE_CHANGED) {
  759. UpdateHDRProperties(renderer);
  760. }
  761. return true;
  762. }
  763. bool SDL_CreateWindowAndRenderer(const char *title, int width, int height, SDL_WindowFlags window_flags, SDL_Window **window, SDL_Renderer **renderer)
  764. {
  765. bool hidden = (window_flags & SDL_WINDOW_HIDDEN) != 0;
  766. if (!window) {
  767. return SDL_InvalidParamError("window");
  768. }
  769. if (!renderer) {
  770. return SDL_InvalidParamError("renderer");
  771. }
  772. // Hide the window so if the renderer recreates it, we don't get a visual flash on screen
  773. window_flags |= SDL_WINDOW_HIDDEN;
  774. *window = SDL_CreateWindow(title, width, height, window_flags);
  775. if (!*window) {
  776. *renderer = NULL;
  777. return false;
  778. }
  779. *renderer = SDL_CreateRenderer(*window, NULL);
  780. if (!*renderer) {
  781. SDL_DestroyWindow(*window);
  782. *window = NULL;
  783. return false;
  784. }
  785. if (!hidden) {
  786. SDL_ShowWindow(*window);
  787. }
  788. return true;
  789. }
  790. #ifndef SDL_RENDER_DISABLED
  791. static SDL_INLINE void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
  792. {
  793. /* all of these functions are required to be implemented, even as no-ops, so we don't
  794. have to check that they aren't NULL over and over. */
  795. SDL_assert(renderer->QueueSetViewport != NULL);
  796. SDL_assert(renderer->QueueSetDrawColor != NULL);
  797. SDL_assert(renderer->QueueDrawPoints != NULL);
  798. SDL_assert(renderer->QueueDrawLines != NULL || renderer->QueueGeometry != NULL);
  799. SDL_assert(renderer->QueueFillRects != NULL || renderer->QueueGeometry != NULL);
  800. SDL_assert(renderer->QueueCopy != NULL || renderer->QueueGeometry != NULL);
  801. SDL_assert(renderer->RunCommandQueue != NULL);
  802. }
  803. static SDL_RenderLineMethod SDL_GetRenderLineMethod(void)
  804. {
  805. const char *hint = SDL_GetHint(SDL_HINT_RENDER_LINE_METHOD);
  806. int method = 0;
  807. if (hint) {
  808. method = SDL_atoi(hint);
  809. }
  810. switch (method) {
  811. case 1:
  812. return SDL_RENDERLINEMETHOD_POINTS;
  813. case 2:
  814. return SDL_RENDERLINEMETHOD_LINES;
  815. case 3:
  816. return SDL_RENDERLINEMETHOD_GEOMETRY;
  817. default:
  818. return SDL_RENDERLINEMETHOD_POINTS;
  819. }
  820. }
  821. static void SDL_CalculateSimulatedVSyncInterval(SDL_Renderer *renderer, SDL_Window *window)
  822. {
  823. SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
  824. const SDL_DisplayMode *mode;
  825. int refresh_num, refresh_den;
  826. if (displayID == 0) {
  827. displayID = SDL_GetPrimaryDisplay();
  828. }
  829. mode = SDL_GetDesktopDisplayMode(displayID);
  830. if (mode && mode->refresh_rate_numerator > 0 && mode->refresh_rate_denominator > 0) {
  831. refresh_num = mode->refresh_rate_numerator;
  832. refresh_den = mode->refresh_rate_denominator;
  833. } else {
  834. // Pick a good default refresh rate
  835. refresh_num = 60;
  836. refresh_den = 1;
  837. }
  838. // Flip numerator and denominator to change from framerate to interval
  839. renderer->simulate_vsync_interval_ns = (SDL_NS_PER_SECOND * refresh_den) / refresh_num;
  840. }
  841. #endif // !SDL_RENDER_DISABLED
  842. SDL_Renderer *SDL_CreateRendererWithProperties(SDL_PropertiesID props)
  843. {
  844. #ifndef SDL_RENDER_DISABLED
  845. SDL_Window *window = (SDL_Window *)SDL_GetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, NULL);
  846. SDL_Surface *surface = (SDL_Surface *)SDL_GetPointerProperty(props, SDL_PROP_RENDERER_CREATE_SURFACE_POINTER, NULL);
  847. const char *driver_name = SDL_GetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, NULL);
  848. const char *hint;
  849. SDL_PropertiesID new_props;
  850. #ifdef SDL_PLATFORM_ANDROID
  851. if (!Android_WaitActiveAndLockActivity()) {
  852. return NULL;
  853. }
  854. #endif
  855. SDL_Renderer *renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
  856. if (!renderer) {
  857. goto error;
  858. }
  859. SDL_SetObjectValid(renderer, SDL_OBJECT_TYPE_RENDERER, true);
  860. if ((!window && !surface) || (window && surface)) {
  861. SDL_InvalidParamError("window");
  862. goto error;
  863. }
  864. if (window && SDL_WindowHasSurface(window)) {
  865. SDL_SetError("Surface already associated with window");
  866. goto error;
  867. }
  868. if (window && SDL_GetRenderer(window)) {
  869. SDL_SetError("Renderer already associated with window");
  870. goto error;
  871. }
  872. hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
  873. if (hint && *hint) {
  874. SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, true));
  875. }
  876. if (surface) {
  877. #ifdef SDL_VIDEO_RENDER_SW
  878. const bool rc = SW_CreateRendererForSurface(renderer, surface, props);
  879. #else
  880. const bool rc = SDL_SetError("SDL not built with software renderer");
  881. #endif
  882. if (!rc) {
  883. goto error;
  884. }
  885. } else {
  886. bool rc = false;
  887. if (!driver_name) {
  888. driver_name = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  889. }
  890. if (driver_name && *driver_name != 0) {
  891. const char *driver_attempt = driver_name;
  892. while (driver_attempt && *driver_attempt != 0 && !rc) {
  893. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  894. const size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt) : SDL_strlen(driver_attempt);
  895. for (int i = 0; render_drivers[i]; i++) {
  896. const SDL_RenderDriver *driver = render_drivers[i];
  897. if ((driver_attempt_len == SDL_strlen(driver->name)) && (SDL_strncasecmp(driver->name, driver_attempt, driver_attempt_len) == 0)) {
  898. rc = driver->CreateRenderer(renderer, window, props);
  899. if (rc) {
  900. break;
  901. }
  902. }
  903. }
  904. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  905. }
  906. } else {
  907. for (int i = 0; render_drivers[i]; i++) {
  908. const SDL_RenderDriver *driver = render_drivers[i];
  909. rc = driver->CreateRenderer(renderer, window, props);
  910. if (rc) {
  911. break;
  912. }
  913. SDL_DestroyRendererWithoutFreeing(renderer);
  914. SDL_zerop(renderer); // make sure we don't leave function pointers from a previous CreateRenderer() in this struct.
  915. }
  916. }
  917. if (!rc) {
  918. if (driver_name) {
  919. SDL_SetError("%s not available", driver_name);
  920. } else {
  921. SDL_SetError("Couldn't find matching render driver");
  922. }
  923. goto error;
  924. }
  925. }
  926. VerifyDrawQueueFunctions(renderer);
  927. renderer->window = window;
  928. renderer->target_mutex = SDL_CreateMutex();
  929. if (surface) {
  930. renderer->main_view.pixel_w = surface->w;
  931. renderer->main_view.pixel_h = surface->h;
  932. }
  933. renderer->main_view.viewport.w = -1;
  934. renderer->main_view.viewport.h = -1;
  935. renderer->main_view.scale.x = 1.0f;
  936. renderer->main_view.scale.y = 1.0f;
  937. renderer->main_view.logical_scale.x = 1.0f;
  938. renderer->main_view.logical_scale.y = 1.0f;
  939. renderer->main_view.current_scale.x = 1.0f;
  940. renderer->main_view.current_scale.y = 1.0f;
  941. renderer->view = &renderer->main_view;
  942. renderer->dpi_scale.x = 1.0f;
  943. renderer->dpi_scale.y = 1.0f;
  944. UpdatePixelViewport(renderer, &renderer->main_view);
  945. UpdatePixelClipRect(renderer, &renderer->main_view);
  946. UpdateMainViewDimensions(renderer);
  947. // new textures start at zero, so we start at 1 so first render doesn't flush by accident.
  948. renderer->render_command_generation = 1;
  949. if (renderer->software) {
  950. // Software renderer always uses line method, for speed
  951. renderer->line_method = SDL_RENDERLINEMETHOD_LINES;
  952. } else {
  953. renderer->line_method = SDL_GetRenderLineMethod();
  954. }
  955. renderer->scale_mode = SDL_SCALEMODE_LINEAR;
  956. renderer->SDR_white_point = 1.0f;
  957. renderer->HDR_headroom = 1.0f;
  958. renderer->desired_color_scale = 1.0f;
  959. renderer->color_scale = 1.0f;
  960. if (window) {
  961. if (SDL_GetWindowFlags(window) & SDL_WINDOW_TRANSPARENT) {
  962. renderer->transparent_window = true;
  963. }
  964. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) {
  965. renderer->hidden = true;
  966. }
  967. }
  968. new_props = SDL_GetRendererProperties(renderer);
  969. SDL_SetStringProperty(new_props, SDL_PROP_RENDERER_NAME_STRING, renderer->name);
  970. if (window) {
  971. SDL_SetPointerProperty(new_props, SDL_PROP_RENDERER_WINDOW_POINTER, window);
  972. }
  973. if (surface) {
  974. SDL_SetPointerProperty(new_props, SDL_PROP_RENDERER_SURFACE_POINTER, surface);
  975. }
  976. SDL_SetNumberProperty(new_props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, renderer->output_colorspace);
  977. UpdateHDRProperties(renderer);
  978. if (window) {
  979. SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_RENDERER_POINTER, renderer);
  980. SDL_AddWindowRenderer(window, renderer);
  981. }
  982. SDL_SetRenderViewport(renderer, NULL);
  983. if (window) {
  984. SDL_AddWindowEventWatch(SDL_WINDOW_EVENT_WATCH_NORMAL, SDL_RendererEventWatch, renderer);
  985. }
  986. int vsync = (int)SDL_GetNumberProperty(props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, 0);
  987. SDL_SetRenderVSync(renderer, vsync);
  988. SDL_CalculateSimulatedVSyncInterval(renderer, window);
  989. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  990. "Created renderer: %s", renderer->name);
  991. renderer->next = SDL_renderers;
  992. SDL_renderers = renderer;
  993. #ifdef SDL_PLATFORM_ANDROID
  994. Android_UnlockActivityMutex();
  995. #endif
  996. SDL_ClearError();
  997. return renderer;
  998. error:
  999. #ifdef SDL_PLATFORM_ANDROID
  1000. Android_UnlockActivityMutex();
  1001. #endif
  1002. if (renderer) {
  1003. SDL_DestroyRenderer(renderer);
  1004. }
  1005. return NULL;
  1006. #else
  1007. SDL_SetError("SDL not built with rendering support");
  1008. return NULL;
  1009. #endif
  1010. }
  1011. SDL_Renderer *SDL_CreateRenderer(SDL_Window *window, const char *name)
  1012. {
  1013. SDL_Renderer *renderer;
  1014. SDL_PropertiesID props = SDL_CreateProperties();
  1015. SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
  1016. SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, name);
  1017. renderer = SDL_CreateRendererWithProperties(props);
  1018. SDL_DestroyProperties(props);
  1019. return renderer;
  1020. }
  1021. SDL_Renderer *SDL_CreateSoftwareRenderer(SDL_Surface *surface)
  1022. {
  1023. #ifdef SDL_VIDEO_RENDER_SW
  1024. SDL_Renderer *renderer;
  1025. if (!surface) {
  1026. SDL_InvalidParamError("surface");
  1027. return NULL;
  1028. }
  1029. SDL_PropertiesID props = SDL_CreateProperties();
  1030. SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_SURFACE_POINTER, surface);
  1031. renderer = SDL_CreateRendererWithProperties(props);
  1032. SDL_DestroyProperties(props);
  1033. return renderer;
  1034. #else
  1035. SDL_SetError("SDL not built with rendering support");
  1036. return NULL;
  1037. #endif // !SDL_RENDER_DISABLED
  1038. }
  1039. SDL_Renderer *SDL_GetRenderer(SDL_Window *window)
  1040. {
  1041. return (SDL_Renderer *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_RENDERER_POINTER, NULL);
  1042. }
  1043. SDL_Window *SDL_GetRenderWindow(SDL_Renderer *renderer)
  1044. {
  1045. CHECK_RENDERER_MAGIC(renderer, NULL);
  1046. return renderer->window;
  1047. }
  1048. const char *SDL_GetRendererName(SDL_Renderer *renderer)
  1049. {
  1050. CHECK_RENDERER_MAGIC(renderer, NULL);
  1051. return SDL_GetPersistentString(renderer->name);
  1052. }
  1053. SDL_PropertiesID SDL_GetRendererProperties(SDL_Renderer *renderer)
  1054. {
  1055. CHECK_RENDERER_MAGIC(renderer, 0);
  1056. if (renderer->props == 0) {
  1057. renderer->props = SDL_CreateProperties();
  1058. }
  1059. return renderer->props;
  1060. }
  1061. bool SDL_GetRenderOutputSize(SDL_Renderer *renderer, int *w, int *h)
  1062. {
  1063. if (w) {
  1064. *w = 0;
  1065. }
  1066. if (h) {
  1067. *h = 0;
  1068. }
  1069. CHECK_RENDERER_MAGIC(renderer, false);
  1070. if (renderer->GetOutputSize) {
  1071. return renderer->GetOutputSize(renderer, w, h);
  1072. } else if (renderer->window) {
  1073. return SDL_GetWindowSizeInPixels(renderer->window, w, h);
  1074. } else {
  1075. SDL_assert(!"This should never happen");
  1076. return SDL_SetError("Renderer doesn't support querying output size");
  1077. }
  1078. }
  1079. bool SDL_GetCurrentRenderOutputSize(SDL_Renderer *renderer, int *w, int *h)
  1080. {
  1081. if (w) {
  1082. *w = 0;
  1083. }
  1084. if (h) {
  1085. *h = 0;
  1086. }
  1087. CHECK_RENDERER_MAGIC(renderer, false);
  1088. const SDL_RenderViewState *view = renderer->view;
  1089. if (w) {
  1090. *w = view->pixel_w;
  1091. }
  1092. if (h) {
  1093. *h = view->pixel_h;
  1094. }
  1095. return true;
  1096. }
  1097. static bool IsSupportedBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  1098. {
  1099. switch (blendMode) {
  1100. // These are required to be supported by all renderers
  1101. case SDL_BLENDMODE_NONE:
  1102. case SDL_BLENDMODE_BLEND:
  1103. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  1104. case SDL_BLENDMODE_ADD:
  1105. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  1106. case SDL_BLENDMODE_MOD:
  1107. case SDL_BLENDMODE_MUL:
  1108. return true;
  1109. default:
  1110. return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
  1111. }
  1112. }
  1113. static bool IsSupportedFormat(SDL_Renderer *renderer, SDL_PixelFormat format)
  1114. {
  1115. int i;
  1116. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1117. if (renderer->texture_formats[i] == format) {
  1118. return true;
  1119. }
  1120. }
  1121. return false;
  1122. }
  1123. static SDL_PixelFormat GetClosestSupportedFormat(SDL_Renderer *renderer, SDL_PixelFormat format)
  1124. {
  1125. int i;
  1126. if (format == SDL_PIXELFORMAT_MJPG) {
  1127. // We'll decode to SDL_PIXELFORMAT_NV12 or SDL_PIXELFORMAT_RGBA32
  1128. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1129. if (renderer->texture_formats[i] == SDL_PIXELFORMAT_NV12) {
  1130. return renderer->texture_formats[i];
  1131. }
  1132. }
  1133. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1134. if (renderer->texture_formats[i] == SDL_PIXELFORMAT_RGBA32) {
  1135. return renderer->texture_formats[i];
  1136. }
  1137. }
  1138. } else if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  1139. // Look for an exact match
  1140. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1141. if (renderer->texture_formats[i] == format) {
  1142. return renderer->texture_formats[i];
  1143. }
  1144. }
  1145. } else if (SDL_ISPIXELFORMAT_10BIT(format) || SDL_ISPIXELFORMAT_FLOAT(format)) {
  1146. if (SDL_ISPIXELFORMAT_10BIT(format)) {
  1147. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1148. if (SDL_ISPIXELFORMAT_10BIT(renderer->texture_formats[i])) {
  1149. return renderer->texture_formats[i];
  1150. }
  1151. }
  1152. }
  1153. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1154. if (SDL_ISPIXELFORMAT_FLOAT(renderer->texture_formats[i])) {
  1155. return renderer->texture_formats[i];
  1156. }
  1157. }
  1158. } else {
  1159. bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  1160. // We just want to match the first format that has the same channels
  1161. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1162. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->texture_formats[i]) &&
  1163. SDL_ISPIXELFORMAT_ALPHA(renderer->texture_formats[i]) == hasAlpha) {
  1164. return renderer->texture_formats[i];
  1165. }
  1166. }
  1167. }
  1168. return renderer->texture_formats[0];
  1169. }
  1170. SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_PropertiesID props)
  1171. {
  1172. SDL_Texture *texture;
  1173. SDL_PixelFormat format = (SDL_PixelFormat)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, SDL_PIXELFORMAT_UNKNOWN);
  1174. SDL_TextureAccess access = (SDL_TextureAccess)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STATIC);
  1175. int w = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, 0);
  1176. int h = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, 0);
  1177. SDL_Colorspace default_colorspace;
  1178. bool texture_is_fourcc_and_target;
  1179. CHECK_RENDERER_MAGIC(renderer, NULL);
  1180. if (!format) {
  1181. format = renderer->texture_formats[0];
  1182. }
  1183. if (SDL_BYTESPERPIXEL(format) == 0) {
  1184. SDL_SetError("Invalid texture format");
  1185. return NULL;
  1186. }
  1187. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1188. if (!IsSupportedFormat(renderer, format)) {
  1189. SDL_SetError("Palettized textures are not supported");
  1190. return NULL;
  1191. }
  1192. }
  1193. if (w <= 0 || h <= 0) {
  1194. SDL_SetError("Texture dimensions can't be 0");
  1195. return NULL;
  1196. }
  1197. int max_texture_size = (int)SDL_GetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, 0);
  1198. if (max_texture_size && (w > max_texture_size || h > max_texture_size)) {
  1199. SDL_SetError("Texture dimensions are limited to %dx%d", max_texture_size, max_texture_size);
  1200. return NULL;
  1201. }
  1202. default_colorspace = SDL_GetDefaultColorspaceForFormat(format);
  1203. texture = (SDL_Texture *)SDL_calloc(1, sizeof(*texture));
  1204. if (!texture) {
  1205. return NULL;
  1206. }
  1207. texture->refcount = 1;
  1208. SDL_SetObjectValid(texture, SDL_OBJECT_TYPE_TEXTURE, true);
  1209. texture->colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, default_colorspace);
  1210. texture->format = format;
  1211. texture->access = access;
  1212. texture->w = w;
  1213. texture->h = h;
  1214. texture->color.r = 1.0f;
  1215. texture->color.g = 1.0f;
  1216. texture->color.b = 1.0f;
  1217. texture->color.a = 1.0f;
  1218. texture->blendMode = SDL_ISPIXELFORMAT_ALPHA(format) ? SDL_BLENDMODE_BLEND : SDL_BLENDMODE_NONE;
  1219. texture->scaleMode = renderer->scale_mode;
  1220. texture->view.pixel_w = w;
  1221. texture->view.pixel_h = h;
  1222. texture->view.viewport.w = -1;
  1223. texture->view.viewport.h = -1;
  1224. texture->view.scale.x = 1.0f;
  1225. texture->view.scale.y = 1.0f;
  1226. texture->view.logical_scale.x = 1.0f;
  1227. texture->view.logical_scale.y = 1.0f;
  1228. texture->view.current_scale.x = 1.0f;
  1229. texture->view.current_scale.y = 1.0f;
  1230. texture->renderer = renderer;
  1231. texture->next = renderer->textures;
  1232. if (renderer->textures) {
  1233. renderer->textures->prev = texture;
  1234. }
  1235. renderer->textures = texture;
  1236. UpdatePixelViewport(renderer, &texture->view);
  1237. UpdatePixelClipRect(renderer, &texture->view);
  1238. texture->SDR_white_point = SDL_GetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_SDR_WHITE_POINT_FLOAT, SDL_GetDefaultSDRWhitePoint(texture->colorspace));
  1239. texture->HDR_headroom = SDL_GetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_HDR_HEADROOM_FLOAT, SDL_GetDefaultHDRHeadroom(texture->colorspace));
  1240. // FOURCC format cannot be used directly by renderer back-ends for target texture
  1241. texture_is_fourcc_and_target = (access == SDL_TEXTUREACCESS_TARGET && SDL_ISPIXELFORMAT_FOURCC(format));
  1242. if (!texture_is_fourcc_and_target && IsSupportedFormat(renderer, format)) {
  1243. if (!renderer->CreateTexture(renderer, texture, props)) {
  1244. SDL_DestroyTexture(texture);
  1245. return NULL;
  1246. }
  1247. } else {
  1248. SDL_PixelFormat closest_format;
  1249. SDL_PropertiesID native_props = SDL_CreateProperties();
  1250. if (!texture_is_fourcc_and_target) {
  1251. closest_format = GetClosestSupportedFormat(renderer, format);
  1252. } else {
  1253. closest_format = renderer->texture_formats[0];
  1254. }
  1255. if (format == SDL_PIXELFORMAT_MJPG && closest_format == SDL_PIXELFORMAT_NV12) {
  1256. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, SDL_COLORSPACE_JPEG);
  1257. } else {
  1258. default_colorspace = SDL_GetDefaultColorspaceForFormat(closest_format);
  1259. if (SDL_COLORSPACETYPE(texture->colorspace) == SDL_COLORSPACETYPE(default_colorspace)) {
  1260. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, texture->colorspace);
  1261. } else {
  1262. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, default_colorspace);
  1263. }
  1264. }
  1265. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, closest_format);
  1266. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, texture->access);
  1267. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, texture->w);
  1268. SDL_SetNumberProperty(native_props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, texture->h);
  1269. texture->native = SDL_CreateTextureWithProperties(renderer, native_props);
  1270. SDL_DestroyProperties(native_props);
  1271. if (!texture->native) {
  1272. SDL_DestroyTexture(texture);
  1273. return NULL;
  1274. }
  1275. SDL_SetPointerProperty(SDL_GetTextureProperties(texture->native), SDL_PROP_TEXTURE_PARENT_POINTER, texture);
  1276. // Swap textures to have texture before texture->native in the list
  1277. texture->native->next = texture->next;
  1278. if (texture->native->next) {
  1279. texture->native->next->prev = texture->native;
  1280. }
  1281. texture->prev = texture->native->prev;
  1282. if (texture->prev) {
  1283. texture->prev->next = texture;
  1284. }
  1285. texture->native->prev = texture;
  1286. texture->next = texture->native;
  1287. renderer->textures = texture;
  1288. if (texture->format == SDL_PIXELFORMAT_MJPG) {
  1289. // We have a custom decode + upload path for this
  1290. } else if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  1291. #ifdef SDL_HAVE_YUV
  1292. texture->yuv = SDL_SW_CreateYUVTexture(texture->format, texture->colorspace, w, h);
  1293. #else
  1294. SDL_SetError("SDL not built with YUV support");
  1295. #endif
  1296. if (!texture->yuv) {
  1297. SDL_DestroyTexture(texture);
  1298. return NULL;
  1299. }
  1300. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  1301. // The pitch is 4 byte aligned
  1302. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  1303. texture->pixels = SDL_calloc(1, (size_t)texture->pitch * h);
  1304. if (!texture->pixels) {
  1305. SDL_DestroyTexture(texture);
  1306. return NULL;
  1307. }
  1308. }
  1309. }
  1310. // Now set the properties for the new texture
  1311. props = SDL_GetTextureProperties(texture);
  1312. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_COLORSPACE_NUMBER, texture->colorspace);
  1313. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_FORMAT_NUMBER, texture->format);
  1314. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_ACCESS_NUMBER, texture->access);
  1315. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, texture->w);
  1316. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, texture->h);
  1317. SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_SDR_WHITE_POINT_FLOAT, texture->SDR_white_point);
  1318. if (texture->HDR_headroom > 0.0f) {
  1319. SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_HDR_HEADROOM_FLOAT, texture->HDR_headroom);
  1320. }
  1321. return texture;
  1322. }
  1323. SDL_Texture *SDL_CreateTexture(SDL_Renderer *renderer, SDL_PixelFormat format, SDL_TextureAccess access, int w, int h)
  1324. {
  1325. SDL_Texture *texture;
  1326. SDL_PropertiesID props = SDL_CreateProperties();
  1327. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, format);
  1328. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, access);
  1329. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, w);
  1330. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, h);
  1331. texture = SDL_CreateTextureWithProperties(renderer, props);
  1332. SDL_DestroyProperties(props);
  1333. return texture;
  1334. }
  1335. static bool SDL_UpdateTextureFromSurface(SDL_Texture *texture, SDL_Rect *rect, SDL_Surface *surface)
  1336. {
  1337. SDL_TextureAccess access;
  1338. bool direct_update;
  1339. SDL_PixelFormat tex_format;
  1340. SDL_PropertiesID surface_props;
  1341. SDL_PropertiesID tex_props;
  1342. SDL_Colorspace surface_colorspace = SDL_COLORSPACE_UNKNOWN;
  1343. SDL_Colorspace texture_colorspace = SDL_COLORSPACE_UNKNOWN;
  1344. if (texture == NULL || surface == NULL) {
  1345. return false;
  1346. }
  1347. tex_props = SDL_GetTextureProperties(texture);
  1348. if (!tex_props) {
  1349. return false;
  1350. }
  1351. surface_props = SDL_GetSurfaceProperties(surface);
  1352. if (!surface_props) {
  1353. return false;
  1354. }
  1355. tex_format = (SDL_PixelFormat)SDL_GetNumberProperty(tex_props, SDL_PROP_TEXTURE_FORMAT_NUMBER, 0);
  1356. access = (SDL_TextureAccess)SDL_GetNumberProperty(tex_props, SDL_PROP_TEXTURE_ACCESS_NUMBER, 0);
  1357. if (access != SDL_TEXTUREACCESS_STATIC && access != SDL_TEXTUREACCESS_STREAMING) {
  1358. return false;
  1359. }
  1360. surface_colorspace = SDL_GetSurfaceColorspace(surface);
  1361. texture_colorspace = surface_colorspace;
  1362. if (surface_colorspace == SDL_COLORSPACE_SRGB_LINEAR ||
  1363. SDL_COLORSPACETRANSFER(surface_colorspace) == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  1364. if (SDL_ISPIXELFORMAT_FLOAT(tex_format)) {
  1365. texture_colorspace = SDL_COLORSPACE_SRGB_LINEAR;
  1366. } else if (SDL_ISPIXELFORMAT_10BIT(tex_format)) {
  1367. texture_colorspace = SDL_COLORSPACE_HDR10;
  1368. } else {
  1369. texture_colorspace = SDL_COLORSPACE_SRGB;
  1370. }
  1371. }
  1372. if (tex_format == surface->format && texture_colorspace == surface_colorspace) {
  1373. if (SDL_ISPIXELFORMAT_ALPHA(surface->format) && SDL_SurfaceHasColorKey(surface)) {
  1374. /* Surface and Renderer formats are identical.
  1375. * Intermediate conversion is needed to convert color key to alpha (SDL_ConvertColorkeyToAlpha()). */
  1376. direct_update = false;
  1377. } else {
  1378. // Update Texture directly
  1379. direct_update = true;
  1380. }
  1381. } else {
  1382. // Surface and Renderer formats are different, it needs an intermediate conversion.
  1383. direct_update = false;
  1384. }
  1385. if (direct_update) {
  1386. if (SDL_MUSTLOCK(surface)) {
  1387. SDL_LockSurface(surface);
  1388. SDL_UpdateTexture(texture, rect, surface->pixels, surface->pitch);
  1389. SDL_UnlockSurface(surface);
  1390. } else {
  1391. SDL_UpdateTexture(texture, rect, surface->pixels, surface->pitch);
  1392. }
  1393. } else {
  1394. SDL_Surface *temp = NULL;
  1395. // Set up a destination surface for the texture update
  1396. temp = SDL_ConvertSurfaceAndColorspace(surface, tex_format, NULL, texture_colorspace, surface_props);
  1397. if (temp) {
  1398. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  1399. SDL_DestroySurface(temp);
  1400. } else {
  1401. return false;
  1402. }
  1403. }
  1404. {
  1405. Uint8 r, g, b, a;
  1406. SDL_BlendMode blendMode;
  1407. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1408. SDL_SetTextureColorMod(texture, r, g, b);
  1409. SDL_GetSurfaceAlphaMod(surface, &a);
  1410. SDL_SetTextureAlphaMod(texture, a);
  1411. if (SDL_SurfaceHasColorKey(surface)) {
  1412. // We converted to a texture with alpha format
  1413. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  1414. } else {
  1415. SDL_GetSurfaceBlendMode(surface, &blendMode);
  1416. SDL_SetTextureBlendMode(texture, blendMode);
  1417. }
  1418. }
  1419. return true;
  1420. }
  1421. SDL_Texture *SDL_CreateTextureFromSurface(SDL_Renderer *renderer, SDL_Surface *surface)
  1422. {
  1423. bool needAlpha;
  1424. int i;
  1425. SDL_PixelFormat format = SDL_PIXELFORMAT_UNKNOWN;
  1426. SDL_Palette *palette;
  1427. SDL_Texture *texture;
  1428. SDL_PropertiesID props;
  1429. SDL_Colorspace surface_colorspace = SDL_COLORSPACE_UNKNOWN;
  1430. SDL_Colorspace texture_colorspace = SDL_COLORSPACE_UNKNOWN;
  1431. CHECK_RENDERER_MAGIC(renderer, NULL);
  1432. if (!SDL_SurfaceValid(surface)) {
  1433. SDL_InvalidParamError("SDL_CreateTextureFromSurface(): surface");
  1434. return NULL;
  1435. }
  1436. // See what the best texture format is
  1437. if (SDL_ISPIXELFORMAT_ALPHA(surface->format) || SDL_SurfaceHasColorKey(surface)) {
  1438. needAlpha = true;
  1439. } else {
  1440. needAlpha = false;
  1441. }
  1442. // If Palette contains alpha values, promotes to alpha format
  1443. palette = SDL_GetSurfacePalette(surface);
  1444. if (palette) {
  1445. bool is_opaque, has_alpha_channel;
  1446. SDL_DetectPalette(palette, &is_opaque, &has_alpha_channel);
  1447. if (!is_opaque) {
  1448. needAlpha = true;
  1449. }
  1450. }
  1451. surface_colorspace = SDL_GetSurfaceColorspace(surface);
  1452. // Try to have the best pixel format for the texture
  1453. // No alpha, but a colorkey => promote to alpha
  1454. if (!SDL_ISPIXELFORMAT_ALPHA(surface->format) && SDL_SurfaceHasColorKey(surface)) {
  1455. if (surface->format == SDL_PIXELFORMAT_XRGB8888) {
  1456. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1457. if (renderer->texture_formats[i] == SDL_PIXELFORMAT_ARGB8888) {
  1458. format = SDL_PIXELFORMAT_ARGB8888;
  1459. break;
  1460. }
  1461. }
  1462. } else if (surface->format == SDL_PIXELFORMAT_XBGR8888) {
  1463. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1464. if (renderer->texture_formats[i] == SDL_PIXELFORMAT_ABGR8888) {
  1465. format = SDL_PIXELFORMAT_ABGR8888;
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. } else {
  1471. // Exact match would be fine
  1472. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1473. if (renderer->texture_formats[i] == surface->format) {
  1474. format = surface->format;
  1475. break;
  1476. }
  1477. }
  1478. }
  1479. // Look for 10-bit pixel formats if needed
  1480. if (format == SDL_PIXELFORMAT_UNKNOWN && SDL_ISPIXELFORMAT_10BIT(surface->format)) {
  1481. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1482. if (SDL_ISPIXELFORMAT_10BIT(renderer->texture_formats[i])) {
  1483. format = renderer->texture_formats[i];
  1484. break;
  1485. }
  1486. }
  1487. }
  1488. // Look for floating point pixel formats if needed
  1489. if (format == SDL_PIXELFORMAT_UNKNOWN &&
  1490. (SDL_ISPIXELFORMAT_10BIT(surface->format) || SDL_ISPIXELFORMAT_FLOAT(surface->format))) {
  1491. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1492. if (SDL_ISPIXELFORMAT_FLOAT(renderer->texture_formats[i])) {
  1493. format = renderer->texture_formats[i];
  1494. break;
  1495. }
  1496. }
  1497. }
  1498. // Fallback, choose a valid pixel format
  1499. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1500. format = renderer->texture_formats[0];
  1501. for (i = 0; i < renderer->num_texture_formats; ++i) {
  1502. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->texture_formats[i]) &&
  1503. SDL_ISPIXELFORMAT_ALPHA(renderer->texture_formats[i]) == needAlpha) {
  1504. format = renderer->texture_formats[i];
  1505. break;
  1506. }
  1507. }
  1508. }
  1509. if (surface_colorspace == SDL_COLORSPACE_SRGB_LINEAR ||
  1510. SDL_COLORSPACETRANSFER(surface_colorspace) == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  1511. if (SDL_ISPIXELFORMAT_FLOAT(format)) {
  1512. texture_colorspace = SDL_COLORSPACE_SRGB_LINEAR;
  1513. } else if (SDL_ISPIXELFORMAT_10BIT(format)) {
  1514. texture_colorspace = SDL_COLORSPACE_HDR10;
  1515. } else {
  1516. texture_colorspace = SDL_COLORSPACE_SRGB;
  1517. }
  1518. }
  1519. props = SDL_CreateProperties();
  1520. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, texture_colorspace);
  1521. if (surface_colorspace == texture_colorspace) {
  1522. SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_SDR_WHITE_POINT_FLOAT,
  1523. SDL_GetSurfaceSDRWhitePoint(surface, surface_colorspace));
  1524. }
  1525. SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_HDR_HEADROOM_FLOAT,
  1526. SDL_GetSurfaceHDRHeadroom(surface, surface_colorspace));
  1527. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, format);
  1528. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STATIC);
  1529. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, surface->w);
  1530. SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, surface->h);
  1531. texture = SDL_CreateTextureWithProperties(renderer, props);
  1532. SDL_DestroyProperties(props);
  1533. if (!texture) {
  1534. return NULL;
  1535. }
  1536. if (!SDL_UpdateTextureFromSurface(texture, NULL, surface)) {
  1537. SDL_DestroyTexture(texture);
  1538. return NULL;
  1539. }
  1540. return texture;
  1541. }
  1542. SDL_Renderer *SDL_GetRendererFromTexture(SDL_Texture *texture)
  1543. {
  1544. CHECK_TEXTURE_MAGIC(texture, NULL);
  1545. return texture->renderer;
  1546. }
  1547. SDL_PropertiesID SDL_GetTextureProperties(SDL_Texture *texture)
  1548. {
  1549. CHECK_TEXTURE_MAGIC(texture, 0);
  1550. if (texture->props == 0) {
  1551. texture->props = SDL_CreateProperties();
  1552. }
  1553. return texture->props;
  1554. }
  1555. bool SDL_GetTextureSize(SDL_Texture *texture, float *w, float *h)
  1556. {
  1557. if (w) {
  1558. *w = 0;
  1559. }
  1560. if (h) {
  1561. *h = 0;
  1562. }
  1563. CHECK_TEXTURE_MAGIC(texture, false);
  1564. if (w) {
  1565. *w = (float)texture->w;
  1566. }
  1567. if (h) {
  1568. *h = (float)texture->h;
  1569. }
  1570. return true;
  1571. }
  1572. bool SDL_SetTextureColorMod(SDL_Texture *texture, Uint8 r, Uint8 g, Uint8 b)
  1573. {
  1574. const float fR = (float)r / 255.0f;
  1575. const float fG = (float)g / 255.0f;
  1576. const float fB = (float)b / 255.0f;
  1577. return SDL_SetTextureColorModFloat(texture, fR, fG, fB);
  1578. }
  1579. bool SDL_SetTextureColorModFloat(SDL_Texture *texture, float r, float g, float b)
  1580. {
  1581. CHECK_TEXTURE_MAGIC(texture, false);
  1582. texture->color.r = r;
  1583. texture->color.g = g;
  1584. texture->color.b = b;
  1585. if (texture->native) {
  1586. return SDL_SetTextureColorModFloat(texture->native, r, g, b);
  1587. }
  1588. return true;
  1589. }
  1590. bool SDL_GetTextureColorMod(SDL_Texture *texture, Uint8 *r, Uint8 *g, Uint8 *b)
  1591. {
  1592. float fR = 1.0f, fG = 1.0f, fB = 1.0f;
  1593. if (!SDL_GetTextureColorModFloat(texture, &fR, &fG, &fB)) {
  1594. if (r) {
  1595. *r = 255;
  1596. }
  1597. if (g) {
  1598. *g = 255;
  1599. }
  1600. if (b) {
  1601. *b = 255;
  1602. }
  1603. return false;
  1604. }
  1605. if (r) {
  1606. *r = (Uint8)SDL_roundf(SDL_clamp(fR, 0.0f, 1.0f) * 255.0f);
  1607. }
  1608. if (g) {
  1609. *g = (Uint8)SDL_roundf(SDL_clamp(fG, 0.0f, 1.0f) * 255.0f);
  1610. }
  1611. if (b) {
  1612. *b = (Uint8)SDL_roundf(SDL_clamp(fB, 0.0f, 1.0f) * 255.0f);
  1613. }
  1614. return true;
  1615. }
  1616. bool SDL_GetTextureColorModFloat(SDL_Texture *texture, float *r, float *g, float *b)
  1617. {
  1618. SDL_FColor color;
  1619. if (r) {
  1620. *r = 1.0f;
  1621. }
  1622. if (g) {
  1623. *g = 1.0f;
  1624. }
  1625. if (b) {
  1626. *b = 1.0f;
  1627. }
  1628. CHECK_TEXTURE_MAGIC(texture, false);
  1629. color = texture->color;
  1630. if (r) {
  1631. *r = color.r;
  1632. }
  1633. if (g) {
  1634. *g = color.g;
  1635. }
  1636. if (b) {
  1637. *b = color.b;
  1638. }
  1639. return true;
  1640. }
  1641. bool SDL_SetTextureAlphaMod(SDL_Texture *texture, Uint8 alpha)
  1642. {
  1643. const float fA = (float)alpha / 255.0f;
  1644. return SDL_SetTextureAlphaModFloat(texture, fA);
  1645. }
  1646. bool SDL_SetTextureAlphaModFloat(SDL_Texture *texture, float alpha)
  1647. {
  1648. CHECK_TEXTURE_MAGIC(texture, false);
  1649. texture->color.a = alpha;
  1650. if (texture->native) {
  1651. return SDL_SetTextureAlphaModFloat(texture->native, alpha);
  1652. }
  1653. return true;
  1654. }
  1655. bool SDL_GetTextureAlphaMod(SDL_Texture *texture, Uint8 *alpha)
  1656. {
  1657. float fA = 1.0f;
  1658. if (!SDL_GetTextureAlphaModFloat(texture, &fA)) {
  1659. if (alpha) {
  1660. *alpha = 255;
  1661. }
  1662. return false;
  1663. }
  1664. if (alpha) {
  1665. *alpha = (Uint8)SDL_roundf(SDL_clamp(fA, 0.0f, 1.0f) * 255.0f);
  1666. }
  1667. return true;
  1668. }
  1669. bool SDL_GetTextureAlphaModFloat(SDL_Texture *texture, float *alpha)
  1670. {
  1671. if (alpha) {
  1672. *alpha = 1.0f;
  1673. }
  1674. CHECK_TEXTURE_MAGIC(texture, false);
  1675. if (alpha) {
  1676. *alpha = texture->color.a;
  1677. }
  1678. return true;
  1679. }
  1680. bool SDL_SetTextureBlendMode(SDL_Texture *texture, SDL_BlendMode blendMode)
  1681. {
  1682. SDL_Renderer *renderer;
  1683. CHECK_TEXTURE_MAGIC(texture, false);
  1684. if (blendMode == SDL_BLENDMODE_INVALID) {
  1685. return SDL_InvalidParamError("blendMode");
  1686. }
  1687. renderer = texture->renderer;
  1688. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1689. return SDL_Unsupported();
  1690. }
  1691. texture->blendMode = blendMode;
  1692. if (texture->native) {
  1693. return SDL_SetTextureBlendMode(texture->native, blendMode);
  1694. }
  1695. return true;
  1696. }
  1697. bool SDL_GetTextureBlendMode(SDL_Texture *texture, SDL_BlendMode *blendMode)
  1698. {
  1699. if (blendMode) {
  1700. *blendMode = SDL_BLENDMODE_INVALID;
  1701. }
  1702. CHECK_TEXTURE_MAGIC(texture, false);
  1703. if (blendMode) {
  1704. *blendMode = texture->blendMode;
  1705. }
  1706. return true;
  1707. }
  1708. bool SDL_SetTextureScaleMode(SDL_Texture *texture, SDL_ScaleMode scaleMode)
  1709. {
  1710. CHECK_TEXTURE_MAGIC(texture, false);
  1711. switch (scaleMode) {
  1712. case SDL_SCALEMODE_NEAREST:
  1713. case SDL_SCALEMODE_LINEAR:
  1714. case SDL_SCALEMODE_PIXELART:
  1715. break;
  1716. default:
  1717. return SDL_InvalidParamError("scaleMode");
  1718. }
  1719. texture->scaleMode = scaleMode;
  1720. if (texture->native) {
  1721. return SDL_SetTextureScaleMode(texture->native, scaleMode);
  1722. }
  1723. return true;
  1724. }
  1725. bool SDL_GetTextureScaleMode(SDL_Texture *texture, SDL_ScaleMode *scaleMode)
  1726. {
  1727. if (scaleMode) {
  1728. *scaleMode = SDL_SCALEMODE_LINEAR;
  1729. }
  1730. CHECK_TEXTURE_MAGIC(texture, false);
  1731. if (scaleMode) {
  1732. *scaleMode = texture->scaleMode;
  1733. }
  1734. return true;
  1735. }
  1736. #ifdef SDL_HAVE_YUV
  1737. static bool SDL_UpdateTextureYUV(SDL_Texture *texture, const SDL_Rect *rect,
  1738. const void *pixels, int pitch)
  1739. {
  1740. SDL_Texture *native = texture->native;
  1741. SDL_Rect full_rect;
  1742. if (!SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch)) {
  1743. return false;
  1744. }
  1745. full_rect.x = 0;
  1746. full_rect.y = 0;
  1747. full_rect.w = texture->w;
  1748. full_rect.h = texture->h;
  1749. rect = &full_rect;
  1750. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1751. // We can lock the texture and copy to it
  1752. void *native_pixels = NULL;
  1753. int native_pitch = 0;
  1754. if (!SDL_LockTexture(native, rect, &native_pixels, &native_pitch)) {
  1755. return false;
  1756. }
  1757. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1758. rect->w, rect->h, native_pixels, native_pitch);
  1759. SDL_UnlockTexture(native);
  1760. } else {
  1761. // Use a temporary buffer for updating
  1762. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1763. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1764. if (alloclen > 0) {
  1765. void *temp_pixels = SDL_malloc(alloclen);
  1766. if (!temp_pixels) {
  1767. return false;
  1768. }
  1769. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1770. rect->w, rect->h, temp_pixels, temp_pitch);
  1771. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1772. SDL_free(temp_pixels);
  1773. }
  1774. }
  1775. return true;
  1776. }
  1777. #endif // SDL_HAVE_YUV
  1778. static bool SDL_UpdateTextureNative(SDL_Texture *texture, const SDL_Rect *rect,
  1779. const void *pixels, int pitch)
  1780. {
  1781. SDL_Texture *native = texture->native;
  1782. if (!rect->w || !rect->h) {
  1783. return true; // nothing to do.
  1784. }
  1785. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1786. // We can lock the texture and copy to it
  1787. void *native_pixels = NULL;
  1788. int native_pitch = 0;
  1789. if (!SDL_LockTexture(native, rect, &native_pixels, &native_pitch)) {
  1790. return false;
  1791. }
  1792. SDL_ConvertPixelsAndColorspace(rect->w, rect->h,
  1793. texture->format, texture->colorspace, 0, pixels, pitch,
  1794. native->format, native->colorspace, 0, native_pixels, native_pitch);
  1795. SDL_UnlockTexture(native);
  1796. } else {
  1797. // Use a temporary buffer for updating
  1798. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1799. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1800. if (alloclen > 0) {
  1801. void *temp_pixels = SDL_malloc(alloclen);
  1802. if (!temp_pixels) {
  1803. return false;
  1804. }
  1805. SDL_ConvertPixelsAndColorspace(rect->w, rect->h,
  1806. texture->format, texture->colorspace, 0, pixels, pitch,
  1807. native->format, native->colorspace, 0, temp_pixels, temp_pitch);
  1808. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1809. SDL_free(temp_pixels);
  1810. }
  1811. }
  1812. return true;
  1813. }
  1814. bool SDL_UpdateTexture(SDL_Texture *texture, const SDL_Rect *rect, const void *pixels, int pitch)
  1815. {
  1816. SDL_Rect real_rect;
  1817. CHECK_TEXTURE_MAGIC(texture, false);
  1818. if (!pixels) {
  1819. return SDL_InvalidParamError("pixels");
  1820. }
  1821. if (!pitch) {
  1822. return SDL_InvalidParamError("pitch");
  1823. }
  1824. real_rect.x = 0;
  1825. real_rect.y = 0;
  1826. real_rect.w = texture->w;
  1827. real_rect.h = texture->h;
  1828. if (rect) {
  1829. if (!SDL_GetRectIntersection(rect, &real_rect, &real_rect)) {
  1830. return true;
  1831. }
  1832. }
  1833. if (real_rect.w == 0 || real_rect.h == 0) {
  1834. return true; // nothing to do.
  1835. #ifdef SDL_HAVE_YUV
  1836. } else if (texture->yuv) {
  1837. return SDL_UpdateTextureYUV(texture, &real_rect, pixels, pitch);
  1838. #endif
  1839. } else if (texture->native) {
  1840. return SDL_UpdateTextureNative(texture, &real_rect, pixels, pitch);
  1841. } else {
  1842. SDL_Renderer *renderer = texture->renderer;
  1843. if (!FlushRenderCommandsIfTextureNeeded(texture)) {
  1844. return false;
  1845. }
  1846. return renderer->UpdateTexture(renderer, texture, &real_rect, pixels, pitch);
  1847. }
  1848. }
  1849. #ifdef SDL_HAVE_YUV
  1850. static bool SDL_UpdateTextureYUVPlanar(SDL_Texture *texture, const SDL_Rect *rect,
  1851. const Uint8 *Yplane, int Ypitch,
  1852. const Uint8 *Uplane, int Upitch,
  1853. const Uint8 *Vplane, int Vpitch)
  1854. {
  1855. SDL_Texture *native = texture->native;
  1856. SDL_Rect full_rect;
  1857. if (!SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch)) {
  1858. return false;
  1859. }
  1860. full_rect.x = 0;
  1861. full_rect.y = 0;
  1862. full_rect.w = texture->w;
  1863. full_rect.h = texture->h;
  1864. rect = &full_rect;
  1865. if (!rect->w || !rect->h) {
  1866. return true; // nothing to do.
  1867. }
  1868. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1869. // We can lock the texture and copy to it
  1870. void *native_pixels = NULL;
  1871. int native_pitch = 0;
  1872. if (!SDL_LockTexture(native, rect, &native_pixels, &native_pitch)) {
  1873. return false;
  1874. }
  1875. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1876. rect->w, rect->h, native_pixels, native_pitch);
  1877. SDL_UnlockTexture(native);
  1878. } else {
  1879. // Use a temporary buffer for updating
  1880. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1881. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1882. if (alloclen > 0) {
  1883. void *temp_pixels = SDL_malloc(alloclen);
  1884. if (!temp_pixels) {
  1885. return false;
  1886. }
  1887. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1888. rect->w, rect->h, temp_pixels, temp_pitch);
  1889. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1890. SDL_free(temp_pixels);
  1891. }
  1892. }
  1893. return true;
  1894. }
  1895. static bool SDL_UpdateTextureNVPlanar(SDL_Texture *texture, const SDL_Rect *rect,
  1896. const Uint8 *Yplane, int Ypitch,
  1897. const Uint8 *UVplane, int UVpitch)
  1898. {
  1899. SDL_Texture *native = texture->native;
  1900. SDL_Rect full_rect;
  1901. if (!SDL_SW_UpdateNVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, UVplane, UVpitch)) {
  1902. return false;
  1903. }
  1904. full_rect.x = 0;
  1905. full_rect.y = 0;
  1906. full_rect.w = texture->w;
  1907. full_rect.h = texture->h;
  1908. rect = &full_rect;
  1909. if (!rect->w || !rect->h) {
  1910. return true; // nothing to do.
  1911. }
  1912. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1913. // We can lock the texture and copy to it
  1914. void *native_pixels = NULL;
  1915. int native_pitch = 0;
  1916. if (!SDL_LockTexture(native, rect, &native_pixels, &native_pitch)) {
  1917. return false;
  1918. }
  1919. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1920. rect->w, rect->h, native_pixels, native_pitch);
  1921. SDL_UnlockTexture(native);
  1922. } else {
  1923. // Use a temporary buffer for updating
  1924. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1925. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1926. if (alloclen > 0) {
  1927. void *temp_pixels = SDL_malloc(alloclen);
  1928. if (!temp_pixels) {
  1929. return false;
  1930. }
  1931. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1932. rect->w, rect->h, temp_pixels, temp_pitch);
  1933. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1934. SDL_free(temp_pixels);
  1935. }
  1936. }
  1937. return true;
  1938. }
  1939. #endif // SDL_HAVE_YUV
  1940. bool SDL_UpdateYUVTexture(SDL_Texture *texture, const SDL_Rect *rect,
  1941. const Uint8 *Yplane, int Ypitch,
  1942. const Uint8 *Uplane, int Upitch,
  1943. const Uint8 *Vplane, int Vpitch)
  1944. {
  1945. #ifdef SDL_HAVE_YUV
  1946. SDL_Renderer *renderer;
  1947. SDL_Rect real_rect;
  1948. CHECK_TEXTURE_MAGIC(texture, false);
  1949. if (!Yplane) {
  1950. return SDL_InvalidParamError("Yplane");
  1951. }
  1952. if (!Ypitch) {
  1953. return SDL_InvalidParamError("Ypitch");
  1954. }
  1955. if (!Uplane) {
  1956. return SDL_InvalidParamError("Uplane");
  1957. }
  1958. if (!Upitch) {
  1959. return SDL_InvalidParamError("Upitch");
  1960. }
  1961. if (!Vplane) {
  1962. return SDL_InvalidParamError("Vplane");
  1963. }
  1964. if (!Vpitch) {
  1965. return SDL_InvalidParamError("Vpitch");
  1966. }
  1967. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  1968. texture->format != SDL_PIXELFORMAT_IYUV) {
  1969. return SDL_SetError("Texture format must by YV12 or IYUV");
  1970. }
  1971. real_rect.x = 0;
  1972. real_rect.y = 0;
  1973. real_rect.w = texture->w;
  1974. real_rect.h = texture->h;
  1975. if (rect) {
  1976. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  1977. }
  1978. if (real_rect.w == 0 || real_rect.h == 0) {
  1979. return true; // nothing to do.
  1980. }
  1981. if (texture->yuv) {
  1982. return SDL_UpdateTextureYUVPlanar(texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1983. } else {
  1984. SDL_assert(!texture->native);
  1985. renderer = texture->renderer;
  1986. SDL_assert(renderer->UpdateTextureYUV);
  1987. if (renderer->UpdateTextureYUV) {
  1988. if (!FlushRenderCommandsIfTextureNeeded(texture)) {
  1989. return false;
  1990. }
  1991. return renderer->UpdateTextureYUV(renderer, texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1992. } else {
  1993. return SDL_Unsupported();
  1994. }
  1995. }
  1996. #else
  1997. return false;
  1998. #endif
  1999. }
  2000. bool SDL_UpdateNVTexture(SDL_Texture *texture, const SDL_Rect *rect,
  2001. const Uint8 *Yplane, int Ypitch,
  2002. const Uint8 *UVplane, int UVpitch)
  2003. {
  2004. #ifdef SDL_HAVE_YUV
  2005. SDL_Renderer *renderer;
  2006. SDL_Rect real_rect;
  2007. CHECK_TEXTURE_MAGIC(texture, false);
  2008. if (!Yplane) {
  2009. return SDL_InvalidParamError("Yplane");
  2010. }
  2011. if (!Ypitch) {
  2012. return SDL_InvalidParamError("Ypitch");
  2013. }
  2014. if (!UVplane) {
  2015. return SDL_InvalidParamError("UVplane");
  2016. }
  2017. if (!UVpitch) {
  2018. return SDL_InvalidParamError("UVpitch");
  2019. }
  2020. if (texture->format != SDL_PIXELFORMAT_NV12 &&
  2021. texture->format != SDL_PIXELFORMAT_NV21) {
  2022. return SDL_SetError("Texture format must by NV12 or NV21");
  2023. }
  2024. real_rect.x = 0;
  2025. real_rect.y = 0;
  2026. real_rect.w = texture->w;
  2027. real_rect.h = texture->h;
  2028. if (rect) {
  2029. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  2030. }
  2031. if (real_rect.w == 0 || real_rect.h == 0) {
  2032. return true; // nothing to do.
  2033. }
  2034. if (texture->yuv) {
  2035. return SDL_UpdateTextureNVPlanar(texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  2036. } else {
  2037. SDL_assert(!texture->native);
  2038. renderer = texture->renderer;
  2039. SDL_assert(renderer->UpdateTextureNV);
  2040. if (renderer->UpdateTextureNV) {
  2041. if (!FlushRenderCommandsIfTextureNeeded(texture)) {
  2042. return false;
  2043. }
  2044. return renderer->UpdateTextureNV(renderer, texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  2045. } else {
  2046. return SDL_Unsupported();
  2047. }
  2048. }
  2049. #else
  2050. return false;
  2051. #endif
  2052. }
  2053. #ifdef SDL_HAVE_YUV
  2054. static bool SDL_LockTextureYUV(SDL_Texture *texture, const SDL_Rect *rect,
  2055. void **pixels, int *pitch)
  2056. {
  2057. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  2058. }
  2059. #endif // SDL_HAVE_YUV
  2060. static bool SDL_LockTextureNative(SDL_Texture *texture, const SDL_Rect *rect,
  2061. void **pixels, int *pitch)
  2062. {
  2063. texture->locked_rect = *rect;
  2064. *pixels = (void *)((Uint8 *)texture->pixels +
  2065. rect->y * texture->pitch +
  2066. rect->x * SDL_BYTESPERPIXEL(texture->format));
  2067. *pitch = texture->pitch;
  2068. return true;
  2069. }
  2070. bool SDL_LockTexture(SDL_Texture *texture, const SDL_Rect *rect, void **pixels, int *pitch)
  2071. {
  2072. SDL_Rect full_rect;
  2073. CHECK_TEXTURE_MAGIC(texture, false);
  2074. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2075. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2076. }
  2077. if (!rect) {
  2078. full_rect.x = 0;
  2079. full_rect.y = 0;
  2080. full_rect.w = texture->w;
  2081. full_rect.h = texture->h;
  2082. rect = &full_rect;
  2083. }
  2084. #ifdef SDL_HAVE_YUV
  2085. if (texture->yuv) {
  2086. if (!FlushRenderCommandsIfTextureNeeded(texture)) {
  2087. return false;
  2088. }
  2089. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  2090. } else
  2091. #endif
  2092. if (texture->native) {
  2093. // Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then.
  2094. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  2095. } else {
  2096. SDL_Renderer *renderer = texture->renderer;
  2097. if (!FlushRenderCommandsIfTextureNeeded(texture)) {
  2098. return false;
  2099. }
  2100. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  2101. }
  2102. }
  2103. bool SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect, SDL_Surface **surface)
  2104. {
  2105. SDL_Rect real_rect;
  2106. void *pixels = NULL;
  2107. int pitch = 0; // fix static analysis
  2108. if (!texture || !surface) {
  2109. return false;
  2110. }
  2111. real_rect.x = 0;
  2112. real_rect.y = 0;
  2113. real_rect.w = texture->w;
  2114. real_rect.h = texture->h;
  2115. if (rect) {
  2116. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  2117. }
  2118. if (!SDL_LockTexture(texture, &real_rect, &pixels, &pitch)) {
  2119. return false;
  2120. }
  2121. texture->locked_surface = SDL_CreateSurfaceFrom(real_rect.w, real_rect.h, texture->format, pixels, pitch);
  2122. if (!texture->locked_surface) {
  2123. SDL_UnlockTexture(texture);
  2124. return false;
  2125. }
  2126. *surface = texture->locked_surface;
  2127. return true;
  2128. }
  2129. #ifdef SDL_HAVE_YUV
  2130. static void SDL_UnlockTextureYUV(SDL_Texture *texture)
  2131. {
  2132. SDL_Texture *native = texture->native;
  2133. void *native_pixels = NULL;
  2134. int native_pitch = 0;
  2135. SDL_Rect rect;
  2136. rect.x = 0;
  2137. rect.y = 0;
  2138. rect.w = texture->w;
  2139. rect.h = texture->h;
  2140. if (!SDL_LockTexture(native, &rect, &native_pixels, &native_pitch)) {
  2141. return;
  2142. }
  2143. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  2144. rect.w, rect.h, native_pixels, native_pitch);
  2145. SDL_UnlockTexture(native);
  2146. }
  2147. #endif // SDL_HAVE_YUV
  2148. static void SDL_UnlockTextureNative(SDL_Texture *texture)
  2149. {
  2150. SDL_Texture *native = texture->native;
  2151. void *native_pixels = NULL;
  2152. int native_pitch = 0;
  2153. const SDL_Rect *rect = &texture->locked_rect;
  2154. const void *pixels = (void *)((Uint8 *)texture->pixels +
  2155. rect->y * texture->pitch +
  2156. rect->x * SDL_BYTESPERPIXEL(texture->format));
  2157. int pitch = texture->pitch;
  2158. if (!SDL_LockTexture(native, rect, &native_pixels, &native_pitch)) {
  2159. return;
  2160. }
  2161. SDL_ConvertPixels(rect->w, rect->h,
  2162. texture->format, pixels, pitch,
  2163. native->format, native_pixels, native_pitch);
  2164. SDL_UnlockTexture(native);
  2165. }
  2166. void SDL_UnlockTexture(SDL_Texture *texture)
  2167. {
  2168. CHECK_TEXTURE_MAGIC(texture,);
  2169. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2170. return;
  2171. }
  2172. #ifdef SDL_HAVE_YUV
  2173. if (texture->yuv) {
  2174. SDL_UnlockTextureYUV(texture);
  2175. } else
  2176. #endif
  2177. if (texture->native) {
  2178. SDL_UnlockTextureNative(texture);
  2179. } else {
  2180. SDL_Renderer *renderer = texture->renderer;
  2181. renderer->UnlockTexture(renderer, texture);
  2182. }
  2183. SDL_DestroySurface(texture->locked_surface);
  2184. texture->locked_surface = NULL;
  2185. }
  2186. bool SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  2187. {
  2188. // texture == NULL is valid and means reset the target to the window
  2189. if (texture) {
  2190. CHECK_TEXTURE_MAGIC(texture, false);
  2191. if (renderer != texture->renderer) {
  2192. return SDL_SetError("Texture was not created with this renderer");
  2193. }
  2194. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  2195. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  2196. }
  2197. if (texture->native) {
  2198. // Always render to the native texture
  2199. texture = texture->native;
  2200. }
  2201. }
  2202. if (texture == renderer->target) {
  2203. // Nothing to do!
  2204. return true;
  2205. }
  2206. FlushRenderCommands(renderer); // time to send everything to the GPU!
  2207. SDL_LockMutex(renderer->target_mutex);
  2208. renderer->target = texture;
  2209. if (texture) {
  2210. renderer->view = &texture->view;
  2211. } else {
  2212. renderer->view = &renderer->main_view;
  2213. }
  2214. UpdateColorScale(renderer);
  2215. if (!renderer->SetRenderTarget(renderer, texture)) {
  2216. SDL_UnlockMutex(renderer->target_mutex);
  2217. return false;
  2218. }
  2219. SDL_UnlockMutex(renderer->target_mutex);
  2220. if (!QueueCmdSetViewport(renderer)) {
  2221. return false;
  2222. }
  2223. if (!QueueCmdSetClipRect(renderer)) {
  2224. return false;
  2225. }
  2226. // All set!
  2227. return true;
  2228. }
  2229. SDL_Texture *SDL_GetRenderTarget(SDL_Renderer *renderer)
  2230. {
  2231. CHECK_RENDERER_MAGIC(renderer, NULL);
  2232. if (!renderer->target) {
  2233. return NULL;
  2234. }
  2235. return (SDL_Texture *) SDL_GetPointerProperty(SDL_GetTextureProperties(renderer->target), SDL_PROP_TEXTURE_PARENT_POINTER, renderer->target);
  2236. }
  2237. static void UpdateLogicalPresentation(SDL_Renderer *renderer)
  2238. {
  2239. SDL_RenderViewState *view = renderer->view;
  2240. const bool is_main_view = (view == &renderer->main_view);
  2241. const float logical_w = view->logical_w;
  2242. const float logical_h = view->logical_h;
  2243. int iwidth, iheight;
  2244. if (renderer->target) {
  2245. iwidth = (int)renderer->target->w;
  2246. iheight = (int)renderer->target->h;
  2247. } else {
  2248. SDL_GetRenderOutputSize(renderer, &iwidth, &iheight);
  2249. }
  2250. view->logical_src_rect.x = 0.0f;
  2251. view->logical_src_rect.y = 0.0f;
  2252. view->logical_src_rect.w = logical_w;
  2253. view->logical_src_rect.h = logical_h;
  2254. if (view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED) {
  2255. view->logical_dst_rect.x = 0.0f;
  2256. view->logical_dst_rect.y = 0.0f;
  2257. view->logical_dst_rect.w = iwidth;
  2258. view->logical_dst_rect.h = iheight;
  2259. view->logical_offset.x = view->logical_offset.y = 0.0f;
  2260. view->logical_scale.x = view->logical_scale.y = 1.0f;
  2261. view->current_scale.x = view->scale.x; // skip the multiplications against 1.0f.
  2262. view->current_scale.y = view->scale.y;
  2263. } else {
  2264. const float output_w = (float)iwidth;
  2265. const float output_h = (float)iheight;
  2266. const float want_aspect = logical_w / logical_h;
  2267. const float real_aspect = output_w / output_h;
  2268. if ((logical_w <= 0.0f) || (logical_h <= 0.0f)) {
  2269. view->logical_dst_rect.x = 0.0f;
  2270. view->logical_dst_rect.y = 0.0f;
  2271. view->logical_dst_rect.w = output_w;
  2272. view->logical_dst_rect.h = output_h;
  2273. } else if (view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
  2274. float scale;
  2275. if (want_aspect > real_aspect) {
  2276. scale = (float)((int)output_w / (int)logical_w); // This an integer division!
  2277. } else {
  2278. scale = (float)((int)output_h / (int)logical_h); // This an integer division!
  2279. }
  2280. if (scale < 1.0f) {
  2281. scale = 1.0f;
  2282. }
  2283. view->logical_dst_rect.w = SDL_floorf(logical_w * scale);
  2284. view->logical_dst_rect.x = (output_w - view->logical_dst_rect.w) / 2.0f;
  2285. view->logical_dst_rect.h = SDL_floorf(logical_h * scale);
  2286. view->logical_dst_rect.y = (output_h - view->logical_dst_rect.h) / 2.0f;
  2287. } else if (view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_STRETCH || SDL_fabsf(want_aspect - real_aspect) < 0.0001f) {
  2288. view->logical_dst_rect.x = 0.0f;
  2289. view->logical_dst_rect.y = 0.0f;
  2290. view->logical_dst_rect.w = output_w;
  2291. view->logical_dst_rect.h = output_h;
  2292. } else if (want_aspect > real_aspect) {
  2293. if (view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
  2294. // We want a wider aspect ratio than is available - letterbox it
  2295. const float scale = output_w / logical_w;
  2296. view->logical_dst_rect.x = 0.0f;
  2297. view->logical_dst_rect.w = output_w;
  2298. view->logical_dst_rect.h = SDL_floorf(logical_h * scale);
  2299. view->logical_dst_rect.y = (output_h - view->logical_dst_rect.h) / 2.0f;
  2300. } else { // view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_OVERSCAN
  2301. /* We want a wider aspect ratio than is available -
  2302. zoom so logical height matches the real height
  2303. and the width will grow off the screen
  2304. */
  2305. const float scale = output_h / logical_h;
  2306. view->logical_dst_rect.y = 0.0f;
  2307. view->logical_dst_rect.h = output_h;
  2308. view->logical_dst_rect.w = SDL_floorf(logical_w * scale);
  2309. view->logical_dst_rect.x = (output_w - view->logical_dst_rect.w) / 2.0f;
  2310. }
  2311. } else {
  2312. if (view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
  2313. // We want a narrower aspect ratio than is available - use side-bars
  2314. const float scale = output_h / logical_h;
  2315. view->logical_dst_rect.y = 0.0f;
  2316. view->logical_dst_rect.h = output_h;
  2317. view->logical_dst_rect.w = SDL_floorf(logical_w * scale);
  2318. view->logical_dst_rect.x = (output_w - view->logical_dst_rect.w) / 2.0f;
  2319. } else { // view->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_OVERSCAN
  2320. /* We want a narrower aspect ratio than is available -
  2321. zoom so logical width matches the real width
  2322. and the height will grow off the screen
  2323. */
  2324. const float scale = output_w / logical_w;
  2325. view->logical_dst_rect.x = 0.0f;
  2326. view->logical_dst_rect.w = output_w;
  2327. view->logical_dst_rect.h = SDL_floorf(logical_h * scale);
  2328. view->logical_dst_rect.y = (output_h - view->logical_dst_rect.h) / 2.0f;
  2329. }
  2330. }
  2331. view->logical_scale.x = (logical_w > 0.0f) ? view->logical_dst_rect.w / logical_w : 0.0f;
  2332. view->logical_scale.y = (logical_h > 0.0f) ? view->logical_dst_rect.h / logical_h : 0.0f;
  2333. view->current_scale.x = view->scale.x * view->logical_scale.x;
  2334. view->current_scale.y = view->scale.y * view->logical_scale.y;
  2335. view->logical_offset.x = view->logical_dst_rect.x;
  2336. view->logical_offset.y = view->logical_dst_rect.y;
  2337. }
  2338. if (is_main_view) {
  2339. // This makes sure the dpi_scale is right. It also sets pixel_w and pixel_h, but we're going to change them directly below here.
  2340. UpdateMainViewDimensions(renderer);
  2341. }
  2342. view->pixel_w = (int) view->logical_dst_rect.w;
  2343. view->pixel_h = (int) view->logical_dst_rect.h;
  2344. UpdatePixelViewport(renderer, view);
  2345. UpdatePixelClipRect(renderer, view);
  2346. }
  2347. bool SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode)
  2348. {
  2349. CHECK_RENDERER_MAGIC(renderer, false);
  2350. SDL_RenderViewState *view = renderer->view;
  2351. view->logical_presentation_mode = mode;
  2352. view->logical_w = w;
  2353. view->logical_h = h;
  2354. UpdateLogicalPresentation(renderer);
  2355. return true;
  2356. }
  2357. bool SDL_GetRenderLogicalPresentation(SDL_Renderer *renderer, int *w, int *h, SDL_RendererLogicalPresentation *mode)
  2358. {
  2359. #define SETVAL(ptr, val) if (ptr) { *ptr = val; }
  2360. SETVAL(w, 0);
  2361. SETVAL(h, 0);
  2362. SETVAL(mode, SDL_LOGICAL_PRESENTATION_DISABLED);
  2363. CHECK_RENDERER_MAGIC(renderer, false);
  2364. const SDL_RenderViewState *view = renderer->view;
  2365. SETVAL(w, view->logical_w);
  2366. SETVAL(h, view->logical_h);
  2367. SETVAL(mode, view->logical_presentation_mode);
  2368. #undef SETVAL
  2369. return true;
  2370. }
  2371. bool SDL_GetRenderLogicalPresentationRect(SDL_Renderer *renderer, SDL_FRect *rect)
  2372. {
  2373. if (rect) {
  2374. SDL_zerop(rect);
  2375. }
  2376. CHECK_RENDERER_MAGIC(renderer, false);
  2377. if (rect) {
  2378. SDL_copyp(rect, &renderer->view->logical_dst_rect);
  2379. }
  2380. return true;
  2381. }
  2382. static void SDL_RenderLogicalBorders(SDL_Renderer *renderer, const SDL_FRect *dst)
  2383. {
  2384. const SDL_RenderViewState *view = renderer->view;
  2385. if (dst->x > 0.0f || dst->y > 0.0f) {
  2386. SDL_BlendMode saved_blend_mode = renderer->blendMode;
  2387. SDL_FColor saved_color = renderer->color;
  2388. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  2389. SDL_SetRenderDrawColorFloat(renderer, 0.0f, 0.0f, 0.0f, 1.0f);
  2390. if (dst->x > 0.0f) {
  2391. SDL_FRect rect;
  2392. rect.x = 0.0f;
  2393. rect.y = 0.0f;
  2394. rect.w = dst->x;
  2395. rect.h = (float)view->pixel_h;
  2396. SDL_RenderFillRect(renderer, &rect);
  2397. rect.x = dst->x + dst->w;
  2398. rect.w = (float)view->pixel_w - rect.x;
  2399. SDL_RenderFillRect(renderer, &rect);
  2400. }
  2401. if (dst->y > 0.0f) {
  2402. SDL_FRect rect;
  2403. rect.x = 0.0f;
  2404. rect.y = 0.0f;
  2405. rect.w = (float)view->pixel_w;
  2406. rect.h = dst->y;
  2407. SDL_RenderFillRect(renderer, &rect);
  2408. rect.y = dst->y + dst->h;
  2409. rect.h = (float)view->pixel_h - rect.y;
  2410. SDL_RenderFillRect(renderer, &rect);
  2411. }
  2412. SDL_SetRenderDrawBlendMode(renderer, saved_blend_mode);
  2413. SDL_SetRenderDrawColorFloat(renderer, saved_color.r, saved_color.g, saved_color.b, saved_color.a);
  2414. }
  2415. }
  2416. static void SDL_RenderLogicalPresentation(SDL_Renderer *renderer)
  2417. {
  2418. SDL_assert(renderer->view == &renderer->main_view);
  2419. SDL_RenderViewState *view = &renderer->main_view;
  2420. const SDL_RendererLogicalPresentation mode = view->logical_presentation_mode;
  2421. if (mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
  2422. // save off some state we're going to trample.
  2423. const int logical_w = view->logical_w;
  2424. const int logical_h = view->logical_h;
  2425. const float scale_x = view->scale.x;
  2426. const float scale_y = view->scale.y;
  2427. const bool clipping_enabled = view->clipping_enabled;
  2428. SDL_Rect orig_viewport, orig_cliprect;
  2429. const SDL_FRect logical_dst_rect = view->logical_dst_rect;
  2430. SDL_copyp(&orig_viewport, &view->viewport);
  2431. if (clipping_enabled) {
  2432. SDL_copyp(&orig_cliprect, &view->clip_rect);
  2433. }
  2434. // trample some state.
  2435. SDL_SetRenderLogicalPresentation(renderer, logical_w, logical_h, SDL_LOGICAL_PRESENTATION_DISABLED);
  2436. SDL_SetRenderViewport(renderer, NULL);
  2437. if (clipping_enabled) {
  2438. SDL_SetRenderClipRect(renderer, NULL);
  2439. }
  2440. SDL_SetRenderScale(renderer, 1.0f, 1.0f);
  2441. // draw the borders.
  2442. SDL_RenderLogicalBorders(renderer, &logical_dst_rect);
  2443. // now set everything back.
  2444. view->logical_presentation_mode = mode;
  2445. SDL_SetRenderViewport(renderer, &orig_viewport);
  2446. if (clipping_enabled) {
  2447. SDL_SetRenderClipRect(renderer, &orig_cliprect);
  2448. }
  2449. SDL_SetRenderScale(renderer, scale_x, scale_y);
  2450. SDL_SetRenderLogicalPresentation(renderer, logical_w, logical_h, mode);
  2451. }
  2452. }
  2453. static bool SDL_RenderVectorFromWindow(SDL_Renderer *renderer, float window_dx, float window_dy, float *dx, float *dy)
  2454. {
  2455. // Convert from window coordinates to pixels within the window
  2456. window_dx *= renderer->dpi_scale.x;
  2457. window_dy *= renderer->dpi_scale.y;
  2458. // Convert from pixels within the window to pixels within the view
  2459. const SDL_RenderViewState *view = &renderer->main_view;
  2460. if (view->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
  2461. const SDL_FRect *src = &view->logical_src_rect;
  2462. const SDL_FRect *dst = &view->logical_dst_rect;
  2463. window_dx = (window_dx * src->w) / dst->w;
  2464. window_dy = (window_dy * src->h) / dst->h;
  2465. }
  2466. window_dx /= view->scale.x;
  2467. window_dy /= view->scale.y;
  2468. *dx = window_dx;
  2469. *dy = window_dy;
  2470. return true;
  2471. }
  2472. bool SDL_RenderCoordinatesFromWindow(SDL_Renderer *renderer, float window_x, float window_y, float *x, float *y)
  2473. {
  2474. float render_x, render_y;
  2475. CHECK_RENDERER_MAGIC(renderer, false);
  2476. // Convert from window coordinates to pixels within the window
  2477. render_x = window_x * renderer->dpi_scale.x;
  2478. render_y = window_y * renderer->dpi_scale.y;
  2479. // Convert from pixels within the window to pixels within the view
  2480. const SDL_RenderViewState *view = &renderer->main_view;
  2481. if (view->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
  2482. const SDL_FRect *src = &view->logical_src_rect;
  2483. const SDL_FRect *dst = &view->logical_dst_rect;
  2484. render_x = ((render_x - dst->x) * src->w) / dst->w;
  2485. render_y = ((render_y - dst->y) * src->h) / dst->h;
  2486. }
  2487. render_x = (render_x / view->scale.x) - view->viewport.x;
  2488. render_y = (render_y / view->scale.y) - view->viewport.y;
  2489. if (x) {
  2490. *x = render_x;
  2491. }
  2492. if (y) {
  2493. *y = render_y;
  2494. }
  2495. return true;
  2496. }
  2497. bool SDL_RenderCoordinatesToWindow(SDL_Renderer *renderer, float x, float y, float *window_x, float *window_y)
  2498. {
  2499. CHECK_RENDERER_MAGIC(renderer, false);
  2500. const SDL_RenderViewState *view = &renderer->main_view;
  2501. x = (view->viewport.x + x) * view->scale.x;
  2502. y = (view->viewport.y + y) * view->scale.y;
  2503. // Convert from render coordinates to pixels within the window
  2504. if (view->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
  2505. const SDL_FRect *src = &view->logical_src_rect;
  2506. const SDL_FRect *dst = &view->logical_dst_rect;
  2507. x = dst->x + ((x * dst->w) / src->w);
  2508. y = dst->y + ((y * dst->h) / src->h);
  2509. }
  2510. // Convert from pixels within the window to window coordinates
  2511. x /= renderer->dpi_scale.x;
  2512. y /= renderer->dpi_scale.y;
  2513. if (window_x) {
  2514. *window_x = x;
  2515. }
  2516. if (window_y) {
  2517. *window_y = y;
  2518. }
  2519. return true;
  2520. }
  2521. bool SDL_ConvertEventToRenderCoordinates(SDL_Renderer *renderer, SDL_Event *event)
  2522. {
  2523. CHECK_RENDERER_MAGIC(renderer, false);
  2524. if (event->type == SDL_EVENT_MOUSE_MOTION) {
  2525. SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
  2526. if (window == renderer->window) {
  2527. SDL_RenderCoordinatesFromWindow(renderer, event->motion.x, event->motion.y, &event->motion.x, &event->motion.y);
  2528. SDL_RenderVectorFromWindow(renderer, event->motion.xrel, event->motion.yrel, &event->motion.xrel, &event->motion.yrel);
  2529. }
  2530. } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
  2531. event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
  2532. SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
  2533. if (window == renderer->window) {
  2534. SDL_RenderCoordinatesFromWindow(renderer, event->button.x, event->button.y, &event->button.x, &event->button.y);
  2535. }
  2536. } else if (event->type == SDL_EVENT_MOUSE_WHEEL) {
  2537. SDL_Window *window = SDL_GetWindowFromID(event->wheel.windowID);
  2538. if (window == renderer->window) {
  2539. SDL_RenderCoordinatesFromWindow(renderer, event->wheel.mouse_x,
  2540. event->wheel.mouse_y,
  2541. &event->wheel.mouse_x,
  2542. &event->wheel.mouse_y);
  2543. }
  2544. } else if (event->type == SDL_EVENT_FINGER_DOWN ||
  2545. event->type == SDL_EVENT_FINGER_UP ||
  2546. event->type == SDL_EVENT_FINGER_CANCELED ||
  2547. event->type == SDL_EVENT_FINGER_MOTION) {
  2548. // FIXME: Are these events guaranteed to be window relative?
  2549. if (renderer->window) {
  2550. int w, h;
  2551. if (!SDL_GetWindowSize(renderer->window, &w, &h)) {
  2552. return false;
  2553. }
  2554. SDL_RenderCoordinatesFromWindow(renderer, event->tfinger.x * w, event->tfinger.y * h, &event->tfinger.x, &event->tfinger.y);
  2555. SDL_RenderVectorFromWindow(renderer, event->tfinger.dx * w, event->tfinger.dy * h, &event->tfinger.dx, &event->tfinger.dy);
  2556. }
  2557. } else if (event->type == SDL_EVENT_PEN_MOTION) {
  2558. SDL_Window *window = SDL_GetWindowFromID(event->pmotion.windowID);
  2559. if (window == renderer->window) {
  2560. SDL_RenderCoordinatesFromWindow(renderer, event->pmotion.x, event->pmotion.y, &event->pmotion.x, &event->pmotion.y);
  2561. }
  2562. } else if ((event->type == SDL_EVENT_PEN_DOWN) || (event->type == SDL_EVENT_PEN_UP)) {
  2563. SDL_Window *window = SDL_GetWindowFromID(event->ptouch.windowID);
  2564. if (window == renderer->window) {
  2565. SDL_RenderCoordinatesFromWindow(renderer, event->ptouch.x, event->ptouch.y, &event->ptouch.x, &event->ptouch.y);
  2566. }
  2567. } else if ((event->type == SDL_EVENT_PEN_BUTTON_DOWN) || (event->type == SDL_EVENT_PEN_BUTTON_UP)) {
  2568. SDL_Window *window = SDL_GetWindowFromID(event->pbutton.windowID);
  2569. if (window == renderer->window) {
  2570. SDL_RenderCoordinatesFromWindow(renderer, event->pbutton.x, event->pbutton.y, &event->pbutton.x, &event->pbutton.y);
  2571. }
  2572. } else if (event->type == SDL_EVENT_PEN_AXIS) {
  2573. SDL_Window *window = SDL_GetWindowFromID(event->paxis.windowID);
  2574. if (window == renderer->window) {
  2575. SDL_RenderCoordinatesFromWindow(renderer, event->paxis.x, event->paxis.y, &event->paxis.x, &event->paxis.y);
  2576. }
  2577. } else if (event->type == SDL_EVENT_DROP_POSITION ||
  2578. event->type == SDL_EVENT_DROP_FILE ||
  2579. event->type == SDL_EVENT_DROP_TEXT ||
  2580. event->type == SDL_EVENT_DROP_COMPLETE) {
  2581. SDL_Window *window = SDL_GetWindowFromID(event->drop.windowID);
  2582. if (window == renderer->window) {
  2583. SDL_RenderCoordinatesFromWindow(renderer, event->drop.x, event->drop.y, &event->drop.x, &event->drop.y);
  2584. }
  2585. }
  2586. return true;
  2587. }
  2588. bool SDL_SetRenderViewport(SDL_Renderer *renderer, const SDL_Rect *rect)
  2589. {
  2590. CHECK_RENDERER_MAGIC(renderer, false);
  2591. SDL_RenderViewState *view = renderer->view;
  2592. if (rect) {
  2593. if ((rect->w < 0) || (rect->h < 0)) {
  2594. return SDL_SetError("rect has a negative size");
  2595. }
  2596. SDL_copyp(&view->viewport, rect);
  2597. } else {
  2598. view->viewport.x = view->viewport.y = 0;
  2599. view->viewport.w = view->viewport.h = -1;
  2600. }
  2601. UpdatePixelViewport(renderer, view);
  2602. return QueueCmdSetViewport(renderer);
  2603. }
  2604. bool SDL_GetRenderViewport(SDL_Renderer *renderer, SDL_Rect *rect)
  2605. {
  2606. if (rect) {
  2607. SDL_zerop(rect);
  2608. }
  2609. CHECK_RENDERER_MAGIC(renderer, false);
  2610. if (rect) {
  2611. const SDL_RenderViewState *view = renderer->view;
  2612. rect->x = view->viewport.x;
  2613. rect->y = view->viewport.y;
  2614. if (view->viewport.w >= 0) {
  2615. rect->w = view->viewport.w;
  2616. } else {
  2617. rect->w = (int)SDL_ceilf(view->pixel_w / view->current_scale.x);
  2618. }
  2619. if (view->viewport.h >= 0) {
  2620. rect->h = view->viewport.h;
  2621. } else {
  2622. rect->h = (int)SDL_ceilf(view->pixel_h / view->current_scale.y);
  2623. }
  2624. }
  2625. return true;
  2626. }
  2627. bool SDL_RenderViewportSet(SDL_Renderer *renderer)
  2628. {
  2629. CHECK_RENDERER_MAGIC(renderer, false);
  2630. const SDL_RenderViewState *view = renderer->view;
  2631. return (view->viewport.w >= 0 && view->viewport.h >= 0);
  2632. }
  2633. static void GetRenderViewportSize(SDL_Renderer *renderer, SDL_FRect *rect)
  2634. {
  2635. const SDL_RenderViewState *view = renderer->view;
  2636. const float scale_x = view->current_scale.x;
  2637. const float scale_y = view->current_scale.y;
  2638. rect->x = 0.0f;
  2639. rect->y = 0.0f;
  2640. if (view->viewport.w >= 0) {
  2641. rect->w = (float)view->viewport.w;
  2642. } else {
  2643. rect->w = view->pixel_w / scale_x;
  2644. }
  2645. if (view->viewport.h >= 0) {
  2646. rect->h = (float)view->viewport.h;
  2647. } else {
  2648. rect->h = view->pixel_h / scale_y;
  2649. }
  2650. }
  2651. bool SDL_GetRenderSafeArea(SDL_Renderer *renderer, SDL_Rect *rect)
  2652. {
  2653. if (rect) {
  2654. SDL_zerop(rect);
  2655. }
  2656. CHECK_RENDERER_MAGIC(renderer, false);
  2657. if (renderer->target || !renderer->window) {
  2658. // The entire viewport is safe for rendering
  2659. return SDL_GetRenderViewport(renderer, rect);
  2660. }
  2661. if (rect) {
  2662. // Get the window safe rect
  2663. SDL_Rect safe;
  2664. if (!SDL_GetWindowSafeArea(renderer->window, &safe)) {
  2665. return false;
  2666. }
  2667. // Convert the coordinates into the render space
  2668. float minx = (float)safe.x;
  2669. float miny = (float)safe.y;
  2670. float maxx = (float)safe.x + safe.w;
  2671. float maxy = (float)safe.y + safe.h;
  2672. if (!SDL_RenderCoordinatesFromWindow(renderer, minx, miny, &minx, &miny) ||
  2673. !SDL_RenderCoordinatesFromWindow(renderer, maxx, maxy, &maxx, &maxy)) {
  2674. return false;
  2675. }
  2676. rect->x = (int)SDL_ceilf(minx);
  2677. rect->y = (int)SDL_ceilf(miny);
  2678. rect->w = (int)SDL_ceilf(maxx - minx);
  2679. rect->h = (int)SDL_ceilf(maxy - miny);
  2680. // Clip with the viewport
  2681. SDL_Rect viewport;
  2682. if (!SDL_GetRenderViewport(renderer, &viewport)) {
  2683. return false;
  2684. }
  2685. if (!SDL_GetRectIntersection(rect, &viewport, rect)) {
  2686. return SDL_SetError("No safe area within viewport");
  2687. }
  2688. }
  2689. return true;
  2690. }
  2691. bool SDL_SetRenderClipRect(SDL_Renderer *renderer, const SDL_Rect *rect)
  2692. {
  2693. CHECK_RENDERER_MAGIC(renderer, false)
  2694. SDL_RenderViewState *view = renderer->view;
  2695. if (rect && rect->w >= 0 && rect->h >= 0) {
  2696. view->clipping_enabled = true;
  2697. SDL_copyp(&view->clip_rect, rect);
  2698. } else {
  2699. view->clipping_enabled = false;
  2700. SDL_zero(view->clip_rect);
  2701. }
  2702. UpdatePixelClipRect(renderer, view);
  2703. return QueueCmdSetClipRect(renderer);
  2704. }
  2705. bool SDL_GetRenderClipRect(SDL_Renderer *renderer, SDL_Rect *rect)
  2706. {
  2707. if (rect) {
  2708. SDL_zerop(rect);
  2709. }
  2710. CHECK_RENDERER_MAGIC(renderer, false)
  2711. if (rect) {
  2712. SDL_copyp(rect, &renderer->view->clip_rect);
  2713. }
  2714. return true;
  2715. }
  2716. bool SDL_RenderClipEnabled(SDL_Renderer *renderer)
  2717. {
  2718. CHECK_RENDERER_MAGIC(renderer, false)
  2719. return renderer->view->clipping_enabled;
  2720. }
  2721. bool SDL_SetRenderScale(SDL_Renderer *renderer, float scaleX, float scaleY)
  2722. {
  2723. bool result = true;
  2724. CHECK_RENDERER_MAGIC(renderer, false);
  2725. SDL_RenderViewState *view = renderer->view;
  2726. if ((view->scale.x == scaleX) && (view->scale.y == scaleY)) {
  2727. return true;
  2728. }
  2729. view->scale.x = scaleX;
  2730. view->scale.y = scaleY;
  2731. view->current_scale.x = scaleX * view->logical_scale.x;
  2732. view->current_scale.y = scaleY * view->logical_scale.y;
  2733. UpdatePixelViewport(renderer, view);
  2734. UpdatePixelClipRect(renderer, view);
  2735. // The scale affects the existing viewport and clip rectangle
  2736. result &= QueueCmdSetViewport(renderer);
  2737. result &= QueueCmdSetClipRect(renderer);
  2738. return result;
  2739. }
  2740. bool SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY)
  2741. {
  2742. if (scaleX) {
  2743. *scaleX = 1.0f;
  2744. }
  2745. if (scaleY) {
  2746. *scaleY = 1.0f;
  2747. }
  2748. CHECK_RENDERER_MAGIC(renderer, false);
  2749. const SDL_RenderViewState *view = renderer->view;
  2750. if (scaleX) {
  2751. *scaleX = view->scale.x;
  2752. }
  2753. if (scaleY) {
  2754. *scaleY = view->scale.y;
  2755. }
  2756. return true;
  2757. }
  2758. bool SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2759. {
  2760. const float fR = (float)r / 255.0f;
  2761. const float fG = (float)g / 255.0f;
  2762. const float fB = (float)b / 255.0f;
  2763. const float fA = (float)a / 255.0f;
  2764. return SDL_SetRenderDrawColorFloat(renderer, fR, fG, fB, fA);
  2765. }
  2766. bool SDL_SetRenderDrawColorFloat(SDL_Renderer *renderer, float r, float g, float b, float a)
  2767. {
  2768. CHECK_RENDERER_MAGIC(renderer, false);
  2769. renderer->color.r = r;
  2770. renderer->color.g = g;
  2771. renderer->color.b = b;
  2772. renderer->color.a = a;
  2773. return true;
  2774. }
  2775. bool SDL_GetRenderDrawColor(SDL_Renderer *renderer, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
  2776. {
  2777. float fR, fG, fB, fA;
  2778. if (!SDL_GetRenderDrawColorFloat(renderer, &fR, &fG, &fB, &fA)) {
  2779. if (r) {
  2780. *r = 0;
  2781. }
  2782. if (g) {
  2783. *g = 0;
  2784. }
  2785. if (b) {
  2786. *b = 0;
  2787. }
  2788. if (a) {
  2789. *a = 0;
  2790. }
  2791. return false;
  2792. }
  2793. if (r) {
  2794. *r = (Uint8)(fR * 255.0f);
  2795. }
  2796. if (g) {
  2797. *g = (Uint8)(fG * 255.0f);
  2798. }
  2799. if (b) {
  2800. *b = (Uint8)(fB * 255.0f);
  2801. }
  2802. if (a) {
  2803. *a = (Uint8)(fA * 255.0f);
  2804. }
  2805. return true;
  2806. }
  2807. bool SDL_GetRenderDrawColorFloat(SDL_Renderer *renderer, float *r, float *g, float *b, float *a)
  2808. {
  2809. SDL_FColor color;
  2810. if (r) {
  2811. *r = 0.0f;
  2812. }
  2813. if (g) {
  2814. *g = 0.0f;
  2815. }
  2816. if (b) {
  2817. *b = 0.0f;
  2818. }
  2819. if (a) {
  2820. *a = 0.0f;
  2821. }
  2822. CHECK_RENDERER_MAGIC(renderer, false);
  2823. color = renderer->color;
  2824. if (r) {
  2825. *r = color.r;
  2826. }
  2827. if (g) {
  2828. *g = color.g;
  2829. }
  2830. if (b) {
  2831. *b = color.b;
  2832. }
  2833. if (a) {
  2834. *a = color.a;
  2835. }
  2836. return true;
  2837. }
  2838. bool SDL_SetRenderColorScale(SDL_Renderer *renderer, float scale)
  2839. {
  2840. CHECK_RENDERER_MAGIC(renderer, false);
  2841. renderer->desired_color_scale = scale;
  2842. UpdateColorScale(renderer);
  2843. return true;
  2844. }
  2845. bool SDL_GetRenderColorScale(SDL_Renderer *renderer, float *scale)
  2846. {
  2847. if (scale) {
  2848. *scale = 1.0f;
  2849. }
  2850. CHECK_RENDERER_MAGIC(renderer, false);
  2851. if (scale) {
  2852. *scale = renderer->desired_color_scale;
  2853. }
  2854. return true;
  2855. }
  2856. bool SDL_SetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  2857. {
  2858. CHECK_RENDERER_MAGIC(renderer, false);
  2859. if (blendMode == SDL_BLENDMODE_INVALID) {
  2860. return SDL_InvalidParamError("blendMode");
  2861. }
  2862. if (blendMode == SDL_BLENDMODE_INVALID) {
  2863. return SDL_InvalidParamError("blendMode");
  2864. }
  2865. if (!IsSupportedBlendMode(renderer, blendMode)) {
  2866. return SDL_Unsupported();
  2867. }
  2868. renderer->blendMode = blendMode;
  2869. return true;
  2870. }
  2871. bool SDL_GetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode *blendMode)
  2872. {
  2873. if (blendMode) {
  2874. *blendMode = SDL_BLENDMODE_INVALID;
  2875. }
  2876. CHECK_RENDERER_MAGIC(renderer, false);
  2877. if (blendMode) {
  2878. *blendMode = renderer->blendMode;
  2879. }
  2880. return true;
  2881. }
  2882. bool SDL_RenderClear(SDL_Renderer *renderer)
  2883. {
  2884. CHECK_RENDERER_MAGIC(renderer, false);
  2885. return QueueCmdClear(renderer);
  2886. }
  2887. bool SDL_RenderPoint(SDL_Renderer *renderer, float x, float y)
  2888. {
  2889. SDL_FPoint fpoint;
  2890. fpoint.x = x;
  2891. fpoint.y = y;
  2892. return SDL_RenderPoints(renderer, &fpoint, 1);
  2893. }
  2894. static bool RenderPointsWithRects(SDL_Renderer *renderer, const SDL_FPoint *fpoints, const int count)
  2895. {
  2896. bool result;
  2897. bool isstack;
  2898. SDL_FRect *frects;
  2899. int i;
  2900. if (count < 1) {
  2901. return true;
  2902. }
  2903. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2904. if (!frects) {
  2905. return false;
  2906. }
  2907. const SDL_RenderViewState *view = renderer->view;
  2908. const float scale_x = view->current_scale.x;
  2909. const float scale_y = view->current_scale.y;
  2910. for (i = 0; i < count; ++i) {
  2911. frects[i].x = fpoints[i].x * scale_x;
  2912. frects[i].y = fpoints[i].y * scale_y;
  2913. frects[i].w = scale_x;
  2914. frects[i].h = scale_y;
  2915. }
  2916. result = QueueCmdFillRects(renderer, frects, count);
  2917. SDL_small_free(frects, isstack);
  2918. return result;
  2919. }
  2920. bool SDL_RenderPoints(SDL_Renderer *renderer, const SDL_FPoint *points, int count)
  2921. {
  2922. bool result;
  2923. CHECK_RENDERER_MAGIC(renderer, false);
  2924. if (!points) {
  2925. return SDL_InvalidParamError("SDL_RenderPoints(): points");
  2926. }
  2927. if (count < 1) {
  2928. return true;
  2929. }
  2930. #if DONT_DRAW_WHILE_HIDDEN
  2931. // Don't draw while we're hidden
  2932. if (renderer->hidden) {
  2933. return true;
  2934. }
  2935. #endif
  2936. const SDL_RenderViewState *view = renderer->view;
  2937. if ((view->current_scale.x != 1.0f) || (view->current_scale.y != 1.0f)) {
  2938. result = RenderPointsWithRects(renderer, points, count);
  2939. } else {
  2940. result = QueueCmdDrawPoints(renderer, points, count);
  2941. }
  2942. return result;
  2943. }
  2944. bool SDL_RenderLine(SDL_Renderer *renderer, float x1, float y1, float x2, float y2)
  2945. {
  2946. SDL_FPoint points[2];
  2947. points[0].x = x1;
  2948. points[0].y = y1;
  2949. points[1].x = x2;
  2950. points[1].y = y2;
  2951. return SDL_RenderLines(renderer, points, 2);
  2952. }
  2953. static bool RenderLineBresenham(SDL_Renderer *renderer, int x1, int y1, int x2, int y2, bool draw_last)
  2954. {
  2955. const SDL_RenderViewState *view = renderer->view;
  2956. const int MAX_PIXELS = SDL_max(view->pixel_w, view->pixel_h) * 4;
  2957. int i, deltax, deltay, numpixels;
  2958. int d, dinc1, dinc2;
  2959. int x, xinc1, xinc2;
  2960. int y, yinc1, yinc2;
  2961. bool result;
  2962. bool isstack;
  2963. SDL_FPoint *points;
  2964. SDL_Rect viewport;
  2965. /* the backend might clip this further to the clipping rect, but we
  2966. just want a basic safety against generating millions of points for
  2967. massive lines. */
  2968. viewport = view->pixel_viewport;
  2969. viewport.x = 0;
  2970. viewport.y = 0;
  2971. if (!SDL_GetRectAndLineIntersection(&viewport, &x1, &y1, &x2, &y2)) {
  2972. return true;
  2973. }
  2974. deltax = SDL_abs(x2 - x1);
  2975. deltay = SDL_abs(y2 - y1);
  2976. if (deltax >= deltay) {
  2977. numpixels = deltax + 1;
  2978. d = (2 * deltay) - deltax;
  2979. dinc1 = deltay * 2;
  2980. dinc2 = (deltay - deltax) * 2;
  2981. xinc1 = 1;
  2982. xinc2 = 1;
  2983. yinc1 = 0;
  2984. yinc2 = 1;
  2985. } else {
  2986. numpixels = deltay + 1;
  2987. d = (2 * deltax) - deltay;
  2988. dinc1 = deltax * 2;
  2989. dinc2 = (deltax - deltay) * 2;
  2990. xinc1 = 0;
  2991. xinc2 = 1;
  2992. yinc1 = 1;
  2993. yinc2 = 1;
  2994. }
  2995. if (x1 > x2) {
  2996. xinc1 = -xinc1;
  2997. xinc2 = -xinc2;
  2998. }
  2999. if (y1 > y2) {
  3000. yinc1 = -yinc1;
  3001. yinc2 = -yinc2;
  3002. }
  3003. x = x1;
  3004. y = y1;
  3005. if (!draw_last) {
  3006. --numpixels;
  3007. }
  3008. if (numpixels > MAX_PIXELS) {
  3009. return SDL_SetError("Line too long (tried to draw %d pixels, max %d)", numpixels, MAX_PIXELS);
  3010. }
  3011. points = SDL_small_alloc(SDL_FPoint, numpixels, &isstack);
  3012. if (!points) {
  3013. return false;
  3014. }
  3015. for (i = 0; i < numpixels; ++i) {
  3016. points[i].x = (float)x;
  3017. points[i].y = (float)y;
  3018. if (d < 0) {
  3019. d += dinc1;
  3020. x += xinc1;
  3021. y += yinc1;
  3022. } else {
  3023. d += dinc2;
  3024. x += xinc2;
  3025. y += yinc2;
  3026. }
  3027. }
  3028. if ((view->current_scale.x != 1.0f) || (view->current_scale.y != 1.0f)) {
  3029. result = RenderPointsWithRects(renderer, points, numpixels);
  3030. } else {
  3031. result = QueueCmdDrawPoints(renderer, points, numpixels);
  3032. }
  3033. SDL_small_free(points, isstack);
  3034. return result;
  3035. }
  3036. static bool RenderLinesWithRectsF(SDL_Renderer *renderer, const SDL_FPoint *points, const int count)
  3037. {
  3038. const SDL_RenderViewState *view = renderer->view;
  3039. const float scale_x = view->current_scale.x;
  3040. const float scale_y = view->current_scale.y;
  3041. SDL_FRect *frect;
  3042. SDL_FRect *frects;
  3043. int i, nrects = 0;
  3044. bool result = true;
  3045. bool isstack;
  3046. bool drew_line = false;
  3047. bool draw_last = false;
  3048. frects = SDL_small_alloc(SDL_FRect, count - 1, &isstack);
  3049. if (!frects) {
  3050. return false;
  3051. }
  3052. for (i = 0; i < count - 1; ++i) {
  3053. bool same_x = (points[i].x == points[i + 1].x);
  3054. bool same_y = (points[i].y == points[i + 1].y);
  3055. if (i == (count - 2)) {
  3056. if (!drew_line || points[i + 1].x != points[0].x || points[i + 1].y != points[0].y) {
  3057. draw_last = true;
  3058. }
  3059. } else {
  3060. if (same_x && same_y) {
  3061. continue;
  3062. }
  3063. }
  3064. if (same_x) {
  3065. const float minY = SDL_min(points[i].y, points[i + 1].y);
  3066. const float maxY = SDL_max(points[i].y, points[i + 1].y);
  3067. frect = &frects[nrects++];
  3068. frect->x = points[i].x * scale_x;
  3069. frect->y = minY * scale_y;
  3070. frect->w = scale_x;
  3071. frect->h = (maxY - minY + draw_last) * scale_y;
  3072. if (!draw_last && points[i + 1].y < points[i].y) {
  3073. frect->y += scale_y;
  3074. }
  3075. } else if (same_y) {
  3076. const float minX = SDL_min(points[i].x, points[i + 1].x);
  3077. const float maxX = SDL_max(points[i].x, points[i + 1].x);
  3078. frect = &frects[nrects++];
  3079. frect->x = minX * scale_x;
  3080. frect->y = points[i].y * scale_y;
  3081. frect->w = (maxX - minX + draw_last) * scale_x;
  3082. frect->h = scale_y;
  3083. if (!draw_last && points[i + 1].x < points[i].x) {
  3084. frect->x += scale_x;
  3085. }
  3086. } else {
  3087. result &= RenderLineBresenham(renderer, (int)SDL_roundf(points[i].x), (int)SDL_roundf(points[i].y),
  3088. (int)SDL_roundf(points[i + 1].x), (int)SDL_roundf(points[i + 1].y), draw_last);
  3089. }
  3090. drew_line = true;
  3091. }
  3092. if (nrects) {
  3093. result &= QueueCmdFillRects(renderer, frects, nrects);
  3094. }
  3095. SDL_small_free(frects, isstack);
  3096. return result;
  3097. }
  3098. bool SDL_RenderLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count)
  3099. {
  3100. bool result = true;
  3101. CHECK_RENDERER_MAGIC(renderer, false);
  3102. if (!points) {
  3103. return SDL_InvalidParamError("SDL_RenderLines(): points");
  3104. }
  3105. if (count < 2) {
  3106. return true;
  3107. }
  3108. #if DONT_DRAW_WHILE_HIDDEN
  3109. // Don't draw while we're hidden
  3110. if (renderer->hidden) {
  3111. return true;
  3112. }
  3113. #endif
  3114. SDL_RenderViewState *view = renderer->view;
  3115. const bool islogical = ((view == &renderer->main_view) && (view->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED));
  3116. if (islogical || (renderer->line_method == SDL_RENDERLINEMETHOD_GEOMETRY)) {
  3117. const float scale_x = view->current_scale.x;
  3118. const float scale_y = view->current_scale.y;
  3119. bool isstack1;
  3120. bool isstack2;
  3121. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  3122. int *indices = SDL_small_alloc(int, (4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
  3123. if (xy && indices) {
  3124. int i;
  3125. float *ptr_xy = xy;
  3126. int *ptr_indices = indices;
  3127. const int xy_stride = 2 * sizeof(float);
  3128. int num_vertices = 4 * count;
  3129. int num_indices = 0;
  3130. const int size_indices = 4;
  3131. int cur_index = -4;
  3132. const int is_looping = (points[0].x == points[count - 1].x && points[0].y == points[count - 1].y);
  3133. SDL_FPoint p; // previous point
  3134. p.x = p.y = 0.0f;
  3135. /* p q
  3136. 0----1------ 4----5
  3137. | \ |``\ | \ |
  3138. | \ | ` `\| \ |
  3139. 3----2-------7----6
  3140. */
  3141. for (i = 0; i < count; ++i) {
  3142. SDL_FPoint q = points[i]; // current point
  3143. q.x *= scale_x;
  3144. q.y *= scale_y;
  3145. *ptr_xy++ = q.x;
  3146. *ptr_xy++ = q.y;
  3147. *ptr_xy++ = q.x + scale_x;
  3148. *ptr_xy++ = q.y;
  3149. *ptr_xy++ = q.x + scale_x;
  3150. *ptr_xy++ = q.y + scale_y;
  3151. *ptr_xy++ = q.x;
  3152. *ptr_xy++ = q.y + scale_y;
  3153. #define ADD_TRIANGLE(i1, i2, i3) \
  3154. *ptr_indices++ = cur_index + (i1); \
  3155. *ptr_indices++ = cur_index + (i2); \
  3156. *ptr_indices++ = cur_index + (i3); \
  3157. num_indices += 3;
  3158. // closed polyline, don´t draw twice the point
  3159. if (i || is_looping == 0) {
  3160. ADD_TRIANGLE(4, 5, 6)
  3161. ADD_TRIANGLE(4, 6, 7)
  3162. }
  3163. // first point only, no segment
  3164. if (i == 0) {
  3165. p = q;
  3166. cur_index += 4;
  3167. continue;
  3168. }
  3169. // draw segment
  3170. if (p.y == q.y) {
  3171. if (p.x < q.x) {
  3172. ADD_TRIANGLE(1, 4, 7)
  3173. ADD_TRIANGLE(1, 7, 2)
  3174. } else {
  3175. ADD_TRIANGLE(5, 0, 3)
  3176. ADD_TRIANGLE(5, 3, 6)
  3177. }
  3178. } else if (p.x == q.x) {
  3179. if (p.y < q.y) {
  3180. ADD_TRIANGLE(2, 5, 4)
  3181. ADD_TRIANGLE(2, 4, 3)
  3182. } else {
  3183. ADD_TRIANGLE(6, 1, 0)
  3184. ADD_TRIANGLE(6, 0, 7)
  3185. }
  3186. } else {
  3187. if (p.y < q.y) {
  3188. if (p.x < q.x) {
  3189. ADD_TRIANGLE(1, 5, 4)
  3190. ADD_TRIANGLE(1, 4, 2)
  3191. ADD_TRIANGLE(2, 4, 7)
  3192. ADD_TRIANGLE(2, 7, 3)
  3193. } else {
  3194. ADD_TRIANGLE(4, 0, 5)
  3195. ADD_TRIANGLE(5, 0, 3)
  3196. ADD_TRIANGLE(5, 3, 6)
  3197. ADD_TRIANGLE(6, 3, 2)
  3198. }
  3199. } else {
  3200. if (p.x < q.x) {
  3201. ADD_TRIANGLE(0, 4, 7)
  3202. ADD_TRIANGLE(0, 7, 1)
  3203. ADD_TRIANGLE(1, 7, 6)
  3204. ADD_TRIANGLE(1, 6, 2)
  3205. } else {
  3206. ADD_TRIANGLE(6, 5, 1)
  3207. ADD_TRIANGLE(6, 1, 0)
  3208. ADD_TRIANGLE(7, 6, 0)
  3209. ADD_TRIANGLE(7, 0, 3)
  3210. }
  3211. }
  3212. }
  3213. p = q;
  3214. cur_index += 4;
  3215. }
  3216. result = QueueCmdGeometry(renderer, NULL,
  3217. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  3218. num_vertices, indices, num_indices, size_indices,
  3219. 1.0f, 1.0f, SDL_TEXTURE_ADDRESS_CLAMP);
  3220. }
  3221. SDL_small_free(xy, isstack1);
  3222. SDL_small_free(indices, isstack2);
  3223. } else if (renderer->line_method == SDL_RENDERLINEMETHOD_POINTS) {
  3224. result = RenderLinesWithRectsF(renderer, points, count);
  3225. } else if (view->scale.x != 1.0f || view->scale.y != 1.0f) { /* we checked for logical scale elsewhere. */
  3226. result = RenderLinesWithRectsF(renderer, points, count);
  3227. } else {
  3228. result = QueueCmdDrawLines(renderer, points, count);
  3229. }
  3230. return result;
  3231. }
  3232. bool SDL_RenderRect(SDL_Renderer *renderer, const SDL_FRect *rect)
  3233. {
  3234. SDL_FRect frect;
  3235. SDL_FPoint points[5];
  3236. CHECK_RENDERER_MAGIC(renderer, false);
  3237. // If 'rect' == NULL, then outline the whole surface
  3238. if (!rect) {
  3239. GetRenderViewportSize(renderer, &frect);
  3240. rect = &frect;
  3241. }
  3242. points[0].x = rect->x;
  3243. points[0].y = rect->y;
  3244. points[1].x = rect->x + rect->w - 1;
  3245. points[1].y = rect->y;
  3246. points[2].x = rect->x + rect->w - 1;
  3247. points[2].y = rect->y + rect->h - 1;
  3248. points[3].x = rect->x;
  3249. points[3].y = rect->y + rect->h - 1;
  3250. points[4].x = rect->x;
  3251. points[4].y = rect->y;
  3252. return SDL_RenderLines(renderer, points, 5);
  3253. }
  3254. bool SDL_RenderRects(SDL_Renderer *renderer, const SDL_FRect *rects, int count)
  3255. {
  3256. int i;
  3257. CHECK_RENDERER_MAGIC(renderer, false);
  3258. if (!rects) {
  3259. return SDL_InvalidParamError("SDL_RenderRects(): rects");
  3260. }
  3261. if (count < 1) {
  3262. return true;
  3263. }
  3264. #if DONT_DRAW_WHILE_HIDDEN
  3265. // Don't draw while we're hidden
  3266. if (renderer->hidden) {
  3267. return true;
  3268. }
  3269. #endif
  3270. for (i = 0; i < count; ++i) {
  3271. if (!SDL_RenderRect(renderer, &rects[i])) {
  3272. return false;
  3273. }
  3274. }
  3275. return true;
  3276. }
  3277. bool SDL_RenderFillRect(SDL_Renderer *renderer, const SDL_FRect *rect)
  3278. {
  3279. SDL_FRect frect;
  3280. CHECK_RENDERER_MAGIC(renderer, false);
  3281. // If 'rect' == NULL, then fill the whole surface
  3282. if (!rect) {
  3283. GetRenderViewportSize(renderer, &frect);
  3284. rect = &frect;
  3285. }
  3286. return SDL_RenderFillRects(renderer, rect, 1);
  3287. }
  3288. bool SDL_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int count)
  3289. {
  3290. SDL_FRect *frects;
  3291. int i;
  3292. bool result;
  3293. bool isstack;
  3294. CHECK_RENDERER_MAGIC(renderer, false);
  3295. if (!rects) {
  3296. return SDL_InvalidParamError("SDL_RenderFillRects(): rects");
  3297. }
  3298. if (count < 1) {
  3299. return true;
  3300. }
  3301. #if DONT_DRAW_WHILE_HIDDEN
  3302. // Don't draw while we're hidden
  3303. if (renderer->hidden) {
  3304. return true;
  3305. }
  3306. #endif
  3307. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  3308. if (!frects) {
  3309. return false;
  3310. }
  3311. const SDL_RenderViewState *view = renderer->view;
  3312. const float scale_x = view->current_scale.x;
  3313. const float scale_y = view->current_scale.y;
  3314. for (i = 0; i < count; ++i) {
  3315. frects[i].x = rects[i].x * scale_x;
  3316. frects[i].y = rects[i].y * scale_y;
  3317. frects[i].w = rects[i].w * scale_x;
  3318. frects[i].h = rects[i].h * scale_y;
  3319. }
  3320. result = QueueCmdFillRects(renderer, frects, count);
  3321. SDL_small_free(frects, isstack);
  3322. return result;
  3323. }
  3324. static bool SDL_RenderTextureInternal(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect)
  3325. {
  3326. const SDL_RenderViewState *view = renderer->view;
  3327. const float scale_x = view->current_scale.x;
  3328. const float scale_y = view->current_scale.y;
  3329. const bool use_rendergeometry = (!renderer->QueueCopy);
  3330. bool result;
  3331. if (use_rendergeometry) {
  3332. float xy[8];
  3333. const int xy_stride = 2 * sizeof(float);
  3334. float uv[8];
  3335. const int uv_stride = 2 * sizeof(float);
  3336. const int num_vertices = 4;
  3337. const int *indices = rect_index_order;
  3338. const int num_indices = 6;
  3339. const int size_indices = 4;
  3340. float minu, minv, maxu, maxv;
  3341. float minx, miny, maxx, maxy;
  3342. minu = srcrect->x / texture->w;
  3343. minv = srcrect->y / texture->h;
  3344. maxu = (srcrect->x + srcrect->w) / texture->w;
  3345. maxv = (srcrect->y + srcrect->h) / texture->h;
  3346. minx = dstrect->x;
  3347. miny = dstrect->y;
  3348. maxx = dstrect->x + dstrect->w;
  3349. maxy = dstrect->y + dstrect->h;
  3350. uv[0] = minu;
  3351. uv[1] = minv;
  3352. uv[2] = maxu;
  3353. uv[3] = minv;
  3354. uv[4] = maxu;
  3355. uv[5] = maxv;
  3356. uv[6] = minu;
  3357. uv[7] = maxv;
  3358. xy[0] = minx;
  3359. xy[1] = miny;
  3360. xy[2] = maxx;
  3361. xy[3] = miny;
  3362. xy[4] = maxx;
  3363. xy[5] = maxy;
  3364. xy[6] = minx;
  3365. xy[7] = maxy;
  3366. result = QueueCmdGeometry(renderer, texture,
  3367. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3368. num_vertices, indices, num_indices, size_indices,
  3369. scale_x, scale_y, SDL_TEXTURE_ADDRESS_CLAMP);
  3370. } else {
  3371. const SDL_FRect rect = { dstrect->x * scale_x, dstrect->y * scale_y, dstrect->w * scale_x, dstrect->h * scale_y };
  3372. result = QueueCmdCopy(renderer, texture, srcrect, &rect);
  3373. }
  3374. return result;
  3375. }
  3376. bool SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect)
  3377. {
  3378. CHECK_RENDERER_MAGIC(renderer, false);
  3379. CHECK_TEXTURE_MAGIC(texture, false);
  3380. if (renderer != texture->renderer) {
  3381. return SDL_SetError("Texture was not created with this renderer");
  3382. }
  3383. #if DONT_DRAW_WHILE_HIDDEN
  3384. // Don't draw while we're hidden
  3385. if (renderer->hidden) {
  3386. return true;
  3387. }
  3388. #endif
  3389. SDL_FRect real_srcrect;
  3390. real_srcrect.x = 0.0f;
  3391. real_srcrect.y = 0.0f;
  3392. real_srcrect.w = (float)texture->w;
  3393. real_srcrect.h = (float)texture->h;
  3394. if (srcrect) {
  3395. if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect) ||
  3396. real_srcrect.w == 0.0f || real_srcrect.h == 0.0f) {
  3397. return true;
  3398. }
  3399. }
  3400. SDL_FRect full_dstrect;
  3401. if (!dstrect) {
  3402. GetRenderViewportSize(renderer, &full_dstrect);
  3403. dstrect = &full_dstrect;
  3404. }
  3405. if (texture->native) {
  3406. texture = texture->native;
  3407. }
  3408. texture->last_command_generation = renderer->render_command_generation;
  3409. return SDL_RenderTextureInternal(renderer, texture, &real_srcrect, dstrect);
  3410. }
  3411. bool SDL_RenderTextureAffine(SDL_Renderer *renderer, SDL_Texture *texture,
  3412. const SDL_FRect *srcrect, const SDL_FPoint *origin, const SDL_FPoint *right, const SDL_FPoint *down)
  3413. {
  3414. SDL_FRect real_srcrect;
  3415. SDL_FRect real_dstrect;
  3416. bool result;
  3417. CHECK_RENDERER_MAGIC(renderer, false);
  3418. CHECK_TEXTURE_MAGIC(texture, false);
  3419. if (renderer != texture->renderer) {
  3420. return SDL_SetError("Texture was not created with this renderer");
  3421. }
  3422. if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
  3423. return SDL_SetError("Renderer does not support RenderCopyEx");
  3424. }
  3425. #if DONT_DRAW_WHILE_HIDDEN
  3426. // Don't draw while we're hidden
  3427. if (renderer->hidden) {
  3428. return true;
  3429. }
  3430. #endif
  3431. real_srcrect.x = 0.0f;
  3432. real_srcrect.y = 0.0f;
  3433. real_srcrect.w = (float)texture->w;
  3434. real_srcrect.h = (float)texture->h;
  3435. if (srcrect) {
  3436. if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect)) {
  3437. return true;
  3438. }
  3439. }
  3440. GetRenderViewportSize(renderer, &real_dstrect);
  3441. if (texture->native) {
  3442. texture = texture->native;
  3443. }
  3444. texture->last_command_generation = renderer->render_command_generation;
  3445. const SDL_RenderViewState *view = renderer->view;
  3446. const float scale_x = view->current_scale.x;
  3447. const float scale_y = view->current_scale.y;
  3448. {
  3449. float xy[8];
  3450. const int xy_stride = 2 * sizeof(float);
  3451. float uv[8];
  3452. const int uv_stride = 2 * sizeof(float);
  3453. const int num_vertices = 4;
  3454. const int *indices = rect_index_order;
  3455. const int num_indices = 6;
  3456. const int size_indices = 4;
  3457. float minu = real_srcrect.x / texture->w;
  3458. float minv = real_srcrect.y / texture->h;
  3459. float maxu = (real_srcrect.x + real_srcrect.w) / texture->w;
  3460. float maxv = (real_srcrect.y + real_srcrect.h) / texture->h;
  3461. uv[0] = minu;
  3462. uv[1] = minv;
  3463. uv[2] = maxu;
  3464. uv[3] = minv;
  3465. uv[4] = maxu;
  3466. uv[5] = maxv;
  3467. uv[6] = minu;
  3468. uv[7] = maxv;
  3469. // (minx, miny)
  3470. if (origin) {
  3471. xy[0] = origin->x;
  3472. xy[1] = origin->y;
  3473. } else {
  3474. xy[0] = real_dstrect.x;
  3475. xy[1] = real_dstrect.y;
  3476. }
  3477. // (maxx, miny)
  3478. if (right) {
  3479. xy[2] = right->x;
  3480. xy[3] = right->y;
  3481. } else {
  3482. xy[2] = real_dstrect.x + real_dstrect.w;
  3483. xy[3] = real_dstrect.y;
  3484. }
  3485. // (minx, maxy)
  3486. if (down) {
  3487. xy[6] = down->x;
  3488. xy[7] = down->y;
  3489. } else {
  3490. xy[6] = real_dstrect.x;
  3491. xy[7] = real_dstrect.y + real_dstrect.h;
  3492. }
  3493. // (maxx, maxy)
  3494. if (origin || right || down) {
  3495. xy[4] = xy[2] + xy[6] - xy[0];
  3496. xy[5] = xy[3] + xy[7] - xy[1];
  3497. } else {
  3498. xy[4] = real_dstrect.x + real_dstrect.w;
  3499. xy[5] = real_dstrect.y + real_dstrect.h;
  3500. }
  3501. result = QueueCmdGeometry(
  3502. renderer, texture,
  3503. xy, xy_stride,
  3504. &texture->color, 0 /* color_stride */,
  3505. uv, uv_stride,
  3506. num_vertices, indices, num_indices, size_indices,
  3507. scale_x, scale_y, SDL_TEXTURE_ADDRESS_CLAMP
  3508. );
  3509. }
  3510. return result;
  3511. }
  3512. bool SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
  3513. const SDL_FRect *srcrect, const SDL_FRect *dstrect,
  3514. const double angle, const SDL_FPoint *center, const SDL_FlipMode flip)
  3515. {
  3516. SDL_FRect real_srcrect;
  3517. SDL_FPoint real_center;
  3518. bool result;
  3519. if (flip == SDL_FLIP_NONE && (int)(angle / 360) == angle / 360) { // fast path when we don't need rotation or flipping
  3520. return SDL_RenderTexture(renderer, texture, srcrect, dstrect);
  3521. }
  3522. CHECK_RENDERER_MAGIC(renderer, false);
  3523. CHECK_TEXTURE_MAGIC(texture, false);
  3524. if (renderer != texture->renderer) {
  3525. return SDL_SetError("Texture was not created with this renderer");
  3526. }
  3527. if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
  3528. return SDL_SetError("Renderer does not support RenderCopyEx");
  3529. }
  3530. #if DONT_DRAW_WHILE_HIDDEN
  3531. // Don't draw while we're hidden
  3532. if (renderer->hidden) {
  3533. return true;
  3534. }
  3535. #endif
  3536. real_srcrect.x = 0.0f;
  3537. real_srcrect.y = 0.0f;
  3538. real_srcrect.w = (float)texture->w;
  3539. real_srcrect.h = (float)texture->h;
  3540. if (srcrect) {
  3541. if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect)) {
  3542. return true;
  3543. }
  3544. }
  3545. // We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we?
  3546. SDL_FRect full_dstrect;
  3547. if (!dstrect) {
  3548. GetRenderViewportSize(renderer, &full_dstrect);
  3549. dstrect = &full_dstrect;
  3550. }
  3551. if (texture->native) {
  3552. texture = texture->native;
  3553. }
  3554. if (center) {
  3555. real_center = *center;
  3556. } else {
  3557. real_center.x = dstrect->w / 2.0f;
  3558. real_center.y = dstrect->h / 2.0f;
  3559. }
  3560. texture->last_command_generation = renderer->render_command_generation;
  3561. const SDL_RenderViewState *view = renderer->view;
  3562. const float scale_x = view->current_scale.x;
  3563. const float scale_y = view->current_scale.y;
  3564. const bool use_rendergeometry = (!renderer->QueueCopyEx);
  3565. if (use_rendergeometry) {
  3566. float xy[8];
  3567. const int xy_stride = 2 * sizeof(float);
  3568. float uv[8];
  3569. const int uv_stride = 2 * sizeof(float);
  3570. const int num_vertices = 4;
  3571. const int *indices = rect_index_order;
  3572. const int num_indices = 6;
  3573. const int size_indices = 4;
  3574. float minu, minv, maxu, maxv;
  3575. float minx, miny, maxx, maxy;
  3576. float centerx, centery;
  3577. float s_minx, s_miny, s_maxx, s_maxy;
  3578. float c_minx, c_miny, c_maxx, c_maxy;
  3579. const float radian_angle = (float)((SDL_PI_D * angle) / 180.0);
  3580. const float s = SDL_sinf(radian_angle);
  3581. const float c = SDL_cosf(radian_angle);
  3582. minu = real_srcrect.x / texture->w;
  3583. minv = real_srcrect.y / texture->h;
  3584. maxu = (real_srcrect.x + real_srcrect.w) / texture->w;
  3585. maxv = (real_srcrect.y + real_srcrect.h) / texture->h;
  3586. centerx = real_center.x + dstrect->x;
  3587. centery = real_center.y + dstrect->y;
  3588. if (flip & SDL_FLIP_HORIZONTAL) {
  3589. minx = dstrect->x + dstrect->w;
  3590. maxx = dstrect->x;
  3591. } else {
  3592. minx = dstrect->x;
  3593. maxx = dstrect->x + dstrect->w;
  3594. }
  3595. if (flip & SDL_FLIP_VERTICAL) {
  3596. miny = dstrect->y + dstrect->h;
  3597. maxy = dstrect->y;
  3598. } else {
  3599. miny = dstrect->y;
  3600. maxy = dstrect->y + dstrect->h;
  3601. }
  3602. uv[0] = minu;
  3603. uv[1] = minv;
  3604. uv[2] = maxu;
  3605. uv[3] = minv;
  3606. uv[4] = maxu;
  3607. uv[5] = maxv;
  3608. uv[6] = minu;
  3609. uv[7] = maxv;
  3610. /* apply rotation with 2x2 matrix ( c -s )
  3611. * ( s c ) */
  3612. s_minx = s * (minx - centerx);
  3613. s_miny = s * (miny - centery);
  3614. s_maxx = s * (maxx - centerx);
  3615. s_maxy = s * (maxy - centery);
  3616. c_minx = c * (minx - centerx);
  3617. c_miny = c * (miny - centery);
  3618. c_maxx = c * (maxx - centerx);
  3619. c_maxy = c * (maxy - centery);
  3620. // (minx, miny)
  3621. xy[0] = (c_minx - s_miny) + centerx;
  3622. xy[1] = (s_minx + c_miny) + centery;
  3623. // (maxx, miny)
  3624. xy[2] = (c_maxx - s_miny) + centerx;
  3625. xy[3] = (s_maxx + c_miny) + centery;
  3626. // (maxx, maxy)
  3627. xy[4] = (c_maxx - s_maxy) + centerx;
  3628. xy[5] = (s_maxx + c_maxy) + centery;
  3629. // (minx, maxy)
  3630. xy[6] = (c_minx - s_maxy) + centerx;
  3631. xy[7] = (s_minx + c_maxy) + centery;
  3632. result = QueueCmdGeometry(renderer, texture,
  3633. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3634. num_vertices, indices, num_indices, size_indices,
  3635. scale_x, scale_y, SDL_TEXTURE_ADDRESS_CLAMP);
  3636. } else {
  3637. result = QueueCmdCopyEx(renderer, texture, &real_srcrect, dstrect, angle, &real_center, flip, scale_x, scale_y);
  3638. }
  3639. return result;
  3640. }
  3641. static bool SDL_RenderTextureTiled_Wrap(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float scale, const SDL_FRect *dstrect)
  3642. {
  3643. float xy[8];
  3644. const int xy_stride = 2 * sizeof(float);
  3645. float uv[8];
  3646. const int uv_stride = 2 * sizeof(float);
  3647. const int num_vertices = 4;
  3648. const int *indices = rect_index_order;
  3649. const int num_indices = 6;
  3650. const int size_indices = 4;
  3651. float minu, minv, maxu, maxv;
  3652. float minx, miny, maxx, maxy;
  3653. minu = 0.0f;
  3654. minv = 0.0f;
  3655. maxu = dstrect->w / (srcrect->w * scale);
  3656. maxv = dstrect->h / (srcrect->h * scale);
  3657. minx = dstrect->x;
  3658. miny = dstrect->y;
  3659. maxx = dstrect->x + dstrect->w;
  3660. maxy = dstrect->y + dstrect->h;
  3661. uv[0] = minu;
  3662. uv[1] = minv;
  3663. uv[2] = maxu;
  3664. uv[3] = minv;
  3665. uv[4] = maxu;
  3666. uv[5] = maxv;
  3667. uv[6] = minu;
  3668. uv[7] = maxv;
  3669. xy[0] = minx;
  3670. xy[1] = miny;
  3671. xy[2] = maxx;
  3672. xy[3] = miny;
  3673. xy[4] = maxx;
  3674. xy[5] = maxy;
  3675. xy[6] = minx;
  3676. xy[7] = maxy;
  3677. const SDL_RenderViewState *view = renderer->view;
  3678. return QueueCmdGeometry(renderer, texture,
  3679. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3680. num_vertices, indices, num_indices, size_indices,
  3681. view->current_scale.x, view->current_scale.y, SDL_TEXTURE_ADDRESS_WRAP);
  3682. }
  3683. static bool SDL_RenderTextureTiled_Iterate(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float scale, const SDL_FRect *dstrect)
  3684. {
  3685. float tile_width = srcrect->w * scale;
  3686. float tile_height = srcrect->h * scale;
  3687. float float_rows, float_cols;
  3688. float remaining_w = SDL_modff(dstrect->w / tile_width, &float_cols);
  3689. float remaining_h = SDL_modff(dstrect->h / tile_height, &float_rows);
  3690. float remaining_src_w = remaining_w * srcrect->w;
  3691. float remaining_src_h = remaining_h * srcrect->h;
  3692. float remaining_dst_w = remaining_w * tile_width;
  3693. float remaining_dst_h = remaining_h * tile_height;
  3694. int rows = (int)float_rows;
  3695. int cols = (int)float_cols;
  3696. SDL_FRect curr_src, curr_dst;
  3697. SDL_copyp(&curr_src, srcrect);
  3698. curr_dst.y = dstrect->y;
  3699. curr_dst.w = tile_width;
  3700. curr_dst.h = tile_height;
  3701. for (int y = 0; y < rows; ++y) {
  3702. curr_dst.x = dstrect->x;
  3703. for (int x = 0; x < cols; ++x) {
  3704. if (!SDL_RenderTextureInternal(renderer, texture, &curr_src, &curr_dst)) {
  3705. return false;
  3706. }
  3707. curr_dst.x += curr_dst.w;
  3708. }
  3709. if (remaining_dst_w > 0.0f) {
  3710. curr_src.w = remaining_src_w;
  3711. curr_dst.w = remaining_dst_w;
  3712. if (!SDL_RenderTextureInternal(renderer, texture, &curr_src, &curr_dst)) {
  3713. return false;
  3714. }
  3715. curr_src.w = srcrect->w;
  3716. curr_dst.w = tile_width;
  3717. }
  3718. curr_dst.y += curr_dst.h;
  3719. }
  3720. if (remaining_dst_h > 0.0f) {
  3721. curr_src.h = remaining_src_h;
  3722. curr_dst.h = remaining_dst_h;
  3723. curr_dst.x = dstrect->x;
  3724. for (int x = 0; x < cols; ++x) {
  3725. if (!SDL_RenderTextureInternal(renderer, texture, &curr_src, &curr_dst)) {
  3726. return false;
  3727. }
  3728. curr_dst.x += curr_dst.w;
  3729. }
  3730. if (remaining_dst_w > 0.0f) {
  3731. curr_src.w = remaining_src_w;
  3732. curr_dst.w = remaining_dst_w;
  3733. if (!SDL_RenderTextureInternal(renderer, texture, &curr_src, &curr_dst)) {
  3734. return false;
  3735. }
  3736. }
  3737. }
  3738. return true;
  3739. }
  3740. bool SDL_RenderTextureTiled(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float scale, const SDL_FRect *dstrect)
  3741. {
  3742. SDL_FRect real_srcrect;
  3743. CHECK_RENDERER_MAGIC(renderer, false);
  3744. CHECK_TEXTURE_MAGIC(texture, false);
  3745. if (renderer != texture->renderer) {
  3746. return SDL_SetError("Texture was not created with this renderer");
  3747. }
  3748. if (scale <= 0.0f) {
  3749. return SDL_InvalidParamError("scale");
  3750. }
  3751. #if DONT_DRAW_WHILE_HIDDEN
  3752. // Don't draw while we're hidden
  3753. if (renderer->hidden) {
  3754. return true;
  3755. }
  3756. #endif
  3757. real_srcrect.x = 0.0f;
  3758. real_srcrect.y = 0.0f;
  3759. real_srcrect.w = (float)texture->w;
  3760. real_srcrect.h = (float)texture->h;
  3761. if (srcrect) {
  3762. if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect)) {
  3763. return true;
  3764. }
  3765. }
  3766. SDL_FRect full_dstrect;
  3767. if (!dstrect) {
  3768. GetRenderViewportSize(renderer, &full_dstrect);
  3769. dstrect = &full_dstrect;
  3770. }
  3771. if (texture->native) {
  3772. texture = texture->native;
  3773. }
  3774. texture->last_command_generation = renderer->render_command_generation;
  3775. // See if we can use geometry with repeating texture coordinates
  3776. if (!renderer->software &&
  3777. (!srcrect ||
  3778. (real_srcrect.x == 0.0f && real_srcrect.y == 0.0f &&
  3779. real_srcrect.w == (float)texture->w && real_srcrect.h == (float)texture->h))) {
  3780. return SDL_RenderTextureTiled_Wrap(renderer, texture, &real_srcrect, scale, dstrect);
  3781. } else {
  3782. return SDL_RenderTextureTiled_Iterate(renderer, texture, &real_srcrect, scale, dstrect);
  3783. }
  3784. }
  3785. bool SDL_RenderTexture9Grid(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float left_width, float right_width, float top_height, float bottom_height, float scale, const SDL_FRect *dstrect)
  3786. {
  3787. SDL_FRect full_src, full_dst;
  3788. SDL_FRect curr_src, curr_dst;
  3789. float dst_left_width;
  3790. float dst_right_width;
  3791. float dst_top_height;
  3792. float dst_bottom_height;
  3793. CHECK_RENDERER_MAGIC(renderer, false);
  3794. CHECK_TEXTURE_MAGIC(texture, false);
  3795. if (renderer != texture->renderer) {
  3796. return SDL_SetError("Texture was not created with this renderer");
  3797. }
  3798. if (!srcrect) {
  3799. full_src.x = 0;
  3800. full_src.y = 0;
  3801. full_src.w = (float)texture->w;
  3802. full_src.h = (float)texture->h;
  3803. srcrect = &full_src;
  3804. }
  3805. if (!dstrect) {
  3806. GetRenderViewportSize(renderer, &full_dst);
  3807. dstrect = &full_dst;
  3808. }
  3809. if (scale <= 0.0f || scale == 1.0f) {
  3810. dst_left_width = SDL_ceilf(left_width);
  3811. dst_right_width = SDL_ceilf(right_width);
  3812. dst_top_height = SDL_ceilf(top_height);
  3813. dst_bottom_height = SDL_ceilf(bottom_height);
  3814. } else {
  3815. dst_left_width = SDL_ceilf(left_width * scale);
  3816. dst_right_width = SDL_ceilf(right_width * scale);
  3817. dst_top_height = SDL_ceilf(top_height * scale);
  3818. dst_bottom_height = SDL_ceilf(bottom_height * scale);
  3819. }
  3820. // Center
  3821. curr_src.x = srcrect->x + left_width;
  3822. curr_src.y = srcrect->y + top_height;
  3823. curr_src.w = srcrect->w - left_width - right_width;
  3824. curr_src.h = srcrect->h - top_height - bottom_height;
  3825. curr_dst.x = dstrect->x + dst_left_width;
  3826. curr_dst.y = dstrect->y + dst_top_height;
  3827. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  3828. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  3829. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3830. return false;
  3831. }
  3832. // Upper-left corner
  3833. curr_src.x = srcrect->x;
  3834. curr_src.y = srcrect->y;
  3835. curr_src.w = left_width;
  3836. curr_src.h = top_height;
  3837. curr_dst.x = dstrect->x;
  3838. curr_dst.y = dstrect->y;
  3839. curr_dst.w = dst_left_width;
  3840. curr_dst.h = dst_top_height;
  3841. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3842. return false;
  3843. }
  3844. // Upper-right corner
  3845. curr_src.x = srcrect->x + srcrect->w - right_width;
  3846. curr_src.w = right_width;
  3847. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  3848. curr_dst.w = dst_right_width;
  3849. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3850. return false;
  3851. }
  3852. // Lower-right corner
  3853. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  3854. curr_src.h = bottom_height;
  3855. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  3856. curr_dst.h = dst_bottom_height;
  3857. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3858. return false;
  3859. }
  3860. // Lower-left corner
  3861. curr_src.x = srcrect->x;
  3862. curr_src.w = left_width;
  3863. curr_dst.x = dstrect->x;
  3864. curr_dst.w = dst_left_width;
  3865. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3866. return false;
  3867. }
  3868. // Left
  3869. curr_src.y = srcrect->y + top_height;
  3870. curr_src.h = srcrect->h - top_height - bottom_height;
  3871. curr_dst.y = dstrect->y + dst_top_height;
  3872. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  3873. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3874. return false;
  3875. }
  3876. // Right
  3877. curr_src.x = srcrect->x + srcrect->w - right_width;
  3878. curr_src.w = right_width;
  3879. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  3880. curr_dst.w = dst_right_width;
  3881. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3882. return false;
  3883. }
  3884. // Top
  3885. curr_src.x = srcrect->x + left_width;
  3886. curr_src.y = srcrect->y;
  3887. curr_src.w = srcrect->w - left_width - right_width;
  3888. curr_src.h = top_height;
  3889. curr_dst.x = dstrect->x + dst_left_width;
  3890. curr_dst.y = dstrect->y;
  3891. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  3892. curr_dst.h = dst_top_height;
  3893. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3894. return false;
  3895. }
  3896. // Bottom
  3897. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  3898. curr_src.h = bottom_height;
  3899. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  3900. curr_dst.h = dst_bottom_height;
  3901. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3902. return false;
  3903. }
  3904. return true;
  3905. }
  3906. bool SDL_RenderTexture9GridTiled(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float left_width, float right_width, float top_height, float bottom_height, float scale, const SDL_FRect *dstrect, float tileScale)
  3907. {
  3908. SDL_FRect full_src, full_dst;
  3909. SDL_FRect curr_src, curr_dst;
  3910. float dst_left_width;
  3911. float dst_right_width;
  3912. float dst_top_height;
  3913. float dst_bottom_height;
  3914. CHECK_RENDERER_MAGIC(renderer, false);
  3915. CHECK_TEXTURE_MAGIC(texture, false);
  3916. if (renderer != texture->renderer) {
  3917. return SDL_SetError("Texture was not created with this renderer");
  3918. }
  3919. if (!srcrect) {
  3920. full_src.x = 0;
  3921. full_src.y = 0;
  3922. full_src.w = (float)texture->w;
  3923. full_src.h = (float)texture->h;
  3924. srcrect = &full_src;
  3925. }
  3926. if (!dstrect) {
  3927. GetRenderViewportSize(renderer, &full_dst);
  3928. dstrect = &full_dst;
  3929. }
  3930. if (scale <= 0.0f || scale == 1.0f) {
  3931. dst_left_width = SDL_ceilf(left_width);
  3932. dst_right_width = SDL_ceilf(right_width);
  3933. dst_top_height = SDL_ceilf(top_height);
  3934. dst_bottom_height = SDL_ceilf(bottom_height);
  3935. } else {
  3936. dst_left_width = SDL_ceilf(left_width * scale);
  3937. dst_right_width = SDL_ceilf(right_width * scale);
  3938. dst_top_height = SDL_ceilf(top_height * scale);
  3939. dst_bottom_height = SDL_ceilf(bottom_height * scale);
  3940. }
  3941. // Center
  3942. curr_src.x = srcrect->x + left_width;
  3943. curr_src.y = srcrect->y + top_height;
  3944. curr_src.w = srcrect->w - left_width - right_width;
  3945. curr_src.h = srcrect->h - top_height - bottom_height;
  3946. curr_dst.x = dstrect->x + dst_left_width;
  3947. curr_dst.y = dstrect->y + dst_top_height;
  3948. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  3949. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  3950. if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
  3951. return false;
  3952. }
  3953. // Upper-left corner
  3954. curr_src.x = srcrect->x;
  3955. curr_src.y = srcrect->y;
  3956. curr_src.w = left_width;
  3957. curr_src.h = top_height;
  3958. curr_dst.x = dstrect->x;
  3959. curr_dst.y = dstrect->y;
  3960. curr_dst.w = dst_left_width;
  3961. curr_dst.h = dst_top_height;
  3962. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3963. return false;
  3964. }
  3965. // Upper-right corner
  3966. curr_src.x = srcrect->x + srcrect->w - right_width;
  3967. curr_src.w = right_width;
  3968. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  3969. curr_dst.w = dst_right_width;
  3970. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3971. return false;
  3972. }
  3973. // Lower-right corner
  3974. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  3975. curr_src.h = bottom_height;
  3976. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  3977. curr_dst.h = dst_bottom_height;
  3978. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3979. return false;
  3980. }
  3981. // Lower-left corner
  3982. curr_src.x = srcrect->x;
  3983. curr_src.w = left_width;
  3984. curr_dst.x = dstrect->x;
  3985. curr_dst.w = dst_left_width;
  3986. if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
  3987. return false;
  3988. }
  3989. // Left
  3990. curr_src.y = srcrect->y + top_height;
  3991. curr_src.h = srcrect->h - top_height - bottom_height;
  3992. curr_dst.y = dstrect->y + dst_top_height;
  3993. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  3994. if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
  3995. return false;
  3996. }
  3997. // Right
  3998. curr_src.x = srcrect->x + srcrect->w - right_width;
  3999. curr_src.w = right_width;
  4000. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  4001. curr_dst.w = dst_right_width;
  4002. if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
  4003. return false;
  4004. }
  4005. // Top
  4006. curr_src.x = srcrect->x + left_width;
  4007. curr_src.y = srcrect->y;
  4008. curr_src.w = srcrect->w - left_width - right_width;
  4009. curr_src.h = top_height;
  4010. curr_dst.x = dstrect->x + dst_left_width;
  4011. curr_dst.y = dstrect->y;
  4012. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  4013. curr_dst.h = dst_top_height;
  4014. if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
  4015. return false;
  4016. }
  4017. // Bottom
  4018. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  4019. curr_src.h = bottom_height;
  4020. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  4021. curr_dst.h = dst_bottom_height;
  4022. if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
  4023. return false;
  4024. }
  4025. return true;
  4026. }
  4027. bool SDL_RenderGeometry(SDL_Renderer *renderer,
  4028. SDL_Texture *texture,
  4029. const SDL_Vertex *vertices, int num_vertices,
  4030. const int *indices, int num_indices)
  4031. {
  4032. if (vertices) {
  4033. const float *xy = &vertices->position.x;
  4034. int xy_stride = sizeof(SDL_Vertex);
  4035. const SDL_FColor *color = &vertices->color;
  4036. int color_stride = sizeof(SDL_Vertex);
  4037. const float *uv = &vertices->tex_coord.x;
  4038. int uv_stride = sizeof(SDL_Vertex);
  4039. int size_indices = 4;
  4040. return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indices);
  4041. } else {
  4042. return SDL_InvalidParamError("vertices");
  4043. }
  4044. }
  4045. #ifdef SDL_VIDEO_RENDER_SW
  4046. static int remap_one_indice(
  4047. int prev,
  4048. int k,
  4049. SDL_Texture *texture,
  4050. const float *xy, int xy_stride,
  4051. const SDL_FColor *color, int color_stride,
  4052. const float *uv, int uv_stride)
  4053. {
  4054. const float *xy0_, *xy1_, *uv0_, *uv1_;
  4055. const SDL_FColor *col0_, *col1_;
  4056. xy0_ = (const float *)((const char *)xy + prev * xy_stride);
  4057. xy1_ = (const float *)((const char *)xy + k * xy_stride);
  4058. if (xy0_[0] != xy1_[0]) {
  4059. return k;
  4060. }
  4061. if (xy0_[1] != xy1_[1]) {
  4062. return k;
  4063. }
  4064. if (texture) {
  4065. uv0_ = (const float *)((const char *)uv + prev * uv_stride);
  4066. uv1_ = (const float *)((const char *)uv + k * uv_stride);
  4067. if (uv0_[0] != uv1_[0]) {
  4068. return k;
  4069. }
  4070. if (uv0_[1] != uv1_[1]) {
  4071. return k;
  4072. }
  4073. }
  4074. col0_ = (const SDL_FColor *)((const char *)color + prev * color_stride);
  4075. col1_ = (const SDL_FColor *)((const char *)color + k * color_stride);
  4076. if (SDL_memcmp(col0_, col1_, sizeof(*col0_)) != 0) {
  4077. return k;
  4078. }
  4079. return prev;
  4080. }
  4081. static int remap_indices(
  4082. int prev[3],
  4083. int k,
  4084. SDL_Texture *texture,
  4085. const float *xy, int xy_stride,
  4086. const SDL_FColor *color, int color_stride,
  4087. const float *uv, int uv_stride)
  4088. {
  4089. int i;
  4090. if (prev[0] == -1) {
  4091. return k;
  4092. }
  4093. for (i = 0; i < 3; i++) {
  4094. int new_k = remap_one_indice(prev[i], k, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  4095. if (new_k != k) {
  4096. return new_k;
  4097. }
  4098. }
  4099. return k;
  4100. }
  4101. #define DEBUG_SW_RENDER_GEOMETRY 0
  4102. // For the software renderer, try to reinterpret triangles as SDL_Rect
  4103. static bool SDLCALL SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
  4104. SDL_Texture *texture,
  4105. const float *xy, int xy_stride,
  4106. const SDL_FColor *color, int color_stride,
  4107. const float *uv, int uv_stride,
  4108. int num_vertices,
  4109. const void *indices, int num_indices, int size_indices)
  4110. {
  4111. int i;
  4112. bool result = true;
  4113. int count = indices ? num_indices : num_vertices;
  4114. int prev[3]; // Previous triangle vertex indices
  4115. float texw = 0.0f, texh = 0.0f;
  4116. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  4117. float r = 0, g = 0, b = 0, a = 0;
  4118. const SDL_RenderViewState *view = renderer->view;
  4119. const float scale_x = view->current_scale.x;
  4120. const float scale_y = view->current_scale.y;
  4121. // Save
  4122. SDL_GetRenderDrawBlendMode(renderer, &blendMode);
  4123. SDL_GetRenderDrawColorFloat(renderer, &r, &g, &b, &a);
  4124. if (texture) {
  4125. SDL_GetTextureSize(texture, &texw, &texh);
  4126. }
  4127. prev[0] = -1;
  4128. prev[1] = -1;
  4129. prev[2] = -1;
  4130. size_indices = indices ? size_indices : 0;
  4131. for (i = 0; i < count; i += 3) {
  4132. int k0, k1, k2; // Current triangle indices
  4133. int is_quad = 1;
  4134. #if DEBUG_SW_RENDER_GEOMETRY
  4135. int is_uniform = 1;
  4136. int is_rectangle = 1;
  4137. #endif
  4138. int A = -1; // Top left vertex
  4139. int B = -1; // Bottom right vertex
  4140. int C = -1; // Third vertex of current triangle
  4141. int C2 = -1; // Last, vertex of previous triangle
  4142. if (size_indices == 4) {
  4143. k0 = ((const Uint32 *)indices)[i];
  4144. k1 = ((const Uint32 *)indices)[i + 1];
  4145. k2 = ((const Uint32 *)indices)[i + 2];
  4146. } else if (size_indices == 2) {
  4147. k0 = ((const Uint16 *)indices)[i];
  4148. k1 = ((const Uint16 *)indices)[i + 1];
  4149. k2 = ((const Uint16 *)indices)[i + 2];
  4150. } else if (size_indices == 1) {
  4151. k0 = ((const Uint8 *)indices)[i];
  4152. k1 = ((const Uint8 *)indices)[i + 1];
  4153. k2 = ((const Uint8 *)indices)[i + 2];
  4154. } else {
  4155. /* Vertices were not provided by indices. Maybe some are duplicated.
  4156. * We try to indentificate the duplicates by comparing with the previous three vertices */
  4157. k0 = remap_indices(prev, i, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  4158. k1 = remap_indices(prev, i + 1, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  4159. k2 = remap_indices(prev, i + 2, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  4160. }
  4161. if (prev[0] == -1) {
  4162. prev[0] = k0;
  4163. prev[1] = k1;
  4164. prev[2] = k2;
  4165. continue;
  4166. }
  4167. /* Two triangles forming a quadialateral,
  4168. * prev and current triangles must have exactly 2 common vertices */
  4169. {
  4170. int cnt = 0, j = 3;
  4171. while (j--) {
  4172. int p = prev[j];
  4173. if (p == k0 || p == k1 || p == k2) {
  4174. cnt++;
  4175. }
  4176. }
  4177. is_quad = (cnt == 2);
  4178. }
  4179. // Identify vertices
  4180. if (is_quad) {
  4181. const float *xy0_, *xy1_, *xy2_;
  4182. float x0, x1, x2;
  4183. float y0, y1, y2;
  4184. xy0_ = (const float *)((const char *)xy + k0 * xy_stride);
  4185. xy1_ = (const float *)((const char *)xy + k1 * xy_stride);
  4186. xy2_ = (const float *)((const char *)xy + k2 * xy_stride);
  4187. x0 = xy0_[0];
  4188. y0 = xy0_[1];
  4189. x1 = xy1_[0];
  4190. y1 = xy1_[1];
  4191. x2 = xy2_[0];
  4192. y2 = xy2_[1];
  4193. // Find top-left
  4194. if (x0 <= x1 && y0 <= y1) {
  4195. if (x0 <= x2 && y0 <= y2) {
  4196. A = k0;
  4197. } else {
  4198. A = k2;
  4199. }
  4200. } else {
  4201. if (x1 <= x2 && y1 <= y2) {
  4202. A = k1;
  4203. } else {
  4204. A = k2;
  4205. }
  4206. }
  4207. // Find bottom-right
  4208. if (x0 >= x1 && y0 >= y1) {
  4209. if (x0 >= x2 && y0 >= y2) {
  4210. B = k0;
  4211. } else {
  4212. B = k2;
  4213. }
  4214. } else {
  4215. if (x1 >= x2 && y1 >= y2) {
  4216. B = k1;
  4217. } else {
  4218. B = k2;
  4219. }
  4220. }
  4221. // Find C
  4222. if (k0 != A && k0 != B) {
  4223. C = k0;
  4224. } else if (k1 != A && k1 != B) {
  4225. C = k1;
  4226. } else {
  4227. C = k2;
  4228. }
  4229. // Find C2
  4230. if (prev[0] != A && prev[0] != B) {
  4231. C2 = prev[0];
  4232. } else if (prev[1] != A && prev[1] != B) {
  4233. C2 = prev[1];
  4234. } else {
  4235. C2 = prev[2];
  4236. }
  4237. xy0_ = (const float *)((const char *)xy + A * xy_stride);
  4238. xy1_ = (const float *)((const char *)xy + B * xy_stride);
  4239. xy2_ = (const float *)((const char *)xy + C * xy_stride);
  4240. x0 = xy0_[0];
  4241. y0 = xy0_[1];
  4242. x1 = xy1_[0];
  4243. y1 = xy1_[1];
  4244. x2 = xy2_[0];
  4245. y2 = xy2_[1];
  4246. // Check if triangle A B C is rectangle
  4247. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)) {
  4248. // ok
  4249. } else {
  4250. is_quad = 0;
  4251. #if DEBUG_SW_RENDER_GEOMETRY
  4252. is_rectangle = 0;
  4253. #endif
  4254. }
  4255. xy2_ = (const float *)((const char *)xy + C2 * xy_stride);
  4256. x2 = xy2_[0];
  4257. y2 = xy2_[1];
  4258. // Check if triangle A B C2 is rectangle
  4259. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)) {
  4260. // ok
  4261. } else {
  4262. is_quad = 0;
  4263. #if DEBUG_SW_RENDER_GEOMETRY
  4264. is_rectangle = 0;
  4265. #endif
  4266. }
  4267. }
  4268. // Check if uniformly colored
  4269. if (is_quad) {
  4270. const SDL_FColor *col0_ = (const SDL_FColor *)((const char *)color + A * color_stride);
  4271. const SDL_FColor *col1_ = (const SDL_FColor *)((const char *)color + B * color_stride);
  4272. const SDL_FColor *col2_ = (const SDL_FColor *)((const char *)color + C * color_stride);
  4273. const SDL_FColor *col3_ = (const SDL_FColor *)((const char *)color + C2 * color_stride);
  4274. if (SDL_memcmp(col0_, col1_, sizeof(*col0_)) == 0 &&
  4275. SDL_memcmp(col0_, col2_, sizeof(*col0_)) == 0 &&
  4276. SDL_memcmp(col0_, col3_, sizeof(*col0_)) == 0) {
  4277. // ok
  4278. } else {
  4279. is_quad = 0;
  4280. #if DEBUG_SW_RENDER_GEOMETRY
  4281. is_uniform = 0;
  4282. #endif
  4283. }
  4284. }
  4285. // Start rendering rect
  4286. if (is_quad) {
  4287. SDL_FRect s;
  4288. SDL_FRect d;
  4289. const float *xy0_, *xy1_, *uv0_, *uv1_;
  4290. const SDL_FColor *col0_ = (const SDL_FColor *)((const char *)color + k0 * color_stride);
  4291. xy0_ = (const float *)((const char *)xy + A * xy_stride);
  4292. xy1_ = (const float *)((const char *)xy + B * xy_stride);
  4293. if (texture) {
  4294. uv0_ = (const float *)((const char *)uv + A * uv_stride);
  4295. uv1_ = (const float *)((const char *)uv + B * uv_stride);
  4296. s.x = uv0_[0] * texw;
  4297. s.y = uv0_[1] * texh;
  4298. s.w = uv1_[0] * texw - s.x;
  4299. s.h = uv1_[1] * texh - s.y;
  4300. } else {
  4301. s.x = s.y = s.w = s.h = 0;
  4302. }
  4303. d.x = xy0_[0];
  4304. d.y = xy0_[1];
  4305. d.w = xy1_[0] - d.x;
  4306. d.h = xy1_[1] - d.y;
  4307. // Rect + texture
  4308. if (texture && s.w != 0 && s.h != 0) {
  4309. SDL_SetTextureAlphaModFloat(texture, col0_->a);
  4310. SDL_SetTextureColorModFloat(texture, col0_->r, col0_->g, col0_->b);
  4311. if (s.w > 0 && s.h > 0) {
  4312. SDL_RenderTexture(renderer, texture, &s, &d);
  4313. } else {
  4314. int flags = 0;
  4315. if (s.w < 0) {
  4316. flags |= SDL_FLIP_HORIZONTAL;
  4317. s.w *= -1;
  4318. s.x -= s.w;
  4319. }
  4320. if (s.h < 0) {
  4321. flags |= SDL_FLIP_VERTICAL;
  4322. s.h *= -1;
  4323. s.y -= s.h;
  4324. }
  4325. SDL_RenderTextureRotated(renderer, texture, &s, &d, 0, NULL, (SDL_FlipMode)flags);
  4326. }
  4327. #if DEBUG_SW_RENDER_GEOMETRY
  4328. SDL_Log("Rect-COPY: RGB %f %f %f - Alpha:%f - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_->r, col0_->g, col0_->b, col0_->a,
  4329. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  4330. #endif
  4331. } else if (d.w != 0.0f && d.h != 0.0f) { // Rect, no texture
  4332. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  4333. SDL_SetRenderDrawColorFloat(renderer, col0_->r, col0_->g, col0_->b, col0_->a);
  4334. SDL_RenderFillRect(renderer, &d);
  4335. #if DEBUG_SW_RENDER_GEOMETRY
  4336. SDL_Log("Rect-FILL: RGB %f %f %f - Alpha:%f - texture=%p: dst (%f, %f, %f x %f)", col0_->r, col0_->g, col0_->b, col0_->a,
  4337. (void *)texture, d.x, d.y, d.w, d.h);
  4338. } else {
  4339. SDL_Log("Rect-DISMISS: RGB %f %f %f - Alpha:%f - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_->r, col0_->g, col0_->b, col0_->a,
  4340. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  4341. #endif
  4342. }
  4343. prev[0] = -1;
  4344. } else {
  4345. // Render triangles
  4346. if (prev[0] != -1) {
  4347. #if DEBUG_SW_RENDER_GEOMETRY
  4348. SDL_Log("Triangle %d %d %d - is_uniform:%d is_rectangle:%d", prev[0], prev[1], prev[2], is_uniform, is_rectangle);
  4349. #endif
  4350. result = QueueCmdGeometry(renderer, texture,
  4351. xy, xy_stride, color, color_stride, uv, uv_stride,
  4352. num_vertices, prev, 3, 4,
  4353. scale_x, scale_y, SDL_TEXTURE_ADDRESS_CLAMP);
  4354. if (!result) {
  4355. goto end;
  4356. }
  4357. }
  4358. prev[0] = k0;
  4359. prev[1] = k1;
  4360. prev[2] = k2;
  4361. }
  4362. } // End for (), next triangle
  4363. if (prev[0] != -1) {
  4364. // flush the last triangle
  4365. #if DEBUG_SW_RENDER_GEOMETRY
  4366. SDL_Log("Last triangle %d %d %d", prev[0], prev[1], prev[2]);
  4367. #endif
  4368. result = QueueCmdGeometry(renderer, texture,
  4369. xy, xy_stride, color, color_stride, uv, uv_stride,
  4370. num_vertices, prev, 3, 4,
  4371. scale_x, scale_y, SDL_TEXTURE_ADDRESS_CLAMP);
  4372. if (!result) {
  4373. goto end;
  4374. }
  4375. }
  4376. end:
  4377. // Restore
  4378. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  4379. SDL_SetRenderDrawColorFloat(renderer, r, g, b, a);
  4380. return result;
  4381. }
  4382. #endif // SDL_VIDEO_RENDER_SW
  4383. bool SDL_RenderGeometryRaw(SDL_Renderer *renderer,
  4384. SDL_Texture *texture,
  4385. const float *xy, int xy_stride,
  4386. const SDL_FColor *color, int color_stride,
  4387. const float *uv, int uv_stride,
  4388. int num_vertices,
  4389. const void *indices, int num_indices, int size_indices)
  4390. {
  4391. int i;
  4392. int count = indices ? num_indices : num_vertices;
  4393. SDL_TextureAddressMode texture_address_mode;
  4394. CHECK_RENDERER_MAGIC(renderer, false);
  4395. if (!renderer->QueueGeometry) {
  4396. return SDL_Unsupported();
  4397. }
  4398. if (texture) {
  4399. CHECK_TEXTURE_MAGIC(texture, false);
  4400. if (renderer != texture->renderer) {
  4401. return SDL_SetError("Texture was not created with this renderer");
  4402. }
  4403. }
  4404. if (!xy) {
  4405. return SDL_InvalidParamError("xy");
  4406. }
  4407. if (!color) {
  4408. return SDL_InvalidParamError("color");
  4409. }
  4410. if (texture && !uv) {
  4411. return SDL_InvalidParamError("uv");
  4412. }
  4413. if (count % 3 != 0) {
  4414. return SDL_InvalidParamError(indices ? "num_indices" : "num_vertices");
  4415. }
  4416. if (indices) {
  4417. if (size_indices != 1 && size_indices != 2 && size_indices != 4) {
  4418. return SDL_InvalidParamError("size_indices");
  4419. }
  4420. } else {
  4421. size_indices = 0;
  4422. }
  4423. #if DONT_DRAW_WHILE_HIDDEN
  4424. // Don't draw while we're hidden
  4425. if (renderer->hidden) {
  4426. return true;
  4427. }
  4428. #endif
  4429. if (num_vertices < 3) {
  4430. return true;
  4431. }
  4432. if (texture && texture->native) {
  4433. texture = texture->native;
  4434. }
  4435. texture_address_mode = renderer->texture_address_mode;
  4436. if (texture_address_mode == SDL_TEXTURE_ADDRESS_AUTO && texture) {
  4437. texture_address_mode = SDL_TEXTURE_ADDRESS_CLAMP;
  4438. for (i = 0; i < num_vertices; ++i) {
  4439. const float *uv_ = (const float *)((const char *)uv + i * uv_stride);
  4440. float u = uv_[0];
  4441. float v = uv_[1];
  4442. if (u < 0.0f || v < 0.0f || u > 1.0f || v > 1.0f) {
  4443. texture_address_mode = SDL_TEXTURE_ADDRESS_WRAP;
  4444. break;
  4445. }
  4446. }
  4447. }
  4448. if (indices) {
  4449. for (i = 0; i < num_indices; ++i) {
  4450. int j;
  4451. if (size_indices == 4) {
  4452. j = ((const Uint32 *)indices)[i];
  4453. } else if (size_indices == 2) {
  4454. j = ((const Uint16 *)indices)[i];
  4455. } else {
  4456. j = ((const Uint8 *)indices)[i];
  4457. }
  4458. if (j < 0 || j >= num_vertices) {
  4459. return SDL_SetError("Values of 'indices' out of bounds");
  4460. }
  4461. }
  4462. }
  4463. if (texture) {
  4464. texture->last_command_generation = renderer->render_command_generation;
  4465. }
  4466. // For the software renderer, try to reinterpret triangles as SDL_Rect
  4467. #ifdef SDL_VIDEO_RENDER_SW
  4468. if (renderer->software && texture_address_mode == SDL_TEXTURE_ADDRESS_CLAMP) {
  4469. return SDL_SW_RenderGeometryRaw(renderer, texture,
  4470. xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices,
  4471. indices, num_indices, size_indices);
  4472. }
  4473. #endif
  4474. const SDL_RenderViewState *view = renderer->view;
  4475. return QueueCmdGeometry(renderer, texture,
  4476. xy, xy_stride, color, color_stride, uv, uv_stride,
  4477. num_vertices, indices, num_indices, size_indices,
  4478. view->current_scale.x, view->current_scale.y,
  4479. texture_address_mode);
  4480. }
  4481. SDL_Surface *SDL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect)
  4482. {
  4483. CHECK_RENDERER_MAGIC(renderer, NULL);
  4484. if (!renderer->RenderReadPixels) {
  4485. SDL_Unsupported();
  4486. return NULL;
  4487. }
  4488. FlushRenderCommands(renderer); // we need to render before we read the results.
  4489. SDL_Rect real_rect = renderer->view->pixel_viewport;
  4490. if (rect) {
  4491. if (!SDL_GetRectIntersection(rect, &real_rect, &real_rect)) {
  4492. SDL_SetError("Can't read outside the current viewport");
  4493. return NULL;
  4494. }
  4495. }
  4496. SDL_Surface *surface = renderer->RenderReadPixels(renderer, &real_rect);
  4497. if (surface) {
  4498. SDL_PropertiesID props = SDL_GetSurfaceProperties(surface);
  4499. if (renderer->target) {
  4500. SDL_Texture *target = renderer->target;
  4501. SDL_Texture *parent = SDL_GetPointerProperty(SDL_GetTextureProperties(target), SDL_PROP_TEXTURE_PARENT_POINTER, NULL);
  4502. SDL_PixelFormat expected_format = (parent ? parent->format : target->format);
  4503. SDL_SetFloatProperty(props, SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT, target->SDR_white_point);
  4504. SDL_SetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, target->HDR_headroom);
  4505. // Set the expected surface format
  4506. if ((surface->format == SDL_PIXELFORMAT_ARGB8888 && expected_format == SDL_PIXELFORMAT_XRGB8888) ||
  4507. (surface->format == SDL_PIXELFORMAT_RGBA8888 && expected_format == SDL_PIXELFORMAT_RGBX8888) ||
  4508. (surface->format == SDL_PIXELFORMAT_ABGR8888 && expected_format == SDL_PIXELFORMAT_XBGR8888) ||
  4509. (surface->format == SDL_PIXELFORMAT_BGRA8888 && expected_format == SDL_PIXELFORMAT_BGRX8888)) {
  4510. surface->format = expected_format;
  4511. surface->fmt = SDL_GetPixelFormatDetails(expected_format);
  4512. }
  4513. } else {
  4514. SDL_SetFloatProperty(props, SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT, renderer->SDR_white_point);
  4515. SDL_SetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, renderer->HDR_headroom);
  4516. }
  4517. }
  4518. return surface;
  4519. }
  4520. static void SDL_RenderApplyWindowShape(SDL_Renderer *renderer)
  4521. {
  4522. SDL_Surface *shape = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(renderer->window), SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
  4523. if (shape != renderer->shape_surface) {
  4524. if (renderer->shape_texture) {
  4525. SDL_DestroyTexture(renderer->shape_texture);
  4526. renderer->shape_texture = NULL;
  4527. }
  4528. if (shape) {
  4529. // There's nothing we can do if this fails, so just keep on going
  4530. renderer->shape_texture = SDL_CreateTextureFromSurface(renderer, shape);
  4531. SDL_SetTextureBlendMode(renderer->shape_texture,
  4532. SDL_ComposeCustomBlendMode(
  4533. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
  4534. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD));
  4535. }
  4536. renderer->shape_surface = shape;
  4537. }
  4538. if (renderer->shape_texture) {
  4539. SDL_RenderTexture(renderer, renderer->shape_texture, NULL, NULL);
  4540. }
  4541. }
  4542. static void SDL_SimulateRenderVSync(SDL_Renderer *renderer)
  4543. {
  4544. Uint64 now, elapsed;
  4545. const Uint64 interval = renderer->simulate_vsync_interval_ns;
  4546. if (!interval) {
  4547. // We can't do sub-ns delay, so just return here
  4548. return;
  4549. }
  4550. now = SDL_GetTicksNS();
  4551. elapsed = (now - renderer->last_present);
  4552. if (elapsed < interval) {
  4553. Uint64 duration = (interval - elapsed);
  4554. SDL_DelayPrecise(duration);
  4555. now = SDL_GetTicksNS();
  4556. }
  4557. elapsed = (now - renderer->last_present);
  4558. if (!renderer->last_present || elapsed > SDL_MS_TO_NS(1000)) {
  4559. // It's been too long, reset the presentation timeline
  4560. renderer->last_present = now;
  4561. } else {
  4562. renderer->last_present += (elapsed / interval) * interval;
  4563. }
  4564. }
  4565. bool SDL_RenderPresent(SDL_Renderer *renderer)
  4566. {
  4567. bool presented = true;
  4568. CHECK_RENDERER_MAGIC(renderer, false);
  4569. if (renderer->target) {
  4570. return SDL_SetError("You can't present on a render target");
  4571. }
  4572. SDL_RenderLogicalPresentation(renderer);
  4573. if (renderer->transparent_window) {
  4574. SDL_RenderApplyWindowShape(renderer);
  4575. }
  4576. FlushRenderCommands(renderer); // time to send everything to the GPU!
  4577. #if DONT_DRAW_WHILE_HIDDEN
  4578. // Don't present while we're hidden
  4579. if (renderer->hidden) {
  4580. presented = false;
  4581. } else
  4582. #endif
  4583. if (!renderer->RenderPresent(renderer)) {
  4584. presented = false;
  4585. }
  4586. if (renderer->simulate_vsync ||
  4587. (!presented && renderer->wanted_vsync)) {
  4588. SDL_SimulateRenderVSync(renderer);
  4589. }
  4590. return true;
  4591. }
  4592. static void SDL_DestroyTextureInternal(SDL_Texture *texture, bool is_destroying)
  4593. {
  4594. SDL_Renderer *renderer;
  4595. SDL_DestroyProperties(texture->props);
  4596. renderer = texture->renderer;
  4597. if (is_destroying) {
  4598. // Renderer get destroyed, avoid to queue more commands
  4599. } else {
  4600. if (texture == renderer->target) {
  4601. SDL_SetRenderTarget(renderer, NULL); // implies command queue flush
  4602. } else {
  4603. FlushRenderCommandsIfTextureNeeded(texture);
  4604. }
  4605. }
  4606. SDL_SetObjectValid(texture, SDL_OBJECT_TYPE_TEXTURE, false);
  4607. if (texture->next) {
  4608. texture->next->prev = texture->prev;
  4609. }
  4610. if (texture->prev) {
  4611. texture->prev->next = texture->next;
  4612. } else {
  4613. renderer->textures = texture->next;
  4614. }
  4615. if (texture->native) {
  4616. SDL_DestroyTextureInternal(texture->native, is_destroying);
  4617. }
  4618. #ifdef SDL_HAVE_YUV
  4619. if (texture->yuv) {
  4620. SDL_SW_DestroyYUVTexture(texture->yuv);
  4621. }
  4622. #endif
  4623. SDL_free(texture->pixels);
  4624. renderer->DestroyTexture(renderer, texture);
  4625. SDL_DestroySurface(texture->locked_surface);
  4626. texture->locked_surface = NULL;
  4627. SDL_free(texture);
  4628. }
  4629. void SDL_DestroyTexture(SDL_Texture *texture)
  4630. {
  4631. CHECK_TEXTURE_MAGIC(texture, );
  4632. if (--texture->refcount > 0) {
  4633. return;
  4634. }
  4635. SDL_DestroyTextureInternal(texture, false /* is_destroying */);
  4636. }
  4637. static void SDL_DiscardAllCommands(SDL_Renderer *renderer)
  4638. {
  4639. SDL_RenderCommand *cmd;
  4640. if (renderer->render_commands_tail) {
  4641. renderer->render_commands_tail->next = renderer->render_commands_pool;
  4642. cmd = renderer->render_commands;
  4643. } else {
  4644. cmd = renderer->render_commands_pool;
  4645. }
  4646. renderer->render_commands_pool = NULL;
  4647. renderer->render_commands_tail = NULL;
  4648. renderer->render_commands = NULL;
  4649. renderer->vertex_data_used = 0;
  4650. while (cmd) {
  4651. SDL_RenderCommand *next = cmd->next;
  4652. SDL_free(cmd);
  4653. cmd = next;
  4654. }
  4655. }
  4656. void SDL_DestroyRendererWithoutFreeing(SDL_Renderer *renderer)
  4657. {
  4658. SDL_assert(renderer != NULL);
  4659. SDL_assert(!renderer->destroyed);
  4660. renderer->destroyed = true;
  4661. SDL_RemoveWindowEventWatch(SDL_WINDOW_EVENT_WATCH_NORMAL, SDL_RendererEventWatch, renderer);
  4662. if (renderer->window) {
  4663. SDL_PropertiesID props = SDL_GetWindowProperties(renderer->window);
  4664. if (SDL_GetPointerProperty(props, SDL_PROP_WINDOW_RENDERER_POINTER, NULL) == renderer) {
  4665. SDL_ClearProperty(props, SDL_PROP_WINDOW_RENDERER_POINTER);
  4666. }
  4667. SDL_RemoveWindowRenderer(renderer->window, renderer);
  4668. }
  4669. if (renderer->software) {
  4670. // Make sure all drawing to a surface is complete
  4671. FlushRenderCommands(renderer);
  4672. }
  4673. SDL_DiscardAllCommands(renderer);
  4674. if (renderer->debug_char_texture_atlas) {
  4675. SDL_DestroyTexture(renderer->debug_char_texture_atlas);
  4676. renderer->debug_char_texture_atlas = NULL;
  4677. }
  4678. // Free existing textures for this renderer
  4679. while (renderer->textures) {
  4680. SDL_Texture *tex = renderer->textures;
  4681. SDL_DestroyTextureInternal(renderer->textures, true /* is_destroying */);
  4682. SDL_assert(tex != renderer->textures); // satisfy static analysis.
  4683. }
  4684. // Clean up renderer-specific resources
  4685. if (renderer->DestroyRenderer) {
  4686. renderer->DestroyRenderer(renderer);
  4687. }
  4688. if (renderer->target_mutex) {
  4689. SDL_DestroyMutex(renderer->target_mutex);
  4690. renderer->target_mutex = NULL;
  4691. }
  4692. if (renderer->vertex_data) {
  4693. SDL_free(renderer->vertex_data);
  4694. renderer->vertex_data = NULL;
  4695. }
  4696. if (renderer->texture_formats) {
  4697. SDL_free(renderer->texture_formats);
  4698. renderer->texture_formats = NULL;
  4699. }
  4700. if (renderer->props) {
  4701. SDL_DestroyProperties(renderer->props);
  4702. renderer->props = 0;
  4703. }
  4704. }
  4705. void SDL_DestroyRenderer(SDL_Renderer *renderer)
  4706. {
  4707. CHECK_RENDERER_MAGIC_BUT_NOT_DESTROYED_FLAG(renderer,);
  4708. // if we've already destroyed the renderer through SDL_DestroyWindow, we just need
  4709. // to free the renderer pointer. This lets apps destroy the window and renderer
  4710. // in either order.
  4711. if (!renderer->destroyed) {
  4712. SDL_DestroyRendererWithoutFreeing(renderer);
  4713. }
  4714. SDL_Renderer *curr = SDL_renderers;
  4715. SDL_Renderer *prev = NULL;
  4716. while (curr) {
  4717. if (curr == renderer) {
  4718. if (prev) {
  4719. prev->next = renderer->next;
  4720. } else {
  4721. SDL_renderers = renderer->next;
  4722. }
  4723. break;
  4724. }
  4725. prev = curr;
  4726. curr = curr->next;
  4727. }
  4728. SDL_SetObjectValid(renderer, SDL_OBJECT_TYPE_RENDERER, false); // It's no longer magical...
  4729. SDL_free(renderer);
  4730. }
  4731. void *SDL_GetRenderMetalLayer(SDL_Renderer *renderer)
  4732. {
  4733. CHECK_RENDERER_MAGIC(renderer, NULL);
  4734. if (renderer->GetMetalLayer) {
  4735. FlushRenderCommands(renderer); // in case the app is going to mess with it.
  4736. return renderer->GetMetalLayer(renderer);
  4737. }
  4738. return NULL;
  4739. }
  4740. void *SDL_GetRenderMetalCommandEncoder(SDL_Renderer *renderer)
  4741. {
  4742. CHECK_RENDERER_MAGIC(renderer, NULL);
  4743. if (renderer->GetMetalCommandEncoder) {
  4744. FlushRenderCommands(renderer); // in case the app is going to mess with it.
  4745. return renderer->GetMetalCommandEncoder(renderer);
  4746. }
  4747. return NULL;
  4748. }
  4749. bool SDL_AddVulkanRenderSemaphores(SDL_Renderer *renderer, Uint32 wait_stage_mask, Sint64 wait_semaphore, Sint64 signal_semaphore)
  4750. {
  4751. CHECK_RENDERER_MAGIC(renderer, false);
  4752. if (!renderer->AddVulkanRenderSemaphores) {
  4753. return SDL_Unsupported();
  4754. }
  4755. return renderer->AddVulkanRenderSemaphores(renderer, wait_stage_mask, wait_semaphore, signal_semaphore);
  4756. }
  4757. static SDL_BlendMode SDL_GetShortBlendMode(SDL_BlendMode blendMode)
  4758. {
  4759. if (blendMode == SDL_BLENDMODE_NONE_FULL) {
  4760. return SDL_BLENDMODE_NONE;
  4761. }
  4762. if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
  4763. return SDL_BLENDMODE_BLEND;
  4764. }
  4765. if (blendMode == SDL_BLENDMODE_BLEND_PREMULTIPLIED_FULL) {
  4766. return SDL_BLENDMODE_BLEND_PREMULTIPLIED;
  4767. }
  4768. if (blendMode == SDL_BLENDMODE_ADD_FULL) {
  4769. return SDL_BLENDMODE_ADD;
  4770. }
  4771. if (blendMode == SDL_BLENDMODE_ADD_PREMULTIPLIED_FULL) {
  4772. return SDL_BLENDMODE_ADD_PREMULTIPLIED;
  4773. }
  4774. if (blendMode == SDL_BLENDMODE_MOD_FULL) {
  4775. return SDL_BLENDMODE_MOD;
  4776. }
  4777. if (blendMode == SDL_BLENDMODE_MUL_FULL) {
  4778. return SDL_BLENDMODE_MUL;
  4779. }
  4780. return blendMode;
  4781. }
  4782. static SDL_BlendMode SDL_GetLongBlendMode(SDL_BlendMode blendMode)
  4783. {
  4784. if (blendMode == SDL_BLENDMODE_NONE) {
  4785. return SDL_BLENDMODE_NONE_FULL;
  4786. }
  4787. if (blendMode == SDL_BLENDMODE_BLEND) {
  4788. return SDL_BLENDMODE_BLEND_FULL;
  4789. }
  4790. if (blendMode == SDL_BLENDMODE_BLEND_PREMULTIPLIED) {
  4791. return SDL_BLENDMODE_BLEND_PREMULTIPLIED_FULL;
  4792. }
  4793. if (blendMode == SDL_BLENDMODE_ADD) {
  4794. return SDL_BLENDMODE_ADD_FULL;
  4795. }
  4796. if (blendMode == SDL_BLENDMODE_ADD_PREMULTIPLIED) {
  4797. return SDL_BLENDMODE_ADD_PREMULTIPLIED_FULL;
  4798. }
  4799. if (blendMode == SDL_BLENDMODE_MOD) {
  4800. return SDL_BLENDMODE_MOD_FULL;
  4801. }
  4802. if (blendMode == SDL_BLENDMODE_MUL) {
  4803. return SDL_BLENDMODE_MUL_FULL;
  4804. }
  4805. return blendMode;
  4806. }
  4807. SDL_BlendMode SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
  4808. SDL_BlendOperation colorOperation,
  4809. SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
  4810. SDL_BlendOperation alphaOperation)
  4811. {
  4812. SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
  4813. srcAlphaFactor, dstAlphaFactor, alphaOperation);
  4814. return SDL_GetShortBlendMode(blendMode);
  4815. }
  4816. SDL_BlendFactor SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
  4817. {
  4818. blendMode = SDL_GetLongBlendMode(blendMode);
  4819. return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
  4820. }
  4821. SDL_BlendFactor SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
  4822. {
  4823. blendMode = SDL_GetLongBlendMode(blendMode);
  4824. return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
  4825. }
  4826. SDL_BlendOperation SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
  4827. {
  4828. blendMode = SDL_GetLongBlendMode(blendMode);
  4829. return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
  4830. }
  4831. SDL_BlendFactor SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
  4832. {
  4833. blendMode = SDL_GetLongBlendMode(blendMode);
  4834. return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
  4835. }
  4836. SDL_BlendFactor SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
  4837. {
  4838. blendMode = SDL_GetLongBlendMode(blendMode);
  4839. return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
  4840. }
  4841. SDL_BlendOperation SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
  4842. {
  4843. blendMode = SDL_GetLongBlendMode(blendMode);
  4844. return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
  4845. }
  4846. bool SDL_SetRenderVSync(SDL_Renderer *renderer, int vsync)
  4847. {
  4848. CHECK_RENDERER_MAGIC(renderer, false);
  4849. renderer->wanted_vsync = vsync ? true : false;
  4850. // for the software renderer, forward the call to the WindowTexture renderer
  4851. #ifdef SDL_VIDEO_RENDER_SW
  4852. if (renderer->software) {
  4853. if (!renderer->window) {
  4854. if (!vsync) {
  4855. return true;
  4856. } else {
  4857. return SDL_Unsupported();
  4858. }
  4859. }
  4860. if (SDL_SetWindowTextureVSync(NULL, renderer->window, vsync)) {
  4861. renderer->simulate_vsync = false;
  4862. return true;
  4863. }
  4864. }
  4865. #endif
  4866. if (!renderer->SetVSync ||
  4867. !renderer->SetVSync(renderer, vsync)) {
  4868. switch (vsync) {
  4869. case 0:
  4870. renderer->simulate_vsync = false;
  4871. break;
  4872. case 1:
  4873. renderer->simulate_vsync = true;
  4874. break;
  4875. default:
  4876. return SDL_Unsupported();
  4877. }
  4878. }
  4879. SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_VSYNC_NUMBER, vsync);
  4880. return true;
  4881. }
  4882. bool SDL_GetRenderVSync(SDL_Renderer *renderer, int *vsync)
  4883. {
  4884. if (vsync) {
  4885. *vsync = 0;
  4886. }
  4887. CHECK_RENDERER_MAGIC(renderer, false);
  4888. if (vsync) {
  4889. *vsync = (int)SDL_GetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_VSYNC_NUMBER, 0);
  4890. }
  4891. return true;
  4892. }
  4893. #define SDL_DEBUG_FONT_GLYPHS_PER_ROW 14
  4894. static bool CreateDebugTextAtlas(SDL_Renderer *renderer)
  4895. {
  4896. SDL_assert(renderer->debug_char_texture_atlas == NULL); // don't double-create it!
  4897. const int charWidth = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  4898. const int charHeight = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  4899. // actually make each glyph two pixels taller/wider, to prevent scaling artifacts.
  4900. const int rows = (SDL_DEBUG_FONT_NUM_GLYPHS / SDL_DEBUG_FONT_GLYPHS_PER_ROW) + 1;
  4901. SDL_Surface *atlas = SDL_CreateSurface((charWidth + 2) * SDL_DEBUG_FONT_GLYPHS_PER_ROW, rows * (charHeight + 2), SDL_PIXELFORMAT_RGBA8888);
  4902. if (!atlas) {
  4903. return false;
  4904. }
  4905. const int pitch = atlas->pitch;
  4906. SDL_memset(atlas->pixels, '\0', atlas->h * atlas->pitch);
  4907. int column = 0;
  4908. int row = 0;
  4909. for (int glyph = 0; glyph < SDL_DEBUG_FONT_NUM_GLYPHS; glyph++) {
  4910. // find top-left of this glyph in destination surface. The +2's account for glyph padding.
  4911. Uint8 *linepos = (((Uint8 *)atlas->pixels) + ((row * (charHeight + 2) + 1) * pitch)) + ((column * (charWidth + 2) + 1) * sizeof (Uint32));
  4912. const Uint8 *charpos = SDL_RenderDebugTextFontData + (glyph * 8);
  4913. // Draw the glyph to the surface...
  4914. for (int iy = 0; iy < charHeight; iy++) {
  4915. Uint32 *curpos = (Uint32 *)linepos;
  4916. for (int ix = 0; ix < charWidth; ix++) {
  4917. if ((*charpos) & (1 << ix)) {
  4918. *curpos = 0xffffffff;
  4919. } else {
  4920. *curpos = 0;
  4921. }
  4922. ++curpos;
  4923. }
  4924. linepos += pitch;
  4925. ++charpos;
  4926. }
  4927. // move to next position (and if too far, start the next row).
  4928. column++;
  4929. if (column >= SDL_DEBUG_FONT_GLYPHS_PER_ROW) {
  4930. row++;
  4931. column = 0;
  4932. }
  4933. }
  4934. SDL_assert((row < rows) || ((row == rows) && (column == 0))); // make sure we didn't overflow the surface.
  4935. // Convert temp surface into texture
  4936. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, atlas);
  4937. if (texture) {
  4938. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_PIXELART);
  4939. renderer->debug_char_texture_atlas = texture;
  4940. }
  4941. SDL_DestroySurface(atlas);
  4942. return texture != NULL;
  4943. }
  4944. static bool DrawDebugCharacter(SDL_Renderer *renderer, float x, float y, Uint32 c)
  4945. {
  4946. SDL_assert(renderer->debug_char_texture_atlas != NULL); // should have been created by now!
  4947. const int charWidth = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  4948. const int charHeight = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  4949. // Character index in cache
  4950. Uint32 ci = c;
  4951. if ((ci <= 32) || ((ci >= 127) && (ci <= 160))) {
  4952. return true; // these are just completely blank chars, don't bother doing anything.
  4953. } else if (ci >= SDL_DEBUG_FONT_NUM_GLYPHS) {
  4954. ci = SDL_DEBUG_FONT_NUM_GLYPHS - 1; // use our "not a valid/supported character" glyph.
  4955. } else if (ci < 127) {
  4956. ci -= 33; // adjust for the 33 blank glyphs at the start
  4957. } else {
  4958. ci -= 67; // adjust for the 33 blank glyphs at the start AND the 34 gap in the middle.
  4959. }
  4960. const float src_x = (float) (((ci % SDL_DEBUG_FONT_GLYPHS_PER_ROW) * (charWidth + 2)) + 1);
  4961. const float src_y = (float) (((ci / SDL_DEBUG_FONT_GLYPHS_PER_ROW) * (charHeight + 2)) + 1);
  4962. // Draw texture onto destination
  4963. const SDL_FRect srect = { src_x, src_y, (float) charWidth, (float) charHeight };
  4964. const SDL_FRect drect = { x, y, (float) charWidth, (float) charHeight };
  4965. return SDL_RenderTexture(renderer, renderer->debug_char_texture_atlas, &srect, &drect);
  4966. }
  4967. bool SDL_RenderDebugText(SDL_Renderer *renderer, float x, float y, const char *s)
  4968. {
  4969. CHECK_RENDERER_MAGIC(renderer, false);
  4970. // Allocate a texture atlas for this renderer if needed.
  4971. if (!renderer->debug_char_texture_atlas) {
  4972. if (!CreateDebugTextAtlas(renderer)) {
  4973. return false;
  4974. }
  4975. }
  4976. bool result = true;
  4977. Uint8 r, g, b, a;
  4978. result &= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  4979. result &= SDL_SetTextureColorMod(renderer->debug_char_texture_atlas, r, g, b);
  4980. result &= SDL_SetTextureAlphaMod(renderer->debug_char_texture_atlas, a);
  4981. float curx = x;
  4982. Uint32 ch;
  4983. while (result && ((ch = SDL_StepUTF8(&s, NULL)) != 0)) {
  4984. result &= DrawDebugCharacter(renderer, curx, y, ch);
  4985. curx += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  4986. }
  4987. return result;
  4988. }
  4989. bool SDL_RenderDebugTextFormat(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  4990. {
  4991. va_list ap;
  4992. va_start(ap, fmt);
  4993. // fast path to avoid unnecessary allocation and copy. If you're going through the dynapi, there's a good chance
  4994. // you _always_ hit this path, since it probably had to process varargs before calling into the jumptable.
  4995. if (SDL_strcmp(fmt, "%s") == 0) {
  4996. const char *str = va_arg(ap, const char *);
  4997. va_end(ap);
  4998. return SDL_RenderDebugText(renderer, x, y, str);
  4999. }
  5000. char *str = NULL;
  5001. const int rc = SDL_vasprintf(&str, fmt, ap);
  5002. va_end(ap);
  5003. if (rc == -1) {
  5004. return false;
  5005. }
  5006. const bool retval = SDL_RenderDebugText(renderer, x, y, str);
  5007. SDL_free(str);
  5008. return retval;
  5009. }
  5010. bool SDL_SetDefaultTextureScaleMode(SDL_Renderer *renderer, SDL_ScaleMode scale_mode)
  5011. {
  5012. CHECK_RENDERER_MAGIC(renderer, false);
  5013. renderer->scale_mode = scale_mode;
  5014. return true;
  5015. }
  5016. bool SDL_GetDefaultTextureScaleMode(SDL_Renderer *renderer, SDL_ScaleMode *scale_mode)
  5017. {
  5018. if (scale_mode) {
  5019. *scale_mode = SDL_SCALEMODE_LINEAR;
  5020. }
  5021. CHECK_RENDERER_MAGIC(renderer, false);
  5022. if (scale_mode) {
  5023. *scale_mode = renderer->scale_mode;
  5024. }
  5025. return true;
  5026. }
  5027. SDL_GPURenderState *SDL_CreateGPURenderState(SDL_Renderer *renderer, SDL_GPURenderStateDesc *desc)
  5028. {
  5029. CHECK_RENDERER_MAGIC(renderer, NULL);
  5030. if (!desc) {
  5031. SDL_InvalidParamError("desc");
  5032. return NULL;
  5033. }
  5034. if (desc->version < sizeof(*desc)) {
  5035. // Update this to handle older versions of this interface
  5036. SDL_SetError("Invalid desc, should be initialized with SDL_INIT_INTERFACE()");
  5037. return NULL;
  5038. }
  5039. if (!desc->fragment_shader) {
  5040. SDL_SetError("desc->fragment_shader is required");
  5041. return NULL;
  5042. }
  5043. SDL_GPUDevice *device = (SDL_GPUDevice *)SDL_GetPointerProperty(renderer->props, SDL_PROP_RENDERER_GPU_DEVICE_POINTER, NULL);
  5044. if (!device) {
  5045. SDL_SetError("Renderer isn't associated with a GPU device");
  5046. return NULL;
  5047. }
  5048. SDL_GPURenderState *state = (SDL_GPURenderState *)SDL_calloc(1, sizeof(*state));
  5049. if (!state) {
  5050. return NULL;
  5051. }
  5052. state->renderer = renderer;
  5053. state->fragment_shader = desc->fragment_shader;
  5054. if (desc->num_sampler_bindings > 0) {
  5055. state->sampler_bindings = (SDL_GPUTextureSamplerBinding *)SDL_calloc(desc->num_sampler_bindings, sizeof(*state->sampler_bindings));
  5056. if (!state->sampler_bindings) {
  5057. SDL_DestroyGPURenderState(state);
  5058. return NULL;
  5059. }
  5060. SDL_memcpy(state->sampler_bindings, desc->sampler_bindings, desc->num_sampler_bindings * sizeof(*state->sampler_bindings));
  5061. state->num_sampler_bindings = desc->num_sampler_bindings;
  5062. }
  5063. if (desc->num_storage_textures > 0) {
  5064. state->storage_textures = (SDL_GPUTexture **)SDL_calloc(desc->num_storage_textures, sizeof(*state->storage_textures));
  5065. if (!state->storage_textures) {
  5066. SDL_DestroyGPURenderState(state);
  5067. return NULL;
  5068. }
  5069. SDL_memcpy(state->storage_textures, desc->storage_textures, desc->num_storage_textures * sizeof(*state->storage_textures));
  5070. state->num_storage_textures = desc->num_storage_textures;
  5071. }
  5072. if (desc->num_storage_buffers > 0) {
  5073. state->storage_buffers = (SDL_GPUBuffer **)SDL_calloc(desc->num_storage_buffers, sizeof(*state->storage_buffers));
  5074. if (!state->storage_buffers) {
  5075. SDL_DestroyGPURenderState(state);
  5076. return NULL;
  5077. }
  5078. SDL_memcpy(state->storage_buffers, desc->storage_buffers, desc->num_storage_buffers * sizeof(*state->storage_buffers));
  5079. state->num_storage_buffers = desc->num_storage_buffers;
  5080. }
  5081. return state;
  5082. }
  5083. bool SDL_SetGPURenderStateFragmentUniforms(SDL_GPURenderState *state, Uint32 slot_index, const void *data, Uint32 length)
  5084. {
  5085. if (!state) {
  5086. return SDL_InvalidParamError("state");
  5087. }
  5088. if (!FlushRenderCommandsIfGPURenderStateNeeded(state)) {
  5089. return false;
  5090. }
  5091. for (int i = 0; i < state->num_uniform_buffers; i++) {
  5092. SDL_GPURenderStateUniformBuffer *buffer = &state->uniform_buffers[i];
  5093. if (buffer->slot_index == slot_index) {
  5094. void *new_data = SDL_realloc(buffer->data, length);
  5095. if (!new_data) {
  5096. return false;
  5097. }
  5098. SDL_memcpy(new_data, data, length);
  5099. buffer->data = new_data;
  5100. buffer->length = length;
  5101. return true;
  5102. }
  5103. }
  5104. SDL_GPURenderStateUniformBuffer *buffers = (SDL_GPURenderStateUniformBuffer *)SDL_realloc(state->uniform_buffers, (state->num_uniform_buffers + 1) * sizeof(*state->uniform_buffers));
  5105. if (!buffers) {
  5106. return false;
  5107. }
  5108. SDL_GPURenderStateUniformBuffer *buffer = &buffers[state->num_uniform_buffers];
  5109. buffer->slot_index = slot_index;
  5110. buffer->length = length;
  5111. buffer->data = SDL_malloc(length);
  5112. if (!buffer->data) {
  5113. SDL_free(buffers);
  5114. return false;
  5115. }
  5116. SDL_memcpy(buffer->data, data, length);
  5117. state->uniform_buffers = buffers;
  5118. ++state->num_uniform_buffers;
  5119. return true;
  5120. }
  5121. bool SDL_SetRenderGPUState(SDL_Renderer *renderer, SDL_GPURenderState *state)
  5122. {
  5123. CHECK_RENDERER_MAGIC(renderer, false);
  5124. renderer->gpu_render_state = state;
  5125. return true;
  5126. }
  5127. void SDL_DestroyGPURenderState(SDL_GPURenderState *state)
  5128. {
  5129. if (!state) {
  5130. return;
  5131. }
  5132. FlushRenderCommandsIfGPURenderStateNeeded(state);
  5133. if (state->num_uniform_buffers > 0) {
  5134. for (int i = 0; i < state->num_uniform_buffers; i++) {
  5135. SDL_free(state->uniform_buffers[i].data);
  5136. }
  5137. SDL_free(state->uniform_buffers);
  5138. }
  5139. SDL_free(state->sampler_bindings);
  5140. SDL_free(state->storage_textures);
  5141. SDL_free(state->storage_buffers);
  5142. SDL_free(state);
  5143. }