src/video/SDL_stretch.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 288 2f5a6062db86
child 627 8b9ac38381d0
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
/* This a stretch blit implementation based on ideas given to me by
slouken@0
    29
   Tomasz Cejner - thanks! :)
slouken@0
    30
slouken@0
    31
   April 27, 2000 - Sam Lantinga
slouken@0
    32
*/
slouken@0
    33
slouken@0
    34
#include "SDL_error.h"
slouken@0
    35
#include "SDL_video.h"
slouken@0
    36
#include "SDL_blit.h"
slouken@0
    37
slouken@0
    38
/* This isn't ready for general consumption yet - it should be folded
slouken@0
    39
   into the general blitting mechanism.
slouken@0
    40
*/
slouken@0
    41
slouken@288
    42
#if (defined(WIN32) && !defined(_M_ALPHA) && !defined(_WIN32_WCE) && \
slouken@288
    43
     !defined(__WATCOMC__) && !defined(__LCC__) && !defined(__FREEBCC__)) || \
slouken@288
    44
    (defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT))
slouken@0
    45
#define USE_ASM_STRETCH
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
#ifdef USE_ASM_STRETCH
slouken@0
    49
slouken@0
    50
#if defined(WIN32) || defined(i386)
slouken@0
    51
#define PREFIX16	0x66
slouken@0
    52
#define STORE_BYTE	0xAA
slouken@0
    53
#define STORE_WORD	0xAB
slouken@0
    54
#define LOAD_BYTE	0xAC
slouken@0
    55
#define LOAD_WORD	0xAD
slouken@0
    56
#define RETURN		0xC3
slouken@0
    57
#else
slouken@0
    58
#error Need assembly opcodes for this architecture
slouken@0
    59
#endif
slouken@0
    60
slouken@0
    61
#if defined(__ELF__) && defined(__GNUC__)
slouken@0
    62
extern unsigned char _copy_row[4096] __attribute__ ((alias ("copy_row")));
slouken@0
    63
#endif
slouken@0
    64
static unsigned char copy_row[4096];
slouken@0
    65
slouken@0
    66
static int generate_rowbytes(int src_w, int dst_w, int bpp)
slouken@0
    67
{
slouken@0
    68
	static struct {
slouken@0
    69
		int bpp;
slouken@0
    70
		int src_w;
slouken@0
    71
		int dst_w;
slouken@0
    72
	} last;
slouken@0
    73
slouken@0
    74
	int i;
slouken@0
    75
	int pos, inc;
slouken@0
    76
	unsigned char *eip;
slouken@0
    77
	unsigned char load, store;
slouken@0
    78
slouken@0
    79
	/* See if we need to regenerate the copy buffer */
slouken@0
    80
	if ( (src_w == last.src_w) &&
slouken@0
    81
	     (dst_w == last.src_w) && (bpp == last.bpp) ) {
slouken@0
    82
		return(0);
slouken@0
    83
	}
slouken@0
    84
	last.bpp = bpp;
slouken@0
    85
	last.src_w = src_w;
slouken@0
    86
	last.dst_w = dst_w;
slouken@0
    87
slouken@0
    88
	switch (bpp) {
slouken@0
    89
	    case 1:
slouken@0
    90
		load = LOAD_BYTE;
slouken@0
    91
		store = STORE_BYTE;
slouken@0
    92
		break;
slouken@0
    93
	    case 2:
slouken@0
    94
	    case 4:
slouken@0
    95
		load = LOAD_WORD;
slouken@0
    96
		store = STORE_WORD;
slouken@0
    97
		break;
slouken@0
    98
	    default:
slouken@0
    99
		SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
slouken@0
   100
		return(-1);
slouken@0
   101
	}
slouken@0
   102
	pos = 0x10000;
slouken@0
   103
	inc = (src_w << 16) / dst_w;
slouken@0
   104
	eip = copy_row;
slouken@0
   105
	for ( i=0; i<dst_w; ++i ) {
slouken@0
   106
		while ( pos >= 0x10000L ) {
slouken@0
   107
			if ( bpp == 2 ) {
slouken@0
   108
				*eip++ = PREFIX16;
slouken@0
   109
			}
slouken@0
   110
			*eip++ = load;
slouken@0
   111
			pos -= 0x10000L;
slouken@0
   112
		}
slouken@0
   113
		if ( bpp == 2 ) {
slouken@0
   114
			*eip++ = PREFIX16;
slouken@0
   115
		}
slouken@0
   116
		*eip++ = store;
slouken@0
   117
		pos += inc;
slouken@0
   118
	}
slouken@0
   119
	*eip++ = RETURN;
slouken@0
   120
slouken@0
   121
	/* Verify that we didn't overflow (too late) */
slouken@0
   122
	if ( eip > (copy_row+sizeof(copy_row)) ) {
slouken@0
   123
		SDL_SetError("Copy buffer overflow");
slouken@0
   124
		return(-1);
slouken@0
   125
	}
slouken@0
   126
	return(0);
slouken@0
   127
}
slouken@0
   128
