Commit 7df7cdde authored by Sam Lantinga's avatar Sam Lantinga

Merged improvements to SDL_SoftStretch() from SDL 1.2

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%404113
parent 410baa46
...@@ -204,6 +204,15 @@ if test x$enable_libc = xyes; then ...@@ -204,6 +204,15 @@ if test x$enable_libc = xyes; then
if test x$ac_cv_func_strtod = xyes; then if test x$ac_cv_func_strtod = xyes; then
AC_DEFINE(HAVE_STRTOD) AC_DEFINE(HAVE_STRTOD)
fi fi
AC_CHECK_FUNC(mprotect,
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/mman.h>
],[
],[
AC_DEFINE(HAVE_MPROTECT)
]),
)
AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep) AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep)
AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"]) AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
......
...@@ -151,6 +151,7 @@ ...@@ -151,6 +151,7 @@
#undef HAVE_NANOSLEEP #undef HAVE_NANOSLEEP
#undef HAVE_CLOCK_GETTIME #undef HAVE_CLOCK_GETTIME
#undef HAVE_GETPAGESIZE #undef HAVE_GETPAGESIZE
#undef HAVE_MPROTECT
#else #else
/* We may need some replacement for stdarg.h here */ /* We may need some replacement for stdarg.h here */
......
...@@ -42,6 +42,16 @@ ...@@ -42,6 +42,16 @@
#ifdef USE_ASM_STRETCH #ifdef USE_ASM_STRETCH
#ifdef HAVE_MPROTECT
#include <sys/types.h>
#include <sys/mman.h>
#endif
#ifdef __GNUC__
#define PAGE_ALIGNED __attribute__((__aligned__(4096)))
#else
#define PAGE_ALIGNED
#endif
#if defined(_M_IX86) || defined(i386) #if defined(_M_IX86) || defined(i386)
#define PREFIX16 0x66 #define PREFIX16 0x66
#define STORE_BYTE 0xAA #define STORE_BYTE 0xAA
...@@ -53,7 +63,7 @@ ...@@ -53,7 +63,7 @@
#error Need assembly opcodes for this architecture #error Need assembly opcodes for this architecture
#endif #endif
static unsigned char copy_row[4096]; static unsigned char copy_row[4096] PAGE_ALIGNED;
static int static int
generate_rowbytes(int src_w, int dst_w, int bpp) generate_rowbytes(int src_w, int dst_w, int bpp)
...@@ -63,6 +73,7 @@ generate_rowbytes(int src_w, int dst_w, int bpp) ...@@ -63,6 +73,7 @@ generate_rowbytes(int src_w, int dst_w, int bpp)
int bpp; int bpp;
int src_w; int src_w;
int dst_w; int dst_w;
int status;
} last; } last;
int i; int i;
...@@ -72,11 +83,12 @@ generate_rowbytes(int src_w, int dst_w, int bpp) ...@@ -72,11 +83,12 @@ generate_rowbytes(int src_w, int dst_w, int bpp)
/* See if we need to regenerate the copy buffer */ /* See if we need to regenerate the copy buffer */
if ((src_w == last.src_w) && (dst_w == last.dst_w) && (bpp == last.bpp)) { if ((src_w == last.src_w) && (dst_w == last.dst_w) && (bpp == last.bpp)) {
return (0); return (last.status);
} }
last.bpp = bpp; last.bpp = bpp;
last.src_w = src_w; last.src_w = src_w;
last.dst_w = dst_w; last.dst_w = dst_w;
last.status = -1;
switch (bpp) { switch (bpp) {
case 1: case 1:
...@@ -92,6 +104,13 @@ generate_rowbytes(int src_w, int dst_w, int bpp) ...@@ -92,6 +104,13 @@ generate_rowbytes(int src_w, int dst_w, int bpp)
SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp); SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
return (-1); return (-1);
} }
#ifdef HAVE_MPROTECT
/* Make the code writeable */
if (mprotect(copy_row, sizeof(copy_row), PROT_READ | PROT_WRITE) < 0) {
SDL_SetError("Couldn't make copy buffer writeable");
return (-1);
}
#endif
pos = 0x10000; pos = 0x10000;
inc = (src_w << 16) / dst_w; inc = (src_w << 16) / dst_w;
eip = copy_row; eip = copy_row;
...@@ -111,47 +130,55 @@ generate_rowbytes(int src_w, int dst_w, int bpp) ...@@ -111,47 +130,55 @@ generate_rowbytes(int src_w, int dst_w, int bpp)
} }
*eip++ = RETURN; *eip++ = RETURN;
/* Verify that we didn't overflow (too late) */ /* Verify that we didn't overflow (too late!!!) */
if (eip > (copy_row + sizeof(copy_row))) { if (eip > (copy_row + sizeof(copy_row))) {
SDL_SetError("Copy buffer overflow"); SDL_SetError("Copy buffer overflow");
return (-1); return (-1);
} }
#ifdef HAVE_MPROTECT
/* Make the code executable but not writeable */
if (mprotect(copy_row, sizeof(copy_row), PROT_READ | PROT_EXEC) < 0) {
SDL_SetError("Couldn't make copy buffer executable");
return (-1);
}
#endif
last.status = 0;
return (0); return (0);
} }
#else #endif /* USE_ASM_STRETCH */
#define DEFINE_COPY_ROW(name, type) \ #define DEFINE_COPY_ROW(name, type) \
void name(type *src, int src_w, type *dst, int dst_w) \ void name(type *src, int src_w, type *dst, int dst_w) \
{ \ { \
int i; \ int i; \
int pos, inc; \ int pos, inc; \
type pixel = 0; \ type pixel = 0; \
\ \
pos = 0x10000; \ pos = 0x10000; \
inc = (src_w << 16) / dst_w; \ inc = (src_w << 16) / dst_w; \
for ( i=dst_w; i>0; --i ) { \ for ( i=dst_w; i>0; --i ) { \
while ( pos >= 0x10000L ) { \ while ( pos >= 0x10000L ) { \
pixel = *src++; \ pixel = *src++; \
pos -= 0x10000L; \ pos -= 0x10000L; \
} \ } \
*dst++ = pixel; \ *dst++ = pixel; \
pos += inc; \ pos += inc; \
} \ } \
} }
/* *INDENT-OFF* */ /* *INDENT-OFF* */
DEFINE_COPY_ROW(copy_row1, Uint8) DEFINE_COPY_ROW(copy_row1, Uint8)
DEFINE_COPY_ROW(copy_row2, Uint16) DEFINE_COPY_ROW(copy_row2, Uint16)
DEFINE_COPY_ROW(copy_row4, Uint32) DEFINE_COPY_ROW(copy_row4, Uint32)
/* *INDENT-ON* */ /* *INDENT-ON* */
#endif /* USE_ASM_STRETCH */
/* The ASM code doesn't handle 24-bpp stretch blits */ /* The ASM code doesn't handle 24-bpp stretch blits */
void void
copy_row3(Uint8 * src, int src_w, Uint8 * dst, int dst_w) copy_row3(Uint8 * src, int src_w, Uint8 * dst, int dst_w)
{ {
int i; int i;
int pos, inc; int pos, inc;
Uint8 pixel[3]; Uint8 pixel[3] = { 0, 0, 0 };
pos = 0x10000; pos = 0x10000;
inc = (src_w << 16) / dst_w; inc = (src_w << 16) / dst_w;
...@@ -186,9 +213,12 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, ...@@ -186,9 +213,12 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
Uint8 *dstp; Uint8 *dstp;
SDL_Rect full_src; SDL_Rect full_src;
SDL_Rect full_dst; SDL_Rect full_dst;
#if defined(USE_ASM_STRETCH) && defined(__GNUC__) #ifdef USE_ASM_STRETCH
SDL_bool use_asm = SDL_TRUE;
#ifdef __GNUC__
int u1, u2; int u1, u2;
#endif #endif
#endif /* USE_ASM_STRETCH */
const int bpp = dst->format->BytesPerPixel; const int bpp = dst->format->BytesPerPixel;
if (src->format->BitsPerPixel != dst->format->BitsPerPixel) { if (src->format->BitsPerPixel != dst->format->BitsPerPixel) {
...@@ -257,8 +287,8 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, ...@@ -257,8 +287,8 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
#ifdef USE_ASM_STRETCH #ifdef USE_ASM_STRETCH
/* Write the opcodes for this stretch */ /* Write the opcodes for this stretch */
if ((bpp != 3) && (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) { if ((bpp == 3) || (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
return (-1); use_asm = SDL_FALSE;
} }
#endif #endif
...@@ -273,13 +303,11 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, ...@@ -273,13 +303,11 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
pos -= 0x10000L; pos -= 0x10000L;
} }
#ifdef USE_ASM_STRETCH #ifdef USE_ASM_STRETCH
switch (bpp) { if (use_asm) {
case 3:
copy_row3(srcp, srcrect->w, dstp, dstrect->w);
break;
default:
#ifdef __GNUC__ #ifdef __GNUC__
__asm__ __volatile__("call *%4": "=&D"(u1), "=&S"(u2): "0"(dstp), "1"(srcp), "r"(copy_row):"memory"); __asm__ __volatile__("call *%4":"=&D"(u1), "=&S"(u2)
:"0"(dstp), "1"(srcp), "r"(copy_row)
:"memory");
#elif defined(_MSC_VER) || defined(__WATCOMC__) #elif defined(_MSC_VER) || defined(__WATCOMC__)
/* *INDENT-OFF* */ /* *INDENT-OFF* */
{ {
...@@ -298,26 +326,24 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, ...@@ -298,26 +326,24 @@ SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
#else #else
#error Need inline assembly for this compiler #error Need inline assembly for this compiler
#endif #endif
break; } else
}
#else
switch (bpp) {
case 1:
copy_row1(srcp, srcrect->w, dstp, dstrect->w);
break;
case 2:
copy_row2((Uint16 *) srcp, srcrect->w,
(Uint16 *) dstp, dstrect->w);
break;
case 3:
copy_row3(srcp, srcrect->w, dstp, dstrect->w);
break;
case 4:
copy_row4((Uint32 *) srcp, srcrect->w,
(Uint32 *) dstp, dstrect->w);
break;
}
#endif #endif
switch (bpp) {
case 1:
copy_row1(srcp, srcrect->w, dstp, dstrect->w);
break;
case 2:
copy_row2((Uint16 *) srcp, srcrect->w,
(Uint16 *) dstp, dstrect->w);
break;
case 3:
copy_row3(srcp, srcrect->w, dstp, dstrect->w);
break;
case 4:
copy_row4((Uint32 *) srcp, srcrect->w,
(Uint32 *) dstp, dstrect->w);
break;
}
pos += inc; pos += inc;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment