src/video/SDL_stretch.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 18 Oct 2009 23:21:15 +0000
changeset 3406 8ae607392409
parent 3405 d5f2dd33f4eb
child 3697 f7b03b6838cb
permissions -rw-r--r--
There's a bug with gcc 4.4.1 and -O2 where srcp doesn't get the correct value after the first scanline. Ugh.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 /* There's a bug with gcc 4.4.1 and -O2 where srcp doesn't get the correct
    41  * value after the first scanline.  FIXME? */
    42 /*#define USE_ASM_STRETCH*/
    43 #endif
    44 
    45 #ifdef USE_ASM_STRETCH
    46 
    47 #ifdef HAVE_MPROTECT
    48 #include <sys/types.h>
    49 #include <sys/mman.h>
    50 #endif
    51 #ifdef __GNUC__
    52 #define PAGE_ALIGNED __attribute__((__aligned__(4096)))
    53 #else
    54 #define PAGE_ALIGNED
    55 #endif
    56 
    57 #if defined(_M_IX86) || defined(i386)
    58 #define PREFIX16	0x66
    59 #define STORE_BYTE	0xAA
    60 #define STORE_WORD	0xAB
    61 #define LOAD_BYTE	0xAC
    62 #define LOAD_WORD	0xAD
    63 #define RETURN		0xC3
    64 #else
    65 #error Need assembly opcodes for this architecture
    66 #endif
    67 
    68 static unsigned char copy_row[4096] PAGE_ALIGNED;
    69 
    70 static int
    71 generate_rowbytes(int src_w, int dst_w, int bpp)
    72 {
    73     static struct
    74     {
    75         int bpp;
    76         int src_w;
    77         int dst_w;
    78         int status;
    79     } last;
    80 
    81     int i;
    82     int pos, inc;
    83     unsigned char *eip;
    84     unsigned char load, store;
    85 
    86     /* See if we need to regenerate the copy buffer */
    87     if ((src_w == last.src_w) && (dst_w == last.dst_w) && (bpp == last.bpp)) {
    88         return (last.status);
    89     }
    90     last.bpp = bpp;
    91     last.src_w = src_w;
    92     last.dst_w = dst_w;
    93     last.status = -1;
    94 
    95     switch (bpp) {
    96     case 1:
    97         load = LOAD_BYTE;
    98         store = STORE_BYTE;
    99         break;
   100     case 2:
   101     case 4:
   102         load = LOAD_WORD;
   103         store = STORE_WORD;
   104         break;
   105     default:
   106         SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
   107         return (-1);
   108     }
   109 #ifdef HAVE_MPROTECT
   110     /* Make the code writeable */
   111     if (mprotect(copy_row, sizeof(copy_row), PROT_READ | PROT_WRITE) < 0) {
   112         SDL_SetError("Couldn't make copy buffer writeable");
   113         return (-1);
   114     }
   115 #endif
   116     pos = 0x10000;
   117     inc = (src_w << 16) / dst_w;
   118     eip = copy_row;
   119     for (i = 0; i < dst_w; ++i) {
   120         while (pos >= 0x10000L) {
   121             if (bpp == 2) {
   122                 *eip++ = PREFIX16;
   123             }
   124             *eip++ = load;
   125             pos -= 0x10000L;
   126         }
   127         if (bpp == 2) {
   128             *eip++ = PREFIX16;
   129         }
   130         *eip++ = store;
   131         pos += inc;
   132     }
   133     *eip++ = RETURN;
   134 
   135     /* Verify that we didn't overflow (too late!!!) */
   136     if (eip > (copy_row + sizeof(copy_row))) {
   137         SDL_SetError("Copy buffer overflow");
   138         return (-1);
   139     }
   140 #ifdef HAVE_MPROTECT
   141     /* Make the code executable but not writeable */
   142     if (mprotect(copy_row, sizeof(copy_row), PROT_READ | PROT_EXEC) < 0) {
   143         SDL_SetError("Couldn't make copy buffer executable");
   144         return (-1);
   145     }
   146 #endif
   147     last.status = 0;
   148     return (0);
   149 }
   150 
   151 #endif /* USE_ASM_STRETCH */
   152 
   153 #define DEFINE_COPY_ROW(name, type)			\
   154 void name(type *src, int src_w, type *dst, int dst_w)	\
   155 {							\
   156 	int i;						\
   157 	int pos, inc;					\
   158 	type pixel = 0;					\
   159 							\
   160 	pos = 0x10000;					\
   161 	inc = (src_w << 16) / dst_w;			\
   162 	for ( i=dst_w; i>0; --i ) {			\
   163 		while ( pos >= 0x10000L ) {		\
   164 			pixel = *src++;			\
   165 			pos -= 0x10000L;		\
   166 		}					\
   167 		*dst++ = pixel;				\
   168 		pos += inc;				\
   169 	}						\
   170 }
   171 /* *INDENT-OFF* */
   172 DEFINE_COPY_ROW(copy_row1, Uint8)
   173 DEFINE_COPY_ROW(copy_row2, Uint16)
   174 DEFINE_COPY_ROW(copy_row4, Uint32)
   175 /* *INDENT-ON* */
   176 
   177 /* The ASM code doesn't handle 24-bpp stretch blits */
   178 void
   179 copy_row3(Uint8 * src, int src_w, Uint8 * dst, int dst_w)
   180 {
   181     int i;
   182     int pos, inc;
   183     Uint8 pixel[3] = { 0, 0, 0 };
   184 
   185     pos = 0x10000;
   186     inc = (src_w << 16) / dst_w;
   187     for (i = dst_w; i > 0; --i) {
   188         while (pos >= 0x10000L) {
   189             pixel[0] = *src++;
   190             pixel[1] = *src++;
   191             pixel[2] = *src++;
   192             pos -= 0x10000L;
   193         }
   194         *dst++ = pixel[0];
   195         *dst++ = pixel[1];
   196         *dst++ = pixel[2];
   197         pos += inc;
   198     }
   199 }
   200 
   201 /* Perform a stretch blit between two surfaces of the same format.
   202    NOTE:  This function is not safe to call from multiple threads!
   203 */
   204 int
   205 SDL_SoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
   206                 SDL_Surface * dst, const SDL_Rect * dstrect)
   207 {
   208     int src_locked;
   209     int dst_locked;
   210     int pos, inc;
   211     int dst_width;
   212     int dst_maxrow;
   213     int src_row, dst_row;
   214     Uint8 *srcp = NULL;
   215     Uint8 *dstp;
   216     SDL_Rect full_src;
   217     SDL_Rect full_dst;
   218 #ifdef USE_ASM_STRETCH
   219     SDL_bool use_asm = SDL_TRUE;
   220 #ifdef __GNUC__
   221     int u1, u2;
   222 #endif
   223 #endif /* USE_ASM_STRETCH */
   224     const int bpp = dst->format->BytesPerPixel;
   225 
   226     if (src->format->BitsPerPixel != dst->format->BitsPerPixel) {
   227         SDL_SetError("Only works with same format surfaces");
   228         return (-1);
   229     }
   230 
   231     /* Verify the blit rectangles */
   232     if (srcrect) {
   233         if ((srcrect->x < 0) || (srcrect->y < 0) ||
   234             ((srcrect->x + srcrect->w) > src->w) ||
   235             ((srcrect->y + srcrect->h) > src->h)) {
   236             SDL_SetError("Invalid source blit rectangle");
   237             return (-1);
   238         }
   239     } else {
   240         full_src.x = 0;
   241         full_src.y = 0;
   242         full_src.w = src->w;
   243         full_src.h = src->h;
   244         srcrect = &full_src;
   245     }
   246     if (dstrect) {
   247         if ((dstrect->x < 0) || (dstrect->y < 0) ||
   248             ((dstrect->x + dstrect->w) > dst->w) ||
   249             ((dstrect->y + dstrect->h) > dst->h)) {
   250             SDL_SetError("Invalid destination blit rectangle");
   251             return (-1);
   252         }
   253     } else {
   254         full_dst.x = 0;
   255         full_dst.y = 0;
   256         full_dst.w = dst->w;
   257         full_dst.h = dst->h;
   258         dstrect = &full_dst;
   259     }
   260 
   261     /* Lock the destination if it's in hardware */
   262     dst_locked = 0;
   263     if (SDL_MUSTLOCK(dst)) {
   264         if (SDL_LockSurface(dst) < 0) {
   265             SDL_SetError("Unable to lock destination surface");
   266             return (-1);
   267         }
   268         dst_locked = 1;
   269     }
   270     /* Lock the source if it's in hardware */
   271     src_locked = 0;
   272     if (SDL_MUSTLOCK(src)) {
   273         if (SDL_LockSurface(src) < 0) {
   274             if (dst_locked) {
   275                 SDL_UnlockSurface(dst);
   276             }
   277             SDL_SetError("Unable to lock source surface");
   278             return (-1);
   279         }
   280         src_locked = 1;
   281     }
   282 
   283     /* Set up the data... */
   284     pos = 0x10000;
   285     inc = (srcrect->h << 16) / dstrect->h;
   286     src_row = srcrect->y;
   287     dst_row = dstrect->y;
   288     dst_width = dstrect->w * bpp;
   289 
   290 #ifdef USE_ASM_STRETCH
   291     /* Write the opcodes for this stretch */
   292     if ((bpp == 3) || (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
   293         use_asm = SDL_FALSE;
   294     }
   295 #endif
   296 
   297     /* Perform the stretch blit */
   298     for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
   299         dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
   300             + (dstrect->x * bpp);
   301         while (pos >= 0x10000L) {
   302             srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
   303                 + (srcrect->x * bpp);
   304             ++src_row;
   305             pos -= 0x10000L;
   306         }
   307 #ifdef USE_ASM_STRETCH
   308         if (use_asm) {
   309 #ifdef __GNUC__
   310             __asm__ __volatile__("call *%4":"=&D"(u1), "=&S"(u2)
   311                                  :"0"(dstp), "1"(srcp), "r"(copy_row)
   312                                  :"memory");
   313 #elif defined(_MSC_VER) || defined(__WATCOMC__)
   314             /* *INDENT-OFF* */
   315             {
   316                 void *code = copy_row;
   317                 __asm {
   318                     push edi
   319                     push esi
   320                     mov edi, dstp
   321                     mov esi, srcp
   322                     call dword ptr code
   323                     pop esi
   324                     pop edi
   325                 }
   326             }
   327             /* *INDENT-ON* */
   328 #else
   329 #error Need inline assembly for this compiler
   330 #endif
   331         } else
   332 #endif
   333             switch (bpp) {
   334             case 1:
   335                 copy_row1(srcp, srcrect->w, dstp, dstrect->w);
   336                 break;
   337             case 2:
   338                 copy_row2((Uint16 *) srcp, srcrect->w,
   339                           (Uint16 *) dstp, dstrect->w);
   340                 break;
   341             case 3:
   342                 copy_row3(srcp, srcrect->w, dstp, dstrect->w);
   343                 break;
   344             case 4:
   345                 copy_row4((Uint32 *) srcp, srcrect->w,
   346                           (Uint32 *) dstp, dstrect->w);
   347                 break;
   348             }
   349         pos += inc;
   350     }
   351 
   352     /* We need to unlock the surfaces if they're locked */
   353     if (dst_locked) {
   354         SDL_UnlockSurface(dst);
   355     }
   356     if (src_locked) {
   357         SDL_UnlockSurface(src);
   358     }
   359     return (0);
   360 }
   361 
   362 /* vi: set ts=4 sw=4 expandtab: */