testautomation_surface.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. /* Suppress C4996 VS compiler warnings for unlink() */
  6. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  7. #define _CRT_SECURE_NO_DEPRECATE
  8. #endif
  9. #if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
  10. #define _CRT_NONSTDC_NO_DEPRECATE
  11. #endif
  12. #include <stdio.h>
  13. #ifndef _MSC_VER
  14. #include <unistd.h>
  15. #endif
  16. #include <sys/stat.h>
  17. #include <SDL3/SDL.h>
  18. #include <SDL3/SDL_test.h>
  19. #include "testautomation_suites.h"
  20. #include "testautomation_images.h"
  21. #define CHECK_FUNC(FUNC, PARAMS) \
  22. { \
  23. int result = FUNC PARAMS; \
  24. if (result != 0) { \
  25. SDLTest_AssertCheck(result == 0, "Validate result from %s, expected: 0, got: %i, %s", #FUNC, result, SDL_GetError()); \
  26. } \
  27. }
  28. /* ================= Test Case Implementation ================== */
  29. /* Shared test surface */
  30. static SDL_Surface *referenceSurface = NULL;
  31. static SDL_Surface *testSurface = NULL;
  32. /* Fixture */
  33. /* Create a 32-bit writable surface for blitting tests */
  34. static void surfaceSetUp(void *arg)
  35. {
  36. int result;
  37. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  38. SDL_BlendMode currentBlendMode;
  39. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  40. testSurface = SDL_CreateSurface(referenceSurface->w, referenceSurface->h, SDL_PIXELFORMAT_RGBA32);
  41. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  42. if (testSurface != NULL) {
  43. /* Disable blend mode for target surface */
  44. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  45. SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result);
  46. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  47. SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result);
  48. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  49. }
  50. }
  51. static void surfaceTearDown(void *arg)
  52. {
  53. SDL_DestroySurface(referenceSurface);
  54. referenceSurface = NULL;
  55. SDL_DestroySurface(testSurface);
  56. testSurface = NULL;
  57. }
  58. /**
  59. * Helper that blits in a specific blend mode, -1 for color mod, -2 for alpha mod
  60. */
  61. static void testBlitBlendModeWithFormats(int mode, SDL_PixelFormat src_format, SDL_PixelFormat dst_format)
  62. {
  63. /* Allow up to 1 delta from theoretical value to account for rounding error */
  64. const int MAXIMUM_ERROR = 1;
  65. int ret;
  66. SDL_Surface *src;
  67. SDL_Surface *dst;
  68. int checkFailCount1;
  69. int checkFailCount2;
  70. int checkFailCount3;
  71. Uint32 color;
  72. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  73. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  74. Uint8 expectedR, expectedG, expectedB, expectedA;
  75. Uint8 actualR, actualG, actualB, actualA;
  76. int deltaR, deltaG, deltaB, deltaA;
  77. /* Create dst surface */
  78. dst = SDL_CreateSurface(1, 1, dst_format);
  79. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  80. if (dst == NULL) {
  81. return;
  82. }
  83. /* Clear surface. */
  84. color = SDL_MapSurfaceRGBA(dst, dstR, dstG, dstB, dstA);
  85. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  86. ret = SDL_FillSurfaceRect(dst, NULL, color);
  87. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  88. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  89. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(dst->format), NULL, &dstR, &dstG, &dstB, &dstA);
  90. /* Create src surface */
  91. src = SDL_CreateSurface(1, 1, src_format);
  92. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  93. if (src == NULL) {
  94. return;
  95. }
  96. /* Reset alpha modulation */
  97. ret = SDL_SetSurfaceAlphaMod(src, 255);
  98. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  99. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret);
  100. /* Reset color modulation */
  101. ret = SDL_SetSurfaceColorMod(src, 255, 255, 255);
  102. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  103. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret);
  104. /* Reset color key */
  105. ret = SDL_SetSurfaceColorKey(src, SDL_FALSE, 0);
  106. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  107. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey(), expected: 0, got: %i", ret);
  108. /* Clear surface. */
  109. color = SDL_MapSurfaceRGBA(src, srcR, srcG, srcB, srcA);
  110. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  111. ret = SDL_FillSurfaceRect(src, NULL, color);
  112. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  113. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  114. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(src->format), NULL, &srcR, &srcG, &srcB, &srcA);
  115. /* Set blend mode. */
  116. if (mode >= 0) {
  117. ret = SDL_SetSurfaceBlendMode(src, (SDL_BlendMode)mode);
  118. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  119. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  120. } else {
  121. ret = SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
  122. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  123. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  124. }
  125. /* Test blend mode. */
  126. #define FLOAT(X) ((float)X / 255.0f)
  127. checkFailCount1 = 0;
  128. checkFailCount2 = 0;
  129. checkFailCount3 = 0;
  130. switch (mode) {
  131. case -1:
  132. /* Set color mod. */
  133. ret = SDL_SetSurfaceColorMod(src, srcR, srcG, srcB);
  134. if (ret != 0) {
  135. checkFailCount2++;
  136. }
  137. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  138. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  139. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  140. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  141. break;
  142. case -2:
  143. /* Set alpha mod. */
  144. ret = SDL_SetSurfaceAlphaMod(src, srcA);
  145. if (ret != 0) {
  146. checkFailCount3++;
  147. }
  148. 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);
  149. 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);
  150. 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);
  151. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  152. break;
  153. case SDL_BLENDMODE_NONE:
  154. expectedR = srcR;
  155. expectedG = srcG;
  156. expectedB = srcB;
  157. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  158. break;
  159. case SDL_BLENDMODE_BLEND:
  160. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  161. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  162. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  163. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  164. break;
  165. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  166. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  167. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  168. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  169. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  170. break;
  171. case SDL_BLENDMODE_ADD:
  172. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  173. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  174. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  175. expectedA = dstA;
  176. break;
  177. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  178. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  179. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  180. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  181. expectedA = dstA;
  182. break;
  183. case SDL_BLENDMODE_MOD:
  184. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  185. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  186. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  187. expectedA = dstA;
  188. break;
  189. case SDL_BLENDMODE_MUL:
  190. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  191. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  192. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  193. expectedA = dstA;
  194. break;
  195. default:
  196. SDLTest_LogError("Invalid blending mode: %d", mode);
  197. return;
  198. }
  199. /* Blitting. */
  200. ret = SDL_BlitSurface(src, NULL, dst, NULL);
  201. if (ret != 0) {
  202. checkFailCount1++;
  203. }
  204. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i", checkFailCount1);
  205. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", checkFailCount2);
  206. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", checkFailCount3);
  207. SDL_ReadSurfacePixel(dst, 0, 0, &actualR, &actualG, &actualB, &actualA);
  208. deltaR = SDL_abs((int)actualR - expectedR);
  209. deltaG = SDL_abs((int)actualG - expectedG);
  210. deltaB = SDL_abs((int)actualB - expectedB);
  211. deltaA = SDL_abs((int)actualA - expectedA);
  212. SDLTest_AssertCheck(
  213. deltaR <= MAXIMUM_ERROR &&
  214. deltaG <= MAXIMUM_ERROR &&
  215. deltaB <= MAXIMUM_ERROR &&
  216. deltaA <= MAXIMUM_ERROR,
  217. "Checking %s -> %s blit results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  218. SDL_GetPixelFormatName(src_format),
  219. SDL_GetPixelFormatName(dst_format),
  220. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  221. /* Clean up */
  222. SDL_DestroySurface(src);
  223. SDL_DestroySurface(dst);
  224. }
  225. static void testBlitBlendMode(int mode)
  226. {
  227. const SDL_PixelFormat src_formats[] = {
  228. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  229. };
  230. const SDL_PixelFormat dst_formats[] = {
  231. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  232. };
  233. int i, j;
  234. for (i = 0; i < SDL_arraysize(src_formats); ++i) {
  235. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  236. testBlitBlendModeWithFormats(mode, src_formats[i], dst_formats[j]);
  237. }
  238. }
  239. }
  240. /* Helper to check that a file exists */
  241. static void AssertFileExist(const char *filename)
  242. {
  243. struct stat st;
  244. int ret = stat(filename, &st);
  245. SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
  246. }
  247. /* Test case functions */
  248. /**
  249. * Tests sprite saving and loading
  250. */
  251. static int surface_testSaveLoadBitmap(void *arg)
  252. {
  253. int ret;
  254. const char *sampleFilename = "testSaveLoadBitmap.bmp";
  255. SDL_Surface *face;
  256. SDL_Surface *rface;
  257. /* Create sample surface */
  258. face = SDLTest_ImageFace();
  259. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  260. if (face == NULL) {
  261. return TEST_ABORTED;
  262. }
  263. /* Delete test file; ignore errors */
  264. unlink(sampleFilename);
  265. /* Save a surface */
  266. ret = SDL_SaveBMP(face, sampleFilename);
  267. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  268. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SaveBMP, expected: 0, got: %i", ret);
  269. AssertFileExist(sampleFilename);
  270. /* Load a surface */
  271. rface = SDL_LoadBMP(sampleFilename);
  272. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  273. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  274. if (rface != NULL) {
  275. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  276. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  277. }
  278. /* Delete test file; ignore errors */
  279. unlink(sampleFilename);
  280. /* Clean up */
  281. SDL_DestroySurface(face);
  282. face = NULL;
  283. SDL_DestroySurface(rface);
  284. rface = NULL;
  285. return TEST_COMPLETED;
  286. }
  287. /**
  288. * Tests surface conversion.
  289. */
  290. static int surface_testSurfaceConversion(void *arg)
  291. {
  292. SDL_Surface *rface = NULL, *face = NULL;
  293. int ret = 0;
  294. /* Create sample surface */
  295. face = SDLTest_ImageFace();
  296. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  297. if (face == NULL) {
  298. return TEST_ABORTED;
  299. }
  300. /* Set transparent pixel as the pixel at (0,0) */
  301. if (SDL_GetSurfacePalette(face)) {
  302. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  303. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  304. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  305. }
  306. /* Convert to 32 bit to compare. */
  307. rface = SDL_ConvertSurface(face, testSurface->format);
  308. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  309. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  310. /* Compare surface. */
  311. ret = SDLTest_CompareSurfaces(rface, face, 0);
  312. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  313. /* Clean up. */
  314. SDL_DestroySurface(face);
  315. face = NULL;
  316. SDL_DestroySurface(rface);
  317. rface = NULL;
  318. return TEST_COMPLETED;
  319. }
  320. /**
  321. * Tests surface conversion across all pixel formats.
  322. */
  323. static int surface_testCompleteSurfaceConversion(void *arg)
  324. {
  325. Uint32 pixel_formats[] = {
  326. SDL_PIXELFORMAT_INDEX8,
  327. SDL_PIXELFORMAT_RGB332,
  328. SDL_PIXELFORMAT_XRGB4444,
  329. SDL_PIXELFORMAT_XBGR4444,
  330. SDL_PIXELFORMAT_XRGB1555,
  331. SDL_PIXELFORMAT_XBGR1555,
  332. SDL_PIXELFORMAT_ARGB4444,
  333. SDL_PIXELFORMAT_RGBA4444,
  334. SDL_PIXELFORMAT_ABGR4444,
  335. SDL_PIXELFORMAT_BGRA4444,
  336. SDL_PIXELFORMAT_ARGB1555,
  337. SDL_PIXELFORMAT_RGBA5551,
  338. SDL_PIXELFORMAT_ABGR1555,
  339. SDL_PIXELFORMAT_BGRA5551,
  340. SDL_PIXELFORMAT_RGB565,
  341. SDL_PIXELFORMAT_BGR565,
  342. SDL_PIXELFORMAT_RGB24,
  343. SDL_PIXELFORMAT_BGR24,
  344. SDL_PIXELFORMAT_XRGB8888,
  345. SDL_PIXELFORMAT_RGBX8888,
  346. SDL_PIXELFORMAT_XBGR8888,
  347. SDL_PIXELFORMAT_BGRX8888,
  348. SDL_PIXELFORMAT_ARGB8888,
  349. SDL_PIXELFORMAT_RGBA8888,
  350. SDL_PIXELFORMAT_ABGR8888,
  351. SDL_PIXELFORMAT_BGRA8888,
  352. #if 0 /* We aren't testing HDR10 colorspace conversion */
  353. SDL_PIXELFORMAT_XRGB2101010,
  354. SDL_PIXELFORMAT_XBGR2101010,
  355. SDL_PIXELFORMAT_ARGB2101010,
  356. SDL_PIXELFORMAT_ABGR2101010,
  357. #endif
  358. };
  359. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  360. const SDL_PixelFormatDetails *fmt1, *fmt2;
  361. int i, j, ret = 0;
  362. /* Create sample surface */
  363. face = SDLTest_ImageFace();
  364. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  365. if (face == NULL) {
  366. return TEST_ABORTED;
  367. }
  368. /* Set transparent pixel as the pixel at (0,0) */
  369. if (SDL_GetSurfacePalette(face)) {
  370. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  371. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  372. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  373. }
  374. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  375. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  376. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  377. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  378. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  379. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  380. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  381. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  382. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  383. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  384. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  385. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  386. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  387. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  388. if (fmt1 && fmt2 &&
  389. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  390. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  391. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  392. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  393. final = SDL_ConvertSurface(cvt2, face->format);
  394. SDL_assert(final != NULL);
  395. /* Compare surface. */
  396. ret = SDLTest_CompareSurfaces(face, final, 0);
  397. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  398. SDL_DestroySurface(final);
  399. }
  400. SDL_DestroySurface(cvt1);
  401. SDL_DestroySurface(cvt2);
  402. }
  403. }
  404. /* Clean up. */
  405. SDL_DestroySurface(face);
  406. return TEST_COMPLETED;
  407. }
  408. /**
  409. * Tests sprite loading. A failure case.
  410. */
  411. static int surface_testLoadFailure(void *arg)
  412. {
  413. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  414. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  415. return TEST_COMPLETED;
  416. }
  417. /**
  418. * Tests some blitting routines.
  419. */
  420. static int surface_testBlit(void *arg)
  421. {
  422. /* Basic blitting */
  423. testBlitBlendMode(SDL_BLENDMODE_NONE);
  424. return TEST_COMPLETED;
  425. }
  426. /**
  427. * Tests some blitting routines with color mod
  428. */
  429. static int surface_testBlitColorMod(void *arg)
  430. {
  431. /* Basic blitting with color mod */
  432. testBlitBlendMode(-1);
  433. return TEST_COMPLETED;
  434. }
  435. /**
  436. * Tests some blitting routines with alpha mod
  437. */
  438. static int surface_testBlitAlphaMod(void *arg)
  439. {
  440. /* Basic blitting with alpha mod */
  441. testBlitBlendMode(-2);
  442. return TEST_COMPLETED;
  443. }
  444. /**
  445. * Tests some more blitting routines.
  446. */
  447. static int surface_testBlitBlendBlend(void *arg)
  448. {
  449. /* Blend blitting */
  450. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  451. return TEST_COMPLETED;
  452. }
  453. /**
  454. * @brief Tests some more blitting routines.
  455. */
  456. static int surface_testBlitBlendPremultiplied(void *arg)
  457. {
  458. /* Blend premultiplied blitting */
  459. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  460. return TEST_COMPLETED;
  461. }
  462. /**
  463. * Tests some more blitting routines.
  464. */
  465. static int surface_testBlitBlendAdd(void *arg)
  466. {
  467. /* Add blitting */
  468. testBlitBlendMode(SDL_BLENDMODE_ADD);
  469. return TEST_COMPLETED;
  470. }
  471. /**
  472. * Tests some more blitting routines.
  473. */
  474. static int surface_testBlitBlendAddPremultiplied(void *arg)
  475. {
  476. /* Add premultiplied blitting */
  477. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  478. return TEST_COMPLETED;
  479. }
  480. /**
  481. * Tests some more blitting routines.
  482. */
  483. static int surface_testBlitBlendMod(void *arg)
  484. {
  485. /* Mod blitting */
  486. testBlitBlendMode(SDL_BLENDMODE_MOD);
  487. return TEST_COMPLETED;
  488. }
  489. /**
  490. * Tests some more blitting routines.
  491. */
  492. static int surface_testBlitBlendMul(void *arg)
  493. {
  494. /* Mod blitting */
  495. testBlitBlendMode(SDL_BLENDMODE_MUL);
  496. return TEST_COMPLETED;
  497. }
  498. static int surface_testOverflow(void *arg)
  499. {
  500. char buf[1024];
  501. const char *expectedError;
  502. SDL_Surface *surface;
  503. SDL_memset(buf, '\0', sizeof(buf));
  504. expectedError = "Parameter 'width' is invalid";
  505. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  506. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  507. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  508. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  509. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  510. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  511. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  512. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  513. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  514. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  515. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  516. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  517. expectedError = "Parameter 'height' is invalid";
  518. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  519. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  520. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  521. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  522. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  523. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  524. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  525. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  526. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  527. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  528. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  529. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  530. expectedError = "Parameter 'pitch' is invalid";
  531. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  532. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  533. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  534. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  535. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  536. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  537. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  538. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  539. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  540. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  541. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  542. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  543. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  544. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  545. surface != NULL ? "(success)" : SDL_GetError());
  546. SDL_DestroySurface(surface);
  547. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  548. * the width, but it must be enough to hold the appropriate number of
  549. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  550. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  551. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  552. surface != NULL ? "(success)" : SDL_GetError());
  553. SDL_DestroySurface(surface);
  554. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  555. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  556. surface != NULL ? "(success)" : SDL_GetError());
  557. SDL_DestroySurface(surface);
  558. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  559. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  560. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  561. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  562. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  563. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  564. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  565. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  566. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  567. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  568. surface != NULL ? "(success)" : SDL_GetError());
  569. SDL_DestroySurface(surface);
  570. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  571. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  572. surface != NULL ? "(success)" : SDL_GetError());
  573. SDL_DestroySurface(surface);
  574. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  575. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  576. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  577. surface != NULL ? "(success)" : SDL_GetError());
  578. SDL_DestroySurface(surface);
  579. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  580. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  581. surface != NULL ? "(success)" : SDL_GetError());
  582. SDL_DestroySurface(surface);
  583. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  584. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  585. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  586. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  587. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  588. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  589. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  590. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  591. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  592. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  593. surface != NULL ? "(success)" : SDL_GetError());
  594. SDL_DestroySurface(surface);
  595. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  596. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  597. surface != NULL ? "(success)" : SDL_GetError());
  598. SDL_DestroySurface(surface);
  599. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  600. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  601. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  602. surface != NULL ? "(success)" : SDL_GetError());
  603. SDL_DestroySurface(surface);
  604. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  605. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  606. surface != NULL ? "(success)" : SDL_GetError());
  607. SDL_DestroySurface(surface);
  608. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  609. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  610. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  611. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  612. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  613. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  614. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  615. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  616. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  617. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  618. surface != NULL ? "(success)" : SDL_GetError());
  619. SDL_DestroySurface(surface);
  620. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  621. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  622. surface != NULL ? "(success)" : SDL_GetError());
  623. SDL_DestroySurface(surface);
  624. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  625. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  626. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  627. surface != NULL ? "(success)" : SDL_GetError());
  628. SDL_DestroySurface(surface);
  629. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  630. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  631. surface != NULL ? "(success)" : SDL_GetError());
  632. SDL_DestroySurface(surface);
  633. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  634. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  635. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  636. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  637. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  638. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  639. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  640. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  641. /* Everything else requires more than 1 byte per pixel, and rounds up
  642. * each pixel to an integer number of bytes (e.g. RGB555 is really
  643. * XRGB1555, with 1 bit per pixel wasted). */
  644. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  645. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  646. surface != NULL ? "(success)" : SDL_GetError());
  647. SDL_DestroySurface(surface);
  648. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  649. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  650. surface != NULL ? "(success)" : SDL_GetError());
  651. SDL_DestroySurface(surface);
  652. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  653. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  654. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  655. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  656. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  657. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  658. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  659. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  660. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  661. SDL_ClearError();
  662. expectedError = "aligning pitch would overflow";
  663. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  664. * alignment padding makes it overflow */
  665. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  666. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  667. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  668. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  669. SDL_ClearError();
  670. expectedError = "width * bpp would overflow";
  671. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  672. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  673. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  674. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  675. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  676. SDL_ClearError();
  677. expectedError = "height * pitch would overflow";
  678. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  679. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  680. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  681. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  682. SDL_ClearError();
  683. expectedError = "height * pitch would overflow";
  684. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  685. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  686. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  687. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  688. } else {
  689. SDLTest_Log("Can't easily overflow size_t on this platform");
  690. }
  691. return TEST_COMPLETED;
  692. }
  693. static int surface_testFlip(void *arg)
  694. {
  695. SDL_Surface *surface;
  696. Uint8 *pixels;
  697. int offset;
  698. const char *expectedError;
  699. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  700. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  701. SDL_ClearError();
  702. expectedError = "Parameter 'surface' is invalid";
  703. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  704. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  705. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  706. SDL_ClearError();
  707. expectedError = "Parameter 'flip' is invalid";
  708. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  709. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  710. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  711. pixels = (Uint8 *)surface->pixels;
  712. *pixels = 0xFF;
  713. offset = 0;
  714. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  715. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  716. SDLTest_AssertCheck(pixels[offset] == 0x00,
  717. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  718. offset = 2 * surface->pitch;
  719. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  720. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  721. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  722. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  723. SDLTest_AssertCheck(pixels[offset] == 0x00,
  724. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  725. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  726. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  727. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  728. SDL_DestroySurface(surface);
  729. return TEST_COMPLETED;
  730. }
  731. static int surface_testPalette(void *arg)
  732. {
  733. SDL_Surface *source, *surface, *output;
  734. SDL_Palette *palette;
  735. Uint8 *pixels;
  736. palette = SDL_CreatePalette(2);
  737. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  738. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  739. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  740. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  741. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  742. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  743. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  744. pixels = (Uint8 *)surface->pixels;
  745. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  746. /* Identity copy between indexed surfaces without a palette */
  747. *(Uint8 *)source->pixels = 1;
  748. SDL_BlitSurface(source, NULL, surface, NULL);
  749. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  750. /* Identity copy between indexed surfaces where the destination has a palette */
  751. palette->colors[0].r = 0;
  752. palette->colors[0].g = 0;
  753. palette->colors[0].b = 0;
  754. palette->colors[1].r = 0xFF;
  755. palette->colors[1].g = 0;
  756. palette->colors[1].b = 0;
  757. SDL_SetSurfacePalette(surface, palette);
  758. *pixels = 0;
  759. SDL_BlitSurface(source, NULL, surface, NULL);
  760. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  761. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  762. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  763. pixels = (Uint8 *)output->pixels;
  764. SDL_BlitSurface(surface, NULL, output, NULL);
  765. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  766. /* Set the palette color and blit again */
  767. palette->colors[1].r = 0xAA;
  768. SDL_SetSurfacePalette(surface, palette);
  769. SDL_BlitSurface(surface, NULL, output, NULL);
  770. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  771. SDL_DestroyPalette(palette);
  772. SDL_DestroySurface(source);
  773. SDL_DestroySurface(surface);
  774. SDL_DestroySurface(output);
  775. return TEST_COMPLETED;
  776. }
  777. /* ================= Test References ================== */
  778. /* Surface test cases */
  779. static const SDLTest_TestCaseReference surfaceTest1 = {
  780. (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED
  781. };
  782. static const SDLTest_TestCaseReference surfaceTest2 = {
  783. (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  784. };
  785. static const SDLTest_TestCaseReference surfaceTest3 = {
  786. (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  787. };
  788. static const SDLTest_TestCaseReference surfaceTest4 = {
  789. (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  790. };
  791. static const SDLTest_TestCaseReference surfaceTest5 = {
  792. (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  793. };
  794. static const SDLTest_TestCaseReference surfaceTest6 = {
  795. (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  796. };
  797. static const SDLTest_TestCaseReference surfaceTest7 = {
  798. (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  799. };
  800. static const SDLTest_TestCaseReference surfaceTest8 = {
  801. (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  802. };
  803. static const SDLTest_TestCaseReference surfaceTest9 = {
  804. (SDLTest_TestCaseFp)surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  805. };
  806. static const SDLTest_TestCaseReference surfaceTest10 = {
  807. (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  808. };
  809. static const SDLTest_TestCaseReference surfaceTest11 = {
  810. (SDLTest_TestCaseFp)surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  811. };
  812. static const SDLTest_TestCaseReference surfaceTest12 = {
  813. (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  814. };
  815. static const SDLTest_TestCaseReference surfaceTest13 = {
  816. (SDLTest_TestCaseFp)surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  817. };
  818. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  819. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  820. };
  821. static const SDLTest_TestCaseReference surfaceTestFlip = {
  822. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  823. };
  824. static const SDLTest_TestCaseReference surfaceTestPalette = {
  825. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  826. };
  827. /* Sequence of Surface test cases */
  828. static const SDLTest_TestCaseReference *surfaceTests[] = {
  829. &surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
  830. &surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
  831. &surfaceTest11, &surfaceTest12, &surfaceTest13,
  832. &surfaceTestOverflow, &surfaceTestFlip, &surfaceTestPalette, NULL
  833. };
  834. /* Surface test suite (global) */
  835. SDLTest_TestSuiteReference surfaceTestSuite = {
  836. "Surface",
  837. surfaceSetUp,
  838. surfaceTests,
  839. surfaceTearDown
  840. };