SDL_render.c 88 KB

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