SDL_render.c 99 KB

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