SDL_render.c 171 KB

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