src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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