SDL_render.c 101 KB

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