testautomation_render.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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. * Blits doing color tests.
  522. *
  523. * \sa SDL_SetTextureColorMod
  524. * \sa SDL_RenderTexture
  525. * \sa SDL_DestroyTexture
  526. */
  527. static int SDLCALL render_testBlitColor(void *arg)
  528. {
  529. int ret;
  530. SDL_FRect rect;
  531. SDL_Texture *tface;
  532. SDL_Surface *referenceSurface = NULL;
  533. int i, j, ni, nj;
  534. int checkFailCount1;
  535. int checkFailCount2;
  536. /* Clear surface. */
  537. clearScreen();
  538. /* Create face surface. */
  539. tface = loadTestFace();
  540. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  541. if (tface == NULL) {
  542. return TEST_ABORTED;
  543. }
  544. /* Constant values. */
  545. rect.w = (float)tface->w;
  546. rect.h = (float)tface->h;
  547. ni = TESTRENDER_SCREEN_W - tface->w;
  548. nj = TESTRENDER_SCREEN_H - tface->h;
  549. /* Test blitting with color mod. */
  550. checkFailCount1 = 0;
  551. checkFailCount2 = 0;
  552. for (j = 0; j <= nj; j += 4) {
  553. for (i = 0; i <= ni; i += 4) {
  554. /* Set color mod. */
  555. ret = SDL_SetTextureColorMod(tface, (Uint8)((255 / nj) * j), (Uint8)((255 / ni) * i), (Uint8)((255 / nj) * j));
  556. if (!ret) {
  557. checkFailCount1++;
  558. }
  559. /* Blitting. */
  560. rect.x = (float)i;
  561. rect.y = (float)j;
  562. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  563. if (!ret) {
  564. checkFailCount2++;
  565. }
  566. }
  567. }
  568. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  569. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount2);
  570. /* See if it's the same. */
  571. referenceSurface = SDLTest_ImageBlitColor();
  572. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  573. /* Make current */
  574. SDL_RenderPresent(renderer);
  575. /* Clean up. */
  576. SDL_DestroyTexture(tface);
  577. SDL_DestroySurface(referenceSurface);
  578. referenceSurface = NULL;
  579. return TEST_COMPLETED;
  580. }
  581. typedef enum TestRenderOperation
  582. {
  583. TEST_RENDER_POINT,
  584. TEST_RENDER_LINE,
  585. TEST_RENDER_RECT,
  586. TEST_RENDER_COPY_XRGB,
  587. TEST_RENDER_COPY_ARGB,
  588. } TestRenderOperation;
  589. /**
  590. * Helper that tests a specific operation and blend mode, -1 for color mod, -2 for alpha mod
  591. */
  592. static void testBlendModeOperation(TestRenderOperation op, int mode, SDL_PixelFormat dst_format)
  593. {
  594. /* Allow up to 2 delta from theoretical value to account for rounding error.
  595. * We allow 2 rounding errors because the software renderer breaks drawing operations into alpha multiplication and a separate blend operation.
  596. */
  597. const int MAXIMUM_ERROR = 2;
  598. int ret;
  599. SDL_Texture *src = NULL;
  600. SDL_Texture *dst;
  601. SDL_Surface *result;
  602. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  603. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  604. Uint8 expectedR, expectedG, expectedB, expectedA;
  605. Uint8 actualR, actualG, actualB, actualA;
  606. int deltaR, deltaG, deltaB, deltaA;
  607. const char *operation = "UNKNOWN";
  608. const char *mode_name = "UNKNOWN";
  609. /* Create dst surface */
  610. dst = SDL_CreateTexture(renderer, dst_format, SDL_TEXTUREACCESS_TARGET, 3, 3);
  611. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  612. if (dst == NULL) {
  613. return;
  614. }
  615. if (SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  616. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  617. ret = SDL_GetTextureBlendMode(dst, &blendMode);
  618. SDLTest_AssertCheck(ret == true, "Verify result from SDL_GetTextureBlendMode(), expected: true, got: %i", ret);
  619. SDLTest_AssertCheck(blendMode == SDL_BLENDMODE_BLEND, "Verify alpha texture blend mode, expected %d, got %" SDL_PRIu32, SDL_BLENDMODE_BLEND, blendMode);
  620. }
  621. /* Set as render target */
  622. SDL_SetRenderTarget(renderer, dst);
  623. /* Clear surface. */
  624. if (!SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  625. dstA = 255;
  626. }
  627. ret = SDL_SetRenderDrawColor(renderer, dstR, dstG, dstB, dstA);
  628. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  629. ret = SDL_RenderClear(renderer);
  630. SDLTest_AssertPass("Call to SDL_RenderClear()");
  631. SDLTest_AssertCheck(ret == true, "Verify result from SDL_RenderClear, expected: true, got: %i", ret);
  632. if (op == TEST_RENDER_COPY_XRGB || op == TEST_RENDER_COPY_ARGB) {
  633. Uint8 pixels[4];
  634. /* Create src surface */
  635. src = SDL_CreateTexture(renderer, op == TEST_RENDER_COPY_XRGB ? SDL_PIXELFORMAT_RGBX32 : SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  636. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  637. if (src == NULL) {
  638. return;
  639. }
  640. /* Clear surface. */
  641. if (op == TEST_RENDER_COPY_XRGB) {
  642. srcA = 255;
  643. }
  644. pixels[0] = srcR;
  645. pixels[1] = srcG;
  646. pixels[2] = srcB;
  647. pixels[3] = srcA;
  648. SDL_UpdateTexture(src, NULL, pixels, sizeof(pixels));
  649. /* Set blend mode. */
  650. if (mode >= 0) {
  651. ret = SDL_SetTextureBlendMode(src, (SDL_BlendMode)mode);
  652. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  653. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  654. } else {
  655. ret = SDL_SetTextureBlendMode(src, SDL_BLENDMODE_BLEND);
  656. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  657. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  658. }
  659. } else {
  660. /* Set draw color */
  661. ret = SDL_SetRenderDrawColor(renderer, srcR, srcG, srcB, srcA);
  662. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  663. /* Set blend mode. */
  664. if (mode >= 0) {
  665. ret = SDL_SetRenderDrawBlendMode(renderer, (SDL_BlendMode)mode);
  666. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  667. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  668. } else {
  669. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  670. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  671. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  672. }
  673. }
  674. /* Test blend mode. */
  675. #define FLOAT(X) ((float)X / 255.0f)
  676. switch (mode) {
  677. case -1:
  678. mode_name = "color modulation";
  679. ret = SDL_SetTextureColorMod(src, srcR, srcG, srcB);
  680. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureColorMod, expected: true, got: %i", ret);
  681. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  682. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  683. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  684. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  685. break;
  686. case -2:
  687. mode_name = "alpha modulation";
  688. ret = SDL_SetTextureAlphaMod(src, srcA);
  689. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureAlphaMod, expected: true, got: %i", ret);
  690. 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);
  691. 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);
  692. 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);
  693. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  694. break;
  695. case SDL_BLENDMODE_NONE:
  696. mode_name = "SDL_BLENDMODE_NONE";
  697. expectedR = srcR;
  698. expectedG = srcG;
  699. expectedB = srcB;
  700. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  701. break;
  702. case SDL_BLENDMODE_BLEND:
  703. mode_name = "SDL_BLENDMODE_BLEND";
  704. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  705. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  706. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  707. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  708. break;
  709. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  710. mode_name = "SDL_BLENDMODE_BLEND_PREMULTIPLIED";
  711. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  712. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  713. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  714. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  715. break;
  716. case SDL_BLENDMODE_ADD:
  717. mode_name = "SDL_BLENDMODE_ADD";
  718. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  719. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  720. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  721. expectedA = dstA;
  722. break;
  723. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  724. mode_name = "SDL_BLENDMODE_ADD_PREMULTIPLIED";
  725. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  726. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  727. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  728. expectedA = dstA;
  729. break;
  730. case SDL_BLENDMODE_MOD:
  731. mode_name = "SDL_BLENDMODE_MOD";
  732. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  733. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  734. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  735. expectedA = dstA;
  736. break;
  737. case SDL_BLENDMODE_MUL:
  738. mode_name = "SDL_BLENDMODE_MUL";
  739. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  740. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  741. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  742. expectedA = dstA;
  743. break;
  744. default:
  745. SDLTest_LogError("Invalid blending mode: %d", mode);
  746. return;
  747. }
  748. switch (op) {
  749. case TEST_RENDER_POINT:
  750. operation = "render point";
  751. ret = SDL_RenderPoint(renderer, 0.0f, 0.0f);
  752. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", ret);
  753. break;
  754. case TEST_RENDER_LINE:
  755. operation = "render line";
  756. ret = SDL_RenderLine(renderer, 0.0f, 0.0f, 2.0f, 2.0f);
  757. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderLine, expected: true, got: %i", ret);
  758. break;
  759. case TEST_RENDER_RECT:
  760. operation = "render rect";
  761. ret = SDL_RenderFillRect(renderer, NULL);
  762. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderFillRect, expected: 0, got: %i", ret);
  763. break;
  764. case TEST_RENDER_COPY_XRGB:
  765. case TEST_RENDER_COPY_ARGB:
  766. operation = (op == TEST_RENDER_COPY_XRGB) ? "render XRGB" : "render ARGB";
  767. ret = SDL_RenderTexture(renderer, src, NULL, NULL);
  768. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderTexture, expected: true, got: %i", ret);
  769. break;
  770. default:
  771. SDLTest_LogError("Invalid blending operation: %d", op);
  772. return;
  773. }
  774. result = SDL_RenderReadPixels(renderer, NULL);
  775. SDL_ReadSurfacePixel(result, 0, 0, &actualR, &actualG, &actualB, &actualA);
  776. deltaR = SDL_abs((int)actualR - expectedR);
  777. deltaG = SDL_abs((int)actualG - expectedG);
  778. deltaB = SDL_abs((int)actualB - expectedB);
  779. deltaA = SDL_abs((int)actualA - expectedA);
  780. SDLTest_AssertCheck(
  781. deltaR <= MAXIMUM_ERROR &&
  782. deltaG <= MAXIMUM_ERROR &&
  783. deltaB <= MAXIMUM_ERROR &&
  784. deltaA <= MAXIMUM_ERROR,
  785. "Checking %s %s operation results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  786. operation, mode_name,
  787. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  788. /* Clean up */
  789. SDL_DestroySurface(result);
  790. SDL_DestroyTexture(src);
  791. SDL_DestroyTexture(dst);
  792. }
  793. static void testBlendMode(int mode)
  794. {
  795. const TestRenderOperation operations[] = {
  796. TEST_RENDER_POINT,
  797. TEST_RENDER_LINE,
  798. TEST_RENDER_RECT,
  799. TEST_RENDER_COPY_XRGB,
  800. TEST_RENDER_COPY_ARGB
  801. };
  802. const SDL_PixelFormat dst_formats[] = {
  803. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  804. };
  805. int i, j;
  806. for (i = 0; i < SDL_arraysize(operations); ++i) {
  807. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  808. TestRenderOperation op = operations[i];
  809. if (mode < 0) {
  810. if (op != TEST_RENDER_COPY_XRGB && op != TEST_RENDER_COPY_ARGB) {
  811. /* Unsupported mode for this operation */
  812. continue;
  813. }
  814. }
  815. testBlendModeOperation(op, mode, dst_formats[j]);
  816. }
  817. }
  818. }
  819. /**
  820. * Tests render operations with blend modes
  821. */
  822. static int SDLCALL render_testBlendModes(void *arg)
  823. {
  824. testBlendMode(-1);
  825. testBlendMode(-2);
  826. testBlendMode(SDL_BLENDMODE_NONE);
  827. testBlendMode(SDL_BLENDMODE_BLEND);
  828. testBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  829. testBlendMode(SDL_BLENDMODE_ADD);
  830. testBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  831. testBlendMode(SDL_BLENDMODE_MOD);
  832. testBlendMode(SDL_BLENDMODE_MUL);
  833. return TEST_COMPLETED;
  834. }
  835. /**
  836. * Test viewport
  837. */
  838. static int SDLCALL render_testViewport(void *arg)
  839. {
  840. SDL_Surface *referenceSurface;
  841. SDL_Rect viewport;
  842. viewport.x = TESTRENDER_SCREEN_W / 3;
  843. viewport.y = TESTRENDER_SCREEN_H / 3;
  844. viewport.w = TESTRENDER_SCREEN_W / 2;
  845. viewport.h = TESTRENDER_SCREEN_H / 2;
  846. /* Create expected result */
  847. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  848. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  849. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  850. /* Clear surface. */
  851. clearScreen();
  852. /* Set the viewport and do a fill operation */
  853. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  854. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  855. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  856. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  857. /* Check to see if final image matches. */
  858. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  859. /*
  860. * Verify that clear ignores the viewport
  861. */
  862. /* Create expected result */
  863. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  864. /* Clear surface. */
  865. clearScreen();
  866. /* Set the viewport and do a clear operation */
  867. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  868. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  869. CHECK_FUNC(SDL_RenderClear, (renderer))
  870. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  871. /* Check to see if final image matches. */
  872. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  873. /* Make current */
  874. SDL_RenderPresent(renderer);
  875. SDL_DestroySurface(referenceSurface);
  876. return TEST_COMPLETED;
  877. }
  878. /**
  879. * Test clip rect
  880. */
  881. static int SDLCALL render_testClipRect(void *arg)
  882. {
  883. SDL_Surface *referenceSurface;
  884. SDL_Rect cliprect;
  885. cliprect.x = TESTRENDER_SCREEN_W / 3;
  886. cliprect.y = TESTRENDER_SCREEN_H / 3;
  887. cliprect.w = TESTRENDER_SCREEN_W / 2;
  888. cliprect.h = TESTRENDER_SCREEN_H / 2;
  889. /* Create expected result */
  890. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  891. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  892. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &cliprect, RENDER_COLOR_GREEN))
  893. /* Clear surface. */
  894. clearScreen();
  895. /* Set the cliprect and do a fill operation */
  896. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  897. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  898. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  899. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  900. /* Check to see if final image matches. */
  901. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  902. /*
  903. * Verify that clear ignores the cliprect
  904. */
  905. /* Create expected result */
  906. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  907. /* Clear surface. */
  908. clearScreen();
  909. /* Set the cliprect and do a clear operation */
  910. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  911. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  912. CHECK_FUNC(SDL_RenderClear, (renderer))
  913. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  914. /* Check to see if final image matches. */
  915. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  916. /* Make current */
  917. SDL_RenderPresent(renderer);
  918. SDL_DestroySurface(referenceSurface);
  919. return TEST_COMPLETED;
  920. }
  921. /**
  922. * Test logical size
  923. */
  924. static int SDLCALL render_testLogicalSize(void *arg)
  925. {
  926. SDL_Surface *referenceSurface;
  927. SDL_Rect viewport;
  928. SDL_FRect rect;
  929. int w, h;
  930. int set_w, set_h;
  931. SDL_RendererLogicalPresentation set_presentation_mode;
  932. SDL_FRect set_rect;
  933. const int factor = 2;
  934. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  935. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  936. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  937. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  938. /* Create expected result */
  939. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  940. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  941. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  942. /* Clear surface. */
  943. clearScreen();
  944. /* Set the logical size and do a fill operation */
  945. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  946. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  947. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  948. SDLTest_AssertCheck(
  949. set_w == (w / factor) &&
  950. set_h == (h / factor) &&
  951. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  952. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  953. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  954. SDLTest_AssertCheck(
  955. set_rect.x == 0.0f &&
  956. set_rect.y == 0.0f &&
  957. set_rect.w == 320.0f &&
  958. set_rect.h == 240.0f,
  959. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  960. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  961. rect.x = (float)viewport.x / factor;
  962. rect.y = (float)viewport.y / factor;
  963. rect.w = (float)viewport.w / factor;
  964. rect.h = (float)viewport.h / factor;
  965. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  966. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  967. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  968. SDLTest_AssertCheck(
  969. set_w == 0 &&
  970. set_h == 0 &&
  971. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  972. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  973. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  974. SDLTest_AssertCheck(
  975. set_rect.x == 0.0f &&
  976. set_rect.y == 0.0f &&
  977. set_rect.w == 320.0f &&
  978. set_rect.h == 240.0f,
  979. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  980. /* Check to see if final image matches. */
  981. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  982. /* Clear surface. */
  983. clearScreen();
  984. /* Set the logical size and viewport and do a fill operation */
  985. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  986. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  987. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  988. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  989. viewport.w = TESTRENDER_SCREEN_W / factor;
  990. viewport.h = TESTRENDER_SCREEN_H / factor;
  991. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  992. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  993. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  994. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  995. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  996. /* Check to see if final image matches. */
  997. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  998. /*
  999. * Test a logical size that isn't the same aspect ratio as the window
  1000. */
  1001. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1002. viewport.y = 0;
  1003. viewport.w = TESTRENDER_SCREEN_W;
  1004. viewport.h = TESTRENDER_SCREEN_H;
  1005. /* Create expected result */
  1006. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1007. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1008. /* Clear surface. */
  1009. clearScreen();
  1010. /* Set the logical size and do a fill operation */
  1011. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1012. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1013. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1014. h,
  1015. SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1016. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1017. SDLTest_AssertCheck(
  1018. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1019. set_h == h &&
  1020. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1021. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1022. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1023. SDLTest_AssertCheck(
  1024. set_rect.x == 20.0f &&
  1025. set_rect.y == 0.0f &&
  1026. set_rect.w == 280.0f &&
  1027. set_rect.h == 240.0f,
  1028. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1029. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1030. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1031. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1032. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1033. SDLTest_AssertCheck(
  1034. set_w == 0 &&
  1035. set_h == 0 &&
  1036. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1037. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1038. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1039. SDLTest_AssertCheck(
  1040. set_rect.x == 0.0f &&
  1041. set_rect.y == 0.0f &&
  1042. set_rect.w == 320.0f &&
  1043. set_rect.h == 240.0f,
  1044. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1045. /* Check to see if final image matches. */
  1046. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1047. /* Clear surface. */
  1048. clearScreen();
  1049. /* Make current */
  1050. SDL_RenderPresent(renderer);
  1051. SDL_DestroySurface(referenceSurface);
  1052. return TEST_COMPLETED;
  1053. }
  1054. /* Helper functions */
  1055. /**
  1056. * Checks to see if functionality is supported. Helper function.
  1057. */
  1058. static bool isSupported(int code)
  1059. {
  1060. return (code != false);
  1061. }
  1062. /**
  1063. * Test to see if we can vary the draw color. Helper function.
  1064. *
  1065. * \sa SDL_SetRenderDrawColor
  1066. * \sa SDL_GetRenderDrawColor
  1067. */
  1068. static bool hasDrawColor(void)
  1069. {
  1070. int ret, fail;
  1071. Uint8 r, g, b, a;
  1072. fail = 0;
  1073. /* Set color. */
  1074. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1075. if (!isSupported(ret)) {
  1076. fail = 1;
  1077. }
  1078. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1079. if (!isSupported(ret)) {
  1080. fail = 1;
  1081. }
  1082. /* Restore natural. */
  1083. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1084. if (!isSupported(ret)) {
  1085. fail = 1;
  1086. }
  1087. /* Something failed, consider not available. */
  1088. if (fail) {
  1089. return false;
  1090. }
  1091. /* Not set properly, consider failed. */
  1092. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1093. return false;
  1094. }
  1095. return true;
  1096. }
  1097. /**
  1098. * Loads the test image 'Face' as texture. Helper function.
  1099. *
  1100. * \sa SDL_CreateTextureFromSurface
  1101. */
  1102. static SDL_Texture *
  1103. loadTestFace(void)
  1104. {
  1105. SDL_Surface *face;
  1106. SDL_Texture *tface;
  1107. face = SDLTest_ImageFace();
  1108. if (!face) {
  1109. return NULL;
  1110. }
  1111. tface = SDL_CreateTextureFromSurface(renderer, face);
  1112. if (!tface) {
  1113. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1114. }
  1115. SDL_DestroySurface(face);
  1116. return tface;
  1117. }
  1118. /**
  1119. * Compares screen pixels with image pixels. Helper function.
  1120. *
  1121. * \param referenceSurface Image to compare against.
  1122. * \param allowable_error allowed difference from the reference image
  1123. *
  1124. * \sa SDL_RenderReadPixels
  1125. * \sa SDL_CreateSurfaceFrom
  1126. * \sa SDL_DestroySurface
  1127. */
  1128. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1129. {
  1130. int ret;
  1131. SDL_Rect rect;
  1132. SDL_Surface *surface, *testSurface;
  1133. /* Explicitly specify the rect in case the window isn't the expected size... */
  1134. rect.x = 0;
  1135. rect.y = 0;
  1136. rect.w = TESTRENDER_SCREEN_W;
  1137. rect.h = TESTRENDER_SCREEN_H;
  1138. surface = SDL_RenderReadPixels(renderer, &rect);
  1139. if (!surface) {
  1140. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1141. return;
  1142. }
  1143. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1144. SDL_DestroySurface(surface);
  1145. if (!testSurface) {
  1146. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1147. return;
  1148. }
  1149. /* Compare surface. */
  1150. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1151. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1152. /* Clean up. */
  1153. SDL_DestroySurface(testSurface);
  1154. }
  1155. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1156. {
  1157. int ret;
  1158. SDL_Rect rect;
  1159. SDL_Surface *surface, *testSurface;
  1160. /* Explicitly specify the rect in case the window isn't the expected size... */
  1161. rect.x = 0;
  1162. rect.y = 0;
  1163. rect.w = TESTRENDER_SCREEN_W * 2;
  1164. rect.h = TESTRENDER_SCREEN_H * 2;
  1165. surface = SDL_RenderReadPixels(renderer, &rect);
  1166. if (!surface) {
  1167. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1168. return;
  1169. }
  1170. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1171. SDL_DestroySurface(surface);
  1172. if (!testSurface) {
  1173. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1174. return;
  1175. }
  1176. /* Compare surface. */
  1177. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1178. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1179. /* Clean up. */
  1180. SDL_DestroySurface(testSurface);
  1181. }
  1182. /**
  1183. * Clears the screen. Helper function.
  1184. *
  1185. * \sa SDL_SetRenderDrawColor
  1186. * \sa SDL_RenderClear
  1187. * \sa SDL_RenderPresent
  1188. * \sa SDL_SetRenderDrawBlendMode
  1189. */
  1190. static int
  1191. clearScreen(void)
  1192. {
  1193. int ret;
  1194. /* Make current */
  1195. SDL_RenderPresent(renderer);
  1196. /* Set color. */
  1197. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1198. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1199. /* Clear screen. */
  1200. ret = SDL_RenderClear(renderer);
  1201. SDLTest_AssertCheck(ret == true, "Validate result from SDL_RenderClear, expected: true, got: %i", ret);
  1202. /* Set defaults. */
  1203. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1204. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawBlendMode, expected: true, got: %i", ret);
  1205. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1206. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1207. return 0;
  1208. }
  1209. /**
  1210. * Tests geometry UV wrapping
  1211. */
  1212. static int SDLCALL render_testUVWrapping(void *arg)
  1213. {
  1214. SDL_Vertex vertices[6];
  1215. SDL_Vertex *verts = vertices;
  1216. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1217. SDL_FRect rect;
  1218. float min_U = -0.5f;
  1219. float max_U = 1.5f;
  1220. float min_V = -0.5f;
  1221. float max_V = 1.5f;
  1222. SDL_Texture *tface;
  1223. SDL_Surface *referenceSurface = NULL;
  1224. /* Clear surface. */
  1225. clearScreen();
  1226. /* Create face surface. */
  1227. tface = loadTestFace();
  1228. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1229. if (tface == NULL) {
  1230. return TEST_ABORTED;
  1231. }
  1232. rect.w = (float)tface->w * 2;
  1233. rect.h = (float)tface->h * 2;
  1234. rect.x = (float)(TESTRENDER_SCREEN_W - rect.w) / 2;
  1235. rect.y = (float)(TESTRENDER_SCREEN_H - rect.h) / 2;
  1236. /*
  1237. * 0--1
  1238. * | /|
  1239. * |/ |
  1240. * 3--2
  1241. *
  1242. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1243. */
  1244. /* 0 */
  1245. verts->position.x = rect.x;
  1246. verts->position.y = rect.y;
  1247. verts->color = color;
  1248. verts->tex_coord.x = min_U;
  1249. verts->tex_coord.y = min_V;
  1250. verts++;
  1251. /* 1 */
  1252. verts->position.x = rect.x + rect.w;
  1253. verts->position.y = rect.y;
  1254. verts->color = color;
  1255. verts->tex_coord.x = max_U;
  1256. verts->tex_coord.y = min_V;
  1257. verts++;
  1258. /* 2 */
  1259. verts->position.x = rect.x + rect.w;
  1260. verts->position.y = rect.y + rect.h;
  1261. verts->color = color;
  1262. verts->tex_coord.x = max_U;
  1263. verts->tex_coord.y = max_V;
  1264. verts++;
  1265. /* 0 */
  1266. verts->position.x = rect.x;
  1267. verts->position.y = rect.y;
  1268. verts->color = color;
  1269. verts->tex_coord.x = min_U;
  1270. verts->tex_coord.y = min_V;
  1271. verts++;
  1272. /* 2 */
  1273. verts->position.x = rect.x + rect.w;
  1274. verts->position.y = rect.y + rect.h;
  1275. verts->color = color;
  1276. verts->tex_coord.x = max_U;
  1277. verts->tex_coord.y = max_V;
  1278. verts++;
  1279. /* 3 */
  1280. verts->position.x = rect.x;
  1281. verts->position.y = rect.y + rect.h;
  1282. verts->color = color;
  1283. verts->tex_coord.x = min_U;
  1284. verts->tex_coord.y = max_V;
  1285. verts++;
  1286. /* Blit sprites as triangles onto the screen */
  1287. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1288. /* See if it's the same */
  1289. referenceSurface = SDLTest_ImageWrappingSprite();
  1290. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1291. /* Make current */
  1292. SDL_RenderPresent(renderer);
  1293. /* Clean up. */
  1294. SDL_DestroyTexture(tface);
  1295. SDL_DestroySurface(referenceSurface);
  1296. referenceSurface = NULL;
  1297. return TEST_COMPLETED;
  1298. }
  1299. /* ================= Test References ================== */
  1300. /* Render test cases */
  1301. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1302. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1303. };
  1304. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1305. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1306. };
  1307. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1308. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1309. };
  1310. static const SDLTest_TestCaseReference renderTestBlit = {
  1311. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1312. };
  1313. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1314. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1315. };
  1316. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  1317. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  1318. };
  1319. static const SDLTest_TestCaseReference renderTestBlitColor = {
  1320. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  1321. };
  1322. static const SDLTest_TestCaseReference renderTestBlendModes = {
  1323. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  1324. };
  1325. static const SDLTest_TestCaseReference renderTestViewport = {
  1326. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  1327. };
  1328. static const SDLTest_TestCaseReference renderTestClipRect = {
  1329. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  1330. };
  1331. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  1332. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  1333. };
  1334. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  1335. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  1336. };
  1337. /* Sequence of Render test cases */
  1338. static const SDLTest_TestCaseReference *renderTests[] = {
  1339. &renderTestGetNumRenderDrivers,
  1340. &renderTestPrimitives,
  1341. &renderTestPrimitivesWithViewport,
  1342. &renderTestBlit,
  1343. &renderTestBlitTiled,
  1344. &renderTestBlit9Grid,
  1345. &renderTestBlitColor,
  1346. &renderTestBlendModes,
  1347. &renderTestViewport,
  1348. &renderTestClipRect,
  1349. &renderTestLogicalSize,
  1350. &renderTestUVWrapping,
  1351. NULL
  1352. };
  1353. /* Render test suite (global) */
  1354. SDLTest_TestSuiteReference renderTestSuite = {
  1355. "Render",
  1356. InitCreateRenderer,
  1357. renderTests,
  1358. CleanupDestroyRenderer
  1359. };