src/video/SDL_blit.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 05 Feb 2007 06:44:51 +0000
branchSDL-1.2
changeset 3909 6832b00d3594
parent 1402 d910939febfa
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Patched to compile on BeOS and old, old GCC releases.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_sysvideo.h"
slouken@0
    26
#include "SDL_blit.h"
slouken@0
    27
#include "SDL_RLEaccel_c.h"
slouken@0
    28
#include "SDL_pixels_c.h"
slouken@0
    29
slouken@1402
    30
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && SDL_ASSEMBLY_ROUTINES
slouken@880
    31
#define MMX_ASMBLIT
icculus@3909
    32
#if (__GNUC__ > 2)  /* SSE instructions aren't in GCC 2. */
icculus@3909
    33
#define SSE_ASMBLIT
icculus@3909
    34
#endif
slouken@880
    35
#endif
slouken@880
    36
slouken@880
    37
#if defined(MMX_ASMBLIT)
slouken@739
    38
#include "SDL_cpuinfo.h"
slouken@689
    39
#include "mmx.h"
slouken@689
    40
#endif
slouken@689
    41
slouken@0
    42
/* The general purpose software blit routine */
slouken@0
    43
static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
    44
			SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
    45
{
slouken@0
    46
	int okay;
slouken@0
    47
	int src_locked;
slouken@0
    48
	int dst_locked;
slouken@0
    49
slouken@0
    50
	/* Everything is okay at the beginning...  */
slouken@0
    51
	okay = 1;
slouken@0
    52
slouken@0
    53
	/* Lock the destination if it's in hardware */
slouken@0
    54
	dst_locked = 0;
slouken@526
    55
	if ( SDL_MUSTLOCK(dst) ) {
slouken@526
    56
		if ( SDL_LockSurface(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@526
    64
	if ( SDL_MUSTLOCK(src) ) {
slouken@526
    65
		if ( SDL_LockSurface(src) < 0 ) {
slouken@0
    66
			okay = 0;
slouken@0
    67
		} else {
slouken@0
    68
			src_locked = 1;
slouken@0
    69
		}
slouken@0
    70
	}
slouken@0
    71
slouken@0
    72
	/* Set up source and destination buffer pointers, and BLIT! */
slouken@0
    73
	if ( okay  && srcrect->w && srcrect->h ) {
slouken@0
    74
		SDL_BlitInfo info;
slouken@0
    75
		SDL_loblit RunBlit;
slouken@0
    76
slouken@0
    77
		/* Set up the blit information */
slouken@526
    78
		info.s_pixels = (Uint8 *)src->pixels +
slouken@0
    79
				(Uint16)srcrect->y*src->pitch +
slouken@0
    80
				(Uint16)srcrect->x*src->format->BytesPerPixel;
slouken@0
    81
		info.s_width = srcrect->w;
slouken@0
    82
		info.s_height = srcrect->h;
slouken@0
    83
		info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
slouken@526
    84
		info.d_pixels = (Uint8 *)dst->pixels +
slouken@0
    85
				(Uint16)dstrect->y*dst->pitch +
slouken@0
    86
				(Uint16)dstrect->x*dst->format->BytesPerPixel;
slouken@0
    87
		info.d_width = dstrect->w;
slouken@0
    88
		info.d_height = dstrect->h;
slouken@0
    89
		info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
slouken@0
    90
		info.aux_data = src->map->sw_data->aux_data;
slouken@0
    91
		info.src = src->format;
slouken@0
    92
		info.table = src->map->table;
slouken@0
    93
		info.dst = dst->format;
slouken@0
    94
		RunBlit = src->map->sw_data->blit;
slouken@0
    95
slouken@0
    96
		/* Run the actual software blit */
slouken@0
    97
		RunBlit(&info);
slouken@0
    98
	}
slouken@0
    99
slouken@0
   100
	/* We need to unlock the surfaces if they're locked */
slouken@0
   101
	if ( dst_locked ) {
slouken@526
   102
		SDL_UnlockSurface(dst);
slouken@310
   103
	}
slouken@0
   104
	if ( src_locked ) {
slouken@526
   105
		SDL_UnlockSurface(src);
slouken@0
   106
	}
slouken@0
   107
	/* Blit is done! */
slouken@0
   108
	return(okay ? 0 : -1);
slouken@0
   109
}
slouken@0
   110
slouken@880
   111
#ifdef MMX_ASMBLIT
slouken@1196
   112
static __inline__ void SDL_memcpyMMX(Uint8 *to, const Uint8 *from, int len)
slouken@689
   113
{
slouken@689
   114
	int i;
slouken@689
   115
slouken@689
   116
	for(i=0; i<len/8; i++) {
slouken@689
   117
		__asm__ __volatile__ (
slouken@689
   118
		"	movq (%0), %%mm0\n"
slouken@689
   119
		"	movq %%mm0, (%1)\n"
slouken@689
   120
		: : "r" (from), "r" (to) : "memory");
slouken@689
   121
		from+=8;
slouken@689
   122
		to+=8;
slouken@689
   123
	}
slouken@689
   124
	if (len&7)
slouken@689
   125
		SDL_memcpy(to, from, len&7);
slouken@689
   126
}
slouken@689
   127
icculus@3909
   128
#ifdef SSE_ASMBLIT
slouken@1196
   129
static __inline__ void SDL_memcpySSE(Uint8 *to, const Uint8 *from, int len)
slouken@689
   130
{
slouken@689
   131
	int i;
slouken@689
   132
slouken@689
   133
	__asm__ __volatile__ (
slouken@689
   134
	"	prefetchnta (%0)\n"
slouken@689
   135
	"	prefetchnta 64(%0)\n"
slouken@689
   136
	"	prefetchnta 128(%0)\n"
slouken@689
   137
	"	prefetchnta 192(%0)\n"
slouken@689
   138
	: : "r" (from) );
slouken@689
   139
slouken@689
   140
	for(i=0; i<len/8; i++) {
slouken@689
   141
		__asm__ __volatile__ (
slouken@689
   142
		"	prefetchnta 256(%0)\n"
slouken@689
   143
		"	movq (%0), %%mm0\n"
slouken@689
   144
		"	movntq %%mm0, (%1)\n"
slouken@689
   145
		: : "r" (from), "r" (to) : "memory");
slouken@689
   146
		from+=8;
slouken@689
   147
		to+=8;
slouken@689
   148
	}
slouken@689
   149
	if (len&7)
slouken@689
   150
		SDL_memcpy(to, from, len&7);
slouken@689
   151
}
slouken@689
   152
#endif
icculus@3909
   153
#endif
slouken@689
   154
slouken@0
   155
static void SDL_BlitCopy(SDL_BlitInfo *info)
slouken@0
   156
{
slouken@0
   157
	Uint8 *src, *dst;
slouken@0
   158
	int w, h;
slouken@0
   159
	int srcskip, dstskip;
slouken@0
   160
slouken@0
   161
	w = info->d_width*info->dst->BytesPerPixel;
slouken@0
   162
	h = info->d_height;
slouken@0
   163
	src = info->s_pixels;
slouken@0
   164
	dst = info->d_pixels;
slouken@0
   165
	srcskip = w+info->s_skip;
slouken@0
   166
	dstskip = w+info->d_skip;
icculus@3909
   167
icculus@3909
   168
#ifdef SSE_ASMBLIT
slouken@739
   169
	if(SDL_HasSSE())
slouken@689
   170
	{
slouken@689
   171
		while ( h-- ) {
slouken@689
   172
			SDL_memcpySSE(dst, src, w);
slouken@689
   173
			src += srcskip;
slouken@689
   174
			dst += dstskip;
slouken@689
   175
		}
slouken@689
   176
		__asm__ __volatile__ (
slouken@689
   177
		"	emms\n"
slouken@689
   178
		::);
slouken@689
   179
	}
slouken@689
   180
	else
icculus@3909
   181
#endif
icculus@3909
   182
#ifdef MMX_ASMBLIT
slouken@739
   183
	if(SDL_HasMMX())
slouken@689
   184
	{
slouken@689
   185
		while ( h-- ) {
slouken@689
   186
			SDL_memcpyMMX(dst, src, w);
slouken@689
   187
			src += srcskip;
slouken@689
   188
			dst += dstskip;
slouken@689
   189
		}
slouken@689
   190
		__asm__ __volatile__ (
slouken@689
   191
		"	emms\n"
slouken@689
   192
		::);
slouken@689
   193
	}
slouken@689
   194
	else
slouken@689
   195
#endif
slouken@0
   196
	while ( h-- ) {
slouken@0
   197
		SDL_memcpy(dst, src, w);
slouken@0
   198
		src += srcskip;
slouken@0
   199
		dst += dstskip;
slouken@0
   200
	}
slouken@0
   201
}
slouken@0
   202
slouken@0
   203
static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
slouken@0
   204
{
slouken@0
   205
	Uint8 *src, *dst;
slouken@0
   206
	int w, h;
slouken@0
   207
	int srcskip, dstskip;
slouken@0
   208
slouken@0
   209
	w = info->d_width*info->dst->BytesPerPixel;
slouken@0
   210
	h = info->d_height;
slouken@0
   211
	src = info->s_pixels;
slouken@0
   212
	dst = info->d_pixels;
slouken@0
   213
	srcskip = w+info->s_skip;
slouken@0
   214
	dstskip = w+info->d_skip;
slouken@0
   215
	if ( dst < src ) {
slouken@0
   216
		while ( h-- ) {
slouken@0
   217
			SDL_memcpy(dst, src, w);
slouken@0
   218
			src += srcskip;
slouken@0
   219
			dst += dstskip;
slouken@0
   220
		}
slouken@0
   221
	} else {
slouken@0
   222
		src += ((h-1) * srcskip);
slouken@0
   223
		dst += ((h-1) * dstskip);
slouken@0
   224
		while ( h-- ) {
slouken@0
   225
			SDL_revcpy(dst, src, w);
slouken@0
   226
			src -= srcskip;
slouken@0
   227
			dst -= dstskip;
slouken@0
   228
		}
slouken@0
   229
	}
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
/* Figure out which of many blit routines to set up on a surface */
slouken@0
   233
int SDL_CalculateBlit(SDL_Surface *surface)
slouken@0
   234
{
slouken@0
   235
	int blit_index;
slouken@0
   236
slouken@0
   237
	/* Clean everything out to start */
slouken@0
   238
	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   239
		SDL_UnRLESurface(surface, 1);
slouken@0
   240
	}
slouken@0
   241
	surface->map->sw_blit = NULL;
slouken@0
   242
slouken@0
   243
	/* Figure out if an accelerated hardware blit is possible */
slouken@0
   244
	surface->flags &= ~SDL_HWACCEL;
slouken@0
   245
	if ( surface->map->identity ) {
slouken@0
   246
		int hw_blit_ok;
slouken@0
   247
slouken@0
   248
		if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@0
   249
			/* We only support accelerated blitting to hardware */
slouken@0
   250
			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
slouken@0
   251
				hw_blit_ok = current_video->info.blit_hw;
slouken@0
   252
			} else {
slouken@0
   253
				hw_blit_ok = 0;
slouken@0
   254
			}
slouken@0
   255
			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@0
   256
				hw_blit_ok = current_video->info.blit_hw_CC;
slouken@0
   257
			}
slouken@0
   258
			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
slouken@0
   259
				hw_blit_ok = current_video->info.blit_hw_A;
slouken@0
   260
			}
slouken@0
   261
		} else {
slouken@0
   262
			/* We only support accelerated blitting to hardware */
slouken@0
   263
			if ( surface->map->dst->flags & SDL_HWSURFACE ) {
slouken@0
   264
				hw_blit_ok = current_video->info.blit_sw;
slouken@0
   265
			} else {
slouken@0
   266
				hw_blit_ok = 0;
slouken@0
   267
			}
slouken@0
   268
			if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@0
   269
				hw_blit_ok = current_video->info.blit_sw_CC;
slouken@0
   270
			}
slouken@0
   271
			if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
slouken@0
   272
				hw_blit_ok = current_video->info.blit_sw_A;
slouken@0
   273
			}
