1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20 */ 21 #include "../SDL_internal.h" 22 23 #ifndef SDL_blit_h_ 24 #define SDL_blit_h_ 25 26 #include "SDL_cpuinfo.h" 27 #include "SDL_endian.h" 28 #include "SDL_surface.h" 29 30 /* Table to do pixel byte expansion */ 31 extern Uint8* SDL_expand_byte[9]; 32 33 /* SDL blit copy flags */ 34 #define SDL_COPY_MODULATE_COLOR 0x00000001 35 #define SDL_COPY_MODULATE_ALPHA 0x00000002 36 #define SDL_COPY_BLEND 0x00000010 37 #define SDL_COPY_ADD 0x00000020 38 #define SDL_COPY_MOD 0x00000040 39 #define SDL_COPY_MUL 0x00000080 40 #define SDL_COPY_COLORKEY 0x00000100 41 #define SDL_COPY_NEAREST 0x00000200 42 #define SDL_COPY_RLE_DESIRED 0x00001000 43 #define SDL_COPY_RLE_COLORKEY 0x00002000 44 #define SDL_COPY_RLE_ALPHAKEY 0x00004000 45 #define SDL_COPY_RLE_MASK (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY) 46 47 /* SDL blit CPU flags */ 48 #define SDL_CPU_ANY 0x00000000 49 #define SDL_CPU_MMX 0x00000001 50 #define SDL_CPU_3DNOW 0x00000002 51 #define SDL_CPU_SSE 0x00000004 52 #define SDL_CPU_SSE2 0x00000008 53 #define SDL_CPU_ALTIVEC_PREFETCH 0x00000010 54 #define SDL_CPU_ALTIVEC_NOPREFETCH 0x00000020 55 56 typedef struct 57 { 58 Uint8 *src; 59 int src_w, src_h; 60 int src_pitch; 61 int src_skip; 62 Uint8 *dst; 63 int dst_w, dst_h; 64 int dst_pitch; 65 int dst_skip; 66 SDL_PixelFormat *src_fmt; 67 SDL_PixelFormat *dst_fmt; 68 Uint8 *table; 69 int flags; 70 Uint32 colorkey; 71 Uint8 r, g, b, a; 72 } SDL_BlitInfo; 73 74 typedef void (*SDL_BlitFunc) (SDL_BlitInfo *info); 75 76 77 typedef struct 78 { 79 Uint32 src_format; 80 Uint32 dst_format; 81 int flags; 82 int cpu; 83 SDL_BlitFunc func; 84 } SDL_BlitFuncEntry; 85 86 /* Blit mapping definition */ 87 typedef struct SDL_BlitMap 88 { 89 SDL_Surface *dst; 90 int identity; 91 SDL_blit blit; 92 void *data; 93 SDL_BlitInfo info; 94 95 /* the version count matches the destination; mismatch indicates 96 an invalid mapping */ 97 Uint32 dst_palette_version; 98 Uint32 src_palette_version; 99 } SDL_BlitMap; 100 101 /* Functions found in SDL_blit.c */ 102 extern int SDL_CalculateBlit(SDL_Surface * surface); 103 104 /* Functions found in SDL_blit_*.c */ 105 extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface); 106 extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface); 107 extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface); 108 extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface); 109 110 /* 111 * Useful macros for blitting routines 112 */ 113 114 #if defined(__GNUC__) 115 #define DECLARE_ALIGNED(t,v,a) t __attribute__((aligned(a))) v 116 #elif defined(_MSC_VER) 117 #define DECLARE_ALIGNED(t,v,a) __declspec(align(a)) t v 118 #else 119 #define DECLARE_ALIGNED(t,v,a) t v 120 #endif 121 122 /* Load pixel of the specified format from a buffer and get its R-G-B values */ 123 #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \ 124 { \ 125 r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \ 126 g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \ 127 b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \ 128 } 129 #define RGB_FROM_RGB565(Pixel, r, g, b) \ 130 { \ 131 r = SDL_expand_byte[3][((Pixel&0xF800)>>11)]; \ 132 g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; \ 133 b = SDL_expand_byte[3][(Pixel&0x001F)]; \ 134 } 135 #define RGB_FROM_RGB555(Pixel, r, g, b) \ 136 { \ 137 r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)]; \ 138 g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; \ 139 b = SDL_expand_byte[3][(Pixel&0x001F)]; \ 140 } 141 #define RGB_FROM_RGB888(Pixel, r, g, b) \ 142 { \ 143 r = ((Pixel&0xFF0000)>>16); \ 144 g = ((Pixel&0xFF00)>>8); \ 145 b = (Pixel&0xFF); \ 146 } 147 #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \ 148 do { \ 149 switch (bpp) { \ 150 case 1: \ 151 Pixel = *((Uint8 *)(buf)); \ 152 break; \ 153 \ 154 case 2: \ 155 Pixel = *((Uint16 *)(buf)); \ 156 break; \ 157 \ 158 case 3: { \ 159 Uint8 *B = (Uint8 *)(buf); \ 160 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \ 161 Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \ 162 } else { \ 163 Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \ 164 } \ 165 } \ 166 break; \ 167 \ 168 case 4: \ 169 Pixel = *((Uint32 *)(buf)); \ 170 break; \ 171 \ 172 default: \ 173 Pixel = 0; /* stop gcc complaints */ \ 174 break; \ 175 } \ 176 } while (0) 177 178 #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \ 179 do { \ 180 switch (bpp) { \ 181 case 1: \ 182 Pixel = *((Uint8 *)(buf)); \ 183 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \ 184 break; \ 185 \ 186 case 2: \ 187 Pixel = *((Uint16 *)(buf)); \ 188 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \ 189 break; \ 190 \ 191 case 3: { \ 192 Pixel = 0; \ 193 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \ 194 r = *((buf)+fmt->Rshift/8); \ 195 g = *((buf)+fmt->Gshift/8); \ 196 b = *((buf)+fmt->Bshift/8); \ 197 } else { \ 198 r = *((buf)+2-fmt->Rshift/8); \ 199 g = *((buf)+2-fmt->Gshift/8); \ 200 b = *((buf)+2-fmt->Bshift/8); \ 201 } \ 202 } \ 203 break; \ 204 \ 205 case 4: \ 206 Pixel = *((Uint32 *)(buf)); \ 207 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \ 208 break; \ 209 \ 210 default: \ 211 /* stop gcc complaints */ \ 212 Pixel = 0; \ 213 r = g = b = 0; \ 214 break; \ 215 } \ 216 } while (0) 217 218 /* Assemble R-G-B values into a specified pixel format and store them */ 219 #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \ 220 { \ 221 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \ 222 ((g>>fmt->Gloss)<<fmt->Gshift)| \ 223 ((b>>fmt->Bloss)<<fmt->Bshift)| \ 224 fmt->Amask; \ 225 } 226 #define RGB565_FROM_RGB(Pixel, r, g, b) \ 227 { \ 228 Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3); \ 229 } 230 #define RGB555_FROM_RGB(Pixel, r, g, b) \ 231 { \ 232 Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3); \ 233 } 234 #define RGB888_FROM_RGB(Pixel, r, g, b) \ 235 { \ 236 Pixel = (r<<16)|(g<<8)|b; \ 237 } 238 #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \ 239 { \ 240 Pixel = (a<<24)|(r<<16)|(g<<8)|b; \ 241 } 242 #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \ 243 { \ 244 Pixel = (r<<24)|(g<<16)|(b<<8)|a; \ 245 } 246 #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \ 247 { \ 248 Pixel = (a<<24)|(b<<16)|(g<<8)|r; \ 249 } 250 #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \ 251 { \ 252 Pixel = (b<<24)|(g<<16)|(r<<8)|a; \ 253 } 254 #define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a) \ 255 { \ 256 r = r ? ((r << 2) | 0x3) : 0; \ 257 g = g ? ((g << 2) | 0x3) : 0; \ 258 b = b ? ((b << 2) | 0x3) : 0; \ 259 a = (a * 3) / 255; \ 260 Pixel = (a<<30)|(r<<20)|(g<<10)|b; \ 261 } 262 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \ 263 { \ 264 switch (bpp) { \ 265 case 1: { \ 266 Uint8 _Pixel; \ 267 \ 268 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \ 269 *((Uint8 *)(buf)) = _Pixel; \ 270 } \ 271 break; \ 272 \ 273 case 2: { \ 274 Uint16 _Pixel; \ 275 \ 276 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \ 277 *((Uint16 *)(buf)) = _Pixel; \ 278 } \ 279 break; \ 280 \ 281 case 3: { \ 282 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \ 283 *((buf)+fmt->Rshift/8) = r; \ 284 *((buf)+fmt->Gshift/8) = g; \ 285 *((buf)+fmt->Bshift/8) = b; \ 286 } else { \ 287 *((buf)+2-fmt->Rshift/8) = r; \ 288 *((buf)+2-fmt->Gshift/8) = g; \ 289 *((buf)+2-fmt->Bshift/8) = b; \ 290 } \ 291 } \ 292 break; \ 293 \ 294 case 4: { \ 295 Uint32 _Pixel; \ 296 \ 297 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \ 298 *((Uint32 *)(buf)) = _Pixel; \ 299 } \ 300 break; \ 301 } \ 302 } 303 304 /* FIXME: Should we rescale alpha into 0..255 here? */ 305 #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \ 306 { \ 307 r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \ 308 g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \ 309 b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \ 310 a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \ 311 } 312 #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \ 313 { \ 314 r = (Pixel&fmt->Rmask)>>fmt->Rshift; \ 315 g = (Pixel&fmt->Gmask)>>fmt->Gshift; \ 316 b = (Pixel&fmt->Bmask)>>fmt->Bshift; \ 317 a = (Pixel&fmt->Amask)>>fmt->Ashift; \ 318 } 319 #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \ 320 { \ 321 r = (Pixel>>24); \ 322 g = ((Pixel>>16)&0xFF); \ 323 b = ((Pixel>>8)&0xFF); \ 324 a = (Pixel&0xFF); \ 325 } 326 #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \ 327 { \ 328 r = ((Pixel>>16)&0xFF); \ 329 g = ((Pixel>>8)&0xFF); \ 330 b = (Pixel&0xFF); \ 331 a = (Pixel>>24); \ 332 } 333 #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \ 334 { \ 335 r = (Pixel&0xFF); \ 336 g = ((Pixel>>8)&0xFF); \ 337 b = ((Pixel>>16)&0xFF); \ 338 a = (Pixel>>24); \ 339 } 340 #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \ 341 { \ 342 r = ((Pixel>>8)&0xFF); \ 343 g = ((Pixel>>16)&0xFF); \ 344 b = (Pixel>>24); \ 345 a = (Pixel&0xFF); \ 346 } 347 #define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a) \ 348 { \ 349 r = ((Pixel>>22)&0xFF); \ 350 g = ((Pixel>>12)&0xFF); \ 351 b = ((Pixel>>2)&0xFF); \ 352 a = SDL_expand_byte[6][(Pixel>>30)]; \ 353 } 354 #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \ 355 do { \ 356 switch (bpp) { \ 357 case 1: \ 358 Pixel = *((Uint8 *)(buf)); \ 359 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \ 360 break; \ 361 \ 362 case 2: \ 363 Pixel = *((Uint16 *)(buf)); \ 364 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \ 365 break; \ 366 \ 367 case 3: { \ 368 Pixel = 0; \ 369 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \ 370 r = *((buf)+fmt->Rshift/8); \ 371 g = *((buf)+fmt->Gshift/8); \ 372 b = *((buf)+fmt->Bshift/8); \ 373 } else { \ 374 r = *((buf)+2-fmt->Rshift/8); \ 375 g = *((buf)+2-fmt->Gshift/8); \ 376 b = *((buf)+2-fmt->Bshift/8); \ 377 } \ 378 a = 0xFF; \ 379 } \ 380 break; \ 381 \ 382 case 4: \ 383 Pixel = *((Uint32 *)(buf)); \ 384 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \ 385 break; \ 386 \ 387 default: \ 388 /* stop gcc complaints */ \ 389 Pixel = 0; \ 390 r = g = b = a = 0; \ 391 break; \ 392 } \ 393 } while (0) 394 395 /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */ 396 #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \ 397 { \ 398 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \ 399 ((g>>fmt->Gloss)<<fmt->Gshift)| \ 400 ((b>>fmt->Bloss)<<fmt->Bshift)| \ 401 ((a>>fmt->Aloss)<<fmt->Ashift); \ 402 } 403 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \ 404 { \ 405 switch (bpp) { \ 406 case 1: { \ 407 Uint8 _pixel; \ 408 \ 409 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \ 410 *((Uint8 *)(buf)) = _pixel; \ 411 } \ 412 break; \ 413 \ 414 case 2: { \ 415 Uint16 _pixel; \ 416 \ 417 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \ 418 *((Uint16 *)(buf)) = _pixel; \ 419 } \ 420 break; \ 421 \ 422 case 3: { \ 423 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \ 424 *((buf)+fmt->Rshift/8) = r; \ 425 *((buf)+fmt->Gshift/8) = g; \ 426 *((buf)+fmt->Bshift/8) = b; \ 427 } else { \ 428 *((buf)+2-fmt->Rshift/8) = r; \ 429 *((buf)+2-fmt->Gshift/8) = g; \ 430 *((buf)+2-fmt->Bshift/8) = b; \ 431 } \ 432 } \ 433 break; \ 434 \ 435 case 4: { \ 436 Uint32 _pixel; \ 437 \ 438 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \ 439 *((Uint32 *)(buf)) = _pixel; \ 440 } \ 441 break; \ 442 } \ 443 } 444 445 /* Blend the RGB values of two pixels with an alpha value */ 446 #define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB) \ 447 do { \ 448 dR = (Uint8)((((int)(sR-dR)*(int)A)/255)+dR); \ 449 dG = (Uint8)((((int)(sG-dG)*(int)A)/255)+dG); \ 450 dB = (Uint8)((((int)(sB-dB)*(int)A)/255)+dB); \ 451 } while(0) 452 453 454 /* Blend the RGBA values of two pixels */ 455 #define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA) \ 456 do { \ 457 dR = (Uint8)((((int)(sR-dR)*(int)sA)/255)+dR); \ 458 dG = (Uint8)((((int)(sG-dG)*(int)sA)/255)+dG); \ 459 dB = (Uint8)((((int)(sB-dB)*(int)sA)/255)+dB); \ 460 dA = (Uint8)((int)sA+dA-((int)sA*dA)/255); \ 461 } while(0) 462 463 464 /* This is a very useful loop for optimizing blitters */ 465 #if defined(_MSC_VER) && (_MSC_VER == 1300) 466 /* There's a bug in the Visual C++ 7 optimizer when compiling this code */ 467 #else 468 #define USE_DUFFS_LOOP 469 #endif 470 #ifdef USE_DUFFS_LOOP 471 472 /* 8-times unrolled loop */ 473 #define DUFFS_LOOP8(pixel_copy_increment, width) \ 474 { int n = (width+7)/8; \ 475 switch (width & 7) { \ 476 case 0: do { pixel_copy_increment; /* fallthrough */ \ 477 case 7: pixel_copy_increment; /* fallthrough */ \ 478 case 6: pixel_copy_increment; /* fallthrough */ \ 479 case 5: pixel_copy_increment; /* fallthrough */ \ 480 case 4: pixel_copy_increment; /* fallthrough */ \ 481 case 3: pixel_copy_increment; /* fallthrough */ \ 482 case 2: pixel_copy_increment; /* fallthrough */ \ 483 case 1: pixel_copy_increment; /* fallthrough */ \ 484 } while ( --n > 0 ); \ 485 } \ 486 } 487 488 /* 4-times unrolled loop */ 489 #define DUFFS_LOOP4(pixel_copy_increment, width) \ 490 { int n = (width+3)/4; \ 491 switch (width & 3) { \ 492 case 0: do { pixel_copy_increment; /* fallthrough */ \ 493 case 3: pixel_copy_increment; /* fallthrough */ \ 494 case 2: pixel_copy_increment; /* fallthrough */ \ 495 case 1: pixel_copy_increment; /* fallthrough */ \ 496 } while (--n > 0); \ 497 } \ 498 } 499 500 /* Use the 8-times version of the loop by default */ 501 #define DUFFS_LOOP(pixel_copy_increment, width) \ 502 DUFFS_LOOP8(pixel_copy_increment, width) 503 504 /* Special version of Duff's device for even more optimization */ 505 #define DUFFS_LOOP_124(pixel_copy_increment1, \ 506 pixel_copy_increment2, \ 507 pixel_copy_increment4, width) \ 508 { int n = width; \ 509 if (n & 1) { \ 510 pixel_copy_increment1; n -= 1; \ 511 } \ 512 if (n & 2) { \ 513 pixel_copy_increment2; n -= 2; \ 514 } \ 515 if (n & 4) { \ 516 pixel_copy_increment4; n -= 4; \ 517 } \ 518 if (n) { \ 519 n /= 8; \ 520 do { \ 521 pixel_copy_increment4; \ 522 pixel_copy_increment4; \ 523 } while (--n > 0); \ 524 } \ 525 } 526 527 #else 528 529 /* Don't use Duff's device to unroll loops */ 530 #define DUFFS_LOOP(pixel_copy_increment, width) \ 531 { int n; \ 532 for ( n=width; n > 0; --n ) { \ 533 pixel_copy_increment; \ 534 } \ 535 } 536 #define DUFFS_LOOP8(pixel_copy_increment, width) \ 537 DUFFS_LOOP(pixel_copy_increment, width) 538 #define DUFFS_LOOP4(pixel_copy_increment, width) \ 539 DUFFS_LOOP(pixel_copy_increment, width) 540 #define DUFFS_LOOP_124(pixel_copy_increment1, \ 541 pixel_copy_increment2, \ 542 pixel_copy_increment4, width) \ 543 DUFFS_LOOP(pixel_copy_increment1, width) 544 545 #endif /* USE_DUFFS_LOOP */ 546 547 /* Prevent Visual C++ 6.0 from printing out stupid warnings */ 548 #if defined(_MSC_VER) && (_MSC_VER >= 600) 549 #pragma warning(disable: 4550) 550 #endif 551 552 #endif /* SDL_blit_h_ */ 553 554 /* vi: set ts=4 sw=4 expandtab: */ 555