src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 252 e8157fcb3114
child 310 c97c1d3b3b5c
permissions -rw-r--r--
Updated copyright information for 2002
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <stdlib.h>
slouken@0
    30
#include <string.h>
slouken@0
    31
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_video.h"
slouken@0
    34
#include "SDL_sysvideo.h"
slouken@0
    35
#include "SDL_blit.h"
slouken@0
    36
#include "SDL_RLEaccel_c.h"
slouken@0
    37
#include "SDL_pixels_c.h"
slouken@0
    38
#include "SDL_memops.h"
slouken@0
    39
slouken@0
    40
/* The general purpose software blit routine */
slouken@0
    41
static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
    42
			SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
    43
{
slouken@0
    44
	int okay;
slouken@0
    45
	int src_locked;
slouken@0
    46
	int dst_locked;
slouken@0
    47
slouken@0
    48
	/* Everything is okay at the beginning...  */
slouken@0
    49
	okay = 1;
slouken@0
    50
slouken@0
    51
	/* Lock the destination if it's in hardware */
slouken@0
    52
	dst_locked = 0;
slouken@0
    53
	if ( dst->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
    54
		SDL_VideoDevice *video = current_video;
slouken@0
    55
		SDL_VideoDevice *this  = current_video;
slouken@0
    56
		if ( video->LockHWSurface(this, dst) < 0 ) {
slouken@0
    57
			okay = 0;
slouken@0
    58
		} else {
slouken@0
    59
			dst_locked = 1;
slouken@0
    60
		}
slouken@0
    61
	}
slouken@0
    62
	/* Lock the source if it's in hardware */
slouken@0
    63
	src_locked = 0;
slouken@0
    64
	if ( src->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
    65
		SDL_VideoDevice *video = current_video;
slouken@0
    66
		SDL_VideoDevice *this  = current_video;
slouken@0
    67
		if ( video->LockHWSurface(this, src) < 0 ) {
slouken@0
    68
			okay = 0;
slouken@0
    69
		} else {
slouken@0
    70
			src_locked = 1;
slouken@0
    71
		}
slouken@0
    72
	}
slouken@0
    73
slouken@0
    74
	/* Unencode the destination if it's RLE encoded */
slouken@0
    75
	if ( dst->flags & SDL_RLEACCEL ) {
slouken@0
    76
		SDL_UnRLESurface(dst, 1);
slouken@0
    77
		dst->flags |= SDL_RLEACCEL;	/* save accel'd state */
slouken@0
    78
	}
slouken@0
    79
slouken@0
    80
	/* Set up source and destination buffer pointers, and BLIT! */
slouken@0
    81
	if ( okay  && srcrect->w && srcrect->h ) {
slouken@0
    82
		SDL_BlitInfo info;
slouken@0
    83
		SDL_loblit RunBlit;
slouken@0
    84
slouken@0
    85
		/* Set up the blit information */
slouken@0
    86
		info.s_pixels = (Uint8 *)src->pixels + src->offset +
slouken@0
    87
				(Uint16)srcrect->y*src->pitch +
slouken@0
    88
				(Uint16)srcrect->x*src->format->BytesPerPixel;
slouken@0
    89
		info.s_width = srcrect->w;
slouken@0
    90
		info.s_height = srcrect->h;
slouken@0
    91
		info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
slouken@0
    92
		info.d_pixels = (Uint8 *)dst->pixels + dst->offset +
slouken@0
    93
				(Uint16)dstrect->y*dst->pitch +
slouken@0
    94
				(Uint16)dstrect->x*dst->format->BytesPerPixel;
slouken@0
    95
		info.d_width = dstrect->w;
slouken@0
    96
		info.d_height = dstrect->h;
slouken@0
    97
		info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
slouken@0
    98
		info.aux_data = src->map->sw_data->aux_data;
slouken@0
    99
		info.src = src->format;
slouken@0
   100
		info.table = src->map->table;
slouken@0
   101
		info.dst = dst->format;
slouken@0
   102
		RunBlit = src->map->sw_data->blit;
slouken@0
   103
slouken@0
   104
		/* Run the actual software blit */
slouken@0
   105
		RunBlit(&info);
slouken@0
   106
	}
slouken@0
   107
slouken@0
   108
	/* Re-encode the destination if it's RLE encoded */
slouken@0
   109
	if ( dst->flags & SDL_RLEACCEL ) {
slouken@0
   110
	        dst->flags &= ~SDL_RLEACCEL; /* stop lying */
slouken@0
   111
		SDL_RLESurface(dst);
slouken@0
   112
	}
slouken@0
   113
slouken@0
   114
	/* We need to unlock the surfaces if they're locked */
slouken@0
   115
	if ( dst_locked ) {
slouken@0
   116
		SDL_VideoDevice *video = current_video;
slouken@0
   117
		SDL_VideoDevice *this  = current_video;
slouken@0
   118
		video->UnlockHWSurface(this, dst);
slouken@0
   119
	} else
slouken@0
   120
	if ( src_locked ) {
slouken@0
   121
		SDL_VideoDevice *video = current_video;
slouken@0
   122
		SDL_VideoDevice *this  = current_video;
slouken@0
   123
		video->UnlockHWSurface(this, src);
slouken@0
   124
	}
slouken@0
   125
	/* Blit is done! */
slouken@0
   126
	return(okay ? 0 : -1);
slouken@0
   127
}
slouken@0
   128
