2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "SDL_config.h"
26 #include "SDL_cpuinfo.h"
27 #include "SDL_endian.h"
28 #include "SDL_surface.h"
30 /* Table to do pixel byte expansion */
31 extern Uint8* SDL_expand_byte[9];
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)
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
65 SDL_PixelFormat *src_fmt;
66 SDL_PixelFormat *dst_fmt;
73 typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
84 /* Blit mapping definition */
85 typedef struct SDL_BlitMap
93 /* the version count matches the destination; mismatch indicates
95 Uint32 dst_palette_version;
96 Uint32 src_palette_version;
99 /* Functions found in SDL_blit.c */
100 extern int SDL_CalculateBlit(SDL_Surface * surface);
102 /* Functions found in SDL_blit_*.c */
103 extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
104 extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
105 extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
106 extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
109 * Useful macros for blitting routines
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
117 #define DECLARE_ALIGNED(t,v,a) t v
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) \
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)]; \
127 #define RGB_FROM_RGB565(Pixel, r, g, b) \
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)]; \
133 #define RGB_FROM_RGB555(Pixel, r, g, b) \
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)]; \
139 #define RGB_FROM_RGB888(Pixel, r, g, b) \
141 r = ((Pixel&0xFF0000)>>16); \
142 g = ((Pixel&0xFF00)>>8); \
145 #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \
149 Pixel = *((Uint16 *)(buf)); \
153 Uint8 *B = (Uint8 *)(buf); \
154 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
155 Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
157 Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
163 Pixel = *((Uint32 *)(buf)); \
167 Pixel = 0; /* stop gcc complaints */ \
172 #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \
176 Pixel = *((Uint16 *)(buf)); \
177 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
182 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
183 r = *((buf)+fmt->Rshift/8); \
184 g = *((buf)+fmt->Gshift/8); \
185 b = *((buf)+fmt->Bshift/8); \
187 r = *((buf)+2-fmt->Rshift/8); \
188 g = *((buf)+2-fmt->Gshift/8); \
189 b = *((buf)+2-fmt->Bshift/8); \
195 Pixel = *((Uint32 *)(buf)); \
196 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
200 /* stop gcc complaints */ \
207 /* Assemble R-G-B values into a specified pixel format and store them */
208 #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \
210 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
211 ((g>>fmt->Gloss)<<fmt->Gshift)| \
212 ((b>>fmt->Bloss)<<fmt->Bshift)| \
215 #define RGB565_FROM_RGB(Pixel, r, g, b) \
217 Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3); \
219 #define RGB555_FROM_RGB(Pixel, r, g, b) \
221 Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3); \
223 #define RGB888_FROM_RGB(Pixel, r, g, b) \
225 Pixel = (r<<16)|(g<<8)|b; \
227 #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \
229 Pixel = (a<<24)|(r<<16)|(g<<8)|b; \
231 #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \
233 Pixel = (r<<24)|(g<<16)|(b<<8)|a; \
235 #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \
237 Pixel = (a<<24)|(b<<16)|(g<<8)|r; \
239 #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \
241 Pixel = (b<<24)|(g<<16)|(r<<8)|a; \
243 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \
249 PIXEL_FROM_RGB(Pixel, fmt, r, g, b); \
250 *((Uint16 *)(buf)) = Pixel; \
255 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
256 *((buf)+fmt->Rshift/8) = r; \
257 *((buf)+fmt->Gshift/8) = g; \
258 *((buf)+fmt->Bshift/8) = b; \
260 *((buf)+2-fmt->Rshift/8) = r; \
261 *((buf)+2-fmt->Gshift/8) = g; \
262 *((buf)+2-fmt->Bshift/8) = b; \
270 PIXEL_FROM_RGB(Pixel, fmt, r, g, b); \
271 *((Uint32 *)(buf)) = Pixel; \
277 /* FIXME: Should we rescale alpha into 0..255 here? */
278 #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \
280 r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
281 g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
282 b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
283 a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
285 #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \
287 r = (Pixel&fmt->Rmask)>>fmt->Rshift; \
288 g = (Pixel&fmt->Gmask)>>fmt->Gshift; \
289 b = (Pixel&fmt->Bmask)>>fmt->Bshift; \
290 a = (Pixel&fmt->Amask)>>fmt->Ashift; \
292 #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \
295 g = ((Pixel>>16)&0xFF); \
296 b = ((Pixel>>8)&0xFF); \
299 #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \
301 r = ((Pixel>>16)&0xFF); \
302 g = ((Pixel>>8)&0xFF); \
306 #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \
309 g = ((Pixel>>8)&0xFF); \
310 b = ((Pixel>>16)&0xFF); \
313 #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \
315 r = ((Pixel>>8)&0xFF); \
316 g = ((Pixel>>16)&0xFF); \
320 #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \
324 Pixel = *((Uint16 *)(buf)); \
325 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
330 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
331 r = *((buf)+fmt->Rshift/8); \
332 g = *((buf)+fmt->Gshift/8); \
333 b = *((buf)+fmt->Bshift/8); \
335 r = *((buf)+2-fmt->Rshift/8); \
336 g = *((buf)+2-fmt->Gshift/8); \
337 b = *((buf)+2-fmt->Bshift/8); \
344 Pixel = *((Uint32 *)(buf)); \
345 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
349 /* stop gcc complaints */ \
356 /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
357 #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
359 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
360 ((g>>fmt->Gloss)<<fmt->Gshift)| \
361 ((b>>fmt->Bloss)<<fmt->Bshift)| \
362 ((a>>fmt->Aloss)<<fmt->Ashift); \
364 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
370 PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a); \
371 *((Uint16 *)(buf)) = Pixel; \
376 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
377 *((buf)+fmt->Rshift/8) = r; \
378 *((buf)+fmt->Gshift/8) = g; \
379 *((buf)+fmt->Bshift/8) = b; \
381 *((buf)+2-fmt->Rshift/8) = r; \
382 *((buf)+2-fmt->Gshift/8) = g; \
383 *((buf)+2-fmt->Bshift/8) = b; \
391 PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a); \
392 *((Uint32 *)(buf)) = Pixel; \
398 /* Blend the RGB values of two Pixels based on a source alpha value */
399 #define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB) \
401 dR = ((((int)(sR-dR)*(int)A)/255)+dR); \
402 dG = ((((int)(sG-dG)*(int)A)/255)+dG); \
403 dB = ((((int)(sB-dB)*(int)A)/255)+dB); \
407 /* This is a very useful loop for optimizing blitters */
408 #if defined(_MSC_VER) && (_MSC_VER == 1300)
409 /* There's a bug in the Visual C++ 7 optimizer when compiling this code */
411 #define USE_DUFFS_LOOP
413 #ifdef USE_DUFFS_LOOP
415 /* 8-times unrolled loop */
416 #define DUFFS_LOOP8(pixel_copy_increment, width) \
417 { int n = (width+7)/8; \
418 switch (width & 7) { \
419 case 0: do { pixel_copy_increment; \
420 case 7: pixel_copy_increment; \
421 case 6: pixel_copy_increment; \
422 case 5: pixel_copy_increment; \
423 case 4: pixel_copy_increment; \
424 case 3: pixel_copy_increment; \
425 case 2: pixel_copy_increment; \
426 case 1: pixel_copy_increment; \
427 } while ( --n > 0 ); \
431 /* 4-times unrolled loop */
432 #define DUFFS_LOOP4(pixel_copy_increment, width) \
433 { int n = (width+3)/4; \
434 switch (width & 3) { \
435 case 0: do { pixel_copy_increment; \
436 case 3: pixel_copy_increment; \
437 case 2: pixel_copy_increment; \
438 case 1: pixel_copy_increment; \
443 /* Use the 8-times version of the loop by default */
444 #define DUFFS_LOOP(pixel_copy_increment, width) \
445 DUFFS_LOOP8(pixel_copy_increment, width)
447 /* Special version of Duff's device for even more optimization */
448 #define DUFFS_LOOP_124(pixel_copy_increment1, \
449 pixel_copy_increment2, \
450 pixel_copy_increment4, width) \
453 pixel_copy_increment1; n -= 1; \
456 pixel_copy_increment2; n -= 2; \
461 case 0: do { pixel_copy_increment4; \
462 case 4: pixel_copy_increment4; \
470 /* Don't use Duff's device to unroll loops */
471 #define DUFFS_LOOP(pixel_copy_increment, width) \
473 for ( n=width; n > 0; --n ) { \
474 pixel_copy_increment; \
477 #define DUFFS_LOOP8(pixel_copy_increment, width) \
478 DUFFS_LOOP(pixel_copy_increment, width)
479 #define DUFFS_LOOP4(pixel_copy_increment, width) \
480 DUFFS_LOOP(pixel_copy_increment, width)
481 #define DUFFS_LOOP_124(pixel_copy_increment1, \
482 pixel_copy_increment2, \
483 pixel_copy_increment4, width) \
484 DUFFS_LOOP(pixel_copy_increment1, width)
486 #endif /* USE_DUFFS_LOOP */
488 /* Prevent Visual C++ 6.0 from printing out stupid warnings */
489 #if defined(_MSC_VER) && (_MSC_VER >= 600)
490 #pragma warning(disable: 4550)
493 #endif /* _SDL_blit_h */
495 /* vi: set ts=4 sw=4 expandtab: */