SDL_render.c 138 KB

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