SDL_render.c 138 KB

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