1
0

testautomation_surface.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. bool result = FUNC PARAMS; \
  24. if (!result) { \
  25. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, 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 SDLCALL 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 == true, "Validate result from SDL_SetSurfaceBlendMode, expected: true, got: %i", result);
  46. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  47. SDLTest_AssertCheck(result == true, "Validate result from SDL_GetSurfaceBlendMode, expected: true, got: %i", result);
  48. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  49. /* Clear the target surface */
  50. result = SDL_FillSurfaceRect(testSurface, NULL, SDL_MapSurfaceRGBA(testSurface, 0, 0, 0, 255));
  51. SDLTest_AssertCheck(result == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", result);
  52. }
  53. }
  54. static void SDLCALL surfaceTearDown(void *arg)
  55. {
  56. SDL_DestroySurface(referenceSurface);
  57. referenceSurface = NULL;
  58. SDL_DestroySurface(testSurface);
  59. testSurface = NULL;
  60. }
  61. static void DitherPalette(SDL_Palette *palette)
  62. {
  63. int i;
  64. for (i = 0; i < palette->ncolors; i++) {
  65. int r, g, b;
  66. /* map each bit field to the full [0, 255] interval,
  67. so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
  68. r = i & 0xe0;
  69. r |= r >> 3 | r >> 6;
  70. palette->colors[i].r = (Uint8)r;
  71. g = (i << 3) & 0xe0;
  72. g |= g >> 3 | g >> 6;
  73. palette->colors[i].g = (Uint8)g;
  74. b = i & 0x3;
  75. b |= b << 2;
  76. b |= b << 4;
  77. palette->colors[i].b = (Uint8)b;
  78. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  79. }
  80. }
  81. /**
  82. * Helper that blits in a specific blend mode, -1 for color mod, -2 for alpha mod
  83. */
  84. static void testBlitBlendModeWithFormats(int mode, SDL_PixelFormat src_format, SDL_PixelFormat dst_format)
  85. {
  86. /* Allow up to 1 delta from theoretical value to account for rounding error */
  87. const int MAXIMUM_ERROR = 1;
  88. int ret;
  89. SDL_Surface *src;
  90. SDL_Surface *dst;
  91. Uint32 color;
  92. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  93. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  94. Uint8 expectedR, expectedG, expectedB, expectedA;
  95. Uint8 actualR, actualG, actualB, actualA;
  96. int deltaR, deltaG, deltaB, deltaA;
  97. /* Create dst surface */
  98. dst = SDL_CreateSurface(9, 1, dst_format);
  99. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  100. if (dst == NULL) {
  101. return;
  102. }
  103. /* Clear surface. */
  104. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  105. SDL_Palette *palette = SDL_CreateSurfacePalette(dst);
  106. DitherPalette(palette);
  107. palette->colors[0].r = dstR;
  108. palette->colors[0].g = dstG;
  109. palette->colors[0].b = dstB;
  110. palette->colors[0].a = dstA;
  111. color = 0;
  112. } else {
  113. color = SDL_MapSurfaceRGBA(dst, dstR, dstG, dstB, dstA);
  114. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  115. }
  116. ret = SDL_FillSurfaceRect(dst, NULL, color);
  117. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  118. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  119. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(dst->format), SDL_GetSurfacePalette(dst), &dstR, &dstG, &dstB, &dstA);
  120. /* Create src surface */
  121. src = SDL_CreateSurface(9, 1, src_format);
  122. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  123. if (src == NULL) {
  124. return;
  125. }
  126. if (SDL_ISPIXELFORMAT_INDEXED(src_format)) {
  127. SDL_Palette *palette = SDL_CreateSurfacePalette(src);
  128. palette->colors[0].r = srcR;
  129. palette->colors[0].g = srcG;
  130. palette->colors[0].b = srcB;
  131. palette->colors[0].a = srcA;
  132. }
  133. /* Reset alpha modulation */
  134. ret = SDL_SetSurfaceAlphaMod(src, 255);
  135. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  136. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceAlphaMod(), expected: true, got: %i", ret);
  137. /* Reset color modulation */
  138. ret = SDL_SetSurfaceColorMod(src, 255, 255, 255);
  139. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  140. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorMod(), expected: true, got: %i", ret);
  141. /* Reset color key */
  142. ret = SDL_SetSurfaceColorKey(src, false, 0);
  143. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  144. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey(), expected: true, got: %i", ret);
  145. /* Clear surface. */
  146. color = SDL_MapSurfaceRGBA(src, srcR, srcG, srcB, srcA);
  147. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  148. ret = SDL_FillSurfaceRect(src, NULL, color);
  149. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  150. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  151. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(src->format), SDL_GetSurfacePalette(src), &srcR, &srcG, &srcB, &srcA);
  152. /* Set blend mode. */
  153. if (mode >= 0) {
  154. ret = SDL_SetSurfaceBlendMode(src, (SDL_BlendMode)mode);
  155. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  156. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  157. } else {
  158. ret = SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
  159. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  160. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  161. }
  162. /* Test blend mode. */
  163. #define FLOAT(X) ((float)X / 255.0f)
  164. switch (mode) {
  165. case -1:
  166. /* Set color mod. */
  167. ret = SDL_SetSurfaceColorMod(src, srcR, srcG, srcB);
  168. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceColorMod, expected: true, got: %i", ret);
  169. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  170. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  171. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  172. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  173. break;
  174. case -2:
  175. /* Set alpha mod. */
  176. ret = SDL_SetSurfaceAlphaMod(src, srcA);
  177. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: true, got: %i", ret);
  178. 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);
  179. 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);
  180. 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);
  181. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  182. break;
  183. case SDL_BLENDMODE_NONE:
  184. expectedR = srcR;
  185. expectedG = srcG;
  186. expectedB = srcB;
  187. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  188. break;
  189. case SDL_BLENDMODE_BLEND:
  190. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  191. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  192. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  193. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  194. break;
  195. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  196. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  197. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  198. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  199. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  200. break;
  201. case SDL_BLENDMODE_ADD:
  202. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  203. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  204. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  205. expectedA = dstA;
  206. break;
  207. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  208. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  209. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  210. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  211. expectedA = dstA;
  212. break;
  213. case SDL_BLENDMODE_MOD:
  214. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  215. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  216. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  217. expectedA = dstA;
  218. break;
  219. case SDL_BLENDMODE_MUL:
  220. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  221. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  222. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  223. expectedA = dstA;
  224. break;
  225. default:
  226. SDLTest_LogError("Invalid blending mode: %d", mode);
  227. return;
  228. }
  229. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  230. SDL_Palette *palette = SDL_GetSurfacePalette(dst);
  231. palette->colors[1].r = expectedR;
  232. palette->colors[1].g = expectedG;
  233. palette->colors[1].b = expectedB;
  234. palette->colors[1].a = expectedA;
  235. }
  236. /* Blitting. */
  237. ret = SDL_BlitSurface(src, NULL, dst, NULL);
  238. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_BlitSurface, expected: true, got: %i: %s", ret, !ret ? SDL_GetError() : "success");
  239. if (ret) {
  240. SDL_ReadSurfacePixel(dst, 0, 0, &actualR, &actualG, &actualB, &actualA);
  241. deltaR = SDL_abs((int)actualR - expectedR);
  242. deltaG = SDL_abs((int)actualG - expectedG);
  243. deltaB = SDL_abs((int)actualB - expectedB);
  244. deltaA = SDL_abs((int)actualA - expectedA);
  245. SDLTest_AssertCheck(
  246. deltaR <= MAXIMUM_ERROR &&
  247. deltaG <= MAXIMUM_ERROR &&
  248. deltaB <= MAXIMUM_ERROR &&
  249. deltaA <= MAXIMUM_ERROR,
  250. "Checking %s -> %s blit results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  251. SDL_GetPixelFormatName(src_format),
  252. SDL_GetPixelFormatName(dst_format),
  253. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  254. }
  255. /* Clean up */
  256. SDL_DestroySurface(src);
  257. SDL_DestroySurface(dst);
  258. }
  259. static void testBlitBlendMode(int mode)
  260. {
  261. const SDL_PixelFormat src_formats[] = {
  262. SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  263. };
  264. const SDL_PixelFormat dst_formats[] = {
  265. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  266. };
  267. int i, j;
  268. for (i = 0; i < SDL_arraysize(src_formats); ++i) {
  269. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  270. testBlitBlendModeWithFormats(mode, src_formats[i], dst_formats[j]);
  271. }
  272. }
  273. }
  274. /* Helper to check that a file exists */
  275. static void AssertFileExist(const char *filename)
  276. {
  277. SDLTest_AssertCheck(SDL_GetPathInfo(filename, NULL), "Verify file '%s' exists", filename);
  278. }
  279. /* Test case functions */
  280. /**
  281. * Tests creating surface with invalid format
  282. */
  283. static int SDLCALL surface_testInvalidFormat(void *arg)
  284. {
  285. SDL_Surface *surface;
  286. surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_UNKNOWN);
  287. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurface(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  288. SDL_DestroySurface(surface);
  289. surface = SDL_CreateSurfaceFrom(32, 32, SDL_PIXELFORMAT_UNKNOWN, NULL, 0);
  290. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurfaceFrom(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  291. SDL_DestroySurface(surface);
  292. return TEST_COMPLETED;
  293. }
  294. /**
  295. * Tests sprite saving and loading
  296. */
  297. static int SDLCALL surface_testSaveLoad(void *arg)
  298. {
  299. int ret;
  300. const char *sampleFilename = "testSaveLoad.tmp";
  301. SDL_Surface *face;
  302. SDL_Surface *rface;
  303. /* Create sample surface */
  304. face = SDLTest_ImageFace();
  305. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  306. if (face == NULL) {
  307. return TEST_ABORTED;
  308. }
  309. /* Delete test file; ignore errors */
  310. unlink(sampleFilename);
  311. /* Save a BMP surface */
  312. ret = SDL_SaveBMP(face, sampleFilename);
  313. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  314. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  315. AssertFileExist(sampleFilename);
  316. /* Load a BMP surface */
  317. rface = SDL_LoadBMP(sampleFilename);
  318. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  319. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  320. if (rface != NULL) {
  321. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  322. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  323. SDL_DestroySurface(rface);
  324. rface = NULL;
  325. }
  326. /* Delete test file; ignore errors */
  327. unlink(sampleFilename);
  328. /* Save a PNG surface */
  329. ret = SDL_SavePNG(face, sampleFilename);
  330. SDLTest_AssertPass("Call to SDL_SavePNG()");
  331. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  332. AssertFileExist(sampleFilename);
  333. /* Load a PNG surface */
  334. rface = SDL_LoadPNG(sampleFilename);
  335. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  336. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  337. if (rface != NULL) {
  338. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  339. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  340. SDL_DestroySurface(rface);
  341. rface = NULL;
  342. }
  343. /* Delete test file; ignore errors */
  344. unlink(sampleFilename);
  345. /* Clean up */
  346. SDL_DestroySurface(face);
  347. face = NULL;
  348. return TEST_COMPLETED;
  349. }
  350. /**
  351. * Tests tiled blitting.
  352. */
  353. static int SDLCALL surface_testBlitTiled(void *arg)
  354. {
  355. SDL_Surface *face = NULL;
  356. SDL_Surface *testSurface2x = NULL;
  357. SDL_Surface *referenceSurface2x = NULL;
  358. int ret = 0;
  359. /* Create sample surface */
  360. face = SDLTest_ImageFace();
  361. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  362. if (face == NULL) {
  363. return TEST_ABORTED;
  364. }
  365. /* Tiled blit - 1.0 scale */
  366. {
  367. ret = SDL_BlitSurfaceTiled(face, NULL, testSurface, NULL);
  368. SDLTest_AssertCheck(ret == true, "Verify result from SDL_BlitSurfaceTiled expected: true, got: %i", ret);
  369. /* See if it's the same */
  370. SDL_DestroySurface(referenceSurface);
  371. referenceSurface = SDLTest_ImageBlitTiled();
  372. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  373. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  374. }
  375. /* Tiled blit - 2.0 scale */
  376. {
  377. testSurface2x = SDL_CreateSurface(testSurface->w * 2, testSurface->h * 2, testSurface->format);
  378. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface2x is not NULL");
  379. ret = SDL_FillSurfaceRect(testSurface2x, NULL, SDL_MapSurfaceRGBA(testSurface2x, 0, 0, 0, 255));
  380. SDLTest_AssertCheck(ret == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  381. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  382. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceTiledWithScale, expected: true, got: %i", ret);
  383. /* See if it's the same */
  384. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  385. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  386. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: true, got: %i", ret);
  387. ret = SDLTest_CompareSurfaces(testSurface2x, referenceSurface2x, 0);
  388. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  389. }
  390. /* Clean up. */
  391. SDL_DestroySurface(face);
  392. SDL_DestroySurface(testSurface2x);
  393. SDL_DestroySurface(referenceSurface2x);
  394. return TEST_COMPLETED;
  395. }
  396. static const Uint8 COLOR_SEPARATION = 85;
  397. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  398. {
  399. SDL_Rect rect;
  400. // Upper left
  401. rect.x = 0;
  402. rect.y = 0;
  403. rect.w = left_width;
  404. rect.h = top_height;
  405. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  406. // Top
  407. rect.x = left_width;
  408. rect.y = 0;
  409. rect.w = surface->w - left_width - right_width;
  410. rect.h = top_height;
  411. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  412. // Upper right
  413. rect.x = surface->w - right_width;
  414. rect.y = 0;
  415. rect.w = right_width;
  416. rect.h = top_height;
  417. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  418. // Left
  419. rect.x = 0;
  420. rect.y = top_height;
  421. rect.w = left_width;
  422. rect.h = surface->h - top_height - bottom_height;
  423. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  424. // Center
  425. rect.x = left_width;
  426. rect.y = top_height;
  427. rect.w = surface->w - right_width - left_width;
  428. rect.h = surface->h - top_height - bottom_height;
  429. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  430. // Right
  431. rect.x = surface->w - right_width;
  432. rect.y = top_height;
  433. rect.w = right_width;
  434. rect.h = surface->h - top_height - bottom_height;
  435. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  436. // Lower left
  437. rect.x = 0;
  438. rect.y = surface->h - bottom_height;
  439. rect.w = left_width;
  440. rect.h = bottom_height;
  441. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  442. // Bottom
  443. rect.x = left_width;
  444. rect.y = surface->h - bottom_height;
  445. rect.w = surface->w - left_width - right_width;
  446. rect.h = bottom_height;
  447. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  448. // Lower right
  449. rect.x = surface->w - right_width;
  450. rect.y = surface->h - bottom_height;
  451. rect.w = right_width;
  452. rect.h = bottom_height;
  453. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  454. }
  455. /**
  456. * Tests 9-grid blitting.
  457. */
  458. static int SDLCALL surface_testBlit9Grid(void *arg)
  459. {
  460. SDL_Surface *source = NULL;
  461. int x, y;
  462. int ret = 0;
  463. /* Create source surface */
  464. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  465. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  466. for (y = 0; y < 3; ++y) {
  467. for (x = 0; x < 3; ++x) {
  468. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  469. }
  470. }
  471. /* 9-grid blit - 1.0 scale */
  472. {
  473. /* Create reference surface */
  474. SDL_DestroySurface(referenceSurface);
  475. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  476. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  477. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  478. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  479. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  480. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  481. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  482. }
  483. /* 9-grid blit - 2.0 scale */
  484. {
  485. /* Create reference surface */
  486. SDL_DestroySurface(referenceSurface);
  487. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  488. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  489. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  490. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  491. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  492. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  493. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  494. }
  495. /* Clean up. */
  496. SDL_DestroySurface(source);
  497. /* Create complex source surface */
  498. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  499. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  500. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  501. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  502. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  503. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  504. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  505. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  506. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  507. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  508. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  509. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  510. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  511. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  512. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  513. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  514. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  515. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  516. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  517. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  518. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  519. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  520. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  521. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  522. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  523. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  524. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  525. /* complex 9-grid blit - 1.0 scale */
  526. {
  527. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  528. /* Create reference surface */
  529. SDL_DestroySurface(referenceSurface);
  530. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  531. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  532. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  533. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  534. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  535. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  536. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  537. }
  538. /* complex 9-grid blit - 2.0 scale */
  539. {
  540. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  541. /* Create reference surface */
  542. SDL_DestroySurface(referenceSurface);
  543. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  544. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  545. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  546. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  547. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  548. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  549. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  550. }
  551. /* Clean up. */
  552. SDL_DestroySurface(source);
  553. return TEST_COMPLETED;
  554. }
  555. /**
  556. * Tests blitting between multiple surfaces of the same format
  557. */
  558. static int SDLCALL surface_testBlitMultiple(void *arg)
  559. {
  560. SDL_Surface *source, *surface;
  561. SDL_Palette *palette;
  562. Uint8 *pixels;
  563. palette = SDL_CreatePalette(2);
  564. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  565. palette->colors[0].r = 0;
  566. palette->colors[0].g = 0;
  567. palette->colors[0].b = 0;
  568. palette->colors[1].r = 0xFF;
  569. palette->colors[1].g = 0;
  570. palette->colors[1].b = 0;
  571. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  572. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  573. SDL_SetSurfacePalette(source, palette);
  574. *(Uint8 *)source->pixels = 1;
  575. /* Set up a blit to a surface using the palette */
  576. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  577. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  578. SDL_SetSurfacePalette(surface, palette);
  579. pixels = (Uint8 *)surface->pixels;
  580. *pixels = 0;
  581. SDL_BlitSurface(source, NULL, surface, NULL);
  582. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  583. /* Set up a blit to another surface using the same palette */
  584. SDL_DestroySurface(surface);
  585. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  586. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  587. SDL_SetSurfacePalette(surface, palette);
  588. pixels = (Uint8 *)surface->pixels;
  589. *pixels = 0;
  590. SDL_BlitSurface(source, NULL, surface, NULL);
  591. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  592. /* Set up a blit to new surface with a different format */
  593. SDL_DestroySurface(surface);
  594. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  595. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  596. pixels = (Uint8 *)surface->pixels;
  597. SDL_BlitSurface(source, NULL, surface, NULL);
  598. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  599. /* Set up a blit to another surface with the same format */
  600. SDL_DestroySurface(surface);
  601. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  602. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  603. pixels = (Uint8 *)surface->pixels;
  604. SDL_BlitSurface(source, NULL, surface, NULL);
  605. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  606. SDL_DestroyPalette(palette);
  607. SDL_DestroySurface(source);
  608. SDL_DestroySurface(surface);
  609. return TEST_COMPLETED;
  610. }
  611. /**
  612. * Tests surface conversion.
  613. */
  614. static int SDLCALL surface_testSurfaceConversion(void *arg)
  615. {
  616. SDL_Surface *rface = NULL, *face = NULL;
  617. int ret = 0;
  618. /* Create sample surface */
  619. face = SDLTest_ImageFace();
  620. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  621. if (face == NULL) {
  622. return TEST_ABORTED;
  623. }
  624. /* Set transparent pixel as the pixel at (0,0) */
  625. if (SDL_GetSurfacePalette(face)) {
  626. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  627. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  628. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  629. }
  630. /* Convert to 32 bit to compare. */
  631. rface = SDL_ConvertSurface(face, testSurface->format);
  632. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  633. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  634. /* Compare surface. */
  635. ret = SDLTest_CompareSurfaces(rface, face, 0);
  636. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  637. /* Clean up. */
  638. SDL_DestroySurface(face);
  639. face = NULL;
  640. SDL_DestroySurface(rface);
  641. rface = NULL;
  642. return TEST_COMPLETED;
  643. }
  644. /**
  645. * Tests surface conversion across all pixel formats.
  646. */
  647. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  648. {
  649. Uint32 pixel_formats[] = {
  650. SDL_PIXELFORMAT_INDEX8,
  651. SDL_PIXELFORMAT_RGB332,
  652. SDL_PIXELFORMAT_XRGB4444,
  653. SDL_PIXELFORMAT_XBGR4444,
  654. SDL_PIXELFORMAT_XRGB1555,
  655. SDL_PIXELFORMAT_XBGR1555,
  656. SDL_PIXELFORMAT_ARGB4444,
  657. SDL_PIXELFORMAT_RGBA4444,
  658. SDL_PIXELFORMAT_ABGR4444,
  659. SDL_PIXELFORMAT_BGRA4444,
  660. SDL_PIXELFORMAT_ARGB1555,
  661. SDL_PIXELFORMAT_RGBA5551,
  662. SDL_PIXELFORMAT_ABGR1555,
  663. SDL_PIXELFORMAT_BGRA5551,
  664. SDL_PIXELFORMAT_RGB565,
  665. SDL_PIXELFORMAT_BGR565,
  666. SDL_PIXELFORMAT_RGB24,
  667. SDL_PIXELFORMAT_BGR24,
  668. SDL_PIXELFORMAT_XRGB8888,
  669. SDL_PIXELFORMAT_RGBX8888,
  670. SDL_PIXELFORMAT_XBGR8888,
  671. SDL_PIXELFORMAT_BGRX8888,
  672. SDL_PIXELFORMAT_ARGB8888,
  673. SDL_PIXELFORMAT_RGBA8888,
  674. SDL_PIXELFORMAT_ABGR8888,
  675. SDL_PIXELFORMAT_BGRA8888,
  676. #if 0 /* We aren't testing HDR10 colorspace conversion */
  677. SDL_PIXELFORMAT_XRGB2101010,
  678. SDL_PIXELFORMAT_XBGR2101010,
  679. SDL_PIXELFORMAT_ARGB2101010,
  680. SDL_PIXELFORMAT_ABGR2101010,
  681. #endif
  682. };
  683. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  684. const SDL_PixelFormatDetails *fmt1, *fmt2;
  685. int i, j, ret = 0;
  686. /* Create sample surface */
  687. face = SDLTest_ImageFace();
  688. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  689. if (face == NULL) {
  690. return TEST_ABORTED;
  691. }
  692. /* Set transparent pixel as the pixel at (0,0) */
  693. if (SDL_GetSurfacePalette(face)) {
  694. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  695. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  696. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  697. }
  698. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  699. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  700. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  701. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  702. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  703. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  704. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  705. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  706. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  707. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  708. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  709. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  710. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  711. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  712. if (fmt1 && fmt2 &&
  713. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  714. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  715. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  716. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  717. final = SDL_ConvertSurface(cvt2, face->format);
  718. SDL_assert(final != NULL);
  719. /* Compare surface. */
  720. ret = SDLTest_CompareSurfaces(face, final, 0);
  721. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  722. SDL_DestroySurface(final);
  723. }
  724. SDL_DestroySurface(cvt1);
  725. SDL_DestroySurface(cvt2);
  726. }
  727. }
  728. /* Clean up. */
  729. SDL_DestroySurface(face);
  730. return TEST_COMPLETED;
  731. }
  732. /**
  733. * Tests sprite loading. A failure case.
  734. */
  735. static int SDLCALL surface_testLoadFailure(void *arg)
  736. {
  737. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  738. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  739. return TEST_COMPLETED;
  740. }
  741. /**
  742. * Tests blitting from a zero sized source rectangle
  743. */
  744. static int SDLCALL surface_testBlitZeroSource(void *arg)
  745. {
  746. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  747. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  748. SDL_Rect srcrect = { 0, 0, 0, 0 };
  749. int ret;
  750. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  751. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  752. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  753. ret = SDLTest_CompareSurfaces(dst, src, 0);
  754. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  755. SDL_DestroySurface(src);
  756. SDL_DestroySurface(dst);
  757. return TEST_COMPLETED;
  758. }
  759. /**
  760. * Tests some blitting routines.
  761. */
  762. static int SDLCALL surface_testBlit(void *arg)
  763. {
  764. /* Basic blitting */
  765. testBlitBlendMode(SDL_BLENDMODE_NONE);
  766. return TEST_COMPLETED;
  767. }
  768. /**
  769. * Tests some blitting routines with color mod
  770. */
  771. static int SDLCALL surface_testBlitColorMod(void *arg)
  772. {
  773. /* Basic blitting with color mod */
  774. testBlitBlendMode(-1);
  775. return TEST_COMPLETED;
  776. }
  777. /**
  778. * Tests some blitting routines with alpha mod
  779. */
  780. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  781. {
  782. /* Basic blitting with alpha mod */
  783. testBlitBlendMode(-2);
  784. return TEST_COMPLETED;
  785. }
  786. /**
  787. * Tests some more blitting routines.
  788. */
  789. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  790. {
  791. /* Blend blitting */
  792. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  793. return TEST_COMPLETED;
  794. }
  795. /**
  796. * @brief Tests some more blitting routines.
  797. */
  798. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  799. {
  800. /* Blend premultiplied blitting */
  801. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  802. return TEST_COMPLETED;
  803. }
  804. /**
  805. * Tests some more blitting routines.
  806. */
  807. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  808. {
  809. /* Add blitting */
  810. testBlitBlendMode(SDL_BLENDMODE_ADD);
  811. return TEST_COMPLETED;
  812. }
  813. /**
  814. * Tests some more blitting routines.
  815. */
  816. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  817. {
  818. /* Add premultiplied blitting */
  819. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  820. return TEST_COMPLETED;
  821. }
  822. /**
  823. * Tests some more blitting routines.
  824. */
  825. static int SDLCALL surface_testBlitBlendMod(void *arg)
  826. {
  827. /* Mod blitting */
  828. testBlitBlendMode(SDL_BLENDMODE_MOD);
  829. return TEST_COMPLETED;
  830. }
  831. /**
  832. * Tests some more blitting routines.
  833. */
  834. static int SDLCALL surface_testBlitBlendMul(void *arg)
  835. {
  836. /* Mod blitting */
  837. testBlitBlendMode(SDL_BLENDMODE_MUL);
  838. return TEST_COMPLETED;
  839. }
  840. /**
  841. * Tests blitting invalid surfaces.
  842. */
  843. static int SDLCALL surface_testBlitInvalid(void *arg)
  844. {
  845. SDL_Surface *valid, *invalid;
  846. bool result;
  847. valid = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  848. SDLTest_AssertCheck(valid != NULL, "Check surface creation");
  849. invalid = SDL_CreateSurface(0, 0, SDL_PIXELFORMAT_RGBA8888);
  850. SDLTest_AssertCheck(invalid != NULL, "Check surface creation");
  851. SDLTest_AssertCheck(invalid->pixels == NULL, "Check surface pixels are NULL");
  852. result = SDL_BlitSurface(invalid, NULL, valid, NULL);
  853. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(invalid, NULL, valid, NULL), result = %s\n", result ? "true" : "false");
  854. result = SDL_BlitSurface(valid, NULL, invalid, NULL);
  855. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, invalid, NULL), result = %s\n", result ? "true" : "false");
  856. result = SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST);
  857. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s\n", result ? "true" : "false");
  858. result = SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST);
  859. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST), result = %s\n", result ? "true" : "false");
  860. SDL_DestroySurface(valid);
  861. SDL_DestroySurface(invalid);
  862. return TEST_COMPLETED;
  863. }
  864. static int SDLCALL surface_testBlitsWithBadCoordinates(void *arg)
  865. {
  866. const SDL_Rect rect[8] = {
  867. { SDL_MAX_SINT32, 0, 2, 2 },
  868. { 0, SDL_MAX_SINT32, 2, 2 },
  869. { 0, 0, SDL_MAX_SINT32, 2 },
  870. { 0, 0, 2, SDL_MAX_SINT32 },
  871. { SDL_MIN_SINT32, 0, 2, 2 },
  872. { 0, SDL_MIN_SINT32, 2, 2 },
  873. { 0, 0, SDL_MIN_SINT32, 2 },
  874. { 0, 0, 2, SDL_MIN_SINT32 }
  875. };
  876. SDL_Surface *s;
  877. bool result;
  878. int i;
  879. s = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  880. SDLTest_AssertCheck(s != NULL, "Check surface creation");
  881. for (i = 0; i < 8; i++) {
  882. result = SDL_BlitSurface(s, NULL, s, &rect[i]);
  883. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, valid, &rect), result = %s", result ? "true" : "false");
  884. result = SDL_BlitSurface(s, &rect[i], s, NULL);
  885. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, &rect, valid, NULL), result = %s", result ? "true" : "false");
  886. result = SDL_BlitSurfaceScaled(s, NULL, s, &rect[i], SDL_SCALEMODE_NEAREST);
  887. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, NULL, valid, &rect, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  888. result = SDL_BlitSurfaceScaled(s, &rect[i], s, NULL, SDL_SCALEMODE_NEAREST);
  889. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, &rect, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  890. }
  891. SDL_DestroySurface(s);
  892. return TEST_COMPLETED;
  893. }
  894. static int SDLCALL surface_testOverflow(void *arg)
  895. {
  896. char buf[1024];
  897. const char *expectedError;
  898. SDL_Surface *surface;
  899. SDL_memset(buf, '\0', sizeof(buf));
  900. expectedError = "Parameter 'width' is invalid";
  901. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  902. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  903. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  904. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  905. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  906. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  907. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  908. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  909. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  910. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  911. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  912. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  913. expectedError = "Parameter 'height' is invalid";
  914. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  915. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  916. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  917. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  918. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  919. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  920. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  921. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  922. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  923. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  924. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  925. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  926. expectedError = "Parameter 'pitch' is invalid";
  927. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  928. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  929. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  930. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  931. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  932. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  933. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  934. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  935. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  936. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  937. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  938. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  939. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  940. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  941. surface != NULL ? "(success)" : SDL_GetError());
  942. SDL_DestroySurface(surface);
  943. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  944. * the width, but it must be enough to hold the appropriate number of
  945. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  946. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  947. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  948. surface != NULL ? "(success)" : SDL_GetError());
  949. SDL_DestroySurface(surface);
  950. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  951. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  952. surface != NULL ? "(success)" : SDL_GetError());
  953. SDL_DestroySurface(surface);
  954. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  955. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  956. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  957. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  958. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  959. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  960. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  961. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  962. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  963. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  964. surface != NULL ? "(success)" : SDL_GetError());
  965. SDL_DestroySurface(surface);
  966. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  967. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  968. surface != NULL ? "(success)" : SDL_GetError());
  969. SDL_DestroySurface(surface);
  970. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  971. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  972. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  973. surface != NULL ? "(success)" : SDL_GetError());
  974. SDL_DestroySurface(surface);
  975. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  976. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  977. surface != NULL ? "(success)" : SDL_GetError());
  978. SDL_DestroySurface(surface);
  979. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  980. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  981. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  982. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  983. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  984. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  985. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  986. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  987. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  988. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  989. surface != NULL ? "(success)" : SDL_GetError());
  990. SDL_DestroySurface(surface);
  991. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  992. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  993. surface != NULL ? "(success)" : SDL_GetError());
  994. SDL_DestroySurface(surface);
  995. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  996. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  997. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  998. surface != NULL ? "(success)" : SDL_GetError());
  999. SDL_DestroySurface(surface);
  1000. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1001. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1002. surface != NULL ? "(success)" : SDL_GetError());
  1003. SDL_DestroySurface(surface);
  1004. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1005. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1006. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1007. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1008. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1009. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1010. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1011. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1012. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  1013. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1014. surface != NULL ? "(success)" : SDL_GetError());
  1015. SDL_DestroySurface(surface);
  1016. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  1017. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1018. surface != NULL ? "(success)" : SDL_GetError());
  1019. SDL_DestroySurface(surface);
  1020. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  1021. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1022. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1023. surface != NULL ? "(success)" : SDL_GetError());
  1024. SDL_DestroySurface(surface);
  1025. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1026. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1027. surface != NULL ? "(success)" : SDL_GetError());
  1028. SDL_DestroySurface(surface);
  1029. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1030. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1031. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1032. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1033. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1034. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1035. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1036. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1037. /* Everything else requires more than 1 byte per pixel, and rounds up
  1038. * each pixel to an integer number of bytes (e.g. RGB555 is really
  1039. * XRGB1555, with 1 bit per pixel wasted). */
  1040. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1041. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1042. surface != NULL ? "(success)" : SDL_GetError());
  1043. SDL_DestroySurface(surface);
  1044. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1045. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1046. surface != NULL ? "(success)" : SDL_GetError());
  1047. SDL_DestroySurface(surface);
  1048. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1049. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1050. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1051. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1052. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1053. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1054. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1055. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1056. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  1057. SDL_ClearError();
  1058. expectedError = "aligning pitch would overflow";
  1059. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  1060. * alignment padding makes it overflow */
  1061. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  1062. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  1063. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1064. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1065. SDL_ClearError();
  1066. expectedError = "width * bpp would overflow";
  1067. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  1068. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  1069. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  1070. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1071. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1072. SDL_ClearError();
  1073. expectedError = "height * pitch would overflow";
  1074. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  1075. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  1076. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1077. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1078. SDL_ClearError();
  1079. expectedError = "height * pitch would overflow";
  1080. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  1081. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  1082. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1083. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1084. } else {
  1085. SDLTest_Log("Can't easily overflow size_t on this platform");
  1086. }
  1087. return TEST_COMPLETED;
  1088. }
  1089. static int SDLCALL surface_testFlip(void *arg)
  1090. {
  1091. SDL_Surface *surface;
  1092. Uint8 *pixels;
  1093. int offset;
  1094. const char *expectedError;
  1095. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1096. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1097. SDL_ClearError();
  1098. expectedError = "Parameter 'surface' is invalid";
  1099. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1100. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1101. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1102. SDL_ClearError();
  1103. expectedError = "Parameter 'flip' is invalid";
  1104. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1105. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1106. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1107. pixels = (Uint8 *)surface->pixels;
  1108. *pixels = 0xFF;
  1109. offset = 0;
  1110. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1111. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1112. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1113. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1114. offset = 2 * surface->pitch;
  1115. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1116. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1117. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1118. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1119. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1120. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1121. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1122. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1123. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1124. SDL_DestroySurface(surface);
  1125. return TEST_COMPLETED;
  1126. }
  1127. static int SDLCALL surface_testPalette(void *arg)
  1128. {
  1129. SDL_Surface *source, *surface, *output;
  1130. SDL_Palette *palette;
  1131. Uint8 *pixels;
  1132. palette = SDL_CreatePalette(2);
  1133. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1134. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1135. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1136. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1137. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1138. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1139. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1140. pixels = (Uint8 *)surface->pixels;
  1141. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1142. /* Identity copy between indexed surfaces without a palette */
  1143. *(Uint8 *)source->pixels = 1;
  1144. SDL_BlitSurface(source, NULL, surface, NULL);
  1145. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1146. /* Identity copy between indexed surfaces where the source has a palette */
  1147. palette->colors[0].r = 0;
  1148. palette->colors[0].g = 0;
  1149. palette->colors[0].b = 0;
  1150. palette->colors[1].r = 0xFF;
  1151. palette->colors[1].g = 0;
  1152. palette->colors[1].b = 0;
  1153. SDL_SetSurfacePalette(source, palette);
  1154. *pixels = 0;
  1155. SDL_BlitSurface(source, NULL, surface, NULL);
  1156. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1157. /* Identity copy between indexed surfaces where the destination has a palette */
  1158. palette->colors[0].r = 0;
  1159. palette->colors[0].g = 0;
  1160. palette->colors[0].b = 0;
  1161. palette->colors[1].r = 0xFF;
  1162. palette->colors[1].g = 0;
  1163. palette->colors[1].b = 0;
  1164. SDL_SetSurfacePalette(source, NULL);
  1165. SDL_SetSurfacePalette(surface, palette);
  1166. *pixels = 0;
  1167. SDL_BlitSurface(source, NULL, surface, NULL);
  1168. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1169. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1170. palette->colors[0].r = 0;
  1171. palette->colors[0].g = 0;
  1172. palette->colors[0].b = 0;
  1173. palette->colors[1].r = 0xFF;
  1174. palette->colors[1].g = 0;
  1175. palette->colors[1].b = 0;
  1176. SDL_SetSurfacePalette(source, palette);
  1177. SDL_SetSurfacePalette(surface, palette);
  1178. *pixels = 0;
  1179. SDL_BlitSurface(source, NULL, surface, NULL);
  1180. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1181. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1182. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1183. pixels = (Uint8 *)output->pixels;
  1184. SDL_BlitSurface(surface, NULL, output, NULL);
  1185. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1186. /* Set the palette color and blit again */
  1187. palette->colors[1].r = 0xAA;
  1188. SDL_SetSurfacePalette(surface, palette);
  1189. SDL_BlitSurface(surface, NULL, output, NULL);
  1190. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1191. SDL_DestroyPalette(palette);
  1192. SDL_DestroySurface(source);
  1193. SDL_DestroySurface(surface);
  1194. SDL_DestroySurface(output);
  1195. return TEST_COMPLETED;
  1196. }
  1197. static int SDLCALL surface_testPalettization(void *arg)
  1198. {
  1199. const SDL_Color palette_colors[] = {
  1200. { 0x80, 0x00, 0x00, 0xff },
  1201. { 0x00, 0x80, 0x00, 0xff },
  1202. { 0x00, 0x00, 0x80, 0xff },
  1203. { 0x40, 0x00, 0x00, 0xff },
  1204. { 0x00, 0x40, 0x00, 0xff },
  1205. { 0x00, 0x00, 0x40, 0xff },
  1206. { 0x00, 0x00, 0x00, 0xff },
  1207. { 0xff, 0x00, 0x00, 0xff },
  1208. { 0x00, 0xff, 0x00, 0xff },
  1209. { 0x00, 0x00, 0xff, 0xff },
  1210. { 0xff, 0xff, 0x00, 0xff },
  1211. { 0x00, 0xff, 0xff, 0xff },
  1212. { 0xff, 0x00, 0xff, 0xff },
  1213. };
  1214. const struct {
  1215. SDL_Color c;
  1216. Uint8 e;
  1217. } colors[] = {
  1218. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1219. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1220. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1221. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1222. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1223. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1224. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1225. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1226. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1227. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1228. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1229. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1230. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1231. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1232. };
  1233. int i;
  1234. int result;
  1235. SDL_Surface *source, *output;
  1236. SDL_Palette *palette;
  1237. Uint8 *pixels;
  1238. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1239. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1240. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1241. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1242. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1243. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1244. SDLTest_AssertCheck(source->w == SDL_arraysize(palette_colors) + SDL_arraysize(colors), "Expected source->w == %d, got %d", (int)(SDL_arraysize(palette_colors) + SDL_arraysize(colors)), source->w);
  1245. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1246. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1247. for (i = 0; i < SDL_arraysize(colors); i++) {
  1248. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1249. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1250. }
  1251. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1252. result = SDL_WriteSurfacePixel(source, SDL_arraysize(colors) + i, 0, palette_colors[i].r, palette_colors[i].g, palette_colors[i].b, palette_colors[i].a);
  1253. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1254. }
  1255. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1256. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1257. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1258. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1259. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1260. pixels = output->pixels;
  1261. for (i = 0; i < SDL_arraysize(colors); i++) {
  1262. int idx = i;
  1263. Uint8 actual = pixels[idx];
  1264. Uint8 expected = colors[i].e;
  1265. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1266. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1267. }
  1268. SDLTest_AssertPass("Check palette 1:1 mapping");
  1269. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1270. int idx = SDL_arraysize(colors) + i;
  1271. Uint8 actual = pixels[idx];
  1272. Uint8 expected = i;
  1273. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1274. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1275. }
  1276. SDL_DestroyPalette(palette);
  1277. SDL_DestroySurface(source);
  1278. SDL_DestroySurface(output);
  1279. return TEST_COMPLETED;
  1280. }
  1281. static int SDLCALL surface_testClearSurface(void *arg)
  1282. {
  1283. SDL_PixelFormat formats[] = {
  1284. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1285. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1286. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1287. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1288. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1289. };
  1290. SDL_Surface *surface;
  1291. SDL_PixelFormat format;
  1292. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1293. const float MAXIMUM_ERROR_YUV = 0.01f;
  1294. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1295. float actualR, actualG, actualB, actualA;
  1296. float deltaR, deltaG, deltaB, deltaA;
  1297. int i, ret;
  1298. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1299. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1300. format = formats[i];
  1301. surface = SDL_CreateSurface(1, 1, format);
  1302. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1303. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1304. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1305. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1306. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1307. deltaR = SDL_fabsf(actualR - srcR);
  1308. deltaG = SDL_fabsf(actualG - srcG);
  1309. deltaB = SDL_fabsf(actualB - srcB);
  1310. deltaA = SDL_fabsf(actualA - srcA);
  1311. SDLTest_AssertCheck(
  1312. deltaR <= MAXIMUM_ERROR &&
  1313. deltaG <= MAXIMUM_ERROR &&
  1314. deltaB <= MAXIMUM_ERROR &&
  1315. deltaA <= MAXIMUM_ERROR,
  1316. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1317. SDL_GetPixelFormatName(format),
  1318. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1319. SDL_DestroySurface(surface);
  1320. }
  1321. return TEST_COMPLETED;
  1322. }
  1323. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1324. {
  1325. SDL_PixelFormat formats[] = {
  1326. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1327. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1328. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1329. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1330. };
  1331. SDL_Surface *surface;
  1332. SDL_PixelFormat format;
  1333. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1334. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1335. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1336. float expectedR = srcR * srcA;
  1337. float expectedG = srcG * srcA;
  1338. float expectedB = srcB * srcA;
  1339. float actualR, actualG, actualB;
  1340. float deltaR, deltaG, deltaB;
  1341. int i, ret;
  1342. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1343. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1344. format = formats[i];
  1345. surface = SDL_CreateSurface(1, 1, format);
  1346. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1347. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1348. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1349. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1350. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1351. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1352. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1353. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1354. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1355. deltaR = SDL_fabsf(actualR - expectedR);
  1356. deltaG = SDL_fabsf(actualG - expectedG);
  1357. deltaB = SDL_fabsf(actualB - expectedB);
  1358. SDLTest_AssertCheck(
  1359. deltaR <= MAXIMUM_ERROR &&
  1360. deltaG <= MAXIMUM_ERROR &&
  1361. deltaB <= MAXIMUM_ERROR,
  1362. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1363. SDL_GetPixelFormatName(format),
  1364. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1365. SDL_DestroySurface(surface);
  1366. }
  1367. return TEST_COMPLETED;
  1368. }
  1369. static int SDLCALL surface_testScale(void *arg)
  1370. {
  1371. SDL_PixelFormat formats[] = {
  1372. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1373. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1374. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1375. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1376. };
  1377. SDL_ScaleMode modes[] = {
  1378. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR, SDL_SCALEMODE_PIXELART
  1379. };
  1380. SDL_Surface *surface, *result;
  1381. SDL_PixelFormat format;
  1382. SDL_ScaleMode mode;
  1383. const float MAXIMUM_ERROR = 0.0001f;
  1384. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1385. float actualR, actualG, actualB, actualA;
  1386. float deltaR, deltaG, deltaB, deltaA;
  1387. int i, j, ret;
  1388. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1389. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1390. format = formats[i];
  1391. mode = modes[j];
  1392. surface = SDL_CreateSurface(1, 1, format);
  1393. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1394. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1395. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1396. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1397. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1398. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1399. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1400. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1401. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1402. deltaR = SDL_fabsf(actualR - srcR);
  1403. deltaG = SDL_fabsf(actualG - srcG);
  1404. deltaB = SDL_fabsf(actualB - srcB);
  1405. deltaA = SDL_fabsf(actualA - srcA);
  1406. SDLTest_AssertCheck(
  1407. deltaR <= MAXIMUM_ERROR &&
  1408. deltaG <= MAXIMUM_ERROR &&
  1409. deltaB <= MAXIMUM_ERROR &&
  1410. deltaA <= MAXIMUM_ERROR,
  1411. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1412. SDL_GetPixelFormatName(format),
  1413. mode == SDL_SCALEMODE_NEAREST ? "nearest" :
  1414. mode == SDL_SCALEMODE_LINEAR ? "linear" :
  1415. mode == SDL_SCALEMODE_PIXELART ? "pixelart" : "unknown",
  1416. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1417. SDL_DestroySurface(surface);
  1418. SDL_DestroySurface(result);
  1419. }
  1420. }
  1421. return TEST_COMPLETED;
  1422. }
  1423. #define GENERATE_SHIFTS
  1424. static Uint32 Calculate(int v, int bits, int vmax, int shift)
  1425. {
  1426. #if defined(GENERATE_FLOOR)
  1427. return (Uint32)SDL_floor(v * 255.0f / vmax) << shift;
  1428. #elif defined(GENERATE_ROUND)
  1429. return (Uint32)SDL_roundf(v * 255.0f / vmax) << shift;
  1430. #elif defined(GENERATE_SHIFTS)
  1431. switch (bits) {
  1432. case 1:
  1433. v = (v << 7) | (v << 6) | (v << 5) | (v << 4) | (v << 3) | (v << 2) | (v << 1) | v;
  1434. break;
  1435. case 2:
  1436. v = (v << 6) | (v << 4) | (v << 2) | v;
  1437. break;
  1438. case 3:
  1439. v = (v << 5) | (v << 2) | (v >> 1);
  1440. break;
  1441. case 4:
  1442. v = (v << 4) | v;
  1443. break;
  1444. case 5:
  1445. v = (v << 3) | (v >> 2);
  1446. break;
  1447. case 6:
  1448. v = (v << 2) | (v >> 4);
  1449. break;
  1450. case 7:
  1451. v = (v << 1) | (v >> 6);
  1452. break;
  1453. case 8:
  1454. break;
  1455. }
  1456. return (Uint32)v << shift;
  1457. #endif
  1458. }
  1459. static Uint32 Calculate565toARGB(int v, const SDL_PixelFormatDetails *fmt)
  1460. {
  1461. Uint8 r = (v & 0xF800) >> 11;
  1462. Uint8 g = (v & 0x07E0) >> 5;
  1463. Uint8 b = (v & 0x001F);
  1464. return fmt->Amask |
  1465. Calculate(r, 5, 31, fmt->Rshift) |
  1466. Calculate(g, 6, 63, fmt->Gshift) |
  1467. Calculate(b, 5, 31, fmt->Bshift);
  1468. }
  1469. static int SDLCALL surface_test16BitTo32Bit(void *arg)
  1470. {
  1471. static const SDL_PixelFormat formats[] = {
  1472. SDL_PIXELFORMAT_ARGB8888,
  1473. SDL_PIXELFORMAT_ABGR8888,
  1474. SDL_PIXELFORMAT_RGBA8888,
  1475. SDL_PIXELFORMAT_BGRA8888
  1476. };
  1477. static Uint16 pixels[1 << 16];
  1478. static Uint32 expected[1 << 16];
  1479. int i, p, ret;
  1480. SDL_Surface *surface16;
  1481. SDL_Surface *surface32;
  1482. SDL_Surface *expected32;
  1483. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1484. pixels[p] = p;
  1485. }
  1486. surface16 = SDL_CreateSurfaceFrom(SDL_arraysize(pixels), 1, SDL_PIXELFORMAT_RGB565, pixels, sizeof(pixels));
  1487. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1488. SDL_PixelFormat format = formats[i];
  1489. const SDL_PixelFormatDetails *fmt = SDL_GetPixelFormatDetails(format);
  1490. SDLTest_Log("Checking conversion from SDL_PIXELFORMAT_RGB565 to %s", SDL_GetPixelFormatName(format));
  1491. surface32 = SDL_ConvertSurface(surface16, format);
  1492. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1493. expected[p] = Calculate565toARGB(p, fmt);
  1494. }
  1495. expected32 = SDL_CreateSurfaceFrom(SDL_arraysize(expected), 1, format, expected, sizeof(expected));
  1496. ret = SDLTest_CompareSurfaces(surface32, expected32, 0);
  1497. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1498. SDL_DestroySurface(surface32);
  1499. SDL_DestroySurface(expected32);
  1500. }
  1501. SDL_DestroySurface(surface16);
  1502. return TEST_COMPLETED;
  1503. }
  1504. /* ================= Test References ================== */
  1505. /* Surface test cases */
  1506. static const SDLTest_TestCaseReference surfaceTestInvalidFormat = {
  1507. surface_testInvalidFormat, "surface_testInvalidFormat", "Tests creating surface with invalid format", TEST_ENABLED
  1508. };
  1509. static const SDLTest_TestCaseReference surfaceTestSaveLoad = {
  1510. surface_testSaveLoad, "surface_testSaveLoad", "Tests sprite saving and loading.", TEST_ENABLED
  1511. };
  1512. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1513. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1514. };
  1515. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1516. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1517. };
  1518. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1519. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1520. };
  1521. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1522. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1523. };
  1524. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1525. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1526. };
  1527. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1528. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1529. };
  1530. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1531. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1532. };
  1533. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1534. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1535. };
  1536. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1537. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1538. };
  1539. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1540. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1541. };
  1542. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1543. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1544. };
  1545. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1546. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1547. };
  1548. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1549. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1550. };
  1551. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1552. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1553. };
  1554. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1555. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1556. };
  1557. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1558. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1559. };
  1560. static const SDLTest_TestCaseReference surfaceTestBlitInvalid = {
  1561. surface_testBlitInvalid, "surface_testBlitInvalid", "Tests blitting routines with invalid surfaces.", TEST_ENABLED
  1562. };
  1563. static const SDLTest_TestCaseReference surfaceTestBlitsWithBadCoordinates = {
  1564. surface_testBlitsWithBadCoordinates, "surface_testBlitsWithBadCoordinates", "Test blitting routines with bad coordinates.", TEST_ENABLED
  1565. };
  1566. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1567. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1568. };
  1569. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1570. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1571. };
  1572. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1573. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1574. };
  1575. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1576. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1577. };
  1578. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1579. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1580. };
  1581. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1582. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1583. };
  1584. static const SDLTest_TestCaseReference surfaceTestScale = {
  1585. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1586. };
  1587. static const SDLTest_TestCaseReference surfaceTest16BitTo32Bit = {
  1588. surface_test16BitTo32Bit, "surface_test16BitTo32Bit", "Test conversion from 16-bit to 32-bit pixels.", TEST_ENABLED
  1589. };
  1590. /* Sequence of Surface test cases */
  1591. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1592. &surfaceTestInvalidFormat,
  1593. &surfaceTestSaveLoad,
  1594. &surfaceTestBlitZeroSource,
  1595. &surfaceTestBlit,
  1596. &surfaceTestBlitTiled,
  1597. &surfaceTestBlit9Grid,
  1598. &surfaceTestBlitMultiple,
  1599. &surfaceTestLoadFailure,
  1600. &surfaceTestSurfaceConversion,
  1601. &surfaceTestCompleteSurfaceConversion,
  1602. &surfaceTestBlitColorMod,
  1603. &surfaceTestBlitAlphaMod,
  1604. &surfaceTestBlitBlendBlend,
  1605. &surfaceTestBlitBlendPremultiplied,
  1606. &surfaceTestBlitBlendAdd,
  1607. &surfaceTestBlitBlendAddPremultiplied,
  1608. &surfaceTestBlitBlendMod,
  1609. &surfaceTestBlitBlendMul,
  1610. &surfaceTestBlitInvalid,
  1611. &surfaceTestBlitsWithBadCoordinates,
  1612. &surfaceTestOverflow,
  1613. &surfaceTestFlip,
  1614. &surfaceTestPalette,
  1615. &surfaceTestPalettization,
  1616. &surfaceTestClearSurface,
  1617. &surfaceTestPremultiplyAlpha,
  1618. &surfaceTestScale,
  1619. &surfaceTest16BitTo32Bit,
  1620. NULL
  1621. };
  1622. /* Surface test suite (global) */
  1623. SDLTest_TestSuiteReference surfaceTestSuite = {
  1624. "Surface",
  1625. surfaceSetUp,
  1626. surfaceTests,
  1627. surfaceTearDown
  1628. };