Added slow but complete blit fallback
authorSam Lantinga <slouken@libsdl.org>
Tue, 02 Dec 2008 17:14:04 +0000
changeset 28244dba7aa7ea77
parent 2823 15e1dd1fff78
child 2825 620a91b6d263
Added slow but complete blit fallback
Don't try to RLE encode surfaces that have alpha channel and alpha modulation
Don't turn on blending when converting an RGB surface to RGBA format
Do turn on blending when converting colorkey to alpha channel
src/video/SDL_RLEaccel.c
src/video/SDL_blit.c
src/video/SDL_blit.h
src/video/SDL_blit_slow.c
src/video/SDL_blit_slow.h
src/video/SDL_surface.c
     1.1 --- a/src/video/SDL_RLEaccel.c	Tue Dec 02 17:10:05 2008 +0000
     1.2 +++ b/src/video/SDL_RLEaccel.c	Tue Dec 02 17:14:04 2008 +0000
     1.3 @@ -1819,6 +1819,7 @@
     1.4  
     1.5      /* Pass on combinations not supported */
     1.6      if ((flags & SDL_COPY_MODULATE_COLOR) ||
     1.7 +        ((flags & SDL_COPY_MODULATE_ALPHA) && surface->format->Amask) ||
     1.8          (flags & (SDL_COPY_ADD | SDL_COPY_MOD)) ||
     1.9          (flags & SDL_COPY_NEAREST)) {
    1.10          return -1;
     2.1 --- a/src/video/SDL_blit.c	Tue Dec 02 17:10:05 2008 +0000
     2.2 +++ b/src/video/SDL_blit.c	Tue Dec 02 17:14:04 2008 +0000
     2.3 @@ -26,6 +26,7 @@
     2.4  #include "SDL_blit.h"
     2.5  #include "SDL_blit_auto.h"
     2.6  #include "SDL_blit_copy.h"
     2.7 +#include "SDL_blit_slow.h"
     2.8  #include "SDL_RLEaccel_c.h"
     2.9  #include "SDL_pixels_c.h"
    2.10  
    2.11 @@ -269,6 +270,15 @@
    2.12              SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
    2.13                                 SDL_GeneratedBlitFuncTable);
    2.14      }
    2.15 +#ifndef TEST_SLOW_BLIT
    2.16 +    if (blit == NULL)
    2.17 +#endif
    2.18 +    {
    2.19 +        if (surface->format->BytesPerPixel > 1
    2.20 +            && dst->format->BytesPerPixel > 1) {
    2.21 +            blit = SDL_Blit_Slow;
    2.22 +        }
    2.23 +    }
    2.24      map->data = blit;
    2.25  
    2.26      /* Make sure we have a blit function */
     3.1 --- a/src/video/SDL_blit.h	Tue Dec 02 17:10:05 2008 +0000
     3.2 +++ b/src/video/SDL_blit.h	Tue Dec 02 17:14:04 2008 +0000
     3.3 @@ -165,7 +165,7 @@
     3.4  									   \
     3.5  		case 3: {						   \
     3.6  		        Uint8 *B = (Uint8 *)(buf);			   \
     3.7 -			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
     3.8 +			if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
     3.9  			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
    3.10  			} else {					   \
    3.11  			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
    3.12 @@ -178,38 +178,42 @@
    3.13  		break;							   \
    3.14  									   \
    3.15  		default:						   \
    3.16 -			Pixel = 0; /* appease gcc */			   \
    3.17 +		        Pixel; /* stop gcc complaints */		   \
    3.18  		break;							   \
    3.19  	}								   \
    3.20 -} while(0)
    3.21 +} while (0)
    3.22  
    3.23  #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
    3.24  do {									   \
    3.25  	switch (bpp) {							   \
    3.26  		case 2:							   \
    3.27  			Pixel = *((Uint16 *)(buf));			   \
    3.28 +			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
    3.29  		break;							   \
    3.30  									   \
    3.31 -		case 3: {						   \
    3.32 -		        Uint8 *B = (Uint8 *)buf;			   \
    3.33 -			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
    3.34 -			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
    3.35 +		case 3:	{						   \
    3.36 +                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
    3.37 +			        r = *((buf)+fmt->Rshift/8);		   \
    3.38 +				g = *((buf)+fmt->Gshift/8);		   \
    3.39 +				b = *((buf)+fmt->Bshift/8);		   \
    3.40  			} else {					   \
    3.41 -			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
    3.42 +			        r = *((buf)+2-fmt->Rshift/8);		   \
    3.43 +				g = *((buf)+2-fmt->Gshift/8);		   \
    3.44 +				b = *((buf)+2-fmt->Bshift/8);		   \
    3.45  			}						   \
    3.46  		}							   \
    3.47  		break;							   \
    3.48  									   \
    3.49  		case 4:							   \
    3.50  			Pixel = *((Uint32 *)(buf));			   \
    3.51 +			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
    3.52  		break;							   \
    3.53  									   \
    3.54 -	        default:						   \
    3.55 -		        Pixel = 0;	/* prevent gcc from complaining */ \
    3.56 +		default:						   \
    3.57 +		        Pixel; /* stop gcc complaints */		   \
    3.58  		break;							   \
    3.59  	}								   \
    3.60 -	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
    3.61 -} while(0)
    3.62 +} while (0)
    3.63  
    3.64  /* Assemble R-G-B values into a specified pixel format and store them */
    3.65  #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
    3.66 @@ -242,7 +246,7 @@
    3.67  		break;							\
    3.68  									\
    3.69  		case 3: {						\
    3.70 -                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
    3.71 +                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
    3.72  			        *((buf)+fmt->Rshift/8) = r;		\
    3.73  				*((buf)+fmt->Gshift/8) = g;		\
    3.74  				*((buf)+fmt->Bshift/8) = b;		\
    3.75 @@ -277,7 +281,7 @@
    3.76  		break;							\
    3.77  									\
    3.78  		case 3: {						\
    3.79 -                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
    3.80 +                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
    3.81  			        *((buf)+fmt->Rshift/8) = r;		\
    3.82  				*((buf)+fmt->Gshift/8) = g;		\
    3.83  				*((buf)+fmt->Bshift/8) = b;		\
    3.84 @@ -342,29 +346,33 @@
    3.85  	switch (bpp) {							   \
    3.86  		case 2:							   \
    3.87  			Pixel = *((Uint16 *)(buf));			   \
    3.88 +			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
    3.89  		break;							   \
    3.90  									   \
    3.91 -		case 3:	{/* FIXME: broken code (no alpha) */		   \
    3.92 -		        Uint8 *b = (Uint8 *)buf;			   \
    3.93 -			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
    3.94 -			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
    3.95 +		case 3:	{						   \
    3.96 +                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
    3.97 +			        r = *((buf)+fmt->Rshift/8);		   \
    3.98 +				g = *((buf)+fmt->Gshift/8);		   \
    3.99 +				b = *((buf)+fmt->Bshift/8);		   \
   3.100  			} else {					   \
   3.101 -			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
   3.102 +			        r = *((buf)+2-fmt->Rshift/8);		   \
   3.103 +				g = *((buf)+2-fmt->Gshift/8);		   \
   3.104 +				b = *((buf)+2-fmt->Bshift/8);		   \
   3.105  			}						   \
   3.106 +			a = 0xFF;					   \
   3.107  		}							   \
   3.108  		break;							   \
   3.109  									   \
   3.110  		case 4:							   \
   3.111  			Pixel = *((Uint32 *)(buf));			   \
   3.112 +			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
   3.113  		break;							   \
   3.114  									   \
   3.115  		default:						   \
   3.116 -		        Pixel = 0; /* stop gcc complaints */		   \
   3.117 +		        Pixel; /* stop gcc complaints */		   \
   3.118  		break;							   \
   3.119  	}								   \
   3.120 -	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
   3.121 -	Pixel &= ~fmt->Amask;						   \
   3.122 -} while(0)
   3.123 +} while (0)
   3.124  
   3.125  /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
   3.126  #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
   3.127 @@ -385,8 +393,8 @@
   3.128  		}							\
   3.129  		break;							\
   3.130  									\
   3.131 -		case 3: { /* FIXME: broken code (no alpha) */		\
   3.132 -                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   3.133 +		case 3: {						\
   3.134 +                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   3.135  			        *((buf)+fmt->Rshift/8) = r;		\
   3.136  				*((buf)+fmt->Gshift/8) = g;		\
   3.137  				*((buf)+fmt->Bshift/8) = b;		\
     4.1 --- a/src/video/SDL_blit_slow.c	Tue Dec 02 17:10:05 2008 +0000
     4.2 +++ b/src/video/SDL_blit_slow.c	Tue Dec 02 17:14:04 2008 +0000
     4.3 @@ -27,5 +27,144 @@
     4.4  /* The ONE TRUE BLITTER
     4.5   * This puppy has to handle all the unoptimized cases - yes, it's slow.
     4.6   */
     4.7 +void
     4.8 +SDL_Blit_Slow(SDL_BlitInfo * info)
     4.9 +{
    4.10 +    const int flags = info->flags;
    4.11 +    const Uint32 modulateR = info->r;
    4.12 +    const Uint32 modulateG = info->g;
    4.13 +    const Uint32 modulateB = info->b;
    4.14 +    const Uint32 modulateA = info->a;
    4.15 +    Uint32 srcpixel;
    4.16 +    Uint32 srcR, srcG, srcB, srcA;
    4.17 +    Uint32 dstpixel;
    4.18 +    Uint32 dstR, dstG, dstB, dstA;
    4.19 +    int srcy, srcx;
    4.20 +    int posy, posx;
    4.21 +    int incy, incx;
    4.22 +    SDL_PixelFormat *src_fmt = info->src_fmt;
    4.23 +    SDL_PixelFormat *dst_fmt = info->dst_fmt;
    4.24 +    int srcbpp = src_fmt->BytesPerPixel;
    4.25 +    int dstbpp = dst_fmt->BytesPerPixel;
    4.26 +
    4.27 +    srcy = 0;
    4.28 +    posy = 0;
    4.29 +    incy = (info->src_h << 16) / info->dst_h;
    4.30 +    incx = (info->src_w << 16) / info->dst_w;
    4.31 +
    4.32 +    while (info->dst_h--) {
    4.33 +        Uint8 *src;
    4.34 +        Uint8 *dst = (Uint8 *) info->dst;
    4.35 +        int n = info->dst_w;
    4.36 +        srcx = -1;
    4.37 +        posx = 0x10000L;
    4.38 +        while (posy >= 0x10000L) {
    4.39 +            ++srcy;
    4.40 +            posy -= 0x10000L;
    4.41 +        }
    4.42 +        while (n--) {
    4.43 +            if (posx >= 0x10000L) {
    4.44 +                while (posx >= 0x10000L) {
    4.45 +                    ++srcx;
    4.46 +                    posx -= 0x10000L;
    4.47 +                }
    4.48 +                src =
    4.49 +                    (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
    4.50 +            }
    4.51 +            if (src_fmt->Amask) {
    4.52 +                DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
    4.53 +                              srcB, srcA);
    4.54 +            } else {
    4.55 +                DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
    4.56 +                             srcB);
    4.57 +                srcA = 0xFF;
    4.58 +            }
    4.59 +            if (flags & SDL_COPY_COLORKEY) {
    4.60 +                /* srcpixel isn't set for 24 bpp */
    4.61 +                if (srcbpp == 24) {
    4.62 +                    srcpixel = (srcR << src_fmt->Rshift) |
    4.63 +                        (srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
    4.64 +                }
    4.65 +                if (srcpixel == info->colorkey) {
    4.66 +                    posx += incx;
    4.67 +                    dst += dstbpp;
    4.68 +                    continue;
    4.69 +                }
    4.70 +            }
    4.71 +            if (dst_fmt->Amask) {
    4.72 +                DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
    4.73 +                              dstB, dstA);
    4.74 +            } else {
    4.75 +                DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
    4.76 +                             dstB);
    4.77 +                dstA = 0xFF;
    4.78 +            }
    4.79 +
    4.80 +            if (flags & SDL_COPY_MODULATE_COLOR) {
    4.81 +                srcR = (srcR * modulateR) / 255;
    4.82 +                srcG = (srcG * modulateG) / 255;
    4.83 +                srcB = (srcB * modulateB) / 255;
    4.84 +            }
    4.85 +            if (flags & SDL_COPY_MODULATE_ALPHA) {
    4.86 +                srcA = (srcA * modulateA) / 255;
    4.87 +            }
    4.88 +            if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
    4.89 +                /* This goes away if we ever use premultiplied alpha */
    4.90 +                if (srcA < 255) {
    4.91 +                    srcR = (srcR * srcA) / 255;
    4.92 +                    srcG = (srcG * srcA) / 255;
    4.93 +                    srcB = (srcB * srcA) / 255;
    4.94 +                }
    4.95 +            }
    4.96 +            switch (flags &
    4.97 +                    (SDL_COPY_MASK | SDL_COPY_BLEND | SDL_COPY_ADD |
    4.98 +                     SDL_COPY_MOD)) {
    4.99 +            case 0:
   4.100 +                dstR = srcR;
   4.101 +                dstG = srcG;
   4.102 +                dstB = srcB;
   4.103 +                dstA = srcA;
   4.104 +                break;
   4.105 +            case SDL_COPY_MASK:
   4.106 +                if (srcA) {
   4.107 +                    dstR = srcR;
   4.108 +                    dstG = srcG;
   4.109 +                    dstB = srcB;
   4.110 +                }
   4.111 +                break;
   4.112 +            case SDL_COPY_BLEND:
   4.113 +                dstR = srcR + ((255 - srcA) * dstR) / 255;
   4.114 +                dstG = srcG + ((255 - srcA) * dstG) / 255;
   4.115 +                dstB = srcB + ((255 - srcA) * dstB) / 255;
   4.116 +                break;
   4.117 +            case SDL_COPY_ADD:
   4.118 +                dstR = srcR + dstR;
   4.119 +                if (dstR > 255)
   4.120 +                    dstR = 255;
   4.121 +                dstG = srcG + dstG;
   4.122 +                if (dstG > 255)
   4.123 +                    dstG = 255;
   4.124 +                dstB = srcB + dstB;
   4.125 +                if (dstB > 255)
   4.126 +                    dstB = 255;
   4.127 +                break;
   4.128 +            case SDL_COPY_MOD:
   4.129 +                dstR = (srcR * dstR) / 255;
   4.130 +                dstG = (srcG * dstG) / 255;
   4.131 +                dstB = (srcB * dstB) / 255;
   4.132 +                break;
   4.133 +            }
   4.134 +            if (dst_fmt->Amask) {
   4.135 +                ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
   4.136 +            } else {
   4.137 +                ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
   4.138 +            }
   4.139 +            posx += incx;
   4.140 +            dst += dstbpp;
   4.141 +        }
   4.142 +        posy += incy;
   4.143 +        info->dst += info->dst_pitch;
   4.144 +    }
   4.145 +}
   4.146  
   4.147  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/video/SDL_blit_slow.h	Tue Dec 02 17:14:04 2008 +0000
     5.3 @@ -0,0 +1,26 @@
     5.4 +/*
     5.5 +    SDL - Simple DirectMedia Layer
     5.6 +    Copyright (C) 1997-2006 Sam Lantinga
     5.7 +
     5.8 +    This library is free software; you can redistribute it and/or
     5.9 +    modify it under the terms of the GNU Lesser General Public
    5.10 +    License as published by the Free Software Foundation; either
    5.11 +    version 2.1 of the License, or (at your option) any later version.
    5.12 +
    5.13 +    This library is distributed in the hope that it will be useful,
    5.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.16 +    Lesser General Public License for more details.
    5.17 +
    5.18 +    You should have received a copy of the GNU Lesser General Public
    5.19 +    License along with this library; if not, write to the Free Software
    5.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.21 +
    5.22 +    Sam Lantinga
    5.23 +    slouken@libsdl.org
    5.24 +*/
    5.25 +#include "SDL_config.h"
    5.26 +
    5.27 +extern void SDL_Blit_Slow(SDL_BlitInfo * info);
    5.28 +
    5.29 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/SDL_surface.c	Tue Dec 02 17:10:05 2008 +0000
     6.2 +++ b/src/video/SDL_surface.c	Tue Dec 02 17:14:04 2008 +0000
     6.3 @@ -336,6 +336,7 @@
     6.4      SDL_UnlockSurface(surface);
     6.5  
     6.6      SDL_SetColorKey(surface, 0, 0);
     6.7 +    SDL_SetSurfaceBlendMode(surface, SDL_TEXTUREBLENDMODE_BLEND);
     6.8  }
     6.9  
    6.10  int
    6.11 @@ -808,7 +809,16 @@
    6.12      SDL_LowerBlit(surface, &bounds, convert, &bounds);
    6.13  
    6.14      /* Clean up the original surface, and update converted surface */
    6.15 -    SDL_SetClipRect(convert, &surface->clip_rect);
    6.16 +    convert->map->info.r = surface->map->info.r;
    6.17 +    convert->map->info.g = surface->map->info.g;
    6.18 +    convert->map->info.b = surface->map->info.b;
    6.19 +    convert->map->info.a = surface->map->info.a;
    6.20 +    convert->map->info.flags =
    6.21 +        (copy_flags &
    6.22 +         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
    6.23 +           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
    6.24 +           SDL_COPY_RLE_ALPHAKEY));
    6.25 +    surface->map->info.flags = copy_flags;
    6.26      if (copy_flags & SDL_COPY_COLORKEY) {
    6.27          Uint8 keyR, keyG, keyB, keyA;
    6.28  
    6.29 @@ -816,21 +826,20 @@
    6.30                      &keyG, &keyB, &keyA);
    6.31          SDL_SetColorKey(convert, 1,
    6.32                          SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
    6.33 +        /* This is needed when converting for 3D texture upload */
    6.34          SDL_ConvertColorkeyToAlpha(convert);
    6.35      }
    6.36 -    convert->map->info.r = surface->map->info.r;
    6.37 -    convert->map->info.g = surface->map->info.g;
    6.38 -    convert->map->info.b = surface->map->info.b;
    6.39 -    convert->map->info.a = surface->map->info.a;
    6.40 -    convert->map->info.flags = copy_flags;
    6.41 -    surface->map->info.flags = copy_flags;
    6.42 +    SDL_SetClipRect(convert, &surface->clip_rect);
    6.43  
    6.44      /* Enable alpha blending by default if the new surface has an
    6.45       * alpha channel or alpha modulation */
    6.46 -    if (format->Amask || (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
    6.47 +    if ((surface->format->Amask && format->Amask) ||
    6.48 +        (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
    6.49          SDL_SetSurfaceBlendMode(convert, SDL_TEXTUREBLENDMODE_BLEND);
    6.50      }
    6.51 -    SDL_SetSurfaceRLE(convert, (flags & SDL_RLEACCEL));
    6.52 +    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
    6.53 +        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
    6.54 +    }
    6.55  
    6.56      /* We're ready to go! */
    6.57      return (convert);