src/video/SDL_blit.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1658 e49147870aac
child 1668 4da1ee79c9af
     1.1 --- a/src/video/SDL_blit.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/video/SDL_blit.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -37,316 +37,308 @@
     1.4  #endif
     1.5  
     1.6  /* The general purpose software blit routine */
     1.7 -static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
     1.8 -			SDL_Surface *dst, SDL_Rect *dstrect)
     1.9 +static int
    1.10 +SDL_SoftBlit (SDL_Surface * src, SDL_Rect * srcrect,
    1.11 +              SDL_Surface * dst, SDL_Rect * dstrect)
    1.12  {
    1.13 -	int okay;
    1.14 -	int src_locked;
    1.15 -	int dst_locked;
    1.16 +    int okay;
    1.17 +    int src_locked;
    1.18 +    int dst_locked;
    1.19  
    1.20 -	/* Everything is okay at the beginning...  */
    1.21 -	okay = 1;
    1.22 +    /* Everything is okay at the beginning...  */
    1.23 +    okay = 1;
    1.24  
    1.25 -	/* Lock the destination if it's in hardware */
    1.26 -	dst_locked = 0;
    1.27 -	if ( SDL_MUSTLOCK(dst) ) {
    1.28 -		if ( SDL_LockSurface(dst) < 0 ) {
    1.29 -			okay = 0;
    1.30 -		} else {
    1.31 -			dst_locked = 1;
    1.32 -		}
    1.33 -	}
    1.34 -	/* Lock the source if it's in hardware */
    1.35 -	src_locked = 0;
    1.36 -	if ( SDL_MUSTLOCK(src) ) {
    1.37 -		if ( SDL_LockSurface(src) < 0 ) {
    1.38 -			okay = 0;
    1.39 -		} else {
    1.40 -			src_locked = 1;
    1.41 -		}
    1.42 -	}
    1.43 +    /* Lock the destination if it's in hardware */
    1.44 +    dst_locked = 0;
    1.45 +    if (SDL_MUSTLOCK (dst)) {
    1.46 +        if (SDL_LockSurface (dst) < 0) {
    1.47 +            okay = 0;
    1.48 +        } else {
    1.49 +            dst_locked = 1;
    1.50 +        }
    1.51 +    }
    1.52 +    /* Lock the source if it's in hardware */
    1.53 +    src_locked = 0;
    1.54 +    if (SDL_MUSTLOCK (src)) {
    1.55 +        if (SDL_LockSurface (src) < 0) {
    1.56 +            okay = 0;
    1.57 +        } else {
    1.58 +            src_locked = 1;
    1.59 +        }
    1.60 +    }
    1.61  
    1.62 -	/* Set up source and destination buffer pointers, and BLIT! */
    1.63 -	if ( okay  && srcrect->w && srcrect->h ) {
    1.64 -		SDL_BlitInfo info;
    1.65 -		SDL_loblit RunBlit;
    1.66 +    /* Set up source and destination buffer pointers, and BLIT! */
    1.67 +    if (okay && srcrect->w && srcrect->h) {
    1.68 +        SDL_BlitInfo info;
    1.69 +        SDL_loblit RunBlit;
    1.70  
    1.71 -		/* Set up the blit information */
    1.72 -		info.s_pixels = (Uint8 *)src->pixels +
    1.73 -				(Uint16)srcrect->y*src->pitch +
    1.74 -				(Uint16)srcrect->x*src->format->BytesPerPixel;
    1.75 -		info.s_width = srcrect->w;
    1.76 -		info.s_height = srcrect->h;
    1.77 -		info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
    1.78 -		info.d_pixels = (Uint8 *)dst->pixels +
    1.79 -				(Uint16)dstrect->y*dst->pitch +
    1.80 -				(Uint16)dstrect->x*dst->format->BytesPerPixel;
    1.81 -		info.d_width = dstrect->w;
    1.82 -		info.d_height = dstrect->h;
    1.83 -		info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
    1.84 -		info.aux_data = src->map->sw_data->aux_data;
    1.85 -		info.src = src->format;
    1.86 -		info.table = src->map->table;
    1.87 -		info.dst = dst->format;
    1.88 -		RunBlit = src->map->sw_data->blit;
    1.89 +        /* Set up the blit information */
    1.90 +        info.s_pixels = (Uint8 *) src->pixels +
    1.91 +            (Uint16) srcrect->y * src->pitch +
    1.92 +            (Uint16) srcrect->x * src->format->BytesPerPixel;
    1.93 +        info.s_width = srcrect->w;
    1.94 +        info.s_height = srcrect->h;
    1.95 +        info.s_skip = src->pitch - info.s_width * src->format->BytesPerPixel;
    1.96 +        info.d_pixels = (Uint8 *) dst->pixels +
    1.97 +            (Uint16) dstrect->y * dst->pitch +
    1.98 +            (Uint16) dstrect->x * dst->format->BytesPerPixel;
    1.99 +        info.d_width = dstrect->w;
   1.100 +        info.d_height = dstrect->h;
   1.101 +        info.d_skip = dst->pitch - info.d_width * dst->format->BytesPerPixel;
   1.102 +        info.aux_data = src->map->sw_data->aux_data;
   1.103 +        info.src = src->format;
   1.104 +        info.table = src->map->table;
   1.105 +        info.dst = dst->format;
   1.106 +        RunBlit = src->map->sw_data->blit;
   1.107  
   1.108 -		/* Run the actual software blit */
   1.109 -		RunBlit(&info);
   1.110 -	}
   1.111 +        /* Run the actual software blit */
   1.112 +        RunBlit (&info);
   1.113 +    }
   1.114  
   1.115 -	/* We need to unlock the surfaces if they're locked */
   1.116 -	if ( dst_locked ) {
   1.117 -		SDL_UnlockSurface(dst);
   1.118 -	}
   1.119 -	if ( src_locked ) {
   1.120 -		SDL_UnlockSurface(src);
   1.121 -	}
   1.122 -	/* Blit is done! */
   1.123 -	return(okay ? 0 : -1);
   1.124 +    /* We need to unlock the surfaces if they're locked */
   1.125 +    if (dst_locked) {
   1.126 +        SDL_UnlockSurface (dst);
   1.127 +    }
   1.128 +    if (src_locked) {
   1.129 +        SDL_UnlockSurface (src);
   1.130 +    }
   1.131 +    /* Blit is done! */
   1.132 +    return (okay ? 0 : -1);
   1.133  }
   1.134  
   1.135  #ifdef MMX_ASMBLIT
   1.136 -static __inline__ void SDL_memcpyMMX(Uint8 *to, const Uint8 *from, int len)
   1.137 +static __inline__ void
   1.138 +SDL_memcpyMMX (Uint8 * to, const Uint8 * from, int len)
   1.139  {
   1.140 -	int i;
   1.141 +    int i;
   1.142  
   1.143 -	for(i=0; i<len/8; i++) {
   1.144 -		__asm__ __volatile__ (
   1.145 -		"	movq (%0), %%mm0\n"
   1.146 -		"	movq %%mm0, (%1)\n"
   1.147 -		: : "r" (from), "r" (to) : "memory");
   1.148 -		from+=8;
   1.149 -		to+=8;
   1.150 -	}
   1.151 -	if (len&7)
   1.152 -		SDL_memcpy(to, from, len&7);
   1.153 +    for (i = 0; i < len / 8; i++) {
   1.154 +        __asm__ __volatile__ ("	movq (%0), %%mm0\n"
   1.155 +                              "	movq %%mm0, (%1)\n"::"r" (from),
   1.156 +                              "r" (to):"memory");
   1.157 +        from += 8;
   1.158 +        to += 8;
   1.159 +    }
   1.160 +    if (len & 7)
   1.161 +        SDL_memcpy (to, from, len & 7);
   1.162  }
   1.163  
   1.164 -static __inline__ void SDL_memcpySSE(Uint8 *to, const Uint8 *from, int len)
   1.165 +static __inline__ void
   1.166 +SDL_memcpySSE (Uint8 * to, const Uint8 * from, int len)
   1.167  {
   1.168 -	int i;
   1.169 +    int i;
   1.170  
   1.171 -	__asm__ __volatile__ (
   1.172 -	"	prefetchnta (%0)\n"
   1.173 -	"	prefetchnta 64(%0)\n"
   1.174 -	"	prefetchnta 128(%0)\n"
   1.175 -	"	prefetchnta 192(%0)\n"
   1.176 -	: : "r" (from) );
   1.177 +    __asm__ __volatile__ ("	prefetchnta (%0)\n"
   1.178 +                          "	prefetchnta 64(%0)\n"
   1.179 +                          "	prefetchnta 128(%0)\n"
   1.180 +                          "	prefetchnta 192(%0)\n"::"r" (from));
   1.181  
   1.182 -	for(i=0; i<len/8; i++) {
   1.183 -		__asm__ __volatile__ (
   1.184 -		"	prefetchnta 256(%0)\n"
   1.185 -		"	movq (%0), %%mm0\n"
   1.186 -		"	movntq %%mm0, (%1)\n"
   1.187 -		: : "r" (from), "r" (to) : "memory");
   1.188 -		from+=8;
   1.189 -		to+=8;
   1.190 -	}
   1.191 -	if (len&7)
   1.192 -		SDL_memcpy(to, from, len&7);
   1.193 +    for (i = 0; i < len / 8; i++) {
   1.194 +        __asm__ __volatile__ ("	prefetchnta 256(%0)\n"
   1.195 +                              "	movq (%0), %%mm0\n"
   1.196 +                              "	movntq %%mm0, (%1)\n"::"r" (from),
   1.197 +                              "r" (to):"memory");
   1.198 +        from += 8;
   1.199 +        to += 8;
   1.200 +    }
   1.201 +    if (len & 7)
   1.202 +        SDL_memcpy (to, from, len & 7);
   1.203  }
   1.204  #endif
   1.205  
   1.206 -static void SDL_BlitCopy(SDL_BlitInfo *info)
   1.207 +static void
   1.208 +SDL_BlitCopy (SDL_BlitInfo * info)
   1.209  {
   1.210 -	Uint8 *src, *dst;
   1.211 -	int w, h;
   1.212 -	int srcskip, dstskip;
   1.213 +    Uint8 *src, *dst;
   1.214 +    int w, h;
   1.215 +    int srcskip, dstskip;
   1.216  
   1.217 -	w = info->d_width*info->dst->BytesPerPixel;
   1.218 -	h = info->d_height;
   1.219 -	src = info->s_pixels;
   1.220 -	dst = info->d_pixels;
   1.221 -	srcskip = w+info->s_skip;
   1.222 -	dstskip = w+info->d_skip;
   1.223 +    w = info->d_width * info->dst->BytesPerPixel;
   1.224 +    h = info->d_height;
   1.225 +    src = info->s_pixels;
   1.226 +    dst = info->d_pixels;
   1.227 +    srcskip = w + info->s_skip;
   1.228 +    dstskip = w + info->d_skip;
   1.229  #ifdef MMX_ASMBLIT
   1.230 -	if(SDL_HasSSE())
   1.231 -	{
   1.232 -		while ( h-- ) {
   1.233 -			SDL_memcpySSE(dst, src, w);
   1.234 -			src += srcskip;
   1.235 -			dst += dstskip;
   1.236 -		}
   1.237 -		__asm__ __volatile__ (
   1.238 -		"	emms\n"
   1.239 -		::);
   1.240 -	}
   1.241 -	else
   1.242 -	if(SDL_HasMMX())
   1.243 -	{
   1.244 -		while ( h-- ) {
   1.245 -			SDL_memcpyMMX(dst, src, w);
   1.246 -			src += srcskip;
   1.247 -			dst += dstskip;
   1.248 -		}
   1.249 -		__asm__ __volatile__ (
   1.250 -		"	emms\n"
   1.251 -		::);
   1.252 -	}
   1.253 -	else
   1.254 +    if (SDL_HasSSE ()) {
   1.255 +        while (h--) {
   1.256 +            SDL_memcpySSE (dst, src, w);
   1.257 +            src += srcskip;
   1.258 +            dst += dstskip;
   1.259 +        }
   1.260 +        __asm__ __volatile__ ("	emms\n"::);
   1.261 +    } else if (SDL_HasMMX ()) {
   1.262 +        while (h--) {
   1.263 +            SDL_memcpyMMX (dst, src, w);
   1.264 +            src += srcskip;
   1.265 +            dst += dstskip;
   1.266 +        }
   1.267 +        __asm__ __volatile__ ("	emms\n"::);
   1.268 +    } else
   1.269  #endif
   1.270 -	while ( h-- ) {
   1.271 -		SDL_memcpy(dst, src, w);
   1.272 -		src += srcskip;
   1.273 -		dst += dstskip;
   1.274 -	}
   1.275 +        while (h--) {
   1.276 +            SDL_memcpy (dst, src, w);
   1.277 +            src += srcskip;
   1.278 +            dst += dstskip;
   1.279 +        }
   1.280  }
   1.281  
   1.282 -static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
   1.283 +static void
   1.284 +SDL_BlitCopyOverlap (SDL_BlitInfo * info)
   1.285  {
   1.286 -	Uint8 *src, *dst;
   1.287 -	int w, h;
   1.288 -	int srcskip, dstskip;
   1.289 +    Uint8 *src, *dst;
   1.290 +    int w, h;
   1.291 +    int srcskip, dstskip;
   1.292  
   1.293 -	w = info->d_width*info->dst->BytesPerPixel;
   1.294 -	h = info->d_height;
   1.295 -	src = info->s_pixels;
   1.296 -	dst = info->d_pixels;
   1.297 -	srcskip = w+info->s_skip;
   1.298 -	dstskip = w+info->d_skip;
   1.299 -	if ( dst < src ) {
   1.300 -		while ( h-- ) {
   1.301 -			SDL_memcpy(dst, src, w);
   1.302 -			src += srcskip;
   1.303 -			dst += dstskip;
   1.304 -		}
   1.305 -	} else {
   1.306 -		src += ((h-1) * srcskip);
   1.307 -		dst += ((h-1) * dstskip);
   1.308 -		while ( h-- ) {
   1.309 -			SDL_revcpy(dst, src, w);
   1.310 -			src -= srcskip;
   1.311 -			dst -= dstskip;
   1.312 -		}
   1.313 -	}
   1.314 +    w = info->d_width * info->dst->BytesPerPixel;
   1.315 +    h = info->d_height;
   1.316 +    src = info->s_pixels;
   1.317 +    dst = info->d_pixels;
   1.318 +    srcskip = w + info->s_skip;
   1.319 +    dstskip = w + info->d_skip;
   1.320 +    if (dst < src) {
   1.321 +        while (h--) {
   1.322 +            SDL_memcpy (dst, src, w);
   1.323 +            src += srcskip;
   1.324 +            dst += dstskip;
   1.325 +        }
   1.326 +    } else {
   1.327 +        src += ((h - 1) * srcskip);
   1.328 +        dst += ((h - 1) * dstskip);
   1.329 +        while (h--) {
   1.330 +            SDL_revcpy (dst, src, w);
   1.331 +            src -= srcskip;
   1.332 +            dst -= dstskip;
   1.333 +        }
   1.334 +    }
   1.335  }
   1.336  
   1.337  /* Figure out which of many blit routines to set up on a surface */
   1.338 -int SDL_CalculateBlit(SDL_Surface *surface)
   1.339 +int
   1.340 +SDL_CalculateBlit (SDL_Surface * surface)
   1.341  {
   1.342 -	int blit_index;
   1.343 +    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
   1.344 +    const SDL_VideoInfo *info = SDL_GetVideoInfo ();
   1.345 +    int blit_index;
   1.346  
   1.347 -	/* Clean everything out to start */
   1.348 -	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
   1.349 -		SDL_UnRLESurface(surface, 1);
   1.350 -	}
   1.351 -	surface->map->sw_blit = NULL;
   1.352 +    /* Clean everything out to start */
   1.353 +    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   1.354 +        SDL_UnRLESurface (surface, 1);
   1.355 +    }
   1.356 +    surface->map->sw_blit = NULL;
   1.357  
   1.358 -	/* Figure out if an accelerated hardware blit is possible */
   1.359 -	surface->flags &= ~SDL_HWACCEL;
   1.360 -	if ( surface->map->identity ) {
   1.361 -		int hw_blit_ok;
   1.362 +    /* Figure out if an accelerated hardware blit is possible */
   1.363 +    surface->flags &= ~SDL_HWACCEL;
   1.364 +    if (surface->map->identity) {
   1.365 +        int hw_blit_ok;
   1.366  
   1.367 -		if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   1.368 -			/* We only support accelerated blitting to hardware */
   1.369 -			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
   1.370 -				hw_blit_ok = current_video->info.blit_hw;
   1.371 -			} else {
   1.372 -				hw_blit_ok = 0;
   1.373 -			}
   1.374 -			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   1.375 -				hw_blit_ok = current_video->info.blit_hw_CC;
   1.376 -			}
   1.377 -			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
   1.378 -				hw_blit_ok = current_video->info.blit_hw_A;
   1.379 -			}
   1.380 -		} else {
   1.381 -			/* We only support accelerated blitting to hardware */
   1.382 -			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
   1.383 -				hw_blit_ok = current_video->info.blit_sw;
   1.384 -			} else {
   1.385 -				hw_blit_ok = 0;
   1.386 -			}
   1.387 -			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   1.388 -				hw_blit_ok = current_video->info.blit_sw_CC;
   1.389 -			}
   1.390 -			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
   1.391 -				hw_blit_ok = current_video->info.blit_sw_A;
   1.392 -			}
   1.393 -		}
   1.394 -		if ( hw_blit_ok ) {
   1.395 -			SDL_VideoDevice *video = current_video;
   1.396 -			SDL_VideoDevice *this  = current_video;
   1.397 -			video->CheckHWBlit(this, surface, surface->map->dst);
   1.398 -		}
   1.399 -	}
   1.400 -	
   1.401 -	/* if an alpha pixel format is specified, we can accelerate alpha blits */
   1.402 -	if (((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE )&&(current_video->displayformatalphapixel)) 
   1.403 -	{
   1.404 -		if ( (surface->flags & SDL_SRCALPHA) ) 
   1.405 -			if ( current_video->info.blit_hw_A ) {
   1.406 -				SDL_VideoDevice *video = current_video;
   1.407 -				SDL_VideoDevice *this  = current_video;
   1.408 -				video->CheckHWBlit(this, surface, surface->map->dst);
   1.409 -			}
   1.410 -	}
   1.411 -	
   1.412 -	/* Get the blit function index, based on surface mode */
   1.413 -	/* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
   1.414 -	blit_index = 0;
   1.415 -	blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY))      << 0;
   1.416 -	if ( surface->flags & SDL_SRCALPHA
   1.417 -	     && (surface->format->alpha != SDL_ALPHA_OPAQUE
   1.418 -		 || surface->format->Amask) ) {
   1.419 -	        blit_index |= 2;
   1.420 -	}
   1.421 +        if ((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.422 +            /* We only support accelerated blitting to hardware */
   1.423 +            if (surface->map->dst->flags & SDL_HWSURFACE) {
   1.424 +                hw_blit_ok = info->blit_hw;
   1.425 +            } else {
   1.426 +                hw_blit_ok = 0;
   1.427 +            }
   1.428 +            if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   1.429 +                hw_blit_ok = info->blit_hw_CC;
   1.430 +            }
   1.431 +            if (hw_blit_ok && (surface->flags & SDL_SRCALPHA)) {
   1.432 +                hw_blit_ok = info->blit_hw_A;
   1.433 +            }
   1.434 +        } else {
   1.435 +            /* We only support accelerated blitting to hardware */
   1.436 +            if (surface->map->dst->flags & SDL_HWSURFACE) {
   1.437 +                hw_blit_ok = info->blit_sw;
   1.438 +            } else {
   1.439 +                hw_blit_ok = 0;
   1.440 +            }
   1.441 +            if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   1.442 +                hw_blit_ok = info->blit_sw_CC;
   1.443 +            }
   1.444 +            if (hw_blit_ok && (surface->flags & SDL_SRCALPHA)) {
   1.445 +                hw_blit_ok = info->blit_sw_A;
   1.446 +            }
   1.447 +        }
   1.448 +        if (hw_blit_ok) {
   1.449 +            _this->CheckHWBlit (_this, surface, surface->map->dst);
   1.450 +        }
   1.451 +    }
   1.452  
   1.453 -	/* Check for special "identity" case -- copy blit */
   1.454 -	if ( surface->map->identity && blit_index == 0 ) {
   1.455 -	        surface->map->sw_data->blit = SDL_BlitCopy;
   1.456 +    /* if an alpha pixel format is specified, we can accelerate alpha blits */
   1.457 +    if (((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
   1.458 +        && (_this->displayformatalphapixel)) {
   1.459 +        if ((surface->flags & SDL_SRCALPHA))
   1.460 +            if (info->blit_hw_A) {
   1.461 +                _this->CheckHWBlit (_this, surface, surface->map->dst);
   1.462 +            }
   1.463 +    }
   1.464  
   1.465 -		/* Handle overlapping blits on the same surface */
   1.466 -		if ( surface == surface->map->dst ) {
   1.467 -		        surface->map->sw_data->blit = SDL_BlitCopyOverlap;
   1.468 -		}
   1.469 -	} else {
   1.470 -		if ( surface->format->BitsPerPixel < 8 ) {
   1.471 -			surface->map->sw_data->blit =
   1.472 -			    SDL_CalculateBlit0(surface, blit_index);
   1.473 -		} else {
   1.474 -			switch ( surface->format->BytesPerPixel ) {
   1.475 -			    case 1:
   1.476 -				surface->map->sw_data->blit =
   1.477 -				    SDL_CalculateBlit1(surface, blit_index);
   1.478 -				break;
   1.479 -			    case 2:
   1.480 -			    case 3:
   1.481 -			    case 4:
   1.482 -				surface->map->sw_data->blit =
   1.483 -				    SDL_CalculateBlitN(surface, blit_index);
   1.484 -				break;
   1.485 -			    default:
   1.486 -				surface->map->sw_data->blit = NULL;
   1.487 -				break;
   1.488 -			}
   1.489 -		}
   1.490 -	}
   1.491 -	/* Make sure we have a blit function */
   1.492 -	if ( surface->map->sw_data->blit == NULL ) {
   1.493 -		SDL_InvalidateMap(surface->map);
   1.494 -		SDL_SetError("Blit combination not supported");
   1.495 -		return(-1);
   1.496 -	}
   1.497 +    /* Get the blit function index, based on surface mode */
   1.498 +    /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
   1.499 +    blit_index = 0;
   1.500 +    blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
   1.501 +    if (surface->flags & SDL_SRCALPHA
   1.502 +        && (surface->format->alpha != SDL_ALPHA_OPAQUE
   1.503 +            || surface->format->Amask)) {
   1.504 +        blit_index |= 2;
   1.505 +    }
   1.506  
   1.507 -	/* Choose software blitting function */
   1.508 -	if(surface->flags & SDL_RLEACCELOK
   1.509 -	   && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
   1.510 +    /* Check for special "identity" case -- copy blit */
   1.511 +    if (surface->map->identity && blit_index == 0) {
   1.512 +        surface->map->sw_data->blit = SDL_BlitCopy;
   1.513  
   1.514 -	        if(surface->map->identity
   1.515 -		   && (blit_index == 1
   1.516 -		       || (blit_index == 3 && !surface->format->Amask))) {
   1.517 -		        if ( SDL_RLESurface(surface) == 0 )
   1.518 -			        surface->map->sw_blit = SDL_RLEBlit;
   1.519 -		} else if(blit_index == 2 && surface->format->Amask) {
   1.520 -		        if ( SDL_RLESurface(surface) == 0 )
   1.521 -			        surface->map->sw_blit = SDL_RLEAlphaBlit;
   1.522 -		}
   1.523 -	}
   1.524 -	
   1.525 -	if ( surface->map->sw_blit == NULL ) {
   1.526 -		surface->map->sw_blit = SDL_SoftBlit;
   1.527 -	}
   1.528 -	return(0);
   1.529 +        /* Handle overlapping blits on the same surface */
   1.530 +        if (surface == surface->map->dst) {
   1.531 +            surface->map->sw_data->blit = SDL_BlitCopyOverlap;
   1.532 +        }
   1.533 +    } else {
   1.534 +        if (surface->format->BitsPerPixel < 8) {
   1.535 +            surface->map->sw_data->blit =
   1.536 +                SDL_CalculateBlit0 (surface, blit_index);
   1.537 +        } else {
   1.538 +            switch (surface->format->BytesPerPixel) {
   1.539 +            case 1:
   1.540 +                surface->map->sw_data->blit =
   1.541 +                    SDL_CalculateBlit1 (surface, blit_index);
   1.542 +                break;
   1.543 +            case 2:
   1.544 +            case 3:
   1.545 +            case 4:
   1.546 +                surface->map->sw_data->blit =
   1.547 +                    SDL_CalculateBlitN (surface, blit_index);
   1.548 +                break;
   1.549 +            default:
   1.550 +                surface->map->sw_data->blit = NULL;
   1.551 +                break;
   1.552 +            }
   1.553 +        }
   1.554 +    }
   1.555 +    /* Make sure we have a blit function */
   1.556 +    if (surface->map->sw_data->blit == NULL) {
   1.557 +        SDL_InvalidateMap (surface->map);
   1.558 +        SDL_SetError ("Blit combination not supported");
   1.559 +        return (-1);
   1.560 +    }
   1.561 +
   1.562 +    /* Choose software blitting function */
   1.563 +    if (surface->flags & SDL_RLEACCELOK
   1.564 +        && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
   1.565 +
   1.566 +        if (surface->map->identity
   1.567 +            && (blit_index == 1
   1.568 +                || (blit_index == 3 && !surface->format->Amask))) {
   1.569 +            if (SDL_RLESurface (surface) == 0)
   1.570 +                surface->map->sw_blit = SDL_RLEBlit;
   1.571 +        } else if (blit_index == 2 && surface->format->Amask) {
   1.572 +            if (SDL_RLESurface (surface) == 0)
   1.573 +                surface->map->sw_blit = SDL_RLEAlphaBlit;
   1.574 +        }
   1.575 +    }
   1.576 +
   1.577 +    if (surface->map->sw_blit == NULL) {
   1.578 +        surface->map->sw_blit = SDL_SoftBlit;
   1.579 +    }
   1.580 +    return (0);
   1.581  }
   1.582  
   1.583 +/* vi: set ts=4 sw=4 expandtab: */