slouken@0
   274
		}
slouken@0
   275
		if ( hw_blit_ok ) {
slouken@0
   276
			SDL_VideoDevice *video = current_video;
slouken@0
   277
			SDL_VideoDevice *this  = current_video;
slouken@0
   278
			video->CheckHWBlit(this, surface, surface->map->dst);
slouken@0
   279
		}
slouken@0
   280
	}
icculus@1052
   281
	
icculus@1052
   282
	/* if an alpha pixel format is specified, we can accelerate alpha blits */
icculus@1052
   283
	if (((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE )&&(current_video->displayformatalphapixel)) 
icculus@1052
   284
	{
icculus@1052
   285
		if ( (surface->flags & SDL_SRCALPHA) ) 
icculus@1052
   286
			if ( current_video->info.blit_hw_A ) {
icculus@1052
   287
				SDL_VideoDevice *video = current_video;
icculus@1052
   288
				SDL_VideoDevice *this  = current_video;
icculus@1052
   289
				video->CheckHWBlit(this, surface, surface->map->dst);
icculus@1052
   290
			}
icculus@1052
   291
	}
slouken@0
   292
slouken@0
   293
	/* Get the blit function index, based on surface mode */
slouken@0
   294
	/* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@0
   295
	blit_index = 0;
slouken@0
   296
	blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY))      << 0;
slouken@0
   297
	if ( surface->flags & SDL_SRCALPHA
slouken@0
   298
	     && (surface->format->alpha != SDL_ALPHA_OPAQUE
slouken@0
   299
		 || surface->format->Amask) ) {
slouken@0
   300
	        blit_index |= 2;
slouken@0
   301
	}
slouken@0
   302
slouken@0
   303
	/* Check for special "identity" case -- copy blit */
slouken@0
   304
	if ( surface->map->identity && blit_index == 0 ) {
slouken@0
   305
	        surface->map->sw_data->blit = SDL_BlitCopy;
slouken@0
   306
slouken@0
   307
		/* Handle overlapping blits on the same surface */
slouken@0
   308
		if ( surface == surface->map->dst ) {
slouken@0
   309
		        surface->map->sw_data->blit = SDL_BlitCopyOverlap;
slouken@0
   310
		}
slouken@0
   311
	} else {
slouken@0
   312
		if ( surface->format->BitsPerPixel < 8 ) {
slouken@0
   313
			surface->map->sw_data->blit =
slouken@0
   314
			    SDL_CalculateBlit0(surface, blit_index);
slouken@0
   315
		} else {
slouken@0
   316
			switch ( surface->format->BytesPerPixel ) {
slouken@0
   317
			    case 1:
slouken@0
   318
				surface->map->sw_data->blit =
slouken@0
   319
				    SDL_CalculateBlit1(surface, blit_index);
slouken@0
   320
				break;
slouken@0
   321
			    case 2:
slouken@0
   322
			    case 3:
slouken@0
   323
			    case 4:
slouken@0
   324
				surface->map->sw_data->blit =
slouken@0
   325
				    SDL_CalculateBlitN(surface, blit_index);
slouken@0
   326
				break;
slouken@0
   327
			    default:
slouken@0
   328
				surface->map->sw_data->blit = NULL;
slouken@0
   329
				break;
slouken@0
   330
			}
slouken@0
   331
		}
slouken@0
   332
	}
slouken@0
   333
	/* Make sure we have a blit function */
slouken@0
   334
	if ( surface->map->sw_data->blit == NULL ) {
slouken@0
   335
		SDL_InvalidateMap(surface->map);
slouken@0
   336
		SDL_SetError("Blit combination not supported");
slouken@0
   337
		return(-1);
slouken@0
   338
	}
slouken@0
   339
slouken@0
   340
	/* Choose software blitting function */
slouken@0
   341
	if(surface->flags & SDL_RLEACCELOK
slouken@0
   342
	   && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
slouken@0
   343
slouken@0
   344
	        if(surface->map->identity
slouken@0
   345
		   && (blit_index == 1
slouken@0
   346
		       || (blit_index == 3 && !surface->format->Amask))) {
slouken@0
   347
		        if ( SDL_RLESurface(surface) == 0 )
slouken@0
   348
			        surface->map->sw_blit = SDL_RLEBlit;
slouken@0
   349
		} else if(blit_index == 2 && surface->format->Amask) {
slouken@0
   350
		        if ( SDL_RLESurface(surface) == 0 )
slouken@0
   351
			        surface->map->sw_blit = SDL_RLEAlphaBlit;
slouken@0
   352
		}
slouken@0
   353
	}
slouken@0
   354
	
slouken@0
   355
	if ( surface->map->sw_blit == NULL ) {
slouken@0
   356
		surface->map->sw_blit = SDL_SoftBlit;
slouken@0
   357
	}
slouken@0
   358
	return(0);
slouken@0
   359
}
slouken@0
   360