testautomation_render.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. float tw, th;
  208. float i, j, ni, nj;
  209. int checkFailCount1;
  210. /* Clear surface. */
  211. clearScreen();
  212. /* Need drawcolor or just skip test. */
  213. SDLTest_AssertCheck(hasDrawColor(), "hasDrawColor)");
  214. /* Create face surface. */
  215. tface = loadTestFace();
  216. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  217. if (tface == NULL) {
  218. return TEST_ABORTED;
  219. }
  220. /* Constant values. */
  221. CHECK_FUNC(SDL_GetTextureSize, (tface, &tw, &th))
  222. rect.w = tw;
  223. rect.h = th;
  224. ni = TESTRENDER_SCREEN_W - tw;
  225. nj = TESTRENDER_SCREEN_H - th;
  226. /* Loop blit. */
  227. checkFailCount1 = 0;
  228. for (j = 0; j <= nj; j += 4) {
  229. for (i = 0; i <= ni; i += 4) {
  230. /* Blitting. */
  231. rect.x = i;
  232. rect.y = j;
  233. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  234. if (!ret) {
  235. checkFailCount1++;
  236. }
  237. }
  238. }
  239. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount1);
  240. /* See if it's the same */
  241. referenceSurface = SDLTest_ImageBlit();
  242. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  243. /* Make current */
  244. SDL_RenderPresent(renderer);
  245. /* Clean up. */
  246. SDL_DestroyTexture(tface);
  247. SDL_DestroySurface(referenceSurface);
  248. referenceSurface = NULL;
  249. return TEST_COMPLETED;
  250. }
  251. /**
  252. * Tests tiled blitting routines.
  253. */
  254. static int SDLCALL render_testBlitTiled(void *arg)
  255. {
  256. int ret;
  257. SDL_FRect rect;
  258. SDL_Texture *tface;
  259. SDL_Surface *referenceSurface = NULL;
  260. SDL_Surface *referenceSurface2x = NULL;
  261. /* Create face surface. */
  262. tface = loadTestFace();
  263. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  264. if (tface == NULL) {
  265. return TEST_ABORTED;
  266. }
  267. SDL_SetTextureScaleMode(tface, SDL_SCALEMODE_NEAREST); /* So 2x scaling is pixel perfect */
  268. /* Tiled blit - 1.0 scale */
  269. {
  270. /* Clear surface. */
  271. clearScreen();
  272. /* Tiled blit. */
  273. rect.x = 0.0f;
  274. rect.y = 0.0f;
  275. rect.w = (float)TESTRENDER_SCREEN_W;
  276. rect.h = (float)TESTRENDER_SCREEN_H;
  277. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 1.0f, &rect);
  278. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  279. /* See if it's the same */
  280. referenceSurface = SDLTest_ImageBlitTiled();
  281. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  282. /* Make current */
  283. SDL_RenderPresent(renderer);
  284. }
  285. /* Tiled blit - 2.0 scale */
  286. {
  287. /* Clear surface. */
  288. clearScreen();
  289. /* Tiled blit. */
  290. rect.x = 0.0f;
  291. rect.y = 0.0f;
  292. rect.w = (float)TESTRENDER_SCREEN_W * 2;
  293. rect.h = (float)TESTRENDER_SCREEN_H * 2;
  294. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 2.0f, &rect);
  295. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  296. /* See if it's the same */
  297. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  298. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  299. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: 0, got: %i", ret);
  300. compare2x(referenceSurface2x, ALLOWABLE_ERROR_OPAQUE);
  301. /* Make current */
  302. SDL_RenderPresent(renderer);
  303. }
  304. /* Clean up. */
  305. SDL_DestroyTexture(tface);
  306. SDL_DestroySurface(referenceSurface);
  307. SDL_DestroySurface(referenceSurface2x);
  308. referenceSurface = NULL;
  309. return TEST_COMPLETED;
  310. }
  311. static const Uint8 COLOR_SEPARATION = 85;
  312. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  313. {
  314. SDL_Rect rect;
  315. // Upper left
  316. rect.x = 0;
  317. rect.y = 0;
  318. rect.w = left_width;
  319. rect.h = top_height;
  320. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  321. // Top
  322. rect.x = left_width;
  323. rect.y = 0;
  324. rect.w = surface->w - left_width - right_width;
  325. rect.h = top_height;
  326. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  327. // Upper right
  328. rect.x = surface->w - right_width;
  329. rect.y = 0;
  330. rect.w = right_width;
  331. rect.h = top_height;
  332. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  333. // Left
  334. rect.x = 0;
  335. rect.y = top_height;
  336. rect.w = left_width;
  337. rect.h = surface->h - top_height - bottom_height;
  338. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  339. // Center
  340. rect.x = left_width;
  341. rect.y = top_height;
  342. rect.w = surface->w - right_width - left_width;
  343. rect.h = surface->h - top_height - bottom_height;
  344. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  345. // Right
  346. rect.x = surface->w - right_width;
  347. rect.y = top_height;
  348. rect.w = right_width;
  349. rect.h = surface->h - top_height - bottom_height;
  350. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  351. // Lower left
  352. rect.x = 0;
  353. rect.y = surface->h - bottom_height;
  354. rect.w = left_width;
  355. rect.h = bottom_height;
  356. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  357. // Bottom
  358. rect.x = left_width;
  359. rect.y = surface->h - bottom_height;
  360. rect.w = surface->w - left_width - right_width;
  361. rect.h = bottom_height;
  362. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  363. // Lower right
  364. rect.x = surface->w - right_width;
  365. rect.y = surface->h - bottom_height;
  366. rect.w = right_width;
  367. rect.h = bottom_height;
  368. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  369. }
  370. /**
  371. * Tests 9-grid blitting.
  372. */
  373. static int SDLCALL render_testBlit9Grid(void *arg)
  374. {
  375. SDL_Surface *referenceSurface = NULL;
  376. SDL_Surface *source = NULL;
  377. SDL_Texture *texture;
  378. int x, y;
  379. SDL_FRect rect;
  380. int ret = 0;
  381. /* Create source surface */
  382. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  383. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  384. for (y = 0; y < 3; ++y) {
  385. for (x = 0; x < 3; ++x) {
  386. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  387. }
  388. }
  389. texture = SDL_CreateTextureFromSurface(renderer, source);
  390. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  391. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  392. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  393. /* 9-grid blit - 1.0 scale */
  394. {
  395. SDLTest_Log("9-grid blit - 1.0 scale");
  396. /* Create reference surface */
  397. SDL_DestroySurface(referenceSurface);
  398. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  399. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  400. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  401. /* Clear surface. */
  402. clearScreen();
  403. /* Tiled blit. */
  404. rect.x = 0.0f;
  405. rect.y = 0.0f;
  406. rect.w = (float)TESTRENDER_SCREEN_W;
  407. rect.h = (float)TESTRENDER_SCREEN_H;
  408. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, &rect);
  409. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  410. /* See if it's the same */
  411. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  412. /* Make current */
  413. SDL_RenderPresent(renderer);
  414. }
  415. /* 9-grid blit - 2.0 scale */
  416. {
  417. SDLTest_Log("9-grid blit - 2.0 scale");
  418. /* Create reference surface */
  419. SDL_DestroySurface(referenceSurface);
  420. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  421. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  422. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  423. /* Clear surface. */
  424. clearScreen();
  425. /* Tiled blit. */
  426. rect.x = 0.0f;
  427. rect.y = 0.0f;
  428. rect.w = (float)TESTRENDER_SCREEN_W;
  429. rect.h = (float)TESTRENDER_SCREEN_H;
  430. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, &rect);
  431. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  432. /* See if it's the same */
  433. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  434. /* Make current */
  435. SDL_RenderPresent(renderer);
  436. }
  437. /* Clean up. */
  438. SDL_DestroySurface(source);
  439. SDL_DestroyTexture(texture);
  440. /* Create complex source surface */
  441. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  442. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  443. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  444. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  445. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  446. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  447. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  448. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  449. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  450. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  451. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  452. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  453. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  454. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  455. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  456. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  457. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  458. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  459. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  460. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  461. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  462. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  463. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  464. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  465. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  466. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  467. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  468. texture = SDL_CreateTextureFromSurface(renderer, source);
  469. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  470. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  471. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  472. /* complex 9-grid blit - 1.0 scale */
  473. {
  474. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  475. /* Create reference surface */
  476. SDL_DestroySurface(referenceSurface);
  477. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  478. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  479. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  480. /* Clear surface. */
  481. clearScreen();
  482. /* Tiled blit. */
  483. rect.x = 0.0f;
  484. rect.y = 0.0f;
  485. rect.w = (float)TESTRENDER_SCREEN_W;
  486. rect.h = (float)TESTRENDER_SCREEN_H;
  487. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, &rect);
  488. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  489. /* See if it's the same */
  490. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  491. /* Make current */
  492. SDL_RenderPresent(renderer);
  493. }
  494. /* complex 9-grid blit - 2.0 scale */
  495. {
  496. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  497. /* Create reference surface */
  498. SDL_DestroySurface(referenceSurface);
  499. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  500. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  501. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  502. /* Clear surface. */
  503. clearScreen();
  504. /* Tiled blit. */
  505. rect.x = 0.0f;
  506. rect.y = 0.0f;
  507. rect.w = (float)TESTRENDER_SCREEN_W;
  508. rect.h = (float)TESTRENDER_SCREEN_H;
  509. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, &rect);
  510. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  511. /* See if it's the same */
  512. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  513. /* Make current */
  514. SDL_RenderPresent(renderer);
  515. }
  516. /* Clean up. */
  517. SDL_DestroySurface(referenceSurface);
  518. SDL_DestroySurface(source);
  519. SDL_DestroyTexture(texture);
  520. return TEST_COMPLETED;
  521. }
  522. /**
  523. * Blits doing color tests.
  524. *
  525. * \sa SDL_SetTextureColorMod
  526. * \sa SDL_RenderTexture
  527. * \sa SDL_DestroyTexture
  528. */
  529. static int SDLCALL render_testBlitColor(void *arg)
  530. {
  531. int ret;
  532. SDL_FRect rect;
  533. SDL_Texture *tface;
  534. SDL_Surface *referenceSurface = NULL;
  535. float tw, th;
  536. int i, j, ni, nj;
  537. int checkFailCount1;
  538. int checkFailCount2;
  539. /* Clear surface. */
  540. clearScreen();
  541. /* Create face surface. */
  542. tface = loadTestFace();
  543. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  544. if (tface == NULL) {
  545. return TEST_ABORTED;
  546. }
  547. /* Constant values. */
  548. CHECK_FUNC(SDL_GetTextureSize, (tface, &tw, &th))
  549. rect.w = tw;
  550. rect.h = th;
  551. ni = TESTRENDER_SCREEN_W - (int)tw;
  552. nj = TESTRENDER_SCREEN_H - (int)th;
  553. /* Test blitting with color mod. */
  554. checkFailCount1 = 0;
  555. checkFailCount2 = 0;
  556. for (j = 0; j <= nj; j += 4) {
  557. for (i = 0; i <= ni; i += 4) {
  558. /* Set color mod. */
  559. ret = SDL_SetTextureColorMod(tface, (Uint8)((255 / nj) * j), (Uint8)((255 / ni) * i), (Uint8)((255 / nj) * j));
  560. if (!ret) {
  561. checkFailCount1++;
  562. }
  563. /* Blitting. */
  564. rect.x = (float)i;
  565. rect.y = (float)j;
  566. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  567. if (!ret) {
  568. checkFailCount2++;
  569. }
  570. }
  571. }
  572. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  573. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount2);
  574. /* See if it's the same. */
  575. referenceSurface = SDLTest_ImageBlitColor();
  576. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  577. /* Make current */
  578. SDL_RenderPresent(renderer);
  579. /* Clean up. */
  580. SDL_DestroyTexture(tface);
  581. SDL_DestroySurface(referenceSurface);
  582. referenceSurface = NULL;
  583. return TEST_COMPLETED;
  584. }
  585. typedef enum TestRenderOperation
  586. {
  587. TEST_RENDER_POINT,
  588. TEST_RENDER_LINE,
  589. TEST_RENDER_RECT,
  590. TEST_RENDER_COPY_XRGB,
  591. TEST_RENDER_COPY_ARGB,
  592. } TestRenderOperation;
  593. /**
  594. * Helper that tests a specific operation and blend mode, -1 for color mod, -2 for alpha mod
  595. */
  596. static void testBlendModeOperation(TestRenderOperation op, int mode, SDL_PixelFormat dst_format)
  597. {
  598. /* Allow up to 2 delta from theoretical value to account for rounding error.
  599. * We allow 2 rounding errors because the software renderer breaks drawing operations into alpha multiplication and a separate blend operation.
  600. */
  601. const int MAXIMUM_ERROR = 2;
  602. int ret;
  603. SDL_Texture *src = NULL;
  604. SDL_Texture *dst;
  605. SDL_Surface *result;
  606. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  607. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  608. Uint8 expectedR, expectedG, expectedB, expectedA;
  609. Uint8 actualR, actualG, actualB, actualA;
  610. int deltaR, deltaG, deltaB, deltaA;
  611. const char *operation = "UNKNOWN";
  612. const char *mode_name = "UNKNOWN";
  613. /* Create dst surface */
  614. dst = SDL_CreateTexture(renderer, dst_format, SDL_TEXTUREACCESS_TARGET, 3, 3);
  615. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  616. if (dst == NULL) {
  617. return;
  618. }
  619. if (SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  620. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  621. ret = SDL_GetTextureBlendMode(dst, &blendMode);
  622. SDLTest_AssertCheck(ret == true, "Verify result from SDL_GetTextureBlendMode(), expected: true, got: %i", ret);
  623. SDLTest_AssertCheck(blendMode == SDL_BLENDMODE_BLEND, "Verify alpha texture blend mode, expected %d, got %" SDL_PRIu32, SDL_BLENDMODE_BLEND, blendMode);
  624. }
  625. /* Set as render target */
  626. SDL_SetRenderTarget(renderer, dst);
  627. /* Clear surface. */
  628. if (!SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  629. dstA = 255;
  630. }
  631. ret = SDL_SetRenderDrawColor(renderer, dstR, dstG, dstB, dstA);
  632. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  633. ret = SDL_RenderClear(renderer);
  634. SDLTest_AssertPass("Call to SDL_RenderClear()");
  635. SDLTest_AssertCheck(ret == true, "Verify result from SDL_RenderClear, expected: true, got: %i", ret);
  636. if (op == TEST_RENDER_COPY_XRGB || op == TEST_RENDER_COPY_ARGB) {
  637. Uint8 pixels[4];
  638. /* Create src surface */
  639. src = SDL_CreateTexture(renderer, op == TEST_RENDER_COPY_XRGB ? SDL_PIXELFORMAT_RGBX32 : SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  640. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  641. if (src == NULL) {
  642. return;
  643. }
  644. /* Clear surface. */
  645. if (op == TEST_RENDER_COPY_XRGB) {
  646. srcA = 255;
  647. }
  648. pixels[0] = srcR;
  649. pixels[1] = srcG;
  650. pixels[2] = srcB;
  651. pixels[3] = srcA;
  652. SDL_UpdateTexture(src, NULL, pixels, sizeof(pixels));
  653. /* Set blend mode. */
  654. if (mode >= 0) {
  655. ret = SDL_SetTextureBlendMode(src, (SDL_BlendMode)mode);
  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. } else {
  659. ret = SDL_SetTextureBlendMode(src, SDL_BLENDMODE_BLEND);
  660. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  661. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  662. }
  663. } else {
  664. /* Set draw color */
  665. ret = SDL_SetRenderDrawColor(renderer, srcR, srcG, srcB, srcA);
  666. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  667. /* Set blend mode. */
  668. if (mode >= 0) {
  669. ret = SDL_SetRenderDrawBlendMode(renderer, (SDL_BlendMode)mode);
  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. } else {
  673. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  674. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  675. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  676. }
  677. }
  678. /* Test blend mode. */
  679. #define FLOAT(X) ((float)X / 255.0f)
  680. switch (mode) {
  681. case -1:
  682. mode_name = "color modulation";
  683. ret = SDL_SetTextureColorMod(src, srcR, srcG, srcB);
  684. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureColorMod, expected: true, got: %i", ret);
  685. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  686. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  687. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  688. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  689. break;
  690. case -2:
  691. mode_name = "alpha modulation";
  692. ret = SDL_SetTextureAlphaMod(src, srcA);
  693. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureAlphaMod, expected: true, got: %i", ret);
  694. 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);
  695. 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);
  696. 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);
  697. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  698. break;
  699. case SDL_BLENDMODE_NONE:
  700. mode_name = "SDL_BLENDMODE_NONE";
  701. expectedR = srcR;
  702. expectedG = srcG;
  703. expectedB = srcB;
  704. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  705. break;
  706. case SDL_BLENDMODE_BLEND:
  707. mode_name = "SDL_BLENDMODE_BLEND";
  708. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  709. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  710. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  711. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  712. break;
  713. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  714. mode_name = "SDL_BLENDMODE_BLEND_PREMULTIPLIED";
  715. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  716. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  717. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  718. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  719. break;
  720. case SDL_BLENDMODE_ADD:
  721. mode_name = "SDL_BLENDMODE_ADD";
  722. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  723. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  724. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  725. expectedA = dstA;
  726. break;
  727. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  728. mode_name = "SDL_BLENDMODE_ADD_PREMULTIPLIED";
  729. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  730. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  731. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  732. expectedA = dstA;
  733. break;
  734. case SDL_BLENDMODE_MOD:
  735. mode_name = "SDL_BLENDMODE_MOD";
  736. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  737. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  738. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  739. expectedA = dstA;
  740. break;
  741. case SDL_BLENDMODE_MUL:
  742. mode_name = "SDL_BLENDMODE_MUL";
  743. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  744. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  745. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  746. expectedA = dstA;
  747. break;
  748. default:
  749. SDLTest_LogError("Invalid blending mode: %d", mode);
  750. return;
  751. }
  752. switch (op) {
  753. case TEST_RENDER_POINT:
  754. operation = "render point";
  755. ret = SDL_RenderPoint(renderer, 0.0f, 0.0f);
  756. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", ret);
  757. break;
  758. case TEST_RENDER_LINE:
  759. operation = "render line";
  760. ret = SDL_RenderLine(renderer, 0.0f, 0.0f, 2.0f, 2.0f);
  761. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderLine, expected: true, got: %i", ret);
  762. break;
  763. case TEST_RENDER_RECT:
  764. operation = "render rect";
  765. ret = SDL_RenderFillRect(renderer, NULL);
  766. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderFillRect, expected: 0, got: %i", ret);
  767. break;
  768. case TEST_RENDER_COPY_XRGB:
  769. case TEST_RENDER_COPY_ARGB:
  770. operation = (op == TEST_RENDER_COPY_XRGB) ? "render XRGB" : "render ARGB";
  771. ret = SDL_RenderTexture(renderer, src, NULL, NULL);
  772. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderTexture, expected: true, got: %i", ret);
  773. break;
  774. default:
  775. SDLTest_LogError("Invalid blending operation: %d", op);
  776. return;
  777. }
  778. result = SDL_RenderReadPixels(renderer, NULL);
  779. SDL_ReadSurfacePixel(result, 0, 0, &actualR, &actualG, &actualB, &actualA);
  780. deltaR = SDL_abs((int)actualR - expectedR);
  781. deltaG = SDL_abs((int)actualG - expectedG);
  782. deltaB = SDL_abs((int)actualB - expectedB);
  783. deltaA = SDL_abs((int)actualA - expectedA);
  784. SDLTest_AssertCheck(
  785. deltaR <= MAXIMUM_ERROR &&
  786. deltaG <= MAXIMUM_ERROR &&
  787. deltaB <= MAXIMUM_ERROR &&
  788. deltaA <= MAXIMUM_ERROR,
  789. "Checking %s %s operation results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  790. operation, mode_name,
  791. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  792. /* Clean up */
  793. SDL_DestroySurface(result);
  794. SDL_DestroyTexture(src);
  795. SDL_DestroyTexture(dst);
  796. }
  797. static void testBlendMode(int mode)
  798. {
  799. const TestRenderOperation operations[] = {
  800. TEST_RENDER_POINT,
  801. TEST_RENDER_LINE,
  802. TEST_RENDER_RECT,
  803. TEST_RENDER_COPY_XRGB,
  804. TEST_RENDER_COPY_ARGB
  805. };
  806. const SDL_PixelFormat dst_formats[] = {
  807. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  808. };
  809. int i, j;
  810. for (i = 0; i < SDL_arraysize(operations); ++i) {
  811. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  812. TestRenderOperation op = operations[i];
  813. if (mode < 0) {
  814. if (op != TEST_RENDER_COPY_XRGB && op != TEST_RENDER_COPY_ARGB) {
  815. /* Unsupported mode for this operation */
  816. continue;
  817. }
  818. }
  819. testBlendModeOperation(op, mode, dst_formats[j]);
  820. }
  821. }
  822. }
  823. /**
  824. * Tests render operations with blend modes
  825. */
  826. static int SDLCALL render_testBlendModes(void *arg)
  827. {
  828. testBlendMode(-1);
  829. testBlendMode(-2);
  830. testBlendMode(SDL_BLENDMODE_NONE);
  831. testBlendMode(SDL_BLENDMODE_BLEND);
  832. testBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  833. testBlendMode(SDL_BLENDMODE_ADD);
  834. testBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  835. testBlendMode(SDL_BLENDMODE_MOD);
  836. testBlendMode(SDL_BLENDMODE_MUL);
  837. return TEST_COMPLETED;
  838. }
  839. /**
  840. * Test viewport
  841. */
  842. static int SDLCALL render_testViewport(void *arg)
  843. {
  844. SDL_Surface *referenceSurface;
  845. SDL_Rect viewport;
  846. viewport.x = TESTRENDER_SCREEN_W / 3;
  847. viewport.y = TESTRENDER_SCREEN_H / 3;
  848. viewport.w = TESTRENDER_SCREEN_W / 2;
  849. viewport.h = TESTRENDER_SCREEN_H / 2;
  850. /* Create expected result */
  851. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  852. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  853. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  854. /* Clear surface. */
  855. clearScreen();
  856. /* Set the viewport and do a fill operation */
  857. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  858. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  859. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  860. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  861. /* Check to see if final image matches. */
  862. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  863. /*
  864. * Verify that clear ignores the viewport
  865. */
  866. /* Create expected result */
  867. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  868. /* Clear surface. */
  869. clearScreen();
  870. /* Set the viewport and do a clear operation */
  871. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  872. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  873. CHECK_FUNC(SDL_RenderClear, (renderer))
  874. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  875. /* Check to see if final image matches. */
  876. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  877. /* Make current */
  878. SDL_RenderPresent(renderer);
  879. SDL_DestroySurface(referenceSurface);
  880. return TEST_COMPLETED;
  881. }
  882. /**
  883. * Test clip rect
  884. */
  885. static int SDLCALL render_testClipRect(void *arg)
  886. {
  887. SDL_Surface *referenceSurface;
  888. SDL_Rect cliprect;
  889. cliprect.x = TESTRENDER_SCREEN_W / 3;
  890. cliprect.y = TESTRENDER_SCREEN_H / 3;
  891. cliprect.w = TESTRENDER_SCREEN_W / 2;
  892. cliprect.h = TESTRENDER_SCREEN_H / 2;
  893. /* Create expected result */
  894. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  895. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  896. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &cliprect, RENDER_COLOR_GREEN))
  897. /* Clear surface. */
  898. clearScreen();
  899. /* Set the cliprect and do a fill operation */
  900. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  901. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  902. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  903. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  904. /* Check to see if final image matches. */
  905. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  906. /*
  907. * Verify that clear ignores the cliprect
  908. */
  909. /* Create expected result */
  910. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  911. /* Clear surface. */
  912. clearScreen();
  913. /* Set the cliprect and do a clear operation */
  914. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  915. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  916. CHECK_FUNC(SDL_RenderClear, (renderer))
  917. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  918. /* Check to see if final image matches. */
  919. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  920. /* Make current */
  921. SDL_RenderPresent(renderer);
  922. SDL_DestroySurface(referenceSurface);
  923. return TEST_COMPLETED;
  924. }
  925. /**
  926. * Test logical size
  927. */
  928. static int SDLCALL render_testLogicalSize(void *arg)
  929. {
  930. SDL_Surface *referenceSurface;
  931. SDL_Rect viewport;
  932. SDL_FRect rect;
  933. int w, h;
  934. int set_w, set_h;
  935. SDL_RendererLogicalPresentation set_presentation_mode;
  936. SDL_FRect set_rect;
  937. const int factor = 2;
  938. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  939. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  940. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  941. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  942. /* Create expected result */
  943. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  944. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  945. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  946. /* Clear surface. */
  947. clearScreen();
  948. /* Set the logical size and do a fill operation */
  949. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  950. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  951. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  952. SDLTest_AssertCheck(
  953. set_w == (w / factor) &&
  954. set_h == (h / factor) &&
  955. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  956. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  957. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  958. SDLTest_AssertCheck(
  959. set_rect.x == 0.0f &&
  960. set_rect.y == 0.0f &&
  961. set_rect.w == 320.0f &&
  962. set_rect.h == 240.0f,
  963. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  964. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  965. rect.x = (float)viewport.x / factor;
  966. rect.y = (float)viewport.y / factor;
  967. rect.w = (float)viewport.w / factor;
  968. rect.h = (float)viewport.h / factor;
  969. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  970. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  971. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  972. SDLTest_AssertCheck(
  973. set_w == 0 &&
  974. set_h == 0 &&
  975. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  976. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  977. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  978. SDLTest_AssertCheck(
  979. set_rect.x == 0.0f &&
  980. set_rect.y == 0.0f &&
  981. set_rect.w == 320.0f &&
  982. set_rect.h == 240.0f,
  983. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  984. /* Check to see if final image matches. */
  985. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  986. /* Clear surface. */
  987. clearScreen();
  988. /* Set the logical size and viewport and do a fill operation */
  989. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  990. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  991. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  992. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  993. viewport.w = TESTRENDER_SCREEN_W / factor;
  994. viewport.h = TESTRENDER_SCREEN_H / factor;
  995. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  996. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  997. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  998. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  999. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1000. /* Check to see if final image matches. */
  1001. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1002. /*
  1003. * Test a logical size that isn't the same aspect ratio as the window
  1004. */
  1005. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1006. viewport.y = 0;
  1007. viewport.w = TESTRENDER_SCREEN_W;
  1008. viewport.h = TESTRENDER_SCREEN_H;
  1009. /* Create expected result */
  1010. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1011. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1012. /* Clear surface. */
  1013. clearScreen();
  1014. /* Set the logical size and do a fill operation */
  1015. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1016. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1017. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1018. h,
  1019. SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1020. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1021. SDLTest_AssertCheck(
  1022. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1023. set_h == h &&
  1024. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1025. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1026. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1027. SDLTest_AssertCheck(
  1028. set_rect.x == 20.0f &&
  1029. set_rect.y == 0.0f &&
  1030. set_rect.w == 280.0f &&
  1031. set_rect.h == 240.0f,
  1032. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1033. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1034. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1035. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1036. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1037. SDLTest_AssertCheck(
  1038. set_w == 0 &&
  1039. set_h == 0 &&
  1040. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1041. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1042. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1043. SDLTest_AssertCheck(
  1044. set_rect.x == 0.0f &&
  1045. set_rect.y == 0.0f &&
  1046. set_rect.w == 320.0f &&
  1047. set_rect.h == 240.0f,
  1048. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1049. /* Check to see if final image matches. */
  1050. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1051. /* Clear surface. */
  1052. clearScreen();
  1053. /* Make current */
  1054. SDL_RenderPresent(renderer);
  1055. SDL_DestroySurface(referenceSurface);
  1056. return TEST_COMPLETED;
  1057. }
  1058. /* Helper functions */
  1059. /**
  1060. * Checks to see if functionality is supported. Helper function.
  1061. */
  1062. static bool isSupported(int code)
  1063. {
  1064. return (code != false);
  1065. }
  1066. /**
  1067. * Test to see if we can vary the draw color. Helper function.
  1068. *
  1069. * \sa SDL_SetRenderDrawColor
  1070. * \sa SDL_GetRenderDrawColor
  1071. */
  1072. static bool hasDrawColor(void)
  1073. {
  1074. int ret, fail;
  1075. Uint8 r, g, b, a;
  1076. fail = 0;
  1077. /* Set color. */
  1078. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1079. if (!isSupported(ret)) {
  1080. fail = 1;
  1081. }
  1082. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1083. if (!isSupported(ret)) {
  1084. fail = 1;
  1085. }
  1086. /* Restore natural. */
  1087. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1088. if (!isSupported(ret)) {
  1089. fail = 1;
  1090. }
  1091. /* Something failed, consider not available. */
  1092. if (fail) {
  1093. return false;
  1094. }
  1095. /* Not set properly, consider failed. */
  1096. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1097. return false;
  1098. }
  1099. return true;
  1100. }
  1101. /**
  1102. * Loads the test image 'Face' as texture. Helper function.
  1103. *
  1104. * \sa SDL_CreateTextureFromSurface
  1105. */
  1106. static SDL_Texture *
  1107. loadTestFace(void)
  1108. {
  1109. SDL_Surface *face;
  1110. SDL_Texture *tface;
  1111. face = SDLTest_ImageFace();
  1112. if (!face) {
  1113. return NULL;
  1114. }
  1115. tface = SDL_CreateTextureFromSurface(renderer, face);
  1116. if (!tface) {
  1117. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1118. }
  1119. SDL_DestroySurface(face);
  1120. return tface;
  1121. }
  1122. /**
  1123. * Compares screen pixels with image pixels. Helper function.
  1124. *
  1125. * \param referenceSurface Image to compare against.
  1126. * \param allowable_error allowed difference from the reference image
  1127. *
  1128. * \sa SDL_RenderReadPixels
  1129. * \sa SDL_CreateSurfaceFrom
  1130. * \sa SDL_DestroySurface
  1131. */
  1132. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1133. {
  1134. int ret;
  1135. SDL_Rect rect;
  1136. SDL_Surface *surface, *testSurface;
  1137. /* Explicitly specify the rect in case the window isn't the expected size... */
  1138. rect.x = 0;
  1139. rect.y = 0;
  1140. rect.w = TESTRENDER_SCREEN_W;
  1141. rect.h = TESTRENDER_SCREEN_H;
  1142. surface = SDL_RenderReadPixels(renderer, &rect);
  1143. if (!surface) {
  1144. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1145. return;
  1146. }
  1147. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1148. SDL_DestroySurface(surface);
  1149. if (!testSurface) {
  1150. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1151. return;
  1152. }
  1153. /* Compare surface. */
  1154. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1155. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1156. /* Clean up. */
  1157. SDL_DestroySurface(testSurface);
  1158. }
  1159. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1160. {
  1161. int ret;
  1162. SDL_Rect rect;
  1163. SDL_Surface *surface, *testSurface;
  1164. /* Explicitly specify the rect in case the window isn't the expected size... */
  1165. rect.x = 0;
  1166. rect.y = 0;
  1167. rect.w = TESTRENDER_SCREEN_W * 2;
  1168. rect.h = TESTRENDER_SCREEN_H * 2;
  1169. surface = SDL_RenderReadPixels(renderer, &rect);
  1170. if (!surface) {
  1171. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1172. return;
  1173. }
  1174. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1175. SDL_DestroySurface(surface);
  1176. if (!testSurface) {
  1177. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1178. return;
  1179. }
  1180. /* Compare surface. */
  1181. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1182. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1183. /* Clean up. */
  1184. SDL_DestroySurface(testSurface);
  1185. }
  1186. /**
  1187. * Clears the screen. Helper function.
  1188. *
  1189. * \sa SDL_SetRenderDrawColor
  1190. * \sa SDL_RenderClear
  1191. * \sa SDL_RenderPresent
  1192. * \sa SDL_SetRenderDrawBlendMode
  1193. */
  1194. static int
  1195. clearScreen(void)
  1196. {
  1197. int ret;
  1198. /* Make current */
  1199. SDL_RenderPresent(renderer);
  1200. /* Set color. */
  1201. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1202. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1203. /* Clear screen. */
  1204. ret = SDL_RenderClear(renderer);
  1205. SDLTest_AssertCheck(ret == true, "Validate result from SDL_RenderClear, expected: true, got: %i", ret);
  1206. /* Set defaults. */
  1207. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1208. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawBlendMode, expected: true, got: %i", ret);
  1209. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1210. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1211. return 0;
  1212. }
  1213. /**
  1214. * Tests geometry UV wrapping
  1215. */
  1216. static int SDLCALL render_testUVWrapping(void *arg)
  1217. {
  1218. SDL_Vertex vertices[6];
  1219. SDL_Vertex *verts = vertices;
  1220. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1221. float tw, th;
  1222. SDL_FRect rect;
  1223. float min_U = -0.5f;
  1224. float max_U = 1.5f;
  1225. float min_V = -0.5f;
  1226. float max_V = 1.5f;
  1227. SDL_Texture *tface;
  1228. SDL_Surface *referenceSurface = NULL;
  1229. /* Clear surface. */
  1230. clearScreen();
  1231. /* Create face surface. */
  1232. tface = loadTestFace();
  1233. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1234. if (tface == NULL) {
  1235. return TEST_ABORTED;
  1236. }
  1237. CHECK_FUNC(SDL_GetTextureSize, (tface, &tw, &th))
  1238. rect.w = tw * 2;
  1239. rect.h = th * 2;
  1240. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1241. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1242. /*
  1243. * 0--1
  1244. * | /|
  1245. * |/ |
  1246. * 3--2
  1247. *
  1248. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1249. */
  1250. /* 0 */
  1251. verts->position.x = rect.x;
  1252. verts->position.y = rect.y;
  1253. verts->color = color;
  1254. verts->tex_coord.x = min_U;
  1255. verts->tex_coord.y = min_V;
  1256. verts++;
  1257. /* 1 */
  1258. verts->position.x = rect.x + rect.w;
  1259. verts->position.y = rect.y;
  1260. verts->color = color;
  1261. verts->tex_coord.x = max_U;
  1262. verts->tex_coord.y = min_V;
  1263. verts++;
  1264. /* 2 */
  1265. verts->position.x = rect.x + rect.w;
  1266. verts->position.y = rect.y + rect.h;
  1267. verts->color = color;
  1268. verts->tex_coord.x = max_U;
  1269. verts->tex_coord.y = max_V;
  1270. verts++;
  1271. /* 0 */
  1272. verts->position.x = rect.x;
  1273. verts->position.y = rect.y;
  1274. verts->color = color;
  1275. verts->tex_coord.x = min_U;
  1276. verts->tex_coord.y = min_V;
  1277. verts++;
  1278. /* 2 */
  1279. verts->position.x = rect.x + rect.w;
  1280. verts->position.y = rect.y + rect.h;
  1281. verts->color = color;
  1282. verts->tex_coord.x = max_U;
  1283. verts->tex_coord.y = max_V;
  1284. verts++;
  1285. /* 3 */
  1286. verts->position.x = rect.x;
  1287. verts->position.y = rect.y + rect.h;
  1288. verts->color = color;
  1289. verts->tex_coord.x = min_U;
  1290. verts->tex_coord.y = max_V;
  1291. verts++;
  1292. /* Blit sprites as triangles onto the screen */
  1293. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1294. /* See if it's the same */
  1295. referenceSurface = SDLTest_ImageWrappingSprite();
  1296. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1297. /* Make current */
  1298. SDL_RenderPresent(renderer);
  1299. /* Clean up. */
  1300. SDL_DestroyTexture(tface);
  1301. SDL_DestroySurface(referenceSurface);
  1302. referenceSurface = NULL;
  1303. return TEST_COMPLETED;
  1304. }
  1305. /* ================= Test References ================== */
  1306. /* Render test cases */
  1307. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1308. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1309. };
  1310. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1311. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1312. };
  1313. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1314. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1315. };
  1316. static const SDLTest_TestCaseReference renderTestBlit = {
  1317. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1318. };
  1319. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1320. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1321. };
  1322. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  1323. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  1324. };
  1325. static const SDLTest_TestCaseReference renderTestBlitColor = {
  1326. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  1327. };
  1328. static const SDLTest_TestCaseReference renderTestBlendModes = {
  1329. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  1330. };
  1331. static const SDLTest_TestCaseReference renderTestViewport = {
  1332. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  1333. };
  1334. static const SDLTest_TestCaseReference renderTestClipRect = {
  1335. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  1336. };
  1337. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  1338. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  1339. };
  1340. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  1341. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  1342. };
  1343. /* Sequence of Render test cases */
  1344. static const SDLTest_TestCaseReference *renderTests[] = {
  1345. &renderTestGetNumRenderDrivers,
  1346. &renderTestPrimitives,
  1347. &renderTestPrimitivesWithViewport,
  1348. &renderTestBlit,
  1349. &renderTestBlitTiled,
  1350. &renderTestBlit9Grid,
  1351. &renderTestBlitColor,
  1352. &renderTestBlendModes,
  1353. &renderTestViewport,
  1354. &renderTestClipRect,
  1355. &renderTestLogicalSize,
  1356. &renderTestUVWrapping,
  1357. NULL
  1358. };
  1359. /* Render test suite (global) */
  1360. SDLTest_TestSuiteReference renderTestSuite = {
  1361. "Render",
  1362. InitCreateRenderer,
  1363. renderTests,
  1364. CleanupDestroyRenderer
  1365. };