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