SDL_render.c 101 KB

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