testautomation_surface.c 91 KB

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