slouken@0
   129
static void SDL_BlitCopy(SDL_BlitInfo *info)
slouken@0
   130
{
slouken@0
   131
	Uint8 *src, *dst;
slouken@0
   132
	int w, h;
slouken@0
   133
	int srcskip, dstskip;
slouken@0
   134
slouken@0
   135
	w = info->d_width*info->dst->BytesPerPixel;
slouken@0
   136
	h = info->d_height;
slouken@0
   137
	src = info->s_pixels;
slouken@0
   138
	dst = info->d_pixels;
slouken@0
   139
	srcskip = w+info->s_skip;
slouken@0
   140
	dstskip = w+info->d_skip;
slouken@0
   141
	while ( h-- ) {
slouken@0
   142
		SDL_memcpy(dst, src, w);
slouken@0
   143
		src += srcskip;
slouken@0
   144
		dst += dstskip;
slouken@0
   145
	}
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
slouken@0
   149
{
slouken@0
   150
	Uint8 *src, *dst;
slouken@0
   151
	int w, h;
slouken@0
   152
	int srcskip, dstskip;
slouken@0
   153
slouken@0
   154
	w = info->d_width*info->dst->BytesPerPixel;
slouken@0
   155
	h = info->d_height;
slouken@0
   156
	src = info->s_pixels;
slouken@0
   157
	dst = info->d_pixels;
slouken@0
   158
	srcskip = w+info->s_skip;
slouken@0
   159
	dstskip = w+info->d_skip;
slouken@0
   160
	if ( dst < src ) {
slouken@0
   161
		while ( h-- ) {
slouken@0
   162
			SDL_memcpy(dst, src, w);
slouken@0
   163
			src += srcskip;
slouken@0
   164
			dst += dstskip;
slouken@0
   165
		}
slouken@0
   166
	} else {
slouken@0
   167
		src += ((h-1) * srcskip);
slouken@0
   168
		dst += ((h-1) * dstskip);
slouken@0
   169
		while ( h-- ) {
slouken@0
   170
			SDL_revcpy(dst, src, w);
slouken@0
   171
			src -= srcskip;
slouken@0
   172
			dst -= dstskip;
slouken@0
   173
		}
slouken@0
   174
	}
slouken@0
   175
}
slouken@0
   176
slouken@0
   177
/* Figure out which of many blit routines to set up on a surface */
slouken@0
   178
int SDL_CalculateBlit(SDL_Surface *surface)
slouken@0
   179
{
slouken@0
   180
	int blit_index;
slouken@0
   181
slouken@0
   182
	/* Clean everything out to start */
slouken@0
   183
	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   184
		SDL_UnRLESurface(surface, 1);
slouken@0
   185
	}
slouken@0
   186
	surface->map->sw_blit = NULL;
slouken@0
   187
slouken@0
   188
	/* Figure out if an accelerated hardware blit is possible */
slouken@0
   189
	surface->flags &= ~SDL_HWACCEL;
slouken@0
   190
	if ( surface->map->identity ) {
slouken@0
   191
		int hw_blit_ok;
slouken@0
   192
slouken@0
   193
		if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@0
   194
			/* We only support accelerated blitting to hardware */
slouken@0
   195
			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
slouken@0
   196
				hw_blit_ok = current_video->info.blit_hw;
slouken@0
   197
			} else {
slouken@0
   198
				hw_blit_ok = 0;
slouken@0
   199
			}
slouken@0
   200
			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@0
   201
				hw_blit_ok = current_video->info.blit_hw_CC;
slouken@0
   202
			}
slouken@0
   203
			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
slouken@0
   204
				hw_blit_ok = current_video->info.blit_hw_A;
slouken@0
   205
			}
