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