testautomation_render.c 70 KB

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