slouken@0
   129
#else
slouken@0
   130
slouken@0
   131
#define DEFINE_COPY_ROW(name, type)			\
slouken@0
   132
void name(type *src, int src_w, type *dst, int dst_w)	\
slouken@0
   133
{							\
slouken@0
   134
	int i;						\
slouken@0
   135
	int pos, inc;					\
slouken@0
   136
	type pixel = 0;					\
slouken@0
   137
							\
slouken@0
   138
	pos = 0x10000;					\
slouken@0
   139
	inc = (src_w << 16) / dst_w;			\
slouken@0
   140
	for ( i=dst_w; i>0; --i ) {			\
slouken@0
   141
		while ( pos >= 0x10000L ) {		\
slouken@0
   142
			pixel = *src++;			\
slouken@0
   143
			pos -= 0x10000L;		\
slouken@0
   144
		}					\
slouken@0
   145
		*dst++ = pixel;				\
slouken@0
   146
		pos += inc;				\
slouken@0
   147
	}						\
slouken@0
   148
}
slouken@0
   149
DEFINE_COPY_ROW(copy_row1, Uint8)
slouken@0
   150
DEFINE_COPY_ROW(copy_row2, Uint16)
slouken@0
   151
DEFINE_COPY_ROW(copy_row4, Uint32)
slouken@0
   152
slouken@0
   153
#endif /* USE_ASM_STRETCH */
slouken@0
   154
slouken@0
   155
/* The ASM code doesn't handle 24-bpp stretch blits */
slouken@0
   156
void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
slouken@0
   157
{
slouken@0
   158
	int i;
slouken@0
   159
	int pos, inc;
slouken@0
   160
	Uint8 pixel[3];
slouken@0
   161
slouken@0
   162
	pos = 0x10000;
slouken@0
   163
	inc = (src_w << 16) / dst_w;
slouken@0
   164
	for ( i=dst_w; i>0; --i ) {
slouken@0
   165
		while ( pos >= 0x10000L ) {
slouken@0
   166
			pixel[0] = *src++;
slouken@0
   167
			pixel[1] = *src++;
slouken@0
   168
			pixel[2] = *src++;
slouken@0
   169
			pos -= 0x10000L;
slouken@0
   170
		}
slouken@0
   171
		*dst++ = pixel[0];
slouken@0
   172
		*dst++ = pixel[1];
slouken@0
   173
		*dst++ = pixel[2];
slouken@0
   174
		pos += inc;
slouken@0
   175
	}
slouken@0
   176
}
slouken@0
   177
slouken@0
   178
/* Perform a stretch blit between two surfaces of the same format.
slouken@0
   179
   NOTE:  This function is not safe to call from multiple threads!
slouken@0
   180
*/
slouken@0
   181
int SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   182
                    SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   183
{
slouken@0
   184
	int pos, inc;
slouken@0
   185
	int dst_width;
slouken@0
   186
	int dst_maxrow;
slouken@0
   187
	int src_row, dst_row;
slouken@0
   188
	Uint8 *srcp = NULL;
slouken@0
   189
	Uint8 *dstp;
slouken@0
   190
	SDL_Rect full_src;
slouken@0
   191
	SDL_Rect full_dst;
slouken@0
   192
#if defined(USE_ASM_STRETCH) && defined(__GNUC__)
slouken@0
   193
	int u1, u2;
slouken@0
   194
#endif
slouken@0
   195
	const int bpp = dst->format->BytesPerPixel;
slouken@0
   196
slouken@0
   197
	if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) {
slouken@0
   198
		SDL_SetError("Only works with same format surfaces");
slouken@0
   199
		return(-1);
slouken@0
   200
	}
slouken@0
   201
slouken@0
   202
	/* Verify the blit rectangles */
slouken@0
   203
	if ( srcrect ) {
slouken@0
   204
		if ( (srcrect->x < 0) || (srcrect->y < 0) ||
slouken@0
   205
		     ((srcrect->x+srcrect->w) > src->w) ||
slouken@0
   206
		     ((srcrect->y+srcrect->h) > src->h) ) {
slouken@0
   207
			SDL_SetError("Invalid source blit rectangle");
slouken@0
   208
			return(-1);
slouken@0
   209
		}
slouken@0
   210
	} else {
slouken@0
   211
		full_src.x = 0;
slouken@0
   212
		full_src.y = 0;
slouken@0
   213
		full_src.w = src->w;
slouken@0
   214
		full_src.h = src->h;
slouken@0
   215
		srcrect = &full_src;
slouken@0
   216
	}
