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