slouken@0
   206
		} else {
slouken@0
   207
			/* We only support accelerated blitting to hardware */
slouken@0
   208
			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
slouken@0
   209
				hw_blit_ok = current_video->info.blit_sw;
slouken@0
   210
			} else {
slouken@0
   211
				hw_blit_ok = 0;
slouken@0
   212
			}
slouken@0
   213
			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@0
   214
				hw_blit_ok = current_video->info.blit_sw_CC;
slouken@0
   215
			}
slouken@0
   216
			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
slouken@0
   217
				hw_blit_ok = current_video->info.blit_sw_A;
slouken@0
   218
			}
slouken@0
   219
		}
slouken@0
   220
		if ( hw_blit_ok ) {
slouken@0
   221
			SDL_VideoDevice *video = current_video;
slouken@0
   222
			SDL_VideoDevice *this  = current_video;
slouken@0
   223
			video->CheckHWBlit(this, surface, surface->map->dst);
slouken@0
   224
		}
slouken@0
   225
	}
slouken@0
   226
slouken@0
   227
	/* Get the blit function index, based on surface mode */
slouken@0
   228
	/* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@0
   229
	blit_index = 0;
slouken@0
   230
	blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY))      << 0;
slouken@0
   231
	if ( surface->flags & SDL_SRCALPHA
slouken@0
   232
	     && (surface->format->alpha != SDL_ALPHA_OPAQUE
slouken@0
   233
		 || surface->format->Amask) ) {
slouken@0
   234
	        blit_index |= 2;
slouken@0
   235
	}
slouken@0
   236
slouken@0
   237
	/* Check for special "identity" case -- copy blit */
slouken@0
   238
	if ( surface->map->identity && blit_index == 0 ) {
slouken@0
   239
	        surface->map->sw_data->blit = SDL_BlitCopy;
slouken@0
   240
slouken@0
   241
		/* Handle overlapping blits on the same surface */
slouken@0
   242
		if ( surface == surface->map->dst ) {
slouken@0
   243
		        surface->map->sw_data->blit = SDL_BlitCopyOverlap;
slouken@0
   244
		}
slouken@0
   245
	} else {
slouken@0
   246
		if ( surface->format->BitsPerPixel < 8 ) {
slouken@0
   247
			surface->map->sw_data->blit =
slouken@0
   248
			    SDL_CalculateBlit0(surface, blit_index);
slouken@0
   249
		} else {
slouken@0
   250
			switch ( surface->format->BytesPerPixel ) {
slouken@0
   251
			    case 1:
slouken@0
   252
				surface->map->sw_data->blit =
slouken@0
   253
				    SDL_CalculateBlit1(surface, blit_index);
slouken@0
   254
				break;
slouken@0
   255
			    case 2:
slouken@0
   256
			    case 3:
slouken@0
   257
			    case 4:
slouken@0
   258
				surface->map->sw_data->blit =
slouken@0
   259
				    SDL_CalculateBlitN(surface, blit_index);
slouken@0
   260
				break;
slouken@0
   261
			    default:
slouken@0
   262
				surface->map->sw_data->blit = NULL;
slouken@0
   263
				break;
slouken@0
   264
			}
slouken@0
   265
		}
slouken@0
   266
	}
slouken@0
   267
	/* Make sure we have a blit function */
slouken@0
   268
	if ( surface->map->sw_data->blit == NULL ) {
slouken@0
   269
		SDL_InvalidateMap(surface->map);
slouken@0
   270
		SDL_SetError("Blit combination not supported");
slouken@0
   271
		return(-1);
slouken@0
   272
	}
slouken@0
   273
slouken@0
   274
	/* Choose software blitting function */
slouken@0
   275
	if(surface->flags & SDL_RLEACCELOK
slouken@0
   276
	   && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
slouken@0
   277
slouken@0
   278
	        if(surface->map->identity
slouken@0
   279
		   && (blit_index == 1
slouken@0
   280
		       || (blit_index == 3 && !surface->format->Amask))) {
slouken@0
   281
		        if ( SDL_RLESurface(surface) == 0 )
slouken@0
   282
			        surface->map->sw_blit = SDL_RLEBlit;
slouken@0
   283
		} else if(blit_index == 2 && surface->format->Amask) {
slouken@0
   284
		        if ( SDL_RLESurface(surface) == 0 )
slouken@0
   285
			        surface->map->sw_blit = SDL_RLEAlphaBlit;
slouken@0
   286
		}
slouken@0
   287
	}
slouken@0
   288
	
slouken@0
   289
	if ( surface->map->sw_blit == NULL ) {
slouken@0
   290
		surface->map->sw_blit = SDL_SoftBlit;
slouken@0
   291
	}
slouken@0
   292
	return(0);
slouken@0
   293
}
slouken@0
   294