testautomation_render.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. SDL_bool result = FUNC PARAMS; \
  20. if (!result) { \
  21. SDLTest_AssertCheck(result, "Validate result from %s, expected: SDL_TRUE, got: SDL_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 SDL_bool isSupported(int code);
  33. static SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTextureTiled, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTextureTiled, expected: SDL_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 == SDL_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 == SDL_TRUE, "Validate results from call to SDL_SetTextureScaleMode, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTexture9Grid, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTexture9Grid, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_SetTextureScaleMode, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTexture9Grid, expected: SDL_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 == SDL_TRUE, "Validate results from call to SDL_RenderTexture9Grid, expected: SDL_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 == SDL_TRUE, "Verify result from SDL_GetTextureBlendMode(), expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetRenderDrawColor(), expected: SDL_TRUE, got: %i", ret);
  633. ret = SDL_RenderClear(renderer);
  634. SDLTest_AssertPass("Call to SDL_RenderClear()");
  635. SDLTest_AssertCheck(ret == SDL_TRUE, "Verify result from SDL_RenderClear, expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetRenderDrawColor(), expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: SDL_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 == SDL_TRUE, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: SDL_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 == SDL_TRUE, "Validate results from calls to SDL_SetTextureColorMod, expected: SDL_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 == SDL_TRUE, "Validate results from calls to SDL_SetTextureAlphaMod, expected: SDL_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 == SDL_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 == SDL_TRUE, "Validate results from calls to SDL_RenderLine, expected: SDL_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 == SDL_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 == SDL_TRUE, "Validate results from calls to SDL_RenderTexture, expected: SDL_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_ScaleMode set_scale_mode;
  937. SDL_FRect set_rect;
  938. const int factor = 2;
  939. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  940. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  941. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  942. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  943. /* Create expected result */
  944. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  945. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  946. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  947. /* Clear surface. */
  948. clearScreen();
  949. /* Set the logical size and do a fill operation */
  950. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  951. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor,
  952. SDL_LOGICAL_PRESENTATION_LETTERBOX,
  953. SDL_SCALEMODE_NEAREST))
  954. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode, &set_scale_mode))
  955. SDLTest_AssertCheck(
  956. set_w == (w / factor) &&
  957. set_h == (h / factor) &&
  958. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX &&
  959. set_scale_mode == SDL_SCALEMODE_NEAREST,
  960. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d, %d", set_w, set_h, set_presentation_mode, set_scale_mode);
  961. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  962. SDLTest_AssertCheck(
  963. set_rect.x == 0.0f &&
  964. set_rect.y == 0.0f &&
  965. set_rect.w == 320.0f &&
  966. set_rect.h == 240.0f,
  967. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  968. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  969. rect.x = (float)viewport.x / factor;
  970. rect.y = (float)viewport.y / factor;
  971. rect.w = (float)viewport.w / factor;
  972. rect.h = (float)viewport.h / factor;
  973. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  974. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0,
  975. SDL_LOGICAL_PRESENTATION_DISABLED,
  976. SDL_SCALEMODE_NEAREST))
  977. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode, &set_scale_mode))
  978. SDLTest_AssertCheck(
  979. set_w == 0 &&
  980. set_h == 0 &&
  981. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED &&
  982. set_scale_mode == SDL_SCALEMODE_NEAREST,
  983. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d, %d", set_w, set_h, set_presentation_mode, set_scale_mode);
  984. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  985. SDLTest_AssertCheck(
  986. set_rect.x == 0.0f &&
  987. set_rect.y == 0.0f &&
  988. set_rect.w == 320.0f &&
  989. set_rect.h == 240.0f,
  990. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  991. /* Check to see if final image matches. */
  992. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  993. /* Clear surface. */
  994. clearScreen();
  995. /* Set the logical size and viewport and do a fill operation */
  996. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  997. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor,
  998. SDL_LOGICAL_PRESENTATION_LETTERBOX,
  999. SDL_SCALEMODE_NEAREST))
  1000. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  1001. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  1002. viewport.w = (TESTRENDER_SCREEN_W / 2) / factor;
  1003. viewport.h = (TESTRENDER_SCREEN_H / 2) / factor;
  1004. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1005. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1006. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1007. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1008. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0,
  1009. SDL_LOGICAL_PRESENTATION_DISABLED,
  1010. SDL_SCALEMODE_NEAREST))
  1011. /* Check to see if final image matches. */
  1012. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1013. /*
  1014. * Test a logical size that isn't the same aspect ratio as the window
  1015. */
  1016. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1017. viewport.y = 0;
  1018. viewport.w = TESTRENDER_SCREEN_W;
  1019. viewport.h = TESTRENDER_SCREEN_H;
  1020. /* Create expected result */
  1021. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1022. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1023. /* Clear surface. */
  1024. clearScreen();
  1025. /* Set the logical size and do a fill operation */
  1026. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1027. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1028. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1029. h,
  1030. SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1031. SDL_SCALEMODE_LINEAR))
  1032. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode, &set_scale_mode))
  1033. SDLTest_AssertCheck(
  1034. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1035. set_h == h &&
  1036. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX &&
  1037. set_scale_mode == SDL_SCALEMODE_LINEAR,
  1038. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d, %d", set_w, set_h, set_presentation_mode, set_scale_mode);
  1039. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1040. SDLTest_AssertCheck(
  1041. set_rect.x == 20.0f &&
  1042. set_rect.y == 0.0f &&
  1043. set_rect.w == 280.0f &&
  1044. set_rect.h == 240.0f,
  1045. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1046. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1047. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1048. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0,
  1049. SDL_LOGICAL_PRESENTATION_DISABLED,
  1050. SDL_SCALEMODE_NEAREST))
  1051. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode, &set_scale_mode))
  1052. SDLTest_AssertCheck(
  1053. set_w == 0 &&
  1054. set_h == 0 &&
  1055. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED &&
  1056. set_scale_mode == SDL_SCALEMODE_NEAREST,
  1057. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d, %d", set_w, set_h, set_presentation_mode, set_scale_mode);
  1058. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1059. SDLTest_AssertCheck(
  1060. set_rect.x == 0.0f &&
  1061. set_rect.y == 0.0f &&
  1062. set_rect.w == 320.0f &&
  1063. set_rect.h == 240.0f,
  1064. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1065. /* Check to see if final image matches. */
  1066. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1067. /* Clear surface. */
  1068. clearScreen();
  1069. /* Make current */
  1070. SDL_RenderPresent(renderer);
  1071. SDL_DestroySurface(referenceSurface);
  1072. return TEST_COMPLETED;
  1073. }
  1074. /* Helper functions */
  1075. /**
  1076. * Checks to see if functionality is supported. Helper function.
  1077. */
  1078. static SDL_bool isSupported(int code)
  1079. {
  1080. return (code != SDL_FALSE);
  1081. }
  1082. /**
  1083. * Test to see if we can vary the draw color. Helper function.
  1084. *
  1085. * \sa SDL_SetRenderDrawColor
  1086. * \sa SDL_GetRenderDrawColor
  1087. */
  1088. static SDL_bool hasDrawColor(void)
  1089. {
  1090. int ret, fail;
  1091. Uint8 r, g, b, a;
  1092. fail = 0;
  1093. /* Set color. */
  1094. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1095. if (!isSupported(ret)) {
  1096. fail = 1;
  1097. }
  1098. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1099. if (!isSupported(ret)) {
  1100. fail = 1;
  1101. }
  1102. /* Restore natural. */
  1103. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1104. if (!isSupported(ret)) {
  1105. fail = 1;
  1106. }
  1107. /* Something failed, consider not available. */
  1108. if (fail) {
  1109. return SDL_FALSE;
  1110. }
  1111. /* Not set properly, consider failed. */
  1112. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1113. return SDL_FALSE;
  1114. }
  1115. return SDL_TRUE;
  1116. }
  1117. /**
  1118. * Loads the test image 'Face' as texture. Helper function.
  1119. *
  1120. * \sa SDL_CreateTextureFromSurface
  1121. */
  1122. static SDL_Texture *
  1123. loadTestFace(void)
  1124. {
  1125. SDL_Surface *face;
  1126. SDL_Texture *tface;
  1127. face = SDLTest_ImageFace();
  1128. if (!face) {
  1129. return NULL;
  1130. }
  1131. tface = SDL_CreateTextureFromSurface(renderer, face);
  1132. if (!tface) {
  1133. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1134. }
  1135. SDL_DestroySurface(face);
  1136. return tface;
  1137. }
  1138. /**
  1139. * Compares screen pixels with image pixels. Helper function.
  1140. *
  1141. * \param referenceSurface Image to compare against.
  1142. * \param allowable_error allowed difference from the reference image
  1143. *
  1144. * \sa SDL_RenderReadPixels
  1145. * \sa SDL_CreateSurfaceFrom
  1146. * \sa SDL_DestroySurface
  1147. */
  1148. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1149. {
  1150. int ret;
  1151. SDL_Rect rect;
  1152. SDL_Surface *surface, *testSurface;
  1153. /* Explicitly specify the rect in case the window isn't the expected size... */
  1154. rect.x = 0;
  1155. rect.y = 0;
  1156. rect.w = TESTRENDER_SCREEN_W;
  1157. rect.h = TESTRENDER_SCREEN_H;
  1158. surface = SDL_RenderReadPixels(renderer, &rect);
  1159. if (!surface) {
  1160. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1161. return;
  1162. }
  1163. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1164. SDL_DestroySurface(surface);
  1165. if (!testSurface) {
  1166. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1167. return;
  1168. }
  1169. /* Compare surface. */
  1170. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1171. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1172. /* Clean up. */
  1173. SDL_DestroySurface(testSurface);
  1174. }
  1175. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1176. {
  1177. int ret;
  1178. SDL_Rect rect;
  1179. SDL_Surface *surface, *testSurface;
  1180. /* Explicitly specify the rect in case the window isn't the expected size... */
  1181. rect.x = 0;
  1182. rect.y = 0;
  1183. rect.w = TESTRENDER_SCREEN_W * 2;
  1184. rect.h = TESTRENDER_SCREEN_H * 2;
  1185. surface = SDL_RenderReadPixels(renderer, &rect);
  1186. if (!surface) {
  1187. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1188. return;
  1189. }
  1190. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1191. SDL_DestroySurface(surface);
  1192. if (!testSurface) {
  1193. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1194. return;
  1195. }
  1196. /* Compare surface. */
  1197. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1198. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1199. /* Clean up. */
  1200. SDL_DestroySurface(testSurface);
  1201. }
  1202. /**
  1203. * Clears the screen. Helper function.
  1204. *
  1205. * \sa SDL_SetRenderDrawColor
  1206. * \sa SDL_RenderClear
  1207. * \sa SDL_RenderPresent
  1208. * \sa SDL_SetRenderDrawBlendMode
  1209. */
  1210. static int
  1211. clearScreen(void)
  1212. {
  1213. int ret;
  1214. /* Make current */
  1215. SDL_RenderPresent(renderer);
  1216. /* Set color. */
  1217. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1218. SDLTest_AssertCheck(ret == SDL_TRUE, "Validate result from SDL_SetRenderDrawColor, expected: SDL_TRUE, got: %i", ret);
  1219. /* Clear screen. */
  1220. ret = SDL_RenderClear(renderer);
  1221. SDLTest_AssertCheck(ret == SDL_TRUE, "Validate result from SDL_RenderClear, expected: SDL_TRUE, got: %i", ret);
  1222. /* Set defaults. */
  1223. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1224. SDLTest_AssertCheck(ret == SDL_TRUE, "Validate result from SDL_SetRenderDrawBlendMode, expected: SDL_TRUE, got: %i", ret);
  1225. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1226. SDLTest_AssertCheck(ret == SDL_TRUE, "Validate result from SDL_SetRenderDrawColor, expected: SDL_TRUE, got: %i", ret);
  1227. return 0;
  1228. }
  1229. /**
  1230. * Tests geometry UV wrapping
  1231. */
  1232. static int SDLCALL render_testUVWrapping(void *arg)
  1233. {
  1234. SDL_Vertex vertices[6];
  1235. SDL_Vertex *verts = vertices;
  1236. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1237. float tw, th;
  1238. SDL_FRect rect;
  1239. float min_U = -0.5f;
  1240. float max_U = 1.5f;
  1241. float min_V = -0.5f;
  1242. float max_V = 1.5f;
  1243. SDL_Texture *tface;
  1244. SDL_Surface *referenceSurface = NULL;
  1245. /* Clear surface. */
  1246. clearScreen();
  1247. /* Create face surface. */
  1248. tface = loadTestFace();
  1249. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1250. if (tface == NULL) {
  1251. return TEST_ABORTED;
  1252. }
  1253. CHECK_FUNC(SDL_GetTextureSize, (tface, &tw, &th))
  1254. rect.w = tw * 2;
  1255. rect.h = th * 2;
  1256. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1257. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1258. /*
  1259. * 0--1
  1260. * | /|
  1261. * |/ |
  1262. * 3--2
  1263. *
  1264. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1265. */
  1266. /* 0 */
  1267. verts->position.x = rect.x;
  1268. verts->position.y = rect.y;
  1269. verts->color = color;
  1270. verts->tex_coord.x = min_U;
  1271. verts->tex_coord.y = min_V;
  1272. verts++;
  1273. /* 1 */
  1274. verts->position.x = rect.x + rect.w;
  1275. verts->position.y = rect.y;
  1276. verts->color = color;
  1277. verts->tex_coord.x = max_U;
  1278. verts->tex_coord.y = min_V;
  1279. verts++;
  1280. /* 2 */
  1281. verts->position.x = rect.x + rect.w;
  1282. verts->position.y = rect.y + rect.h;
  1283. verts->color = color;
  1284. verts->tex_coord.x = max_U;
  1285. verts->tex_coord.y = max_V;
  1286. verts++;
  1287. /* 0 */
  1288. verts->position.x = rect.x;
  1289. verts->position.y = rect.y;
  1290. verts->color = color;
  1291. verts->tex_coord.x = min_U;
  1292. verts->tex_coord.y = min_V;
  1293. verts++;
  1294. /* 2 */
  1295. verts->position.x = rect.x + rect.w;
  1296. verts->position.y = rect.y + rect.h;
  1297. verts->color = color;
  1298. verts->tex_coord.x = max_U;
  1299. verts->tex_coord.y = max_V;
  1300. verts++;
  1301. /* 3 */
  1302. verts->position.x = rect.x;
  1303. verts->position.y = rect.y + rect.h;
  1304. verts->color = color;
  1305. verts->tex_coord.x = min_U;
  1306. verts->tex_coord.y = max_V;
  1307. verts++;
  1308. /* Blit sprites as triangles onto the screen */
  1309. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1310. /* See if it's the same */
  1311. referenceSurface = SDLTest_ImageWrappingSprite();
  1312. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1313. /* Make current */
  1314. SDL_RenderPresent(renderer);
  1315. /* Clean up. */
  1316. SDL_DestroyTexture(tface);
  1317. SDL_DestroySurface(referenceSurface);
  1318. referenceSurface = NULL;
  1319. return TEST_COMPLETED;
  1320. }
  1321. /* ================= Test References ================== */
  1322. /* Render test cases */
  1323. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1324. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1325. };
  1326. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1327. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1328. };
  1329. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1330. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1331. };
  1332. static const SDLTest_TestCaseReference renderTestBlit = {
  1333. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1334. };
  1335. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1336. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1337. };
  1338. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  1339. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  1340. };
  1341. static const SDLTest_TestCaseReference renderTestBlitColor = {
  1342. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  1343. };
  1344. static const SDLTest_TestCaseReference renderTestBlendModes = {
  1345. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  1346. };
  1347. static const SDLTest_TestCaseReference renderTestViewport = {
  1348. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  1349. };
  1350. static const SDLTest_TestCaseReference renderTestClipRect = {
  1351. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  1352. };
  1353. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  1354. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  1355. };
  1356. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  1357. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  1358. };
  1359. /* Sequence of Render test cases */
  1360. static const SDLTest_TestCaseReference *renderTests[] = {
  1361. &renderTestGetNumRenderDrivers,
  1362. &renderTestPrimitives,
  1363. &renderTestPrimitivesWithViewport,
  1364. &renderTestBlit,
  1365. &renderTestBlitTiled,
  1366. &renderTestBlit9Grid,
  1367. &renderTestBlitColor,
  1368. &renderTestBlendModes,
  1369. &renderTestViewport,
  1370. &renderTestClipRect,
  1371. &renderTestLogicalSize,
  1372. &renderTestUVWrapping,
  1373. NULL
  1374. };
  1375. /* Render test suite (global) */
  1376. SDLTest_TestSuiteReference renderTestSuite = {
  1377. "Render",
  1378. InitCreateRenderer,
  1379. renderTests,
  1380. CleanupDestroyRenderer
  1381. };