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