SDL_render.c 125 KB

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