SDL_render.c 200 KB

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