SDL_render.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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 int UpdateLogicalSize(SDL_Renderer *renderer);
  92. int
  93. SDL_GetNumRenderDrivers(void)
  94. {
  95. #if !SDL_RENDER_DISABLED
  96. return SDL_arraysize(render_drivers);
  97. #else
  98. return 0;
  99. #endif
  100. }
  101. int
  102. SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  103. {
  104. #if !SDL_RENDER_DISABLED
  105. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  106. return SDL_SetError("index must be in the range of 0 - %d",
  107. SDL_GetNumRenderDrivers() - 1);
  108. }
  109. *info = render_drivers[index]->info;
  110. return 0;
  111. #else
  112. return SDL_SetError("SDL not built with rendering support");
  113. #endif
  114. }
  115. static int SDLCALL
  116. SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  117. {
  118. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  119. if (event->type == SDL_WINDOWEVENT) {
  120. SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
  121. if (window == renderer->window) {
  122. if (renderer->WindowEvent) {
  123. renderer->WindowEvent(renderer, &event->window);
  124. }
  125. if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  126. /* Make sure we're operating on the default render target */
  127. SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
  128. if (saved_target) {
  129. SDL_SetRenderTarget(renderer, NULL);
  130. }
  131. if (renderer->logical_w) {
  132. UpdateLogicalSize(renderer);
  133. } else {
  134. /* Window was resized, reset viewport */
  135. int w, h;
  136. if (renderer->GetOutputSize) {
  137. renderer->GetOutputSize(renderer, &w, &h);
  138. } else {
  139. SDL_GetWindowSize(renderer->window, &w, &h);
  140. }
  141. if (renderer->target) {
  142. renderer->viewport_backup.x = 0;
  143. renderer->viewport_backup.y = 0;
  144. renderer->viewport_backup.w = w;
  145. renderer->viewport_backup.h = h;
  146. } else {
  147. renderer->viewport.x = 0;
  148. renderer->viewport.y = 0;
  149. renderer->viewport.w = w;
  150. renderer->viewport.h = h;
  151. renderer->UpdateViewport(renderer);
  152. }
  153. }
  154. if (saved_target) {
  155. SDL_SetRenderTarget(renderer, saved_target);
  156. }
  157. } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
  158. renderer->hidden = SDL_TRUE;
  159. } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
  160. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  161. renderer->hidden = SDL_FALSE;
  162. }
  163. } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
  164. renderer->hidden = SDL_TRUE;
  165. } else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
  166. event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
  167. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  168. renderer->hidden = SDL_FALSE;
  169. }
  170. }
  171. }
  172. } else if (event->type == SDL_MOUSEMOTION) {
  173. SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
  174. if (renderer->logical_w && window == renderer->window) {
  175. event->motion.x -= (int)(renderer->viewport.x * renderer->dpi_scale.x);
  176. event->motion.y -= (int)(renderer->viewport.y * renderer->dpi_scale.y);
  177. event->motion.x = (int)(event->motion.x / (renderer->scale.x * renderer->dpi_scale.x));
  178. event->motion.y = (int)(event->motion.y / (renderer->scale.y * renderer->dpi_scale.y));
  179. if (event->motion.xrel > 0) {
  180. event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / (renderer->scale.x * renderer->dpi_scale.x)));
  181. } else if (event->motion.xrel < 0) {
  182. event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / (renderer->scale.x * renderer->dpi_scale.x)));
  183. }
  184. if (event->motion.yrel > 0) {
  185. event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / (renderer->scale.y * renderer->dpi_scale.y)));
  186. } else if (event->motion.yrel < 0) {
  187. event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / (renderer->scale.y * renderer->dpi_scale.y)));
  188. }
  189. }
  190. } else if (event->type == SDL_MOUSEBUTTONDOWN ||
  191. event->type == SDL_MOUSEBUTTONUP) {
  192. SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
  193. if (renderer->logical_w && window == renderer->window) {
  194. event->button.x -= (int)(renderer->viewport.x * renderer->dpi_scale.x);
  195. event->button.y -= (int)(renderer->viewport.y * renderer->dpi_scale.y);
  196. event->button.x = (int)(event->button.x / (renderer->scale.x * renderer->dpi_scale.x));
  197. event->button.y = (int)(event->button.y / (renderer->scale.y * renderer->dpi_scale.y));
  198. }
  199. } else if (event->type == SDL_FINGERDOWN ||
  200. event->type == SDL_FINGERUP ||
  201. event->type == SDL_FINGERMOTION) {
  202. if (renderer->logical_w) {
  203. int w = 1;
  204. int h = 1;
  205. SDL_GetRendererOutputSize(renderer, &w, &h);
  206. event->tfinger.x *= (w - 1);
  207. event->tfinger.y *= (h - 1);
  208. event->tfinger.x -= (renderer->viewport.x * renderer->dpi_scale.x);
  209. event->tfinger.y -= (renderer->viewport.y * renderer->dpi_scale.y);
  210. event->tfinger.x = (event->tfinger.x / (renderer->scale.x * renderer->dpi_scale.x));
  211. event->tfinger.y = (event->tfinger.y / (renderer->scale.y * renderer->dpi_scale.y));
  212. if (renderer->logical_w > 1) {
  213. event->tfinger.x = event->tfinger.x / (renderer->logical_w - 1);
  214. } else {
  215. event->tfinger.x = 0.5f;
  216. }
  217. if (renderer->logical_h > 1) {
  218. event->tfinger.y = event->tfinger.y / (renderer->logical_h - 1);
  219. } else {
  220. event->tfinger.y = 0.5f;
  221. }
  222. }
  223. }
  224. return 0;
  225. }
  226. int
  227. SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
  228. SDL_Window **window, SDL_Renderer **renderer)
  229. {
  230. *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
  231. SDL_WINDOWPOS_UNDEFINED,
  232. width, height, window_flags);
  233. if (!*window) {
  234. *renderer = NULL;
  235. return -1;
  236. }
  237. *renderer = SDL_CreateRenderer(*window, -1, 0);
  238. if (!*renderer) {
  239. return -1;
  240. }
  241. return 0;
  242. }
  243. SDL_Renderer *
  244. SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  245. {
  246. #if !SDL_RENDER_DISABLED
  247. SDL_Renderer *renderer = NULL;
  248. int n = SDL_GetNumRenderDrivers();
  249. const char *hint;
  250. if (!window) {
  251. SDL_SetError("Invalid window");
  252. return NULL;
  253. }
  254. if (SDL_GetRenderer(window)) {
  255. SDL_SetError("Renderer already associated with window");
  256. return NULL;
  257. }
  258. if (SDL_GetHint(SDL_HINT_RENDER_VSYNC)) {
  259. if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
  260. flags |= SDL_RENDERER_PRESENTVSYNC;
  261. } else {
  262. flags &= ~SDL_RENDERER_PRESENTVSYNC;
  263. }
  264. }
  265. if (index < 0) {
  266. hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  267. if (hint) {
  268. for (index = 0; index < n; ++index) {
  269. const SDL_RenderDriver *driver = render_drivers[index];
  270. if (SDL_strcasecmp(hint, driver->info.name) == 0) {
  271. /* Create a new renderer instance */
  272. renderer = driver->CreateRenderer(window, flags);
  273. break;
  274. }
  275. }
  276. }
  277. if (!renderer) {
  278. for (index = 0; index < n; ++index) {
  279. const SDL_RenderDriver *driver = render_drivers[index];
  280. if ((driver->info.flags & flags) == flags) {
  281. /* Create a new renderer instance */
  282. renderer = driver->CreateRenderer(window, flags);
  283. if (renderer) {
  284. /* Yay, we got one! */
  285. break;
  286. }
  287. }
  288. }
  289. }
  290. if (index == n) {
  291. SDL_SetError("Couldn't find matching render driver");
  292. return NULL;
  293. }
  294. } else {
  295. if (index >= SDL_GetNumRenderDrivers()) {
  296. SDL_SetError("index must be -1 or in the range of 0 - %d",
  297. SDL_GetNumRenderDrivers() - 1);
  298. return NULL;
  299. }
  300. /* Create a new renderer instance */
  301. renderer = render_drivers[index]->CreateRenderer(window, flags);
  302. }
  303. if (renderer) {
  304. renderer->magic = &renderer_magic;
  305. renderer->window = window;
  306. renderer->scale.x = 1.0f;
  307. renderer->scale.y = 1.0f;
  308. renderer->dpi_scale.x = 1.0f;
  309. renderer->dpi_scale.y = 1.0f;
  310. if (window && renderer->GetOutputSize) {
  311. int window_w, window_h;
  312. int output_w, output_h;
  313. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  314. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  315. renderer->dpi_scale.x = (float)window_w / output_w;
  316. renderer->dpi_scale.y = (float)window_h / output_h;
  317. }
  318. }
  319. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
  320. renderer->hidden = SDL_TRUE;
  321. } else {
  322. renderer->hidden = SDL_FALSE;
  323. }
  324. SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
  325. SDL_RenderSetViewport(renderer, NULL);
  326. SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
  327. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  328. "Created renderer: %s", renderer->info.name);
  329. }
  330. return renderer;
  331. #else
  332. SDL_SetError("SDL not built with rendering support");
  333. return NULL;
  334. #endif
  335. }
  336. SDL_Renderer *
  337. SDL_CreateSoftwareRenderer(SDL_Surface * surface)
  338. {
  339. #if !SDL_RENDER_DISABLED
  340. SDL_Renderer *renderer;
  341. renderer = SW_CreateRendererForSurface(surface);
  342. if (renderer) {
  343. renderer->magic = &renderer_magic;
  344. renderer->scale.x = 1.0f;
  345. renderer->scale.y = 1.0f;
  346. SDL_RenderSetViewport(renderer, NULL);
  347. }
  348. return renderer;
  349. #else
  350. SDL_SetError("SDL not built with rendering support");
  351. return NULL;
  352. #endif /* !SDL_RENDER_DISABLED */
  353. }
  354. SDL_Renderer *
  355. SDL_GetRenderer(SDL_Window * window)
  356. {
  357. return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
  358. }
  359. int
  360. SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
  361. {
  362. CHECK_RENDERER_MAGIC(renderer, -1);
  363. *info = renderer->info;
  364. return 0;
  365. }
  366. int
  367. SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
  368. {
  369. CHECK_RENDERER_MAGIC(renderer, -1);
  370. if (renderer->target) {
  371. return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
  372. } else if (renderer->GetOutputSize) {
  373. return renderer->GetOutputSize(renderer, w, h);
  374. } else if (renderer->window) {
  375. SDL_GetWindowSize(renderer->window, w, h);
  376. return 0;
  377. } else {
  378. SDL_assert(0 && "This should never happen");
  379. return SDL_SetError("Renderer doesn't support querying output size");
  380. }
  381. }
  382. static SDL_bool
  383. IsSupportedBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  384. {
  385. switch (blendMode)
  386. {
  387. /* These are required to be supported by all renderers */
  388. case SDL_BLENDMODE_NONE:
  389. case SDL_BLENDMODE_BLEND:
  390. case SDL_BLENDMODE_ADD:
  391. case SDL_BLENDMODE_MOD:
  392. return SDL_TRUE;
  393. default:
  394. return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
  395. }
  396. }
  397. static SDL_bool
  398. IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  399. {
  400. Uint32 i;
  401. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  402. if (renderer->info.texture_formats[i] == format) {
  403. return SDL_TRUE;
  404. }
  405. }
  406. return SDL_FALSE;
  407. }
  408. static Uint32
  409. GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  410. {
  411. Uint32 i;
  412. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  413. /* Look for an exact match */
  414. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  415. if (renderer->info.texture_formats[i] == format) {
  416. return renderer->info.texture_formats[i];
  417. }
  418. }
  419. } else {
  420. SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  421. /* We just want to match the first format that has the same channels */
  422. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  423. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  424. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
  425. return renderer->info.texture_formats[i];
  426. }
  427. }
  428. }
  429. return renderer->info.texture_formats[0];
  430. }
  431. SDL_Texture *
  432. SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
  433. {
  434. SDL_Texture *texture;
  435. CHECK_RENDERER_MAGIC(renderer, NULL);
  436. if (!format) {
  437. format = renderer->info.texture_formats[0];
  438. }
  439. if (SDL_BYTESPERPIXEL(format) == 0) {
  440. SDL_SetError("Invalid texture format");
  441. return NULL;
  442. }
  443. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  444. SDL_SetError("Palettized textures are not supported");
  445. return NULL;
  446. }
  447. if (w <= 0 || h <= 0) {
  448. SDL_SetError("Texture dimensions can't be 0");
  449. return NULL;
  450. }
  451. if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
  452. (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
  453. SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
  454. return NULL;
  455. }
  456. texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  457. if (!texture) {
  458. SDL_OutOfMemory();
  459. return NULL;
  460. }
  461. texture->magic = &texture_magic;
  462. texture->format = format;
  463. texture->access = access;
  464. texture->w = w;
  465. texture->h = h;
  466. texture->r = 255;
  467. texture->g = 255;
  468. texture->b = 255;
  469. texture->a = 255;
  470. texture->renderer = renderer;
  471. texture->next = renderer->textures;
  472. if (renderer->textures) {
  473. renderer->textures->prev = texture;
  474. }
  475. renderer->textures = texture;
  476. if (IsSupportedFormat(renderer, format)) {
  477. if (renderer->CreateTexture(renderer, texture) < 0) {
  478. SDL_DestroyTexture(texture);
  479. return NULL;
  480. }
  481. } else {
  482. texture->native = SDL_CreateTexture(renderer,
  483. GetClosestSupportedFormat(renderer, format),
  484. access, w, h);
  485. if (!texture->native) {
  486. SDL_DestroyTexture(texture);
  487. return NULL;
  488. }
  489. /* Swap textures to have texture before texture->native in the list */
  490. texture->native->next = texture->next;
  491. if (texture->native->next) {
  492. texture->native->next->prev = texture->native;
  493. }
  494. texture->prev = texture->native->prev;
  495. if (texture->prev) {
  496. texture->prev->next = texture;
  497. }
  498. texture->native->prev = texture;
  499. texture->next = texture->native;
  500. renderer->textures = texture;
  501. if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  502. texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
  503. if (!texture->yuv) {
  504. SDL_DestroyTexture(texture);
  505. return NULL;
  506. }
  507. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  508. /* The pitch is 4 byte aligned */
  509. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  510. texture->pixels = SDL_calloc(1, texture->pitch * h);
  511. if (!texture->pixels) {
  512. SDL_DestroyTexture(texture);
  513. return NULL;
  514. }
  515. }
  516. }
  517. return texture;
  518. }
  519. SDL_Texture *
  520. SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
  521. {
  522. const SDL_PixelFormat *fmt;
  523. SDL_bool needAlpha;
  524. Uint32 i;
  525. Uint32 format;
  526. SDL_Texture *texture;
  527. CHECK_RENDERER_MAGIC(renderer, NULL);
  528. if (!surface) {
  529. SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  530. return NULL;
  531. }
  532. /* See what the best texture format is */
  533. fmt = surface->format;
  534. if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
  535. needAlpha = SDL_TRUE;
  536. } else {
  537. needAlpha = SDL_FALSE;
  538. }
  539. format = renderer->info.texture_formats[0];
  540. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  541. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  542. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
  543. format = renderer->info.texture_formats[i];
  544. break;
  545. }
  546. }
  547. texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
  548. surface->w, surface->h);
  549. if (!texture) {
  550. return NULL;
  551. }
  552. if (format == surface->format->format) {
  553. if (SDL_MUSTLOCK(surface)) {
  554. SDL_LockSurface(surface);
  555. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  556. SDL_UnlockSurface(surface);
  557. } else {
  558. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  559. }
  560. } else {
  561. SDL_PixelFormat *dst_fmt;
  562. SDL_Surface *temp = NULL;
  563. /* Set up a destination surface for the texture update */
  564. dst_fmt = SDL_AllocFormat(format);
  565. if (!dst_fmt) {
  566. SDL_DestroyTexture(texture);
  567. return NULL;
  568. }
  569. temp = SDL_ConvertSurface(surface, dst_fmt, 0);
  570. SDL_FreeFormat(dst_fmt);
  571. if (temp) {
  572. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  573. SDL_FreeSurface(temp);
  574. } else {
  575. SDL_DestroyTexture(texture);
  576. return NULL;
  577. }
  578. }
  579. {
  580. Uint8 r, g, b, a;
  581. SDL_BlendMode blendMode;
  582. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  583. SDL_SetTextureColorMod(texture, r, g, b);
  584. SDL_GetSurfaceAlphaMod(surface, &a);
  585. SDL_SetTextureAlphaMod(texture, a);
  586. if (SDL_GetColorKey(surface, NULL) == 0) {
  587. /* We converted to a texture with alpha format */
  588. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  589. } else {
  590. SDL_GetSurfaceBlendMode(surface, &blendMode);
  591. SDL_SetTextureBlendMode(texture, blendMode);
  592. }
  593. }
  594. return texture;
  595. }
  596. int
  597. SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  598. int *w, int *h)
  599. {
  600. CHECK_TEXTURE_MAGIC(texture, -1);
  601. if (format) {
  602. *format = texture->format;
  603. }
  604. if (access) {
  605. *access = texture->access;
  606. }
  607. if (w) {
  608. *w = texture->w;
  609. }
  610. if (h) {
  611. *h = texture->h;
  612. }
  613. return 0;
  614. }
  615. int
  616. SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  617. {
  618. SDL_Renderer *renderer;
  619. CHECK_TEXTURE_MAGIC(texture, -1);
  620. renderer = texture->renderer;
  621. if (r < 255 || g < 255 || b < 255) {
  622. texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  623. } else {
  624. texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  625. }
  626. texture->r = r;
  627. texture->g = g;
  628. texture->b = b;
  629. if (texture->native) {
  630. return SDL_SetTextureColorMod(texture->native, r, g, b);
  631. } else if (renderer->SetTextureColorMod) {
  632. return renderer->SetTextureColorMod(renderer, texture);
  633. } else {
  634. return 0;
  635. }
  636. }
  637. int
  638. SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  639. Uint8 * b)
  640. {
  641. CHECK_TEXTURE_MAGIC(texture, -1);
  642. if (r) {
  643. *r = texture->r;
  644. }
  645. if (g) {
  646. *g = texture->g;
  647. }
  648. if (b) {
  649. *b = texture->b;
  650. }
  651. return 0;
  652. }
  653. int
  654. SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  655. {
  656. SDL_Renderer *renderer;
  657. CHECK_TEXTURE_MAGIC(texture, -1);
  658. renderer = texture->renderer;
  659. if (alpha < 255) {
  660. texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  661. } else {
  662. texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  663. }
  664. texture->a = alpha;
  665. if (texture->native) {
  666. return SDL_SetTextureAlphaMod(texture->native, alpha);
  667. } else if (renderer->SetTextureAlphaMod) {
  668. return renderer->SetTextureAlphaMod(renderer, texture);
  669. } else {
  670. return 0;
  671. }
  672. }
  673. int
  674. SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  675. {
  676. CHECK_TEXTURE_MAGIC(texture, -1);
  677. if (alpha) {
  678. *alpha = texture->a;
  679. }
  680. return 0;
  681. }
  682. int
  683. SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
  684. {
  685. SDL_Renderer *renderer;
  686. CHECK_TEXTURE_MAGIC(texture, -1);
  687. renderer = texture->renderer;
  688. if (!IsSupportedBlendMode(renderer, blendMode)) {
  689. return SDL_Unsupported();
  690. }
  691. texture->blendMode = blendMode;
  692. if (texture->native) {
  693. return SDL_SetTextureBlendMode(texture->native, blendMode);
  694. } else if (renderer->SetTextureBlendMode) {
  695. return renderer->SetTextureBlendMode(renderer, texture);
  696. } else {
  697. return 0;
  698. }
  699. }
  700. int
  701. SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
  702. {
  703. CHECK_TEXTURE_MAGIC(texture, -1);
  704. if (blendMode) {
  705. *blendMode = texture->blendMode;
  706. }
  707. return 0;
  708. }
  709. static int
  710. SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  711. const void *pixels, int pitch)
  712. {
  713. SDL_Texture *native = texture->native;
  714. SDL_Rect full_rect;
  715. if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
  716. return -1;
  717. }
  718. full_rect.x = 0;
  719. full_rect.y = 0;
  720. full_rect.w = texture->w;
  721. full_rect.h = texture->h;
  722. rect = &full_rect;
  723. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  724. /* We can lock the texture and copy to it */
  725. void *native_pixels = NULL;
  726. int native_pitch = 0;
  727. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  728. return -1;
  729. }
  730. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  731. rect->w, rect->h, native_pixels, native_pitch);
  732. SDL_UnlockTexture(native);
  733. } else {
  734. /* Use a temporary buffer for updating */
  735. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  736. const size_t alloclen = rect->h * temp_pitch;
  737. if (alloclen > 0) {
  738. void *temp_pixels = SDL_malloc(alloclen);
  739. if (!temp_pixels) {
  740. return SDL_OutOfMemory();
  741. }
  742. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  743. rect->w, rect->h, temp_pixels, temp_pitch);
  744. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  745. SDL_free(temp_pixels);
  746. }
  747. }
  748. return 0;
  749. }
  750. static int
  751. SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  752. const void *pixels, int pitch)
  753. {
  754. SDL_Texture *native = texture->native;
  755. if (!rect->w || !rect->h) {
  756. return 0; /* nothing to do. */
  757. }
  758. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  759. /* We can lock the texture and copy to it */
  760. void *native_pixels = NULL;
  761. int native_pitch = 0;
  762. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  763. return -1;
  764. }
  765. SDL_ConvertPixels(rect->w, rect->h,
  766. texture->format, pixels, pitch,
  767. native->format, native_pixels, native_pitch);
  768. SDL_UnlockTexture(native);
  769. } else {
  770. /* Use a temporary buffer for updating */
  771. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  772. const size_t alloclen = rect->h * temp_pitch;
  773. if (alloclen > 0) {
  774. void *temp_pixels = SDL_malloc(alloclen);
  775. if (!temp_pixels) {
  776. return SDL_OutOfMemory();
  777. }
  778. SDL_ConvertPixels(rect->w, rect->h,
  779. texture->format, pixels, pitch,
  780. native->format, temp_pixels, temp_pitch);
  781. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  782. SDL_free(temp_pixels);
  783. }
  784. }
  785. return 0;
  786. }
  787. int
  788. SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  789. const void *pixels, int pitch)
  790. {
  791. SDL_Renderer *renderer;
  792. SDL_Rect full_rect;
  793. CHECK_TEXTURE_MAGIC(texture, -1);
  794. if (!pixels) {
  795. return SDL_InvalidParamError("pixels");
  796. }
  797. if (!pitch) {
  798. return SDL_InvalidParamError("pitch");
  799. }
  800. if (!rect) {
  801. full_rect.x = 0;
  802. full_rect.y = 0;
  803. full_rect.w = texture->w;
  804. full_rect.h = texture->h;
  805. rect = &full_rect;
  806. }
  807. if ((rect->w == 0) || (rect->h == 0)) {
  808. return 0; /* nothing to do. */
  809. } else if (texture->yuv) {
  810. return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
  811. } else if (texture->native) {
  812. return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
  813. } else {
  814. renderer = texture->renderer;
  815. return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  816. }
  817. }
  818. static int
  819. SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
  820. const Uint8 *Yplane, int Ypitch,
  821. const Uint8 *Uplane, int Upitch,
  822. const Uint8 *Vplane, int Vpitch)
  823. {
  824. SDL_Texture *native = texture->native;
  825. SDL_Rect full_rect;
  826. if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
  827. return -1;
  828. }
  829. full_rect.x = 0;
  830. full_rect.y = 0;
  831. full_rect.w = texture->w;
  832. full_rect.h = texture->h;
  833. rect = &full_rect;
  834. if (!rect->w || !rect->h) {
  835. return 0; /* nothing to do. */
  836. }
  837. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  838. /* We can lock the texture and copy to it */
  839. void *native_pixels = NULL;
  840. int native_pitch = 0;
  841. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  842. return -1;
  843. }
  844. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  845. rect->w, rect->h, native_pixels, native_pitch);
  846. SDL_UnlockTexture(native);
  847. } else {
  848. /* Use a temporary buffer for updating */
  849. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  850. const size_t alloclen = rect->h * temp_pitch;
  851. if (alloclen > 0) {
  852. void *temp_pixels = SDL_malloc(alloclen);
  853. if (!temp_pixels) {
  854. return SDL_OutOfMemory();
  855. }
  856. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  857. rect->w, rect->h, temp_pixels, temp_pitch);
  858. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  859. SDL_free(temp_pixels);
  860. }
  861. }
  862. return 0;
  863. }
  864. int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
  865. const Uint8 *Yplane, int Ypitch,
  866. const Uint8 *Uplane, int Upitch,
  867. const Uint8 *Vplane, int Vpitch)
  868. {
  869. SDL_Renderer *renderer;
  870. SDL_Rect full_rect;
  871. CHECK_TEXTURE_MAGIC(texture, -1);
  872. if (!Yplane) {
  873. return SDL_InvalidParamError("Yplane");
  874. }
  875. if (!Ypitch) {
  876. return SDL_InvalidParamError("Ypitch");
  877. }
  878. if (!Uplane) {
  879. return SDL_InvalidParamError("Uplane");
  880. }
  881. if (!Upitch) {
  882. return SDL_InvalidParamError("Upitch");
  883. }
  884. if (!Vplane) {
  885. return SDL_InvalidParamError("Vplane");
  886. }
  887. if (!Vpitch) {
  888. return SDL_InvalidParamError("Vpitch");
  889. }
  890. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  891. texture->format != SDL_PIXELFORMAT_IYUV) {
  892. return SDL_SetError("Texture format must by YV12 or IYUV");
  893. }
  894. if (!rect) {
  895. full_rect.x = 0;
  896. full_rect.y = 0;
  897. full_rect.w = texture->w;
  898. full_rect.h = texture->h;
  899. rect = &full_rect;
  900. }
  901. if (!rect->w || !rect->h) {
  902. return 0; /* nothing to do. */
  903. }
  904. if (texture->yuv) {
  905. return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  906. } else {
  907. SDL_assert(!texture->native);
  908. renderer = texture->renderer;
  909. SDL_assert(renderer->UpdateTextureYUV);
  910. if (renderer->UpdateTextureYUV) {
  911. return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  912. } else {
  913. return SDL_Unsupported();
  914. }
  915. }
  916. }
  917. static int
  918. SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  919. void **pixels, int *pitch)
  920. {
  921. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  922. }
  923. static int
  924. SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  925. void **pixels, int *pitch)
  926. {
  927. texture->locked_rect = *rect;
  928. *pixels = (void *) ((Uint8 *) texture->pixels +
  929. rect->y * texture->pitch +
  930. rect->x * SDL_BYTESPERPIXEL(texture->format));
  931. *pitch = texture->pitch;
  932. return 0;
  933. }
  934. int
  935. SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
  936. void **pixels, int *pitch)
  937. {
  938. SDL_Renderer *renderer;
  939. SDL_Rect full_rect;
  940. CHECK_TEXTURE_MAGIC(texture, -1);
  941. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  942. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  943. }
  944. if (!rect) {
  945. full_rect.x = 0;
  946. full_rect.y = 0;
  947. full_rect.w = texture->w;
  948. full_rect.h = texture->h;
  949. rect = &full_rect;
  950. }
  951. if (texture->yuv) {
  952. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  953. } else if (texture->native) {
  954. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  955. } else {
  956. renderer = texture->renderer;
  957. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  958. }
  959. }
  960. static void
  961. SDL_UnlockTextureYUV(SDL_Texture * texture)
  962. {
  963. SDL_Texture *native = texture->native;
  964. void *native_pixels = NULL;
  965. int native_pitch = 0;
  966. SDL_Rect rect;
  967. rect.x = 0;
  968. rect.y = 0;
  969. rect.w = texture->w;
  970. rect.h = texture->h;
  971. if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
  972. return;
  973. }
  974. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  975. rect.w, rect.h, native_pixels, native_pitch);
  976. SDL_UnlockTexture(native);
  977. }
  978. static void
  979. SDL_UnlockTextureNative(SDL_Texture * texture)
  980. {
  981. SDL_Texture *native = texture->native;
  982. void *native_pixels = NULL;
  983. int native_pitch = 0;
  984. const SDL_Rect *rect = &texture->locked_rect;
  985. const void* pixels = (void *) ((Uint8 *) texture->pixels +
  986. rect->y * texture->pitch +
  987. rect->x * SDL_BYTESPERPIXEL(texture->format));
  988. int pitch = texture->pitch;
  989. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  990. return;
  991. }
  992. SDL_ConvertPixels(rect->w, rect->h,
  993. texture->format, pixels, pitch,
  994. native->format, native_pixels, native_pitch);
  995. SDL_UnlockTexture(native);
  996. }
  997. void
  998. SDL_UnlockTexture(SDL_Texture * texture)
  999. {
  1000. SDL_Renderer *renderer;
  1001. CHECK_TEXTURE_MAGIC(texture, );
  1002. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1003. return;
  1004. }
  1005. if (texture->yuv) {
  1006. SDL_UnlockTextureYUV(texture);
  1007. } else if (texture->native) {
  1008. SDL_UnlockTextureNative(texture);
  1009. } else {
  1010. renderer = texture->renderer;
  1011. renderer->UnlockTexture(renderer, texture);
  1012. }
  1013. }
  1014. SDL_bool
  1015. SDL_RenderTargetSupported(SDL_Renderer *renderer)
  1016. {
  1017. if (!renderer || !renderer->SetRenderTarget) {
  1018. return SDL_FALSE;
  1019. }
  1020. return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
  1021. }
  1022. int
  1023. SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  1024. {
  1025. if (!SDL_RenderTargetSupported(renderer)) {
  1026. return SDL_Unsupported();
  1027. }
  1028. if (texture == renderer->target) {
  1029. /* Nothing to do! */
  1030. return 0;
  1031. }
  1032. /* texture == NULL is valid and means reset the target to the window */
  1033. if (texture) {
  1034. CHECK_TEXTURE_MAGIC(texture, -1);
  1035. if (renderer != texture->renderer) {
  1036. return SDL_SetError("Texture was not created with this renderer");
  1037. }
  1038. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  1039. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  1040. }
  1041. if (texture->native) {
  1042. /* Always render to the native texture */
  1043. texture = texture->native;
  1044. }
  1045. }
  1046. if (texture && !renderer->target) {
  1047. /* Make a backup of the viewport */
  1048. renderer->viewport_backup = renderer->viewport;
  1049. renderer->clip_rect_backup = renderer->clip_rect;
  1050. renderer->clipping_enabled_backup = renderer->clipping_enabled;
  1051. renderer->scale_backup = renderer->scale;
  1052. renderer->logical_w_backup = renderer->logical_w;
  1053. renderer->logical_h_backup = renderer->logical_h;
  1054. }
  1055. renderer->target = texture;
  1056. if (renderer->SetRenderTarget(renderer, texture) < 0) {
  1057. return -1;
  1058. }
  1059. if (texture) {
  1060. renderer->viewport.x = 0;
  1061. renderer->viewport.y = 0;
  1062. renderer->viewport.w = texture->w;
  1063. renderer->viewport.h = texture->h;
  1064. SDL_zero(renderer->clip_rect);
  1065. renderer->clipping_enabled = SDL_FALSE;
  1066. renderer->scale.x = 1.0f;
  1067. renderer->scale.y = 1.0f;
  1068. renderer->logical_w = texture->w;
  1069. renderer->logical_h = texture->h;
  1070. } else {
  1071. renderer->viewport = renderer->viewport_backup;
  1072. renderer->clip_rect = renderer->clip_rect_backup;
  1073. renderer->clipping_enabled = renderer->clipping_enabled_backup;
  1074. renderer->scale = renderer->scale_backup;
  1075. renderer->logical_w = renderer->logical_w_backup;
  1076. renderer->logical_h = renderer->logical_h_backup;
  1077. }
  1078. if (renderer->UpdateViewport(renderer) < 0) {
  1079. return -1;
  1080. }
  1081. if (renderer->UpdateClipRect(renderer) < 0) {
  1082. return -1;
  1083. }
  1084. /* All set! */
  1085. return 0;
  1086. }
  1087. SDL_Texture *
  1088. SDL_GetRenderTarget(SDL_Renderer *renderer)
  1089. {
  1090. return renderer->target;
  1091. }
  1092. static int
  1093. UpdateLogicalSize(SDL_Renderer *renderer)
  1094. {
  1095. int w = 1, h = 1;
  1096. float want_aspect;
  1097. float real_aspect;
  1098. float scale;
  1099. SDL_Rect viewport;
  1100. /* 0 is for letterbox, 1 is for overscan */
  1101. int scale_policy = 0;
  1102. const char *hint;
  1103. if (!renderer->logical_w || !renderer->logical_h) {
  1104. return 0;
  1105. }
  1106. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  1107. return -1;
  1108. }
  1109. hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
  1110. if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
  1111. SDL_bool overscan_supported = SDL_TRUE;
  1112. /* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
  1113. which the overscan implementation relies on.
  1114. */
  1115. if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
  1116. overscan_supported = SDL_FALSE;
  1117. }
  1118. if (overscan_supported) {
  1119. scale_policy = 1;
  1120. }
  1121. }
  1122. want_aspect = (float)renderer->logical_w / renderer->logical_h;
  1123. real_aspect = (float)w / h;
  1124. /* Clear the scale because we're setting viewport in output coordinates */
  1125. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  1126. if (renderer->integer_scale) {
  1127. if (want_aspect > real_aspect) {
  1128. scale = (float)(w / renderer->logical_w);
  1129. } else {
  1130. scale = (float)(h / renderer->logical_h);
  1131. }
  1132. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1133. viewport.x = (w - viewport.w) / 2;
  1134. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1135. viewport.y = (h - viewport.h) / 2;
  1136. SDL_RenderSetViewport(renderer, &viewport);
  1137. } else if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
  1138. /* The aspect ratios are the same, just scale appropriately */
  1139. scale = (float)w / renderer->logical_w;
  1140. SDL_RenderSetViewport(renderer, NULL);
  1141. } else if (want_aspect > real_aspect) {
  1142. if (scale_policy == 1) {
  1143. /* We want a wider aspect ratio than is available -
  1144. zoom so logical height matches the real height
  1145. and the width will grow off the screen
  1146. */
  1147. scale = (float)h / renderer->logical_h;
  1148. viewport.y = 0;
  1149. viewport.h = h;
  1150. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1151. viewport.x = (w - viewport.w) / 2;
  1152. SDL_RenderSetViewport(renderer, &viewport);
  1153. } else {
  1154. /* We want a wider aspect ratio than is available - letterbox it */
  1155. scale = (float)w / renderer->logical_w;
  1156. viewport.x = 0;
  1157. viewport.w = w;
  1158. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1159. viewport.y = (h - viewport.h) / 2;
  1160. SDL_RenderSetViewport(renderer, &viewport);
  1161. }
  1162. } else {
  1163. if (scale_policy == 1) {
  1164. /* We want a narrower aspect ratio than is available -
  1165. zoom so logical width matches the real width
  1166. and the height will grow off the screen
  1167. */
  1168. scale = (float)w / renderer->logical_w;
  1169. viewport.x = 0;
  1170. viewport.w = w;
  1171. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1172. viewport.y = (h - viewport.h) / 2;
  1173. SDL_RenderSetViewport(renderer, &viewport);
  1174. } else {
  1175. /* We want a narrower aspect ratio than is available - use side-bars */
  1176. scale = (float)h / renderer->logical_h;
  1177. viewport.y = 0;
  1178. viewport.h = h;
  1179. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1180. viewport.x = (w - viewport.w) / 2;
  1181. SDL_RenderSetViewport(renderer, &viewport);
  1182. }
  1183. }
  1184. /* Set the new scale */
  1185. SDL_RenderSetScale(renderer, scale, scale);
  1186. return 0;
  1187. }
  1188. int
  1189. SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
  1190. {
  1191. CHECK_RENDERER_MAGIC(renderer, -1);
  1192. if (!w || !h) {
  1193. /* Clear any previous logical resolution */
  1194. renderer->logical_w = 0;
  1195. renderer->logical_h = 0;
  1196. SDL_RenderSetViewport(renderer, NULL);
  1197. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  1198. return 0;
  1199. }
  1200. renderer->logical_w = w;
  1201. renderer->logical_h = h;
  1202. return UpdateLogicalSize(renderer);
  1203. }
  1204. void
  1205. SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
  1206. {
  1207. CHECK_RENDERER_MAGIC(renderer, );
  1208. if (w) {
  1209. *w = renderer->logical_w;
  1210. }
  1211. if (h) {
  1212. *h = renderer->logical_h;
  1213. }
  1214. }
  1215. int
  1216. SDL_RenderSetIntegerScale(SDL_Renderer * renderer, SDL_bool enable)
  1217. {
  1218. CHECK_RENDERER_MAGIC(renderer, -1);
  1219. renderer->integer_scale = enable;
  1220. return UpdateLogicalSize(renderer);
  1221. }
  1222. SDL_bool
  1223. SDLCALL SDL_RenderGetIntegerScale(SDL_Renderer * renderer)
  1224. {
  1225. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE);
  1226. return renderer->integer_scale;
  1227. }
  1228. int
  1229. SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
  1230. {
  1231. CHECK_RENDERER_MAGIC(renderer, -1);
  1232. if (rect) {
  1233. renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1234. renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1235. renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1236. renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1237. } else {
  1238. renderer->viewport.x = 0;
  1239. renderer->viewport.y = 0;
  1240. if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
  1241. return -1;
  1242. }
  1243. }
  1244. return renderer->UpdateViewport(renderer);
  1245. }
  1246. void
  1247. SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
  1248. {
  1249. CHECK_RENDERER_MAGIC(renderer, );
  1250. if (rect) {
  1251. rect->x = (int)(renderer->viewport.x / renderer->scale.x);
  1252. rect->y = (int)(renderer->viewport.y / renderer->scale.y);
  1253. rect->w = (int)(renderer->viewport.w / renderer->scale.x);
  1254. rect->h = (int)(renderer->viewport.h / renderer->scale.y);
  1255. }
  1256. }
  1257. int
  1258. SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1259. {
  1260. CHECK_RENDERER_MAGIC(renderer, -1)
  1261. if (rect) {
  1262. renderer->clipping_enabled = SDL_TRUE;
  1263. renderer->clip_rect.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1264. renderer->clip_rect.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1265. renderer->clip_rect.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1266. renderer->clip_rect.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1267. } else {
  1268. renderer->clipping_enabled = SDL_FALSE;
  1269. SDL_zero(renderer->clip_rect);
  1270. }
  1271. return renderer->UpdateClipRect(renderer);
  1272. }
  1273. void
  1274. SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
  1275. {
  1276. CHECK_RENDERER_MAGIC(renderer, )
  1277. if (rect) {
  1278. rect->x = (int)(renderer->clip_rect.x / renderer->scale.x);
  1279. rect->y = (int)(renderer->clip_rect.y / renderer->scale.y);
  1280. rect->w = (int)(renderer->clip_rect.w / renderer->scale.x);
  1281. rect->h = (int)(renderer->clip_rect.h / renderer->scale.y);
  1282. }
  1283. }
  1284. SDL_bool
  1285. SDL_RenderIsClipEnabled(SDL_Renderer * renderer)
  1286. {
  1287. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE)
  1288. return renderer->clipping_enabled;
  1289. }
  1290. int
  1291. SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
  1292. {
  1293. CHECK_RENDERER_MAGIC(renderer, -1);
  1294. renderer->scale.x = scaleX;
  1295. renderer->scale.y = scaleY;
  1296. return 0;
  1297. }
  1298. void
  1299. SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
  1300. {
  1301. CHECK_RENDERER_MAGIC(renderer, );
  1302. if (scaleX) {
  1303. *scaleX = renderer->scale.x;
  1304. }
  1305. if (scaleY) {
  1306. *scaleY = renderer->scale.y;
  1307. }
  1308. }
  1309. int
  1310. SDL_SetRenderDrawColor(SDL_Renderer * renderer,
  1311. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1312. {
  1313. CHECK_RENDERER_MAGIC(renderer, -1);
  1314. renderer->r = r;
  1315. renderer->g = g;
  1316. renderer->b = b;
  1317. renderer->a = a;
  1318. return 0;
  1319. }
  1320. int
  1321. SDL_GetRenderDrawColor(SDL_Renderer * renderer,
  1322. Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  1323. {
  1324. CHECK_RENDERER_MAGIC(renderer, -1);
  1325. if (r) {
  1326. *r = renderer->r;
  1327. }
  1328. if (g) {
  1329. *g = renderer->g;
  1330. }
  1331. if (b) {
  1332. *b = renderer->b;
  1333. }
  1334. if (a) {
  1335. *a = renderer->a;
  1336. }
  1337. return 0;
  1338. }
  1339. int
  1340. SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  1341. {
  1342. CHECK_RENDERER_MAGIC(renderer, -1);
  1343. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1344. return SDL_Unsupported();
  1345. }
  1346. renderer->blendMode = blendMode;
  1347. return 0;
  1348. }
  1349. int
  1350. SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
  1351. {
  1352. CHECK_RENDERER_MAGIC(renderer, -1);
  1353. *blendMode = renderer->blendMode;
  1354. return 0;
  1355. }
  1356. int
  1357. SDL_RenderClear(SDL_Renderer * renderer)
  1358. {
  1359. CHECK_RENDERER_MAGIC(renderer, -1);
  1360. /* Don't draw while we're hidden */
  1361. if (renderer->hidden) {
  1362. return 0;
  1363. }
  1364. return renderer->RenderClear(renderer);
  1365. }
  1366. int
  1367. SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
  1368. {
  1369. SDL_Point point;
  1370. point.x = x;
  1371. point.y = y;
  1372. return SDL_RenderDrawPoints(renderer, &point, 1);
  1373. }
  1374. static int
  1375. RenderDrawPointsWithRects(SDL_Renderer * renderer,
  1376. const SDL_Point * points, int count)
  1377. {
  1378. SDL_FRect *frects;
  1379. int i;
  1380. int status;
  1381. frects = SDL_stack_alloc(SDL_FRect, count);
  1382. if (!frects) {
  1383. return SDL_OutOfMemory();
  1384. }
  1385. for (i = 0; i < count; ++i) {
  1386. frects[i].x = points[i].x * renderer->scale.x;
  1387. frects[i].y = points[i].y * renderer->scale.y;
  1388. frects[i].w = renderer->scale.x;
  1389. frects[i].h = renderer->scale.y;
  1390. }
  1391. status = renderer->RenderFillRects(renderer, frects, count);
  1392. SDL_stack_free(frects);
  1393. return status;
  1394. }
  1395. int
  1396. SDL_RenderDrawPoints(SDL_Renderer * renderer,
  1397. const SDL_Point * points, int count)
  1398. {
  1399. SDL_FPoint *fpoints;
  1400. int i;
  1401. int status;
  1402. CHECK_RENDERER_MAGIC(renderer, -1);
  1403. if (!points) {
  1404. return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  1405. }
  1406. if (count < 1) {
  1407. return 0;
  1408. }
  1409. /* Don't draw while we're hidden */
  1410. if (renderer->hidden) {
  1411. return 0;
  1412. }
  1413. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  1414. return RenderDrawPointsWithRects(renderer, points, count);
  1415. }
  1416. fpoints = SDL_stack_alloc(SDL_FPoint, count);
  1417. if (!fpoints) {
  1418. return SDL_OutOfMemory();
  1419. }
  1420. for (i = 0; i < count; ++i) {
  1421. fpoints[i].x = points[i].x * renderer->scale.x;
  1422. fpoints[i].y = points[i].y * renderer->scale.y;
  1423. }
  1424. status = renderer->RenderDrawPoints(renderer, fpoints, count);
  1425. SDL_stack_free(fpoints);
  1426. return status;
  1427. }
  1428. int
  1429. SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
  1430. {
  1431. SDL_Point points[2];
  1432. points[0].x = x1;
  1433. points[0].y = y1;
  1434. points[1].x = x2;
  1435. points[1].y = y2;
  1436. return SDL_RenderDrawLines(renderer, points, 2);
  1437. }
  1438. static int
  1439. RenderDrawLinesWithRects(SDL_Renderer * renderer,
  1440. const SDL_Point * points, int count)
  1441. {
  1442. SDL_FRect *frect;
  1443. SDL_FRect *frects;
  1444. SDL_FPoint fpoints[2];
  1445. int i, nrects;
  1446. int status;
  1447. frects = SDL_stack_alloc(SDL_FRect, count-1);
  1448. if (!frects) {
  1449. return SDL_OutOfMemory();
  1450. }
  1451. status = 0;
  1452. nrects = 0;
  1453. for (i = 0; i < count-1; ++i) {
  1454. if (points[i].x == points[i+1].x) {
  1455. int minY = SDL_min(points[i].y, points[i+1].y);
  1456. int maxY = SDL_max(points[i].y, points[i+1].y);
  1457. frect = &frects[nrects++];
  1458. frect->x = points[i].x * renderer->scale.x;
  1459. frect->y = minY * renderer->scale.y;
  1460. frect->w = renderer->scale.x;
  1461. frect->h = (maxY - minY + 1) * renderer->scale.y;
  1462. } else if (points[i].y == points[i+1].y) {
  1463. int minX = SDL_min(points[i].x, points[i+1].x);
  1464. int maxX = SDL_max(points[i].x, points[i+1].x);
  1465. frect = &frects[nrects++];
  1466. frect->x = minX * renderer->scale.x;
  1467. frect->y = points[i].y * renderer->scale.y;
  1468. frect->w = (maxX - minX + 1) * renderer->scale.x;
  1469. frect->h = renderer->scale.y;
  1470. } else {
  1471. /* FIXME: We can't use a rect for this line... */
  1472. fpoints[0].x = points[i].x * renderer->scale.x;
  1473. fpoints[0].y = points[i].y * renderer->scale.y;
  1474. fpoints[1].x = points[i+1].x * renderer->scale.x;
  1475. fpoints[1].y = points[i+1].y * renderer->scale.y;
  1476. status += renderer->RenderDrawLines(renderer, fpoints, 2);
  1477. }
  1478. }
  1479. status += renderer->RenderFillRects(renderer, frects, nrects);
  1480. SDL_stack_free(frects);
  1481. if (status < 0) {
  1482. status = -1;
  1483. }
  1484. return status;
  1485. }
  1486. int
  1487. SDL_RenderDrawLines(SDL_Renderer * renderer,
  1488. const SDL_Point * points, int count)
  1489. {
  1490. SDL_FPoint *fpoints;
  1491. int i;
  1492. int status;
  1493. CHECK_RENDERER_MAGIC(renderer, -1);
  1494. if (!points) {
  1495. return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  1496. }
  1497. if (count < 2) {
  1498. return 0;
  1499. }
  1500. /* Don't draw while we're hidden */
  1501. if (renderer->hidden) {
  1502. return 0;
  1503. }
  1504. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  1505. return RenderDrawLinesWithRects(renderer, points, count);
  1506. }
  1507. fpoints = SDL_stack_alloc(SDL_FPoint, count);
  1508. if (!fpoints) {
  1509. return SDL_OutOfMemory();
  1510. }
  1511. for (i = 0; i < count; ++i) {
  1512. fpoints[i].x = points[i].x * renderer->scale.x;
  1513. fpoints[i].y = points[i].y * renderer->scale.y;
  1514. }
  1515. status = renderer->RenderDrawLines(renderer, fpoints, count);
  1516. SDL_stack_free(fpoints);
  1517. return status;
  1518. }
  1519. int
  1520. SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1521. {
  1522. SDL_Rect full_rect;
  1523. SDL_Point points[5];
  1524. CHECK_RENDERER_MAGIC(renderer, -1);
  1525. /* If 'rect' == NULL, then outline the whole surface */
  1526. if (!rect) {
  1527. SDL_RenderGetViewport(renderer, &full_rect);
  1528. full_rect.x = 0;
  1529. full_rect.y = 0;
  1530. rect = &full_rect;
  1531. }
  1532. points[0].x = rect->x;
  1533. points[0].y = rect->y;
  1534. points[1].x = rect->x+rect->w-1;
  1535. points[1].y = rect->y;
  1536. points[2].x = rect->x+rect->w-1;
  1537. points[2].y = rect->y+rect->h-1;
  1538. points[3].x = rect->x;
  1539. points[3].y = rect->y+rect->h-1;
  1540. points[4].x = rect->x;
  1541. points[4].y = rect->y;
  1542. return SDL_RenderDrawLines(renderer, points, 5);
  1543. }
  1544. int
  1545. SDL_RenderDrawRects(SDL_Renderer * renderer,
  1546. const SDL_Rect * rects, int count)
  1547. {
  1548. int i;
  1549. CHECK_RENDERER_MAGIC(renderer, -1);
  1550. if (!rects) {
  1551. return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  1552. }
  1553. if (count < 1) {
  1554. return 0;
  1555. }
  1556. /* Don't draw while we're hidden */
  1557. if (renderer->hidden) {
  1558. return 0;
  1559. }
  1560. for (i = 0; i < count; ++i) {
  1561. if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
  1562. return -1;
  1563. }
  1564. }
  1565. return 0;
  1566. }
  1567. int
  1568. SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1569. {
  1570. SDL_Rect full_rect = { 0, 0, 0, 0 };
  1571. CHECK_RENDERER_MAGIC(renderer, -1);
  1572. /* If 'rect' == NULL, then outline the whole surface */
  1573. if (!rect) {
  1574. SDL_RenderGetViewport(renderer, &full_rect);
  1575. full_rect.x = 0;
  1576. full_rect.y = 0;
  1577. rect = &full_rect;
  1578. }
  1579. return SDL_RenderFillRects(renderer, rect, 1);
  1580. }
  1581. int
  1582. SDL_RenderFillRects(SDL_Renderer * renderer,
  1583. const SDL_Rect * rects, int count)
  1584. {
  1585. SDL_FRect *frects;
  1586. int i;
  1587. int status;
  1588. CHECK_RENDERER_MAGIC(renderer, -1);
  1589. if (!rects) {
  1590. return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  1591. }
  1592. if (count < 1) {
  1593. return 0;
  1594. }
  1595. /* Don't draw while we're hidden */
  1596. if (renderer->hidden) {
  1597. return 0;
  1598. }
  1599. frects = SDL_stack_alloc(SDL_FRect, count);
  1600. if (!frects) {
  1601. return SDL_OutOfMemory();
  1602. }
  1603. for (i = 0; i < count; ++i) {
  1604. frects[i].x = rects[i].x * renderer->scale.x;
  1605. frects[i].y = rects[i].y * renderer->scale.y;
  1606. frects[i].w = rects[i].w * renderer->scale.x;
  1607. frects[i].h = rects[i].h * renderer->scale.y;
  1608. }
  1609. status = renderer->RenderFillRects(renderer, frects, count);
  1610. SDL_stack_free(frects);
  1611. return status;
  1612. }
  1613. int
  1614. SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  1615. const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  1616. {
  1617. SDL_Rect real_srcrect = { 0, 0, 0, 0 };
  1618. SDL_Rect real_dstrect = { 0, 0, 0, 0 };
  1619. SDL_FRect frect;
  1620. CHECK_RENDERER_MAGIC(renderer, -1);
  1621. CHECK_TEXTURE_MAGIC(texture, -1);
  1622. if (renderer != texture->renderer) {
  1623. return SDL_SetError("Texture was not created with this renderer");
  1624. }
  1625. /* Don't draw while we're hidden */
  1626. if (renderer->hidden) {
  1627. return 0;
  1628. }
  1629. real_srcrect.x = 0;
  1630. real_srcrect.y = 0;
  1631. real_srcrect.w = texture->w;
  1632. real_srcrect.h = texture->h;
  1633. if (srcrect) {
  1634. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1635. return 0;
  1636. }
  1637. }
  1638. SDL_RenderGetViewport(renderer, &real_dstrect);
  1639. real_dstrect.x = 0;
  1640. real_dstrect.y = 0;
  1641. if (dstrect) {
  1642. if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
  1643. return 0;
  1644. }
  1645. real_dstrect = *dstrect;
  1646. }
  1647. if (texture->native) {
  1648. texture = texture->native;
  1649. }
  1650. frect.x = real_dstrect.x * renderer->scale.x;
  1651. frect.y = real_dstrect.y * renderer->scale.y;
  1652. frect.w = real_dstrect.w * renderer->scale.x;
  1653. frect.h = real_dstrect.h * renderer->scale.y;
  1654. return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
  1655. }
  1656. int
  1657. SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  1658. const SDL_Rect * srcrect, const SDL_Rect * dstrect,
  1659. const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
  1660. {
  1661. SDL_Rect real_srcrect = { 0, 0, 0, 0 };
  1662. SDL_Rect real_dstrect = { 0, 0, 0, 0 };
  1663. SDL_Point real_center;
  1664. SDL_FRect frect;
  1665. SDL_FPoint fcenter;
  1666. if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */
  1667. return SDL_RenderCopy(renderer, texture, srcrect, dstrect);
  1668. }
  1669. CHECK_RENDERER_MAGIC(renderer, -1);
  1670. CHECK_TEXTURE_MAGIC(texture, -1);
  1671. if (renderer != texture->renderer) {
  1672. return SDL_SetError("Texture was not created with this renderer");
  1673. }
  1674. if (!renderer->RenderCopyEx) {
  1675. return SDL_SetError("Renderer does not support RenderCopyEx");
  1676. }
  1677. /* Don't draw while we're hidden */
  1678. if (renderer->hidden) {
  1679. return 0;
  1680. }
  1681. real_srcrect.x = 0;
  1682. real_srcrect.y = 0;
  1683. real_srcrect.w = texture->w;
  1684. real_srcrect.h = texture->h;
  1685. if (srcrect) {
  1686. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1687. return 0;
  1688. }
  1689. }
  1690. /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
  1691. if (dstrect) {
  1692. real_dstrect = *dstrect;
  1693. } else {
  1694. SDL_RenderGetViewport(renderer, &real_dstrect);
  1695. real_dstrect.x = 0;
  1696. real_dstrect.y = 0;
  1697. }
  1698. if (texture->native) {
  1699. texture = texture->native;
  1700. }
  1701. if (center) {
  1702. real_center = *center;
  1703. } else {
  1704. real_center.x = real_dstrect.w/2;
  1705. real_center.y = real_dstrect.h/2;
  1706. }
  1707. frect.x = real_dstrect.x * renderer->scale.x;
  1708. frect.y = real_dstrect.y * renderer->scale.y;
  1709. frect.w = real_dstrect.w * renderer->scale.x;
  1710. frect.h = real_dstrect.h * renderer->scale.y;
  1711. fcenter.x = real_center.x * renderer->scale.x;
  1712. fcenter.y = real_center.y * renderer->scale.y;
  1713. return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
  1714. }
  1715. int
  1716. SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1717. Uint32 format, void * pixels, int pitch)
  1718. {
  1719. SDL_Rect real_rect;
  1720. CHECK_RENDERER_MAGIC(renderer, -1);
  1721. if (!renderer->RenderReadPixels) {
  1722. return SDL_Unsupported();
  1723. }
  1724. if (!format) {
  1725. format = SDL_GetWindowPixelFormat(renderer->window);
  1726. }
  1727. real_rect.x = renderer->viewport.x;
  1728. real_rect.y = renderer->viewport.y;
  1729. real_rect.w = renderer->viewport.w;
  1730. real_rect.h = renderer->viewport.h;
  1731. if (rect) {
  1732. if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1733. return 0;
  1734. }
  1735. if (real_rect.y > rect->y) {
  1736. pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  1737. }
  1738. if (real_rect.x > rect->x) {
  1739. int bpp = SDL_BYTESPERPIXEL(format);
  1740. pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  1741. }
  1742. }
  1743. return renderer->RenderReadPixels(renderer, &real_rect,
  1744. format, pixels, pitch);
  1745. }
  1746. void
  1747. SDL_RenderPresent(SDL_Renderer * renderer)
  1748. {
  1749. CHECK_RENDERER_MAGIC(renderer, );
  1750. /* Don't draw while we're hidden */
  1751. if (renderer->hidden) {
  1752. return;
  1753. }
  1754. renderer->RenderPresent(renderer);
  1755. }
  1756. void
  1757. SDL_DestroyTexture(SDL_Texture * texture)
  1758. {
  1759. SDL_Renderer *renderer;
  1760. CHECK_TEXTURE_MAGIC(texture, );
  1761. renderer = texture->renderer;
  1762. if (texture == renderer->target) {
  1763. SDL_SetRenderTarget(renderer, NULL);
  1764. }
  1765. texture->magic = NULL;
  1766. if (texture->next) {
  1767. texture->next->prev = texture->prev;
  1768. }
  1769. if (texture->prev) {
  1770. texture->prev->next = texture->next;
  1771. } else {
  1772. renderer->textures = texture->next;
  1773. }
  1774. if (texture->native) {
  1775. SDL_DestroyTexture(texture->native);
  1776. }
  1777. if (texture->yuv) {
  1778. SDL_SW_DestroyYUVTexture(texture->yuv);
  1779. }
  1780. SDL_free(texture->pixels);
  1781. renderer->DestroyTexture(renderer, texture);
  1782. SDL_free(texture);
  1783. }
  1784. void
  1785. SDL_DestroyRenderer(SDL_Renderer * renderer)
  1786. {
  1787. CHECK_RENDERER_MAGIC(renderer, );
  1788. SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
  1789. /* Free existing textures for this renderer */
  1790. while (renderer->textures) {
  1791. SDL_Texture *tex = renderer->textures; (void) tex;
  1792. SDL_DestroyTexture(renderer->textures);
  1793. SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
  1794. }
  1795. if (renderer->window) {
  1796. SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
  1797. }
  1798. /* It's no longer magical... */
  1799. renderer->magic = NULL;
  1800. /* Free the renderer instance */
  1801. renderer->DestroyRenderer(renderer);
  1802. }
  1803. int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
  1804. {
  1805. SDL_Renderer *renderer;
  1806. CHECK_TEXTURE_MAGIC(texture, -1);
  1807. renderer = texture->renderer;
  1808. if (texture->native) {
  1809. return SDL_GL_BindTexture(texture->native, texw, texh);
  1810. } else if (renderer && renderer->GL_BindTexture) {
  1811. return renderer->GL_BindTexture(renderer, texture, texw, texh);
  1812. } else {
  1813. return SDL_Unsupported();
  1814. }
  1815. }
  1816. int SDL_GL_UnbindTexture(SDL_Texture *texture)
  1817. {
  1818. SDL_Renderer *renderer;
  1819. CHECK_TEXTURE_MAGIC(texture, -1);
  1820. renderer = texture->renderer;
  1821. if (texture->native) {
  1822. return SDL_GL_UnbindTexture(texture->native);
  1823. } else if (renderer && renderer->GL_UnbindTexture) {
  1824. return renderer->GL_UnbindTexture(renderer, texture);
  1825. }
  1826. return SDL_Unsupported();
  1827. }
  1828. void *
  1829. SDL_RenderGetMetalLayer(SDL_Renderer * renderer)
  1830. {
  1831. CHECK_RENDERER_MAGIC(renderer, NULL);
  1832. if (renderer->GetMetalLayer) {
  1833. return renderer->GetMetalLayer(renderer);
  1834. }
  1835. return NULL;
  1836. }
  1837. void *
  1838. SDL_RenderGetMetalCommandEncoder(SDL_Renderer * renderer)
  1839. {
  1840. CHECK_RENDERER_MAGIC(renderer, NULL);
  1841. if (renderer->GetMetalCommandEncoder) {
  1842. return renderer->GetMetalCommandEncoder(renderer);
  1843. }
  1844. return NULL;
  1845. }
  1846. static SDL_BlendMode
  1847. SDL_GetShortBlendMode(SDL_BlendMode blendMode)
  1848. {
  1849. if (blendMode == SDL_BLENDMODE_NONE_FULL) {
  1850. return SDL_BLENDMODE_NONE;
  1851. }
  1852. if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
  1853. return SDL_BLENDMODE_BLEND;
  1854. }
  1855. if (blendMode == SDL_BLENDMODE_ADD_FULL) {
  1856. return SDL_BLENDMODE_ADD;
  1857. }
  1858. if (blendMode == SDL_BLENDMODE_MOD_FULL) {
  1859. return SDL_BLENDMODE_MOD;
  1860. }
  1861. return blendMode;
  1862. }
  1863. static SDL_BlendMode
  1864. SDL_GetLongBlendMode(SDL_BlendMode blendMode)
  1865. {
  1866. if (blendMode == SDL_BLENDMODE_NONE) {
  1867. return SDL_BLENDMODE_NONE_FULL;
  1868. }
  1869. if (blendMode == SDL_BLENDMODE_BLEND) {
  1870. return SDL_BLENDMODE_BLEND_FULL;
  1871. }
  1872. if (blendMode == SDL_BLENDMODE_ADD) {
  1873. return SDL_BLENDMODE_ADD_FULL;
  1874. }
  1875. if (blendMode == SDL_BLENDMODE_MOD) {
  1876. return SDL_BLENDMODE_MOD_FULL;
  1877. }
  1878. return blendMode;
  1879. }
  1880. SDL_BlendMode
  1881. SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
  1882. SDL_BlendOperation colorOperation,
  1883. SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
  1884. SDL_BlendOperation alphaOperation)
  1885. {
  1886. SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
  1887. srcAlphaFactor, dstAlphaFactor, alphaOperation);
  1888. return SDL_GetShortBlendMode(blendMode);
  1889. }
  1890. SDL_BlendFactor
  1891. SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
  1892. {
  1893. blendMode = SDL_GetLongBlendMode(blendMode);
  1894. return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
  1895. }
  1896. SDL_BlendFactor
  1897. SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
  1898. {
  1899. blendMode = SDL_GetLongBlendMode(blendMode);
  1900. return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
  1901. }
  1902. SDL_BlendOperation
  1903. SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
  1904. {
  1905. blendMode = SDL_GetLongBlendMode(blendMode);
  1906. return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
  1907. }
  1908. SDL_BlendFactor
  1909. SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
  1910. {
  1911. blendMode = SDL_GetLongBlendMode(blendMode);
  1912. return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
  1913. }
  1914. SDL_BlendFactor
  1915. SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
  1916. {
  1917. blendMode = SDL_GetLongBlendMode(blendMode);
  1918. return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
  1919. }
  1920. SDL_BlendOperation
  1921. SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
  1922. {
  1923. blendMode = SDL_GetLongBlendMode(blendMode);
  1924. return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
  1925. }
  1926. /* vi: set ts=4 sw=4 expandtab: */