SDL_render.c 105 KB

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