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