SDL_render.c 99 KB

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