src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Oct 2002 07:56:36 +0000
changeset 526 4314a501d7be
parent 310 c97c1d3b3b5c
child 689 5bb080d35049
permissions -rw-r--r--
Fixed a crash blitting RLE surfaces to RLE surfaces
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    31 
    32 #include "SDL_error.h"
    33 #include "SDL_video.h"
    34 #include "SDL_sysvideo.h"
    35 #include "SDL_blit.h"
    36 #include "SDL_RLEaccel_c.h"
    37 #include "SDL_pixels_c.h"
    38 #include "SDL_memops.h"
    39 
    40 /* The general purpose software blit routine */
    41 static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
    42 			SDL_Surface *dst, SDL_Rect *dstrect)
    43 {
    44 	int okay;
    45 	int src_locked;
    46 	int dst_locked;
    47 
    48 	/* Everything is okay at the beginning...  */
    49 	okay = 1;
    50 
    51 	/* Lock the destination if it's in hardware */
    52 	dst_locked = 0;
    53 	if ( SDL_MUSTLOCK(dst) ) {
    54 		if ( SDL_LockSurface(dst) < 0 ) {
    55 			okay = 0;
    56 		} else {
    57 			dst_locked = 1;
    58 		}
    59 	}
    60 	/* Lock the source if it's in hardware */
    61 	src_locked = 0;
    62 	if ( SDL_MUSTLOCK(src) ) {
    63 		if ( SDL_LockSurface(src) < 0 ) {
    64 			okay = 0;
    65 		} else {
    66 			src_locked = 1;
    67 		}
    68 	}
    69 
    70 	/* Set up source and destination buffer pointers, and BLIT! */
    71 	if ( okay  && srcrect->w && srcrect->h ) {
    72 		SDL_BlitInfo info;
    73 		SDL_loblit RunBlit;
    74 
    75 		/* Set up the blit information */
    76 		info.s_pixels = (Uint8 *)src->pixels +
    77 				(Uint16)srcrect->y*src->pitch +
    78 				(Uint16)srcrect->x*src->format->BytesPerPixel;
    79 		info.s_width = srcrect->w;
    80 		info.s_height = srcrect->h;
    81 		info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
    82 		info.d_pixels = (Uint8 *)dst->pixels +
    83 				(Uint16)dstrect->y*dst->pitch +
    84 				(Uint16)dstrect->x*dst->format->BytesPerPixel;
    85 		info.d_width = dstrect->w;
    86 		info.d_height = dstrect->h;
    87 		info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
    88 		info.aux_data = src->map->sw_data->aux_data;
    89 		info.src = src->format;
    90 		info.table = src->map->table;
    91 		info.dst = dst->format;
    92 		RunBlit = src->map->sw_data->blit;
    93 
    94 		/* Run the actual software blit */
    95 		RunBlit(&info);
    96 	}
    97 
    98 	/* We need to unlock the surfaces if they're locked */
    99 	if ( dst_locked ) {
   100 		SDL_UnlockSurface(dst);
   101 	}
   102 	if ( src_locked ) {
   103 		SDL_UnlockSurface(src);
   104 	}
   105 	/* Blit is done! */
   106 	return(okay ? 0 : -1);
   107 }
   108 
   109 static void SDL_BlitCopy(SDL_BlitInfo *info)
   110 {
   111 	Uint8 *src, *dst;
   112 	int w, h;
   113 	int srcskip, dstskip;
   114 
   115 	w = info->d_width*info->dst->BytesPerPixel;
   116 	h = info->d_height;
   117 	src = info->s_pixels;
   118 	dst = info->d_pixels;
   119 	srcskip = w+info->s_skip;
   120 	dstskip = w+info->d_skip;
   121 	while ( h-- ) {
   122 		SDL_memcpy(dst, src, w);
   123 		src += srcskip;
   124 		dst += dstskip;
   125 	}
   126 }
   127 
   128 static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
   129 {
   130 	Uint8 *src, *dst;
   131 	int w, h;
   132 	int srcskip, dstskip;
   133 
   134 	w = info->d_width*info->dst->BytesPerPixel;
   135 	h = info->d_height;
   136 	src = info->s_pixels;
   137 	dst = info->d_pixels;
   138 	srcskip = w+info->s_skip;
   139 	dstskip = w+info->d_skip;
   140 	if ( dst < src ) {
   141 		while ( h-- ) {
   142 			SDL_memcpy(dst, src, w);
   143 			src += srcskip;
   144 			dst += dstskip;
   145 		}
   146 	} else {
   147 		src += ((h-1) * srcskip);
   148 		dst += ((h-1) * dstskip);
   149 		while ( h-- ) {
   150 			SDL_revcpy(dst, src, w);
   151 			src -= srcskip;
   152 			dst -= dstskip;
   153 		}
   154 	}
   155 }
   156 
   157 /* Figure out which of many blit routines to set up on a surface */
   158 int SDL_CalculateBlit(SDL_Surface *surface)
   159 {
   160 	int blit_index;
   161 
   162 	/* Clean everything out to start */
   163 	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
   164 		SDL_UnRLESurface(surface, 1);
   165 	}
   166 	surface->map->sw_blit = NULL;
   167 
   168 	/* Figure out if an accelerated hardware blit is possible */
   169 	surface->flags &= ~SDL_HWACCEL;
   170 	if ( surface->map->identity ) {
   171 		int hw_blit_ok;
   172 
   173 		if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   174 			/* We only support accelerated blitting to hardware */
   175 			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
   176 				hw_blit_ok = current_video->info.blit_hw;
   177 			} else {
   178 				hw_blit_ok = 0;
   179 			}
   180 			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   181 				hw_blit_ok = current_video->info.blit_hw_CC;
   182 			}
   183 			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
   184 				hw_blit_ok = current_video->info.blit_hw_A;
   185 			}
   186 		} else {
   187 			/* We only support accelerated blitting to hardware */
   188 			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
   189 				hw_blit_ok = current_video->info.blit_sw;
   190 			} else {
   191 				hw_blit_ok = 0;
   192 			}
   193 			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
   194 				hw_blit_ok = current_video->info.blit_sw_CC;
   195 			}
   196 			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
   197 				hw_blit_ok = current_video->info.blit_sw_A;
   198 			}
   199 		}
   200 		if ( hw_blit_ok ) {
   201 			SDL_VideoDevice *video = current_video;
   202 			SDL_VideoDevice *this  = current_video;
   203 			video->CheckHWBlit(this, surface, surface->map->dst);
   204 		}
   205 	}
   206 
   207 	/* Get the blit function index, based on surface mode */
   208 	/* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
   209 	blit_index = 0;
   210 	blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY))      << 0;
   211 	if ( surface->flags & SDL_SRCALPHA
   212 	     && (surface->format->alpha != SDL_ALPHA_OPAQUE
   213 		 || surface->format->Amask) ) {
   214 	        blit_index |= 2;
   215 	}
   216 
   217 	/* Check for special "identity" case -- copy blit */
   218 	if ( surface->map->identity && blit_index == 0 ) {
   219 	        surface->map->sw_data->blit = SDL_BlitCopy;
   220 
   221 		/* Handle overlapping blits on the same surface */
   222 		if ( surface == surface->map->dst ) {
   223 		        surface->map->sw_data->blit = SDL_BlitCopyOverlap;
   224 		}
   225 	} else {
   226 		if ( surface->format->BitsPerPixel < 8 ) {
   227 			surface->map->sw_data->blit =
   228 			    SDL_CalculateBlit0(surface, blit_index);
   229 		} else {
   230 			switch ( surface->format->BytesPerPixel ) {
   231 			    case 1:
   232 				surface->map->sw_data->blit =
   233 				    SDL_CalculateBlit1(surface, blit_index);
   234 				break;
   235 			    case 2:
   236 			    case 3:
   237 			    case 4:
   238 				surface->map->sw_data->blit =
   239 				    SDL_CalculateBlitN(surface, blit_index);
   240 				break;
   241 			    default:
   242 				surface->map->sw_data->blit = NULL;
   243 				break;
   244 			}
   245 		}
   246 	}
   247 	/* Make sure we have a blit function */
   248 	if ( surface->map->sw_data->blit == NULL ) {
   249 		SDL_InvalidateMap(surface->map);
   250 		SDL_SetError("Blit combination not supported");
   251 		return(-1);
   252 	}
   253 
   254 	/* Choose software blitting function */
   255 	if(surface->flags & SDL_RLEACCELOK
   256 	   && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
   257 
   258 	        if(surface->map->identity
   259 		   && (blit_index == 1
   260 		       || (blit_index == 3 && !surface->format->Amask))) {
   261 		        if ( SDL_RLESurface(surface) == 0 )
   262 			        surface->map->sw_blit = SDL_RLEBlit;
   263 		} else if(blit_index == 2 && surface->format->Amask) {
   264 		        if ( SDL_RLESurface(surface) == 0 )
   265 			        surface->map->sw_blit = SDL_RLEAlphaBlit;
   266 		}
   267 	}
   268 	
   269 	if ( surface->map->sw_blit == NULL ) {
   270 		surface->map->sw_blit = SDL_SoftBlit;
   271 	}
   272 	return(0);
   273 }
   274