testautomation_surface.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. currentBlendMode = SDL_GetSurfaceBlendMode(testSurface);
  47. SDLTest_AssertCheck(currentBlendMode != SDL_BLENDMODE_INVALID, "Validate result from SDL_GetSurfaceBlendMode, expected: !SDL_BLENDMODE_INVALID, got: 0x%" SDL_PRIx32, currentBlendMode);
  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. /* ================= Test References ================== */
  809. /* Surface test cases */
  810. static const SDLTest_TestCaseReference surfaceTest1 = {
  811. (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED
  812. };
  813. static const SDLTest_TestCaseReference surfaceTest2 = {
  814. (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  815. };
  816. static const SDLTest_TestCaseReference surfaceTest3 = {
  817. (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  818. };
  819. static const SDLTest_TestCaseReference surfaceTest4 = {
  820. (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  821. };
  822. static const SDLTest_TestCaseReference surfaceTest5 = {
  823. (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  824. };
  825. static const SDLTest_TestCaseReference surfaceTest6 = {
  826. (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  827. };
  828. static const SDLTest_TestCaseReference surfaceTest7 = {
  829. (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  830. };
  831. static const SDLTest_TestCaseReference surfaceTest8 = {
  832. (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  833. };
  834. static const SDLTest_TestCaseReference surfaceTest9 = {
  835. (SDLTest_TestCaseFp)surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  836. };
  837. static const SDLTest_TestCaseReference surfaceTest10 = {
  838. (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  839. };
  840. static const SDLTest_TestCaseReference surfaceTest11 = {
  841. (SDLTest_TestCaseFp)surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  842. };
  843. static const SDLTest_TestCaseReference surfaceTest12 = {
  844. (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  845. };
  846. static const SDLTest_TestCaseReference surfaceTest13 = {
  847. (SDLTest_TestCaseFp)surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  848. };
  849. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  850. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  851. };
  852. static const SDLTest_TestCaseReference surfaceTestFlip = {
  853. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  854. };
  855. static const SDLTest_TestCaseReference surfaceTestPalette = {
  856. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  857. };
  858. /* Sequence of Surface test cases */
  859. static const SDLTest_TestCaseReference *surfaceTests[] = {
  860. &surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
  861. &surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
  862. &surfaceTest11, &surfaceTest12, &surfaceTest13,
  863. &surfaceTestOverflow, &surfaceTestFlip, &surfaceTestPalette, NULL
  864. };
  865. /* Surface test suite (global) */
  866. SDLTest_TestSuiteReference surfaceTestSuite = {
  867. "Surface",
  868. surfaceSetUp,
  869. surfaceTests,
  870. surfaceTearDown
  871. };