src/video/SDL_stretch.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     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 DEFINE_COPY_ROW(copy_row1, Uint8)
   143     DEFINE_COPY_ROW(copy_row2, Uint16) DEFINE_COPY_ROW(copy_row4, Uint32)
   144 #endif /* USE_ASM_STRETCH */
   145 /* The ASM code doesn't handle 24-bpp stretch blits */
   146      void
   147      copy_row3(Uint8 * src, int src_w, Uint8 * dst, int dst_w)
   148 {
   149     int i;
   150     int pos, inc;
   151     Uint8 pixel[3];
   152 
   153     pos = 0x10000;
   154     inc = (src_w << 16) / dst_w;
   155     for (i = dst_w; i > 0; --i) {
   156         while (pos >= 0x10000L) {
   157             pixel[0] = *src++;
   158             pixel[1] = *src++;
   159             pixel[2] = *src++;
   160             pos -= 0x10000L;
   161         }
   162         *dst++ = pixel[0];
   163         *dst++ = pixel[1];
   164         *dst++ = pixel[2];
   165         pos += inc;
   166     }
   167 }
   168 
   169 /* Perform a stretch blit between two surfaces of the same format.
   170    NOTE:  This function is not safe to call from multiple threads!
   171 */
   172 int
   173 SDL_SoftStretch(SDL_Surface * src, SDL_Rect * srcrect,
   174                 SDL_Surface * dst, SDL_Rect * dstrect)
   175 {
   176     int src_locked;
   177     int dst_locked;
   178     int pos, inc;
   179     int dst_width;
   180     int dst_maxrow;
   181     int src_row, dst_row;
   182     Uint8 *srcp = NULL;
   183     Uint8 *dstp;
   184     SDL_Rect full_src;
   185     SDL_Rect full_dst;
   186 #if defined(USE_ASM_STRETCH) && defined(__GNUC__)
   187     int u1, u2;
   188 #endif
   189     const int bpp = dst->format->BytesPerPixel;
   190 
   191     if (src->format->BitsPerPixel != dst->format->BitsPerPixel) {
   192         SDL_SetError("Only works with same format surfaces");
   193         return (-1);
   194     }
   195 
   196     /* Verify the blit rectangles */
   197     if (srcrect) {
   198         if ((srcrect->x < 0) || (srcrect->y < 0) ||
   199             ((srcrect->x + srcrect->w) > src->w) ||
   200             ((srcrect->y + srcrect->h) > src->h)) {
   201             SDL_SetError("Invalid source blit rectangle");
   202             return (-1);
   203         }
   204     } else {
   205         full_src.x = 0;
   206         full_src.y = 0;
   207         full_src.w = src->w;
   208         full_src.h = src->h;
   209         srcrect = &full_src;
   210     }
   211     if (dstrect) {
   212         if ((dstrect->x < 0) || (dstrect->y < 0) ||
   213             ((dstrect->x + dstrect->w) > dst->w) ||
   214             ((dstrect->y + dstrect->h) > dst->h)) {
   215             SDL_SetError("Invalid destination blit rectangle");
   216             return (-1);
   217         }
   218     } else {
   219         full_dst.x = 0;
   220         full_dst.y = 0;
   221         full_dst.w = dst->w;
   222         full_dst.h = dst->h;
   223         dstrect = &full_dst;
   224     }
   225 
   226     /* Lock the destination if it's in hardware */
   227     dst_locked = 0;
   228     if (SDL_MUSTLOCK(dst)) {
   229         if (SDL_LockSurface(dst) < 0) {
   230             SDL_SetError("Unable to lock destination surface");
   231             return (-1);
   232         }
   233         dst_locked = 1;
   234     }
   235     /* Lock the source if it's in hardware */
   236     src_locked = 0;
   237     if (SDL_MUSTLOCK(src)) {
   238         if (SDL_LockSurface(src) < 0) {
   239             if (dst_locked) {
   240                 SDL_UnlockSurface(dst);
   241             }
   242             SDL_SetError("Unable to lock source surface");
   243             return (-1);
   244         }
   245         src_locked = 1;
   246     }
   247 
   248     /* Set up the data... */
   249     pos = 0x10000;
   250     inc = (srcrect->h << 16) / dstrect->h;
   251     src_row = srcrect->y;
   252     dst_row = dstrect->y;
   253     dst_width = dstrect->w * bpp;
   254 
   255 #ifdef USE_ASM_STRETCH
   256     /* Write the opcodes for this stretch */
   257     if ((bpp != 3) && (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
   258         return (-1);
   259     }
   260 #endif
   261 
   262     /* Perform the stretch blit */
   263     for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
   264         dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
   265             + (dstrect->x * bpp);
   266         while (pos >= 0x10000L) {
   267             srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
   268                 + (srcrect->x * bpp);
   269             ++src_row;
   270             pos -= 0x10000L;
   271         }
   272 #ifdef USE_ASM_STRETCH
   273         switch (bpp) {
   274         case 3:
   275             copy_row3(srcp, srcrect->w, dstp, dstrect->w);
   276             break;
   277         default:
   278 #ifdef __GNUC__
   279           __asm__ __volatile__("call *%4": "=&D"(u1), "=&S"(u2): "0"(dstp), "1"(srcp), "r"(copy_row):"memory");
   280 #elif defined(_MSC_VER) || defined(__WATCOMC__)
   281             {
   282                 void *code = copy_row;
   283                 __asm {
   284                 push edi
   285                         push esi
   286                         mov edi, dstp
   287                         mov esi, srcp call dword ptr code pop esi pop edi}
   288             }
   289 #else
   290 #error Need inline assembly for this compiler
   291 #endif
   292             break;
   293         }
   294 #else
   295         switch (bpp) {
   296         case 1:
   297             copy_row1(srcp, srcrect->w, dstp, dstrect->w);
   298             break;
   299         case 2:
   300             copy_row2((Uint16 *) srcp, srcrect->w,
   301                       (Uint16 *) dstp, dstrect->w);
   302             break;
   303         case 3:
   304             copy_row3(srcp, srcrect->w, dstp, dstrect->w);
   305             break;
   306         case 4:
   307             copy_row4((Uint32 *) srcp, srcrect->w,
   308                       (Uint32 *) dstp, dstrect->w);
   309             break;
   310         }
   311 #endif
   312         pos += inc;
   313     }
   314 
   315     /* We need to unlock the surfaces if they're locked */
   316     if (dst_locked) {
   317         SDL_UnlockSurface(dst);
   318     }
   319     if (src_locked) {
   320         SDL_UnlockSurface(src);
   321     }
   322     return (0);
   323 }
   324 
   325 /* vi: set ts=4 sw=4 expandtab: */