slouken@0
   217
	if ( dstrect ) {
slouken@0
   218
		if ( (dstrect->x < 0) || (dstrect->y < 0) ||
slouken@0
   219
		     ((dstrect->x+dstrect->w) > dst->w) ||
slouken@0
   220
		     ((dstrect->y+dstrect->h) > dst->h) ) {
slouken@0
   221
			SDL_SetError("Invalid destination blit rectangle");
slouken@0
   222
			return(-1);
slouken@0
   223
		}
slouken@0
   224
	} else {
slouken@0
   225
		full_dst.x = 0;
slouken@0
   226
		full_dst.y = 0;
slouken@0
   227
		full_dst.w = dst->w;
slouken@0
   228
		full_dst.h = dst->h;
slouken@0
   229
		dstrect = &full_dst;
slouken@0
   230
	}
slouken@0
   231
slouken@0
   232
	/* Set up the data... */
slouken@0
   233
	pos = 0x10000;
slouken@0
   234
	inc = (srcrect->h << 16) / dstrect->h;
slouken@0
   235
	src_row = srcrect->y;
slouken@0
   236
	dst_row = dstrect->y;
slouken@0
   237
	dst_width = dstrect->w*bpp;
slouken@0
   238
slouken@0
   239
#ifdef USE_ASM_STRETCH
slouken@0
   240
	/* Write the opcodes for this stretch */
slouken@0
   241
	if ( (bpp != 3) &&
slouken@0
   242
	     (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0) ) {
slouken@0
   243
		return(-1);
slouken@0
   244
	}
slouken@0
   245
#endif
slouken@0
   246
slouken@0
   247
	/* Perform the stretch blit */
slouken@0
   248
	for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
slouken@0
   249
		dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
slouken@0
   250
		                            + (dstrect->x*bpp);
slouken@0
   251
		while ( pos >= 0x10000L ) {
slouken@0
   252
			srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
slouken@0
   253
			                            + (srcrect->x*bpp);
slouken@0
   254
			++src_row;
slouken@0
   255
			pos -= 0x10000L;
slouken@0
   256
		}
slouken@0
   257
#ifdef USE_ASM_STRETCH
slouken@0
   258
		switch (bpp) {
slouken@0
   259
		    case 3:
slouken@0
   260
			copy_row3(srcp, srcrect->w, dstp, dstrect->w);
slouken@0
   261
			break;
slouken@0
   262
		    default:
slouken@0
   263
#ifdef __GNUC__
slouken@0
   264
			__asm__ __volatile__ ("
slouken@0
   265
				call _copy_row
slouken@0
   266
			"
slouken@0
   267
			: "=&D" (u1), "=&S" (u2)
slouken@0
   268
			: "0" (dstp), "1" (srcp)
slouken@0
   269
			: "memory" );
slouken@0
   270
#else
slouken@0
   271
#ifdef WIN32
slouken@0
   272
		{ void *code = &copy_row;
slouken@0
   273
			__asm {
slouken@0
   274
				push edi
slouken@0
   275
				push esi
slouken@0
   276
	
slouken@0
   277
				mov edi, dstp
slouken@0
   278
				mov esi, srcp
slouken@0
   279
				call dword ptr code
slouken@0
   280
slouken@0
   281
				pop esi
slouken@0
   282
				pop edi
slouken@0
   283
			}
slouken@0
   284
		}
slouken@0
   285
#else
slouken@0
   286
#error Need inline assembly for this compiler
slouken@0
   287
#endif
slouken@0
   288
#endif /* __GNUC__ */
slouken@0
   289
			break;
slouken@0
   290
		}
slouken@0
   291
#else
slouken@0
   292
		switch (bpp) {
slouken@0
   293
		    case 1:
slouken@0
   294
			copy_row1(srcp, srcrect->w, dstp, dstrect->w);
slouken@0
   295
			break;
slouken@0
   296
		    case 2:
slouken@0
   297
			copy_row2((Uint16 *)srcp, srcrect->w,
slouken@0
   298
			          (Uint16 *)dstp, dstrect->w);
slouken@0
   299
			break;
slouken@0
   300
		    case 3:
slouken@0
   301
			copy_row3(srcp, srcrect->w, dstp, dstrect->w);
slouken@0
   302
			break;
slouken@0
   303
		    case 4:
slouken@0
   304
			copy_row4((Uint32 *)srcp, srcrect->w,
slouken@0
   305
			          (Uint32 *)dstp, dstrect->w);
slouken@0
   306
			break;
slouken@0
   307
		}
slouken@0
   308
#endif
slouken@0
   309
		pos += inc;
slouken@0
   310
	}
slouken@0
   311
	return(0);
slouken@0
   312
}
slouken@0
   313