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