src/video/SDL_blit.h
author Sam Lantinga
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5440 810b064d41fe
child 6138 4c64952a58fb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 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_config.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_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 
    55 typedef 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 
    73 typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
    74 
    75 typedef 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 */
    85 typedef 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 palette_version;
    96 } SDL_BlitMap;
    97 
    98 /* Functions found in SDL_blit.c */
    99 extern int SDL_CalculateBlit(SDL_Surface * surface);
   100 
   101 /* Functions found in SDL_blit_*.c */
   102 extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
   103 extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
   104 extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
   105 extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
   106 
   107 /*
   108  * Useful macros for blitting routines
   109  */
   110 
   111 #if defined(__GNUC__)
   112 #define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
   113 #elif defined(_MSC_VER)
   114 #define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
   115 #else
   116 #define DECLARE_ALIGNED(t,v,a)  t v
   117 #endif
   118 
   119 /* Load pixel of the specified format from a buffer and get its R-G-B values */
   120 #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
   121 {									\
   122 	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
   123 	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
   124 	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
   125 }
   126 #define RGB_FROM_RGB565(Pixel, r, g, b)					\
   127 {									\
   128 	r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];		 			\
   129 	g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; 					\
   130 	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
   131 }
   132 #define RGB_FROM_RGB555(Pixel, r, g, b)					\
   133 {									\
   134 	r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];		 			\
   135 	g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; 					\
   136 	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
   137 }
   138 #define RGB_FROM_RGB888(Pixel, r, g, b)					\
   139 {									\
   140 	r = ((Pixel&0xFF0000)>>16);		 			\
   141 	g = ((Pixel&0xFF00)>>8);		 			\
   142 	b = (Pixel&0xFF);			 			\
   143 }
   144 #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
   145 do {									   \
   146 	switch (bpp) {							   \
   147 		case 2:							   \
   148 			Pixel = *((Uint16 *)(buf));			   \
   149 		break;							   \
   150 									   \
   151 		case 3: {						   \
   152 		        Uint8 *B = (Uint8 *)(buf);			   \
   153 			if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   154 			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   155 			} else {					   \
   156 			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   157 			}						   \
   158 		}							   \
   159 		break;							   \
   160 									   \
   161 		case 4:							   \
   162 			Pixel = *((Uint32 *)(buf));			   \
   163 		break;							   \
   164 									   \
   165 		default:						   \
   166 		        Pixel = 0; /* stop gcc complaints */		   \
   167 		break;							   \
   168 	}								   \
   169 } while (0)
   170 
   171 #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
   172 do {									   \
   173 	switch (bpp) {							   \
   174 		case 2:							   \
   175 			Pixel = *((Uint16 *)(buf));			   \
   176 			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
   177 		break;							   \
   178 									   \
   179 		case 3:	{						   \
   180             Pixel = 0;                  \
   181                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   182 			        r = *((buf)+fmt->Rshift/8);		   \
   183 				g = *((buf)+fmt->Gshift/8);		   \
   184 				b = *((buf)+fmt->Bshift/8);		   \
   185 			} else {					   \
   186 			        r = *((buf)+2-fmt->Rshift/8);		   \
   187 				g = *((buf)+2-fmt->Gshift/8);		   \
   188 				b = *((buf)+2-fmt->Bshift/8);		   \
   189 			}						   \
   190 		}							   \
   191 		break;							   \
   192 									   \
   193 		case 4:							   \
   194 			Pixel = *((Uint32 *)(buf));			   \
   195 			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
   196 		break;							   \
   197 									   \
   198 		default:						   \
   199                 /* stop gcc complaints */		   \
   200 		        Pixel = 0;              \
   201                 r = g = b = 0;          \
   202 		break;							   \
   203 	}								   \
   204 } while (0)
   205 
   206 /* Assemble R-G-B values into a specified pixel format and store them */
   207 #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
   208 {									\
   209 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   210 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   211 		((b>>fmt->Bloss)<<fmt->Bshift)| \
   212         fmt->Amask;				\
   213 }
   214 #define RGB565_FROM_RGB(Pixel, r, g, b)					\
   215 {									\
   216 	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
   217 }
   218 #define RGB555_FROM_RGB(Pixel, r, g, b)					\
   219 {									\
   220 	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
   221 }
   222 #define RGB888_FROM_RGB(Pixel, r, g, b)					\
   223 {									\
   224 	Pixel = (r<<16)|(g<<8)|b;					\
   225 }
   226 #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)				\
   227 {									\
   228 	Pixel = (a<<24)|(r<<16)|(g<<8)|b;				\
   229 }
   230 #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)				\
   231 {									\
   232 	Pixel = (r<<24)|(g<<16)|(b<<8)|a;				\
   233 }
   234 #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)				\
   235 {									\
   236 	Pixel = (a<<24)|(b<<16)|(g<<8)|r;				\
   237 }
   238 #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)				\
   239 {									\
   240 	Pixel = (b<<24)|(g<<16)|(r<<8)|a;				\
   241 }
   242 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
   243 {									\
   244 	switch (bpp) {							\
   245 		case 2: {						\
   246 			Uint16 Pixel;					\
   247 									\
   248 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   249 			*((Uint16 *)(buf)) = Pixel;		\
   250 		}							\
   251 		break;							\
   252 									\
   253 		case 3: {						\
   254                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   255 			        *((buf)+fmt->Rshift/8) = r;		\
   256 				*((buf)+fmt->Gshift/8) = g;		\
   257 				*((buf)+fmt->Bshift/8) = b;		\
   258 			} else {					\
   259 			        *((buf)+2-fmt->Rshift/8) = r;		\
   260 				*((buf)+2-fmt->Gshift/8) = g;		\
   261 				*((buf)+2-fmt->Bshift/8) = b;		\
   262 			}						\
   263 		}							\
   264 		break;							\
   265 									\
   266 		case 4: {						\
   267 			Uint32 Pixel;					\
   268 									\
   269 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   270 			*((Uint32 *)(buf)) = Pixel;			\
   271 		}							\
   272 		break;							\
   273 	}								\
   274 }
   275 
   276 /* FIXME: Should we rescale alpha into 0..255 here? */
   277 #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
   278 {									\
   279 	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
   280 	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
   281 	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
   282 	a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
   283 }
   284 #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
   285 {						\
   286 	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
   287 	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
   288 	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
   289 	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
   290 }
   291 #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
   292 {									\
   293 	r = (Pixel>>24);						\
   294 	g = ((Pixel>>16)&0xFF);						\
   295 	b = ((Pixel>>8)&0xFF);						\
   296 	a = (Pixel&0xFF);						\
   297 }
   298 #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
   299 {									\
   300 	r = ((Pixel>>16)&0xFF);						\
   301 	g = ((Pixel>>8)&0xFF);						\
   302 	b = (Pixel&0xFF);						\
   303 	a = (Pixel>>24);						\
   304 }
   305 #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
   306 {									\
   307 	r = (Pixel&0xFF);						\
   308 	g = ((Pixel>>8)&0xFF);						\
   309 	b = ((Pixel>>16)&0xFF);						\
   310 	a = (Pixel>>24);						\
   311 }
   312 #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)				\
   313 {									\
   314 	r = ((Pixel>>8)&0xFF);						\
   315 	g = ((Pixel>>16)&0xFF);						\
   316 	b = (Pixel>>24);						\
   317 	a = (Pixel&0xFF);						\
   318 }
   319 #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
   320 do {									   \
   321 	switch (bpp) {							   \
   322 		case 2:							   \
   323 			Pixel = *((Uint16 *)(buf));			   \
   324 			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
   325 		break;							   \
   326 									   \
   327 		case 3:	{						   \
   328             Pixel = 0; \
   329                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   330 			        r = *((buf)+fmt->Rshift/8);		   \
   331 				g = *((buf)+fmt->Gshift/8);		   \
   332 				b = *((buf)+fmt->Bshift/8);		   \
   333 			} else {					   \
   334 			        r = *((buf)+2-fmt->Rshift/8);		   \
   335 				g = *((buf)+2-fmt->Gshift/8);		   \
   336 				b = *((buf)+2-fmt->Bshift/8);		   \
   337 			}						   \
   338 			a = 0xFF;					   \
   339 		}							   \
   340 		break;							   \
   341 									   \
   342 		case 4:							   \
   343 			Pixel = *((Uint32 *)(buf));			   \
   344 			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
   345 		break;							   \
   346 									   \
   347 		default:						   \
   348                 /* stop gcc complaints */		   \
   349 		        Pixel = 0;              \
   350                 r = g = b = a = 0;      \
   351 		break;							   \
   352 	}								   \
   353 } while (0)
   354 
   355 /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
   356 #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
   357 {									\
   358 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   359 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   360 		((b>>fmt->Bloss)<<fmt->Bshift)|				\
   361 		((a>>fmt->Aloss)<<fmt->Ashift);				\
   362 }
   363 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
   364 {									\
   365 	switch (bpp) {							\
   366 		case 2: {						\
   367 			Uint16 Pixel;					\
   368 									\
   369 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   370 			*((Uint16 *)(buf)) = Pixel;		\
   371 		}							\
   372 		break;							\
   373 									\
   374 		case 3: {						\
   375                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   376 			        *((buf)+fmt->Rshift/8) = r;		\
   377 				*((buf)+fmt->Gshift/8) = g;		\
   378 				*((buf)+fmt->Bshift/8) = b;		\
   379 			} else {					\
   380 			        *((buf)+2-fmt->Rshift/8) = r;		\
   381 				*((buf)+2-fmt->Gshift/8) = g;		\
   382 				*((buf)+2-fmt->Bshift/8) = b;		\
   383 			}						\
   384 		}							\
   385 		break;							\
   386 									\
   387 		case 4: {						\
   388 			Uint32 Pixel;					\
   389 									\
   390 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   391 			*((Uint32 *)(buf)) = Pixel;			\
   392 		}							\
   393 		break;							\
   394 	}								\
   395 }
   396 
   397 /* Blend the RGB values of two Pixels based on a source alpha value */
   398 #define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
   399 do {						\
   400 	dR = ((((int)(sR-dR)*(int)A)/255)+dR);	\
   401 	dG = ((((int)(sG-dG)*(int)A)/255)+dG);	\
   402 	dB = ((((int)(sB-dB)*(int)A)/255)+dB);	\
   403 } while(0)
   404 
   405 
   406 /* This is a very useful loop for optimizing blitters */
   407 #if defined(_MSC_VER) && (_MSC_VER == 1300)
   408 /* There's a bug in the Visual C++ 7 optimizer when compiling this code */
   409 #else
   410 #define USE_DUFFS_LOOP
   411 #endif
   412 #ifdef USE_DUFFS_LOOP
   413 
   414 /* 8-times unrolled loop */
   415 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   416 { int n = (width+7)/8;							\
   417 	switch (width & 7) {						\
   418 	case 0: do {	pixel_copy_increment;				\
   419 	case 7:		pixel_copy_increment;				\
   420 	case 6:		pixel_copy_increment;				\
   421 	case 5:		pixel_copy_increment;				\
   422 	case 4:		pixel_copy_increment;				\
   423 	case 3:		pixel_copy_increment;				\
   424 	case 2:		pixel_copy_increment;				\
   425 	case 1:		pixel_copy_increment;				\
   426 		} while ( --n > 0 );					\
   427 	}								\
   428 }
   429 
   430 /* 4-times unrolled loop */
   431 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   432 { int n = (width+3)/4;							\
   433 	switch (width & 3) {						\
   434 	case 0: do {	pixel_copy_increment;				\
   435 	case 3:		pixel_copy_increment;				\
   436 	case 2:		pixel_copy_increment;				\
   437 	case 1:		pixel_copy_increment;				\
   438 		} while (--n > 0);					\
   439 	}								\
   440 }
   441 
   442 /* Use the 8-times version of the loop by default */
   443 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   444 	DUFFS_LOOP8(pixel_copy_increment, width)
   445 
   446 /* Special version of Duff's device for even more optimization */
   447 #define DUFFS_LOOP_124(pixel_copy_increment1,				\
   448                        pixel_copy_increment2,				\
   449                        pixel_copy_increment4, width)			\
   450 { int n = width;							\
   451 	if (n & 1) {							\
   452 		pixel_copy_increment1; n -= 1;				\
   453 	}								\
   454 	if (n & 2) {							\
   455 		pixel_copy_increment2; n -= 2;				\
   456 	}								\
   457 	if (n) {							\
   458 		n = (n+7)/ 8;						\
   459 		switch (n & 4) {					\
   460 		case 0: do {	pixel_copy_increment4;			\
   461 		case 4:		pixel_copy_increment4;			\
   462 			} while (--n > 0);				\
   463 		}							\
   464 	}								\
   465 }
   466 
   467 #else
   468 
   469 /* Don't use Duff's device to unroll loops */
   470 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   471 { int n;								\
   472 	for ( n=width; n > 0; --n ) {					\
   473 		pixel_copy_increment;					\
   474 	}								\
   475 }
   476 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   477 	DUFFS_LOOP(pixel_copy_increment, width)
   478 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   479 	DUFFS_LOOP(pixel_copy_increment, width)
   480 #define DUFFS_LOOP_124(pixel_copy_increment1,				\
   481                        pixel_copy_increment2,				\
   482                        pixel_copy_increment4, width)			\
   483 	DUFFS_LOOP(pixel_copy_increment1, width)
   484 
   485 #endif /* USE_DUFFS_LOOP */
   486 
   487 /* Prevent Visual C++ 6.0 from printing out stupid warnings */
   488 #if defined(_MSC_VER) && (_MSC_VER >= 600)
   489 #pragma warning(disable: 4550)
   490 #endif
   491 
   492 #endif /* _SDL_blit_h */
   493 
   494 /* vi: set ts=4 sw=4 expandtab: */