SDL_render.c 205 KB

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