testautomation_surface.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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. int ret;
  753. bool result;
  754. face = SDLTest_ImageFace();
  755. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  756. if (face == NULL) {
  757. return TEST_ABORTED;
  758. }
  759. /* RLE encoding only works for 32-bit surfaces with alpha in the high bits */
  760. if (face->format != SDL_PIXELFORMAT_ARGB8888) {
  761. tmp = SDL_ConvertSurface(face, SDL_PIXELFORMAT_ARGB8888);
  762. SDLTest_AssertCheck(tmp != NULL, "Verify tmp surface is not NULL");
  763. if (tmp == NULL) {
  764. return TEST_ABORTED;
  765. }
  766. SDL_DestroySurface(face);
  767. face = tmp;
  768. }
  769. /* Create a temporary surface to trigger RLE encoding during blit */
  770. tmp = SDL_DuplicateSurface(face);
  771. SDLTest_AssertCheck(tmp != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  772. result = SDL_SetSurfaceRLE(face, true);
  773. SDLTest_AssertCheck(result, "Verify result from SDL_SetSurfaceRLE() is true");
  774. /* Test duplication with RLE pixels */
  775. a = SDL_DuplicateSurface(face);
  776. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  777. SDLTest_AssertCheck(SDL_SurfaceHasRLE(a), "Verify result from SDL_DuplicateSurface() with RLE pixels has RLE set");
  778. ret = SDLTest_CompareSurfaces(a, face, 0);
  779. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  780. /* Verify that blitting from an RLE surface does RLE encode it */
  781. SDLTest_AssertCheck(!SDL_MUSTLOCK(a), "Verify initial RLE surface does not need to be locked");
  782. SDLTest_AssertCheck(a->pixels != NULL, "Verify initial RLE surface has pixels available");
  783. result = SDL_BlitSurface(a, NULL, tmp, NULL);
  784. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurface() with RLE surface is true");
  785. SDLTest_AssertCheck(SDL_MUSTLOCK(a), "Verify RLE surface after blit needs to be locked");
  786. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after blit does not have pixels available");
  787. ret = SDLTest_CompareSurfaces(tmp, face, 0);
  788. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  789. /* Test scaling with RLE pixels */
  790. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  791. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() is not NULL");
  792. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ScaleSurface() with RLE pixels has RLE set");
  793. /* Test scaling blitting with RLE pixels */
  794. result = SDL_BlitSurfaceScaled(a, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  795. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurfaceScaled() with src having RLE pixels is true");
  796. SDL_BlitSurface(a, NULL, tmp, NULL);
  797. SDL_DestroySurface(b);
  798. b = NULL;
  799. /* Test conversion with RLE pixels */
  800. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  801. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels is not NULL");
  802. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels has RLE set");
  803. ret = SDLTest_CompareSurfacesIgnoreTransparentPixels(b, face, 0);
  804. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  805. SDL_BlitSurface(a, NULL, tmp, NULL);
  806. SDL_DestroySurface(b);
  807. b = NULL;
  808. #if 0 /* This will currently fail, you must lock the surface first */
  809. /* Test filling surface with RLE pixels */
  810. result = SDL_FillSurfaceRect(a, NULL, 0);
  811. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having RLE pixels is true");
  812. #endif
  813. /* Make sure the RLE surface still needs to be locked after surface operations */
  814. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after operations does not have pixels available");
  815. /* Clean up. */
  816. SDL_DestroySurface(face);
  817. SDL_DestroySurface(a);
  818. SDL_DestroySurface(b);
  819. SDL_DestroySurface(tmp);
  820. return TEST_COMPLETED;
  821. }
  822. /**
  823. * Tests surface conversion.
  824. */
  825. static int SDLCALL surface_testSurfaceConversion(void *arg)
  826. {
  827. SDL_Surface *rface = NULL, *face = NULL;
  828. int ret = 0;
  829. /* Create sample surface */
  830. face = SDLTest_ImageFace();
  831. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  832. if (face == NULL) {
  833. return TEST_ABORTED;
  834. }
  835. /* Set transparent pixel as the pixel at (0,0) */
  836. if (SDL_GetSurfacePalette(face)) {
  837. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  838. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  839. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  840. }
  841. /* Convert to 32 bit to compare. */
  842. rface = SDL_ConvertSurface(face, testSurface->format);
  843. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  844. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  845. /* Compare surface. */
  846. ret = SDLTest_CompareSurfaces(rface, face, 0);
  847. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  848. /* Clean up. */
  849. SDL_DestroySurface(face);
  850. SDL_DestroySurface(rface);
  851. return TEST_COMPLETED;
  852. }
  853. /**
  854. * Tests surface conversion across all pixel formats.
  855. */
  856. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  857. {
  858. Uint32 pixel_formats[] = {
  859. SDL_PIXELFORMAT_INDEX8,
  860. SDL_PIXELFORMAT_RGB332,
  861. SDL_PIXELFORMAT_XRGB4444,
  862. SDL_PIXELFORMAT_XBGR4444,
  863. SDL_PIXELFORMAT_XRGB1555,
  864. SDL_PIXELFORMAT_XBGR1555,
  865. SDL_PIXELFORMAT_ARGB4444,
  866. SDL_PIXELFORMAT_RGBA4444,
  867. SDL_PIXELFORMAT_ABGR4444,
  868. SDL_PIXELFORMAT_BGRA4444,
  869. SDL_PIXELFORMAT_ARGB1555,
  870. SDL_PIXELFORMAT_RGBA5551,
  871. SDL_PIXELFORMAT_ABGR1555,
  872. SDL_PIXELFORMAT_BGRA5551,
  873. SDL_PIXELFORMAT_RGB565,
  874. SDL_PIXELFORMAT_BGR565,
  875. SDL_PIXELFORMAT_RGB24,
  876. SDL_PIXELFORMAT_BGR24,
  877. SDL_PIXELFORMAT_XRGB8888,
  878. SDL_PIXELFORMAT_RGBX8888,
  879. SDL_PIXELFORMAT_XBGR8888,
  880. SDL_PIXELFORMAT_BGRX8888,
  881. SDL_PIXELFORMAT_ARGB8888,
  882. SDL_PIXELFORMAT_RGBA8888,
  883. SDL_PIXELFORMAT_ABGR8888,
  884. SDL_PIXELFORMAT_BGRA8888,
  885. #if 0 /* We aren't testing HDR10 colorspace conversion */
  886. SDL_PIXELFORMAT_XRGB2101010,
  887. SDL_PIXELFORMAT_XBGR2101010,
  888. SDL_PIXELFORMAT_ARGB2101010,
  889. SDL_PIXELFORMAT_ABGR2101010,
  890. #endif
  891. };
  892. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  893. const SDL_PixelFormatDetails *fmt1, *fmt2;
  894. int i, j, ret = 0;
  895. /* Create sample surface */
  896. face = SDLTest_ImageFace();
  897. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  898. if (face == NULL) {
  899. return TEST_ABORTED;
  900. }
  901. /* Set transparent pixel as the pixel at (0,0) */
  902. if (SDL_GetSurfacePalette(face)) {
  903. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  904. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  905. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  906. }
  907. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  908. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  909. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  910. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  911. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  912. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  913. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  914. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  915. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  916. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  917. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  918. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  919. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  920. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  921. if (fmt1 && fmt2 &&
  922. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  923. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  924. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  925. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  926. final = SDL_ConvertSurface(cvt2, face->format);
  927. SDL_assert(final != NULL);
  928. /* Compare surface. */
  929. ret = SDLTest_CompareSurfaces(face, final, 0);
  930. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  931. SDL_DestroySurface(final);
  932. }
  933. SDL_DestroySurface(cvt1);
  934. SDL_DestroySurface(cvt2);
  935. }
  936. }
  937. /* Clean up. */
  938. SDL_DestroySurface(face);
  939. return TEST_COMPLETED;
  940. }
  941. /**
  942. * Tests sprite loading. A failure case.
  943. */
  944. static int SDLCALL surface_testLoadFailure(void *arg)
  945. {
  946. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  947. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  948. return TEST_COMPLETED;
  949. }
  950. /**
  951. * Tests blitting from a zero sized source rectangle
  952. */
  953. static int SDLCALL surface_testBlitZeroSource(void *arg)
  954. {
  955. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  956. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  957. SDL_Rect srcrect = { 0, 0, 0, 0 };
  958. int ret;
  959. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  960. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  961. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  962. ret = SDLTest_CompareSurfaces(dst, src, 0);
  963. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  964. SDL_DestroySurface(src);
  965. SDL_DestroySurface(dst);
  966. return TEST_COMPLETED;
  967. }
  968. /**
  969. * Tests some blitting routines.
  970. */
  971. static int SDLCALL surface_testBlit(void *arg)
  972. {
  973. /* Basic blitting */
  974. testBlitBlendMode(SDL_BLENDMODE_NONE);
  975. return TEST_COMPLETED;
  976. }
  977. /**
  978. * Tests some blitting routines with color mod
  979. */
  980. static int SDLCALL surface_testBlitColorMod(void *arg)
  981. {
  982. /* Basic blitting with color mod */
  983. testBlitBlendMode(-1);
  984. return TEST_COMPLETED;
  985. }
  986. /**
  987. * Tests some blitting routines with alpha mod
  988. */
  989. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  990. {
  991. /* Basic blitting with alpha mod */
  992. testBlitBlendMode(-2);
  993. return TEST_COMPLETED;
  994. }
  995. /**
  996. * Tests some more blitting routines.
  997. */
  998. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  999. {
  1000. /* Blend blitting */
  1001. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  1002. return TEST_COMPLETED;
  1003. }
  1004. /**
  1005. * @brief Tests some more blitting routines.
  1006. */
  1007. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  1008. {
  1009. /* Blend premultiplied blitting */
  1010. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  1011. return TEST_COMPLETED;
  1012. }
  1013. /**
  1014. * Tests some more blitting routines.
  1015. */
  1016. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  1017. {
  1018. /* Add blitting */
  1019. testBlitBlendMode(SDL_BLENDMODE_ADD);
  1020. return TEST_COMPLETED;
  1021. }
  1022. /**
  1023. * Tests some more blitting routines.
  1024. */
  1025. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  1026. {
  1027. /* Add premultiplied blitting */
  1028. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  1029. return TEST_COMPLETED;
  1030. }
  1031. /**
  1032. * Tests some more blitting routines.
  1033. */
  1034. static int SDLCALL surface_testBlitBlendMod(void *arg)
  1035. {
  1036. /* Mod blitting */
  1037. testBlitBlendMode(SDL_BLENDMODE_MOD);
  1038. return TEST_COMPLETED;
  1039. }
  1040. /**
  1041. * Tests some more blitting routines.
  1042. */
  1043. static int SDLCALL surface_testBlitBlendMul(void *arg)
  1044. {
  1045. /* Mod blitting */
  1046. testBlitBlendMode(SDL_BLENDMODE_MUL);
  1047. return TEST_COMPLETED;
  1048. }
  1049. /**
  1050. * Tests blitting bitmaps
  1051. */
  1052. static int SDLCALL surface_testBlitBitmap(void *arg)
  1053. {
  1054. const SDL_PixelFormat formats[] = {
  1055. SDL_PIXELFORMAT_INDEX1LSB,
  1056. SDL_PIXELFORMAT_INDEX1MSB,
  1057. SDL_PIXELFORMAT_INDEX2LSB,
  1058. SDL_PIXELFORMAT_INDEX2MSB,
  1059. SDL_PIXELFORMAT_INDEX4LSB,
  1060. SDL_PIXELFORMAT_INDEX4MSB
  1061. };
  1062. Uint8 pixel;
  1063. int i, j;
  1064. bool result;
  1065. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
  1066. SDL_Color colors[] = {
  1067. { 0x00, 0x00, 0x00, 0xFF },
  1068. { 0xFF, 0xFF, 0xFF, 0xFF }
  1069. };
  1070. SDL_Palette *palette;
  1071. Uint32 value, expected = 0xFFFFFFFF;
  1072. palette = SDL_CreatePalette(SDL_arraysize(colors));
  1073. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette() != NULL, result = %p", palette);
  1074. result = SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  1075. SDLTest_AssertCheck(result, "SDL_SetPaletteColors, result = %s", result ? "true" : "false");
  1076. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1077. SDL_PixelFormat format = formats[i];
  1078. int bpp = SDL_BITSPERPIXEL(format);
  1079. int width = (8 / bpp);
  1080. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1081. switch (bpp) {
  1082. case 1:
  1083. pixel = 0x80;
  1084. break;
  1085. case 2:
  1086. pixel = 0x40;
  1087. break;
  1088. case 4:
  1089. pixel = 0x10;
  1090. break;
  1091. default:
  1092. SDL_assert(!"Unexpected bpp");
  1093. break;
  1094. }
  1095. } else {
  1096. pixel = 0x01;
  1097. }
  1098. for (j = 0; j < width; ++j) {
  1099. SDL_Rect rect = { j, 0, 1, 1 };
  1100. SDL_Surface *src = SDL_CreateSurfaceFrom(width, 1, format, &pixel, 1);
  1101. SDL_SetSurfacePalette(src, palette);
  1102. *(Uint32 *)dst->pixels = 0;
  1103. result = SDL_BlitSurface(src, &rect, dst, NULL);
  1104. SDLTest_AssertCheck(result, "SDL_BlitSurface(%s pixel %d), result = %s", SDL_GetPixelFormatName(format), j, result ? "true" : "false");
  1105. value = *(Uint32 *)dst->pixels;
  1106. SDLTest_AssertCheck(value == expected, "Expected value == 0x%" SDL_PRIx32 ", actually = 0x%" SDL_PRIx32, expected, value);
  1107. SDL_DestroySurface(src);
  1108. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1109. pixel >>= bpp;
  1110. } else {
  1111. pixel <<= bpp;
  1112. }
  1113. }
  1114. }
  1115. SDL_DestroyPalette(palette);
  1116. SDL_DestroySurface(dst);
  1117. return TEST_COMPLETED;
  1118. }
  1119. /**
  1120. * Tests blitting invalid surfaces.
  1121. */
  1122. static int SDLCALL surface_testBlitInvalid(void *arg)
  1123. {
  1124. SDL_Surface *valid, *invalid;
  1125. bool result;
  1126. valid = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1127. SDLTest_AssertCheck(valid != NULL, "Check surface creation");
  1128. invalid = SDL_CreateSurface(0, 0, SDL_PIXELFORMAT_RGBA8888);
  1129. SDLTest_AssertCheck(invalid != NULL, "Check surface creation");
  1130. SDLTest_AssertCheck(invalid->pixels == NULL, "Check surface pixels are NULL");
  1131. result = SDL_BlitSurface(invalid, NULL, valid, NULL);
  1132. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(invalid, NULL, valid, NULL), result = %s", result ? "true" : "false");
  1133. result = SDL_BlitSurface(valid, NULL, invalid, NULL);
  1134. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(valid, NULL, invalid, NULL), result = %s", result ? "true" : "false");
  1135. result = SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST);
  1136. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1137. result = SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST);
  1138. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1139. SDL_DestroySurface(valid);
  1140. SDL_DestroySurface(invalid);
  1141. return TEST_COMPLETED;
  1142. }
  1143. static int SDLCALL surface_testBlitsWithBadCoordinates(void *arg)
  1144. {
  1145. const SDL_Rect rect[8] = {
  1146. { SDL_MAX_SINT32, 0, 2, 2 },
  1147. { 0, SDL_MAX_SINT32, 2, 2 },
  1148. { 0, 0, SDL_MAX_SINT32, 2 },
  1149. { 0, 0, 2, SDL_MAX_SINT32 },
  1150. { SDL_MIN_SINT32, 0, 2, 2 },
  1151. { 0, SDL_MIN_SINT32, 2, 2 },
  1152. { 0, 0, SDL_MIN_SINT32, 2 },
  1153. { 0, 0, 2, SDL_MIN_SINT32 }
  1154. };
  1155. SDL_Surface *s;
  1156. bool result;
  1157. int i;
  1158. s = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1159. SDLTest_AssertCheck(s != NULL, "Check surface creation");
  1160. for (i = 0; i < 8; i++) {
  1161. result = SDL_BlitSurface(s, NULL, s, &rect[i]);
  1162. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, valid, &rect), result = %s", result ? "true" : "false");
  1163. result = SDL_BlitSurface(s, &rect[i], s, NULL);
  1164. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, &rect, valid, NULL), result = %s", result ? "true" : "false");
  1165. result = SDL_BlitSurfaceScaled(s, NULL, s, &rect[i], SDL_SCALEMODE_NEAREST);
  1166. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, NULL, valid, &rect, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1167. result = SDL_BlitSurfaceScaled(s, &rect[i], s, NULL, SDL_SCALEMODE_NEAREST);
  1168. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, &rect, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1169. }
  1170. SDL_DestroySurface(s);
  1171. return TEST_COMPLETED;
  1172. }
  1173. static int SDLCALL surface_testOverflow(void *arg)
  1174. {
  1175. char buf[1024];
  1176. const char *expectedError;
  1177. SDL_Surface *surface;
  1178. SDL_memset(buf, '\0', sizeof(buf));
  1179. expectedError = "Parameter 'width' is invalid";
  1180. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  1181. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1182. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1183. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1184. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1185. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1186. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1187. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1188. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1189. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1190. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1191. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1192. expectedError = "Parameter 'height' is invalid";
  1193. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  1194. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1195. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1196. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1197. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1198. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1199. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1200. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1201. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1202. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1203. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1204. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1205. expectedError = "Parameter 'pitch' is invalid";
  1206. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  1207. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1208. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1209. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1210. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  1211. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1212. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1213. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1214. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  1215. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  1216. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1217. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1218. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  1219. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  1220. surface != NULL ? "(success)" : SDL_GetError());
  1221. SDL_DestroySurface(surface);
  1222. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  1223. * the width, but it must be enough to hold the appropriate number of
  1224. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  1225. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1226. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1227. surface != NULL ? "(success)" : SDL_GetError());
  1228. SDL_DestroySurface(surface);
  1229. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1230. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1231. surface != NULL ? "(success)" : SDL_GetError());
  1232. SDL_DestroySurface(surface);
  1233. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1234. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1235. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1236. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1237. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1238. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1239. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1240. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1241. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  1242. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1243. surface != NULL ? "(success)" : SDL_GetError());
  1244. SDL_DestroySurface(surface);
  1245. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  1246. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1247. surface != NULL ? "(success)" : SDL_GetError());
  1248. SDL_DestroySurface(surface);
  1249. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  1250. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1251. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1252. surface != NULL ? "(success)" : SDL_GetError());
  1253. SDL_DestroySurface(surface);
  1254. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1255. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1256. surface != NULL ? "(success)" : SDL_GetError());
  1257. SDL_DestroySurface(surface);
  1258. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1259. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  1260. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1261. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1262. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1263. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1264. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1265. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1266. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  1267. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1268. surface != NULL ? "(success)" : SDL_GetError());
  1269. SDL_DestroySurface(surface);
  1270. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  1271. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1272. surface != NULL ? "(success)" : SDL_GetError());
  1273. SDL_DestroySurface(surface);
  1274. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  1275. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1276. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1277. surface != NULL ? "(success)" : SDL_GetError());
  1278. SDL_DestroySurface(surface);
  1279. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1280. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1281. surface != NULL ? "(success)" : SDL_GetError());
  1282. SDL_DestroySurface(surface);
  1283. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1284. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1285. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1286. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1287. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1288. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1289. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1290. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1291. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  1292. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1293. surface != NULL ? "(success)" : SDL_GetError());
  1294. SDL_DestroySurface(surface);
  1295. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  1296. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1297. surface != NULL ? "(success)" : SDL_GetError());
  1298. SDL_DestroySurface(surface);
  1299. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  1300. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1301. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1302. surface != NULL ? "(success)" : SDL_GetError());
  1303. SDL_DestroySurface(surface);
  1304. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1305. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1306. surface != NULL ? "(success)" : SDL_GetError());
  1307. SDL_DestroySurface(surface);
  1308. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1309. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1310. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1311. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1312. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1313. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1314. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1315. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1316. /* Everything else requires more than 1 byte per pixel, and rounds up
  1317. * each pixel to an integer number of bytes (e.g. RGB555 is really
  1318. * XRGB1555, with 1 bit per pixel wasted). */
  1319. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1320. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1321. surface != NULL ? "(success)" : SDL_GetError());
  1322. SDL_DestroySurface(surface);
  1323. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1324. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1325. surface != NULL ? "(success)" : SDL_GetError());
  1326. SDL_DestroySurface(surface);
  1327. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1328. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1329. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1330. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1331. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1332. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1333. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1334. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1335. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  1336. SDL_ClearError();
  1337. expectedError = "aligning pitch would overflow";
  1338. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  1339. * alignment padding makes it overflow */
  1340. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  1341. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  1342. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1343. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1344. SDL_ClearError();
  1345. expectedError = "width * bpp would overflow";
  1346. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  1347. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  1348. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  1349. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1350. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1351. SDL_ClearError();
  1352. expectedError = "height * pitch would overflow";
  1353. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  1354. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  1355. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1356. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1357. SDL_ClearError();
  1358. expectedError = "height * pitch would overflow";
  1359. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  1360. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  1361. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1362. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1363. } else {
  1364. SDLTest_Log("Can't easily overflow size_t on this platform");
  1365. }
  1366. return TEST_COMPLETED;
  1367. }
  1368. static int surface_testSetGetSurfaceClipRect(void *args)
  1369. {
  1370. const struct {
  1371. SDL_Rect r;
  1372. bool clipsetval;
  1373. bool cmpval;
  1374. } rect_list[] = {
  1375. { { 0, 0, 0, 0}, false, true},
  1376. { { 2, 2, 0, 0}, false, true},
  1377. { { 2, 2, 5, 1}, true, true},
  1378. { { 6, 5, 10, 3}, true, false},
  1379. { { 0, 0, 10, 10}, true, true},
  1380. { { 0, 0, -10, 10}, false, true},
  1381. { { 0, 0, -10, -10}, false, true},
  1382. { { -10, -10, 10, 10}, false, false},
  1383. { { 10, -10, 10, 10}, false, false},
  1384. { { 10, 10, 10, 10}, true, false}
  1385. };
  1386. SDL_Surface *s;
  1387. SDL_Rect r;
  1388. int i;
  1389. bool b;
  1390. SDLTest_AssertPass("About to call SDL_CreateSurface(15, 15, SDL_PIXELFORMAT_RGBA32)");
  1391. s = SDL_CreateSurface(15, 15, SDL_PIXELFORMAT_RGBA32);
  1392. SDLTest_AssertCheck(s != NULL, "SDL_CreateSurface returned non-null surface");
  1393. SDL_zero(r);
  1394. b = SDL_GetSurfaceClipRect(s, &r);
  1395. SDLTest_AssertCheck(b, "SDL_GetSurfaceClipRect succeeded (%s)", SDL_GetError());
  1396. SDLTest_AssertCheck(r.x == 0 && r.y == 0 && r.w == 15 && r.h == 15,
  1397. "SDL_GetSurfaceClipRect of just-created surface. Got {%d, %d, %d, %d}. (Expected {%d, %d, %d, %d})",
  1398. r.x, r.y, r.w, r.h, 0, 0, 15, 15);
  1399. for (i = 0; i < SDL_arraysize(rect_list); i++) {
  1400. const SDL_Rect *r_in = &rect_list[i].r;
  1401. SDL_Rect r_out;
  1402. SDLTest_AssertPass("About to do SDL_SetClipRect({%d, %d, %d, %d})", r_in->x, r_in->y, r_in->w, r_in->h);
  1403. b = SDL_SetSurfaceClipRect(s, r_in);
  1404. SDLTest_AssertCheck(b == rect_list[i].clipsetval, "SDL_SetSurfaceClipRect returned %d (expected %d)", b, rect_list[i].clipsetval);
  1405. SDL_zero(r_out);
  1406. SDL_GetSurfaceClipRect(s, &r_out);
  1407. SDLTest_AssertPass("SDL_GetSurfaceClipRect returned {%d, %d, %d, %d}", r_out.x, r_out.y, r_out.w, r_out.h);
  1408. b = r_out.x == r_in->x && r_out.y == r_in->y && r_out.w == r_in->w && r_out.h == r_in->h;
  1409. SDLTest_AssertCheck(b == rect_list[i].cmpval, "Current clipping rect is identical to input clipping rect: %d (expected %d)",
  1410. b, rect_list[i].cmpval);
  1411. }
  1412. SDL_DestroySurface(s);
  1413. return TEST_COMPLETED;
  1414. };
  1415. static int SDLCALL surface_testFlip(void *arg)
  1416. {
  1417. SDL_Surface *surface;
  1418. Uint8 *pixels;
  1419. int offset;
  1420. const char *expectedError;
  1421. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1422. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1423. SDL_ClearError();
  1424. expectedError = "Parameter 'surface' is invalid";
  1425. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1426. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1427. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1428. SDL_ClearError();
  1429. expectedError = "Parameter 'flip' is invalid";
  1430. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1431. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1432. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1433. pixels = (Uint8 *)surface->pixels;
  1434. *pixels = 0xFF;
  1435. offset = 0;
  1436. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1437. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1438. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1439. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1440. offset = 2 * surface->pitch;
  1441. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1442. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1443. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1444. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1445. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1446. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1447. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1448. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1449. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1450. SDL_DestroySurface(surface);
  1451. return TEST_COMPLETED;
  1452. }
  1453. static int SDLCALL surface_testPalette(void *arg)
  1454. {
  1455. SDL_Surface *source, *surface, *output;
  1456. SDL_Palette *palette;
  1457. Uint8 *pixels;
  1458. palette = SDL_CreatePalette(2);
  1459. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1460. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1461. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1462. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1463. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1464. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1465. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1466. pixels = (Uint8 *)surface->pixels;
  1467. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1468. /* Identity copy between indexed surfaces without a palette */
  1469. *(Uint8 *)source->pixels = 1;
  1470. SDL_BlitSurface(source, NULL, surface, NULL);
  1471. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1472. /* Identity copy between indexed surfaces where the source has a palette */
  1473. palette->colors[0].r = 0;
  1474. palette->colors[0].g = 0;
  1475. palette->colors[0].b = 0;
  1476. palette->colors[1].r = 0xFF;
  1477. palette->colors[1].g = 0;
  1478. palette->colors[1].b = 0;
  1479. SDL_SetSurfacePalette(source, palette);
  1480. *pixels = 0;
  1481. SDL_BlitSurface(source, NULL, surface, NULL);
  1482. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1483. /* Identity copy between indexed surfaces where the destination has a palette */
  1484. palette->colors[0].r = 0;
  1485. palette->colors[0].g = 0;
  1486. palette->colors[0].b = 0;
  1487. palette->colors[1].r = 0xFF;
  1488. palette->colors[1].g = 0;
  1489. palette->colors[1].b = 0;
  1490. SDL_SetSurfacePalette(source, NULL);
  1491. SDL_SetSurfacePalette(surface, palette);
  1492. *pixels = 0;
  1493. SDL_BlitSurface(source, NULL, surface, NULL);
  1494. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1495. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1496. palette->colors[0].r = 0;
  1497. palette->colors[0].g = 0;
  1498. palette->colors[0].b = 0;
  1499. palette->colors[1].r = 0xFF;
  1500. palette->colors[1].g = 0;
  1501. palette->colors[1].b = 0;
  1502. SDL_SetSurfacePalette(source, palette);
  1503. SDL_SetSurfacePalette(surface, palette);
  1504. *pixels = 0;
  1505. SDL_BlitSurface(source, NULL, surface, NULL);
  1506. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1507. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1508. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1509. pixels = (Uint8 *)output->pixels;
  1510. SDL_BlitSurface(surface, NULL, output, NULL);
  1511. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1512. /* Set the palette color and blit again */
  1513. palette->colors[1].r = 0xAA;
  1514. SDL_SetSurfacePalette(surface, palette);
  1515. SDL_BlitSurface(surface, NULL, output, NULL);
  1516. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1517. SDL_DestroyPalette(palette);
  1518. SDL_DestroySurface(source);
  1519. SDL_DestroySurface(surface);
  1520. SDL_DestroySurface(output);
  1521. return TEST_COMPLETED;
  1522. }
  1523. static int SDLCALL surface_testPalettization(void *arg)
  1524. {
  1525. const SDL_Color palette_colors[] = {
  1526. { 0x80, 0x00, 0x00, 0xff },
  1527. { 0x00, 0x80, 0x00, 0xff },
  1528. { 0x00, 0x00, 0x80, 0xff },
  1529. { 0x40, 0x00, 0x00, 0xff },
  1530. { 0x00, 0x40, 0x00, 0xff },
  1531. { 0x00, 0x00, 0x40, 0xff },
  1532. { 0x00, 0x00, 0x00, 0xff },
  1533. { 0xff, 0x00, 0x00, 0xff },
  1534. { 0x00, 0xff, 0x00, 0xff },
  1535. { 0x00, 0x00, 0xff, 0xff },
  1536. { 0xff, 0xff, 0x00, 0xff },
  1537. { 0x00, 0xff, 0xff, 0xff },
  1538. { 0xff, 0x00, 0xff, 0xff },
  1539. };
  1540. const struct {
  1541. SDL_Color c;
  1542. Uint8 e;
  1543. } colors[] = {
  1544. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1545. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1546. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1547. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1548. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1549. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1550. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1551. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1552. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1553. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1554. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1555. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1556. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1557. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1558. };
  1559. int i;
  1560. int result;
  1561. SDL_Surface *source, *output;
  1562. SDL_Palette *palette;
  1563. Uint8 *pixels;
  1564. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1565. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1566. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1567. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1568. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1569. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1570. 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);
  1571. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1572. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1573. for (i = 0; i < SDL_arraysize(colors); i++) {
  1574. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1575. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1576. }
  1577. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1578. 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);
  1579. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1580. }
  1581. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1582. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1583. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1584. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1585. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1586. pixels = output->pixels;
  1587. for (i = 0; i < SDL_arraysize(colors); i++) {
  1588. int idx = i;
  1589. Uint8 actual = pixels[idx];
  1590. Uint8 expected = colors[i].e;
  1591. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1592. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1593. }
  1594. SDLTest_AssertPass("Check palette 1:1 mapping");
  1595. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1596. int idx = SDL_arraysize(colors) + i;
  1597. Uint8 actual = pixels[idx];
  1598. Uint8 expected = i;
  1599. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1600. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1601. }
  1602. SDL_DestroyPalette(palette);
  1603. SDL_DestroySurface(source);
  1604. SDL_DestroySurface(output);
  1605. return TEST_COMPLETED;
  1606. }
  1607. static int SDLCALL surface_testClearSurface(void *arg)
  1608. {
  1609. SDL_PixelFormat formats[] = {
  1610. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1611. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1612. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1613. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1614. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1615. };
  1616. SDL_Surface *surface;
  1617. SDL_PixelFormat format;
  1618. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1619. const float MAXIMUM_ERROR_YUV = 0.01f;
  1620. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1621. float actualR, actualG, actualB, actualA;
  1622. float deltaR, deltaG, deltaB, deltaA;
  1623. int i, ret;
  1624. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1625. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1626. format = formats[i];
  1627. surface = SDL_CreateSurface(1, 1, format);
  1628. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1629. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1630. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1631. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1632. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1633. deltaR = SDL_fabsf(actualR - srcR);
  1634. deltaG = SDL_fabsf(actualG - srcG);
  1635. deltaB = SDL_fabsf(actualB - srcB);
  1636. deltaA = SDL_fabsf(actualA - srcA);
  1637. SDLTest_AssertCheck(
  1638. deltaR <= MAXIMUM_ERROR &&
  1639. deltaG <= MAXIMUM_ERROR &&
  1640. deltaB <= MAXIMUM_ERROR &&
  1641. deltaA <= MAXIMUM_ERROR,
  1642. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1643. SDL_GetPixelFormatName(format),
  1644. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1645. SDL_DestroySurface(surface);
  1646. }
  1647. return TEST_COMPLETED;
  1648. }
  1649. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1650. {
  1651. SDL_PixelFormat formats[] = {
  1652. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1653. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1654. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1655. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1656. };
  1657. SDL_Surface *surface;
  1658. SDL_PixelFormat format;
  1659. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1660. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1661. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1662. float expectedR = srcR * srcA;
  1663. float expectedG = srcG * srcA;
  1664. float expectedB = srcB * srcA;
  1665. float actualR, actualG, actualB;
  1666. float deltaR, deltaG, deltaB;
  1667. int i, ret;
  1668. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1669. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1670. format = formats[i];
  1671. surface = SDL_CreateSurface(1, 1, format);
  1672. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1673. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1674. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1675. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1676. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1677. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1678. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1679. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1680. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1681. deltaR = SDL_fabsf(actualR - expectedR);
  1682. deltaG = SDL_fabsf(actualG - expectedG);
  1683. deltaB = SDL_fabsf(actualB - expectedB);
  1684. SDLTest_AssertCheck(
  1685. deltaR <= MAXIMUM_ERROR &&
  1686. deltaG <= MAXIMUM_ERROR &&
  1687. deltaB <= MAXIMUM_ERROR,
  1688. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1689. SDL_GetPixelFormatName(format),
  1690. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1691. SDL_DestroySurface(surface);
  1692. }
  1693. return TEST_COMPLETED;
  1694. }
  1695. static int SDLCALL surface_testScale(void *arg)
  1696. {
  1697. SDL_PixelFormat formats[] = {
  1698. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1699. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1700. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1701. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1702. };
  1703. SDL_ScaleMode modes[] = {
  1704. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR, SDL_SCALEMODE_PIXELART
  1705. };
  1706. SDL_Surface *surface, *result;
  1707. SDL_PixelFormat format;
  1708. SDL_ScaleMode mode;
  1709. const float MAXIMUM_ERROR = 0.0001f;
  1710. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1711. float actualR, actualG, actualB, actualA;
  1712. float deltaR, deltaG, deltaB, deltaA;
  1713. int i, j, ret;
  1714. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1715. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1716. format = formats[i];
  1717. mode = modes[j];
  1718. surface = SDL_CreateSurface(1, 1, format);
  1719. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1720. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1721. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1722. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1723. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1724. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1725. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1726. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1727. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1728. deltaR = SDL_fabsf(actualR - srcR);
  1729. deltaG = SDL_fabsf(actualG - srcG);
  1730. deltaB = SDL_fabsf(actualB - srcB);
  1731. deltaA = SDL_fabsf(actualA - srcA);
  1732. SDLTest_AssertCheck(
  1733. deltaR <= MAXIMUM_ERROR &&
  1734. deltaG <= MAXIMUM_ERROR &&
  1735. deltaB <= MAXIMUM_ERROR &&
  1736. deltaA <= MAXIMUM_ERROR,
  1737. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1738. SDL_GetPixelFormatName(format),
  1739. mode == SDL_SCALEMODE_NEAREST ? "nearest" :
  1740. mode == SDL_SCALEMODE_LINEAR ? "linear" :
  1741. mode == SDL_SCALEMODE_PIXELART ? "pixelart" : "unknown",
  1742. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1743. SDL_DestroySurface(surface);
  1744. SDL_DestroySurface(result);
  1745. }
  1746. }
  1747. return TEST_COMPLETED;
  1748. }
  1749. #define GENERATE_SHIFTS
  1750. static Uint32 Calculate(int v, int bits, int vmax, int shift)
  1751. {
  1752. #if defined(GENERATE_FLOOR)
  1753. return (Uint32)SDL_floor(v * 255.0f / vmax) << shift;
  1754. #elif defined(GENERATE_ROUND)
  1755. return (Uint32)SDL_roundf(v * 255.0f / vmax) << shift;
  1756. #elif defined(GENERATE_SHIFTS)
  1757. switch (bits) {
  1758. case 1:
  1759. v = (v << 7) | (v << 6) | (v << 5) | (v << 4) | (v << 3) | (v << 2) | (v << 1) | v;
  1760. break;
  1761. case 2:
  1762. v = (v << 6) | (v << 4) | (v << 2) | v;
  1763. break;
  1764. case 3:
  1765. v = (v << 5) | (v << 2) | (v >> 1);
  1766. break;
  1767. case 4:
  1768. v = (v << 4) | v;
  1769. break;
  1770. case 5:
  1771. v = (v << 3) | (v >> 2);
  1772. break;
  1773. case 6:
  1774. v = (v << 2) | (v >> 4);
  1775. break;
  1776. case 7:
  1777. v = (v << 1) | (v >> 6);
  1778. break;
  1779. case 8:
  1780. break;
  1781. }
  1782. return (Uint32)v << shift;
  1783. #endif
  1784. }
  1785. static Uint32 Calculate565toARGB(int v, const SDL_PixelFormatDetails *fmt)
  1786. {
  1787. Uint8 r = (v & 0xF800) >> 11;
  1788. Uint8 g = (v & 0x07E0) >> 5;
  1789. Uint8 b = (v & 0x001F);
  1790. return fmt->Amask |
  1791. Calculate(r, 5, 31, fmt->Rshift) |
  1792. Calculate(g, 6, 63, fmt->Gshift) |
  1793. Calculate(b, 5, 31, fmt->Bshift);
  1794. }
  1795. static int SDLCALL surface_test16BitTo32Bit(void *arg)
  1796. {
  1797. static const SDL_PixelFormat formats[] = {
  1798. SDL_PIXELFORMAT_ARGB8888,
  1799. SDL_PIXELFORMAT_ABGR8888,
  1800. SDL_PIXELFORMAT_RGBA8888,
  1801. SDL_PIXELFORMAT_BGRA8888
  1802. };
  1803. static Uint16 pixels[1 << 16];
  1804. static Uint32 expected[1 << 16];
  1805. int i, p, ret;
  1806. SDL_Surface *surface16;
  1807. SDL_Surface *surface32;
  1808. SDL_Surface *expected32;
  1809. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1810. pixels[p] = p;
  1811. }
  1812. surface16 = SDL_CreateSurfaceFrom(SDL_arraysize(pixels), 1, SDL_PIXELFORMAT_RGB565, pixels, sizeof(pixels));
  1813. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1814. SDL_PixelFormat format = formats[i];
  1815. const SDL_PixelFormatDetails *fmt = SDL_GetPixelFormatDetails(format);
  1816. SDLTest_Log("Checking conversion from SDL_PIXELFORMAT_RGB565 to %s", SDL_GetPixelFormatName(format));
  1817. surface32 = SDL_ConvertSurface(surface16, format);
  1818. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1819. expected[p] = Calculate565toARGB(p, fmt);
  1820. }
  1821. expected32 = SDL_CreateSurfaceFrom(SDL_arraysize(expected), 1, format, expected, sizeof(expected));
  1822. ret = SDLTest_CompareSurfaces(surface32, expected32, 0);
  1823. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1824. SDL_DestroySurface(surface32);
  1825. SDL_DestroySurface(expected32);
  1826. }
  1827. SDL_DestroySurface(surface16);
  1828. return TEST_COMPLETED;
  1829. }
  1830. /* ================= Test References ================== */
  1831. /* Surface test cases */
  1832. static const SDLTest_TestCaseReference surfaceTestInvalidFormat = {
  1833. surface_testInvalidFormat, "surface_testInvalidFormat", "Tests creating surface with invalid format", TEST_ENABLED
  1834. };
  1835. static const SDLTest_TestCaseReference surfaceTestSaveLoad = {
  1836. surface_testSaveLoad, "surface_testSaveLoad", "Tests sprite saving and loading.", TEST_ENABLED
  1837. };
  1838. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1839. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1840. };
  1841. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1842. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1843. };
  1844. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1845. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1846. };
  1847. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1848. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1849. };
  1850. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1851. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1852. };
  1853. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1854. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1855. };
  1856. static const SDLTest_TestCaseReference surfaceTestNULLPixels = {
  1857. surface_testSurfaceNULLPixels, "surface_testSurfaceNULLPixels", "Tests surface operations with NULL pixels.", TEST_ENABLED
  1858. };
  1859. static const SDLTest_TestCaseReference surfaceTestRLEPixels = {
  1860. surface_testSurfaceRLEPixels, "surface_testSurfaceRLEPixels", "Tests surface operations with RLE surfaces.", TEST_ENABLED
  1861. };
  1862. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1863. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1864. };
  1865. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1866. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1867. };
  1868. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1869. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1870. };
  1871. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1872. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1873. };
  1874. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1875. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1876. };
  1877. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1878. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1879. };
  1880. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1881. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1882. };
  1883. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1884. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1885. };
  1886. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1887. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1888. };
  1889. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1890. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1891. };
  1892. static const SDLTest_TestCaseReference surfaceTestBlitBitmap = {
  1893. surface_testBlitBitmap, "surface_testBlitBitmap", "Tests blitting routines with bitmap surfaces.", TEST_ENABLED
  1894. };
  1895. static const SDLTest_TestCaseReference surfaceTestBlitInvalid = {
  1896. surface_testBlitInvalid, "surface_testBlitInvalid", "Tests blitting routines with invalid surfaces.", TEST_ENABLED
  1897. };
  1898. static const SDLTest_TestCaseReference surfaceTestBlitsWithBadCoordinates = {
  1899. surface_testBlitsWithBadCoordinates, "surface_testBlitsWithBadCoordinates", "Test blitting routines with bad coordinates.", TEST_ENABLED
  1900. };
  1901. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1902. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1903. };
  1904. static const SDLTest_TestCaseReference surfaceTestSetGetClipRect = {
  1905. surface_testSetGetSurfaceClipRect, "surface_testSetGetSurfaceClipRect", "Test SDL_(Set|Get)SurfaceClipRect.", TEST_ENABLED
  1906. };
  1907. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1908. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1909. };
  1910. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1911. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1912. };
  1913. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1914. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1915. };
  1916. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1917. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1918. };
  1919. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1920. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1921. };
  1922. static const SDLTest_TestCaseReference surfaceTestScale = {
  1923. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1924. };
  1925. static const SDLTest_TestCaseReference surfaceTest16BitTo32Bit = {
  1926. surface_test16BitTo32Bit, "surface_test16BitTo32Bit", "Test conversion from 16-bit to 32-bit pixels.", TEST_ENABLED
  1927. };
  1928. /* Sequence of Surface test cases */
  1929. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1930. &surfaceTestInvalidFormat,
  1931. &surfaceTestSaveLoad,
  1932. &surfaceTestBlitZeroSource,
  1933. &surfaceTestBlit,
  1934. &surfaceTestBlitTiled,
  1935. &surfaceTestBlit9Grid,
  1936. &surfaceTestBlitMultiple,
  1937. &surfaceTestLoadFailure,
  1938. &surfaceTestNULLPixels,
  1939. &surfaceTestRLEPixels,
  1940. &surfaceTestSurfaceConversion,
  1941. &surfaceTestCompleteSurfaceConversion,
  1942. &surfaceTestBlitColorMod,
  1943. &surfaceTestBlitAlphaMod,
  1944. &surfaceTestBlitBlendBlend,
  1945. &surfaceTestBlitBlendPremultiplied,
  1946. &surfaceTestBlitBlendAdd,
  1947. &surfaceTestBlitBlendAddPremultiplied,
  1948. &surfaceTestBlitBlendMod,
  1949. &surfaceTestBlitBlendMul,
  1950. &surfaceTestBlitBitmap,
  1951. &surfaceTestBlitInvalid,
  1952. &surfaceTestBlitsWithBadCoordinates,
  1953. &surfaceTestOverflow,
  1954. &surfaceTestSetGetClipRect,
  1955. &surfaceTestFlip,
  1956. &surfaceTestPalette,
  1957. &surfaceTestPalettization,
  1958. &surfaceTestClearSurface,
  1959. &surfaceTestPremultiplyAlpha,
  1960. &surfaceTestScale,
  1961. &surfaceTest16BitTo32Bit,
  1962. NULL
  1963. };
  1964. /* Surface test suite (global) */
  1965. SDLTest_TestSuiteReference surfaceTestSuite = {
  1966. "Surface",
  1967. surfaceSetUp,
  1968. surfaceTests,
  1969. surfaceTearDown
  1970. };