SDL_render.c 141 KB

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