testautomation_surface.c 48 KB

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