testautomation_surface.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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 *indexed_surface;
  296. SDL_Surface *rface;
  297. SDL_Palette *palette;
  298. SDL_Color colors[] = {
  299. { 255, 0, 0, SDL_ALPHA_OPAQUE }, /* Red */
  300. { 0, 255, 0, SDL_ALPHA_OPAQUE } /* Green */
  301. };
  302. SDL_IOStream *stream;
  303. Uint8 r, g, b, a;
  304. /* Create sample surface */
  305. face = SDLTest_ImageFace();
  306. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  307. if (face == NULL) {
  308. return TEST_ABORTED;
  309. }
  310. indexed_surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_INDEX8);
  311. SDLTest_AssertCheck(indexed_surface != NULL, "SDL_CreateSurface(SDL_PIXELFORMAT_INDEX8)");
  312. /* Delete test file; ignore errors */
  313. SDL_RemovePath(sampleFilename);
  314. /* Saving an indexed surface without palette as BMP fails */
  315. ret = SDL_SaveBMP(indexed_surface, sampleFilename);
  316. SDLTest_AssertPass("Call to SDL_SaveBMP() using an indexed surface without palette");
  317. SDLTest_AssertCheck(ret == false, "Verify result of SDL_SaveBMP(indexed_surface without palette), expected: false, got: %i", ret);
  318. SDLTest_AssertCheck(!SDL_GetPathInfo(sampleFilename, NULL), "No file is created after trying to save a indexed surface without palette");
  319. /* Delete test file; ignore errors */
  320. SDL_RemovePath(sampleFilename);
  321. /* Save a BMP surface */
  322. ret = SDL_SaveBMP(face, sampleFilename);
  323. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  324. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  325. AssertFileExist(sampleFilename);
  326. /* Load a BMP surface */
  327. rface = SDL_LoadBMP(sampleFilename);
  328. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  329. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  330. if (rface != NULL) {
  331. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  332. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  333. SDL_DestroySurface(rface);
  334. rface = NULL;
  335. }
  336. /* Delete test file; ignore errors */
  337. SDL_RemovePath(sampleFilename);
  338. /* Saving an indexed surface as PNG fails */
  339. ret = SDL_SavePNG(indexed_surface, sampleFilename);
  340. SDLTest_AssertPass("Call to SDL_SavePNG() using an indexed surface without palette");
  341. SDLTest_AssertCheck(ret == false, "Verify result of SDL_SavePNG(indexed surface without palette), expected: false, got: %i", ret);
  342. SDLTest_AssertCheck(ret == false, "Verify result of SDL_SavePNG(indexed surface without palette), expected: false, got: %i", ret);
  343. /* Delete test file; ignore errors */
  344. SDL_RemovePath(sampleFilename);
  345. /* Save a PNG surface */
  346. ret = SDL_SavePNG(face, sampleFilename);
  347. SDLTest_AssertPass("Call to SDL_SavePNG()");
  348. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  349. AssertFileExist(sampleFilename);
  350. /* Load a PNG surface */
  351. rface = SDL_LoadPNG(sampleFilename);
  352. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  353. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  354. if (rface != NULL) {
  355. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  356. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  357. SDL_DestroySurface(rface);
  358. rface = NULL;
  359. }
  360. /* Delete test file; ignore errors */
  361. SDL_RemovePath(sampleFilename);
  362. /* Clean up */
  363. SDL_DestroySurface(face);
  364. face = NULL;
  365. /* Create an 8-bit image */
  366. face = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  367. SDLTest_AssertCheck(face != NULL, "Verify 8-bit surface is not NULL");
  368. if (face == NULL) {
  369. return TEST_ABORTED;
  370. }
  371. palette = SDL_CreatePalette(2);
  372. SDLTest_AssertCheck(palette != NULL, "Verify palette is not NULL");
  373. if (palette == NULL) {
  374. return TEST_ABORTED;
  375. }
  376. SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  377. SDL_SetSurfacePalette(face, palette);
  378. SDL_DestroyPalette(palette);
  379. /* Set a green pixel */
  380. *(Uint8 *)face->pixels = 1;
  381. /* Save and reload as a BMP */
  382. stream = SDL_IOFromDynamicMem();
  383. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  384. if (stream == NULL) {
  385. return TEST_ABORTED;
  386. }
  387. ret = SDL_SaveBMP_IO(indexed_surface, stream, false);
  388. SDLTest_AssertCheck(ret == false, "Verify result from SDL_SaveBMP (indexed surface without palette), expected: false, got: %i", ret);
  389. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  390. ret = SDL_SaveBMP_IO(face, stream, false);
  391. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  392. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  393. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  394. rface = SDL_LoadBMP_IO(stream, false);
  395. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  396. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  397. if (rface != NULL) {
  398. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  399. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  400. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  401. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  402. SDLTest_AssertCheck(r == colors[1].r &&
  403. g == colors[1].g &&
  404. b == colors[1].b &&
  405. a == colors[1].a,
  406. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  407. r, g, b, a,
  408. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  409. SDL_DestroySurface(rface);
  410. rface = NULL;
  411. }
  412. SDL_CloseIO(stream);
  413. stream = NULL;
  414. /* Save and reload as a PNG */
  415. stream = SDL_IOFromDynamicMem();
  416. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  417. if (stream == NULL) {
  418. return TEST_ABORTED;
  419. }
  420. ret = SDL_SavePNG_IO(indexed_surface, stream, false);
  421. SDLTest_AssertCheck(ret == false, "Verify result from SDL_SavePNG_IO (indexed surface without palette), expected: false, got: %i", ret);
  422. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  423. ret = SDL_SavePNG_IO(face, stream, false);
  424. SDLTest_AssertPass("Call to SDL_SavePNG()");
  425. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  426. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  427. rface = SDL_LoadPNG_IO(stream, false);
  428. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  429. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  430. if (rface != NULL) {
  431. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  432. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  433. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  434. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  435. SDLTest_AssertCheck(r == colors[1].r &&
  436. g == colors[1].g &&
  437. b == colors[1].b &&
  438. a == colors[1].a,
  439. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  440. r, g, b, a,
  441. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  442. SDL_DestroySurface(rface);
  443. rface = NULL;
  444. }
  445. SDL_CloseIO(stream);
  446. stream = NULL;
  447. SDL_DestroySurface(indexed_surface);
  448. SDL_DestroySurface(face);
  449. return TEST_COMPLETED;
  450. }
  451. /**
  452. * Tests tiled blitting.
  453. */
  454. static int SDLCALL surface_testBlitTiled(void *arg)
  455. {
  456. SDL_Surface *face = NULL;
  457. SDL_Surface *testSurface2x = NULL;
  458. SDL_Surface *referenceSurface2x = NULL;
  459. int ret = 0;
  460. /* Create sample surface */
  461. face = SDLTest_ImageFace();
  462. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  463. if (face == NULL) {
  464. return TEST_ABORTED;
  465. }
  466. /* Tiled blit - 1.0 scale */
  467. {
  468. ret = SDL_BlitSurfaceTiled(face, NULL, testSurface, NULL);
  469. SDLTest_AssertCheck(ret == true, "Verify result from SDL_BlitSurfaceTiled expected: true, got: %i", ret);
  470. /* See if it's the same */
  471. SDL_DestroySurface(referenceSurface);
  472. referenceSurface = SDLTest_ImageBlitTiled();
  473. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  474. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  475. }
  476. /* Tiled blit - 2.0 scale */
  477. {
  478. testSurface2x = SDL_CreateSurface(testSurface->w * 2, testSurface->h * 2, testSurface->format);
  479. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface2x is not NULL");
  480. ret = SDL_FillSurfaceRect(testSurface2x, NULL, SDL_MapSurfaceRGBA(testSurface2x, 0, 0, 0, 255));
  481. SDLTest_AssertCheck(ret == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  482. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  483. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceTiledWithScale, expected: true, got: %i", ret);
  484. /* See if it's the same */
  485. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  486. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  487. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: true, got: %i", ret);
  488. ret = SDLTest_CompareSurfaces(testSurface2x, referenceSurface2x, 0);
  489. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  490. }
  491. /* Tiled blit - very small scale */
  492. {
  493. float tiny_scale = 0.01f;
  494. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, tiny_scale, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  495. SDLTest_AssertCheck(ret == true, "Expected SDL_BlitSurfaceTiledWithScale to succeed with very small scale: %f, got: %i", tiny_scale, ret);
  496. }
  497. /* Clean up. */
  498. SDL_DestroySurface(face);
  499. SDL_DestroySurface(testSurface2x);
  500. SDL_DestroySurface(referenceSurface2x);
  501. return TEST_COMPLETED;
  502. }
  503. static const Uint8 COLOR_SEPARATION = 85;
  504. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  505. {
  506. SDL_Rect rect;
  507. // Upper left
  508. rect.x = 0;
  509. rect.y = 0;
  510. rect.w = left_width;
  511. rect.h = top_height;
  512. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  513. // Top
  514. rect.x = left_width;
  515. rect.y = 0;
  516. rect.w = surface->w - left_width - right_width;
  517. rect.h = top_height;
  518. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  519. // Upper right
  520. rect.x = surface->w - right_width;
  521. rect.y = 0;
  522. rect.w = right_width;
  523. rect.h = top_height;
  524. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  525. // Left
  526. rect.x = 0;
  527. rect.y = top_height;
  528. rect.w = left_width;
  529. rect.h = surface->h - top_height - bottom_height;
  530. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  531. // Center
  532. rect.x = left_width;
  533. rect.y = top_height;
  534. rect.w = surface->w - right_width - left_width;
  535. rect.h = surface->h - top_height - bottom_height;
  536. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  537. // Right
  538. rect.x = surface->w - right_width;
  539. rect.y = top_height;
  540. rect.w = right_width;
  541. rect.h = surface->h - top_height - bottom_height;
  542. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  543. // Lower left
  544. rect.x = 0;
  545. rect.y = surface->h - bottom_height;
  546. rect.w = left_width;
  547. rect.h = bottom_height;
  548. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  549. // Bottom
  550. rect.x = left_width;
  551. rect.y = surface->h - bottom_height;
  552. rect.w = surface->w - left_width - right_width;
  553. rect.h = bottom_height;
  554. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  555. // Lower right
  556. rect.x = surface->w - right_width;
  557. rect.y = surface->h - bottom_height;
  558. rect.w = right_width;
  559. rect.h = bottom_height;
  560. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  561. }
  562. /**
  563. * Tests 9-grid blitting.
  564. */
  565. static int SDLCALL surface_testBlit9Grid(void *arg)
  566. {
  567. SDL_Surface *source = NULL;
  568. int x, y;
  569. int ret = 0;
  570. /* Create source surface */
  571. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  572. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  573. for (y = 0; y < 3; ++y) {
  574. for (x = 0; x < 3; ++x) {
  575. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  576. }
  577. }
  578. /* 9-grid blit - 1.0 scale */
  579. {
  580. /* Create reference surface */
  581. SDL_DestroySurface(referenceSurface);
  582. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  583. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  584. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  585. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  586. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  587. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  588. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  589. }
  590. /* 9-grid blit - 2.0 scale */
  591. {
  592. /* Create reference surface */
  593. SDL_DestroySurface(referenceSurface);
  594. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  595. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  596. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  597. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  598. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  599. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  600. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  601. }
  602. /* Clean up. */
  603. SDL_DestroySurface(source);
  604. /* Create complex source surface */
  605. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  606. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  607. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  608. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  609. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  610. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  611. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  612. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  613. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  614. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  615. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  616. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  617. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  618. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  619. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  620. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  621. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  622. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  623. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  624. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  625. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  626. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  627. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  628. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  629. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  630. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  631. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  632. /* complex 9-grid blit - 1.0 scale */
  633. {
  634. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  635. /* Create reference surface */
  636. SDL_DestroySurface(referenceSurface);
  637. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  638. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  639. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  640. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  641. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  642. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  643. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  644. }
  645. /* complex 9-grid blit - 2.0 scale */
  646. {
  647. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  648. /* Create reference surface */
  649. SDL_DestroySurface(referenceSurface);
  650. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  651. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  652. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  653. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  654. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  655. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  656. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  657. }
  658. /* Clean up. */
  659. SDL_DestroySurface(source);
  660. return TEST_COMPLETED;
  661. }
  662. /**
  663. * Tests blitting between multiple surfaces of the same format
  664. */
  665. static int SDLCALL surface_testBlitMultiple(void *arg)
  666. {
  667. SDL_Surface *source, *surface;
  668. SDL_Palette *palette;
  669. Uint8 *pixels;
  670. palette = SDL_CreatePalette(2);
  671. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  672. palette->colors[0].r = 0;
  673. palette->colors[0].g = 0;
  674. palette->colors[0].b = 0;
  675. palette->colors[1].r = 0xFF;
  676. palette->colors[1].g = 0;
  677. palette->colors[1].b = 0;
  678. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  679. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  680. SDL_SetSurfacePalette(source, palette);
  681. *(Uint8 *)source->pixels = 1;
  682. /* Set up a blit to a surface using the palette */
  683. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  684. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  685. SDL_SetSurfacePalette(surface, palette);
  686. pixels = (Uint8 *)surface->pixels;
  687. *pixels = 0;
  688. SDL_BlitSurface(source, NULL, surface, NULL);
  689. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  690. /* Set up a blit to another surface using the same palette */
  691. SDL_DestroySurface(surface);
  692. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  693. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  694. SDL_SetSurfacePalette(surface, palette);
  695. pixels = (Uint8 *)surface->pixels;
  696. *pixels = 0;
  697. SDL_BlitSurface(source, NULL, surface, NULL);
  698. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  699. /* Set up a blit to new surface with a different format */
  700. SDL_DestroySurface(surface);
  701. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  702. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  703. pixels = (Uint8 *)surface->pixels;
  704. SDL_BlitSurface(source, NULL, surface, NULL);
  705. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  706. /* Set up a blit to another surface with the same format */
  707. SDL_DestroySurface(surface);
  708. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  709. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  710. pixels = (Uint8 *)surface->pixels;
  711. SDL_BlitSurface(source, NULL, surface, NULL);
  712. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  713. SDL_DestroyPalette(palette);
  714. SDL_DestroySurface(source);
  715. SDL_DestroySurface(surface);
  716. return TEST_COMPLETED;
  717. }
  718. /**
  719. * Tests operations on surfaces with NULL pixels
  720. */
  721. static int SDLCALL surface_testSurfaceNULLPixels(void *arg)
  722. {
  723. SDL_Surface *a, *b, *face;
  724. bool result;
  725. face = SDLTest_ImageFace();
  726. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  727. if (face == NULL) {
  728. return TEST_ABORTED;
  729. }
  730. /* Test blitting with NULL pixels */
  731. a = SDL_CreateSurfaceFrom(face->w, face->h, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  732. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  733. result = SDL_BlitSurface(a, NULL, face, NULL);
  734. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with src having NULL pixels is false");
  735. result = SDL_BlitSurface(face, NULL, a, NULL);
  736. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with dst having NULL pixels is false");
  737. b = SDL_CreateSurfaceFrom(face->w * 2, face->h * 2, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  738. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  739. result = SDL_BlitSurfaceScaled(b, NULL, face, NULL, SDL_SCALEMODE_NEAREST);
  740. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with src having NULL pixels is false");
  741. result = SDL_BlitSurfaceScaled(face, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  742. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with dst having NULL pixels is false");
  743. SDL_DestroySurface(b);
  744. b = NULL;
  745. /* Test conversion with NULL pixels */
  746. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  747. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with NULL pixels is not NULL");
  748. SDL_DestroySurface(b);
  749. b = NULL;
  750. /* Test duplication with NULL pixels */
  751. b = SDL_DuplicateSurface(a);
  752. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_DuplicateSurface() with NULL pixels is not NULL");
  753. SDL_DestroySurface(b);
  754. b = NULL;
  755. /* Test scaling with NULL pixels */
  756. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  757. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() with NULL pixels is not NULL");
  758. SDLTest_AssertCheck(b->pixels == NULL, "Verify pixels from SDL_ScaleSurface() is NULL");
  759. SDL_DestroySurface(b);
  760. b = NULL;
  761. /* Test filling surface with NULL pixels */
  762. result = SDL_FillSurfaceRect(a, NULL, 0);
  763. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having NULL pixels is true");
  764. /* Clean up. */
  765. SDL_DestroySurface(face);
  766. SDL_DestroySurface(a);
  767. SDL_DestroySurface(b);
  768. return TEST_COMPLETED;
  769. }
  770. /**
  771. * Tests operations on surfaces with RLE pixels
  772. */
  773. static int SDLCALL surface_testSurfaceRLEPixels(void *arg)
  774. {
  775. SDL_Surface *face, *a, *b, *tmp;
  776. int ret;
  777. bool result;
  778. face = SDLTest_ImageFace();
  779. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  780. if (face == NULL) {
  781. return TEST_ABORTED;
  782. }
  783. /* RLE encoding only works for 32-bit surfaces with alpha in the high bits */
  784. if (face->format != SDL_PIXELFORMAT_ARGB8888) {
  785. tmp = SDL_ConvertSurface(face, SDL_PIXELFORMAT_ARGB8888);
  786. SDLTest_AssertCheck(tmp != NULL, "Verify tmp surface is not NULL");
  787. if (tmp == NULL) {
  788. return TEST_ABORTED;
  789. }
  790. SDL_DestroySurface(face);
  791. face = tmp;
  792. }
  793. /* Create a temporary surface to trigger RLE encoding during blit */
  794. tmp = SDL_DuplicateSurface(face);
  795. SDLTest_AssertCheck(tmp != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  796. result = SDL_SetSurfaceRLE(face, true);
  797. SDLTest_AssertCheck(result, "Verify result from SDL_SetSurfaceRLE() is true");
  798. /* Test duplication with RLE pixels */
  799. a = SDL_DuplicateSurface(face);
  800. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  801. SDLTest_AssertCheck(SDL_SurfaceHasRLE(a), "Verify result from SDL_DuplicateSurface() with RLE pixels has RLE set");
  802. ret = SDLTest_CompareSurfaces(a, face, 0);
  803. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  804. /* Verify that blitting from an RLE surface does RLE encode it */
  805. SDLTest_AssertCheck(!SDL_MUSTLOCK(a), "Verify initial RLE surface does not need to be locked");
  806. SDLTest_AssertCheck(a->pixels != NULL, "Verify initial RLE surface has pixels available");
  807. result = SDL_BlitSurface(a, NULL, tmp, NULL);
  808. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurface() with RLE surface is true");
  809. SDLTest_AssertCheck(SDL_MUSTLOCK(a), "Verify RLE surface after blit needs to be locked");
  810. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after blit does not have pixels available");
  811. ret = SDLTest_CompareSurfaces(tmp, face, 0);
  812. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  813. /* Test scaling with RLE pixels */
  814. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  815. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() is not NULL");
  816. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ScaleSurface() with RLE pixels has RLE set");
  817. /* Test scaling blitting with RLE pixels */
  818. result = SDL_BlitSurfaceScaled(a, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  819. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurfaceScaled() with src having RLE pixels is true");
  820. SDL_BlitSurface(a, NULL, tmp, NULL);
  821. SDL_DestroySurface(b);
  822. b = NULL;
  823. /* Test conversion with RLE pixels */
  824. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  825. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels is not NULL");
  826. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels has RLE set");
  827. ret = SDLTest_CompareSurfacesIgnoreTransparentPixels(b, face, 0);
  828. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  829. SDL_BlitSurface(a, NULL, tmp, NULL);
  830. SDL_DestroySurface(b);
  831. b = NULL;
  832. #if 0 /* This will currently fail, you must lock the surface first */
  833. /* Test filling surface with RLE pixels */
  834. result = SDL_FillSurfaceRect(a, NULL, 0);
  835. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having RLE pixels is true");
  836. #endif
  837. /* Make sure the RLE surface still needs to be locked after surface operations */
  838. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after operations does not have pixels available");
  839. /* Clean up. */
  840. SDL_DestroySurface(face);
  841. SDL_DestroySurface(a);
  842. SDL_DestroySurface(b);
  843. SDL_DestroySurface(tmp);
  844. return TEST_COMPLETED;
  845. }
  846. /**
  847. * Tests surface conversion.
  848. */
  849. static int SDLCALL surface_testSurfaceConversion(void *arg)
  850. {
  851. SDL_Surface *rface = NULL, *face = NULL;
  852. int ret = 0;
  853. /* Create sample surface */
  854. face = SDLTest_ImageFace();
  855. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  856. if (face == NULL) {
  857. return TEST_ABORTED;
  858. }
  859. /* Set transparent pixel as the pixel at (0,0) */
  860. if (SDL_GetSurfacePalette(face)) {
  861. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  862. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  863. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  864. }
  865. /* Convert to 32 bit to compare. */
  866. rface = SDL_ConvertSurface(face, testSurface->format);
  867. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  868. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  869. /* Compare surface. */
  870. ret = SDLTest_CompareSurfaces(rface, face, 0);
  871. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  872. /* Clean up. */
  873. SDL_DestroySurface(face);
  874. SDL_DestroySurface(rface);
  875. return TEST_COMPLETED;
  876. }
  877. /**
  878. * Tests surface conversion across all pixel formats.
  879. */
  880. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  881. {
  882. Uint32 pixel_formats[] = {
  883. SDL_PIXELFORMAT_INDEX8,
  884. SDL_PIXELFORMAT_RGB332,
  885. SDL_PIXELFORMAT_XRGB4444,
  886. SDL_PIXELFORMAT_XBGR4444,
  887. SDL_PIXELFORMAT_XRGB1555,
  888. SDL_PIXELFORMAT_XBGR1555,
  889. SDL_PIXELFORMAT_ARGB4444,
  890. SDL_PIXELFORMAT_RGBA4444,
  891. SDL_PIXELFORMAT_ABGR4444,
  892. SDL_PIXELFORMAT_BGRA4444,
  893. SDL_PIXELFORMAT_ARGB1555,
  894. SDL_PIXELFORMAT_RGBA5551,
  895. SDL_PIXELFORMAT_ABGR1555,
  896. SDL_PIXELFORMAT_BGRA5551,
  897. SDL_PIXELFORMAT_RGB565,
  898. SDL_PIXELFORMAT_BGR565,
  899. SDL_PIXELFORMAT_RGB24,
  900. SDL_PIXELFORMAT_BGR24,
  901. SDL_PIXELFORMAT_XRGB8888,
  902. SDL_PIXELFORMAT_RGBX8888,
  903. SDL_PIXELFORMAT_XBGR8888,
  904. SDL_PIXELFORMAT_BGRX8888,
  905. SDL_PIXELFORMAT_ARGB8888,
  906. SDL_PIXELFORMAT_RGBA8888,
  907. SDL_PIXELFORMAT_ABGR8888,
  908. SDL_PIXELFORMAT_BGRA8888,
  909. #if 0 /* We aren't testing HDR10 colorspace conversion */
  910. SDL_PIXELFORMAT_XRGB2101010,
  911. SDL_PIXELFORMAT_XBGR2101010,
  912. SDL_PIXELFORMAT_ARGB2101010,
  913. SDL_PIXELFORMAT_ABGR2101010,
  914. #endif
  915. };
  916. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  917. const SDL_PixelFormatDetails *fmt1, *fmt2;
  918. int i, j, ret = 0;
  919. /* Create sample surface */
  920. face = SDLTest_ImageFace();
  921. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  922. if (face == NULL) {
  923. return TEST_ABORTED;
  924. }
  925. /* Set transparent pixel as the pixel at (0,0) */
  926. if (SDL_GetSurfacePalette(face)) {
  927. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  928. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  929. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  930. }
  931. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  932. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  933. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  934. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  935. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  936. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  937. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  938. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  939. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  940. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  941. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  942. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  943. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  944. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  945. if (fmt1 && fmt2 &&
  946. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  947. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  948. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  949. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  950. final = SDL_ConvertSurface(cvt2, face->format);
  951. SDL_assert(final != NULL);
  952. /* Compare surface. */
  953. ret = SDLTest_CompareSurfaces(face, final, 0);
  954. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  955. SDL_DestroySurface(final);
  956. }
  957. SDL_DestroySurface(cvt1);
  958. SDL_DestroySurface(cvt2);
  959. }
  960. }
  961. /* Clean up. */
  962. SDL_DestroySurface(face);
  963. return TEST_COMPLETED;
  964. }
  965. /**
  966. * Tests sprite loading. A failure case.
  967. */
  968. static int SDLCALL surface_testLoadFailure(void *arg)
  969. {
  970. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  971. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  972. return TEST_COMPLETED;
  973. }
  974. /**
  975. * Tests blitting from a zero sized source rectangle
  976. */
  977. static int SDLCALL surface_testBlitZeroSource(void *arg)
  978. {
  979. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  980. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  981. SDL_Rect srcrect = { 0, 0, 0, 0 };
  982. int ret;
  983. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  984. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  985. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  986. ret = SDLTest_CompareSurfaces(dst, src, 0);
  987. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  988. SDL_DestroySurface(src);
  989. SDL_DestroySurface(dst);
  990. return TEST_COMPLETED;
  991. }
  992. /**
  993. * Tests some blitting routines.
  994. */
  995. static int SDLCALL surface_testBlit(void *arg)
  996. {
  997. /* Basic blitting */
  998. testBlitBlendMode(SDL_BLENDMODE_NONE);
  999. return TEST_COMPLETED;
  1000. }
  1001. /**
  1002. * Tests some blitting routines with color mod
  1003. */
  1004. static int SDLCALL surface_testBlitColorMod(void *arg)
  1005. {
  1006. /* Basic blitting with color mod */
  1007. testBlitBlendMode(-1);
  1008. return TEST_COMPLETED;
  1009. }
  1010. /**
  1011. * Tests some blitting routines with alpha mod
  1012. */
  1013. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  1014. {
  1015. /* Basic blitting with alpha mod */
  1016. testBlitBlendMode(-2);
  1017. return TEST_COMPLETED;
  1018. }
  1019. /**
  1020. * Tests some more blitting routines.
  1021. */
  1022. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  1023. {
  1024. /* Blend blitting */
  1025. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  1026. return TEST_COMPLETED;
  1027. }
  1028. /**
  1029. * @brief Tests some more blitting routines.
  1030. */
  1031. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  1032. {
  1033. /* Blend premultiplied blitting */
  1034. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  1035. return TEST_COMPLETED;
  1036. }
  1037. /**
  1038. * Tests some more blitting routines.
  1039. */
  1040. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  1041. {
  1042. /* Add blitting */
  1043. testBlitBlendMode(SDL_BLENDMODE_ADD);
  1044. return TEST_COMPLETED;
  1045. }
  1046. /**
  1047. * Tests some more blitting routines.
  1048. */
  1049. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  1050. {
  1051. /* Add premultiplied blitting */
  1052. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  1053. return TEST_COMPLETED;
  1054. }
  1055. /**
  1056. * Tests some more blitting routines.
  1057. */
  1058. static int SDLCALL surface_testBlitBlendMod(void *arg)
  1059. {
  1060. /* Mod blitting */
  1061. testBlitBlendMode(SDL_BLENDMODE_MOD);
  1062. return TEST_COMPLETED;
  1063. }
  1064. /**
  1065. * Tests some more blitting routines.
  1066. */
  1067. static int SDLCALL surface_testBlitBlendMul(void *arg)
  1068. {
  1069. /* Mod blitting */
  1070. testBlitBlendMode(SDL_BLENDMODE_MUL);
  1071. return TEST_COMPLETED;
  1072. }
  1073. /**
  1074. * Tests blitting bitmaps
  1075. */
  1076. static int SDLCALL surface_testBlitBitmap(void *arg)
  1077. {
  1078. const SDL_PixelFormat formats[] = {
  1079. SDL_PIXELFORMAT_INDEX1LSB,
  1080. SDL_PIXELFORMAT_INDEX1MSB,
  1081. SDL_PIXELFORMAT_INDEX2LSB,
  1082. SDL_PIXELFORMAT_INDEX2MSB,
  1083. SDL_PIXELFORMAT_INDEX4LSB,
  1084. SDL_PIXELFORMAT_INDEX4MSB
  1085. };
  1086. Uint8 pixel;
  1087. int i, j;
  1088. bool result;
  1089. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
  1090. SDL_Color colors[] = {
  1091. { 0x00, 0x00, 0x00, 0xFF },
  1092. { 0xFF, 0xFF, 0xFF, 0xFF }
  1093. };
  1094. SDL_Palette *palette;
  1095. Uint32 value, expected = 0xFFFFFFFF;
  1096. palette = SDL_CreatePalette(SDL_arraysize(colors));
  1097. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette() != NULL, result = %p", palette);
  1098. result = SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  1099. SDLTest_AssertCheck(result, "SDL_SetPaletteColors, result = %s", result ? "true" : "false");
  1100. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1101. SDL_PixelFormat format = formats[i];
  1102. int bpp = SDL_BITSPERPIXEL(format);
  1103. int width = (8 / bpp);
  1104. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1105. switch (bpp) {
  1106. case 1:
  1107. pixel = 0x80;
  1108. break;
  1109. case 2:
  1110. pixel = 0x40;
  1111. break;
  1112. case 4:
  1113. pixel = 0x10;
  1114. break;
  1115. default:
  1116. SDL_assert(!"Unexpected bpp");
  1117. break;
  1118. }
  1119. } else {
  1120. pixel = 0x01;
  1121. }
  1122. for (j = 0; j < width; ++j) {
  1123. SDL_Rect rect = { j, 0, 1, 1 };
  1124. SDL_Surface *src = SDL_CreateSurfaceFrom(width, 1, format, &pixel, 1);
  1125. SDL_SetSurfacePalette(src, palette);
  1126. *(Uint32 *)dst->pixels = 0;
  1127. result = SDL_BlitSurface(src, &rect, dst, NULL);
  1128. SDLTest_AssertCheck(result, "SDL_BlitSurface(%s pixel %d), result = %s", SDL_GetPixelFormatName(format), j, result ? "true" : "false");
  1129. value = *(Uint32 *)dst->pixels;
  1130. SDLTest_AssertCheck(value == expected, "Expected value == 0x%" SDL_PRIx32 ", actually = 0x%" SDL_PRIx32, expected, value);
  1131. SDL_DestroySurface(src);
  1132. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1133. pixel >>= bpp;
  1134. } else {
  1135. pixel <<= bpp;
  1136. }
  1137. }
  1138. }
  1139. SDL_DestroyPalette(palette);
  1140. SDL_DestroySurface(dst);
  1141. return TEST_COMPLETED;
  1142. }
  1143. /**
  1144. * Tests blitting invalid surfaces.
  1145. */
  1146. static int SDLCALL surface_testBlitInvalid(void *arg)
  1147. {
  1148. SDL_Surface *valid, *invalid;
  1149. bool result;
  1150. valid = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1151. SDLTest_AssertCheck(valid != NULL, "Check surface creation");
  1152. invalid = SDL_CreateSurface(0, 0, SDL_PIXELFORMAT_RGBA8888);
  1153. SDLTest_AssertCheck(invalid != NULL, "Check surface creation");
  1154. SDLTest_AssertCheck(invalid->pixels == NULL, "Check surface pixels are NULL");
  1155. result = SDL_BlitSurface(invalid, NULL, valid, NULL);
  1156. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(invalid, NULL, valid, NULL), result = %s", result ? "true" : "false");
  1157. result = SDL_BlitSurface(valid, NULL, invalid, NULL);
  1158. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(valid, NULL, invalid, NULL), result = %s", result ? "true" : "false");
  1159. result = SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST);
  1160. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1161. result = SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST);
  1162. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1163. SDL_DestroySurface(valid);
  1164. SDL_DestroySurface(invalid);
  1165. return TEST_COMPLETED;
  1166. }
  1167. static int SDLCALL surface_testBlitsWithBadCoordinates(void *arg)
  1168. {
  1169. const SDL_Rect rect[8] = {
  1170. { SDL_MAX_SINT32, 0, 2, 2 },
  1171. { 0, SDL_MAX_SINT32, 2, 2 },
  1172. { 0, 0, SDL_MAX_SINT32, 2 },
  1173. { 0, 0, 2, SDL_MAX_SINT32 },
  1174. { SDL_MIN_SINT32, 0, 2, 2 },
  1175. { 0, SDL_MIN_SINT32, 2, 2 },
  1176. { 0, 0, SDL_MIN_SINT32, 2 },
  1177. { 0, 0, 2, SDL_MIN_SINT32 }
  1178. };
  1179. SDL_Surface *s;
  1180. bool result;
  1181. int i;
  1182. s = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1183. SDLTest_AssertCheck(s != NULL, "Check surface creation");
  1184. for (i = 0; i < 8; i++) {
  1185. result = SDL_BlitSurface(s, NULL, s, &rect[i]);
  1186. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, valid, &rect), result = %s", result ? "true" : "false");
  1187. result = SDL_BlitSurface(s, &rect[i], s, NULL);
  1188. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, &rect, valid, NULL), result = %s", result ? "true" : "false");
  1189. result = SDL_BlitSurfaceScaled(s, NULL, s, &rect[i], SDL_SCALEMODE_NEAREST);
  1190. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, NULL, valid, &rect, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1191. result = SDL_BlitSurfaceScaled(s, &rect[i], s, NULL, SDL_SCALEMODE_NEAREST);
  1192. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, &rect, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1193. }
  1194. SDL_DestroySurface(s);
  1195. return TEST_COMPLETED;
  1196. }
  1197. static int SDLCALL surface_testOverflow(void *arg)
  1198. {
  1199. char buf[1024];
  1200. const char *expectedError;
  1201. SDL_Surface *surface;
  1202. SDL_memset(buf, '\0', sizeof(buf));
  1203. expectedError = "Parameter 'width' is invalid";
  1204. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  1205. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1206. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1207. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1208. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1209. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1210. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1211. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1212. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1213. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1214. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1215. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1216. expectedError = "Parameter 'height' is invalid";
  1217. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  1218. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1219. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1220. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1221. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1222. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1223. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1224. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1225. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1226. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1227. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1228. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1229. expectedError = "Parameter 'pitch' is invalid";
  1230. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  1231. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1232. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1233. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1234. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  1235. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1236. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1237. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1238. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  1239. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  1240. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1241. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1242. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  1243. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  1244. surface != NULL ? "(success)" : SDL_GetError());
  1245. SDL_DestroySurface(surface);
  1246. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  1247. * the width, but it must be enough to hold the appropriate number of
  1248. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  1249. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1250. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1251. surface != NULL ? "(success)" : SDL_GetError());
  1252. SDL_DestroySurface(surface);
  1253. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1254. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1255. surface != NULL ? "(success)" : SDL_GetError());
  1256. SDL_DestroySurface(surface);
  1257. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1258. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1259. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1260. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1261. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1262. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1263. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1264. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1265. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  1266. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1267. surface != NULL ? "(success)" : SDL_GetError());
  1268. SDL_DestroySurface(surface);
  1269. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  1270. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1271. surface != NULL ? "(success)" : SDL_GetError());
  1272. SDL_DestroySurface(surface);
  1273. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  1274. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1275. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1276. surface != NULL ? "(success)" : SDL_GetError());
  1277. SDL_DestroySurface(surface);
  1278. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1279. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1280. surface != NULL ? "(success)" : SDL_GetError());
  1281. SDL_DestroySurface(surface);
  1282. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1283. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  1284. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1285. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1286. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1287. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1288. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1289. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1290. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  1291. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1292. surface != NULL ? "(success)" : SDL_GetError());
  1293. SDL_DestroySurface(surface);
  1294. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  1295. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1296. surface != NULL ? "(success)" : SDL_GetError());
  1297. SDL_DestroySurface(surface);
  1298. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  1299. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1300. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1301. surface != NULL ? "(success)" : SDL_GetError());
  1302. SDL_DestroySurface(surface);
  1303. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1304. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1305. surface != NULL ? "(success)" : SDL_GetError());
  1306. SDL_DestroySurface(surface);
  1307. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1308. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1309. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1310. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1311. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1312. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1313. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1314. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1315. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  1316. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1317. surface != NULL ? "(success)" : SDL_GetError());
  1318. SDL_DestroySurface(surface);
  1319. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  1320. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1321. surface != NULL ? "(success)" : SDL_GetError());
  1322. SDL_DestroySurface(surface);
  1323. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  1324. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1325. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1326. surface != NULL ? "(success)" : SDL_GetError());
  1327. SDL_DestroySurface(surface);
  1328. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1329. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1330. surface != NULL ? "(success)" : SDL_GetError());
  1331. SDL_DestroySurface(surface);
  1332. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1333. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1334. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1335. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1336. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1337. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1338. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1339. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1340. /* Everything else requires more than 1 byte per pixel, and rounds up
  1341. * each pixel to an integer number of bytes (e.g. RGB555 is really
  1342. * XRGB1555, with 1 bit per pixel wasted). */
  1343. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1344. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1345. surface != NULL ? "(success)" : SDL_GetError());
  1346. SDL_DestroySurface(surface);
  1347. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1348. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1349. surface != NULL ? "(success)" : SDL_GetError());
  1350. SDL_DestroySurface(surface);
  1351. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1352. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1353. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1354. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1355. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1356. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1357. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1358. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1359. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  1360. SDL_ClearError();
  1361. expectedError = "aligning pitch would overflow";
  1362. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  1363. * alignment padding makes it overflow */
  1364. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  1365. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  1366. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1367. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1368. SDL_ClearError();
  1369. expectedError = "width * bpp would overflow";
  1370. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  1371. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  1372. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  1373. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1374. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1375. SDL_ClearError();
  1376. expectedError = "height * pitch would overflow";
  1377. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  1378. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  1379. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1380. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1381. SDL_ClearError();
  1382. expectedError = "height * pitch would overflow";
  1383. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  1384. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  1385. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1386. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1387. } else {
  1388. SDLTest_Log("Can't easily overflow size_t on this platform");
  1389. }
  1390. return TEST_COMPLETED;
  1391. }
  1392. static int surface_testSetGetSurfaceClipRect(void *args)
  1393. {
  1394. const struct {
  1395. SDL_Rect r;
  1396. bool clipsetval;
  1397. bool cmpval;
  1398. } rect_list[] = {
  1399. { { 0, 0, 0, 0}, false, true},
  1400. { { 2, 2, 0, 0}, false, true},
  1401. { { 2, 2, 5, 1}, true, true},
  1402. { { 6, 5, 10, 3}, true, false},
  1403. { { 0, 0, 10, 10}, true, true},
  1404. { { 0, 0, -10, 10}, false, true},
  1405. { { 0, 0, -10, -10}, false, true},
  1406. { { -10, -10, 10, 10}, false, false},
  1407. { { 10, -10, 10, 10}, false, false},
  1408. { { 10, 10, 10, 10}, true, false}
  1409. };
  1410. SDL_Surface *s;
  1411. SDL_Rect r;
  1412. int i;
  1413. bool b;
  1414. SDLTest_AssertPass("About to call SDL_CreateSurface(15, 15, SDL_PIXELFORMAT_RGBA32)");
  1415. s = SDL_CreateSurface(15, 15, SDL_PIXELFORMAT_RGBA32);
  1416. SDLTest_AssertCheck(s != NULL, "SDL_CreateSurface returned non-null surface");
  1417. SDL_zero(r);
  1418. b = SDL_GetSurfaceClipRect(s, &r);
  1419. SDLTest_AssertCheck(b, "SDL_GetSurfaceClipRect succeeded (%s)", SDL_GetError());
  1420. SDLTest_AssertCheck(r.x == 0 && r.y == 0 && r.w == 15 && r.h == 15,
  1421. "SDL_GetSurfaceClipRect of just-created surface. Got {%d, %d, %d, %d}. (Expected {%d, %d, %d, %d})",
  1422. r.x, r.y, r.w, r.h, 0, 0, 15, 15);
  1423. for (i = 0; i < SDL_arraysize(rect_list); i++) {
  1424. const SDL_Rect *r_in = &rect_list[i].r;
  1425. SDL_Rect r_out;
  1426. SDLTest_AssertPass("About to do SDL_SetClipRect({%d, %d, %d, %d})", r_in->x, r_in->y, r_in->w, r_in->h);
  1427. b = SDL_SetSurfaceClipRect(s, r_in);
  1428. SDLTest_AssertCheck(b == rect_list[i].clipsetval, "SDL_SetSurfaceClipRect returned %d (expected %d)", b, rect_list[i].clipsetval);
  1429. SDL_zero(r_out);
  1430. SDL_GetSurfaceClipRect(s, &r_out);
  1431. SDLTest_AssertPass("SDL_GetSurfaceClipRect returned {%d, %d, %d, %d}", r_out.x, r_out.y, r_out.w, r_out.h);
  1432. 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;
  1433. SDLTest_AssertCheck(b == rect_list[i].cmpval, "Current clipping rect is identical to input clipping rect: %d (expected %d)",
  1434. b, rect_list[i].cmpval);
  1435. }
  1436. SDL_DestroySurface(s);
  1437. return TEST_COMPLETED;
  1438. };
  1439. static int SDLCALL surface_testFlip(void *arg)
  1440. {
  1441. SDL_Surface *surface;
  1442. Uint8 *pixels;
  1443. int offset;
  1444. const char *expectedError;
  1445. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1446. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1447. SDL_ClearError();
  1448. expectedError = "Parameter 'surface' is invalid";
  1449. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1450. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1451. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1452. SDL_ClearError();
  1453. expectedError = "Parameter 'flip' is invalid";
  1454. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1455. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1456. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1457. pixels = (Uint8 *)surface->pixels;
  1458. *pixels = 0xFF;
  1459. offset = 0;
  1460. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1461. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1462. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1463. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1464. offset = 2 * surface->pitch;
  1465. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1466. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1467. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1468. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1469. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1470. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1471. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1472. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1473. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1474. SDL_DestroySurface(surface);
  1475. return TEST_COMPLETED;
  1476. }
  1477. static int SDLCALL surface_testPalette(void *arg)
  1478. {
  1479. SDL_Surface *source, *surface, *output;
  1480. SDL_Palette *palette;
  1481. Uint8 *pixels;
  1482. palette = SDL_CreatePalette(2);
  1483. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1484. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1485. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1486. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1487. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1488. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1489. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1490. pixels = (Uint8 *)surface->pixels;
  1491. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1492. /* Identity copy between indexed surfaces without a palette */
  1493. *(Uint8 *)source->pixels = 1;
  1494. SDL_BlitSurface(source, NULL, surface, NULL);
  1495. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1496. /* Identity copy between indexed surfaces where the source has a palette */
  1497. palette->colors[0].r = 0;
  1498. palette->colors[0].g = 0;
  1499. palette->colors[0].b = 0;
  1500. palette->colors[1].r = 0xFF;
  1501. palette->colors[1].g = 0;
  1502. palette->colors[1].b = 0;
  1503. SDL_SetSurfacePalette(source, palette);
  1504. *pixels = 0;
  1505. SDL_BlitSurface(source, NULL, surface, NULL);
  1506. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1507. /* Identity copy between indexed surfaces where the destination has a palette */
  1508. palette->colors[0].r = 0;
  1509. palette->colors[0].g = 0;
  1510. palette->colors[0].b = 0;
  1511. palette->colors[1].r = 0xFF;
  1512. palette->colors[1].g = 0;
  1513. palette->colors[1].b = 0;
  1514. SDL_SetSurfacePalette(source, NULL);
  1515. SDL_SetSurfacePalette(surface, palette);
  1516. *pixels = 0;
  1517. SDL_BlitSurface(source, NULL, surface, NULL);
  1518. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1519. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1520. palette->colors[0].r = 0;
  1521. palette->colors[0].g = 0;
  1522. palette->colors[0].b = 0;
  1523. palette->colors[1].r = 0xFF;
  1524. palette->colors[1].g = 0;
  1525. palette->colors[1].b = 0;
  1526. SDL_SetSurfacePalette(source, palette);
  1527. SDL_SetSurfacePalette(surface, palette);
  1528. *pixels = 0;
  1529. SDL_BlitSurface(source, NULL, surface, NULL);
  1530. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1531. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1532. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1533. pixels = (Uint8 *)output->pixels;
  1534. SDL_BlitSurface(surface, NULL, output, NULL);
  1535. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1536. /* Set the palette color and blit again */
  1537. palette->colors[1].r = 0xAA;
  1538. SDL_SetSurfacePalette(surface, palette);
  1539. SDL_BlitSurface(surface, NULL, output, NULL);
  1540. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1541. SDL_DestroyPalette(palette);
  1542. SDL_DestroySurface(source);
  1543. SDL_DestroySurface(surface);
  1544. SDL_DestroySurface(output);
  1545. return TEST_COMPLETED;
  1546. }
  1547. static int SDLCALL surface_testPalettization(void *arg)
  1548. {
  1549. const SDL_Color palette_colors[] = {
  1550. { 0x80, 0x00, 0x00, 0xff },
  1551. { 0x00, 0x80, 0x00, 0xff },
  1552. { 0x00, 0x00, 0x80, 0xff },
  1553. { 0x40, 0x00, 0x00, 0xff },
  1554. { 0x00, 0x40, 0x00, 0xff },
  1555. { 0x00, 0x00, 0x40, 0xff },
  1556. { 0x00, 0x00, 0x00, 0xff },
  1557. { 0xff, 0x00, 0x00, 0xff },
  1558. { 0x00, 0xff, 0x00, 0xff },
  1559. { 0x00, 0x00, 0xff, 0xff },
  1560. { 0xff, 0xff, 0x00, 0xff },
  1561. { 0x00, 0xff, 0xff, 0xff },
  1562. { 0xff, 0x00, 0xff, 0xff },
  1563. };
  1564. const struct {
  1565. SDL_Color c;
  1566. Uint8 e;
  1567. } colors[] = {
  1568. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1569. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1570. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1571. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1572. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1573. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1574. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1575. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1576. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1577. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1578. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1579. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1580. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1581. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1582. };
  1583. int i;
  1584. int result;
  1585. SDL_Surface *source, *output;
  1586. SDL_Palette *palette;
  1587. Uint8 *pixels;
  1588. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1589. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1590. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1591. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1592. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1593. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1594. 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);
  1595. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1596. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1597. for (i = 0; i < SDL_arraysize(colors); i++) {
  1598. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1599. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1600. }
  1601. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1602. 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);
  1603. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1604. }
  1605. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1606. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1607. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1608. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1609. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1610. pixels = output->pixels;
  1611. for (i = 0; i < SDL_arraysize(colors); i++) {
  1612. int idx = i;
  1613. Uint8 actual = pixels[idx];
  1614. Uint8 expected = colors[i].e;
  1615. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1616. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1617. }
  1618. SDLTest_AssertPass("Check palette 1:1 mapping");
  1619. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1620. int idx = SDL_arraysize(colors) + i;
  1621. Uint8 actual = pixels[idx];
  1622. Uint8 expected = i;
  1623. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1624. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1625. }
  1626. SDL_DestroyPalette(palette);
  1627. SDL_DestroySurface(source);
  1628. SDL_DestroySurface(output);
  1629. return TEST_COMPLETED;
  1630. }
  1631. static int SDLCALL surface_testClearSurface(void *arg)
  1632. {
  1633. SDL_PixelFormat formats[] = {
  1634. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1635. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1636. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1637. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1638. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1639. };
  1640. SDL_Surface *surface;
  1641. SDL_PixelFormat format;
  1642. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1643. const float MAXIMUM_ERROR_YUV = 0.01f;
  1644. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1645. float actualR, actualG, actualB, actualA;
  1646. float deltaR, deltaG, deltaB, deltaA;
  1647. int i, ret;
  1648. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1649. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1650. format = formats[i];
  1651. surface = SDL_CreateSurface(1, 1, format);
  1652. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1653. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1654. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1655. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1656. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1657. deltaR = SDL_fabsf(actualR - srcR);
  1658. deltaG = SDL_fabsf(actualG - srcG);
  1659. deltaB = SDL_fabsf(actualB - srcB);
  1660. deltaA = SDL_fabsf(actualA - srcA);
  1661. SDLTest_AssertCheck(
  1662. deltaR <= MAXIMUM_ERROR &&
  1663. deltaG <= MAXIMUM_ERROR &&
  1664. deltaB <= MAXIMUM_ERROR &&
  1665. deltaA <= MAXIMUM_ERROR,
  1666. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1667. SDL_GetPixelFormatName(format),
  1668. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1669. SDL_DestroySurface(surface);
  1670. }
  1671. return TEST_COMPLETED;
  1672. }
  1673. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1674. {
  1675. SDL_PixelFormat formats[] = {
  1676. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1677. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1678. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1679. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1680. };
  1681. SDL_Surface *surface;
  1682. SDL_PixelFormat format;
  1683. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1684. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1685. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1686. float expectedR = srcR * srcA;
  1687. float expectedG = srcG * srcA;
  1688. float expectedB = srcB * srcA;
  1689. float actualR, actualG, actualB;
  1690. float deltaR, deltaG, deltaB;
  1691. int i, ret;
  1692. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1693. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1694. format = formats[i];
  1695. surface = SDL_CreateSurface(1, 1, format);
  1696. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1697. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1698. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1699. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1700. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1701. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1702. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1703. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1704. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1705. deltaR = SDL_fabsf(actualR - expectedR);
  1706. deltaG = SDL_fabsf(actualG - expectedG);
  1707. deltaB = SDL_fabsf(actualB - expectedB);
  1708. SDLTest_AssertCheck(
  1709. deltaR <= MAXIMUM_ERROR &&
  1710. deltaG <= MAXIMUM_ERROR &&
  1711. deltaB <= MAXIMUM_ERROR,
  1712. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1713. SDL_GetPixelFormatName(format),
  1714. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1715. SDL_DestroySurface(surface);
  1716. }
  1717. return TEST_COMPLETED;
  1718. }
  1719. static int SDLCALL surface_testScale(void *arg)
  1720. {
  1721. SDL_PixelFormat formats[] = {
  1722. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1723. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1724. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1725. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1726. };
  1727. SDL_ScaleMode modes[] = {
  1728. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR, SDL_SCALEMODE_PIXELART
  1729. };
  1730. SDL_Surface *surface, *result;
  1731. SDL_PixelFormat format;
  1732. SDL_ScaleMode mode;
  1733. const float MAXIMUM_ERROR = 0.0001f;
  1734. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1735. float actualR, actualG, actualB, actualA;
  1736. float deltaR, deltaG, deltaB, deltaA;
  1737. int i, j, ret;
  1738. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1739. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1740. format = formats[i];
  1741. mode = modes[j];
  1742. surface = SDL_CreateSurface(1, 1, format);
  1743. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1744. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1745. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1746. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1747. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1748. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1749. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1750. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1751. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1752. deltaR = SDL_fabsf(actualR - srcR);
  1753. deltaG = SDL_fabsf(actualG - srcG);
  1754. deltaB = SDL_fabsf(actualB - srcB);
  1755. deltaA = SDL_fabsf(actualA - srcA);
  1756. SDLTest_AssertCheck(
  1757. deltaR <= MAXIMUM_ERROR &&
  1758. deltaG <= MAXIMUM_ERROR &&
  1759. deltaB <= MAXIMUM_ERROR &&
  1760. deltaA <= MAXIMUM_ERROR,
  1761. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1762. SDL_GetPixelFormatName(format),
  1763. mode == SDL_SCALEMODE_NEAREST ? "nearest" :
  1764. mode == SDL_SCALEMODE_LINEAR ? "linear" :
  1765. mode == SDL_SCALEMODE_PIXELART ? "pixelart" : "unknown",
  1766. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1767. SDL_DestroySurface(surface);
  1768. SDL_DestroySurface(result);
  1769. }
  1770. }
  1771. return TEST_COMPLETED;
  1772. }
  1773. #define GENERATE_SHIFTS
  1774. static Uint32 Calculate(int v, int bits, int vmax, int shift)
  1775. {
  1776. #if defined(GENERATE_FLOOR)
  1777. return (Uint32)SDL_floor(v * 255.0f / vmax) << shift;
  1778. #elif defined(GENERATE_ROUND)
  1779. return (Uint32)SDL_roundf(v * 255.0f / vmax) << shift;
  1780. #elif defined(GENERATE_SHIFTS)
  1781. switch (bits) {
  1782. case 1:
  1783. v = (v << 7) | (v << 6) | (v << 5) | (v << 4) | (v << 3) | (v << 2) | (v << 1) | v;
  1784. break;
  1785. case 2:
  1786. v = (v << 6) | (v << 4) | (v << 2) | v;
  1787. break;
  1788. case 3:
  1789. v = (v << 5) | (v << 2) | (v >> 1);
  1790. break;
  1791. case 4:
  1792. v = (v << 4) | v;
  1793. break;
  1794. case 5:
  1795. v = (v << 3) | (v >> 2);
  1796. break;
  1797. case 6:
  1798. v = (v << 2) | (v >> 4);
  1799. break;
  1800. case 7:
  1801. v = (v << 1) | (v >> 6);
  1802. break;
  1803. case 8:
  1804. break;
  1805. }
  1806. return (Uint32)v << shift;
  1807. #endif
  1808. }
  1809. static Uint32 Calculate565toARGB(int v, const SDL_PixelFormatDetails *fmt)
  1810. {
  1811. Uint8 r = (v & 0xF800) >> 11;
  1812. Uint8 g = (v & 0x07E0) >> 5;
  1813. Uint8 b = (v & 0x001F);
  1814. return fmt->Amask |
  1815. Calculate(r, 5, 31, fmt->Rshift) |
  1816. Calculate(g, 6, 63, fmt->Gshift) |
  1817. Calculate(b, 5, 31, fmt->Bshift);
  1818. }
  1819. static int SDLCALL surface_test16BitTo32Bit(void *arg)
  1820. {
  1821. static const SDL_PixelFormat formats[] = {
  1822. SDL_PIXELFORMAT_ARGB8888,
  1823. SDL_PIXELFORMAT_ABGR8888,
  1824. SDL_PIXELFORMAT_RGBA8888,
  1825. SDL_PIXELFORMAT_BGRA8888
  1826. };
  1827. static Uint16 pixels[1 << 16];
  1828. static Uint32 expected[1 << 16];
  1829. int i, p, ret;
  1830. SDL_Surface *surface16;
  1831. SDL_Surface *surface32;
  1832. SDL_Surface *expected32;
  1833. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1834. pixels[p] = p;
  1835. }
  1836. surface16 = SDL_CreateSurfaceFrom(SDL_arraysize(pixels), 1, SDL_PIXELFORMAT_RGB565, pixels, sizeof(pixels));
  1837. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1838. SDL_PixelFormat format = formats[i];
  1839. const SDL_PixelFormatDetails *fmt = SDL_GetPixelFormatDetails(format);
  1840. SDLTest_Log("Checking conversion from SDL_PIXELFORMAT_RGB565 to %s", SDL_GetPixelFormatName(format));
  1841. surface32 = SDL_ConvertSurface(surface16, format);
  1842. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1843. expected[p] = Calculate565toARGB(p, fmt);
  1844. }
  1845. expected32 = SDL_CreateSurfaceFrom(SDL_arraysize(expected), 1, format, expected, sizeof(expected));
  1846. ret = SDLTest_CompareSurfaces(surface32, expected32, 0);
  1847. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1848. SDL_DestroySurface(surface32);
  1849. SDL_DestroySurface(expected32);
  1850. }
  1851. SDL_DestroySurface(surface16);
  1852. return TEST_COMPLETED;
  1853. }
  1854. /* ================= Test References ================== */
  1855. /* Surface test cases */
  1856. static const SDLTest_TestCaseReference surfaceTestInvalidFormat = {
  1857. surface_testInvalidFormat, "surface_testInvalidFormat", "Tests creating surface with invalid format", TEST_ENABLED
  1858. };
  1859. static const SDLTest_TestCaseReference surfaceTestSaveLoad = {
  1860. surface_testSaveLoad, "surface_testSaveLoad", "Tests sprite saving and loading.", TEST_ENABLED
  1861. };
  1862. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1863. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1864. };
  1865. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1866. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1867. };
  1868. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1869. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1870. };
  1871. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1872. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1873. };
  1874. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1875. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1876. };
  1877. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1878. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1879. };
  1880. static const SDLTest_TestCaseReference surfaceTestNULLPixels = {
  1881. surface_testSurfaceNULLPixels, "surface_testSurfaceNULLPixels", "Tests surface operations with NULL pixels.", TEST_ENABLED
  1882. };
  1883. static const SDLTest_TestCaseReference surfaceTestRLEPixels = {
  1884. surface_testSurfaceRLEPixels, "surface_testSurfaceRLEPixels", "Tests surface operations with RLE surfaces.", TEST_ENABLED
  1885. };
  1886. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1887. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1888. };
  1889. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1890. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1891. };
  1892. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1893. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1894. };
  1895. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1896. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1897. };
  1898. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1899. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1900. };
  1901. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1902. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1903. };
  1904. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1905. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1906. };
  1907. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1908. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1909. };
  1910. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1911. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1912. };
  1913. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1914. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1915. };
  1916. static const SDLTest_TestCaseReference surfaceTestBlitBitmap = {
  1917. surface_testBlitBitmap, "surface_testBlitBitmap", "Tests blitting routines with bitmap surfaces.", TEST_ENABLED
  1918. };
  1919. static const SDLTest_TestCaseReference surfaceTestBlitInvalid = {
  1920. surface_testBlitInvalid, "surface_testBlitInvalid", "Tests blitting routines with invalid surfaces.", TEST_ENABLED
  1921. };
  1922. static const SDLTest_TestCaseReference surfaceTestBlitsWithBadCoordinates = {
  1923. surface_testBlitsWithBadCoordinates, "surface_testBlitsWithBadCoordinates", "Test blitting routines with bad coordinates.", TEST_ENABLED
  1924. };
  1925. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1926. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1927. };
  1928. static const SDLTest_TestCaseReference surfaceTestSetGetClipRect = {
  1929. surface_testSetGetSurfaceClipRect, "surface_testSetGetSurfaceClipRect", "Test SDL_(Set|Get)SurfaceClipRect.", TEST_ENABLED
  1930. };
  1931. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1932. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1933. };
  1934. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1935. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1936. };
  1937. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1938. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1939. };
  1940. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1941. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1942. };
  1943. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1944. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1945. };
  1946. static const SDLTest_TestCaseReference surfaceTestScale = {
  1947. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1948. };
  1949. static const SDLTest_TestCaseReference surfaceTest16BitTo32Bit = {
  1950. surface_test16BitTo32Bit, "surface_test16BitTo32Bit", "Test conversion from 16-bit to 32-bit pixels.", TEST_ENABLED
  1951. };
  1952. /* Sequence of Surface test cases */
  1953. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1954. &surfaceTestInvalidFormat,
  1955. &surfaceTestSaveLoad,
  1956. &surfaceTestBlitZeroSource,
  1957. &surfaceTestBlit,
  1958. &surfaceTestBlitTiled,
  1959. &surfaceTestBlit9Grid,
  1960. &surfaceTestBlitMultiple,
  1961. &surfaceTestLoadFailure,
  1962. &surfaceTestNULLPixels,
  1963. &surfaceTestRLEPixels,
  1964. &surfaceTestSurfaceConversion,
  1965. &surfaceTestCompleteSurfaceConversion,
  1966. &surfaceTestBlitColorMod,
  1967. &surfaceTestBlitAlphaMod,
  1968. &surfaceTestBlitBlendBlend,
  1969. &surfaceTestBlitBlendPremultiplied,
  1970. &surfaceTestBlitBlendAdd,
  1971. &surfaceTestBlitBlendAddPremultiplied,
  1972. &surfaceTestBlitBlendMod,
  1973. &surfaceTestBlitBlendMul,
  1974. &surfaceTestBlitBitmap,
  1975. &surfaceTestBlitInvalid,
  1976. &surfaceTestBlitsWithBadCoordinates,
  1977. &surfaceTestOverflow,
  1978. &surfaceTestSetGetClipRect,
  1979. &surfaceTestFlip,
  1980. &surfaceTestPalette,
  1981. &surfaceTestPalettization,
  1982. &surfaceTestClearSurface,
  1983. &surfaceTestPremultiplyAlpha,
  1984. &surfaceTestScale,
  1985. &surfaceTest16BitTo32Bit,
  1986. NULL
  1987. };
  1988. /* Surface test suite (global) */
  1989. SDLTest_TestSuiteReference surfaceTestSuite = {
  1990. "Surface",
  1991. surfaceSetUp,
  1992. surfaceTests,
  1993. surfaceTearDown
  1994. };