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