SDL_render.c 204 KB

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