src/video/SDL_blit.h
author Sam Lantinga
Fri, 09 Jun 2006 07:06:12 +0000
branchSDL-1.3
changeset 1672 8e754b82cecc
parent 1668 4da1ee79c9af
permissions -rw-r--r--
Updated SDL_Surface code for software-only access, fixed some build errors
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_endian.h"
    28 
    29 /* The structure passed to the low level blit functions */
    30 typedef struct
    31 {
    32     Uint8 *s_pixels;
    33     int s_width;
    34     int s_height;
    35     int s_skip;
    36     Uint8 *d_pixels;
    37     int d_width;
    38     int d_height;
    39     int d_skip;
    40     void *aux_data;
    41     SDL_PixelFormat *src;
    42     Uint8 *table;
    43     SDL_PixelFormat *dst;
    44 } SDL_BlitInfo;
    45 
    46 /* The type definition for the low level blit functions */
    47 typedef void (*SDL_loblit) (SDL_BlitInfo * info);
    48 
    49 /* This is the private info structure for software accelerated blits */
    50 struct private_swaccel
    51 {
    52     SDL_loblit blit;
    53     void *aux_data;
    54 };
    55 
    56 /* Blit mapping definition */
    57 typedef struct SDL_BlitMap
    58 {
    59     SDL_Surface *dst;
    60     int identity;
    61     Uint8 *table;
    62     SDL_blit sw_blit;
    63     struct private_swaccel *sw_data;
    64 
    65     /* the version count matches the destination; mismatch indicates
    66        an invalid mapping */
    67     unsigned int format_version;
    68 } SDL_BlitMap;
    69 
    70 
    71 /* Functions found in SDL_blit.c */
    72 extern int SDL_CalculateBlit(SDL_Surface * surface);
    73 
    74 /* Functions found in SDL_blit_{0,1,N,A}.c */
    75 extern SDL_loblit SDL_CalculateBlit0(SDL_Surface * surface, int complex);
    76 extern SDL_loblit SDL_CalculateBlit1(SDL_Surface * surface, int complex);
    77 extern SDL_loblit SDL_CalculateBlitN(SDL_Surface * surface, int complex);
    78 extern SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface * surface, int complex);
    79 
    80 /*
    81  * Useful macros for blitting routines
    82  */
    83 
    84 #define FORMAT_EQUAL(A, B)						\
    85     ((A)->BitsPerPixel == (B)->BitsPerPixel				\
    86      && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
    87 
    88 /* Load pixel of the specified format from a buffer and get its R-G-B values */
    89 /* FIXME: rescale values to 0..255 here? */
    90 #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
    91 {									\
    92 	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
    93 	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
    94 	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
    95 }
    96 #define RGB_FROM_RGB565(Pixel, r, g, b)					\
    97 {									\
    98 	r = (((Pixel&0xF800)>>11)<<3);		 			\
    99 	g = (((Pixel&0x07E0)>>5)<<2); 					\
   100 	b = ((Pixel&0x001F)<<3); 					\
   101 }
   102 #define RGB_FROM_RGB555(Pixel, r, g, b)					\
   103 {									\
   104 	r = (((Pixel&0x7C00)>>10)<<3);		 			\
   105 	g = (((Pixel&0x03E0)>>5)<<3); 					\
   106 	b = ((Pixel&0x001F)<<3); 					\
   107 }
   108 #define RGB_FROM_RGB888(Pixel, r, g, b)					\
   109 {									\
   110 	r = ((Pixel&0xFF0000)>>16);		 			\
   111 	g = ((Pixel&0xFF00)>>8);		 			\
   112 	b = (Pixel&0xFF);			 			\
   113 }
   114 #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
   115 do {									   \
   116 	switch (bpp) {							   \
   117 		case 2:							   \
   118 			Pixel = *((Uint16 *)(buf));			   \
   119 		break;							   \
   120 									   \
   121 		case 3: {						   \
   122 		        Uint8 *B = (Uint8 *)(buf);			   \
   123 			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   124 			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   125 			} else {					   \
   126 			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   127 			}						   \
   128 		}							   \
   129 		break;							   \
   130 									   \
   131 		case 4:							   \
   132 			Pixel = *((Uint32 *)(buf));			   \
   133 		break;							   \
   134 									   \
   135 		default:						   \
   136 			Pixel = 0; /* appease gcc */			   \
   137 		break;							   \
   138 	}								   \
   139 } while(0)
   140 
   141 #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
   142 do {									   \
   143 	switch (bpp) {							   \
   144 		case 2:							   \
   145 			Pixel = *((Uint16 *)(buf));			   \
   146 		break;							   \
   147 									   \
   148 		case 3: {						   \
   149 		        Uint8 *B = (Uint8 *)buf;			   \
   150 			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   151 			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   152 			} else {					   \
   153 			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   154 			}						   \
   155 		}							   \
   156 		break;							   \
   157 									   \
   158 		case 4:							   \
   159 			Pixel = *((Uint32 *)(buf));			   \
   160 		break;							   \
   161 									   \
   162 	        default:						   \
   163 		        Pixel = 0;	/* prevent gcc from complaining */ \
   164 		break;							   \
   165 	}								   \
   166 	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
   167 } while(0)
   168 
   169 /* Assemble R-G-B values into a specified pixel format and store them */
   170 #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
   171 {									\
   172 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   173 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   174 		((b>>fmt->Bloss)<<fmt->Bshift);				\
   175 }
   176 #define RGB565_FROM_RGB(Pixel, r, g, b)					\
   177 {									\
   178 	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
   179 }
   180 #define RGB555_FROM_RGB(Pixel, r, g, b)					\
   181 {									\
   182 	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
   183 }
   184 #define RGB888_FROM_RGB(Pixel, r, g, b)					\
   185 {									\
   186 	Pixel = (r<<16)|(g<<8)|b;					\
   187 }
   188 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
   189 {									\
   190 	switch (bpp) {							\
   191 		case 2: {						\
   192 			Uint16 Pixel;					\
   193 									\
   194 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   195 			*((Uint16 *)(buf)) = Pixel;			\
   196 		}							\
   197 		break;							\
   198 									\
   199 		case 3: {						\
   200                         if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   201 			        *((buf)+fmt->Rshift/8) = r;		\
   202 				*((buf)+fmt->Gshift/8) = g;		\
   203 				*((buf)+fmt->Bshift/8) = b;		\
   204 			} else {					\
   205 			        *((buf)+2-fmt->Rshift/8) = r;		\
   206 				*((buf)+2-fmt->Gshift/8) = g;		\
   207 				*((buf)+2-fmt->Bshift/8) = b;		\
   208 			}						\
   209 		}							\
   210 		break;							\
   211 									\
   212 		case 4: {						\
   213 			Uint32 Pixel;					\
   214 									\
   215 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   216 			*((Uint32 *)(buf)) = Pixel;			\
   217 		}							\
   218 		break;							\
   219 	}								\
   220 }
   221 #define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
   222 {									\
   223 	switch (bpp) {							\
   224 		case 2: {						\
   225 			Uint16 *bufp;					\
   226 			Uint16 Pixel;					\
   227 									\
   228 			bufp = (Uint16 *)buf;				\
   229 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   230 			*bufp = Pixel | (*bufp & Amask);		\
   231 		}							\
   232 		break;							\
   233 									\
   234 		case 3: {						\
   235                         if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   236 			        *((buf)+fmt->Rshift/8) = r;		\
   237 				*((buf)+fmt->Gshift/8) = g;		\
   238 				*((buf)+fmt->Bshift/8) = b;		\
   239 			} else {					\
   240 			        *((buf)+2-fmt->Rshift/8) = r;		\
   241 				*((buf)+2-fmt->Gshift/8) = g;		\
   242 				*((buf)+2-fmt->Bshift/8) = b;		\
   243 			}						\
   244 		}							\
   245 		break;							\
   246 									\
   247 		case 4: {						\
   248 			Uint32 *bufp;					\
   249 			Uint32 Pixel;					\
   250 									\
   251 			bufp = (Uint32 *)buf;				\
   252 			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
   253 			*bufp = Pixel | (*bufp & Amask);		\
   254 		}							\
   255 		break;							\
   256 	}								\
   257 }
   258 
   259 /* FIXME: Should we rescale alpha into 0..255 here? */
   260 #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
   261 {									\
   262 	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
   263 	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
   264 	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
   265 	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
   266 }
   267 #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
   268 {						\
   269 	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
   270 	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
   271 	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
   272 	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
   273 }
   274 #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
   275 {									\
   276 	r = (Pixel>>24);						\
   277 	g = ((Pixel>>16)&0xFF);						\
   278 	b = ((Pixel>>8)&0xFF);						\
   279 	a = (Pixel&0xFF);						\
   280 }
   281 #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
   282 {									\
   283 	r = ((Pixel>>16)&0xFF);						\
   284 	g = ((Pixel>>8)&0xFF);						\
   285 	b = (Pixel&0xFF);						\
   286 	a = (Pixel>>24);						\
   287 }
   288 #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
   289 {									\
   290 	r = (Pixel&0xFF);						\
   291 	g = ((Pixel>>8)&0xFF);						\
   292 	b = ((Pixel>>16)&0xFF);						\
   293 	a = (Pixel>>24);						\
   294 }
   295 #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
   296 do {									   \
   297 	switch (bpp) {							   \
   298 		case 2:							   \
   299 			Pixel = *((Uint16 *)(buf));			   \
   300 		break;							   \
   301 									   \
   302 		case 3:	{/* FIXME: broken code (no alpha) */		   \
   303 		        Uint8 *b = (Uint8 *)buf;			   \
   304 			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   305 			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
   306 			} else {					   \
   307 			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
   308 			}						   \
   309 		}							   \
   310 		break;							   \
   311 									   \
   312 		case 4:							   \
   313 			Pixel = *((Uint32 *)(buf));			   \
   314 		break;							   \
   315 									   \
   316 		default:						   \
   317 		        Pixel = 0; /* stop gcc complaints */		   \
   318 		break;							   \
   319 	}								   \
   320 	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
   321 	Pixel &= ~fmt->Amask;						   \
   322 } while(0)
   323 
   324 /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
   325 #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
   326 {									\
   327 	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   328 		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   329 		((b>>fmt->Bloss)<<fmt->Bshift)|				\
   330 		((a>>fmt->Aloss)<<fmt->Ashift);				\
   331 }
   332 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
   333 {									\
   334 	switch (bpp) {							\
   335 		case 2: {						\
   336 			Uint16 Pixel;					\
   337 									\
   338 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   339 			*((Uint16 *)(buf)) = Pixel;			\
   340 		}							\
   341 		break;							\
   342 									\
   343 		case 3: { /* FIXME: broken code (no alpha) */		\
   344                         if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   345 			        *((buf)+fmt->Rshift/8) = r;		\
   346 				*((buf)+fmt->Gshift/8) = g;		\
   347 				*((buf)+fmt->Bshift/8) = b;		\
   348 			} else {					\
   349 			        *((buf)+2-fmt->Rshift/8) = r;		\
   350 				*((buf)+2-fmt->Gshift/8) = g;		\
   351 				*((buf)+2-fmt->Bshift/8) = b;		\
   352 			}						\
   353 		}							\
   354 		break;							\
   355 									\
   356 		case 4: {						\
   357 			Uint32 Pixel;					\
   358 									\
   359 			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
   360 			*((Uint32 *)(buf)) = Pixel;			\
   361 		}							\
   362 		break;							\
   363 	}								\
   364 }
   365 
   366 /* Blend the RGB values of two Pixels based on a source alpha value */
   367 #define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
   368 do {						\
   369 	dR = (((sR-dR)*(A))>>8)+dR;		\
   370 	dG = (((sG-dG)*(A))>>8)+dG;		\
   371 	dB = (((sB-dB)*(A))>>8)+dB;		\
   372 } while(0)
   373 
   374 /* Blend the RGB values of two Pixels based on a source alpha value */
   375 #define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
   376 do {						\
   377     unsigned tR, tG, tB, tA; \
   378     tA = 255 - sA; \
   379     tR = 1 + (sR * sA) + (dR * tA); \
   380     dR = (tR + (tR >> 8)) >> 8; \
   381     tG = 1 + (sG * sA) + (dG * tA); \
   382     dG = (tG + (tG >> 8)) >> 8; \
   383     tB = 1 + (sB * sA) + (dB * tA); \
   384     dB = (tB + (tB >> 8)) >> 8; \
   385 } while(0)
   386 
   387 
   388 /* This is a very useful loop for optimizing blitters */
   389 #if defined(_MSC_VER) && (_MSC_VER == 1300)
   390 /* There's a bug in the Visual C++ 7 optimizer when compiling this code */
   391 #else
   392 #define USE_DUFFS_LOOP
   393 #endif
   394 #ifdef USE_DUFFS_LOOP
   395 
   396 /* 8-times unrolled loop */
   397 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   398 { int n = (width+7)/8;							\
   399 	switch (width & 7) {						\
   400 	case 0: do {	pixel_copy_increment;				\
   401 	case 7:		pixel_copy_increment;				\
   402 	case 6:		pixel_copy_increment;				\
   403 	case 5:		pixel_copy_increment;				\
   404 	case 4:		pixel_copy_increment;				\
   405 	case 3:		pixel_copy_increment;				\
   406 	case 2:		pixel_copy_increment;				\
   407 	case 1:		pixel_copy_increment;				\
   408 		} while ( --n > 0 );					\
   409 	}								\
   410 }
   411 
   412 /* 4-times unrolled loop */
   413 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   414 { int n = (width+3)/4;							\
   415 	switch (width & 3) {						\
   416 	case 0: do {	pixel_copy_increment;				\
   417 	case 3:		pixel_copy_increment;				\
   418 	case 2:		pixel_copy_increment;				\
   419 	case 1:		pixel_copy_increment;				\
   420 		} while ( --n > 0 );					\
   421 	}								\
   422 }
   423 
   424 /* 2 - times unrolled loop */
   425 #define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
   426 				double_pixel_copy_increment, width)	\
   427 { int n, w = width;							\
   428 	if( w & 1 ) {							\
   429 	    pixel_copy_increment;					\
   430 	    w--;							\
   431 	}								\
   432 	if ( w > 0 )	{						\
   433 	    n = ( w + 2) / 4;						\
   434 	    switch( w & 2 ) {						\
   435 	    case 0: do {	double_pixel_copy_increment;		\
   436 	    case 2:		double_pixel_copy_increment;		\
   437 		    } while ( --n > 0 );					\
   438 	    }								\
   439 	}								\
   440 }
   441 
   442 /* 2 - times unrolled loop 4 pixels */
   443 #define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
   444 				double_pixel_copy_increment,		\
   445 				quatro_pixel_copy_increment, width)	\
   446 { int n, w = width;								\
   447         if(w & 1) {							\
   448 	  pixel_copy_increment;						\
   449 	  w--;								\
   450 	}								\
   451 	if(w & 2) {							\
   452 	  double_pixel_copy_increment;					\
   453 	  w -= 2;							\
   454 	}								\
   455 	if ( w > 0 ) {							\
   456 	    n = ( w + 7 ) / 8;						\
   457 	    switch( w & 4 ) {						\
   458 	    case 0: do {	quatro_pixel_copy_increment;		\
   459 	    case 4:		quatro_pixel_copy_increment;		\
   460 		    } while ( --n > 0 );					\
   461 	    }								\
   462 	}								\
   463 }
   464 
   465 /* Use the 8-times version of the loop by default */
   466 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   467 	DUFFS_LOOP8(pixel_copy_increment, width)
   468 
   469 #else
   470 
   471 /* Don't use Duff's device to unroll loops */
   472 #define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
   473 			 double_pixel_copy_increment, width)		\
   474 { int n = width;								\
   475     if( n & 1 ) {							\
   476 	pixel_copy_increment;						\
   477 	n--;								\
   478     }									\
   479     n=n>>1;								\
   480     for(; n > 0; --n) {   						\
   481 	double_pixel_copy_increment;					\
   482     }									\
   483 }
   484 
   485 /* Don't use Duff's device to unroll loops */
   486 #define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
   487 				double_pixel_copy_increment,		\
   488 				quatro_pixel_copy_increment, width)	\
   489 { int n = width;								\
   490         if(n & 1) {							\
   491 	  pixel_copy_increment;						\
   492 	  n--;								\
   493 	}								\
   494 	if(n & 2) {							\
   495 	  double_pixel_copy_increment;					\
   496 	  n -= 2;							\
   497 	}								\
   498 	n=n>>2;								\
   499 	for(; n > 0; --n) {   						\
   500 	  quatro_pixel_copy_increment;					\
   501         }								\
   502 }
   503 
   504 /* Don't use Duff's device to unroll loops */
   505 #define DUFFS_LOOP(pixel_copy_increment, width)				\
   506 { int n;								\
   507 	for ( n=width; n > 0; --n ) {					\
   508 		pixel_copy_increment;					\
   509 	}								\
   510 }
   511 #define DUFFS_LOOP8(pixel_copy_increment, width)			\
   512 	DUFFS_LOOP(pixel_copy_increment, width)
   513 #define DUFFS_LOOP4(pixel_copy_increment, width)			\
   514 	DUFFS_LOOP(pixel_copy_increment, width)
   515 
   516 #endif /* USE_DUFFS_LOOP */
   517 
   518 /* Prevent Visual C++ 6.0 from printing out stupid warnings */
   519 #if defined(_MSC_VER) && (_MSC_VER >= 600)
   520 #pragma warning(disable: 4550)
   521 #endif
   522 
   523 #endif /* _SDL_blit_h */
   524 /* vi: set ts=4 sw=4 expandtab: */