testautomation_render.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. /**
  2. * Original code: automated SDL platform test written by Edgar Simo "bobbens"
  3. * Extended and extensively updated by aschiffler at ferzkopp dot net
  4. */
  5. #include <SDL3/SDL.h>
  6. #include <SDL3/SDL_test.h>
  7. #include "testautomation_images.h"
  8. #include "testautomation_suites.h"
  9. /* ================= Test Case Implementation ================== */
  10. #define TESTRENDER_SCREEN_W 80
  11. #define TESTRENDER_SCREEN_H 60
  12. #define RENDER_COMPARE_FORMAT SDL_PIXELFORMAT_ARGB8888
  13. #define RENDER_COLOR_CLEAR 0xFF000000
  14. #define RENDER_COLOR_GREEN 0xFF00FF00
  15. #define ALLOWABLE_ERROR_OPAQUE 0
  16. #define ALLOWABLE_ERROR_BLENDED 0
  17. #define CHECK_FUNC(FUNC, PARAMS) \
  18. { \
  19. bool result = FUNC PARAMS; \
  20. if (!result) { \
  21. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, SDL_GetError()); \
  22. } \
  23. }
  24. /* Test window and renderer */
  25. static SDL_Window *window = NULL;
  26. static SDL_Renderer *renderer = NULL;
  27. /* Prototypes for helper functions */
  28. static int clearScreen(void);
  29. static void compare(SDL_Surface *reference, int allowable_error);
  30. static void compare2x(SDL_Surface *reference, int allowable_error);
  31. static SDL_Texture *loadTestFace(void);
  32. static bool isSupported(int code);
  33. static bool hasDrawColor(void);
  34. /**
  35. * Create software renderer for tests
  36. */
  37. static void SDLCALL InitCreateRenderer(void **arg)
  38. {
  39. int width = 320, height = 240;
  40. const char *renderer_name = NULL;
  41. renderer = NULL;
  42. window = SDL_CreateWindow("render_testCreateRenderer", width, height, 0);
  43. SDLTest_AssertPass("SDL_CreateWindow()");
  44. SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
  45. if (window == NULL) {
  46. return;
  47. }
  48. renderer = SDL_CreateRenderer(window, renderer_name);
  49. SDLTest_AssertPass("SDL_CreateRenderer()");
  50. SDLTest_AssertCheck(renderer != NULL, "Check SDL_CreateRenderer result: %s", renderer != NULL ? "success" : SDL_GetError());
  51. if (renderer == NULL) {
  52. SDL_DestroyWindow(window);
  53. return;
  54. }
  55. }
  56. /**
  57. * Destroy renderer for tests
  58. */
  59. static void SDLCALL CleanupDestroyRenderer(void *arg)
  60. {
  61. if (renderer) {
  62. SDL_DestroyRenderer(renderer);
  63. renderer = NULL;
  64. SDLTest_AssertPass("SDL_DestroyRenderer()");
  65. }
  66. if (window) {
  67. SDL_DestroyWindow(window);
  68. window = NULL;
  69. SDLTest_AssertPass("SDL_DestroyWindow");
  70. }
  71. }
  72. /**
  73. * Tests call to SDL_GetNumRenderDrivers
  74. *
  75. * \sa SDL_GetNumRenderDrivers
  76. */
  77. static int SDLCALL render_testGetNumRenderDrivers(void *arg)
  78. {
  79. int n;
  80. n = SDL_GetNumRenderDrivers();
  81. SDLTest_AssertCheck(n >= 1, "Number of renderers >= 1, reported as %i", n);
  82. return TEST_COMPLETED;
  83. }
  84. /**
  85. * Tests the SDL primitives for rendering.
  86. *
  87. * \sa SDL_SetRenderDrawColor
  88. * \sa SDL_RenderFillRect
  89. * \sa SDL_RenderLine
  90. *
  91. */
  92. static int SDLCALL render_testPrimitives(void *arg)
  93. {
  94. int ret;
  95. int x, y;
  96. SDL_FRect rect;
  97. SDL_Surface *referenceSurface = NULL;
  98. int checkFailCount1;
  99. int checkFailCount2;
  100. /* Clear surface. */
  101. clearScreen();
  102. /* Need drawcolor or just skip test. */
  103. SDLTest_AssertCheck(hasDrawColor(), "hasDrawColor");
  104. /* Draw a rectangle. */
  105. rect.x = 40.0f;
  106. rect.y = 0.0f;
  107. rect.w = 40.0f;
  108. rect.h = 80.0f;
  109. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 13, 73, 200, SDL_ALPHA_OPAQUE))
  110. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  111. /* Draw a rectangle. */
  112. rect.x = 10.0f;
  113. rect.y = 10.0f;
  114. rect.w = 60.0f;
  115. rect.h = 40.0f;
  116. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 200, 0, 100, SDL_ALPHA_OPAQUE))
  117. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  118. /* Draw some points like so:
  119. * X.X.X.X..
  120. * .X.X.X.X.
  121. * X.X.X.X.. */
  122. checkFailCount1 = 0;
  123. checkFailCount2 = 0;
  124. for (y = 0; y < 3; y++) {
  125. for (x = y % 2; x < TESTRENDER_SCREEN_W; x += 2) {
  126. ret = SDL_SetRenderDrawColor(renderer, (Uint8)(x * y), (Uint8)(x * y / 2), (Uint8)(x * y / 3), SDL_ALPHA_OPAQUE);
  127. if (!ret) {
  128. checkFailCount1++;
  129. }
  130. ret = SDL_RenderPoint(renderer, (float)x, (float)y);
  131. if (!ret) {
  132. checkFailCount2++;
  133. }
  134. }
  135. }
  136. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  137. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", checkFailCount2);
  138. /* Draw some lines. */
  139. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  140. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 30.0f, TESTRENDER_SCREEN_W, 30.0f))
  141. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 55, 55, 5, SDL_ALPHA_OPAQUE))
  142. CHECK_FUNC(SDL_RenderLine, (renderer, 40.0f, 30.0f, 40.0f, 60.0f))
  143. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 5, 105, 105, SDL_ALPHA_OPAQUE))
  144. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 0.0f, 29.0f, 29.0f))
  145. CHECK_FUNC(SDL_RenderLine, (renderer, 29.0f, 30.0f, 0.0f, 59.0f))
  146. CHECK_FUNC(SDL_RenderLine, (renderer, 79.0f, 0.0f, 50.0f, 29.0f))
  147. CHECK_FUNC(SDL_RenderLine, (renderer, 79.0f, 59.0f, 50.0f, 30.0f))
  148. /* See if it's the same. */
  149. referenceSurface = SDLTest_ImagePrimitives();
  150. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  151. /* Make current */
  152. SDL_RenderPresent(renderer);
  153. /* Clean up. */
  154. SDL_DestroySurface(referenceSurface);
  155. referenceSurface = NULL;
  156. return TEST_COMPLETED;
  157. }
  158. /**
  159. * Tests the SDL primitives for rendering within a viewport.
  160. *
  161. * \sa SDL_SetRenderDrawColor
  162. * \sa SDL_RenderFillRect
  163. * \sa SDL_RenderLine
  164. *
  165. */
  166. static int SDLCALL render_testPrimitivesWithViewport(void *arg)
  167. {
  168. SDL_Rect viewport;
  169. SDL_Surface *surface;
  170. /* Clear surface. */
  171. clearScreen();
  172. viewport.x = 2;
  173. viewport.y = 2;
  174. viewport.w = 2;
  175. viewport.h = 2;
  176. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport));
  177. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 255, 255, 255, SDL_ALPHA_OPAQUE))
  178. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 0.0f, 1.0f, 1.0f));
  179. viewport.x = 3;
  180. viewport.y = 3;
  181. viewport.w = 1;
  182. viewport.h = 1;
  183. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport));
  184. surface = SDL_RenderReadPixels(renderer, NULL);
  185. if (surface) {
  186. Uint8 r, g, b, a;
  187. CHECK_FUNC(SDL_ReadSurfacePixel, (surface, 0, 0, &r, &g, &b, &a));
  188. SDLTest_AssertCheck(r == 0xFF && g == 0xFF && b == 0xFF && a == 0xFF, "Validate diagonal line drawing with viewport, expected 0xFFFFFFFF, got 0x%.2x%.2x%.2x%.2x", r, g, b, a);
  189. SDL_DestroySurface(surface);
  190. } else {
  191. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  192. }
  193. return TEST_COMPLETED;
  194. }
  195. /**
  196. * Tests some blitting routines.
  197. *
  198. * \sa SDL_RenderTexture
  199. * \sa SDL_DestroyTexture
  200. */
  201. static int SDLCALL render_testBlit(void *arg)
  202. {
  203. int ret;
  204. SDL_FRect rect;
  205. SDL_Texture *tface;
  206. SDL_Surface *referenceSurface = NULL;
  207. int i, j, ni, nj;
  208. int checkFailCount1;
  209. /* Clear surface. */
  210. clearScreen();
  211. /* Need drawcolor or just skip test. */
  212. SDLTest_AssertCheck(hasDrawColor(), "hasDrawColor)");
  213. /* Create face surface. */
  214. tface = loadTestFace();
  215. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  216. if (tface == NULL) {
  217. return TEST_ABORTED;
  218. }
  219. /* Constant values. */
  220. rect.w = (float)tface->w;
  221. rect.h = (float)tface->h;
  222. ni = TESTRENDER_SCREEN_W - tface->w;
  223. nj = TESTRENDER_SCREEN_H - tface->h;
  224. /* Loop blit. */
  225. checkFailCount1 = 0;
  226. for (j = 0; j <= nj; j += 4) {
  227. for (i = 0; i <= ni; i += 4) {
  228. /* Blitting. */
  229. rect.x = (float)i;
  230. rect.y = (float)j;
  231. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  232. if (!ret) {
  233. checkFailCount1++;
  234. }
  235. }
  236. }
  237. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount1);
  238. /* See if it's the same */
  239. referenceSurface = SDLTest_ImageBlit();
  240. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  241. /* Make current */
  242. SDL_RenderPresent(renderer);
  243. /* Clean up. */
  244. SDL_DestroyTexture(tface);
  245. SDL_DestroySurface(referenceSurface);
  246. referenceSurface = NULL;
  247. return TEST_COMPLETED;
  248. }
  249. /**
  250. * Tests tiled blitting routines.
  251. */
  252. static int SDLCALL render_testBlitTiled(void *arg)
  253. {
  254. int ret;
  255. SDL_FRect rect;
  256. SDL_Texture *tface;
  257. SDL_Surface *referenceSurface = NULL;
  258. SDL_Surface *referenceSurface2x = NULL;
  259. /* Create face surface. */
  260. tface = loadTestFace();
  261. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  262. if (tface == NULL) {
  263. return TEST_ABORTED;
  264. }
  265. SDL_SetTextureScaleMode(tface, SDL_SCALEMODE_NEAREST); /* So 2x scaling is pixel perfect */
  266. /* Tiled blit - 1.0 scale */
  267. {
  268. /* Clear surface. */
  269. clearScreen();
  270. /* Tiled blit. */
  271. rect.x = 0.0f;
  272. rect.y = 0.0f;
  273. rect.w = (float)TESTRENDER_SCREEN_W;
  274. rect.h = (float)TESTRENDER_SCREEN_H;
  275. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 1.0f, &rect);
  276. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  277. /* See if it's the same */
  278. referenceSurface = SDLTest_ImageBlitTiled();
  279. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  280. /* Make current */
  281. SDL_RenderPresent(renderer);
  282. }
  283. /* Tiled blit - 2.0 scale */
  284. {
  285. /* Clear surface. */
  286. clearScreen();
  287. /* Tiled blit. */
  288. rect.x = 0.0f;
  289. rect.y = 0.0f;
  290. rect.w = (float)TESTRENDER_SCREEN_W * 2;
  291. rect.h = (float)TESTRENDER_SCREEN_H * 2;
  292. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 2.0f, &rect);
  293. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  294. /* See if it's the same */
  295. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  296. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  297. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: 0, got: %i", ret);
  298. compare2x(referenceSurface2x, ALLOWABLE_ERROR_OPAQUE);
  299. /* Make current */
  300. SDL_RenderPresent(renderer);
  301. }
  302. /* Clean up. */
  303. SDL_DestroyTexture(tface);
  304. SDL_DestroySurface(referenceSurface);
  305. SDL_DestroySurface(referenceSurface2x);
  306. referenceSurface = NULL;
  307. return TEST_COMPLETED;
  308. }
  309. static const Uint8 COLOR_SEPARATION = 85;
  310. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  311. {
  312. SDL_Rect rect;
  313. // Upper left
  314. rect.x = 0;
  315. rect.y = 0;
  316. rect.w = left_width;
  317. rect.h = top_height;
  318. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  319. // Top
  320. rect.x = left_width;
  321. rect.y = 0;
  322. rect.w = surface->w - left_width - right_width;
  323. rect.h = top_height;
  324. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  325. // Upper right
  326. rect.x = surface->w - right_width;
  327. rect.y = 0;
  328. rect.w = right_width;
  329. rect.h = top_height;
  330. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  331. // Left
  332. rect.x = 0;
  333. rect.y = top_height;
  334. rect.w = left_width;
  335. rect.h = surface->h - top_height - bottom_height;
  336. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  337. // Center
  338. rect.x = left_width;
  339. rect.y = top_height;
  340. rect.w = surface->w - right_width - left_width;
  341. rect.h = surface->h - top_height - bottom_height;
  342. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  343. // Right
  344. rect.x = surface->w - right_width;
  345. rect.y = top_height;
  346. rect.w = right_width;
  347. rect.h = surface->h - top_height - bottom_height;
  348. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  349. // Lower left
  350. rect.x = 0;
  351. rect.y = surface->h - bottom_height;
  352. rect.w = left_width;
  353. rect.h = bottom_height;
  354. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  355. // Bottom
  356. rect.x = left_width;
  357. rect.y = surface->h - bottom_height;
  358. rect.w = surface->w - left_width - right_width;
  359. rect.h = bottom_height;
  360. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  361. // Lower right
  362. rect.x = surface->w - right_width;
  363. rect.y = surface->h - bottom_height;
  364. rect.w = right_width;
  365. rect.h = bottom_height;
  366. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  367. }
  368. /**
  369. * Tests 9-grid blitting.
  370. */
  371. static int SDLCALL render_testBlit9Grid(void *arg)
  372. {
  373. SDL_Surface *referenceSurface = NULL;
  374. SDL_Surface *source = NULL;
  375. SDL_Texture *texture;
  376. int x, y;
  377. SDL_FRect rect;
  378. int ret = 0;
  379. /* Create source surface */
  380. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  381. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  382. for (y = 0; y < 3; ++y) {
  383. for (x = 0; x < 3; ++x) {
  384. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  385. }
  386. }
  387. texture = SDL_CreateTextureFromSurface(renderer, source);
  388. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  389. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  390. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  391. /* 9-grid blit - 1.0 scale */
  392. {
  393. SDLTest_Log("9-grid blit - 1.0 scale");
  394. /* Create reference surface */
  395. SDL_DestroySurface(referenceSurface);
  396. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  397. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  398. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  399. /* Clear surface. */
  400. clearScreen();
  401. /* Tiled blit. */
  402. rect.x = 0.0f;
  403. rect.y = 0.0f;
  404. rect.w = (float)TESTRENDER_SCREEN_W;
  405. rect.h = (float)TESTRENDER_SCREEN_H;
  406. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, &rect);
  407. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  408. /* See if it's the same */
  409. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  410. /* Make current */
  411. SDL_RenderPresent(renderer);
  412. }
  413. /* 9-grid blit - 2.0 scale */
  414. {
  415. SDLTest_Log("9-grid blit - 2.0 scale");
  416. /* Create reference surface */
  417. SDL_DestroySurface(referenceSurface);
  418. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  419. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  420. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  421. /* Clear surface. */
  422. clearScreen();
  423. /* Tiled blit. */
  424. rect.x = 0.0f;
  425. rect.y = 0.0f;
  426. rect.w = (float)TESTRENDER_SCREEN_W;
  427. rect.h = (float)TESTRENDER_SCREEN_H;
  428. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, &rect);
  429. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  430. /* See if it's the same */
  431. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  432. /* Make current */
  433. SDL_RenderPresent(renderer);
  434. }
  435. /* Clean up. */
  436. SDL_DestroySurface(source);
  437. SDL_DestroyTexture(texture);
  438. /* Create complex source surface */
  439. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  440. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  441. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  442. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  443. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  444. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  445. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  446. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  447. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  448. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  449. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  450. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  451. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  452. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  453. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  454. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  455. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  456. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  457. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  458. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  459. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  460. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  461. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  462. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  463. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  464. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  465. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  466. texture = SDL_CreateTextureFromSurface(renderer, source);
  467. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  468. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  469. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  470. /* complex 9-grid blit - 1.0 scale */
  471. {
  472. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  473. /* Create reference surface */
  474. SDL_DestroySurface(referenceSurface);
  475. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  476. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  477. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  478. /* Clear surface. */
  479. clearScreen();
  480. /* Tiled blit. */
  481. rect.x = 0.0f;
  482. rect.y = 0.0f;
  483. rect.w = (float)TESTRENDER_SCREEN_W;
  484. rect.h = (float)TESTRENDER_SCREEN_H;
  485. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, &rect);
  486. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  487. /* See if it's the same */
  488. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  489. /* Make current */
  490. SDL_RenderPresent(renderer);
  491. }
  492. /* complex 9-grid blit - 2.0 scale */
  493. {
  494. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  495. /* Create reference surface */
  496. SDL_DestroySurface(referenceSurface);
  497. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  498. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  499. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  500. /* Clear surface. */
  501. clearScreen();
  502. /* Tiled blit. */
  503. rect.x = 0.0f;
  504. rect.y = 0.0f;
  505. rect.w = (float)TESTRENDER_SCREEN_W;
  506. rect.h = (float)TESTRENDER_SCREEN_H;
  507. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, &rect);
  508. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  509. /* See if it's the same */
  510. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  511. /* Make current */
  512. SDL_RenderPresent(renderer);
  513. }
  514. /* Clean up. */
  515. SDL_DestroySurface(referenceSurface);
  516. SDL_DestroySurface(source);
  517. SDL_DestroyTexture(texture);
  518. return TEST_COMPLETED;
  519. }
  520. /**
  521. * Tests tiled 9-grid blitting.
  522. */
  523. static int SDLCALL render_testBlit9GridTiled(void *arg)
  524. {
  525. SDL_Surface *referenceSurface = NULL;
  526. SDL_Surface *source = NULL;
  527. SDL_Texture *texture;
  528. int x, y;
  529. SDL_FRect rect;
  530. int ret = 0;
  531. /* Create source surface */
  532. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  533. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  534. for (y = 0; y < 3; ++y) {
  535. for (x = 0; x < 3; ++x) {
  536. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  537. }
  538. }
  539. texture = SDL_CreateTextureFromSurface(renderer, source);
  540. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  541. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  542. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  543. /* Tiled 9-grid blit - 1.0 scale */
  544. {
  545. SDLTest_Log("tiled 9-grid blit - 1.0 scale");
  546. /* Create reference surface */
  547. SDL_DestroySurface(referenceSurface);
  548. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  549. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  550. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  551. /* Clear surface. */
  552. clearScreen();
  553. /* Tiled blit. */
  554. rect.x = 0.0f;
  555. rect.y = 0.0f;
  556. rect.w = (float)TESTRENDER_SCREEN_W;
  557. rect.h = (float)TESTRENDER_SCREEN_H;
  558. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, &rect, 1.0f);
  559. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  560. /* See if it's the same */
  561. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  562. /* Make current */
  563. SDL_RenderPresent(renderer);
  564. }
  565. /* Tiled 9-grid blit - 2.0 scale */
  566. {
  567. SDLTest_Log("tiled 9-grid blit - 2.0 scale");
  568. /* Create reference surface */
  569. SDL_DestroySurface(referenceSurface);
  570. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  571. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  572. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  573. /* Clear surface. */
  574. clearScreen();
  575. /* Tiled blit. */
  576. rect.x = 0.0f;
  577. rect.y = 0.0f;
  578. rect.w = (float)TESTRENDER_SCREEN_W;
  579. rect.h = (float)TESTRENDER_SCREEN_H;
  580. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, &rect, 2.0f);
  581. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  582. /* See if it's the same */
  583. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  584. /* Make current */
  585. SDL_RenderPresent(renderer);
  586. }
  587. /* Clean up. */
  588. SDL_DestroySurface(source);
  589. SDL_DestroyTexture(texture);
  590. /* Create complex source surface */
  591. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  592. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  593. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  594. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  595. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  596. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  597. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  598. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  599. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  600. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  601. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  602. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  603. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  604. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  605. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  606. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  607. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  608. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  609. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  610. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  611. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  612. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  613. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  614. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  615. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  616. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  617. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  618. texture = SDL_CreateTextureFromSurface(renderer, source);
  619. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  620. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  621. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  622. /* complex tiled 9-grid blit - 1.0 scale */
  623. {
  624. SDLTest_Log("complex tiled 9-grid blit - 1.0 scale");
  625. /* Create reference surface */
  626. SDL_DestroySurface(referenceSurface);
  627. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  628. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  629. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  630. /* Clear surface. */
  631. clearScreen();
  632. /* Tiled blit. */
  633. rect.x = 0.0f;
  634. rect.y = 0.0f;
  635. rect.w = (float)TESTRENDER_SCREEN_W;
  636. rect.h = (float)TESTRENDER_SCREEN_H;
  637. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, &rect, 1.0f);
  638. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  639. /* See if it's the same */
  640. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  641. /* Make current */
  642. SDL_RenderPresent(renderer);
  643. }
  644. /* complex tiled 9-grid blit - 2.0 scale */
  645. {
  646. SDLTest_Log("complex tiled 9-grid blit - 2.0 scale");
  647. /* Create reference surface */
  648. SDL_DestroySurface(referenceSurface);
  649. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  650. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  651. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  652. /* Clear surface. */
  653. clearScreen();
  654. /* Tiled blit. */
  655. rect.x = 0.0f;
  656. rect.y = 0.0f;
  657. rect.w = (float)TESTRENDER_SCREEN_W;
  658. rect.h = (float)TESTRENDER_SCREEN_H;
  659. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, &rect, 2.0f);
  660. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  661. /* See if it's the same */
  662. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  663. /* Make current */
  664. SDL_RenderPresent(renderer);
  665. }
  666. /* Clean up. */
  667. SDL_DestroySurface(referenceSurface);
  668. SDL_DestroySurface(source);
  669. SDL_DestroyTexture(texture);
  670. return TEST_COMPLETED;
  671. }
  672. /**
  673. * Blits doing color tests.
  674. *
  675. * \sa SDL_SetTextureColorMod
  676. * \sa SDL_RenderTexture
  677. * \sa SDL_DestroyTexture
  678. */
  679. static int SDLCALL render_testBlitColor(void *arg)
  680. {
  681. int ret;
  682. SDL_FRect rect;
  683. SDL_Texture *tface;
  684. SDL_Surface *referenceSurface = NULL;
  685. int i, j, ni, nj;
  686. int checkFailCount1;
  687. int checkFailCount2;
  688. /* Clear surface. */
  689. clearScreen();
  690. /* Create face surface. */
  691. tface = loadTestFace();
  692. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  693. if (tface == NULL) {
  694. return TEST_ABORTED;
  695. }
  696. /* Constant values. */
  697. rect.w = (float)tface->w;
  698. rect.h = (float)tface->h;
  699. ni = TESTRENDER_SCREEN_W - tface->w;
  700. nj = TESTRENDER_SCREEN_H - tface->h;
  701. /* Test blitting with color mod. */
  702. checkFailCount1 = 0;
  703. checkFailCount2 = 0;
  704. for (j = 0; j <= nj; j += 4) {
  705. for (i = 0; i <= ni; i += 4) {
  706. /* Set color mod. */
  707. ret = SDL_SetTextureColorMod(tface, (Uint8)((255 / nj) * j), (Uint8)((255 / ni) * i), (Uint8)((255 / nj) * j));
  708. if (!ret) {
  709. checkFailCount1++;
  710. }
  711. /* Blitting. */
  712. rect.x = (float)i;
  713. rect.y = (float)j;
  714. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  715. if (!ret) {
  716. checkFailCount2++;
  717. }
  718. }
  719. }
  720. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  721. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount2);
  722. /* See if it's the same. */
  723. referenceSurface = SDLTest_ImageBlitColor();
  724. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  725. /* Make current */
  726. SDL_RenderPresent(renderer);
  727. /* Clean up. */
  728. SDL_DestroyTexture(tface);
  729. SDL_DestroySurface(referenceSurface);
  730. referenceSurface = NULL;
  731. return TEST_COMPLETED;
  732. }
  733. typedef enum TestRenderOperation
  734. {
  735. TEST_RENDER_POINT,
  736. TEST_RENDER_LINE,
  737. TEST_RENDER_RECT,
  738. TEST_RENDER_COPY_XRGB,
  739. TEST_RENDER_COPY_ARGB,
  740. } TestRenderOperation;
  741. /**
  742. * Helper that tests a specific operation and blend mode, -1 for color mod, -2 for alpha mod
  743. */
  744. static void testBlendModeOperation(TestRenderOperation op, int mode, SDL_PixelFormat dst_format)
  745. {
  746. /* Allow up to 2 delta from theoretical value to account for rounding error.
  747. * We allow 2 rounding errors because the software renderer breaks drawing operations into alpha multiplication and a separate blend operation.
  748. */
  749. const int MAXIMUM_ERROR = 2;
  750. int ret;
  751. SDL_Texture *src = NULL;
  752. SDL_Texture *dst;
  753. SDL_Surface *result;
  754. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  755. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  756. Uint8 expectedR, expectedG, expectedB, expectedA;
  757. Uint8 actualR, actualG, actualB, actualA;
  758. int deltaR, deltaG, deltaB, deltaA;
  759. const char *operation = "UNKNOWN";
  760. const char *mode_name = "UNKNOWN";
  761. /* Create dst surface */
  762. dst = SDL_CreateTexture(renderer, dst_format, SDL_TEXTUREACCESS_TARGET, 3, 3);
  763. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  764. if (dst == NULL) {
  765. return;
  766. }
  767. if (SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  768. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  769. ret = SDL_GetTextureBlendMode(dst, &blendMode);
  770. SDLTest_AssertCheck(ret == true, "Verify result from SDL_GetTextureBlendMode(), expected: true, got: %i", ret);
  771. SDLTest_AssertCheck(blendMode == SDL_BLENDMODE_BLEND, "Verify alpha texture blend mode, expected %d, got %" SDL_PRIu32, SDL_BLENDMODE_BLEND, blendMode);
  772. }
  773. /* Set as render target */
  774. SDL_SetRenderTarget(renderer, dst);
  775. /* Clear surface. */
  776. if (!SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  777. dstA = 255;
  778. }
  779. ret = SDL_SetRenderDrawColor(renderer, dstR, dstG, dstB, dstA);
  780. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  781. ret = SDL_RenderClear(renderer);
  782. SDLTest_AssertPass("Call to SDL_RenderClear()");
  783. SDLTest_AssertCheck(ret == true, "Verify result from SDL_RenderClear, expected: true, got: %i", ret);
  784. if (op == TEST_RENDER_COPY_XRGB || op == TEST_RENDER_COPY_ARGB) {
  785. Uint8 pixels[4];
  786. /* Create src surface */
  787. src = SDL_CreateTexture(renderer, op == TEST_RENDER_COPY_XRGB ? SDL_PIXELFORMAT_RGBX32 : SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  788. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  789. if (src == NULL) {
  790. return;
  791. }
  792. /* Clear surface. */
  793. if (op == TEST_RENDER_COPY_XRGB) {
  794. srcA = 255;
  795. }
  796. pixels[0] = srcR;
  797. pixels[1] = srcG;
  798. pixels[2] = srcB;
  799. pixels[3] = srcA;
  800. SDL_UpdateTexture(src, NULL, pixels, sizeof(pixels));
  801. /* Set blend mode. */
  802. if (mode >= 0) {
  803. ret = SDL_SetTextureBlendMode(src, (SDL_BlendMode)mode);
  804. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  805. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  806. } else {
  807. ret = SDL_SetTextureBlendMode(src, SDL_BLENDMODE_BLEND);
  808. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  809. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  810. }
  811. } else {
  812. /* Set draw color */
  813. ret = SDL_SetRenderDrawColor(renderer, srcR, srcG, srcB, srcA);
  814. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  815. /* Set blend mode. */
  816. if (mode >= 0) {
  817. ret = SDL_SetRenderDrawBlendMode(renderer, (SDL_BlendMode)mode);
  818. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  819. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  820. } else {
  821. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  822. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  823. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  824. }
  825. }
  826. /* Test blend mode. */
  827. #define FLOAT(X) ((float)X / 255.0f)
  828. switch (mode) {
  829. case -1:
  830. mode_name = "color modulation";
  831. ret = SDL_SetTextureColorMod(src, srcR, srcG, srcB);
  832. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureColorMod, expected: true, got: %i", ret);
  833. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  834. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  835. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  836. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  837. break;
  838. case -2:
  839. mode_name = "alpha modulation";
  840. ret = SDL_SetTextureAlphaMod(src, srcA);
  841. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureAlphaMod, expected: true, got: %i", ret);
  842. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstR) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  843. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstG) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  844. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstB) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  845. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  846. break;
  847. case SDL_BLENDMODE_NONE:
  848. mode_name = "SDL_BLENDMODE_NONE";
  849. expectedR = srcR;
  850. expectedG = srcG;
  851. expectedB = srcB;
  852. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  853. break;
  854. case SDL_BLENDMODE_BLEND:
  855. mode_name = "SDL_BLENDMODE_BLEND";
  856. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  857. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  858. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  859. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  860. break;
  861. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  862. mode_name = "SDL_BLENDMODE_BLEND_PREMULTIPLIED";
  863. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  864. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  865. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  866. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  867. break;
  868. case SDL_BLENDMODE_ADD:
  869. mode_name = "SDL_BLENDMODE_ADD";
  870. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  871. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  872. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  873. expectedA = dstA;
  874. break;
  875. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  876. mode_name = "SDL_BLENDMODE_ADD_PREMULTIPLIED";
  877. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  878. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  879. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  880. expectedA = dstA;
  881. break;
  882. case SDL_BLENDMODE_MOD:
  883. mode_name = "SDL_BLENDMODE_MOD";
  884. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  885. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  886. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  887. expectedA = dstA;
  888. break;
  889. case SDL_BLENDMODE_MUL:
  890. mode_name = "SDL_BLENDMODE_MUL";
  891. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  892. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  893. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  894. expectedA = dstA;
  895. break;
  896. default:
  897. SDLTest_LogError("Invalid blending mode: %d", mode);
  898. return;
  899. }
  900. switch (op) {
  901. case TEST_RENDER_POINT:
  902. operation = "render point";
  903. ret = SDL_RenderPoint(renderer, 0.0f, 0.0f);
  904. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", ret);
  905. break;
  906. case TEST_RENDER_LINE:
  907. operation = "render line";
  908. ret = SDL_RenderLine(renderer, 0.0f, 0.0f, 2.0f, 2.0f);
  909. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderLine, expected: true, got: %i", ret);
  910. break;
  911. case TEST_RENDER_RECT:
  912. operation = "render rect";
  913. ret = SDL_RenderFillRect(renderer, NULL);
  914. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderFillRect, expected: 0, got: %i", ret);
  915. break;
  916. case TEST_RENDER_COPY_XRGB:
  917. case TEST_RENDER_COPY_ARGB:
  918. operation = (op == TEST_RENDER_COPY_XRGB) ? "render XRGB" : "render ARGB";
  919. ret = SDL_RenderTexture(renderer, src, NULL, NULL);
  920. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderTexture, expected: true, got: %i", ret);
  921. break;
  922. default:
  923. SDLTest_LogError("Invalid blending operation: %d", op);
  924. return;
  925. }
  926. result = SDL_RenderReadPixels(renderer, NULL);
  927. SDL_ReadSurfacePixel(result, 0, 0, &actualR, &actualG, &actualB, &actualA);
  928. deltaR = SDL_abs((int)actualR - expectedR);
  929. deltaG = SDL_abs((int)actualG - expectedG);
  930. deltaB = SDL_abs((int)actualB - expectedB);
  931. deltaA = SDL_abs((int)actualA - expectedA);
  932. SDLTest_AssertCheck(
  933. deltaR <= MAXIMUM_ERROR &&
  934. deltaG <= MAXIMUM_ERROR &&
  935. deltaB <= MAXIMUM_ERROR &&
  936. deltaA <= MAXIMUM_ERROR,
  937. "Checking %s %s operation results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  938. operation, mode_name,
  939. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  940. /* Clean up */
  941. SDL_DestroySurface(result);
  942. SDL_DestroyTexture(src);
  943. SDL_DestroyTexture(dst);
  944. }
  945. static void testBlendMode(int mode)
  946. {
  947. const TestRenderOperation operations[] = {
  948. TEST_RENDER_POINT,
  949. TEST_RENDER_LINE,
  950. TEST_RENDER_RECT,
  951. TEST_RENDER_COPY_XRGB,
  952. TEST_RENDER_COPY_ARGB
  953. };
  954. const SDL_PixelFormat dst_formats[] = {
  955. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  956. };
  957. int i, j;
  958. for (i = 0; i < SDL_arraysize(operations); ++i) {
  959. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  960. TestRenderOperation op = operations[i];
  961. if (mode < 0) {
  962. if (op != TEST_RENDER_COPY_XRGB && op != TEST_RENDER_COPY_ARGB) {
  963. /* Unsupported mode for this operation */
  964. continue;
  965. }
  966. }
  967. testBlendModeOperation(op, mode, dst_formats[j]);
  968. }
  969. }
  970. }
  971. /**
  972. * Tests render operations with blend modes
  973. */
  974. static int SDLCALL render_testBlendModes(void *arg)
  975. {
  976. testBlendMode(-1);
  977. testBlendMode(-2);
  978. testBlendMode(SDL_BLENDMODE_NONE);
  979. testBlendMode(SDL_BLENDMODE_BLEND);
  980. testBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  981. testBlendMode(SDL_BLENDMODE_ADD);
  982. testBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  983. testBlendMode(SDL_BLENDMODE_MOD);
  984. testBlendMode(SDL_BLENDMODE_MUL);
  985. return TEST_COMPLETED;
  986. }
  987. /**
  988. * Test viewport
  989. */
  990. static int SDLCALL render_testViewport(void *arg)
  991. {
  992. SDL_Surface *referenceSurface;
  993. SDL_Rect viewport;
  994. viewport.x = TESTRENDER_SCREEN_W / 3;
  995. viewport.y = TESTRENDER_SCREEN_H / 3;
  996. viewport.w = TESTRENDER_SCREEN_W / 2;
  997. viewport.h = TESTRENDER_SCREEN_H / 2;
  998. /* Create expected result */
  999. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1000. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1001. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1002. /* Clear surface. */
  1003. clearScreen();
  1004. /* Set the viewport and do a fill operation */
  1005. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1006. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1007. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1008. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1009. /* Check to see if final image matches. */
  1010. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1011. /*
  1012. * Verify that clear ignores the viewport
  1013. */
  1014. /* Create expected result */
  1015. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  1016. /* Clear surface. */
  1017. clearScreen();
  1018. /* Set the viewport and do a clear operation */
  1019. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1020. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1021. CHECK_FUNC(SDL_RenderClear, (renderer))
  1022. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1023. /* Check to see if final image matches. */
  1024. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1025. /* Make current */
  1026. SDL_RenderPresent(renderer);
  1027. SDL_DestroySurface(referenceSurface);
  1028. return TEST_COMPLETED;
  1029. }
  1030. static int SDLCALL render_testRGBSurfaceNoAlpha(void* arg)
  1031. {
  1032. SDL_Surface *surface;
  1033. SDL_Renderer *software_renderer;
  1034. SDL_Surface *surface2;
  1035. SDL_Texture *texture2;
  1036. bool result;
  1037. SDL_FRect dest_rect;
  1038. SDL_FPoint point;
  1039. const SDL_PixelFormatDetails *format_details;
  1040. Uint8 r, g, b, a;
  1041. SDLTest_AssertPass("About to call SDL_CreateSurface(128, 128, SDL_PIXELFORMAT_RGBX32)");
  1042. surface = SDL_CreateSurface(128, 128, SDL_PIXELFORMAT_RGBX32);
  1043. SDLTest_AssertCheck(surface != NULL, "Returned surface must be not NULL");
  1044. if (surface == NULL) {
  1045. return TEST_ABORTED;
  1046. }
  1047. SDLTest_AssertPass("About to call SDL_GetPixelFormatDetails(surface->format)");
  1048. format_details = SDL_GetPixelFormatDetails(surface->format);
  1049. SDLTest_AssertCheck(format_details != NULL, "Result must be non-NULL, is %p", format_details);
  1050. if (format_details == NULL) {
  1051. SDL_DestroySurface(surface);
  1052. return TEST_ABORTED;
  1053. }
  1054. SDLTest_AssertCheck(format_details->bits_per_pixel == 32, "format_details->bits_per_pixel is %d, should be %d", format_details->bits_per_pixel, 32);
  1055. SDLTest_AssertCheck(format_details->bytes_per_pixel == 4, "format_details->bytes_per_pixel is %d, should be %d", format_details->bytes_per_pixel, 4);
  1056. SDLTest_AssertPass("About to call SDL_CreateSoftwareRenderer(surface)");
  1057. software_renderer = SDL_CreateSoftwareRenderer(surface);
  1058. SDLTest_AssertCheck(software_renderer != NULL, "Returned renderer must be not NULL");
  1059. if (software_renderer == NULL) {
  1060. SDL_DestroySurface(surface);
  1061. return TEST_ABORTED;
  1062. }
  1063. SDLTest_AssertPass("About to call SDL_CreateSurface(16, 16, SDL_PIXELFORMAT_RGBX32)");
  1064. surface2 = SDL_CreateSurface(16, 16, SDL_PIXELFORMAT_RGBX32);
  1065. SDLTest_AssertCheck(surface2 != NULL, "Returned surface must be not NULL");
  1066. if (surface2 == NULL) {
  1067. SDL_DestroySurface(surface);
  1068. SDL_DestroyRenderer(software_renderer);
  1069. return TEST_ABORTED;
  1070. }
  1071. SDLTest_AssertPass("About to call SDL_FillRect(surface2, NULL, 0)");
  1072. result = SDL_FillSurfaceRect(surface2, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface2->format), NULL, 0, 0, 0));
  1073. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1074. SDLTest_AssertPass("About to call SDL_CreateTextureFromSurface(software_renderer, surface2)");
  1075. texture2 = SDL_CreateTextureFromSurface(software_renderer, surface2);
  1076. SDLTest_AssertCheck(texture2 != NULL, "Returned texture is not NULL");
  1077. SDLTest_AssertPass("About to call SDL_SetRenderDrawColor(renderer, 0xaa, 0xbb, 0xcc, 0x0)");
  1078. result = SDL_SetRenderDrawColor(software_renderer, 0xaa, 0xbb, 0xcc, 0x0);
  1079. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1080. SDLTest_AssertPass("About to call SDL_RenderClear(renderer)");
  1081. result = SDL_RenderClear(software_renderer);
  1082. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1083. SDLTest_AssertPass("About to call SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0x0)");
  1084. result = SDL_SetRenderDrawColor(software_renderer, 0x0, 0x0, 0x0, 0x0);
  1085. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1086. dest_rect.x = 32;
  1087. dest_rect.y = 32;
  1088. dest_rect.w = (float)surface2->w;
  1089. dest_rect.h = (float)surface2->h;
  1090. point.x = 0;
  1091. point.y = 0;
  1092. SDLTest_AssertPass("About to call SDL_RenderCopy(software_renderer, texture, NULL, &{%g, %g, %g, %g})",
  1093. dest_rect.x, dest_rect.h, dest_rect.w, dest_rect.h);
  1094. result = SDL_RenderTextureRotated(software_renderer, texture2, NULL, &dest_rect, 180, &point, SDL_FLIP_NONE);
  1095. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1096. SDLTest_AssertPass("About to call SDL_RenderPresent(software_renderer)");
  1097. SDL_RenderPresent(software_renderer);
  1098. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(0, 0)");
  1099. result = SDL_ReadSurfacePixel(surface, 0, 0, &r, &g, &b, &a);
  1100. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1101. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1102. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1103. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1104. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(15, 15)");
  1105. result = SDL_ReadSurfacePixel(surface, 15, 15, &r, &g, &b, &a);
  1106. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1107. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1108. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1109. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1110. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(16, 16)");
  1111. result = SDL_ReadSurfacePixel(surface, 16, 16, &r, &g, &b, &a);
  1112. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1113. SDLTest_AssertCheck(r == 0x00 && g == 0x00 && b == 0x00 && a == SDL_ALPHA_OPAQUE,
  1114. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1115. r, g, b, a, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  1116. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(31, 31)");
  1117. result = SDL_ReadSurfacePixel(surface, 31, 31, &r, &g, &b, &a);
  1118. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1119. SDLTest_AssertCheck(r == 0x00 && g == 0x00 && b == 0x00 && a == SDL_ALPHA_OPAQUE,
  1120. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1121. r, g, b, a, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  1122. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(32, 32)");
  1123. result = SDL_ReadSurfacePixel(surface, 32, 32, &r, &g, &b, &a);
  1124. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1125. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1126. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1127. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1128. SDL_DestroyTexture(texture2);
  1129. SDL_DestroySurface(surface2);
  1130. SDL_DestroyRenderer(software_renderer);
  1131. SDL_DestroySurface(surface);
  1132. return TEST_COMPLETED;
  1133. }
  1134. /**
  1135. * Test clip rect
  1136. */
  1137. static int SDLCALL render_testClipRect(void *arg)
  1138. {
  1139. SDL_Surface *referenceSurface;
  1140. SDL_Rect cliprect;
  1141. cliprect.x = TESTRENDER_SCREEN_W / 3;
  1142. cliprect.y = TESTRENDER_SCREEN_H / 3;
  1143. cliprect.w = TESTRENDER_SCREEN_W / 2;
  1144. cliprect.h = TESTRENDER_SCREEN_H / 2;
  1145. /* Create expected result */
  1146. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1147. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1148. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &cliprect, RENDER_COLOR_GREEN))
  1149. /* Clear surface. */
  1150. clearScreen();
  1151. /* Set the cliprect and do a fill operation */
  1152. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1153. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1154. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1155. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1156. /* Check to see if final image matches. */
  1157. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1158. /*
  1159. * Verify that clear ignores the cliprect
  1160. */
  1161. /* Create expected result */
  1162. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  1163. /* Clear surface. */
  1164. clearScreen();
  1165. /* Set the cliprect and do a clear operation */
  1166. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1167. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1168. CHECK_FUNC(SDL_RenderClear, (renderer))
  1169. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1170. /* Check to see if final image matches. */
  1171. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1172. /* Make current */
  1173. SDL_RenderPresent(renderer);
  1174. SDL_DestroySurface(referenceSurface);
  1175. return TEST_COMPLETED;
  1176. }
  1177. /**
  1178. * Test logical size
  1179. */
  1180. static int SDLCALL render_testLogicalSize(void *arg)
  1181. {
  1182. SDL_Surface *referenceSurface;
  1183. SDL_Rect viewport;
  1184. SDL_FRect rect;
  1185. int w, h;
  1186. int set_w, set_h;
  1187. SDL_RendererLogicalPresentation set_presentation_mode;
  1188. SDL_FRect set_rect;
  1189. const int factor = 2;
  1190. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  1191. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  1192. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  1193. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  1194. /* Create expected result */
  1195. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1196. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1197. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1198. /* Clear surface. */
  1199. clearScreen();
  1200. /* Set the logical size and do a fill operation */
  1201. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1202. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1203. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1204. SDLTest_AssertCheck(
  1205. set_w == (w / factor) &&
  1206. set_h == (h / factor) &&
  1207. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1208. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1209. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1210. SDLTest_AssertCheck(
  1211. set_rect.x == 0.0f &&
  1212. set_rect.y == 0.0f &&
  1213. set_rect.w == 320.0f &&
  1214. set_rect.h == 240.0f,
  1215. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1216. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1217. rect.x = (float)viewport.x / factor;
  1218. rect.y = (float)viewport.y / factor;
  1219. rect.w = (float)viewport.w / factor;
  1220. rect.h = (float)viewport.h / factor;
  1221. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  1222. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1223. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1224. SDLTest_AssertCheck(
  1225. set_w == 0 &&
  1226. set_h == 0 &&
  1227. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1228. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1229. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1230. SDLTest_AssertCheck(
  1231. set_rect.x == 0.0f &&
  1232. set_rect.y == 0.0f &&
  1233. set_rect.w == 320.0f &&
  1234. set_rect.h == 240.0f,
  1235. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1236. /* Check to see if final image matches. */
  1237. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1238. /* Clear surface. */
  1239. clearScreen();
  1240. /* Set the logical size and viewport and do a fill operation */
  1241. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1242. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1243. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  1244. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  1245. viewport.w = (TESTRENDER_SCREEN_W / 2) / factor;
  1246. viewport.h = (TESTRENDER_SCREEN_H / 2) / factor;
  1247. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1248. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1249. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1250. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1251. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1252. /* Check to see if final image matches. */
  1253. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1254. /*
  1255. * Test a logical size that isn't the same aspect ratio as the window
  1256. */
  1257. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1258. viewport.y = 0;
  1259. viewport.w = TESTRENDER_SCREEN_W;
  1260. viewport.h = TESTRENDER_SCREEN_H;
  1261. /* Create expected result */
  1262. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1263. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1264. /* Clear surface. */
  1265. clearScreen();
  1266. /* Set the logical size and do a fill operation */
  1267. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1268. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1269. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1270. h,
  1271. SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1272. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1273. SDLTest_AssertCheck(
  1274. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1275. set_h == h &&
  1276. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1277. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1278. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1279. SDLTest_AssertCheck(
  1280. set_rect.x == 20.0f &&
  1281. set_rect.y == 0.0f &&
  1282. set_rect.w == 280.0f &&
  1283. set_rect.h == 240.0f,
  1284. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1285. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1286. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1287. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1288. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1289. SDLTest_AssertCheck(
  1290. set_w == 0 &&
  1291. set_h == 0 &&
  1292. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1293. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1294. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1295. SDLTest_AssertCheck(
  1296. set_rect.x == 0.0f &&
  1297. set_rect.y == 0.0f &&
  1298. set_rect.w == 320.0f &&
  1299. set_rect.h == 240.0f,
  1300. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1301. /* Check to see if final image matches. */
  1302. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1303. /* Clear surface. */
  1304. clearScreen();
  1305. /* Make current */
  1306. SDL_RenderPresent(renderer);
  1307. SDL_DestroySurface(referenceSurface);
  1308. return TEST_COMPLETED;
  1309. }
  1310. /**
  1311. * @brief Tests setting and getting texture scale mode.
  1312. *
  1313. * \sa
  1314. * http://wiki.libsdl.org/SDL2/SDL_SetTextureScaleMode
  1315. * http://wiki.libsdl.org/SDL2/SDL_GetTextureScaleMode
  1316. */
  1317. static int SDLCALL render_testGetSetTextureScaleMode(void *arg)
  1318. {
  1319. const struct {
  1320. const char *name;
  1321. SDL_ScaleMode mode;
  1322. } modes[] = {
  1323. { "SDL_SCALEMODE_NEAREST", SDL_SCALEMODE_NEAREST },
  1324. { "SDL_SCALEMODE_LINEAR", SDL_SCALEMODE_LINEAR },
  1325. { "SDL_SCALEMODE_PIXELART", SDL_SCALEMODE_PIXELART },
  1326. };
  1327. size_t i;
  1328. for (i = 0; i < SDL_arraysize(modes); i++) {
  1329. SDL_Texture *texture;
  1330. bool result;
  1331. SDL_ScaleMode actual_mode = SDL_SCALEMODE_NEAREST;
  1332. SDL_ClearError();
  1333. SDLTest_AssertPass("About to call SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16)");
  1334. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16);
  1335. SDLTest_AssertCheck(texture != NULL, "SDL_CreateTexture must return a non-NULL texture");
  1336. SDLTest_AssertPass("About to call SDL_SetTextureScaleMode(texture, %s)", modes[i].name);
  1337. result = SDL_SetTextureScaleMode(texture, modes[i].mode);
  1338. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1339. SDLTest_AssertPass("About to call SDL_GetTextureScaleMode(texture)");
  1340. result = SDL_GetTextureScaleMode(texture, &actual_mode);
  1341. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1342. SDLTest_AssertCheck(actual_mode == modes[i].mode, "SDL_GetTextureScaleMode must return %s (%d), actual=%d",
  1343. modes[i].name, modes[i].mode, actual_mode);
  1344. }
  1345. return TEST_COMPLETED;
  1346. }
  1347. /* Helper functions */
  1348. /**
  1349. * Checks to see if functionality is supported. Helper function.
  1350. */
  1351. static bool isSupported(int code)
  1352. {
  1353. return (code != false);
  1354. }
  1355. /**
  1356. * Test to see if we can vary the draw color. Helper function.
  1357. *
  1358. * \sa SDL_SetRenderDrawColor
  1359. * \sa SDL_GetRenderDrawColor
  1360. */
  1361. static bool hasDrawColor(void)
  1362. {
  1363. int ret, fail;
  1364. Uint8 r, g, b, a;
  1365. fail = 0;
  1366. /* Set color. */
  1367. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1368. if (!isSupported(ret)) {
  1369. fail = 1;
  1370. }
  1371. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1372. if (!isSupported(ret)) {
  1373. fail = 1;
  1374. }
  1375. /* Restore natural. */
  1376. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1377. if (!isSupported(ret)) {
  1378. fail = 1;
  1379. }
  1380. /* Something failed, consider not available. */
  1381. if (fail) {
  1382. return false;
  1383. }
  1384. /* Not set properly, consider failed. */
  1385. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1386. return false;
  1387. }
  1388. return true;
  1389. }
  1390. /**
  1391. * Loads the test image 'Face' as texture. Helper function.
  1392. *
  1393. * \sa SDL_CreateTextureFromSurface
  1394. */
  1395. static SDL_Texture *
  1396. loadTestFace(void)
  1397. {
  1398. SDL_Surface *face;
  1399. SDL_Texture *tface;
  1400. face = SDLTest_ImageFace();
  1401. if (!face) {
  1402. return NULL;
  1403. }
  1404. tface = SDL_CreateTextureFromSurface(renderer, face);
  1405. if (!tface) {
  1406. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1407. }
  1408. SDL_DestroySurface(face);
  1409. return tface;
  1410. }
  1411. /**
  1412. * Compares screen pixels with image pixels. Helper function.
  1413. *
  1414. * \param referenceSurface Image to compare against.
  1415. * \param allowable_error allowed difference from the reference image
  1416. *
  1417. * \sa SDL_RenderReadPixels
  1418. * \sa SDL_CreateSurfaceFrom
  1419. * \sa SDL_DestroySurface
  1420. */
  1421. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1422. {
  1423. int ret;
  1424. SDL_Rect rect;
  1425. SDL_Surface *surface, *testSurface;
  1426. /* Explicitly specify the rect in case the window isn't the expected size... */
  1427. rect.x = 0;
  1428. rect.y = 0;
  1429. rect.w = TESTRENDER_SCREEN_W;
  1430. rect.h = TESTRENDER_SCREEN_H;
  1431. surface = SDL_RenderReadPixels(renderer, &rect);
  1432. if (!surface) {
  1433. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1434. return;
  1435. }
  1436. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1437. SDL_DestroySurface(surface);
  1438. if (!testSurface) {
  1439. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1440. return;
  1441. }
  1442. /* Compare surface. */
  1443. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1444. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1445. /* Clean up. */
  1446. SDL_DestroySurface(testSurface);
  1447. }
  1448. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1449. {
  1450. int ret;
  1451. SDL_Rect rect;
  1452. SDL_Surface *surface, *testSurface;
  1453. /* Explicitly specify the rect in case the window isn't the expected size... */
  1454. rect.x = 0;
  1455. rect.y = 0;
  1456. rect.w = TESTRENDER_SCREEN_W * 2;
  1457. rect.h = TESTRENDER_SCREEN_H * 2;
  1458. surface = SDL_RenderReadPixels(renderer, &rect);
  1459. if (!surface) {
  1460. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1461. return;
  1462. }
  1463. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1464. SDL_DestroySurface(surface);
  1465. if (!testSurface) {
  1466. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1467. return;
  1468. }
  1469. /* Compare surface. */
  1470. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1471. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1472. /* Clean up. */
  1473. SDL_DestroySurface(testSurface);
  1474. }
  1475. /**
  1476. * Clears the screen. Helper function.
  1477. *
  1478. * \sa SDL_SetRenderDrawColor
  1479. * \sa SDL_RenderClear
  1480. * \sa SDL_RenderPresent
  1481. * \sa SDL_SetRenderDrawBlendMode
  1482. */
  1483. static int
  1484. clearScreen(void)
  1485. {
  1486. int ret;
  1487. /* Make current */
  1488. SDL_RenderPresent(renderer);
  1489. /* Set color. */
  1490. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1491. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1492. /* Clear screen. */
  1493. ret = SDL_RenderClear(renderer);
  1494. SDLTest_AssertCheck(ret == true, "Validate result from SDL_RenderClear, expected: true, got: %i", ret);
  1495. /* Set defaults. */
  1496. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1497. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawBlendMode, expected: true, got: %i", ret);
  1498. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1499. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1500. return 0;
  1501. }
  1502. /**
  1503. * Tests geometry UV wrapping
  1504. */
  1505. static int SDLCALL render_testUVWrapping(void *arg)
  1506. {
  1507. SDL_Vertex vertices[6];
  1508. SDL_Vertex *verts = vertices;
  1509. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1510. SDL_FRect rect;
  1511. float min_U = -0.5f;
  1512. float max_U = 1.5f;
  1513. float min_V = -0.5f;
  1514. float max_V = 1.5f;
  1515. SDL_Texture *tface;
  1516. SDL_Surface *referenceSurface = NULL;
  1517. /* Clear surface. */
  1518. clearScreen();
  1519. /* Create face surface. */
  1520. tface = loadTestFace();
  1521. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1522. if (tface == NULL) {
  1523. return TEST_ABORTED;
  1524. }
  1525. rect.w = (float)tface->w * 2;
  1526. rect.h = (float)tface->h * 2;
  1527. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1528. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1529. /*
  1530. * 0--1
  1531. * | /|
  1532. * |/ |
  1533. * 3--2
  1534. *
  1535. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1536. */
  1537. /* 0 */
  1538. verts->position.x = rect.x;
  1539. verts->position.y = rect.y;
  1540. verts->color = color;
  1541. verts->tex_coord.x = min_U;
  1542. verts->tex_coord.y = min_V;
  1543. verts++;
  1544. /* 1 */
  1545. verts->position.x = rect.x + rect.w;
  1546. verts->position.y = rect.y;
  1547. verts->color = color;
  1548. verts->tex_coord.x = max_U;
  1549. verts->tex_coord.y = min_V;
  1550. verts++;
  1551. /* 2 */
  1552. verts->position.x = rect.x + rect.w;
  1553. verts->position.y = rect.y + rect.h;
  1554. verts->color = color;
  1555. verts->tex_coord.x = max_U;
  1556. verts->tex_coord.y = max_V;
  1557. verts++;
  1558. /* 0 */
  1559. verts->position.x = rect.x;
  1560. verts->position.y = rect.y;
  1561. verts->color = color;
  1562. verts->tex_coord.x = min_U;
  1563. verts->tex_coord.y = min_V;
  1564. verts++;
  1565. /* 2 */
  1566. verts->position.x = rect.x + rect.w;
  1567. verts->position.y = rect.y + rect.h;
  1568. verts->color = color;
  1569. verts->tex_coord.x = max_U;
  1570. verts->tex_coord.y = max_V;
  1571. verts++;
  1572. /* 3 */
  1573. verts->position.x = rect.x;
  1574. verts->position.y = rect.y + rect.h;
  1575. verts->color = color;
  1576. verts->tex_coord.x = min_U;
  1577. verts->tex_coord.y = max_V;
  1578. verts++;
  1579. /* Blit sprites as triangles onto the screen */
  1580. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1581. /* See if it's the same */
  1582. referenceSurface = SDLTest_ImageWrappingSprite();
  1583. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1584. /* Make current */
  1585. SDL_RenderPresent(renderer);
  1586. /* Clean up. */
  1587. SDL_DestroyTexture(tface);
  1588. SDL_DestroySurface(referenceSurface);
  1589. referenceSurface = NULL;
  1590. return TEST_COMPLETED;
  1591. }
  1592. /**
  1593. * Tests texture state changes
  1594. */
  1595. static int SDLCALL render_testTextureState(void *arg)
  1596. {
  1597. const Uint8 pixels[8] = {
  1598. 0x00, 0x00, 0x00, 0xFF,
  1599. 0xFF, 0xFF, 0xFF, 0xFF
  1600. };
  1601. const SDL_Color expected[] = {
  1602. /* Step 0: plain copy */
  1603. { 0x00, 0x00, 0x00, 0xFF },
  1604. { 0xFF, 0xFF, 0xFF, 0xFF },
  1605. /* Step 1: color mod to red */
  1606. { 0x00, 0x00, 0x00, 0xFF },
  1607. { 0xFF, 0x00, 0x00, 0xFF },
  1608. /* Step 2: alpha mod to 128 (cleared to green) */
  1609. { 0x00, 0x7F, 0x00, 0xFF },
  1610. { 0x80, 0xFF, 0x80, 0xFF },
  1611. /* Step 3: nearest stretch */
  1612. { 0xFF, 0xFF, 0xFF, 0xFF },
  1613. { 0x00, 0xFF, 0x00, 0xFF },
  1614. /* Step 4: linear stretch */
  1615. { 0x80, 0x80, 0x80, 0xFF },
  1616. { 0x00, 0xFF, 0x00, 0xFF },
  1617. };
  1618. SDL_Texture *texture;
  1619. SDL_Rect rect;
  1620. SDL_FRect dst;
  1621. int i;
  1622. /* Clear surface to green */
  1623. SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
  1624. SDL_RenderClear(renderer);
  1625. /* Create 2-pixel surface. */
  1626. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 2, 1);
  1627. SDLTest_AssertCheck(texture != NULL, "Verify SDL_CreateTexture() result");
  1628. if (texture == NULL) {
  1629. return TEST_ABORTED;
  1630. }
  1631. SDL_UpdateTexture(texture, NULL, pixels, sizeof(pixels));
  1632. dst.x = 0.0f;
  1633. dst.y = 0.0f;
  1634. dst.w = 2.0f;
  1635. dst.h = 1.0f;
  1636. /* Step 0: plain copy */
  1637. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1638. dst.y += 1;
  1639. /* Step 1: color mod to red */
  1640. SDL_SetTextureColorMod(texture, 0xFF, 0x00, 0x00);
  1641. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1642. SDL_SetTextureColorMod(texture, 0xFF, 0xFF, 0xFF);
  1643. dst.y += 1;
  1644. /* Step 2: alpha mod to 128 */
  1645. SDL_SetTextureAlphaMod(texture, 0x80);
  1646. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1647. SDL_SetTextureAlphaMod(texture, 0xFF);
  1648. dst.y += 1;
  1649. /* Step 3: nearest stretch */
  1650. dst.w = 1;
  1651. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  1652. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1653. dst.y += 1;
  1654. /* Step 4: linear stretch */
  1655. dst.w = 1;
  1656. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_LINEAR);
  1657. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1658. dst.y += 1;
  1659. /* Verify results */
  1660. rect.x = 0;
  1661. rect.y = 0;
  1662. rect.w = 2;
  1663. rect.h = 1;
  1664. for (i = 0; i < SDL_arraysize(expected); ) {
  1665. const int MAX_DELTA = 1;
  1666. SDL_Color actual;
  1667. int deltaR, deltaG, deltaB, deltaA;
  1668. SDL_Surface *surface = SDL_RenderReadPixels(renderer, &rect);
  1669. SDL_ReadSurfacePixel(surface, 0, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1670. deltaR = (actual.r - expected[i].r);
  1671. deltaG = (actual.g - expected[i].g);
  1672. deltaB = (actual.b - expected[i].b);
  1673. deltaA = (actual.a - expected[i].a);
  1674. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1675. SDL_abs(deltaG) <= MAX_DELTA &&
  1676. SDL_abs(deltaB) <= MAX_DELTA &&
  1677. SDL_abs(deltaA) <= MAX_DELTA,
  1678. "Validate left pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1679. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1680. actual.r, actual.g, actual.b, actual.a);
  1681. ++i;
  1682. SDL_ReadSurfacePixel(surface, 1, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1683. deltaR = (actual.r - expected[i].r);
  1684. deltaG = (actual.g - expected[i].g);
  1685. deltaB = (actual.b - expected[i].b);
  1686. deltaA = (actual.a - expected[i].a);
  1687. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1688. SDL_abs(deltaG) <= MAX_DELTA &&
  1689. SDL_abs(deltaB) <= MAX_DELTA &&
  1690. SDL_abs(deltaA) <= MAX_DELTA,
  1691. "Validate right pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1692. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1693. actual.r, actual.g, actual.b, actual.a);
  1694. ++i;
  1695. SDL_DestroySurface(surface);
  1696. rect.y += 1;
  1697. }
  1698. /* Clean up. */
  1699. SDL_DestroyTexture(texture);
  1700. return TEST_COMPLETED;
  1701. }
  1702. /* ================= Test References ================== */
  1703. /* Render test cases */
  1704. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1705. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1706. };
  1707. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1708. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1709. };
  1710. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1711. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1712. };
  1713. static const SDLTest_TestCaseReference renderTestBlit = {
  1714. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1715. };
  1716. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1717. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1718. };
  1719. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  1720. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  1721. };
  1722. static const SDLTest_TestCaseReference renderTestBlit9GridTiled = {
  1723. render_testBlit9GridTiled, "render_testBlit9GridTiled", "Tests tiled 9-grid blitting", TEST_ENABLED
  1724. };
  1725. static const SDLTest_TestCaseReference renderTestBlitColor = {
  1726. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  1727. };
  1728. static const SDLTest_TestCaseReference renderTestBlendModes = {
  1729. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  1730. };
  1731. static const SDLTest_TestCaseReference renderTestViewport = {
  1732. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  1733. };
  1734. static const SDLTest_TestCaseReference renderTestClipRect = {
  1735. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  1736. };
  1737. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  1738. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  1739. };
  1740. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  1741. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  1742. };
  1743. static const SDLTest_TestCaseReference renderTestTextureState = {
  1744. render_testTextureState, "render_testTextureState", "Tests texture state changes", TEST_ENABLED
  1745. };
  1746. static const SDLTest_TestCaseReference renderTestGetSetTextureScaleMode = {
  1747. render_testGetSetTextureScaleMode, "render_testGetSetTextureScaleMode", "Tests setting/getting texture scale mode", TEST_ENABLED
  1748. };
  1749. static const SDLTest_TestCaseReference renderTestRGBSurfaceNoAlpha = {
  1750. render_testRGBSurfaceNoAlpha, "render_testRGBSurfaceNoAlpha", "Tests RGB surface with no alpha using software renderer", TEST_ENABLED
  1751. };
  1752. /* Sequence of Render test cases */
  1753. static const SDLTest_TestCaseReference *renderTests[] = {
  1754. &renderTestGetNumRenderDrivers,
  1755. &renderTestPrimitives,
  1756. &renderTestPrimitivesWithViewport,
  1757. &renderTestBlit,
  1758. &renderTestBlitTiled,
  1759. &renderTestBlit9Grid,
  1760. &renderTestBlit9GridTiled,
  1761. &renderTestBlitColor,
  1762. &renderTestBlendModes,
  1763. &renderTestViewport,
  1764. &renderTestClipRect,
  1765. &renderTestLogicalSize,
  1766. &renderTestUVWrapping,
  1767. &renderTestTextureState,
  1768. &renderTestGetSetTextureScaleMode,
  1769. &renderTestRGBSurfaceNoAlpha,
  1770. NULL
  1771. };
  1772. /* Render test suite (global) */
  1773. SDLTest_TestSuiteReference renderTestSuite = {
  1774. "Render",
  1775. InitCreateRenderer,
  1776. renderTests,
  1777. CleanupDestroyRenderer
  1778. };