testautomation_surface.c 92 KB

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