SDL_render.c 184 KB

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