src/video/SDL_blit.h
author Sam Lantinga
Sun, 06 Mar 2011 21:17:09 -0800
changeset 5426 7a3982c50af8
parent 5423 b69fa50e80d7
child 5439 3a778c6c0269
permissions -rw-r--r--
Fixed compiler warnings
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifndef _SDL_blit_h
    25 #define _SDL_blit_h
    26 
    27 #include "SDL_cpuinfo.h"
    28 #include "SDL_endian.h"
    29 #include "SDL_surface.h"
    30 
    31 /* SDL blit copy flags */
    32 #define SDL_COPY_MODULATE_COLOR     0x00000001
    33 #define SDL_COPY_MODULATE_ALPHA     0x00000002
    34 #define SDL_COPY_BLEND              0x00000010
    35 #define SDL_COPY_ADD                0x00000020
    36 #define SDL_COPY_MOD                0x00000040
    37 #define SDL_COPY_COLORKEY           0x00000100
    38 #define SDL_COPY_NEAREST            0x00000200
    39 #define SDL_COPY_RLE_DESIRED        0x00001000
    40 #define SDL_COPY_RLE_COLORKEY       0x00002000
    41 #define SDL_COPY_RLE_ALPHAKEY       0x00004000
    42 #define SDL_COPY_RLE_MASK           (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY)
    43 
    44 /* SDL blit CPU flags */
    45 #define SDL_CPU_ANY                 0x00000000
    46 #define SDL_CPU_MMX                 0x00000001
    47 #define SDL_CPU_3DNOW               0x00000002
    48 #define SDL_CPU_SSE                 0x00000004
    49 #define SDL_CPU_SSE2                0x00000008
    50 #define SDL_CPU_ALTIVEC_PREFETCH    0x00000010
    51 #define SDL_CPU_ALTIVEC_NOPREFETCH  0x00000020
    52 
    53 typedef struct
    54 {
    55     Uint8 *src;
    56     int src_w, src_h;
    57     int src_pitch;
    58     int src_skip;
    59     Uint8 *dst;
    60     int dst_w, dst_h;
    61     int dst_pitch;
    62     int dst_skip;
    63     SDL_PixelFormat *src_fmt;
    64     SDL_PixelFormat *dst_fmt;
    65     Uint8 *table;
    66     int flags;
    67     Uint32 colorkey;
    68     Uint8 r, g, b, a;
    69 } SDL_BlitInfo;
    70 
    71 typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
    72 
    73 typedef struct
    74 {
    75     Uint32 src_format;
    76     Uint32 dst_format;
    77     int flags;
    78     int cpu;
    79     SDL_BlitFunc func;
    80 } SDL_BlitFuncEntry;
    81 
    82 /* Blit mapping definition */
    83 typedef struct SDL_BlitMap
    84 {
    85     SDL_Surface *dst;
    86     int identity;
    87     SDL_blit blit;
    88     void *data;
    89     SDL_BlitInfo info;
    90 
    91     /* the version count matches the destination; mismatch indicates
    92        an invalid mapping */
    93     Uint32 palette_version;
    94 } SDL_BlitMap;
    95 
    96 /* Functions found in SDL_blit.c */
    97 extern int SDL_CalculateBlit(SDL_Surface * surface);
    98 
    99 /* Functions found in SDL_blit_*.c */
   100 extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
   101 extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
   102 extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
   103 extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
   104 
   105 /*
   106  * Useful macros for blitting routines
   107  */
   108 
   109 #if defined(__GNUC__)
   110 #define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
   111 #elif defined(_MSC_VER)
   112 #define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
   113 #else
   114 #define DECLARE_ALIGNED(t,v,a)  t v
   115 #endif
   116 
   117 /* The Nintendo surfaces are special. Bit 15 is the transparency
   118  * bit. It must be set for the pixel to be displayed. By setting that
   119  * value to 0 for other platforms, their compiler should optimize it
   120  * out. */
   121 #ifdef __NDS__
   122 #define NDS_BIT15 0x8000
   123 #else
   124 #define NDS_BIT15 0
   125 #endif
   126 
   127 /* Load pixel of the specified format from a buffer and get its R-G-B values */
   128 /* FIXME: rescale values to 0..255 here? */
   129 #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
   130 {									\
   131 	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
   132 	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
   133 	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
   134 }
   135 #define RGB_FROM_RGB565(Pixel, r, g, b)					\
   136 {									\
   137 	r = (((Pixel&0xF800)>>11)<<3);		 			\
   138 	g = (((Pixel&0x07E0)>>5)<<2); 					\
   139 	b = ((Pixel&0x001F)<<3); 					\
   140 }
   141 #define RGB_FROM_RGB555(Pixel, r, g, b)					\
   142 {									\
   143 	r = (((Pixel&0x7C00)>>10)<<3);		 			\
   144 	g = (((Pixel&0x03E0)>>5)<<3); 					\
   145 	b = ((Pixel&0x001F)<<3); 					\
   146 }
   147 #define RGB_FROM_RGB888(Pixel, r, g, b)					\
   148 {									\
   149 	r = ((Pixel&0xFF0000)>>16);		 			\
   150 	g = ((Pixel&0xFF00)>>8);		 			\
   151 	b = (Pixel&0xFF);			 			\
   152 }
   153 #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
   154 do {									   \
   155 	switch (bpp) {							   \
   156 		case 2:							   \
   157 			Pixel = *((Uint16 *)(buf));			   \
   158 		break;							   \
   159 									   \
   160 		case 3: {						   \
   161 		        Uint8 *B = (Uint8 *)(buf);			   \
   162 			if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   163 			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   164 			} else {					   \
   165 			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   166 			}						   \
   167 		}							   \
   168 		break;							   \
   169 									   \
   170 		case 4:							   \
   171 			Pixel = *((Uint32 *)(buf));			   \
   172 		break;							   \
   173 									   \
   174 		default:						   \
   175 		        Pixel = 0; /* stop gcc complaints */		   \
   176 		break;							   \
   177 	}								   \
   178 } while (0)
   179 
   180 #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
   181 do {									   \
   182 	switch (bpp) {							   \
   183 		case 2:							   \
   184 			Pixel = *((Uint16 *)(buf));			   \
   185 			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
   186 		break;							   \
   187 									   \
   188 		case 3:	{						   \
   189             Pixel = 0;                  \
   190                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   191 			        r = *((buf)+fmt->Rshift/8);		   \
   192 				g = *((buf)+fmt->Gshift/8);		   \
   193 				b = *((buf)+fmt->Bshift/8);		   \
   194 			} else {					   \
   195 			        r = *((buf)+2-fmt->Rshift/8);		   \
   196 				g = *((buf)+2-fmt->Gshift/8);		   \
   197 				b = *((buf)+2-fmt->Bshift/8);		   \
   198 			}						   \
   199 		}							   \
   200 		break;							   \
   201 									   \
   202 		case 4:							   \
   203 			Pixel = *((Uint32 *)(buf));			   \
   204 			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
   205 		break;							   \
   206 									   \
   207 		default:						   \
   208                 /* stop gcc complaints */		   \
   209 		        Pixel = 0;              \
   210                 r = g = b = 0;          \
   211 		break;							   \
   212 	}								   \
   213 } while (0)
   214 
   215 /* Assemble R-G-B values into a specified pixel format and store them */
   216 #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
   217 {									\
   218 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   219 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   220 		((b>>fmt->Bloss)<<fmt->Bshift);				\
   221 }
   222 #define RGB565_FROM_RGB(Pixel, r, g, b)					\
   223 {									\
   224 	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
   225 }
   226 #define RGB555_FROM_RGB(Pixel, r, g, b)					\
   227 {									\
   228 	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
   229 }
   230 #define RGB888_FROM_RGB(Pixel, r, g, b)					\
   231 {									\
   232 	Pixel = (r<<16)|(g<<8)|b;					\
   233 }
   234 #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)				\
   235 {									\
   236 	Pixel = (a<<24)|(r<<16)|(g<<8)|b;				\
   237 }
   238 #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)				\
   239 {									\
   240 	Pixel = (r<<24)|(g<<16)|(b<<8)|a;				\
   241 }
   242 #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)				\
   243 {									\
   244 	Pixel = (a<<24)|(b<<16)|(g<<8)|r;				\
   245 }
   246 #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)				\
   247 {									\
   248 	Pixel = (b<<24)|(g<<16)|(r<<8)|a;				\
   249 }
   250 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
   251 {									\
   252 	switch (bpp) {							\
   253 		case 2: {						\
   254 			Uint16 Pixel;					\
   255 									\
   256 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   257 			*((Uint16 *)(buf)) = Pixel | NDS_BIT15;		\
   258 		}							\
   259 		break;							\
   260 									\
   261 		case 3: {						\
   262                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   263 			        *((buf)+fmt->Rshift/8) = r;		\
   264 				*((buf)+fmt->Gshift/8) = g;		\
   265 				*((buf)+fmt->Bshift/8) = b;		\
   266 			} else {					\
   267 			        *((buf)+2-fmt->Rshift/8) = r;		\
   268 				*((buf)+2-fmt->Gshift/8) = g;		\
   269 				*((buf)+2-fmt->Bshift/8) = b;		\
   270 			}						\
   271 		}							\
   272 		break;							\
   273 									\
   274 		case 4: {						\
   275 			Uint32 Pixel;					\
   276 									\
   277 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   278 			*((Uint32 *)(buf)) = Pixel;			\
   279 		}							\
   280 		break;							\
   281 	}								\
   282 }
   283 
   284 /* FIXME: Should we rescale alpha into 0..255 here? */
   285 #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
   286 {									\
   287 	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
   288 	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
   289 	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
   290 	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
   291 }
   292 #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
   293 {						\
   294 	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
   295 	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
   296 	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
   297 	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
   298 }
   299 #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
   300 {									\
   301 	r = (Pixel>>24);						\
   302 	g = ((Pixel>>16)&0xFF);						\
   303 	b = ((Pixel>>8)&0xFF);						\
   304 	a = (Pixel&0xFF);						\
   305 }
   306 #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
   307 {									\
   308 	r = ((Pixel>>16)&0xFF);						\
   309 	g = ((Pixel>>8)&0xFF);						\
   310 	b = (Pixel&0xFF);						\
   311 	a = (Pixel>>24);						\
   312 }
   313 #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
   314 {									\
   315 	r = (Pixel&0xFF);						\
   316 	g = ((Pixel>>8)&0xFF);						\
   317 	b = ((Pixel>>16)&0xFF);						\
   318 	a = (Pixel>>24);						\
   319 }
   320 #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)				\
   321 {									\
   322 	r = ((Pixel>>8)&0xFF);						\
   323 	g = ((Pixel>>16)&0xFF);						\
   324 	b = (Pixel>>24);						\
   325 	a = (Pixel&0xFF);						\
   326 }
   327 #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
   328 do {									   \
   329 	switch (bpp) {							   \
   330 		case 2:							   \
   331 			Pixel = *((Uint16 *)(buf));			   \
   332 			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
   333 		break;							   \
   334 									   \
   335 		case 3:	{						   \
   336             Pixel = 0; \
   337                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   338 			        r = *((buf)+fmt->Rshift/8);		   \
   339 				g = *((buf)+fmt->Gshift/8);		   \
   340 				b = *((buf)+fmt->Bshift/8);		   \
   341 			} else {					   \
   342 			        r = *((buf)+2-fmt->Rshift/8);		   \
   343 				g = *((buf)+2-fmt->Gshift/8);		   \
   344 				b = *((buf)+2-fmt->Bshift/8);		   \
   345 			}						   \
   346 			a = 0xFF;					   \
   347 		}							   \
   348 		break;							   \
   349 									   \
   350 		case 4:							   \
   351 			Pixel = *((Uint32 *)(buf));			   \
   352 			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
   353 		break;							   \
   354 									   \
   355 		default:						   \
   356                 /* stop gcc complaints */		   \
   357 		        Pixel = 0;              \
   358                 r = g = b = a = 0;      \
   359 		break;							   \
   360 	}								   \
   361 } while (0)
   362 
   363 /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
   364 #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
   365 {									\
   366 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   367 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   368 		((b>>fmt->Bloss)<<fmt->Bshift)|				\
   369 		((a>>fmt->Aloss)<<fmt->Ashift);				\
   370 }
   371 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
   372 {									\
   373 	switch (bpp) {							\
   374 		case 2: {						\
   375 			Uint16 Pixel;					\
   376 									\
   377 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   378 			*((Uint16 *)(buf)) = Pixel | NDS_BIT15;		\
   379 		}							\
   380 		break;							\
   381 									\
   382 		case 3: {						\
   383                         if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   384 			        *((buf)+fmt->Rshift/8) = r;		\
   385 				*((buf)+fmt->Gshift/8) = g;		\
   386 				*((buf)+fmt->Bshift/8) = b;		\
   387 			} else {					\
   388 			        *((buf)+2-fmt->Rshift/8) = r;		\
   389 				*((buf)+2-fmt->Gshift/8) = g;		\
   390 				*((buf)+2-fmt->Bshift/8) = b;		\
   391 			}						\
   392 		}							\
   393 		break;							\
   394 									\
   395 		case 4: {						\
   396 			Uint32 Pixel;					\
   397 									\
   398 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   399 			*((Uint32 *)(buf)) = Pixel;			\
   400 		}							\
   401 		break;							\
   402 	}								\
   403 }
   404 
   405 /* Blend the RGB values of two Pixels based on a source alpha value */
   406 #define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
   407 do {						\
   408 	dR = ((((int)(sR-dR)*(int)A)/255)+dR);	\
   409 	dG = ((((int)(sG-dG)*(int)A)/255)+dG);	\
   410 	dB = ((((int)(sB-dB)*(int)A)/255)+dB);	\
   411 } while(0)
   412 
   413 
   414 /* This is a very useful loop for optimizing blitters */
   415 #if defined(_MSC_VER) && (_MSC_VER == 1300)
   416 /* There's a bug in the Visual C++ 7 optimizer when compiling this code */
   417 #else
   418 #define USE_DUFFS_LOOP
   419 #endif
   420 #ifdef USE_DUFFS_LOOP
   421 
   422 /* 8-times unrolled loop */
   423 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   424 { int n = (width+7)/8;							\
   425 	switch (width & 7) {						\
   426 	case 0: do {	pixel_copy_increment;				\
   427 	case 7:		pixel_copy_increment;				\
   428 	case 6:		pixel_copy_increment;				\
   429 	case 5:		pixel_copy_increment;				\
   430 	case 4:		pixel_copy_increment;				\
   431 	case 3:		pixel_copy_increment;				\
   432 	case 2:		pixel_copy_increment;				\
   433 	case 1:		pixel_copy_increment;				\
   434 		} while ( --n > 0 );					\
   435 	}								\
   436 }
   437 
   438 /* 4-times unrolled loop */
   439 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   440 { int n = (width+3)/4;							\
   441 	switch (width & 3) {						\
   442 	case 0: do {	pixel_copy_increment;				\
   443 	case 3:		pixel_copy_increment;				\
   444 	case 2:		pixel_copy_increment;				\
   445 	case 1:		pixel_copy_increment;				\
   446 		} while (--n > 0);					\
   447 	}								\
   448 }
   449 
   450 /* Use the 8-times version of the loop by default */
   451 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   452 	DUFFS_LOOP8(pixel_copy_increment, width)
   453 
   454 /* Special version of Duff's device for even more optimization */
   455 #define DUFFS_LOOP_124(pixel_copy_increment1,				\
   456                        pixel_copy_increment2,				\
   457                        pixel_copy_increment4, width)			\
   458 { int n = width;							\
   459 	if (n & 1) {							\
   460 		pixel_copy_increment1; n -= 1;				\
   461 	}								\
   462 	if (n & 2) {							\
   463 		pixel_copy_increment2; n -= 2;				\
   464 	}								\
   465 	if (n) {							\
   466 		n = (n+7)/ 8;						\
   467 		switch (n & 4) {					\
   468 		case 0: do {	pixel_copy_increment4;			\
   469 		case 4:		pixel_copy_increment4;			\
   470 			} while (--n > 0);				\
   471 		}							\
   472 	}								\
   473 }
   474 
   475 #else
   476 
   477 /* Don't use Duff's device to unroll loops */
   478 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   479 { int n;								\
   480 	for ( n=width; n > 0; --n ) {					\
   481 		pixel_copy_increment;					\
   482 	}								\
   483 }
   484 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   485 	DUFFS_LOOP(pixel_copy_increment, width)
   486 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   487 	DUFFS_LOOP(pixel_copy_increment, width)
   488 #define DUFFS_LOOP_124(pixel_copy_increment1,				\
   489                        pixel_copy_increment2,				\
   490                        pixel_copy_increment4, width)			\
   491 	DUFFS_LOOP(pixel_copy_increment1, width)
   492 
   493 #endif /* USE_DUFFS_LOOP */
   494 
   495 /* Prevent Visual C++ 6.0 from printing out stupid warnings */
   496 #if defined(_MSC_VER) && (_MSC_VER >= 600)
   497 #pragma warning(disable: 4550)
   498 #endif
   499 
   500 #endif /* _SDL_blit_h */
   501 
   502 /* vi: set ts=4 sw=4 expandtab: */