SDL_render.c 139 KB

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