src/video/SDL_stretch.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* This a stretch blit implementation based on ideas given to me by
    24    Tomasz Cejner - thanks! :)
    25 
    26    April 27, 2000 - Sam Lantinga
    27 */
    28 
    29 #include "SDL_video.h"
    30 #include "SDL_blit.h"
    31 
    32 /* This isn't ready for general consumption yet - it should be folded
    33    into the general blitting mechanism.
    34 */
    35 
    36 #if ((defined(_MFC_VER) && defined(_M_IX86)/* && !defined(_WIN32_WCE) still needed? */) || \
    37      (defined(i386) && defined(__GNUC__))) && SDL_ASSEMBLY_BLITTERS
    38 #define USE_ASM_STRETCH
    39 #endif
    40 
    41 #ifdef USE_ASM_STRETCH
    42 
    43 #if defined(_M_IX86) || defined(i386)
    44 #define PREFIX16	0x66
    45 #define STORE_BYTE	0xAA
    46 #define STORE_WORD	0xAB
    47 #define LOAD_BYTE	0xAC
    48 #define LOAD_WORD	0xAD
    49 #define RETURN		0xC3
    50 #else
    51 #error Need assembly opcodes for this architecture
    52 #endif
    53 
    54 static unsigned char copy_row[4096];
    55 
    56 static int generate_rowbytes(int src_w, int dst_w, int bpp)
    57 {
    58 	static struct {
    59 		int bpp;
    60 		int src_w;
    61 		int dst_w;
    62 	} last;
    63 
    64 	int i;
    65 	int pos, inc;
    66 	unsigned char *eip;
    67 	unsigned char load, store;
    68 
    69 	/* See if we need to regenerate the copy buffer */
    70 	if ( (src_w == last.src_w) &&
    71 	     (dst_w == last.dst_w) && (bpp == last.bpp) ) {
    72 		return(0);
    73 	}
    74 	last.bpp = bpp;
    75 	last.src_w = src_w;
    76 	last.dst_w = dst_w;
    77 
    78 	switch (bpp) {
    79 	    case 1:
    80 		load = LOAD_BYTE;
    81 		store = STORE_BYTE;
    82 		break;
    83 	    case 2:
    84 	    case 4:
    85 		load = LOAD_WORD;
    86 		store = STORE_WORD;
    87 		break;
    88 	    default:
    89 		SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
    90 		return(-1);
    91 	}
    92 	pos = 0x10000;
    93 	inc = (src_w << 16) / dst_w;
    94 	eip = copy_row;
    95 	for ( i=0; i<dst_w; ++i ) {
    96 		while ( pos >= 0x10000L ) {
    97 			if ( bpp == 2 ) {
    98 				*eip++ = PREFIX16;
    99 			}
   100 			*eip++ = load;
   101 			pos -= 0x10000L;
   102 		}
   103 		if ( bpp == 2 ) {
   104 			*eip++ = PREFIX16;
   105 		}
   106 		*eip++ = store;
   107 		pos += inc;
   108 	}
   109 	*eip++ = RETURN;
   110 
   111 	/* Verify that we didn't overflow (too late) */
   112 	if ( eip > (copy_row+sizeof(copy_row)) ) {
   113 		SDL_SetError("Copy buffer overflow");
   114 		return(-1);
   115 	}
   116 	return(0);
   117 }
   118 
   119 #else
   120 
   121 #define DEFINE_COPY_ROW(name, type)			\
   122 void name(type *src, int src_w, type *dst, int dst_w)	\
   123 {							\
   124 	int i;						\
   125 	int pos, inc;					\
   126 	type pixel = 0;					\
   127 							\
   128 	pos = 0x10000;					\
   129 	inc = (src_w << 16) / dst_w;			\
   130 	for ( i=dst_w; i>0; --i ) {			\
   131 		while ( pos >= 0x10000L ) {		\
   132 			pixel = *src++;			\
   133 			pos -= 0x10000L;		\
   134 		}					\
   135 		*dst++ = pixel;				\
   136 		pos += inc;				\
   137 	}						\
   138 }
   139 DEFINE_COPY_ROW(copy_row1, Uint8)
   140 DEFINE_COPY_ROW(copy_row2, Uint16)
   141 DEFINE_COPY_ROW(copy_row4, Uint32)
   142 
   143 #endif /* USE_ASM_STRETCH */
   144 
   145 /* The ASM code doesn't handle 24-bpp stretch blits */
   146 void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
   147 {
   148 	int i;
   149 	int pos, inc;
   150 	Uint8 pixel[3];
   151 
   152 	pos = 0x10000;
   153 	inc = (src_w << 16) / dst_w;
   154 	for ( i=dst_w; i>0; --i ) {
   155 		while ( pos >= 0x10000L ) {
   156 			pixel[0] = *src++;
   157 			pixel[1] = *src++;
   158 			pixel[2] = *src++;
   159 			pos -= 0x10000L;
   160 		}
   161 		*dst++ = pixel[0];
   162 		*dst++ = pixel[1];
   163 		*dst++ = pixel[2];
   164 		pos += inc;
   165 	}
   166 }
   167 
   168 /* Perform a stretch blit between two surfaces of the same format.
   169    NOTE:  This function is not safe to call from multiple threads!
   170 */
   171 int SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect,
   172                     SDL_Surface *dst, SDL_Rect *dstrect)
   173 {
   174 	int src_locked;
   175 	int dst_locked;
   176 	int pos, inc;
   177 	int dst_width;
   178 	int dst_maxrow;
   179 	int src_row, dst_row;
   180 	Uint8 *srcp = NULL;
   181 	Uint8 *dstp;
   182 	SDL_Rect full_src;
   183 	SDL_Rect full_dst;
   184 #if defined(USE_ASM_STRETCH) && defined(__GNUC__)
   185 	int u1, u2;
   186 #endif
   187 	const int bpp = dst->format->BytesPerPixel;
   188 
   189 	if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) {
   190 		SDL_SetError("Only works with same format surfaces");
   191 		return(-1);
   192 	}
   193 
   194 	/* Verify the blit rectangles */
   195 	if ( srcrect ) {
   196 		if ( (srcrect->x < 0) || (srcrect->y < 0) ||
   197 		     ((srcrect->x+srcrect->w) > src->w) ||
   198 		     ((srcrect->y+srcrect->h) > src->h) ) {
   199 			SDL_SetError("Invalid source blit rectangle");
   200 			return(-1);
   201 		}
   202 	} else {
   203 		full_src.x = 0;
   204 		full_src.y = 0;
   205 		full_src.w = src->w;
   206 		full_src.h = src->h;
   207 		srcrect = &full_src;
   208 	}
   209 	if ( dstrect ) {
   210 		if ( (dstrect->x < 0) || (dstrect->y < 0) ||
   211 		     ((dstrect->x+dstrect->w) > dst->w) ||
   212 		     ((dstrect->y+dstrect->h) > dst->h) ) {
   213 			SDL_SetError("Invalid destination blit rectangle");
   214 			return(-1);
   215 		}
   216 	} else {
   217 		full_dst.x = 0;
   218 		full_dst.y = 0;
   219 		full_dst.w = dst->w;
   220 		full_dst.h = dst->h;
   221 		dstrect = &full_dst;
   222 	}
   223 
   224 	/* Lock the destination if it's in hardware */
   225 	dst_locked = 0;
   226 	if ( SDL_MUSTLOCK(dst) ) {
   227 		if ( SDL_LockSurface(dst) < 0 ) {
   228 			SDL_SetError("Unable to lock destination surface");
   229 			return(-1);
   230 		}
   231 		dst_locked = 1;
   232 	}
   233 	/* Lock the source if it's in hardware */
   234 	src_locked = 0;
   235 	if ( SDL_MUSTLOCK(src) ) {
   236 		if ( SDL_LockSurface(src) < 0 ) {
   237 			if ( dst_locked ) {
   238 				SDL_UnlockSurface(dst);
   239 			}
   240 			SDL_SetError("Unable to lock source surface");
   241 			return(-1);
   242 		}
   243 		src_locked = 1;
   244 	}
   245 
   246 	/* Set up the data... */
   247 	pos = 0x10000;
   248 	inc = (srcrect->h << 16) / dstrect->h;
   249 	src_row = srcrect->y;
   250 	dst_row = dstrect->y;
   251 	dst_width = dstrect->w*bpp;
   252 
   253 #ifdef USE_ASM_STRETCH
   254 	/* Write the opcodes for this stretch */
   255 	if ( (bpp != 3) &&
   256 	     (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0) ) {
   257 		return(-1);
   258 	}
   259 #endif
   260 
   261 	/* Perform the stretch blit */
   262 	for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
   263 		dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
   264 		                            + (dstrect->x*bpp);
   265 		while ( pos >= 0x10000L ) {
   266 			srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
   267 			                            + (srcrect->x*bpp);
   268 			++src_row;
   269 			pos -= 0x10000L;
   270 		}
   271 #ifdef USE_ASM_STRETCH
   272 		switch (bpp) {
   273 		    case 3:
   274 			copy_row3(srcp, srcrect->w, dstp, dstrect->w);
   275 			break;
   276 		    default:
   277 #ifdef __GNUC__
   278 			__asm__ __volatile__ (
   279 			"call *%4"
   280 			: "=&D" (u1), "=&S" (u2)
   281 			: "0" (dstp), "1" (srcp), "r" (copy_row)
   282 			: "memory" );
   283 #else
   284 #ifdef _MSC_VER
   285 		{ void *code = copy_row;
   286 			__asm {
   287 				push edi
   288 				push esi
   289 	
   290 				mov edi, dstp
   291 				mov esi, srcp
   292 				call dword ptr code
   293 
   294 				pop esi
   295 				pop edi
   296 			}
   297 		}
   298 #else
   299 #error Need inline assembly for this compiler
   300 #endif
   301 #endif /* __GNUC__ */
   302 			break;
   303 		}
   304 #else
   305 		switch (bpp) {
   306 		    case 1:
   307 			copy_row1(srcp, srcrect->w, dstp, dstrect->w);
   308 			break;
   309 		    case 2:
   310 			copy_row2((Uint16 *)srcp, srcrect->w,
   311 			          (Uint16 *)dstp, dstrect->w);
   312 			break;
   313 		    case 3:
   314 			copy_row3(srcp, srcrect->w, dstp, dstrect->w);
   315 			break;
   316 		    case 4:
   317 			copy_row4((Uint32 *)srcp, srcrect->w,
   318 			          (Uint32 *)dstp, dstrect->w);
   319 			break;
   320 		}
   321 #endif
   322 		pos += inc;
   323 	}
   324 
   325 	/* We need to unlock the surfaces if they're locked */
   326 	if ( dst_locked ) {
   327 		SDL_UnlockSurface(dst);
   328 	}
   329 	if ( src_locked ) {
   330 		SDL_UnlockSurface(src);
   331 	}
   332 	return(0);
   333 }
   334