SDL_render.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_hints.h"
  21. #include "SDL_render.h"
  22. #include "SDL_sysrender.h"
  23. #include "software/SDL_render_sw_c.h"
  24. #include "../video/SDL_pixels_c.h"
  25. #if defined(__ANDROID__)
  26. # include "../core/android/SDL_android.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_APP_WILLENTERBACKGROUND 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(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__)
  35. #define DONT_DRAW_WHILE_HIDDEN 1
  36. #else
  37. #define DONT_DRAW_WHILE_HIDDEN 0
  38. #endif
  39. #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
  40. #define CHECK_RENDERER_MAGIC(renderer, retval) \
  41. if (!renderer || renderer->magic != &renderer_magic) { \
  42. SDL_InvalidParamError("renderer"); \
  43. return retval; \
  44. }
  45. #define CHECK_TEXTURE_MAGIC(texture, retval) \
  46. if (!texture || texture->magic != &texture_magic) { \
  47. SDL_InvalidParamError("texture"); \
  48. return retval; \
  49. }
  50. /* Predefined blend modes */
  51. #define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
  52. srcAlphaFactor, dstAlphaFactor, alphaOperation) \
  53. (SDL_BlendMode)(((Uint32)colorOperation << 0) | \
  54. ((Uint32)srcColorFactor << 4) | \
  55. ((Uint32)dstColorFactor << 8) | \
  56. ((Uint32)alphaOperation << 16) | \
  57. ((Uint32)srcAlphaFactor << 20) | \
  58. ((Uint32)dstAlphaFactor << 24))
  59. #define SDL_BLENDMODE_NONE_FULL \
  60. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
  61. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
  62. #define SDL_BLENDMODE_BLEND_FULL \
  63. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  64. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  65. #define SDL_BLENDMODE_ADD_FULL \
  66. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
  67. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  68. #define SDL_BLENDMODE_MOD_FULL \
  69. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
  70. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  71. #define SDL_BLENDMODE_MUL_FULL \
  72. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_DST_COLOR, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  73. SDL_BLENDFACTOR_DST_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  74. #if !SDL_RENDER_DISABLED
  75. static const SDL_RenderDriver *render_drivers[] = {
  76. #if SDL_VIDEO_RENDER_D3D
  77. &D3D_RenderDriver,
  78. #endif
  79. #if SDL_VIDEO_RENDER_D3D11
  80. &D3D11_RenderDriver,
  81. #endif
  82. #if SDL_VIDEO_RENDER_D3D12
  83. &D3D12_RenderDriver,
  84. #endif
  85. #if SDL_VIDEO_RENDER_METAL
  86. &METAL_RenderDriver,
  87. #endif
  88. #if SDL_VIDEO_RENDER_OGL
  89. &GL_RenderDriver,
  90. #endif
  91. #if SDL_VIDEO_RENDER_OGL_ES2
  92. &GLES2_RenderDriver,
  93. #endif
  94. #if SDL_VIDEO_RENDER_OGL_ES
  95. &GLES_RenderDriver,
  96. #endif
  97. #if SDL_VIDEO_RENDER_DIRECTFB
  98. &DirectFB_RenderDriver,
  99. #endif
  100. #if SDL_VIDEO_RENDER_PS2 && !SDL_RENDER_DISABLED
  101. &PS2_RenderDriver,
  102. #endif
  103. #if SDL_VIDEO_RENDER_PSP
  104. &PSP_RenderDriver,
  105. #endif
  106. #if SDL_VIDEO_RENDER_VITA_GXM
  107. &VITA_GXM_RenderDriver,
  108. #endif
  109. #if SDL_VIDEO_RENDER_SW
  110. &SW_RenderDriver
  111. #endif
  112. };
  113. #endif /* !SDL_RENDER_DISABLED */
  114. static char renderer_magic;
  115. static char texture_magic;
  116. static SDL_INLINE void
  117. DebugLogRenderCommands(const SDL_RenderCommand *cmd)
  118. {
  119. #if 0
  120. unsigned int i = 1;
  121. SDL_Log("Render commands to flush:");
  122. while (cmd) {
  123. switch (cmd->command) {
  124. case SDL_RENDERCMD_NO_OP:
  125. SDL_Log(" %u. no-op", i++);
  126. break;
  127. case SDL_RENDERCMD_SETVIEWPORT:
  128. SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
  129. (unsigned int) cmd->data.viewport.first,
  130. cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
  131. cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
  132. break;
  133. case SDL_RENDERCMD_SETCLIPRECT:
  134. SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
  135. cmd->data.cliprect.enabled ? "true" : "false",
  136. cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
  137. cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
  138. break;
  139. case SDL_RENDERCMD_SETDRAWCOLOR:
  140. SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  141. (unsigned int) cmd->data.color.first,
  142. (int) cmd->data.color.r, (int) cmd->data.color.g,
  143. (int) cmd->data.color.b, (int) cmd->data.color.a);
  144. break;
  145. case SDL_RENDERCMD_CLEAR:
  146. SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  147. (unsigned int) cmd->data.color.first,
  148. (int) cmd->data.color.r, (int) cmd->data.color.g,
  149. (int) cmd->data.color.b, (int) cmd->data.color.a);
  150. break;
  151. case SDL_RENDERCMD_DRAW_POINTS:
  152. SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  153. (unsigned int) cmd->data.draw.first,
  154. (unsigned int) cmd->data.draw.count,
  155. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  156. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  157. (int) cmd->data.draw.blend);
  158. break;
  159. case SDL_RENDERCMD_DRAW_LINES:
  160. SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  161. (unsigned int) cmd->data.draw.first,
  162. (unsigned int) cmd->data.draw.count,
  163. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  164. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  165. (int) cmd->data.draw.blend);
  166. break;
  167. case SDL_RENDERCMD_FILL_RECTS:
  168. SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  169. (unsigned int) cmd->data.draw.first,
  170. (unsigned int) cmd->data.draw.count,
  171. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  172. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  173. (int) cmd->data.draw.blend);
  174. break;
  175. case SDL_RENDERCMD_COPY:
  176. SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  177. (unsigned int) cmd->data.draw.first,
  178. (unsigned int) cmd->data.draw.count,
  179. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  180. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  181. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  182. break;
  183. case SDL_RENDERCMD_COPY_EX:
  184. SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  185. (unsigned int) cmd->data.draw.first,
  186. (unsigned int) cmd->data.draw.count,
  187. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  188. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  189. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  190. break;
  191. case SDL_RENDERCMD_GEOMETRY:
  192. SDL_Log(" %u. geometry (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  193. (unsigned int) cmd->data.draw.first,
  194. (unsigned int) cmd->data.draw.count,
  195. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  196. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  197. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  198. break;
  199. }
  200. cmd = cmd->next;
  201. }
  202. #endif
  203. }
  204. static int
  205. FlushRenderCommands(SDL_Renderer *renderer)
  206. {
  207. int retval;
  208. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  209. if (renderer->render_commands == NULL) { /* nothing to do! */
  210. SDL_assert(renderer->vertex_data_used == 0);
  211. return 0;
  212. }
  213. DebugLogRenderCommands(renderer->render_commands);
  214. retval = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
  215. /* Move the whole render command queue to the unused pool so we can reuse them next time. */
  216. if (renderer->render_commands_tail != NULL) {
  217. renderer->render_commands_tail->next = renderer->render_commands_pool;
  218. renderer->render_commands_pool = renderer->render_commands;
  219. renderer->render_commands_tail = NULL;
  220. renderer->render_commands = NULL;
  221. }
  222. renderer->vertex_data_used = 0;
  223. renderer->render_command_generation++;
  224. renderer->color_queued = SDL_FALSE;
  225. renderer->viewport_queued = SDL_FALSE;
  226. renderer->cliprect_queued = SDL_FALSE;
  227. return retval;
  228. }
  229. static int
  230. FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
  231. {
  232. SDL_Renderer *renderer = texture->renderer;
  233. if (texture->last_command_generation == renderer->render_command_generation) {
  234. /* the current command queue depends on this texture, flush the queue now before it changes */
  235. return FlushRenderCommands(renderer);
  236. }
  237. return 0;
  238. }
  239. static SDL_INLINE int
  240. FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer)
  241. {
  242. return renderer->batching ? 0 : FlushRenderCommands(renderer);
  243. }
  244. int
  245. SDL_RenderFlush(SDL_Renderer * renderer)
  246. {
  247. return FlushRenderCommands(renderer);
  248. }
  249. void *
  250. SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset)
  251. {
  252. const size_t needed = renderer->vertex_data_used + numbytes + alignment;
  253. const size_t current_offset = renderer->vertex_data_used;
  254. const size_t aligner = (alignment && ((current_offset & (alignment - 1)) != 0)) ? (alignment - (current_offset & (alignment - 1))) : 0;
  255. const size_t aligned = current_offset + aligner;
  256. if (renderer->vertex_data_allocation < needed) {
  257. const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
  258. size_t newsize = current_allocation * 2;
  259. void *ptr;
  260. while (newsize < needed) {
  261. newsize *= 2;
  262. }
  263. ptr = SDL_realloc(renderer->vertex_data, newsize);
  264. if (ptr == NULL) {
  265. SDL_OutOfMemory();
  266. return NULL;
  267. }
  268. renderer->vertex_data = ptr;
  269. renderer->vertex_data_allocation = newsize;
  270. }
  271. if (offset) {
  272. *offset = aligned;
  273. }
  274. renderer->vertex_data_used += aligner + numbytes;
  275. return ((Uint8 *) renderer->vertex_data) + aligned;
  276. }
  277. static SDL_RenderCommand *
  278. AllocateRenderCommand(SDL_Renderer *renderer)
  279. {
  280. SDL_RenderCommand *retval = NULL;
  281. /* !!! FIXME: are there threading limitations in SDL's render API? If not, we need to mutex this. */
  282. retval = renderer->render_commands_pool;
  283. if (retval != NULL) {
  284. renderer->render_commands_pool = retval->next;
  285. retval->next = NULL;
  286. } else {
  287. retval = SDL_calloc(1, sizeof (*retval));
  288. if (!retval) {
  289. SDL_OutOfMemory();
  290. return NULL;
  291. }
  292. }
  293. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  294. if (renderer->render_commands_tail != NULL) {
  295. renderer->render_commands_tail->next = retval;
  296. } else {
  297. renderer->render_commands = retval;
  298. }
  299. renderer->render_commands_tail = retval;
  300. return retval;
  301. }
  302. static int
  303. QueueCmdSetViewport(SDL_Renderer *renderer)
  304. {
  305. int retval = 0;
  306. if (!renderer->viewport_queued || (SDL_memcmp(&renderer->viewport, &renderer->last_queued_viewport, sizeof (SDL_DRect)) != 0)) {
  307. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  308. retval = -1;
  309. if (cmd != NULL) {
  310. cmd->command = SDL_RENDERCMD_SETVIEWPORT;
  311. cmd->data.viewport.first = 0; /* render backend will fill this in. */
  312. /* Convert SDL_DRect to SDL_Rect */
  313. cmd->data.viewport.rect.x = (int)SDL_floor(renderer->viewport.x);
  314. cmd->data.viewport.rect.y = (int)SDL_floor(renderer->viewport.y);
  315. cmd->data.viewport.rect.w = (int)SDL_floor(renderer->viewport.w);
  316. cmd->data.viewport.rect.h = (int)SDL_floor(renderer->viewport.h);
  317. retval = renderer->QueueSetViewport(renderer, cmd);
  318. if (retval < 0) {
  319. cmd->command = SDL_RENDERCMD_NO_OP;
  320. } else {
  321. SDL_copyp(&renderer->last_queued_viewport, &renderer->viewport);
  322. renderer->viewport_queued = SDL_TRUE;
  323. }
  324. }
  325. }
  326. return retval;
  327. }
  328. static int
  329. QueueCmdSetClipRect(SDL_Renderer *renderer)
  330. {
  331. int retval = 0;
  332. if ((!renderer->cliprect_queued) ||
  333. (renderer->clipping_enabled != renderer->last_queued_cliprect_enabled) ||
  334. (SDL_memcmp(&renderer->clip_rect, &renderer->last_queued_cliprect, sizeof (SDL_DRect)) != 0)) {
  335. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  336. if (cmd == NULL) {
  337. retval = -1;
  338. } else {
  339. cmd->command = SDL_RENDERCMD_SETCLIPRECT;
  340. cmd->data.cliprect.enabled = renderer->clipping_enabled;
  341. /* Convert SDL_DRect to SDL_Rect */
  342. cmd->data.cliprect.rect.x = (int)SDL_floor(renderer->clip_rect.x);
  343. cmd->data.cliprect.rect.y = (int)SDL_floor(renderer->clip_rect.y);
  344. cmd->data.cliprect.rect.w = (int)SDL_floor(renderer->clip_rect.w);
  345. cmd->data.cliprect.rect.h = (int)SDL_floor(renderer->clip_rect.h);
  346. SDL_copyp(&renderer->last_queued_cliprect, &renderer->clip_rect);
  347. renderer->last_queued_cliprect_enabled = renderer->clipping_enabled;
  348. renderer->cliprect_queued = SDL_TRUE;
  349. }
  350. }
  351. return retval;
  352. }
  353. static int
  354. QueueCmdSetDrawColor(SDL_Renderer *renderer, SDL_Color *col)
  355. {
  356. const Uint32 color = (((Uint32)col->a << 24) | (col->r << 16) | (col->g << 8) | col->b);
  357. int retval = 0;
  358. if (!renderer->color_queued || (color != renderer->last_queued_color)) {
  359. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  360. retval = -1;
  361. if (cmd != NULL) {
  362. cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
  363. cmd->data.color.first = 0; /* render backend will fill this in. */
  364. cmd->data.color.r = col->r;
  365. cmd->data.color.g = col->g;
  366. cmd->data.color.b = col->b;
  367. cmd->data.color.a = col->a;
  368. retval = renderer->QueueSetDrawColor(renderer, cmd);
  369. if (retval < 0) {
  370. cmd->command = SDL_RENDERCMD_NO_OP;
  371. } else {
  372. renderer->last_queued_color = color;
  373. renderer->color_queued = SDL_TRUE;
  374. }
  375. }
  376. }
  377. return retval;
  378. }
  379. static int
  380. QueueCmdClear(SDL_Renderer *renderer)
  381. {
  382. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  383. if (cmd == NULL) {
  384. return -1;
  385. }
  386. cmd->command = SDL_RENDERCMD_CLEAR;
  387. cmd->data.color.first = 0;
  388. cmd->data.color.r = renderer->color.r;
  389. cmd->data.color.g = renderer->color.g;
  390. cmd->data.color.b = renderer->color.b;
  391. cmd->data.color.a = renderer->color.a;
  392. return 0;
  393. }
  394. static SDL_RenderCommand *
  395. PrepQueueCmdDraw(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype, SDL_Texture *texture)
  396. {
  397. SDL_RenderCommand *cmd = NULL;
  398. int retval = 0;
  399. SDL_Color *color;
  400. SDL_BlendMode blendMode;
  401. if (texture) {
  402. color = &texture->color;
  403. blendMode = texture->blendMode;
  404. } else {
  405. color = &renderer->color;
  406. blendMode = renderer->blendMode;
  407. }
  408. if (cmdtype != SDL_RENDERCMD_GEOMETRY) {
  409. /* !!! FIXME: drop this draw if viewport w or h is zero. */
  410. retval = QueueCmdSetDrawColor(renderer, color);
  411. }
  412. /* Set the viewport and clip rect directly before draws, so the backends
  413. * don't have to worry about that state not being valid at draw time. */
  414. if (retval == 0 && !renderer->viewport_queued) {
  415. retval = QueueCmdSetViewport(renderer);
  416. }
  417. if (retval == 0 && !renderer->cliprect_queued) {
  418. retval = QueueCmdSetClipRect(renderer);
  419. }
  420. if (retval == 0) {
  421. cmd = AllocateRenderCommand(renderer);
  422. if (cmd != NULL) {
  423. cmd->command = cmdtype;
  424. cmd->data.draw.first = 0; /* render backend will fill this in. */
  425. cmd->data.draw.count = 0; /* render backend will fill this in. */
  426. cmd->data.draw.r = color->r;
  427. cmd->data.draw.g = color->g;
  428. cmd->data.draw.b = color->b;
  429. cmd->data.draw.a = color->a;
  430. cmd->data.draw.blend = blendMode;
  431. cmd->data.draw.texture = texture;
  432. }
  433. }
  434. return cmd;
  435. }
  436. static int
  437. QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
  438. {
  439. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_POINTS, NULL);
  440. int retval = -1;
  441. if (cmd != NULL) {
  442. retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
  443. if (retval < 0) {
  444. cmd->command = SDL_RENDERCMD_NO_OP;
  445. }
  446. }
  447. return retval;
  448. }
  449. static int
  450. QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
  451. {
  452. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_LINES, NULL);
  453. int retval = -1;
  454. if (cmd != NULL) {
  455. retval = renderer->QueueDrawLines(renderer, cmd, points, count);
  456. if (retval < 0) {
  457. cmd->command = SDL_RENDERCMD_NO_OP;
  458. }
  459. }
  460. return retval;
  461. }
  462. static int
  463. QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
  464. {
  465. SDL_RenderCommand *cmd;
  466. int retval = -1;
  467. const int use_rendergeometry = (renderer->QueueFillRects == NULL);
  468. cmd = PrepQueueCmdDraw(renderer, (use_rendergeometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS), NULL);
  469. if (cmd != NULL) {
  470. if (use_rendergeometry) {
  471. SDL_bool isstack1;
  472. SDL_bool isstack2;
  473. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  474. int *indices = SDL_small_alloc(int, 6 * count, &isstack2);
  475. if (xy && indices) {
  476. int i;
  477. float *ptr_xy = xy;
  478. int *ptr_indices = indices;
  479. const int xy_stride = 2 * sizeof (float);
  480. const int num_vertices = 4 * count;
  481. const int num_indices = 6 * count;
  482. const int size_indices = 4;
  483. int cur_indice = 0;
  484. for (i = 0; i < count; ++i) {
  485. float minx, miny, maxx, maxy;
  486. minx = rects[i].x;
  487. miny = rects[i].y;
  488. maxx = rects[i].x + rects[i].w;
  489. maxy = rects[i].y + rects[i].h;
  490. *ptr_xy++ = minx;
  491. *ptr_xy++ = miny;
  492. *ptr_xy++ = maxx;
  493. *ptr_xy++ = miny;
  494. *ptr_xy++ = maxx;
  495. *ptr_xy++ = maxy;
  496. *ptr_xy++ = minx;
  497. *ptr_xy++ = maxy;
  498. *ptr_indices++ = cur_indice + 0;
  499. *ptr_indices++ = cur_indice + 1;
  500. *ptr_indices++ = cur_indice + 2;
  501. *ptr_indices++ = cur_indice + 0;
  502. *ptr_indices++ = cur_indice + 2;
  503. *ptr_indices++ = cur_indice + 3;
  504. cur_indice += 4;
  505. }
  506. retval = renderer->QueueGeometry(renderer, cmd, NULL,
  507. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  508. num_vertices, indices, num_indices, size_indices,
  509. 1.0f, 1.0f);
  510. if (retval < 0) {
  511. cmd->command = SDL_RENDERCMD_NO_OP;
  512. }
  513. }
  514. SDL_small_free(xy, isstack1);
  515. SDL_small_free(indices, isstack2);
  516. } else {
  517. retval = renderer->QueueFillRects(renderer, cmd, rects, count);
  518. if (retval < 0) {
  519. cmd->command = SDL_RENDERCMD_NO_OP;
  520. }
  521. }
  522. }
  523. return retval;
  524. }
  525. static int
  526. QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  527. {
  528. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
  529. int retval = -1;
  530. if (cmd != NULL) {
  531. retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
  532. if (retval < 0) {
  533. cmd->command = SDL_RENDERCMD_NO_OP;
  534. }
  535. }
  536. return retval;
  537. }
  538. static int
  539. QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
  540. const SDL_Rect * srcquad, const SDL_FRect * dstrect,
  541. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
  542. {
  543. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
  544. int retval = -1;
  545. if (cmd != NULL) {
  546. retval = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip, scale_x, scale_y);
  547. if (retval < 0) {
  548. cmd->command = SDL_RENDERCMD_NO_OP;
  549. }
  550. }
  551. return retval;
  552. }
  553. static int
  554. QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
  555. const float *xy, int xy_stride,
  556. const SDL_Color *color, int color_stride,
  557. const float *uv, int uv_stride,
  558. int num_vertices,
  559. const void *indices, int num_indices, int size_indices,
  560. float scale_x, float scale_y)
  561. {
  562. SDL_RenderCommand *cmd;
  563. int retval = -1;
  564. cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
  565. if (cmd != NULL) {
  566. retval = renderer->QueueGeometry(renderer, cmd, texture,
  567. xy, xy_stride,
  568. color, color_stride, uv, uv_stride,
  569. num_vertices, indices, num_indices, size_indices,
  570. scale_x, scale_y);
  571. if (retval < 0) {
  572. cmd->command = SDL_RENDERCMD_NO_OP;
  573. }
  574. }
  575. return retval;
  576. }
  577. static int UpdateLogicalSize(SDL_Renderer *renderer, SDL_bool flush_viewport_cmd);
  578. int
  579. SDL_GetNumRenderDrivers(void)
  580. {
  581. #if !SDL_RENDER_DISABLED
  582. return SDL_arraysize(render_drivers);
  583. #else
  584. return 0;
  585. #endif
  586. }
  587. int
  588. SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  589. {
  590. #if !SDL_RENDER_DISABLED
  591. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  592. return SDL_SetError("index must be in the range of 0 - %d",
  593. SDL_GetNumRenderDrivers() - 1);
  594. }
  595. *info = render_drivers[index]->info;
  596. return 0;
  597. #else
  598. return SDL_SetError("SDL not built with rendering support");
  599. #endif
  600. }
  601. static void GetWindowViewportValues(SDL_Renderer *renderer, int *logical_w, int *logical_h, SDL_DRect *viewport, SDL_FPoint *scale)
  602. {
  603. SDL_LockMutex(renderer->target_mutex);
  604. *logical_w = renderer->target ? renderer->logical_w_backup : renderer->logical_w;
  605. *logical_h = renderer->target ? renderer->logical_h_backup : renderer->logical_h;
  606. *viewport = renderer->target ? renderer->viewport_backup : renderer->viewport;
  607. *scale = renderer->target ? renderer->scale_backup : renderer->scale;
  608. SDL_UnlockMutex(renderer->target_mutex);
  609. }
  610. static int SDLCALL
  611. SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  612. {
  613. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  614. if (event->type == SDL_WINDOWEVENT) {
  615. SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
  616. if (window == renderer->window) {
  617. if (renderer->WindowEvent) {
  618. renderer->WindowEvent(renderer, &event->window);
  619. }
  620. /* In addition to size changes, we also want to do this block for
  621. * window display changes as well! If the new display has a new DPI,
  622. * we need to update the viewport for the new window/drawable ratio.
  623. */
  624. if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
  625. event->window.event == SDL_WINDOWEVENT_DISPLAY_CHANGED) {
  626. /* Make sure we're operating on the default render target */
  627. SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
  628. if (saved_target) {
  629. SDL_SetRenderTarget(renderer, NULL);
  630. }
  631. /* Update the DPI scale if the window has been resized. */
  632. if (window && renderer->GetOutputSize) {
  633. int window_w, window_h;
  634. int output_w, output_h;
  635. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  636. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  637. renderer->dpi_scale.x = (float)window_w / output_w;
  638. renderer->dpi_scale.y = (float)window_h / output_h;
  639. }
  640. }
  641. if (renderer->logical_w) {
  642. #if defined(__ANDROID__)
  643. /* Don't immediatly flush because the app may be in
  644. * background, and the egl context shouldn't be used. */
  645. SDL_bool flush_viewport_cmd = SDL_FALSE;
  646. #else
  647. SDL_bool flush_viewport_cmd = SDL_TRUE;
  648. #endif
  649. UpdateLogicalSize(renderer, flush_viewport_cmd);
  650. } else {
  651. /* Window was resized, reset viewport */
  652. int w, h;
  653. if (renderer->GetOutputSize) {
  654. renderer->GetOutputSize(renderer, &w, &h);
  655. } else {
  656. SDL_GetWindowSize(renderer->window, &w, &h);
  657. }
  658. renderer->viewport.x = (double)0;
  659. renderer->viewport.y = (double)0;
  660. renderer->viewport.w = (double)w;
  661. renderer->viewport.h = (double)h;
  662. QueueCmdSetViewport(renderer);
  663. #if defined(__ANDROID__)
  664. /* Don't immediatly flush because the app may be in
  665. * background, and the egl context shouldn't be used. */
  666. #else
  667. FlushRenderCommandsIfNotBatching(renderer);
  668. #endif
  669. }
  670. if (saved_target) {
  671. SDL_SetRenderTarget(renderer, saved_target);
  672. }
  673. } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
  674. renderer->hidden = SDL_TRUE;
  675. } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
  676. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  677. renderer->hidden = SDL_FALSE;
  678. }
  679. } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
  680. renderer->hidden = SDL_TRUE;
  681. } else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
  682. event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
  683. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  684. renderer->hidden = SDL_FALSE;
  685. }
  686. }
  687. }
  688. } else if (event->type == SDL_MOUSEMOTION) {
  689. SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
  690. if (window == renderer->window) {
  691. int logical_w, logical_h;
  692. SDL_DRect viewport;
  693. SDL_FPoint scale;
  694. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  695. if (logical_w) {
  696. event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
  697. event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
  698. event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
  699. event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
  700. if (event->motion.xrel != 0 && renderer->relative_scaling) {
  701. float rel = renderer->xrel + event->motion.xrel / (scale.x * renderer->dpi_scale.x);
  702. float truncated = SDL_truncf(rel);
  703. renderer->xrel = rel - truncated;
  704. event->motion.xrel = (Sint32) truncated;
  705. }
  706. if (event->motion.yrel != 0 && renderer->relative_scaling) {
  707. float rel = renderer->yrel + event->motion.yrel / (scale.y * renderer->dpi_scale.y);
  708. float truncated = SDL_truncf(rel);
  709. renderer->yrel = rel - truncated;
  710. event->motion.yrel = (Sint32) truncated;
  711. }
  712. }
  713. }
  714. } else if (event->type == SDL_MOUSEBUTTONDOWN ||
  715. event->type == SDL_MOUSEBUTTONUP) {
  716. SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
  717. if (window == renderer->window) {
  718. int logical_w, logical_h;
  719. SDL_DRect viewport;
  720. SDL_FPoint scale;
  721. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  722. if (logical_w) {
  723. event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
  724. event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
  725. event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
  726. event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
  727. }
  728. }
  729. } else if (event->type == SDL_FINGERDOWN ||
  730. event->type == SDL_FINGERUP ||
  731. event->type == SDL_FINGERMOTION) {
  732. int logical_w, logical_h;
  733. float physical_w, physical_h;
  734. SDL_DRect viewport;
  735. SDL_FPoint scale;
  736. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  737. /* !!! FIXME: we probably should drop events that are outside of the
  738. !!! FIXME: viewport, but we can't do that from an event watcher,
  739. !!! FIXME: and we would have to track if a touch happened outside
  740. !!! FIXME: the viewport and then slid into it to insert extra
  741. !!! FIXME: events, which is a mess, so for now we just clamp these
  742. !!! FIXME: events to the edge. */
  743. if (renderer->GetOutputSize) {
  744. int w, h;
  745. renderer->GetOutputSize(renderer, &w, &h);
  746. physical_w = (float) w;
  747. physical_h = (float) h;
  748. } else {
  749. int w, h;
  750. SDL_GetWindowSize(renderer->window, &w, &h);
  751. physical_w = ((float) w) * renderer->dpi_scale.x;
  752. physical_h = ((float) h) * renderer->dpi_scale.y;
  753. }
  754. if (physical_w == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
  755. event->tfinger.x = 0.5f;
  756. } else {
  757. const float normalized_viewport_x = ((float) viewport.x) / physical_w;
  758. const float normalized_viewport_w = ((float) viewport.w) / physical_w;
  759. if (event->tfinger.x <= normalized_viewport_x) {
  760. event->tfinger.x = 0.0f; /* to the left of the viewport, clamp to the edge. */
  761. } else if (event->tfinger.x >= (normalized_viewport_x + normalized_viewport_w)) {
  762. event->tfinger.x = 1.0f; /* to the right of the viewport, clamp to the edge. */
  763. } else {
  764. event->tfinger.x = (event->tfinger.x - normalized_viewport_x) / normalized_viewport_w;
  765. }
  766. }
  767. if (physical_h == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
  768. event->tfinger.y = 0.5f;
  769. } else {
  770. const float normalized_viewport_y = ((float) viewport.y) / physical_h;
  771. const float normalized_viewport_h = ((float) viewport.h) / physical_h;
  772. if (event->tfinger.y <= normalized_viewport_y) {
  773. event->tfinger.y = 0.0f; /* to the left of the viewport, clamp to the edge. */
  774. } else if (event->tfinger.y >= (normalized_viewport_y + normalized_viewport_h)) {
  775. event->tfinger.y = 1.0f; /* to the right of the viewport, clamp to the edge. */
  776. } else {
  777. event->tfinger.y = (event->tfinger.y - normalized_viewport_y) / normalized_viewport_h;
  778. }
  779. }
  780. }
  781. return 0;
  782. }
  783. int
  784. SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
  785. SDL_Window **window, SDL_Renderer **renderer)
  786. {
  787. *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
  788. SDL_WINDOWPOS_UNDEFINED,
  789. width, height, window_flags);
  790. if (!*window) {
  791. *renderer = NULL;
  792. return -1;
  793. }
  794. *renderer = SDL_CreateRenderer(*window, -1, 0);
  795. if (!*renderer) {
  796. return -1;
  797. }
  798. return 0;
  799. }
  800. static SDL_INLINE
  801. void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
  802. {
  803. /* all of these functions are required to be implemented, even as no-ops, so we don't
  804. have to check that they aren't NULL over and over. */
  805. SDL_assert(renderer->QueueSetViewport != NULL);
  806. SDL_assert(renderer->QueueSetDrawColor != NULL);
  807. SDL_assert(renderer->QueueDrawPoints != NULL);
  808. SDL_assert(renderer->QueueDrawLines != NULL || renderer->QueueGeometry != NULL);
  809. SDL_assert(renderer->QueueFillRects != NULL || renderer->QueueGeometry != NULL);
  810. SDL_assert(renderer->QueueCopy != NULL || renderer->QueueGeometry != NULL);
  811. SDL_assert(renderer->RunCommandQueue != NULL);
  812. }
  813. static SDL_RenderLineMethod SDL_GetRenderLineMethod()
  814. {
  815. const char *hint = SDL_GetHint(SDL_HINT_RENDER_LINE_METHOD);
  816. int method = 0;
  817. if (hint) {
  818. method = SDL_atoi(hint);
  819. }
  820. switch (method) {
  821. case 1:
  822. return SDL_RENDERLINEMETHOD_POINTS;
  823. case 2:
  824. return SDL_RENDERLINEMETHOD_LINES;
  825. case 3:
  826. return SDL_RENDERLINEMETHOD_GEOMETRY;
  827. default:
  828. return SDL_RENDERLINEMETHOD_POINTS;
  829. }
  830. }
  831. SDL_Renderer *
  832. SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  833. {
  834. #if !SDL_RENDER_DISABLED
  835. SDL_Renderer *renderer = NULL;
  836. int n = SDL_GetNumRenderDrivers();
  837. SDL_bool batching = SDL_TRUE;
  838. const char *hint;
  839. #if defined(__ANDROID__)
  840. Android_ActivityMutex_Lock_Running();
  841. #endif
  842. if (!window) {
  843. SDL_InvalidParamError("window");
  844. goto error;
  845. }
  846. if (SDL_GetRenderer(window)) {
  847. SDL_SetError("Renderer already associated with window");
  848. goto error;
  849. }
  850. hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
  851. if (hint && *hint) {
  852. if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
  853. flags |= SDL_RENDERER_PRESENTVSYNC;
  854. } else {
  855. flags &= ~SDL_RENDERER_PRESENTVSYNC;
  856. }
  857. }
  858. if (index < 0) {
  859. hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  860. if (hint) {
  861. for (index = 0; index < n; ++index) {
  862. const SDL_RenderDriver *driver = render_drivers[index];
  863. if (SDL_strcasecmp(hint, driver->info.name) == 0) {
  864. /* Create a new renderer instance */
  865. renderer = driver->CreateRenderer(window, flags);
  866. if (renderer) {
  867. batching = SDL_FALSE;
  868. }
  869. break;
  870. }
  871. }
  872. }
  873. if (!renderer) {
  874. for (index = 0; index < n; ++index) {
  875. const SDL_RenderDriver *driver = render_drivers[index];
  876. if ((driver->info.flags & flags) == flags) {
  877. /* Create a new renderer instance */
  878. renderer = driver->CreateRenderer(window, flags);
  879. if (renderer) {
  880. /* Yay, we got one! */
  881. break;
  882. }
  883. }
  884. }
  885. }
  886. if (!renderer) {
  887. SDL_SetError("Couldn't find matching render driver");
  888. goto error;
  889. }
  890. } else {
  891. if (index >= n) {
  892. SDL_SetError("index must be -1 or in the range of 0 - %d",
  893. n - 1);
  894. goto error;
  895. }
  896. /* Create a new renderer instance */
  897. renderer = render_drivers[index]->CreateRenderer(window, flags);
  898. batching = SDL_FALSE;
  899. if (!renderer) {
  900. goto error;
  901. }
  902. }
  903. VerifyDrawQueueFunctions(renderer);
  904. /* let app/user override batching decisions. */
  905. if (renderer->always_batch) {
  906. batching = SDL_TRUE;
  907. } else if (SDL_GetHint(SDL_HINT_RENDER_BATCHING)) {
  908. batching = SDL_GetHintBoolean(SDL_HINT_RENDER_BATCHING, SDL_TRUE);
  909. }
  910. renderer->batching = batching;
  911. renderer->magic = &renderer_magic;
  912. renderer->window = window;
  913. renderer->target_mutex = SDL_CreateMutex();
  914. renderer->scale.x = 1.0f;
  915. renderer->scale.y = 1.0f;
  916. renderer->dpi_scale.x = 1.0f;
  917. renderer->dpi_scale.y = 1.0f;
  918. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  919. renderer->render_command_generation = 1;
  920. if (window && renderer->GetOutputSize) {
  921. int window_w, window_h;
  922. int output_w, output_h;
  923. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  924. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  925. renderer->dpi_scale.x = (float)window_w / output_w;
  926. renderer->dpi_scale.y = (float)window_h / output_h;
  927. }
  928. }
  929. renderer->relative_scaling = SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_SCALING, SDL_TRUE);
  930. renderer->line_method = SDL_GetRenderLineMethod();
  931. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
  932. renderer->hidden = SDL_TRUE;
  933. } else {
  934. renderer->hidden = SDL_FALSE;
  935. }
  936. SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
  937. SDL_RenderSetViewport(renderer, NULL);
  938. SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
  939. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  940. "Created renderer: %s", renderer->info.name);
  941. #if defined(__ANDROID__)
  942. Android_ActivityMutex_Unlock();
  943. #endif
  944. return renderer;
  945. error:
  946. #if defined(__ANDROID__)
  947. Android_ActivityMutex_Unlock();
  948. #endif
  949. return NULL;
  950. #else
  951. SDL_SetError("SDL not built with rendering support");
  952. return NULL;
  953. #endif
  954. }
  955. SDL_Renderer *
  956. SDL_CreateSoftwareRenderer(SDL_Surface * surface)
  957. {
  958. #if !SDL_RENDER_DISABLED && SDL_VIDEO_RENDER_SW
  959. SDL_Renderer *renderer;
  960. renderer = SW_CreateRendererForSurface(surface);
  961. if (renderer) {
  962. VerifyDrawQueueFunctions(renderer);
  963. renderer->magic = &renderer_magic;
  964. renderer->target_mutex = SDL_CreateMutex();
  965. renderer->scale.x = 1.0f;
  966. renderer->scale.y = 1.0f;
  967. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  968. renderer->render_command_generation = 1;
  969. /* Software renderer always uses line method, for speed */
  970. renderer->line_method = SDL_RENDERLINEMETHOD_LINES;
  971. SDL_RenderSetViewport(renderer, NULL);
  972. }
  973. return renderer;
  974. #else
  975. SDL_SetError("SDL not built with rendering support");
  976. return NULL;
  977. #endif /* !SDL_RENDER_DISABLED */
  978. }
  979. SDL_Renderer *
  980. SDL_GetRenderer(SDL_Window * window)
  981. {
  982. return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
  983. }
  984. SDL_Window *
  985. SDL_RenderGetWindow(SDL_Renderer *renderer)
  986. {
  987. CHECK_RENDERER_MAGIC(renderer, NULL);
  988. return renderer->window;
  989. }
  990. int
  991. SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
  992. {
  993. CHECK_RENDERER_MAGIC(renderer, -1);
  994. *info = renderer->info;
  995. return 0;
  996. }
  997. int
  998. SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
  999. {
  1000. CHECK_RENDERER_MAGIC(renderer, -1);
  1001. if (renderer->target) {
  1002. return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
  1003. } else if (renderer->GetOutputSize) {
  1004. return renderer->GetOutputSize(renderer, w, h);
  1005. } else if (renderer->window) {
  1006. SDL_GetWindowSize(renderer->window, w, h);
  1007. return 0;
  1008. } else {
  1009. SDL_assert(0 && "This should never happen");
  1010. return SDL_SetError("Renderer doesn't support querying output size");
  1011. }
  1012. }
  1013. static SDL_bool
  1014. IsSupportedBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  1015. {
  1016. switch (blendMode)
  1017. {
  1018. /* These are required to be supported by all renderers */
  1019. case SDL_BLENDMODE_NONE:
  1020. case SDL_BLENDMODE_BLEND:
  1021. case SDL_BLENDMODE_ADD:
  1022. case SDL_BLENDMODE_MOD:
  1023. case SDL_BLENDMODE_MUL:
  1024. return SDL_TRUE;
  1025. default:
  1026. return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
  1027. }
  1028. }
  1029. static SDL_bool
  1030. IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  1031. {
  1032. Uint32 i;
  1033. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1034. if (renderer->info.texture_formats[i] == format) {
  1035. return SDL_TRUE;
  1036. }
  1037. }
  1038. return SDL_FALSE;
  1039. }
  1040. static Uint32
  1041. GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  1042. {
  1043. Uint32 i;
  1044. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  1045. /* Look for an exact match */
  1046. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1047. if (renderer->info.texture_formats[i] == format) {
  1048. return renderer->info.texture_formats[i];
  1049. }
  1050. }
  1051. } else {
  1052. SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  1053. /* We just want to match the first format that has the same channels */
  1054. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1055. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  1056. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
  1057. return renderer->info.texture_formats[i];
  1058. }
  1059. }
  1060. }
  1061. return renderer->info.texture_formats[0];
  1062. }
  1063. static SDL_ScaleMode SDL_GetScaleMode(void)
  1064. {
  1065. const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
  1066. if (!hint || SDL_strcasecmp(hint, "nearest") == 0) {
  1067. return SDL_ScaleModeNearest;
  1068. } else if (SDL_strcasecmp(hint, "linear") == 0) {
  1069. return SDL_ScaleModeLinear;
  1070. } else if (SDL_strcasecmp(hint, "best") == 0) {
  1071. return SDL_ScaleModeBest;
  1072. } else {
  1073. return (SDL_ScaleMode)SDL_atoi(hint);
  1074. }
  1075. }
  1076. SDL_Texture *
  1077. SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
  1078. {
  1079. SDL_Texture *texture;
  1080. SDL_bool texture_is_fourcc_and_target;
  1081. CHECK_RENDERER_MAGIC(renderer, NULL);
  1082. if (!format) {
  1083. format = renderer->info.texture_formats[0];
  1084. }
  1085. if (SDL_BYTESPERPIXEL(format) == 0) {
  1086. SDL_SetError("Invalid texture format");
  1087. return NULL;
  1088. }
  1089. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1090. if (!IsSupportedFormat(renderer, format)) {
  1091. SDL_SetError("Palettized textures are not supported");
  1092. return NULL;
  1093. }
  1094. }
  1095. if (w <= 0 || h <= 0) {
  1096. SDL_SetError("Texture dimensions can't be 0");
  1097. return NULL;
  1098. }
  1099. if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
  1100. (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
  1101. SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
  1102. return NULL;
  1103. }
  1104. texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1105. if (!texture) {
  1106. SDL_OutOfMemory();
  1107. return NULL;
  1108. }
  1109. texture->magic = &texture_magic;
  1110. texture->format = format;
  1111. texture->access = access;
  1112. texture->w = w;
  1113. texture->h = h;
  1114. texture->color.r = 255;
  1115. texture->color.g = 255;
  1116. texture->color.b = 255;
  1117. texture->color.a = 255;
  1118. texture->scaleMode = SDL_GetScaleMode();
  1119. texture->renderer = renderer;
  1120. texture->next = renderer->textures;
  1121. if (renderer->textures) {
  1122. renderer->textures->prev = texture;
  1123. }
  1124. renderer->textures = texture;
  1125. /* FOURCC format cannot be used directly by renderer back-ends for target texture */
  1126. texture_is_fourcc_and_target = (access == SDL_TEXTUREACCESS_TARGET && SDL_ISPIXELFORMAT_FOURCC(texture->format));
  1127. if (texture_is_fourcc_and_target == SDL_FALSE && IsSupportedFormat(renderer, format)) {
  1128. if (renderer->CreateTexture(renderer, texture) < 0) {
  1129. SDL_DestroyTexture(texture);
  1130. return NULL;
  1131. }
  1132. } else {
  1133. int closest_format;
  1134. if (texture_is_fourcc_and_target == SDL_FALSE) {
  1135. closest_format = GetClosestSupportedFormat(renderer, format);
  1136. } else {
  1137. closest_format = renderer->info.texture_formats[0];
  1138. }
  1139. texture->native = SDL_CreateTexture(renderer, closest_format, access, w, h);
  1140. if (!texture->native) {
  1141. SDL_DestroyTexture(texture);
  1142. return NULL;
  1143. }
  1144. /* Swap textures to have texture before texture->native in the list */
  1145. texture->native->next = texture->next;
  1146. if (texture->native->next) {
  1147. texture->native->next->prev = texture->native;
  1148. }
  1149. texture->prev = texture->native->prev;
  1150. if (texture->prev) {
  1151. texture->prev->next = texture;
  1152. }
  1153. texture->native->prev = texture;
  1154. texture->next = texture->native;
  1155. renderer->textures = texture;
  1156. if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  1157. #if SDL_HAVE_YUV
  1158. texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
  1159. #else
  1160. SDL_SetError("SDL not built with YUV support");
  1161. #endif
  1162. if (!texture->yuv) {
  1163. SDL_DestroyTexture(texture);
  1164. return NULL;
  1165. }
  1166. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  1167. /* The pitch is 4 byte aligned */
  1168. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  1169. texture->pixels = SDL_calloc(1, texture->pitch * h);
  1170. if (!texture->pixels) {
  1171. SDL_DestroyTexture(texture);
  1172. return NULL;
  1173. }
  1174. }
  1175. }
  1176. return texture;
  1177. }
  1178. SDL_Texture *
  1179. SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
  1180. {
  1181. const SDL_PixelFormat *fmt;
  1182. SDL_bool needAlpha;
  1183. SDL_bool direct_update;
  1184. int i;
  1185. Uint32 format = SDL_PIXELFORMAT_UNKNOWN;
  1186. SDL_Texture *texture;
  1187. CHECK_RENDERER_MAGIC(renderer, NULL);
  1188. if (!surface) {
  1189. SDL_InvalidParamError("SDL_CreateTextureFromSurface(): surface");
  1190. return NULL;
  1191. }
  1192. /* See what the best texture format is */
  1193. fmt = surface->format;
  1194. if (fmt->Amask || SDL_HasColorKey(surface)) {
  1195. needAlpha = SDL_TRUE;
  1196. } else {
  1197. needAlpha = SDL_FALSE;
  1198. }
  1199. /* If Palette contains alpha values, promotes to alpha format */
  1200. if (fmt->palette) {
  1201. SDL_bool is_opaque, has_alpha_channel;
  1202. SDL_DetectPalette(fmt->palette, &is_opaque, &has_alpha_channel);
  1203. if (!is_opaque) {
  1204. needAlpha = SDL_TRUE;
  1205. }
  1206. }
  1207. /* Try to have the best pixel format for the texture */
  1208. /* No alpha, but a colorkey => promote to alpha */
  1209. if (!fmt->Amask && SDL_HasColorKey(surface)) {
  1210. if (fmt->format == SDL_PIXELFORMAT_RGB888) {
  1211. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1212. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ARGB8888) {
  1213. format = SDL_PIXELFORMAT_ARGB8888;
  1214. break;
  1215. }
  1216. }
  1217. } else if (fmt->format == SDL_PIXELFORMAT_BGR888) {
  1218. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1219. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ABGR8888) {
  1220. format = SDL_PIXELFORMAT_ABGR8888;
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. } else {
  1226. /* Exact match would be fine */
  1227. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1228. if (renderer->info.texture_formats[i] == fmt->format) {
  1229. format = fmt->format;
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. /* Fallback, choose a valid pixel format */
  1235. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1236. format = renderer->info.texture_formats[0];
  1237. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1238. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  1239. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
  1240. format = renderer->info.texture_formats[i];
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
  1246. surface->w, surface->h);
  1247. if (!texture) {
  1248. return NULL;
  1249. }
  1250. if (format == surface->format->format) {
  1251. if (surface->format->Amask && SDL_HasColorKey(surface)) {
  1252. /* Surface and Renderer formats are identicals.
  1253. * Intermediate conversion is needed to convert color key to alpha (SDL_ConvertColorkeyToAlpha()). */
  1254. direct_update = SDL_FALSE;
  1255. } else {
  1256. /* Update Texture directly */
  1257. direct_update = SDL_TRUE;
  1258. }
  1259. } else {
  1260. /* Surface and Renderer formats are differents, it needs an intermediate conversion. */
  1261. direct_update = SDL_FALSE;
  1262. }
  1263. if (direct_update) {
  1264. if (SDL_MUSTLOCK(surface)) {
  1265. SDL_LockSurface(surface);
  1266. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1267. SDL_UnlockSurface(surface);
  1268. } else {
  1269. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1270. }
  1271. #if SDL_VIDEO_RENDER_DIRECTFB
  1272. /* DirectFB allows palette format for textures.
  1273. * Copy SDL_Surface palette to the texture */
  1274. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1275. if (SDL_strcasecmp(renderer->info.name, "directfb") == 0) {
  1276. extern void DirectFB_SetTexturePalette(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Palette *pal);
  1277. DirectFB_SetTexturePalette(renderer, texture, surface->format->palette);
  1278. }
  1279. }
  1280. #endif
  1281. } else {
  1282. SDL_PixelFormat *dst_fmt;
  1283. SDL_Surface *temp = NULL;
  1284. /* Set up a destination surface for the texture update */
  1285. dst_fmt = SDL_AllocFormat(format);
  1286. if (!dst_fmt) {
  1287. SDL_DestroyTexture(texture);
  1288. return NULL;
  1289. }
  1290. temp = SDL_ConvertSurface(surface, dst_fmt, 0);
  1291. SDL_FreeFormat(dst_fmt);
  1292. if (temp) {
  1293. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  1294. SDL_FreeSurface(temp);
  1295. } else {
  1296. SDL_DestroyTexture(texture);
  1297. return NULL;
  1298. }
  1299. }
  1300. {
  1301. Uint8 r, g, b, a;
  1302. SDL_BlendMode blendMode;
  1303. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1304. SDL_SetTextureColorMod(texture, r, g, b);
  1305. SDL_GetSurfaceAlphaMod(surface, &a);
  1306. SDL_SetTextureAlphaMod(texture, a);
  1307. if (SDL_HasColorKey(surface)) {
  1308. /* We converted to a texture with alpha format */
  1309. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  1310. } else {
  1311. SDL_GetSurfaceBlendMode(surface, &blendMode);
  1312. SDL_SetTextureBlendMode(texture, blendMode);
  1313. }
  1314. }
  1315. return texture;
  1316. }
  1317. int
  1318. SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  1319. int *w, int *h)
  1320. {
  1321. CHECK_TEXTURE_MAGIC(texture, -1);
  1322. if (format) {
  1323. *format = texture->format;
  1324. }
  1325. if (access) {
  1326. *access = texture->access;
  1327. }
  1328. if (w) {
  1329. *w = texture->w;
  1330. }
  1331. if (h) {
  1332. *h = texture->h;
  1333. }
  1334. return 0;
  1335. }
  1336. int
  1337. SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  1338. {
  1339. CHECK_TEXTURE_MAGIC(texture, -1);
  1340. if (r < 255 || g < 255 || b < 255) {
  1341. texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1342. } else {
  1343. texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1344. }
  1345. texture->color.r = r;
  1346. texture->color.g = g;
  1347. texture->color.b = b;
  1348. if (texture->native) {
  1349. return SDL_SetTextureColorMod(texture->native, r, g, b);
  1350. }
  1351. return 0;
  1352. }
  1353. int
  1354. SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  1355. Uint8 * b)
  1356. {
  1357. CHECK_TEXTURE_MAGIC(texture, -1);
  1358. if (r) {
  1359. *r = texture->color.r;
  1360. }
  1361. if (g) {
  1362. *g = texture->color.g;
  1363. }
  1364. if (b) {
  1365. *b = texture->color.b;
  1366. }
  1367. return 0;
  1368. }
  1369. int
  1370. SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  1371. {
  1372. CHECK_TEXTURE_MAGIC(texture, -1);
  1373. if (alpha < 255) {
  1374. texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1375. } else {
  1376. texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1377. }
  1378. texture->color.a = alpha;
  1379. if (texture->native) {
  1380. return SDL_SetTextureAlphaMod(texture->native, alpha);
  1381. }
  1382. return 0;
  1383. }
  1384. int
  1385. SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  1386. {
  1387. CHECK_TEXTURE_MAGIC(texture, -1);
  1388. if (alpha) {
  1389. *alpha = texture->color.a;
  1390. }
  1391. return 0;
  1392. }
  1393. int
  1394. SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
  1395. {
  1396. SDL_Renderer *renderer;
  1397. CHECK_TEXTURE_MAGIC(texture, -1);
  1398. renderer = texture->renderer;
  1399. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1400. return SDL_Unsupported();
  1401. }
  1402. texture->blendMode = blendMode;
  1403. if (texture->native) {
  1404. return SDL_SetTextureBlendMode(texture->native, blendMode);
  1405. }
  1406. return 0;
  1407. }
  1408. int
  1409. SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
  1410. {
  1411. CHECK_TEXTURE_MAGIC(texture, -1);
  1412. if (blendMode) {
  1413. *blendMode = texture->blendMode;
  1414. }
  1415. return 0;
  1416. }
  1417. int
  1418. SDL_SetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode scaleMode)
  1419. {
  1420. SDL_Renderer *renderer;
  1421. CHECK_TEXTURE_MAGIC(texture, -1);
  1422. renderer = texture->renderer;
  1423. texture->scaleMode = scaleMode;
  1424. if (texture->native) {
  1425. return SDL_SetTextureScaleMode(texture->native, scaleMode);
  1426. } else {
  1427. renderer->SetTextureScaleMode(renderer, texture, scaleMode);
  1428. }
  1429. return 0;
  1430. }
  1431. int
  1432. SDL_GetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode *scaleMode)
  1433. {
  1434. CHECK_TEXTURE_MAGIC(texture, -1);
  1435. if (scaleMode) {
  1436. *scaleMode = texture->scaleMode;
  1437. }
  1438. return 0;
  1439. }
  1440. int
  1441. SDL_SetTextureUserData(SDL_Texture * texture, void *userdata)
  1442. {
  1443. CHECK_TEXTURE_MAGIC(texture, -1);
  1444. texture->userdata = userdata;
  1445. return 0;
  1446. }
  1447. void *
  1448. SDL_GetTextureUserData(SDL_Texture * texture)
  1449. {
  1450. CHECK_TEXTURE_MAGIC(texture, NULL);
  1451. return texture->userdata;
  1452. }
  1453. #if SDL_HAVE_YUV
  1454. static int
  1455. SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  1456. const void *pixels, int pitch)
  1457. {
  1458. SDL_Texture *native = texture->native;
  1459. SDL_Rect full_rect;
  1460. if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
  1461. return -1;
  1462. }
  1463. full_rect.x = 0;
  1464. full_rect.y = 0;
  1465. full_rect.w = texture->w;
  1466. full_rect.h = texture->h;
  1467. rect = &full_rect;
  1468. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1469. /* We can lock the texture and copy to it */
  1470. void *native_pixels = NULL;
  1471. int native_pitch = 0;
  1472. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1473. return -1;
  1474. }
  1475. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1476. rect->w, rect->h, native_pixels, native_pitch);
  1477. SDL_UnlockTexture(native);
  1478. } else {
  1479. /* Use a temporary buffer for updating */
  1480. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1481. const size_t alloclen = rect->h * temp_pitch;
  1482. if (alloclen > 0) {
  1483. void *temp_pixels = SDL_malloc(alloclen);
  1484. if (!temp_pixels) {
  1485. return SDL_OutOfMemory();
  1486. }
  1487. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1488. rect->w, rect->h, temp_pixels, temp_pitch);
  1489. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1490. SDL_free(temp_pixels);
  1491. }
  1492. }
  1493. return 0;
  1494. }
  1495. #endif /* SDL_HAVE_YUV */
  1496. static int
  1497. SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  1498. const void *pixels, int pitch)
  1499. {
  1500. SDL_Texture *native = texture->native;
  1501. if (!rect->w || !rect->h) {
  1502. return 0; /* nothing to do. */
  1503. }
  1504. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1505. /* We can lock the texture and copy to it */
  1506. void *native_pixels = NULL;
  1507. int native_pitch = 0;
  1508. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1509. return -1;
  1510. }
  1511. SDL_ConvertPixels(rect->w, rect->h,
  1512. texture->format, pixels, pitch,
  1513. native->format, native_pixels, native_pitch);
  1514. SDL_UnlockTexture(native);
  1515. } else {
  1516. /* Use a temporary buffer for updating */
  1517. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1518. const size_t alloclen = rect->h * temp_pitch;
  1519. if (alloclen > 0) {
  1520. void *temp_pixels = SDL_malloc(alloclen);
  1521. if (!temp_pixels) {
  1522. return SDL_OutOfMemory();
  1523. }
  1524. SDL_ConvertPixels(rect->w, rect->h,
  1525. texture->format, pixels, pitch,
  1526. native->format, temp_pixels, temp_pitch);
  1527. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1528. SDL_free(temp_pixels);
  1529. }
  1530. }
  1531. return 0;
  1532. }
  1533. int
  1534. SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1535. const void *pixels, int pitch)
  1536. {
  1537. SDL_Rect real_rect;
  1538. CHECK_TEXTURE_MAGIC(texture, -1);
  1539. if (!pixels) {
  1540. return SDL_InvalidParamError("pixels");
  1541. }
  1542. if (!pitch) {
  1543. return SDL_InvalidParamError("pitch");
  1544. }
  1545. real_rect.x = 0;
  1546. real_rect.y = 0;
  1547. real_rect.w = texture->w;
  1548. real_rect.h = texture->h;
  1549. if (rect) {
  1550. if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1551. return 0;
  1552. }
  1553. }
  1554. if (real_rect.w == 0 || real_rect.h == 0) {
  1555. return 0; /* nothing to do. */
  1556. #if SDL_HAVE_YUV
  1557. } else if (texture->yuv) {
  1558. return SDL_UpdateTextureYUV(texture, &real_rect, pixels, pitch);
  1559. #endif
  1560. } else if (texture->native) {
  1561. return SDL_UpdateTextureNative(texture, &real_rect, pixels, pitch);
  1562. } else {
  1563. SDL_Renderer *renderer = texture->renderer;
  1564. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1565. return -1;
  1566. }
  1567. return renderer->UpdateTexture(renderer, texture, &real_rect, pixels, pitch);
  1568. }
  1569. }
  1570. #if SDL_HAVE_YUV
  1571. static int
  1572. SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
  1573. const Uint8 *Yplane, int Ypitch,
  1574. const Uint8 *Uplane, int Upitch,
  1575. const Uint8 *Vplane, int Vpitch)
  1576. {
  1577. SDL_Texture *native = texture->native;
  1578. SDL_Rect full_rect;
  1579. if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
  1580. return -1;
  1581. }
  1582. full_rect.x = 0;
  1583. full_rect.y = 0;
  1584. full_rect.w = texture->w;
  1585. full_rect.h = texture->h;
  1586. rect = &full_rect;
  1587. if (!rect->w || !rect->h) {
  1588. return 0; /* nothing to do. */
  1589. }
  1590. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1591. /* We can lock the texture and copy to it */
  1592. void *native_pixels = NULL;
  1593. int native_pitch = 0;
  1594. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1595. return -1;
  1596. }
  1597. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1598. rect->w, rect->h, native_pixels, native_pitch);
  1599. SDL_UnlockTexture(native);
  1600. } else {
  1601. /* Use a temporary buffer for updating */
  1602. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1603. const size_t alloclen = rect->h * temp_pitch;
  1604. if (alloclen > 0) {
  1605. void *temp_pixels = SDL_malloc(alloclen);
  1606. if (!temp_pixels) {
  1607. return SDL_OutOfMemory();
  1608. }
  1609. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1610. rect->w, rect->h, temp_pixels, temp_pitch);
  1611. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1612. SDL_free(temp_pixels);
  1613. }
  1614. }
  1615. return 0;
  1616. }
  1617. static int
  1618. SDL_UpdateTextureNVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
  1619. const Uint8 *Yplane, int Ypitch,
  1620. const Uint8 *UVplane, int UVpitch)
  1621. {
  1622. SDL_Texture *native = texture->native;
  1623. SDL_Rect full_rect;
  1624. if (SDL_SW_UpdateNVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, UVplane, UVpitch) < 0) {
  1625. return -1;
  1626. }
  1627. full_rect.x = 0;
  1628. full_rect.y = 0;
  1629. full_rect.w = texture->w;
  1630. full_rect.h = texture->h;
  1631. rect = &full_rect;
  1632. if (!rect->w || !rect->h) {
  1633. return 0; /* nothing to do. */
  1634. }
  1635. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1636. /* We can lock the texture and copy to it */
  1637. void *native_pixels = NULL;
  1638. int native_pitch = 0;
  1639. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1640. return -1;
  1641. }
  1642. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1643. rect->w, rect->h, native_pixels, native_pitch);
  1644. SDL_UnlockTexture(native);
  1645. } else {
  1646. /* Use a temporary buffer for updating */
  1647. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1648. const size_t alloclen = rect->h * temp_pitch;
  1649. if (alloclen > 0) {
  1650. void *temp_pixels = SDL_malloc(alloclen);
  1651. if (!temp_pixels) {
  1652. return SDL_OutOfMemory();
  1653. }
  1654. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1655. rect->w, rect->h, temp_pixels, temp_pitch);
  1656. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1657. SDL_free(temp_pixels);
  1658. }
  1659. }
  1660. return 0;
  1661. }
  1662. #endif /* SDL_HAVE_YUV */
  1663. int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1664. const Uint8 *Yplane, int Ypitch,
  1665. const Uint8 *Uplane, int Upitch,
  1666. const Uint8 *Vplane, int Vpitch)
  1667. {
  1668. #if SDL_HAVE_YUV
  1669. SDL_Renderer *renderer;
  1670. SDL_Rect real_rect;
  1671. CHECK_TEXTURE_MAGIC(texture, -1);
  1672. if (!Yplane) {
  1673. return SDL_InvalidParamError("Yplane");
  1674. }
  1675. if (!Ypitch) {
  1676. return SDL_InvalidParamError("Ypitch");
  1677. }
  1678. if (!Uplane) {
  1679. return SDL_InvalidParamError("Uplane");
  1680. }
  1681. if (!Upitch) {
  1682. return SDL_InvalidParamError("Upitch");
  1683. }
  1684. if (!Vplane) {
  1685. return SDL_InvalidParamError("Vplane");
  1686. }
  1687. if (!Vpitch) {
  1688. return SDL_InvalidParamError("Vpitch");
  1689. }
  1690. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  1691. texture->format != SDL_PIXELFORMAT_IYUV) {
  1692. return SDL_SetError("Texture format must by YV12 or IYUV");
  1693. }
  1694. real_rect.x = 0;
  1695. real_rect.y = 0;
  1696. real_rect.w = texture->w;
  1697. real_rect.h = texture->h;
  1698. if (rect) {
  1699. SDL_IntersectRect(rect, &real_rect, &real_rect);
  1700. }
  1701. if (real_rect.w == 0 || real_rect.h == 0) {
  1702. return 0; /* nothing to do. */
  1703. }
  1704. if (texture->yuv) {
  1705. return SDL_UpdateTextureYUVPlanar(texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1706. } else {
  1707. SDL_assert(!texture->native);
  1708. renderer = texture->renderer;
  1709. SDL_assert(renderer->UpdateTextureYUV);
  1710. if (renderer->UpdateTextureYUV) {
  1711. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1712. return -1;
  1713. }
  1714. return renderer->UpdateTextureYUV(renderer, texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1715. } else {
  1716. return SDL_Unsupported();
  1717. }
  1718. }
  1719. #else
  1720. return -1;
  1721. #endif
  1722. }
  1723. int SDL_UpdateNVTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1724. const Uint8 *Yplane, int Ypitch,
  1725. const Uint8 *UVplane, int UVpitch)
  1726. {
  1727. #if SDL_HAVE_YUV
  1728. SDL_Renderer *renderer;
  1729. SDL_Rect real_rect;
  1730. CHECK_TEXTURE_MAGIC(texture, -1);
  1731. if (!Yplane) {
  1732. return SDL_InvalidParamError("Yplane");
  1733. }
  1734. if (!Ypitch) {
  1735. return SDL_InvalidParamError("Ypitch");
  1736. }
  1737. if (!UVplane) {
  1738. return SDL_InvalidParamError("UVplane");
  1739. }
  1740. if (!UVpitch) {
  1741. return SDL_InvalidParamError("UVpitch");
  1742. }
  1743. if (texture->format != SDL_PIXELFORMAT_NV12 &&
  1744. texture->format != SDL_PIXELFORMAT_NV21) {
  1745. return SDL_SetError("Texture format must by NV12 or NV21");
  1746. }
  1747. real_rect.x = 0;
  1748. real_rect.y = 0;
  1749. real_rect.w = texture->w;
  1750. real_rect.h = texture->h;
  1751. if (rect) {
  1752. SDL_IntersectRect(rect, &real_rect, &real_rect);
  1753. }
  1754. if (real_rect.w == 0 || real_rect.h == 0) {
  1755. return 0; /* nothing to do. */
  1756. }
  1757. if (texture->yuv) {
  1758. return SDL_UpdateTextureNVPlanar(texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  1759. } else {
  1760. SDL_assert(!texture->native);
  1761. renderer = texture->renderer;
  1762. SDL_assert(renderer->UpdateTextureNV);
  1763. if (renderer->UpdateTextureNV) {
  1764. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1765. return -1;
  1766. }
  1767. return renderer->UpdateTextureNV(renderer, texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  1768. } else {
  1769. return SDL_Unsupported();
  1770. }
  1771. }
  1772. #else
  1773. return -1;
  1774. #endif
  1775. }
  1776. #if SDL_HAVE_YUV
  1777. static int
  1778. SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  1779. void **pixels, int *pitch)
  1780. {
  1781. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  1782. }
  1783. #endif /* SDL_HAVE_YUV */
  1784. static int
  1785. SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  1786. void **pixels, int *pitch)
  1787. {
  1788. texture->locked_rect = *rect;
  1789. *pixels = (void *) ((Uint8 *) texture->pixels +
  1790. rect->y * texture->pitch +
  1791. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1792. *pitch = texture->pitch;
  1793. return 0;
  1794. }
  1795. int
  1796. SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1797. void **pixels, int *pitch)
  1798. {
  1799. SDL_Rect full_rect;
  1800. CHECK_TEXTURE_MAGIC(texture, -1);
  1801. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1802. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1803. }
  1804. if (!rect) {
  1805. full_rect.x = 0;
  1806. full_rect.y = 0;
  1807. full_rect.w = texture->w;
  1808. full_rect.h = texture->h;
  1809. rect = &full_rect;
  1810. }
  1811. #if SDL_HAVE_YUV
  1812. if (texture->yuv) {
  1813. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1814. return -1;
  1815. }
  1816. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  1817. } else
  1818. #endif
  1819. if (texture->native) {
  1820. /* Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then. */
  1821. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  1822. } else {
  1823. SDL_Renderer *renderer = texture->renderer;
  1824. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1825. return -1;
  1826. }
  1827. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  1828. }
  1829. }
  1830. int
  1831. SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
  1832. SDL_Surface **surface)
  1833. {
  1834. SDL_Rect real_rect;
  1835. void *pixels = NULL;
  1836. int pitch = 0; /* fix static analysis */
  1837. int ret;
  1838. if (texture == NULL || surface == NULL) {
  1839. return -1;
  1840. }
  1841. real_rect.x = 0;
  1842. real_rect.y = 0;
  1843. real_rect.w = texture->w;
  1844. real_rect.h = texture->h;
  1845. if (rect) {
  1846. SDL_IntersectRect(rect, &real_rect, &real_rect);
  1847. }
  1848. ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
  1849. if (ret < 0) {
  1850. return ret;
  1851. }
  1852. texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, real_rect.w, real_rect.h, 0, pitch, texture->format);
  1853. if (texture->locked_surface == NULL) {
  1854. SDL_UnlockTexture(texture);
  1855. return -1;
  1856. }
  1857. *surface = texture->locked_surface;
  1858. return 0;
  1859. }
  1860. #if SDL_HAVE_YUV
  1861. static void
  1862. SDL_UnlockTextureYUV(SDL_Texture * texture)
  1863. {
  1864. SDL_Texture *native = texture->native;
  1865. void *native_pixels = NULL;
  1866. int native_pitch = 0;
  1867. SDL_Rect rect;
  1868. rect.x = 0;
  1869. rect.y = 0;
  1870. rect.w = texture->w;
  1871. rect.h = texture->h;
  1872. if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
  1873. return;
  1874. }
  1875. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  1876. rect.w, rect.h, native_pixels, native_pitch);
  1877. SDL_UnlockTexture(native);
  1878. }
  1879. #endif /* SDL_HAVE_YUV */
  1880. static void
  1881. SDL_UnlockTextureNative(SDL_Texture * texture)
  1882. {
  1883. SDL_Texture *native = texture->native;
  1884. void *native_pixels = NULL;
  1885. int native_pitch = 0;
  1886. const SDL_Rect *rect = &texture->locked_rect;
  1887. const void* pixels = (void *) ((Uint8 *) texture->pixels +
  1888. rect->y * texture->pitch +
  1889. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1890. int pitch = texture->pitch;
  1891. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1892. return;
  1893. }
  1894. SDL_ConvertPixels(rect->w, rect->h,
  1895. texture->format, pixels, pitch,
  1896. native->format, native_pixels, native_pitch);
  1897. SDL_UnlockTexture(native);
  1898. }
  1899. void
  1900. SDL_UnlockTexture(SDL_Texture * texture)
  1901. {
  1902. CHECK_TEXTURE_MAGIC(texture, );
  1903. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1904. return;
  1905. }
  1906. #if SDL_HAVE_YUV
  1907. if (texture->yuv) {
  1908. SDL_UnlockTextureYUV(texture);
  1909. } else
  1910. #endif
  1911. if (texture->native) {
  1912. SDL_UnlockTextureNative(texture);
  1913. } else {
  1914. SDL_Renderer *renderer = texture->renderer;
  1915. renderer->UnlockTexture(renderer, texture);
  1916. }
  1917. SDL_FreeSurface(texture->locked_surface);
  1918. texture->locked_surface = NULL;
  1919. }
  1920. SDL_bool
  1921. SDL_RenderTargetSupported(SDL_Renderer *renderer)
  1922. {
  1923. if (!renderer || !renderer->SetRenderTarget) {
  1924. return SDL_FALSE;
  1925. }
  1926. return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
  1927. }
  1928. int
  1929. SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  1930. {
  1931. if (!SDL_RenderTargetSupported(renderer)) {
  1932. return SDL_Unsupported();
  1933. }
  1934. /* texture == NULL is valid and means reset the target to the window */
  1935. if (texture) {
  1936. CHECK_TEXTURE_MAGIC(texture, -1);
  1937. if (renderer != texture->renderer) {
  1938. return SDL_SetError("Texture was not created with this renderer");
  1939. }
  1940. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  1941. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  1942. }
  1943. if (texture->native) {
  1944. /* Always render to the native texture */
  1945. texture = texture->native;
  1946. }
  1947. }
  1948. if (texture == renderer->target) {
  1949. /* Nothing to do! */
  1950. return 0;
  1951. }
  1952. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  1953. SDL_LockMutex(renderer->target_mutex);
  1954. if (texture && !renderer->target) {
  1955. /* Make a backup of the viewport */
  1956. renderer->viewport_backup = renderer->viewport;
  1957. renderer->clip_rect_backup = renderer->clip_rect;
  1958. renderer->clipping_enabled_backup = renderer->clipping_enabled;
  1959. renderer->scale_backup = renderer->scale;
  1960. renderer->logical_w_backup = renderer->logical_w;
  1961. renderer->logical_h_backup = renderer->logical_h;
  1962. }
  1963. renderer->target = texture;
  1964. if (renderer->SetRenderTarget(renderer, texture) < 0) {
  1965. SDL_UnlockMutex(renderer->target_mutex);
  1966. return -1;
  1967. }
  1968. if (texture) {
  1969. renderer->viewport.x = (double)0;
  1970. renderer->viewport.y = (double)0;
  1971. renderer->viewport.w = (double)texture->w;
  1972. renderer->viewport.h = (double)texture->h;
  1973. SDL_zero(renderer->clip_rect);
  1974. renderer->clipping_enabled = SDL_FALSE;
  1975. renderer->scale.x = 1.0f;
  1976. renderer->scale.y = 1.0f;
  1977. renderer->logical_w = texture->w;
  1978. renderer->logical_h = texture->h;
  1979. } else {
  1980. renderer->viewport = renderer->viewport_backup;
  1981. renderer->clip_rect = renderer->clip_rect_backup;
  1982. renderer->clipping_enabled = renderer->clipping_enabled_backup;
  1983. renderer->scale = renderer->scale_backup;
  1984. renderer->logical_w = renderer->logical_w_backup;
  1985. renderer->logical_h = renderer->logical_h_backup;
  1986. }
  1987. SDL_UnlockMutex(renderer->target_mutex);
  1988. if (QueueCmdSetViewport(renderer) < 0) {
  1989. return -1;
  1990. }
  1991. if (QueueCmdSetClipRect(renderer) < 0) {
  1992. return -1;
  1993. }
  1994. /* All set! */
  1995. return FlushRenderCommandsIfNotBatching(renderer);
  1996. }
  1997. SDL_Texture *
  1998. SDL_GetRenderTarget(SDL_Renderer *renderer)
  1999. {
  2000. CHECK_RENDERER_MAGIC(renderer, NULL);
  2001. return renderer->target;
  2002. }
  2003. static int
  2004. UpdateLogicalSize(SDL_Renderer *renderer, SDL_bool flush_viewport_cmd)
  2005. {
  2006. int w = 1, h = 1;
  2007. float want_aspect;
  2008. float real_aspect;
  2009. float scale;
  2010. SDL_Rect viewport;
  2011. /* 0 is for letterbox, 1 is for overscan */
  2012. int scale_policy = 0;
  2013. const char *hint;
  2014. if (!renderer->logical_w || !renderer->logical_h) {
  2015. return 0;
  2016. }
  2017. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  2018. return -1;
  2019. }
  2020. hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
  2021. if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
  2022. #if SDL_VIDEO_RENDER_D3D
  2023. SDL_bool overscan_supported = SDL_TRUE;
  2024. /* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
  2025. which the overscan implementation relies on.
  2026. */
  2027. if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
  2028. overscan_supported = SDL_FALSE;
  2029. }
  2030. if (overscan_supported) {
  2031. scale_policy = 1;
  2032. }
  2033. #else
  2034. scale_policy = 1;
  2035. #endif
  2036. }
  2037. want_aspect = (float)renderer->logical_w / renderer->logical_h;
  2038. real_aspect = (float)w / h;
  2039. /* Clear the scale because we're setting viewport in output coordinates */
  2040. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  2041. if (renderer->integer_scale) {
  2042. if (want_aspect > real_aspect) {
  2043. scale = (float)(w / renderer->logical_w);
  2044. } else {
  2045. scale = (float)(h / renderer->logical_h);
  2046. }
  2047. if (scale < 1.0f) {
  2048. scale = 1.0f;
  2049. }
  2050. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2051. viewport.x = (w - viewport.w) / 2;
  2052. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2053. viewport.y = (h - viewport.h) / 2;
  2054. } else if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
  2055. /* The aspect ratios are the same, just scale appropriately */
  2056. scale = (float)w / renderer->logical_w;
  2057. SDL_zero(viewport);
  2058. SDL_GetRendererOutputSize(renderer, &viewport.w, &viewport.h);
  2059. } else if (want_aspect > real_aspect) {
  2060. if (scale_policy == 1) {
  2061. /* We want a wider aspect ratio than is available -
  2062. zoom so logical height matches the real height
  2063. and the width will grow off the screen
  2064. */
  2065. scale = (float)h / renderer->logical_h;
  2066. viewport.y = 0;
  2067. viewport.h = h;
  2068. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2069. viewport.x = (w - viewport.w) / 2;
  2070. } else {
  2071. /* We want a wider aspect ratio than is available - letterbox it */
  2072. scale = (float)w / renderer->logical_w;
  2073. viewport.x = 0;
  2074. viewport.w = w;
  2075. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2076. viewport.y = (h - viewport.h) / 2;
  2077. }
  2078. } else {
  2079. if (scale_policy == 1) {
  2080. /* We want a narrower aspect ratio than is available -
  2081. zoom so logical width matches the real width
  2082. and the height will grow off the screen
  2083. */
  2084. scale = (float)w / renderer->logical_w;
  2085. viewport.x = 0;
  2086. viewport.w = w;
  2087. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2088. viewport.y = (h - viewport.h) / 2;
  2089. } else {
  2090. /* We want a narrower aspect ratio than is available - use side-bars */
  2091. scale = (float)h / renderer->logical_h;
  2092. viewport.y = 0;
  2093. viewport.h = h;
  2094. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2095. viewport.x = (w - viewport.w) / 2;
  2096. }
  2097. }
  2098. /* Set the new viewport */
  2099. renderer->viewport.x = (double)viewport.x * renderer->scale.x;
  2100. renderer->viewport.y = (double)viewport.y * renderer->scale.y;
  2101. renderer->viewport.w = (double)viewport.w * renderer->scale.x;
  2102. renderer->viewport.h = (double)viewport.h * renderer->scale.y;
  2103. QueueCmdSetViewport(renderer);
  2104. if (flush_viewport_cmd) {
  2105. FlushRenderCommandsIfNotBatching(renderer);
  2106. }
  2107. /* Set the new scale */
  2108. SDL_RenderSetScale(renderer, scale, scale);
  2109. return 0;
  2110. }
  2111. int
  2112. SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
  2113. {
  2114. CHECK_RENDERER_MAGIC(renderer, -1);
  2115. if (!w || !h) {
  2116. /* Clear any previous logical resolution */
  2117. renderer->logical_w = 0;
  2118. renderer->logical_h = 0;
  2119. SDL_RenderSetViewport(renderer, NULL);
  2120. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  2121. return 0;
  2122. }
  2123. renderer->logical_w = w;
  2124. renderer->logical_h = h;
  2125. return UpdateLogicalSize(renderer, SDL_TRUE);
  2126. }
  2127. void
  2128. SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
  2129. {
  2130. CHECK_RENDERER_MAGIC(renderer, );
  2131. if (w) {
  2132. *w = renderer->logical_w;
  2133. }
  2134. if (h) {
  2135. *h = renderer->logical_h;
  2136. }
  2137. }
  2138. int
  2139. SDL_RenderSetIntegerScale(SDL_Renderer * renderer, SDL_bool enable)
  2140. {
  2141. CHECK_RENDERER_MAGIC(renderer, -1);
  2142. renderer->integer_scale = enable;
  2143. return UpdateLogicalSize(renderer, SDL_TRUE);
  2144. }
  2145. SDL_bool
  2146. SDLCALL SDL_RenderGetIntegerScale(SDL_Renderer * renderer)
  2147. {
  2148. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE);
  2149. return renderer->integer_scale;
  2150. }
  2151. int
  2152. SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
  2153. {
  2154. int retval;
  2155. CHECK_RENDERER_MAGIC(renderer, -1);
  2156. if (rect) {
  2157. renderer->viewport.x = (double)rect->x * renderer->scale.x;
  2158. renderer->viewport.y = (double)rect->y * renderer->scale.y;
  2159. renderer->viewport.w = (double)rect->w * renderer->scale.x;
  2160. renderer->viewport.h = (double)rect->h * renderer->scale.y;
  2161. } else {
  2162. int w, h;
  2163. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  2164. return -1;
  2165. }
  2166. renderer->viewport.x = (double)0;
  2167. renderer->viewport.y = (double)0;
  2168. renderer->viewport.w = (double)w;
  2169. renderer->viewport.h = (double)h;
  2170. }
  2171. retval = QueueCmdSetViewport(renderer);
  2172. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2173. }
  2174. void
  2175. SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
  2176. {
  2177. CHECK_RENDERER_MAGIC(renderer, );
  2178. if (rect) {
  2179. rect->x = (int)SDL_floor(renderer->viewport.x / renderer->scale.x);
  2180. rect->y = (int)SDL_floor(renderer->viewport.y / renderer->scale.y);
  2181. rect->w = (int)SDL_floor(renderer->viewport.w / renderer->scale.x);
  2182. rect->h = (int)SDL_floor(renderer->viewport.h / renderer->scale.y);
  2183. }
  2184. }
  2185. static void
  2186. RenderGetViewportSize(SDL_Renderer * renderer, SDL_FRect * rect)
  2187. {
  2188. rect->x = 0.0f;
  2189. rect->y = 0.0f;
  2190. rect->w = (float)(renderer->viewport.w / renderer->scale.x);
  2191. rect->h = (float)(renderer->viewport.h / renderer->scale.y);
  2192. }
  2193. int
  2194. SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  2195. {
  2196. int retval;
  2197. CHECK_RENDERER_MAGIC(renderer, -1)
  2198. if (rect) {
  2199. renderer->clipping_enabled = SDL_TRUE;
  2200. renderer->clip_rect.x = (double)rect->x * renderer->scale.x;
  2201. renderer->clip_rect.y = (double)rect->y * renderer->scale.y;
  2202. renderer->clip_rect.w = (double)rect->w * renderer->scale.x;
  2203. renderer->clip_rect.h = (double)rect->h * renderer->scale.y;
  2204. } else {
  2205. renderer->clipping_enabled = SDL_FALSE;
  2206. SDL_zero(renderer->clip_rect);
  2207. }
  2208. retval = QueueCmdSetClipRect(renderer);
  2209. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2210. }
  2211. void
  2212. SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
  2213. {
  2214. CHECK_RENDERER_MAGIC(renderer, )
  2215. if (rect) {
  2216. rect->x = (int)SDL_floor(renderer->clip_rect.x / renderer->scale.x);
  2217. rect->y = (int)SDL_floor(renderer->clip_rect.y / renderer->scale.y);
  2218. rect->w = (int)SDL_floor(renderer->clip_rect.w / renderer->scale.x);
  2219. rect->h = (int)SDL_floor(renderer->clip_rect.h / renderer->scale.y);
  2220. }
  2221. }
  2222. SDL_bool
  2223. SDL_RenderIsClipEnabled(SDL_Renderer * renderer)
  2224. {
  2225. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE)
  2226. return renderer->clipping_enabled;
  2227. }
  2228. int
  2229. SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
  2230. {
  2231. CHECK_RENDERER_MAGIC(renderer, -1);
  2232. renderer->scale.x = scaleX;
  2233. renderer->scale.y = scaleY;
  2234. return 0;
  2235. }
  2236. void
  2237. SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
  2238. {
  2239. CHECK_RENDERER_MAGIC(renderer, );
  2240. if (scaleX) {
  2241. *scaleX = renderer->scale.x;
  2242. }
  2243. if (scaleY) {
  2244. *scaleY = renderer->scale.y;
  2245. }
  2246. }
  2247. void
  2248. SDL_RenderWindowToLogical(SDL_Renderer * renderer, int windowX, int windowY, float *logicalX, float *logicalY)
  2249. {
  2250. float window_physical_x, window_physical_y;
  2251. CHECK_RENDERER_MAGIC(renderer, );
  2252. window_physical_x = ((float) windowX) / renderer->dpi_scale.x;
  2253. window_physical_y = ((float) windowY) / renderer->dpi_scale.y;
  2254. if (logicalX) {
  2255. *logicalX = (float)((window_physical_x - renderer->viewport.x) / renderer->scale.x);
  2256. }
  2257. if (logicalY) {
  2258. *logicalY = (float)((window_physical_y - renderer->viewport.y) / renderer->scale.y);
  2259. }
  2260. }
  2261. void
  2262. SDL_RenderLogicalToWindow(SDL_Renderer * renderer, float logicalX, float logicalY, int *windowX, int *windowY)
  2263. {
  2264. float window_physical_x, window_physical_y;
  2265. CHECK_RENDERER_MAGIC(renderer, );
  2266. window_physical_x = (float)((logicalX * renderer->scale.x) + renderer->viewport.x);
  2267. window_physical_y = (float)((logicalY * renderer->scale.y) + renderer->viewport.y);
  2268. if (windowX) {
  2269. *windowX = (int)(window_physical_x * renderer->dpi_scale.x);
  2270. }
  2271. if (windowY) {
  2272. *windowY = (int)(window_physical_y * renderer->dpi_scale.y);
  2273. }
  2274. }
  2275. int
  2276. SDL_SetRenderDrawColor(SDL_Renderer * renderer,
  2277. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2278. {
  2279. CHECK_RENDERER_MAGIC(renderer, -1);
  2280. renderer->color.r = r;
  2281. renderer->color.g = g;
  2282. renderer->color.b = b;
  2283. renderer->color.a = a;
  2284. return 0;
  2285. }
  2286. int
  2287. SDL_GetRenderDrawColor(SDL_Renderer * renderer,
  2288. Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2289. {
  2290. CHECK_RENDERER_MAGIC(renderer, -1);
  2291. if (r) {
  2292. *r = renderer->color.r;
  2293. }
  2294. if (g) {
  2295. *g = renderer->color.g;
  2296. }
  2297. if (b) {
  2298. *b = renderer->color.b;
  2299. }
  2300. if (a) {
  2301. *a = renderer->color.a;
  2302. }
  2303. return 0;
  2304. }
  2305. int
  2306. SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  2307. {
  2308. CHECK_RENDERER_MAGIC(renderer, -1);
  2309. if (!IsSupportedBlendMode(renderer, blendMode)) {
  2310. return SDL_Unsupported();
  2311. }
  2312. renderer->blendMode = blendMode;
  2313. return 0;
  2314. }
  2315. int
  2316. SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
  2317. {
  2318. CHECK_RENDERER_MAGIC(renderer, -1);
  2319. *blendMode = renderer->blendMode;
  2320. return 0;
  2321. }
  2322. int
  2323. SDL_RenderClear(SDL_Renderer * renderer)
  2324. {
  2325. int retval;
  2326. CHECK_RENDERER_MAGIC(renderer, -1);
  2327. retval = QueueCmdClear(renderer);
  2328. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2329. }
  2330. /* !!! FIXME: delete all the duplicate code for the integer versions in 2.1,
  2331. !!! FIXME: making the floating point versions the only available APIs. */
  2332. int
  2333. SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
  2334. {
  2335. SDL_FPoint fpoint;
  2336. fpoint.x = (float) x;
  2337. fpoint.y = (float) y;
  2338. return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
  2339. }
  2340. int
  2341. SDL_RenderDrawPointF(SDL_Renderer * renderer, float x, float y)
  2342. {
  2343. SDL_FPoint fpoint;
  2344. fpoint.x = x;
  2345. fpoint.y = y;
  2346. return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
  2347. }
  2348. static int
  2349. RenderDrawPointsWithRects(SDL_Renderer * renderer,
  2350. const SDL_Point * points, const int count)
  2351. {
  2352. int retval;
  2353. SDL_bool isstack;
  2354. SDL_FRect *frects;
  2355. int i;
  2356. if (count < 1) {
  2357. return 0;
  2358. }
  2359. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2360. if (!frects) {
  2361. return SDL_OutOfMemory();
  2362. }
  2363. for (i = 0; i < count; ++i) {
  2364. frects[i].x = points[i].x * renderer->scale.x;
  2365. frects[i].y = points[i].y * renderer->scale.y;
  2366. frects[i].w = renderer->scale.x;
  2367. frects[i].h = renderer->scale.y;
  2368. }
  2369. retval = QueueCmdFillRects(renderer, frects, count);
  2370. SDL_small_free(frects, isstack);
  2371. return retval;
  2372. }
  2373. int
  2374. SDL_RenderDrawPoints(SDL_Renderer * renderer,
  2375. const SDL_Point * points, int count)
  2376. {
  2377. SDL_FPoint *fpoints;
  2378. int i;
  2379. int retval;
  2380. SDL_bool isstack;
  2381. CHECK_RENDERER_MAGIC(renderer, -1);
  2382. if (!points) {
  2383. return SDL_InvalidParamError("SDL_RenderDrawPoints(): points");
  2384. }
  2385. if (count < 1) {
  2386. return 0;
  2387. }
  2388. #if DONT_DRAW_WHILE_HIDDEN
  2389. /* Don't draw while we're hidden */
  2390. if (renderer->hidden) {
  2391. return 0;
  2392. }
  2393. #endif
  2394. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2395. retval = RenderDrawPointsWithRects(renderer, points, count);
  2396. } else {
  2397. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2398. if (!fpoints) {
  2399. return SDL_OutOfMemory();
  2400. }
  2401. for (i = 0; i < count; ++i) {
  2402. fpoints[i].x = (float) points[i].x;
  2403. fpoints[i].y = (float) points[i].y;
  2404. }
  2405. retval = QueueCmdDrawPoints(renderer, fpoints, count);
  2406. SDL_small_free(fpoints, isstack);
  2407. }
  2408. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2409. }
  2410. static int
  2411. RenderDrawPointsWithRectsF(SDL_Renderer * renderer,
  2412. const SDL_FPoint * fpoints, const int count)
  2413. {
  2414. int retval;
  2415. SDL_bool isstack;
  2416. SDL_FRect *frects;
  2417. int i;
  2418. if (count < 1) {
  2419. return 0;
  2420. }
  2421. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2422. if (!frects) {
  2423. return SDL_OutOfMemory();
  2424. }
  2425. for (i = 0; i < count; ++i) {
  2426. frects[i].x = fpoints[i].x * renderer->scale.x;
  2427. frects[i].y = fpoints[i].y * renderer->scale.y;
  2428. frects[i].w = renderer->scale.x;
  2429. frects[i].h = renderer->scale.y;
  2430. }
  2431. retval = QueueCmdFillRects(renderer, frects, count);
  2432. SDL_small_free(frects, isstack);
  2433. return retval;
  2434. }
  2435. int
  2436. SDL_RenderDrawPointsF(SDL_Renderer * renderer,
  2437. const SDL_FPoint * points, int count)
  2438. {
  2439. int retval;
  2440. CHECK_RENDERER_MAGIC(renderer, -1);
  2441. if (!points) {
  2442. return SDL_InvalidParamError("SDL_RenderDrawPointsF(): points");
  2443. }
  2444. if (count < 1) {
  2445. return 0;
  2446. }
  2447. #if DONT_DRAW_WHILE_HIDDEN
  2448. /* Don't draw while we're hidden */
  2449. if (renderer->hidden) {
  2450. return 0;
  2451. }
  2452. #endif
  2453. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2454. retval = RenderDrawPointsWithRectsF(renderer, points, count);
  2455. } else {
  2456. retval = QueueCmdDrawPoints(renderer, points, count);
  2457. }
  2458. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2459. }
  2460. int
  2461. SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
  2462. {
  2463. SDL_FPoint points[2];
  2464. points[0].x = (float) x1;
  2465. points[0].y = (float) y1;
  2466. points[1].x = (float) x2;
  2467. points[1].y = (float) y2;
  2468. return SDL_RenderDrawLinesF(renderer, points, 2);
  2469. }
  2470. int
  2471. SDL_RenderDrawLineF(SDL_Renderer * renderer, float x1, float y1, float x2, float y2)
  2472. {
  2473. SDL_FPoint points[2];
  2474. points[0].x = x1;
  2475. points[0].y = y1;
  2476. points[1].x = x2;
  2477. points[1].y = y2;
  2478. return SDL_RenderDrawLinesF(renderer, points, 2);
  2479. }
  2480. static int RenderDrawLineBresenham(SDL_Renderer *renderer, int x1, int y1, int x2, int y2, SDL_bool draw_last)
  2481. {
  2482. int i, deltax, deltay, numpixels;
  2483. int d, dinc1, dinc2;
  2484. int x, xinc1, xinc2;
  2485. int y, yinc1, yinc2;
  2486. int retval;
  2487. SDL_bool isstack;
  2488. SDL_FPoint *points;
  2489. deltax = SDL_abs(x2 - x1);
  2490. deltay = SDL_abs(y2 - y1);
  2491. if (deltax >= deltay) {
  2492. numpixels = deltax + 1;
  2493. d = (2 * deltay) - deltax;
  2494. dinc1 = deltay * 2;
  2495. dinc2 = (deltay - deltax) * 2;
  2496. xinc1 = 1;
  2497. xinc2 = 1;
  2498. yinc1 = 0;
  2499. yinc2 = 1;
  2500. } else {
  2501. numpixels = deltay + 1;
  2502. d = (2 * deltax) - deltay;
  2503. dinc1 = deltax * 2;
  2504. dinc2 = (deltax - deltay) * 2;
  2505. xinc1 = 0;
  2506. xinc2 = 1;
  2507. yinc1 = 1;
  2508. yinc2 = 1;
  2509. }
  2510. if (x1 > x2) {
  2511. xinc1 = -xinc1;
  2512. xinc2 = -xinc2;
  2513. }
  2514. if (y1 > y2) {
  2515. yinc1 = -yinc1;
  2516. yinc2 = -yinc2;
  2517. }
  2518. x = x1;
  2519. y = y1;
  2520. if (!draw_last) {
  2521. --numpixels;
  2522. }
  2523. points = SDL_small_alloc(SDL_FPoint, numpixels, &isstack);
  2524. if (!points) {
  2525. return SDL_OutOfMemory();
  2526. }
  2527. for (i = 0; i < numpixels; ++i) {
  2528. points[i].x = (float)x;
  2529. points[i].y = (float)y;
  2530. if (d < 0) {
  2531. d += dinc1;
  2532. x += xinc1;
  2533. y += yinc1;
  2534. } else {
  2535. d += dinc2;
  2536. x += xinc2;
  2537. y += yinc2;
  2538. }
  2539. }
  2540. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2541. retval = RenderDrawPointsWithRectsF(renderer, points, numpixels);
  2542. } else {
  2543. retval = QueueCmdDrawPoints(renderer, points, numpixels);
  2544. }
  2545. SDL_small_free(points, isstack);
  2546. return retval;
  2547. }
  2548. static int
  2549. RenderDrawLinesWithRectsF(SDL_Renderer * renderer,
  2550. const SDL_FPoint * points, const int count)
  2551. {
  2552. const float scale_x = renderer->scale.x;
  2553. const float scale_y = renderer->scale.y;
  2554. SDL_FRect *frect;
  2555. SDL_FRect *frects;
  2556. int i, nrects = 0;
  2557. int retval = 0;
  2558. SDL_bool isstack;
  2559. SDL_bool drew_line = SDL_FALSE;
  2560. SDL_bool draw_last = SDL_FALSE;
  2561. frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
  2562. if (!frects) {
  2563. return SDL_OutOfMemory();
  2564. }
  2565. for (i = 0; i < count-1; ++i) {
  2566. SDL_bool same_x = (points[i].x == points[i+1].x);
  2567. SDL_bool same_y = (points[i].y == points[i+1].y);
  2568. if (i == (count - 2)) {
  2569. if (!drew_line || points[i+1].x != points[0].x || points[i+1].y != points[0].y) {
  2570. draw_last = SDL_TRUE;
  2571. }
  2572. } else {
  2573. if (same_x && same_y) {
  2574. continue;
  2575. }
  2576. }
  2577. if (same_x) {
  2578. const float minY = SDL_min(points[i].y, points[i+1].y);
  2579. const float maxY = SDL_max(points[i].y, points[i+1].y);
  2580. frect = &frects[nrects++];
  2581. frect->x = points[i].x * scale_x;
  2582. frect->y = minY * scale_y;
  2583. frect->w = scale_x;
  2584. frect->h = (maxY - minY + draw_last) * scale_y;
  2585. if (!draw_last && points[i+1].y < points[i].y) {
  2586. frect->y += scale_y;
  2587. }
  2588. } else if (same_y) {
  2589. const float minX = SDL_min(points[i].x, points[i+1].x);
  2590. const float maxX = SDL_max(points[i].x, points[i+1].x);
  2591. frect = &frects[nrects++];
  2592. frect->x = minX * scale_x;
  2593. frect->y = points[i].y * scale_y;
  2594. frect->w = (maxX - minX + draw_last) * scale_x;
  2595. frect->h = scale_y;
  2596. if (!draw_last && points[i+1].x < points[i].x) {
  2597. frect->x += scale_x;
  2598. }
  2599. } else {
  2600. retval += RenderDrawLineBresenham(renderer, (int)points[i].x, (int)points[i].y,
  2601. (int)points[i+1].x, (int)points[i+1].y, draw_last);
  2602. }
  2603. drew_line = SDL_TRUE;
  2604. }
  2605. if (nrects) {
  2606. retval += QueueCmdFillRects(renderer, frects, nrects);
  2607. }
  2608. SDL_small_free(frects, isstack);
  2609. if (retval < 0) {
  2610. retval = -1;
  2611. }
  2612. return retval;
  2613. }
  2614. int
  2615. SDL_RenderDrawLines(SDL_Renderer * renderer,
  2616. const SDL_Point * points, int count)
  2617. {
  2618. SDL_FPoint *fpoints;
  2619. int i;
  2620. int retval;
  2621. SDL_bool isstack;
  2622. CHECK_RENDERER_MAGIC(renderer, -1);
  2623. if (!points) {
  2624. return SDL_InvalidParamError("SDL_RenderDrawLines(): points");
  2625. }
  2626. if (count < 2) {
  2627. return 0;
  2628. }
  2629. #if DONT_DRAW_WHILE_HIDDEN
  2630. /* Don't draw while we're hidden */
  2631. if (renderer->hidden) {
  2632. return 0;
  2633. }
  2634. #endif
  2635. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2636. if (!fpoints) {
  2637. return SDL_OutOfMemory();
  2638. }
  2639. for (i = 0; i < count; ++i) {
  2640. fpoints[i].x = (float) points[i].x;
  2641. fpoints[i].y = (float) points[i].y;
  2642. }
  2643. retval = SDL_RenderDrawLinesF(renderer, fpoints, count);
  2644. SDL_small_free(fpoints, isstack);
  2645. return retval;
  2646. }
  2647. int
  2648. SDL_RenderDrawLinesF(SDL_Renderer * renderer,
  2649. const SDL_FPoint * points, int count)
  2650. {
  2651. int retval = 0;
  2652. CHECK_RENDERER_MAGIC(renderer, -1);
  2653. if (!points) {
  2654. return SDL_InvalidParamError("SDL_RenderDrawLinesF(): points");
  2655. }
  2656. if (count < 2) {
  2657. return 0;
  2658. }
  2659. #if DONT_DRAW_WHILE_HIDDEN
  2660. /* Don't draw while we're hidden */
  2661. if (renderer->hidden) {
  2662. return 0;
  2663. }
  2664. #endif
  2665. if (renderer->line_method == SDL_RENDERLINEMETHOD_POINTS) {
  2666. retval = RenderDrawLinesWithRectsF(renderer, points, count);
  2667. } else if (renderer->line_method == SDL_RENDERLINEMETHOD_GEOMETRY) {
  2668. SDL_bool isstack1;
  2669. SDL_bool isstack2;
  2670. const float scale_x = renderer->scale.x;
  2671. const float scale_y = renderer->scale.y;
  2672. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  2673. int *indices = SDL_small_alloc(int,
  2674. (4) * 3 * (count - 1)
  2675. + (2) * 3 * (count)
  2676. , &isstack2);
  2677. if (xy && indices) {
  2678. int i;
  2679. float *ptr_xy = xy;
  2680. int *ptr_indices = indices;
  2681. const int xy_stride = 2 * sizeof (float);
  2682. int num_vertices = 4 * count;
  2683. int num_indices = 0;
  2684. const int size_indices = 4;
  2685. int cur_indice = -4;
  2686. const int is_looping = (points[0].x == points[count - 1].x && points[0].y == points[count - 1].y);
  2687. SDL_FPoint p; /* previous point */
  2688. p.x = p.y = 0.0f;
  2689. /* p q
  2690. 0----1------ 4----5
  2691. | \ |``\ | \ |
  2692. | \ | ` `\| \ |
  2693. 3----2-------7----6
  2694. */
  2695. for (i = 0; i < count; ++i) {
  2696. SDL_FPoint q = points[i]; /* current point */
  2697. q.x *= scale_x;
  2698. q.y *= scale_y;
  2699. *ptr_xy++ = q.x;
  2700. *ptr_xy++ = q.y;
  2701. *ptr_xy++ = q.x + scale_x;
  2702. *ptr_xy++ = q.y;
  2703. *ptr_xy++ = q.x + scale_x;
  2704. *ptr_xy++ = q.y + scale_y;
  2705. *ptr_xy++ = q.x;
  2706. *ptr_xy++ = q.y + scale_y;
  2707. #define ADD_TRIANGLE(i1, i2, i3) \
  2708. *ptr_indices++ = cur_indice + i1; \
  2709. *ptr_indices++ = cur_indice + i2; \
  2710. *ptr_indices++ = cur_indice + i3; \
  2711. num_indices += 3; \
  2712. /* closed polyline, don´t draw twice the point */
  2713. if (i || is_looping == 0) {
  2714. ADD_TRIANGLE(4, 5, 6)
  2715. ADD_TRIANGLE(4, 6, 7)
  2716. }
  2717. /* first point only, no segment */
  2718. if (i == 0) {
  2719. p = q;
  2720. cur_indice += 4;
  2721. continue;
  2722. }
  2723. /* draw segment */
  2724. if (p.y == q.y) {
  2725. if (p.x < q.x) {
  2726. ADD_TRIANGLE(1, 4, 7)
  2727. ADD_TRIANGLE(1, 7, 2)
  2728. } else {
  2729. ADD_TRIANGLE(5, 0, 3)
  2730. ADD_TRIANGLE(5, 3, 6)
  2731. }
  2732. } else if (p.x == q.x) {
  2733. if (p.y < q.y) {
  2734. ADD_TRIANGLE(2, 5, 4)
  2735. ADD_TRIANGLE(2, 4, 3)
  2736. } else {
  2737. ADD_TRIANGLE(6, 1, 0)
  2738. ADD_TRIANGLE(6, 0, 7)
  2739. }
  2740. } else {
  2741. if (p.y < q.y) {
  2742. if (p.x < q.x) {
  2743. ADD_TRIANGLE(1, 5, 4)
  2744. ADD_TRIANGLE(1, 4, 2)
  2745. ADD_TRIANGLE(2, 4, 7)
  2746. ADD_TRIANGLE(2, 7, 3)
  2747. } else {
  2748. ADD_TRIANGLE(4, 0, 5)
  2749. ADD_TRIANGLE(5, 0, 3)
  2750. ADD_TRIANGLE(5, 3, 6)
  2751. ADD_TRIANGLE(6, 3, 2)
  2752. }
  2753. } else {
  2754. if (p.x < q.x) {
  2755. ADD_TRIANGLE(0, 4, 7)
  2756. ADD_TRIANGLE(0, 7, 1)
  2757. ADD_TRIANGLE(1, 7, 6)
  2758. ADD_TRIANGLE(1, 6, 2)
  2759. } else {
  2760. ADD_TRIANGLE(6, 5, 1)
  2761. ADD_TRIANGLE(6, 1, 0)
  2762. ADD_TRIANGLE(7, 6, 0)
  2763. ADD_TRIANGLE(7, 0, 3)
  2764. }
  2765. }
  2766. }
  2767. p = q;
  2768. cur_indice += 4;
  2769. }
  2770. retval = QueueCmdGeometry(renderer, NULL,
  2771. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  2772. num_vertices, indices, num_indices, size_indices,
  2773. 1.0f, 1.0f);
  2774. }
  2775. SDL_small_free(xy, isstack1);
  2776. SDL_small_free(indices, isstack2);
  2777. } else if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2778. retval = RenderDrawLinesWithRectsF(renderer, points, count);
  2779. } else {
  2780. retval = QueueCmdDrawLines(renderer, points, count);
  2781. }
  2782. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2783. }
  2784. int
  2785. SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  2786. {
  2787. SDL_FRect frect;
  2788. SDL_FRect *prect = NULL;
  2789. if (rect) {
  2790. frect.x = (float) rect->x;
  2791. frect.y = (float) rect->y;
  2792. frect.w = (float) rect->w;
  2793. frect.h = (float) rect->h;
  2794. prect = &frect;
  2795. }
  2796. return SDL_RenderDrawRectF(renderer, prect);
  2797. }
  2798. int
  2799. SDL_RenderDrawRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
  2800. {
  2801. SDL_FRect frect;
  2802. SDL_FPoint points[5];
  2803. CHECK_RENDERER_MAGIC(renderer, -1);
  2804. /* If 'rect' == NULL, then outline the whole surface */
  2805. if (!rect) {
  2806. RenderGetViewportSize(renderer, &frect);
  2807. rect = &frect;
  2808. }
  2809. points[0].x = rect->x;
  2810. points[0].y = rect->y;
  2811. points[1].x = rect->x+rect->w-1;
  2812. points[1].y = rect->y;
  2813. points[2].x = rect->x+rect->w-1;
  2814. points[2].y = rect->y+rect->h-1;
  2815. points[3].x = rect->x;
  2816. points[3].y = rect->y+rect->h-1;
  2817. points[4].x = rect->x;
  2818. points[4].y = rect->y;
  2819. return SDL_RenderDrawLinesF(renderer, points, 5);
  2820. }
  2821. int
  2822. SDL_RenderDrawRects(SDL_Renderer * renderer,
  2823. const SDL_Rect * rects, int count)
  2824. {
  2825. int i;
  2826. CHECK_RENDERER_MAGIC(renderer, -1);
  2827. if (!rects) {
  2828. return SDL_InvalidParamError("SDL_RenderDrawRects(): rects");
  2829. }
  2830. if (count < 1) {
  2831. return 0;
  2832. }
  2833. #if DONT_DRAW_WHILE_HIDDEN
  2834. /* Don't draw while we're hidden */
  2835. if (renderer->hidden) {
  2836. return 0;
  2837. }
  2838. #endif
  2839. for (i = 0; i < count; ++i) {
  2840. if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
  2841. return -1;
  2842. }
  2843. }
  2844. return 0;
  2845. }
  2846. int
  2847. SDL_RenderDrawRectsF(SDL_Renderer * renderer,
  2848. const SDL_FRect * rects, int count)
  2849. {
  2850. int i;
  2851. CHECK_RENDERER_MAGIC(renderer, -1);
  2852. if (!rects) {
  2853. return SDL_InvalidParamError("SDL_RenderDrawRectsF(): rects");
  2854. }
  2855. if (count < 1) {
  2856. return 0;
  2857. }
  2858. #if DONT_DRAW_WHILE_HIDDEN
  2859. /* Don't draw while we're hidden */
  2860. if (renderer->hidden) {
  2861. return 0;
  2862. }
  2863. #endif
  2864. for (i = 0; i < count; ++i) {
  2865. if (SDL_RenderDrawRectF(renderer, &rects[i]) < 0) {
  2866. return -1;
  2867. }
  2868. }
  2869. return 0;
  2870. }
  2871. int
  2872. SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  2873. {
  2874. SDL_FRect frect;
  2875. CHECK_RENDERER_MAGIC(renderer, -1);
  2876. /* If 'rect' == NULL, then outline the whole surface */
  2877. if (rect) {
  2878. frect.x = (float) rect->x;
  2879. frect.y = (float) rect->y;
  2880. frect.w = (float) rect->w;
  2881. frect.h = (float) rect->h;
  2882. } else {
  2883. RenderGetViewportSize(renderer, &frect);
  2884. }
  2885. return SDL_RenderFillRectsF(renderer, &frect, 1);
  2886. }
  2887. int
  2888. SDL_RenderFillRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
  2889. {
  2890. SDL_FRect frect;
  2891. CHECK_RENDERER_MAGIC(renderer, -1);
  2892. /* If 'rect' == NULL, then outline the whole surface */
  2893. if (!rect) {
  2894. RenderGetViewportSize(renderer, &frect);
  2895. rect = &frect;
  2896. }
  2897. return SDL_RenderFillRectsF(renderer, rect, 1);
  2898. }
  2899. int
  2900. SDL_RenderFillRects(SDL_Renderer * renderer,
  2901. const SDL_Rect * rects, int count)
  2902. {
  2903. SDL_FRect *frects;
  2904. int i;
  2905. int retval;
  2906. SDL_bool isstack;
  2907. CHECK_RENDERER_MAGIC(renderer, -1);
  2908. if (!rects) {
  2909. return SDL_InvalidParamError("SDL_RenderFillRects(): rects");
  2910. }
  2911. if (count < 1) {
  2912. return 0;
  2913. }
  2914. #if DONT_DRAW_WHILE_HIDDEN
  2915. /* Don't draw while we're hidden */
  2916. if (renderer->hidden) {
  2917. return 0;
  2918. }
  2919. #endif
  2920. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2921. if (!frects) {
  2922. return SDL_OutOfMemory();
  2923. }
  2924. for (i = 0; i < count; ++i) {
  2925. frects[i].x = rects[i].x * renderer->scale.x;
  2926. frects[i].y = rects[i].y * renderer->scale.y;
  2927. frects[i].w = rects[i].w * renderer->scale.x;
  2928. frects[i].h = rects[i].h * renderer->scale.y;
  2929. }
  2930. retval = QueueCmdFillRects(renderer, frects, count);
  2931. SDL_small_free(frects, isstack);
  2932. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2933. }
  2934. int
  2935. SDL_RenderFillRectsF(SDL_Renderer * renderer,
  2936. const SDL_FRect * rects, int count)
  2937. {
  2938. SDL_FRect *frects;
  2939. int i;
  2940. int retval;
  2941. SDL_bool isstack;
  2942. CHECK_RENDERER_MAGIC(renderer, -1);
  2943. if (!rects) {
  2944. return SDL_InvalidParamError("SDL_RenderFillRectsF(): rects");
  2945. }
  2946. if (count < 1) {
  2947. return 0;
  2948. }
  2949. #if DONT_DRAW_WHILE_HIDDEN
  2950. /* Don't draw while we're hidden */
  2951. if (renderer->hidden) {
  2952. return 0;
  2953. }
  2954. #endif
  2955. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2956. if (!frects) {
  2957. return SDL_OutOfMemory();
  2958. }
  2959. for (i = 0; i < count; ++i) {
  2960. frects[i].x = rects[i].x * renderer->scale.x;
  2961. frects[i].y = rects[i].y * renderer->scale.y;
  2962. frects[i].w = rects[i].w * renderer->scale.x;
  2963. frects[i].h = rects[i].h * renderer->scale.y;
  2964. }
  2965. retval = QueueCmdFillRects(renderer, frects, count);
  2966. SDL_small_free(frects, isstack);
  2967. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2968. }
  2969. int
  2970. SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  2971. const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  2972. {
  2973. SDL_FRect dstfrect;
  2974. SDL_FRect *pdstfrect = NULL;
  2975. if (dstrect) {
  2976. dstfrect.x = (float) dstrect->x;
  2977. dstfrect.y = (float) dstrect->y;
  2978. dstfrect.w = (float) dstrect->w;
  2979. dstfrect.h = (float) dstrect->h;
  2980. pdstfrect = &dstfrect;
  2981. }
  2982. return SDL_RenderCopyF(renderer, texture, srcrect, pdstfrect);
  2983. }
  2984. int
  2985. SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
  2986. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  2987. {
  2988. SDL_Rect real_srcrect;
  2989. SDL_FRect real_dstrect;
  2990. int retval;
  2991. int use_rendergeometry;
  2992. CHECK_RENDERER_MAGIC(renderer, -1);
  2993. CHECK_TEXTURE_MAGIC(texture, -1);
  2994. if (renderer != texture->renderer) {
  2995. return SDL_SetError("Texture was not created with this renderer");
  2996. }
  2997. #if DONT_DRAW_WHILE_HIDDEN
  2998. /* Don't draw while we're hidden */
  2999. if (renderer->hidden) {
  3000. return 0;
  3001. }
  3002. #endif
  3003. use_rendergeometry = (renderer->QueueCopy == NULL);
  3004. real_srcrect.x = 0;
  3005. real_srcrect.y = 0;
  3006. real_srcrect.w = texture->w;
  3007. real_srcrect.h = texture->h;
  3008. if (srcrect) {
  3009. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  3010. return 0;
  3011. }
  3012. }
  3013. RenderGetViewportSize(renderer, &real_dstrect);
  3014. if (dstrect) {
  3015. if (!SDL_HasIntersectionF(dstrect, &real_dstrect)) {
  3016. return 0;
  3017. }
  3018. real_dstrect = *dstrect;
  3019. }
  3020. if (texture->native) {
  3021. texture = texture->native;
  3022. }
  3023. texture->last_command_generation = renderer->render_command_generation;
  3024. if (use_rendergeometry) {
  3025. float xy[8];
  3026. const int xy_stride = 2 * sizeof (float);
  3027. float uv[8];
  3028. const int uv_stride = 2 * sizeof (float);
  3029. const int num_vertices = 4;
  3030. const int indices[6] = {0, 1, 2, 0, 2, 3};
  3031. const int num_indices = 6;
  3032. const int size_indices = 4;
  3033. float minu, minv, maxu, maxv;
  3034. float minx, miny, maxx, maxy;
  3035. minu = (float) (real_srcrect.x) / (float) texture->w;
  3036. minv = (float) (real_srcrect.y) / (float) texture->h;
  3037. maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
  3038. maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
  3039. minx = real_dstrect.x;
  3040. miny = real_dstrect.y;
  3041. maxx = real_dstrect.x + real_dstrect.w;
  3042. maxy = real_dstrect.y + real_dstrect.h;
  3043. uv[0] = minu;
  3044. uv[1] = minv;
  3045. uv[2] = maxu;
  3046. uv[3] = minv;
  3047. uv[4] = maxu;
  3048. uv[5] = maxv;
  3049. uv[6] = minu;
  3050. uv[7] = maxv;
  3051. xy[0] = minx;
  3052. xy[1] = miny;
  3053. xy[2] = maxx;
  3054. xy[3] = miny;
  3055. xy[4] = maxx;
  3056. xy[5] = maxy;
  3057. xy[6] = minx;
  3058. xy[7] = maxy;
  3059. retval = QueueCmdGeometry(renderer, texture,
  3060. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3061. num_vertices,
  3062. indices, num_indices, size_indices,
  3063. renderer->scale.x, renderer->scale.y);
  3064. } else {
  3065. real_dstrect.x *= renderer->scale.x;
  3066. real_dstrect.y *= renderer->scale.y;
  3067. real_dstrect.w *= renderer->scale.x;
  3068. real_dstrect.h *= renderer->scale.y;
  3069. retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
  3070. }
  3071. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  3072. }
  3073. int
  3074. SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  3075. const SDL_Rect * srcrect, const SDL_Rect * dstrect,
  3076. const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
  3077. {
  3078. SDL_FRect dstfrect;
  3079. SDL_FRect *pdstfrect = NULL;
  3080. SDL_FPoint fcenter;
  3081. SDL_FPoint *pfcenter = NULL;
  3082. if (dstrect) {
  3083. dstfrect.x = (float) dstrect->x;
  3084. dstfrect.y = (float) dstrect->y;
  3085. dstfrect.w = (float) dstrect->w;
  3086. dstfrect.h = (float) dstrect->h;
  3087. pdstfrect = &dstfrect;
  3088. }
  3089. if (center) {
  3090. fcenter.x = (float) center->x;
  3091. fcenter.y = (float) center->y;
  3092. pfcenter = &fcenter;
  3093. }
  3094. return SDL_RenderCopyExF(renderer, texture, srcrect, pdstfrect, angle, pfcenter, flip);
  3095. }
  3096. int
  3097. SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
  3098. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  3099. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  3100. {
  3101. SDL_Rect real_srcrect;
  3102. SDL_FRect real_dstrect;
  3103. SDL_FPoint real_center;
  3104. int retval;
  3105. int use_rendergeometry;
  3106. if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */
  3107. return SDL_RenderCopyF(renderer, texture, srcrect, dstrect);
  3108. }
  3109. CHECK_RENDERER_MAGIC(renderer, -1);
  3110. CHECK_TEXTURE_MAGIC(texture, -1);
  3111. if (renderer != texture->renderer) {
  3112. return SDL_SetError("Texture was not created with this renderer");
  3113. }
  3114. if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
  3115. return SDL_SetError("Renderer does not support RenderCopyEx");
  3116. }
  3117. #if DONT_DRAW_WHILE_HIDDEN
  3118. /* Don't draw while we're hidden */
  3119. if (renderer->hidden) {
  3120. return 0;
  3121. }
  3122. #endif
  3123. use_rendergeometry = (renderer->QueueCopyEx == NULL);
  3124. real_srcrect.x = 0;
  3125. real_srcrect.y = 0;
  3126. real_srcrect.w = texture->w;
  3127. real_srcrect.h = texture->h;
  3128. if (srcrect) {
  3129. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  3130. return 0;
  3131. }
  3132. }
  3133. /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
  3134. if (dstrect) {
  3135. real_dstrect = *dstrect;
  3136. } else {
  3137. RenderGetViewportSize(renderer, &real_dstrect);
  3138. }
  3139. if (texture->native) {
  3140. texture = texture->native;
  3141. }
  3142. if (center) {
  3143. real_center = *center;
  3144. } else {
  3145. real_center.x = real_dstrect.w / 2.0f;
  3146. real_center.y = real_dstrect.h / 2.0f;
  3147. }
  3148. texture->last_command_generation = renderer->render_command_generation;
  3149. if (use_rendergeometry) {
  3150. float xy[8];
  3151. const int xy_stride = 2 * sizeof (float);
  3152. float uv[8];
  3153. const int uv_stride = 2 * sizeof (float);
  3154. const int num_vertices = 4;
  3155. const int indices[6] = {0, 1, 2, 0, 2, 3};
  3156. const int num_indices = 6;
  3157. const int size_indices = 4;
  3158. float minu, minv, maxu, maxv;
  3159. float minx, miny, maxx, maxy;
  3160. float centerx, centery;
  3161. float s_minx, s_miny, s_maxx, s_maxy;
  3162. float c_minx, c_miny, c_maxx, c_maxy;
  3163. const float radian_angle = (float)((M_PI * angle) / 180.0);
  3164. const float s = SDL_sinf(radian_angle);
  3165. const float c = SDL_cosf(radian_angle);
  3166. minu = (float) (real_srcrect.x) / (float) texture->w;
  3167. minv = (float) (real_srcrect.y) / (float) texture->h;
  3168. maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
  3169. maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
  3170. centerx = real_center.x + real_dstrect.x;
  3171. centery = real_center.y + real_dstrect.y;
  3172. if (flip & SDL_FLIP_HORIZONTAL) {
  3173. minx = real_dstrect.x + real_dstrect.w;
  3174. maxx = real_dstrect.x;
  3175. } else {
  3176. minx = real_dstrect.x;
  3177. maxx = real_dstrect.x + real_dstrect.w;
  3178. }
  3179. if (flip & SDL_FLIP_VERTICAL) {
  3180. miny = real_dstrect.y + real_dstrect.h;
  3181. maxy = real_dstrect.y;
  3182. } else {
  3183. miny = real_dstrect.y;
  3184. maxy = real_dstrect.y + real_dstrect.h;
  3185. }
  3186. uv[0] = minu;
  3187. uv[1] = minv;
  3188. uv[2] = maxu;
  3189. uv[3] = minv;
  3190. uv[4] = maxu;
  3191. uv[5] = maxv;
  3192. uv[6] = minu;
  3193. uv[7] = maxv;
  3194. /* apply rotation with 2x2 matrix ( c -s )
  3195. * ( s c ) */
  3196. s_minx = s * (minx - centerx);
  3197. s_miny = s * (miny - centery);
  3198. s_maxx = s * (maxx - centerx);
  3199. s_maxy = s * (maxy - centery);
  3200. c_minx = c * (minx - centerx);
  3201. c_miny = c * (miny - centery);
  3202. c_maxx = c * (maxx - centerx);
  3203. c_maxy = c * (maxy - centery);
  3204. /* (minx, miny) */
  3205. xy[0] = (c_minx - s_miny) + centerx;
  3206. xy[1] = (s_minx + c_miny) + centery;
  3207. /* (maxx, miny) */
  3208. xy[2] = (c_maxx - s_miny) + centerx;
  3209. xy[3] = (s_maxx + c_miny) + centery;
  3210. /* (maxx, maxy) */
  3211. xy[4] = (c_maxx - s_maxy) + centerx;
  3212. xy[5] = (s_maxx + c_maxy) + centery;
  3213. /* (minx, maxy) */
  3214. xy[6] = (c_minx - s_maxy) + centerx;
  3215. xy[7] = (s_minx + c_maxy) + centery;
  3216. retval = QueueCmdGeometry(renderer, texture,
  3217. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3218. num_vertices,
  3219. indices, num_indices, size_indices,
  3220. renderer->scale.x, renderer->scale.y);
  3221. } else {
  3222. retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip, renderer->scale.x, renderer->scale.y);
  3223. }
  3224. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  3225. }
  3226. int
  3227. SDL_RenderGeometry(SDL_Renderer *renderer,
  3228. SDL_Texture *texture,
  3229. const SDL_Vertex *vertices, int num_vertices,
  3230. const int *indices, int num_indices)
  3231. {
  3232. if (vertices) {
  3233. const float *xy = &vertices->position.x;
  3234. int xy_stride = sizeof (SDL_Vertex);
  3235. const SDL_Color *color = &vertices->color;
  3236. int color_stride = sizeof (SDL_Vertex);
  3237. const float *uv = &vertices->tex_coord.x;
  3238. int uv_stride = sizeof (SDL_Vertex);
  3239. int size_indices = 4;
  3240. return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indices);
  3241. } else {
  3242. return SDL_InvalidParamError("vertices");
  3243. }
  3244. }
  3245. static int
  3246. remap_one_indice(
  3247. int prev,
  3248. int k,
  3249. SDL_Texture *texture,
  3250. const float *xy, int xy_stride,
  3251. const SDL_Color *color, int color_stride,
  3252. const float *uv, int uv_stride)
  3253. {
  3254. const float *xy0_, *xy1_, *uv0_, *uv1_;
  3255. int col0_, col1_;
  3256. xy0_ = (const float *)((const char*)xy + prev * xy_stride);
  3257. xy1_ = (const float *)((const char*)xy + k * xy_stride);
  3258. if (xy0_[0] != xy1_[0]) {
  3259. return k;
  3260. }
  3261. if (xy0_[1] != xy1_[1]) {
  3262. return k;
  3263. }
  3264. if (texture) {
  3265. uv0_ = (const float *)((const char*)uv + prev * uv_stride);
  3266. uv1_ = (const float *)((const char*)uv + k * uv_stride);
  3267. if (uv0_[0] != uv1_[0]) {
  3268. return k;
  3269. }
  3270. if (uv0_[1] != uv1_[1]) {
  3271. return k;
  3272. }
  3273. }
  3274. col0_ = *(const int *)((const char*)color + prev * color_stride);
  3275. col1_ = *(const int *)((const char*)color + k * color_stride);
  3276. if (col0_ != col1_) {
  3277. return k;
  3278. }
  3279. return prev;
  3280. }
  3281. static int
  3282. remap_indices(
  3283. int prev[3],
  3284. int k,
  3285. SDL_Texture *texture,
  3286. const float *xy, int xy_stride,
  3287. const SDL_Color *color, int color_stride,
  3288. const float *uv, int uv_stride)
  3289. {
  3290. int i;
  3291. if (prev[0] == -1) {
  3292. return k;
  3293. }
  3294. for (i = 0; i < 3; i++) {
  3295. int new_k = remap_one_indice(prev[i], k, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3296. if (new_k != k) {
  3297. return new_k;
  3298. }
  3299. }
  3300. return k;
  3301. }
  3302. #define DEBUG_SW_RENDER_GEOMETRY 0
  3303. /* For the software renderer, try to reinterpret triangles as SDL_Rect */
  3304. static int SDLCALL
  3305. SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
  3306. SDL_Texture *texture,
  3307. const float *xy, int xy_stride,
  3308. const SDL_Color *color, int color_stride,
  3309. const float *uv, int uv_stride,
  3310. int num_vertices,
  3311. const void *indices, int num_indices, int size_indices)
  3312. {
  3313. int i;
  3314. int retval = 0;
  3315. int count = indices ? num_indices : num_vertices;
  3316. int prev[3]; /* Previous triangle vertex indices */
  3317. int texw = 0, texh = 0;
  3318. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  3319. Uint8 r = 0, g = 0, b = 0, a = 0;
  3320. /* Save */
  3321. SDL_GetRenderDrawBlendMode(renderer, &blendMode);
  3322. SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  3323. if (texture) {
  3324. SDL_QueryTexture(texture, NULL, NULL, &texw, &texh);
  3325. }
  3326. prev[0] = -1; prev[1] = -1; prev[2] = -1;
  3327. size_indices = indices ? size_indices : 0;
  3328. for (i = 0; i < count; i += 3) {
  3329. int k0, k1, k2; /* Current triangle indices */
  3330. int is_quad = 1;
  3331. #if DEBUG_SW_RENDER_GEOMETRY
  3332. int is_uniform = 1;
  3333. int is_rectangle = 1;
  3334. #endif
  3335. int A = -1; /* Top left vertex */
  3336. int B = -1; /* Bottom right vertex */
  3337. int C = -1; /* Third vertex of current triangle */
  3338. int C2 = -1; /* Last, vertex of previous triangle */
  3339. if (size_indices == 4) {
  3340. k0 = ((const Uint32 *)indices)[i];
  3341. k1 = ((const Uint32 *)indices)[i + 1];
  3342. k2 = ((const Uint32 *)indices)[i + 2];
  3343. } else if (size_indices == 2) {
  3344. k0 = ((const Uint16 *)indices)[i];
  3345. k1 = ((const Uint16 *)indices)[i + 1];
  3346. k2 = ((const Uint16 *)indices)[i + 2];
  3347. } else if (size_indices == 1) {
  3348. k0 = ((const Uint8 *)indices)[i];
  3349. k1 = ((const Uint8 *)indices)[i + 1];
  3350. k2 = ((const Uint8 *)indices)[i + 2];
  3351. } else {
  3352. /* Vertices were not provided by indices. Maybe some are duplicated.
  3353. * We try to indentificate the duplicates by comparing with the previous three vertices */
  3354. k0 = remap_indices(prev, i, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3355. k1 = remap_indices(prev, i + 1, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3356. k2 = remap_indices(prev, i + 2, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3357. }
  3358. if (prev[0] == -1) {
  3359. prev[0] = k0;
  3360. prev[1] = k1;
  3361. prev[2] = k2;
  3362. continue;
  3363. }
  3364. /* Two triangles forming a quadialateral,
  3365. * prev and current triangles must have exactly 2 common vertices */
  3366. {
  3367. int cnt = 0, j = 3;
  3368. while (j--) {
  3369. int p = prev[j];
  3370. if (p == k0 || p == k1 || p == k2) {
  3371. cnt++;
  3372. }
  3373. }
  3374. is_quad = (cnt == 2);
  3375. }
  3376. /* Identify vertices */
  3377. if (is_quad) {
  3378. const float *xy0_, *xy1_, *xy2_;
  3379. float x0, x1, x2;
  3380. float y0, y1, y2;
  3381. xy0_ = (const float *)((const char*)xy + k0 * xy_stride);
  3382. xy1_ = (const float *)((const char*)xy + k1 * xy_stride);
  3383. xy2_ = (const float *)((const char*)xy + k2 * xy_stride);
  3384. x0 = xy0_[0]; y0 = xy0_[1];
  3385. x1 = xy1_[0]; y1 = xy1_[1];
  3386. x2 = xy2_[0]; y2 = xy2_[1];
  3387. /* Find top-left */
  3388. if (x0 <= x1 && y0 <= y1) {
  3389. if (x0 <= x2 && y0 <= y2) {
  3390. A = k0;
  3391. } else {
  3392. A = k2;
  3393. }
  3394. } else {
  3395. if (x1 <= x2 && y1 <= y2) {
  3396. A = k1;
  3397. } else {
  3398. A = k2;
  3399. }
  3400. }
  3401. /* Find bottom-right */
  3402. if (x0 >= x1 && y0 >= y1) {
  3403. if (x0 >= x2 && y0 >= y2) {
  3404. B = k0;
  3405. } else {
  3406. B = k2;
  3407. }
  3408. } else {
  3409. if (x1 >= x2 && y1 >= y2) {
  3410. B = k1;
  3411. } else {
  3412. B = k2;
  3413. }
  3414. }
  3415. /* Find C */
  3416. if (k0 != A && k0 != B) {
  3417. C = k0;
  3418. } else if (k1 != A && k1 != B) {
  3419. C = k1;
  3420. } else {
  3421. C = k2;
  3422. }
  3423. /* Find C2 */
  3424. if (prev[0] != A && prev[0] != B) {
  3425. C2 = prev[0];
  3426. } else if (prev[1] != A && prev[1] != B) {
  3427. C2 = prev[1];
  3428. } else {
  3429. C2 = prev[2];
  3430. }
  3431. xy0_ = (const float *)((const char*)xy + A * xy_stride);
  3432. xy1_ = (const float *)((const char*)xy + B * xy_stride);
  3433. xy2_ = (const float *)((const char*)xy + C * xy_stride);
  3434. x0 = xy0_[0]; y0 = xy0_[1];
  3435. x1 = xy1_[0]; y1 = xy1_[1];
  3436. x2 = xy2_[0]; y2 = xy2_[1];
  3437. /* Check if triangle A B C is rectangle */
  3438. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)){
  3439. /* ok */
  3440. } else {
  3441. is_quad = 0;
  3442. #if DEBUG_SW_RENDER_GEOMETRY
  3443. is_rectangle = 0;
  3444. #endif
  3445. }
  3446. xy2_ = (const float *)((const char*)xy + C2 * xy_stride);
  3447. x2 = xy2_[0]; y2 = xy2_[1];
  3448. /* Check if triangle A B C2 is rectangle */
  3449. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)){
  3450. /* ok */
  3451. } else {
  3452. is_quad = 0;
  3453. #if DEBUG_SW_RENDER_GEOMETRY
  3454. is_rectangle = 0;
  3455. #endif
  3456. }
  3457. }
  3458. /* Check if uniformly colored */
  3459. if (is_quad) {
  3460. const int col0_ = *(const int *)((const char*)color + A * color_stride);
  3461. const int col1_ = *(const int *)((const char*)color + B * color_stride);
  3462. const int col2_ = *(const int *)((const char*)color + C * color_stride);
  3463. const int col3_ = *(const int *)((const char*)color + C2 * color_stride);
  3464. if (col0_ == col1_ && col0_ == col2_ && col0_ == col3_) {
  3465. /* ok */
  3466. } else {
  3467. is_quad = 0;
  3468. #if DEBUG_SW_RENDER_GEOMETRY
  3469. is_uniform = 0;
  3470. #endif
  3471. }
  3472. }
  3473. /* Start rendering rect */
  3474. if (is_quad) {
  3475. SDL_Rect s;
  3476. SDL_FRect d;
  3477. const float *xy0_, *xy1_, *uv0_, *uv1_;
  3478. SDL_Color col0_ = *(const SDL_Color *)((const char*)color + k0 * color_stride);
  3479. xy0_ = (const float *)((const char*)xy + A * xy_stride);
  3480. xy1_ = (const float *)((const char*)xy + B * xy_stride);
  3481. if (texture) {
  3482. uv0_ = (const float *)((const char*)uv + A * uv_stride);
  3483. uv1_ = (const float *)((const char*)uv + B * uv_stride);
  3484. s.x = (int) (uv0_[0] * texw);
  3485. s.y = (int) (uv0_[1] * texh);
  3486. s.w = (int) (uv1_[0] * texw - s.x);
  3487. s.h = (int) (uv1_[1] * texh - s.y);
  3488. }
  3489. d.x = xy0_[0];
  3490. d.y = xy0_[1];
  3491. d.w = xy1_[0] - d.x;
  3492. d.h = xy1_[1] - d.y;
  3493. /* Rect + texture */
  3494. if (texture && s.w != 0 && s.h != 0) {
  3495. SDL_SetTextureAlphaMod(texture, col0_.a);
  3496. SDL_SetTextureColorMod(texture, col0_.r, col0_.g, col0_.b);
  3497. if (s.w > 0 && s.h > 0) {
  3498. SDL_RenderCopyF(renderer, texture, &s, &d);
  3499. } else {
  3500. int flags = 0;
  3501. if (s.w < 0) {
  3502. flags |= SDL_FLIP_HORIZONTAL;
  3503. s.w *= -1;
  3504. s.x -= s.w;
  3505. }
  3506. if (s.h < 0) {
  3507. flags |= SDL_FLIP_VERTICAL;
  3508. s.h *= -1;
  3509. s.y -= s.h;
  3510. }
  3511. SDL_RenderCopyExF(renderer, texture, &s, &d, 0, NULL, flags);
  3512. }
  3513. #if DEBUG_SW_RENDER_GEOMETRY
  3514. SDL_Log("Rect-COPY: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3515. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  3516. #endif
  3517. } else if (d.w != 0.0f && d.h != 0.0f) { /* Rect, no texture */
  3518. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  3519. SDL_SetRenderDrawColor(renderer, col0_.r, col0_.g, col0_.b, col0_.a);
  3520. SDL_RenderFillRectF(renderer, &d);
  3521. #if DEBUG_SW_RENDER_GEOMETRY
  3522. SDL_Log("Rect-FILL: RGB %d %d %d - Alpha:%d - texture=%p: dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3523. (void *)texture, d.x, d.y, d.w, d.h);
  3524. } else {
  3525. SDL_Log("Rect-DISMISS: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3526. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  3527. #endif
  3528. }
  3529. prev[0] = -1;
  3530. } else {
  3531. /* Render triangles */
  3532. if (prev[0] != -1) {
  3533. #if DEBUG_SW_RENDER_GEOMETRY
  3534. SDL_Log("Triangle %d %d %d - is_uniform:%d is_rectangle:%d", prev[0], prev[1], prev[2], is_uniform, is_rectangle);
  3535. #endif
  3536. retval = QueueCmdGeometry(renderer, texture,
  3537. xy, xy_stride, color, color_stride, uv, uv_stride,
  3538. num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
  3539. if (retval < 0) {
  3540. goto end;
  3541. } else {
  3542. FlushRenderCommandsIfNotBatching(renderer);
  3543. }
  3544. }
  3545. prev[0] = k0;
  3546. prev[1] = k1;
  3547. prev[2] = k2;
  3548. }
  3549. } /* End for(), next triangle */
  3550. if (prev[0] != -1) {
  3551. /* flush the last triangle */
  3552. #if DEBUG_SW_RENDER_GEOMETRY
  3553. SDL_Log("Last triangle %d %d %d", prev[0], prev[1], prev[2]);
  3554. #endif
  3555. retval = QueueCmdGeometry(renderer, texture,
  3556. xy, xy_stride, color, color_stride, uv, uv_stride,
  3557. num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
  3558. if (retval < 0) {
  3559. goto end;
  3560. } else {
  3561. FlushRenderCommandsIfNotBatching(renderer);
  3562. }
  3563. }
  3564. end:
  3565. /* Restore */
  3566. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  3567. SDL_SetRenderDrawColor(renderer, r, g, b, a);
  3568. return retval;
  3569. }
  3570. int
  3571. SDL_RenderGeometryRaw(SDL_Renderer *renderer,
  3572. SDL_Texture *texture,
  3573. const float *xy, int xy_stride,
  3574. const SDL_Color *color, int color_stride,
  3575. const float *uv, int uv_stride,
  3576. int num_vertices,
  3577. const void *indices, int num_indices, int size_indices)
  3578. {
  3579. int i;
  3580. int retval = 0;
  3581. int count = indices ? num_indices : num_vertices;
  3582. CHECK_RENDERER_MAGIC(renderer, -1);
  3583. if (!renderer->QueueGeometry) {
  3584. return SDL_Unsupported();
  3585. }
  3586. if (texture) {
  3587. CHECK_TEXTURE_MAGIC(texture, -1);
  3588. if (renderer != texture->renderer) {
  3589. return SDL_SetError("Texture was not created with this renderer");
  3590. }
  3591. }
  3592. if (!xy) {
  3593. return SDL_InvalidParamError("xy");
  3594. }
  3595. if (!color) {
  3596. return SDL_InvalidParamError("color");
  3597. }
  3598. if (texture && !uv) {
  3599. return SDL_InvalidParamError("uv");
  3600. }
  3601. if (count % 3 != 0) {
  3602. return SDL_InvalidParamError(indices ? "num_indices" : "num_vertices");
  3603. }
  3604. if (indices) {
  3605. if (size_indices != 1 && size_indices != 2 && size_indices != 4) {
  3606. return SDL_InvalidParamError("size_indices");
  3607. }
  3608. } else {
  3609. size_indices = 0;
  3610. }
  3611. #if DONT_DRAW_WHILE_HIDDEN
  3612. /* Don't draw while we're hidden */
  3613. if (renderer->hidden) {
  3614. return 0;
  3615. }
  3616. #endif
  3617. if (num_vertices < 3) {
  3618. return 0;
  3619. }
  3620. if (texture && texture->native) {
  3621. texture = texture->native;
  3622. }
  3623. if (texture) {
  3624. for (i = 0; i < num_vertices; ++i) {
  3625. const float *uv_ = (const float *)((const char*)uv + i * uv_stride);
  3626. float u = uv_[0];
  3627. float v = uv_[1];
  3628. if (u < 0.0f || v < 0.0f || u > 1.0f || v > 1.0f) {
  3629. return SDL_SetError("Values of 'uv' out of bounds %f %f at %d/%d", u, v, i, num_vertices);
  3630. }
  3631. }
  3632. }
  3633. if (indices) {
  3634. for (i = 0; i < num_indices; ++i) {
  3635. int j;
  3636. if (size_indices == 4) {
  3637. j = ((const Uint32 *)indices)[i];
  3638. } else if (size_indices == 2) {
  3639. j = ((const Uint16 *)indices)[i];
  3640. } else {
  3641. j = ((const Uint8 *)indices)[i];
  3642. }
  3643. if (j < 0 || j >= num_vertices) {
  3644. return SDL_SetError("Values of 'indices' out of bounds");
  3645. }
  3646. }
  3647. }
  3648. if (texture) {
  3649. texture->last_command_generation = renderer->render_command_generation;
  3650. }
  3651. /* For the software renderer, try to reinterpret triangles as SDL_Rect */
  3652. if (renderer->info.flags & SDL_RENDERER_SOFTWARE) {
  3653. return SDL_SW_RenderGeometryRaw(renderer, texture,
  3654. xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices,
  3655. indices, num_indices, size_indices);
  3656. }
  3657. retval = QueueCmdGeometry(renderer, texture,
  3658. xy, xy_stride, color, color_stride, uv, uv_stride,
  3659. num_vertices,
  3660. indices, num_indices, size_indices,
  3661. renderer->scale.x, renderer->scale.y);
  3662. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  3663. }
  3664. int
  3665. SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  3666. Uint32 format, void * pixels, int pitch)
  3667. {
  3668. SDL_Rect real_rect;
  3669. CHECK_RENDERER_MAGIC(renderer, -1);
  3670. if (!renderer->RenderReadPixels) {
  3671. return SDL_Unsupported();
  3672. }
  3673. FlushRenderCommands(renderer); /* we need to render before we read the results. */
  3674. if (!format) {
  3675. if (renderer->target == NULL) {
  3676. format = SDL_GetWindowPixelFormat(renderer->window);
  3677. } else {
  3678. format = renderer->target->format;
  3679. }
  3680. }
  3681. real_rect.x = (int)SDL_floor(renderer->viewport.x);
  3682. real_rect.y = (int)SDL_floor(renderer->viewport.y);
  3683. real_rect.w = (int)SDL_floor(renderer->viewport.w);
  3684. real_rect.h = (int)SDL_floor(renderer->viewport.h);
  3685. if (rect) {
  3686. if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  3687. return 0;
  3688. }
  3689. if (real_rect.y > rect->y) {
  3690. pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  3691. }
  3692. if (real_rect.x > rect->x) {
  3693. int bpp = SDL_BYTESPERPIXEL(format);
  3694. pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  3695. }
  3696. }
  3697. return renderer->RenderReadPixels(renderer, &real_rect,
  3698. format, pixels, pitch);
  3699. }
  3700. void
  3701. SDL_RenderPresent(SDL_Renderer * renderer)
  3702. {
  3703. CHECK_RENDERER_MAGIC(renderer, );
  3704. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  3705. #if DONT_DRAW_WHILE_HIDDEN
  3706. /* Don't present while we're hidden */
  3707. if (renderer->hidden) {
  3708. return;
  3709. }
  3710. #endif
  3711. renderer->RenderPresent(renderer);
  3712. }
  3713. void
  3714. SDL_DestroyTexture(SDL_Texture * texture)
  3715. {
  3716. SDL_Renderer *renderer;
  3717. CHECK_TEXTURE_MAGIC(texture, );
  3718. renderer = texture->renderer;
  3719. if (texture == renderer->target) {
  3720. SDL_SetRenderTarget(renderer, NULL); /* implies command queue flush */
  3721. } else {
  3722. FlushRenderCommandsIfTextureNeeded(texture);
  3723. }
  3724. texture->magic = NULL;
  3725. if (texture->next) {
  3726. texture->next->prev = texture->prev;
  3727. }
  3728. if (texture->prev) {
  3729. texture->prev->next = texture->next;
  3730. } else {
  3731. renderer->textures = texture->next;
  3732. }
  3733. if (texture->native) {
  3734. SDL_DestroyTexture(texture->native);
  3735. }
  3736. #if SDL_HAVE_YUV
  3737. if (texture->yuv) {
  3738. SDL_SW_DestroyYUVTexture(texture->yuv);
  3739. }
  3740. #endif
  3741. SDL_free(texture->pixels);
  3742. renderer->DestroyTexture(renderer, texture);
  3743. SDL_FreeSurface(texture->locked_surface);
  3744. texture->locked_surface = NULL;
  3745. SDL_free(texture);
  3746. }
  3747. void
  3748. SDL_DestroyRenderer(SDL_Renderer * renderer)
  3749. {
  3750. SDL_RenderCommand *cmd;
  3751. CHECK_RENDERER_MAGIC(renderer, );
  3752. SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
  3753. if (renderer->render_commands_tail != NULL) {
  3754. renderer->render_commands_tail->next = renderer->render_commands_pool;
  3755. cmd = renderer->render_commands;
  3756. } else {
  3757. cmd = renderer->render_commands_pool;
  3758. }
  3759. renderer->render_commands_pool = NULL;
  3760. renderer->render_commands_tail = NULL;
  3761. renderer->render_commands = NULL;
  3762. while (cmd != NULL) {
  3763. SDL_RenderCommand *next = cmd->next;
  3764. SDL_free(cmd);
  3765. cmd = next;
  3766. }
  3767. SDL_free(renderer->vertex_data);
  3768. /* Free existing textures for this renderer */
  3769. while (renderer->textures) {
  3770. SDL_Texture *tex = renderer->textures; (void) tex;
  3771. SDL_DestroyTexture(renderer->textures);
  3772. SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
  3773. }
  3774. if (renderer->window) {
  3775. SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
  3776. }
  3777. /* It's no longer magical... */
  3778. renderer->magic = NULL;
  3779. /* Free the target mutex */
  3780. SDL_DestroyMutex(renderer->target_mutex);
  3781. renderer->target_mutex = NULL;
  3782. /* Free the renderer instance */
  3783. renderer->DestroyRenderer(renderer);
  3784. }
  3785. int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
  3786. {
  3787. SDL_Renderer *renderer;
  3788. CHECK_TEXTURE_MAGIC(texture, -1);
  3789. renderer = texture->renderer;
  3790. if (texture->native) {
  3791. return SDL_GL_BindTexture(texture->native, texw, texh);
  3792. } else if (renderer && renderer->GL_BindTexture) {
  3793. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app is going to mess with it. */
  3794. return renderer->GL_BindTexture(renderer, texture, texw, texh);
  3795. } else {
  3796. return SDL_Unsupported();
  3797. }
  3798. }
  3799. int SDL_GL_UnbindTexture(SDL_Texture *texture)
  3800. {
  3801. SDL_Renderer *renderer;
  3802. CHECK_TEXTURE_MAGIC(texture, -1);
  3803. renderer = texture->renderer;
  3804. if (texture->native) {
  3805. return SDL_GL_UnbindTexture(texture->native);
  3806. } else if (renderer && renderer->GL_UnbindTexture) {
  3807. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app messed with it. */
  3808. return renderer->GL_UnbindTexture(renderer, texture);
  3809. }
  3810. return SDL_Unsupported();
  3811. }
  3812. void *
  3813. SDL_RenderGetMetalLayer(SDL_Renderer * renderer)
  3814. {
  3815. CHECK_RENDERER_MAGIC(renderer, NULL);
  3816. if (renderer->GetMetalLayer) {
  3817. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  3818. return renderer->GetMetalLayer(renderer);
  3819. }
  3820. return NULL;
  3821. }
  3822. void *
  3823. SDL_RenderGetMetalCommandEncoder(SDL_Renderer * renderer)
  3824. {
  3825. CHECK_RENDERER_MAGIC(renderer, NULL);
  3826. if (renderer->GetMetalCommandEncoder) {
  3827. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  3828. return renderer->GetMetalCommandEncoder(renderer);
  3829. }
  3830. return NULL;
  3831. }
  3832. static SDL_BlendMode
  3833. SDL_GetShortBlendMode(SDL_BlendMode blendMode)
  3834. {
  3835. if (blendMode == SDL_BLENDMODE_NONE_FULL) {
  3836. return SDL_BLENDMODE_NONE;
  3837. }
  3838. if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
  3839. return SDL_BLENDMODE_BLEND;
  3840. }
  3841. if (blendMode == SDL_BLENDMODE_ADD_FULL) {
  3842. return SDL_BLENDMODE_ADD;
  3843. }
  3844. if (blendMode == SDL_BLENDMODE_MOD_FULL) {
  3845. return SDL_BLENDMODE_MOD;
  3846. }
  3847. if (blendMode == SDL_BLENDMODE_MUL_FULL) {
  3848. return SDL_BLENDMODE_MUL;
  3849. }
  3850. return blendMode;
  3851. }
  3852. static SDL_BlendMode
  3853. SDL_GetLongBlendMode(SDL_BlendMode blendMode)
  3854. {
  3855. if (blendMode == SDL_BLENDMODE_NONE) {
  3856. return SDL_BLENDMODE_NONE_FULL;
  3857. }
  3858. if (blendMode == SDL_BLENDMODE_BLEND) {
  3859. return SDL_BLENDMODE_BLEND_FULL;
  3860. }
  3861. if (blendMode == SDL_BLENDMODE_ADD) {
  3862. return SDL_BLENDMODE_ADD_FULL;
  3863. }
  3864. if (blendMode == SDL_BLENDMODE_MOD) {
  3865. return SDL_BLENDMODE_MOD_FULL;
  3866. }
  3867. if (blendMode == SDL_BLENDMODE_MUL) {
  3868. return SDL_BLENDMODE_MUL_FULL;
  3869. }
  3870. return blendMode;
  3871. }
  3872. SDL_BlendMode
  3873. SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
  3874. SDL_BlendOperation colorOperation,
  3875. SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
  3876. SDL_BlendOperation alphaOperation)
  3877. {
  3878. SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
  3879. srcAlphaFactor, dstAlphaFactor, alphaOperation);
  3880. return SDL_GetShortBlendMode(blendMode);
  3881. }
  3882. SDL_BlendFactor
  3883. SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
  3884. {
  3885. blendMode = SDL_GetLongBlendMode(blendMode);
  3886. return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
  3887. }
  3888. SDL_BlendFactor
  3889. SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
  3890. {
  3891. blendMode = SDL_GetLongBlendMode(blendMode);
  3892. return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
  3893. }
  3894. SDL_BlendOperation
  3895. SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
  3896. {
  3897. blendMode = SDL_GetLongBlendMode(blendMode);
  3898. return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
  3899. }
  3900. SDL_BlendFactor
  3901. SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
  3902. {
  3903. blendMode = SDL_GetLongBlendMode(blendMode);
  3904. return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
  3905. }
  3906. SDL_BlendFactor
  3907. SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
  3908. {
  3909. blendMode = SDL_GetLongBlendMode(blendMode);
  3910. return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
  3911. }
  3912. SDL_BlendOperation
  3913. SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
  3914. {
  3915. blendMode = SDL_GetLongBlendMode(blendMode);
  3916. return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
  3917. }
  3918. int
  3919. SDL_RenderSetVSync(SDL_Renderer * renderer, int vsync)
  3920. {
  3921. CHECK_RENDERER_MAGIC(renderer, -1);
  3922. if (vsync != 0 && vsync != 1) {
  3923. return SDL_Unsupported();
  3924. }
  3925. if (renderer->SetVSync) {
  3926. return renderer->SetVSync(renderer, vsync);
  3927. }
  3928. return SDL_Unsupported();
  3929. }
  3930. /* vi: set ts=4 sw=4 expandtab: */