src/video/SDL_stretch.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1849 b5a4ac87b98c
child 1985 8055185ae4ed
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This a stretch blit implementation based on ideas given to me by
slouken@0
    25
   Tomasz Cejner - thanks! :)
slouken@0
    26
slouken@0
    27
   April 27, 2000 - Sam Lantinga
slouken@0
    28
*/
slouken@0
    29
slouken@0
    30
#include "SDL_video.h"
slouken@0
    31
#include "SDL_blit.h"
slouken@0
    32
slouken@0
    33
/* This isn't ready for general consumption yet - it should be folded
slouken@0
    34
   into the general blitting mechanism.
slouken@0
    35
*/
slouken@0
    36
slouken@1361
    37
#if ((defined(_MFC_VER) && defined(_M_IX86)/* && !defined(_WIN32_WCE) still needed? */) || \
slouken@1442
    38
     defined(__WATCOMC__) || \
slouken@1402
    39
     (defined(__GNUC__) && defined(__i386__))) && SDL_ASSEMBLY_ROUTINES
slouken@0
    40
#define USE_ASM_STRETCH
slouken@0
    41
#endif
slouken@0
    42
slouken@0
    43
#ifdef USE_ASM_STRETCH
slouken@0
    44
slouken@1361
    45
#if defined(_M_IX86) || defined(i386)
slouken@0
    46
#define PREFIX16	0x66
slouken@0
    47
#define STORE_BYTE	0xAA
slouken@0
    48
#define STORE_WORD	0xAB
slouken@0
    49
#define LOAD_BYTE	0xAC
slouken@0
    50
#define LOAD_WORD	0xAD
slouken@0
    51
#define RETURN		0xC3
slouken@0
    52
#else
slouken@0
    53
#error Need assembly opcodes for this architecture
slouken@0
    54
#endif
slouken@0
    55
slouken@0
    56
static unsigned char copy_row[4096];
slouken@0
    57
slouken@1895
    58
static int
slouken@1895
    59
generate_rowbytes(int src_w, int dst_w, int bpp)
slouken@0
    60
{
slouken@1895
    61
    static struct
slouken@1895
    62
    {
slouken@1895
    63
        int bpp;
slouken@1895
    64
        int src_w;
slouken@1895
    65
        int dst_w;
slouken@1895
    66
    } last;
slouken@0
    67
slouken@1895
    68
    int i;
slouken@1895
    69
    int pos, inc;
slouken@1895
    70
    unsigned char *eip;
slouken@1895
    71
    unsigned char load, store;
slouken@0
    72
slouken@1895
    73
    /* See if we need to regenerate the copy buffer */
slouken@1895
    74
    if ((src_w == last.src_w) && (dst_w == last.dst_w) && (bpp == last.bpp)) {
slouken@1895
    75
        return (0);
slouken@1895
    76
    }
slouken@1895
    77
    last.bpp = bpp;
slouken@1895
    78
    last.src_w = src_w;
slouken@1895
    79
    last.dst_w = dst_w;
slouken@0
    80
slouken@1895
    81
    switch (bpp) {
slouken@1895
    82
    case 1:
slouken@1895
    83
        load = LOAD_BYTE;
slouken@1895
    84
        store = STORE_BYTE;
slouken@1895
    85
        break;
slouken@1895
    86
    case 2:
slouken@1895
    87
    case 4:
slouken@1895
    88
        load = LOAD_WORD;
slouken@1895
    89
        store = STORE_WORD;
slouken@1895
    90
        break;
slouken@1895
    91
    default:
slouken@1895
    92
        SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
slouken@1895
    93
        return (-1);
slouken@1895
    94
    }
slouken@1895
    95
    pos = 0x10000;
slouken@1895
    96
    inc = (src_w << 16) / dst_w;
slouken@1895
    97
    eip = copy_row;
slouken@1895
    98
    for (i = 0; i < dst_w; ++i) {
slouken@1895
    99
        while (pos >= 0x10000L) {
slouken@1895
   100
            if (bpp == 2) {
slouken@1895
   101
                *eip++ = PREFIX16;
slouken@1895
   102
            }
slouken@1895
   103
            *eip++ = load;
slouken@1895
   104
            pos -= 0x10000L;
slouken@1895
   105
        }
slouken@1895
   106
        if (bpp == 2) {
slouken@1895
   107
            *eip++ = PREFIX16;
slouken@1895
   108
        }
slouken@1895
   109
        *eip++ = store;
slouken@1895
   110
        pos += inc;
slouken@1895
   111
    }
slouken@1895
   112
    *eip++ = RETURN;
slouken@0
   113
slouken@1895
   114
    /* Verify that we didn't overflow (too late) */
slouken@1895
   115
    if (eip > (copy_row + sizeof(copy_row))) {
slouken@1895
   116
        SDL_SetError("Copy buffer overflow");
slouken@1895
   117
        return (-1);
slouken@1895
   118
    }
slouken@1895
   119
    return (0);
slouken@0
   120
}
slouken@0
   121
slouken@0
   122
#else
slouken@0
   123
slouken@0
   124
#define DEFINE_COPY_ROW(name, type)			\
slouken@0
   125
void name(type *src, int src_w, type *dst, int dst_w)	\
slouken@0
   126
{							\
slouken@0
   127
	int i;						\
slouken@0
   128
	int pos, inc;					\
slouken@0
   129
	type pixel = 0;					\
slouken@0
   130
							\
slouken@0
   131
	pos = 0x10000;					\
slouken@0
   132
	inc = (src_w << 16) / dst_w;			\
slouken@0
   133
	for ( i=dst_w; i>0; --i ) {			\
slouken@0
   134
		while ( pos >= 0x10000L ) {		\
slouken@0
   135
			pixel = *src++;			\
slouken@0
   136
			pos -= 0x10000L;		\
slouken@0
   137
		}					\
slouken@0
   138
		*dst++ = pixel;				\
slouken@0
   139
		pos += inc;				\
slouken@0
   140
	}						\
slouken@0
   141
}
slouken@0
   142
DEFINE_COPY_ROW(copy_row1, Uint8)
slouken@1895
   143
    DEFINE_COPY_ROW(copy_row2, Uint16) DEFINE_COPY_ROW(copy_row4, Uint32)
slouken@1895
   144
#endif /* USE_ASM_STRETCH */
slouken@1895
   145
/* The ASM code doesn't handle 24-bpp stretch blits */
slouken@1895
   146
     void
slouken@1895
   147
     copy_row3(Uint8 * src, int src_w, Uint8 * dst, int dst_w)
slouken@1895
   148
{
slouken@1895
   149
    int i;
slouken@1895
   150
    int pos, inc;
slouken@1895
   151
    Uint8 pixel[3];
slouken@0
   152
slouken@1895
   153
    pos = 0x10000;
slouken@1895
   154
    inc = (src_w << 16) / dst_w;
slouken@1895
   155
    for (i = dst_w; i > 0; --i) {
slouken@1895
   156
        while (pos >= 0x10000L) {
slouken@1895
   157
            pixel[0] = *src++;
slouken@1895
   158
            pixel[1] = *src++;
slouken@1895
   159
            pixel[2] = *src++;
slouken@1895
   160
            pos -= 0x10000L;
slouken@1895
   161
        }
slouken@1895
   162
        *dst++ = pixel[0];
slouken@1895
   163
        *dst++ = pixel[1];
slouken@1895
   164
        *dst++ = pixel[2];
slouken@1895
   165
        pos += inc;
slouken@1895
   166
    }
slouken@0
   167
}
slouken@0
   168
slouken@0
   169
/* Perform a stretch blit between two surfaces of the same format.
slouken@0
   170
   NOTE:  This function is not safe to call from multiple threads!
slouken@0
   171
*/
slouken@1895
   172
int
slouken@1895
   173
SDL_SoftStretch(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   174
                SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   175
{
slouken@1895
   176
    int src_locked;
slouken@1895
   177
    int dst_locked;
slouken@1895
   178
    int pos, inc;
slouken@1895
   179
    int dst_width;
slouken@1895
   180
    int dst_maxrow;
slouken@1895
   181
    int src_row, dst_row;
slouken@1895
   182
    Uint8 *srcp = NULL;
slouken@1895
   183
    Uint8 *dstp;
slouken@1895
   184
    SDL_Rect full_src;
slouken@1895
   185
    SDL_Rect full_dst;
slouken@0
   186
#if defined(USE_ASM_STRETCH) && defined(__GNUC__)
slouken@1895
   187
    int u1, u2;
slouken@0
   188
#endif
slouken@1895
   189
    const int bpp = dst->format->BytesPerPixel;
slouken@0
   190
slouken@1895
   191
    if (src->format->BitsPerPixel != dst->format->BitsPerPixel) {
slouken@1895
   192
        SDL_SetError("Only works with same format surfaces");
slouken@1895
   193
        return (-1);
slouken@1895
   194
    }
slouken@0
   195
slouken@1895
   196
    /* Verify the blit rectangles */
slouken@1895
   197
    if (srcrect) {
slouken@1895
   198
        if ((srcrect->x < 0) || (srcrect->y < 0) ||
slouken@1895
   199
            ((srcrect->x + srcrect->w) > src->w) ||
slouken@1895
   200
            ((srcrect->y + srcrect->h) > src->h)) {
slouken@1895
   201
            SDL_SetError("Invalid source blit rectangle");
slouken@1895
   202
            return (-1);
slouken@1895
   203
        }
slouken@1895
   204
    } else {
slouken@1895
   205
        full_src.x = 0;
slouken@1895
   206
        full_src.y = 0;
slouken@1895
   207
        full_src.w = src->w;
slouken@1895
   208
        full_src.h = src->h;
slouken@1895
   209
        srcrect = &full_src;
slouken@1895
   210
    }
slouken@1895
   211
    if (dstrect) {
slouken@1895
   212
        if ((dstrect->x < 0) || (dstrect->y < 0) ||
slouken@1895
   213
            ((dstrect->x + dstrect->w) > dst->w) ||
slouken@1895
   214
            ((dstrect->y + dstrect->h) > dst->h)) {
slouken@1895
   215
            SDL_SetError("Invalid destination blit rectangle");
slouken@1895
   216
            return (-1);
slouken@1895
   217
        }
slouken@1895
   218
    } else {
slouken@1895
   219
        full_dst.x = 0;
slouken@1895
   220
        full_dst.y = 0;
slouken@1895
   221
        full_dst.w = dst->w;
slouken@1895
   222
        full_dst.h = dst->h;
slouken@1895
   223
        dstrect = &full_dst;
slouken@1895
   224
    }
slouken@0
   225
slouken@1895
   226
    /* Lock the destination if it's in hardware */
slouken@1895
   227
    dst_locked = 0;
slouken@1895
   228
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
   229
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
   230
            SDL_SetError("Unable to lock destination surface");
slouken@1895
   231
            return (-1);
slouken@1895
   232
        }
slouken@1895
   233
        dst_locked = 1;
slouken@1895
   234
    }
slouken@1895
   235
    /* Lock the source if it's in hardware */
slouken@1895
   236
    src_locked = 0;
slouken@1895
   237
    if (SDL_MUSTLOCK(src)) {
slouken@1895
   238
        if (SDL_LockSurface(src) < 0) {
slouken@1895
   239
            if (dst_locked) {
slouken@1895
   240
                SDL_UnlockSurface(dst);
slouken@1895
   241
            }
slouken@1895
   242
            SDL_SetError("Unable to lock source surface");
slouken@1895
   243
            return (-1);
slouken@1895
   244
        }
slouken@1895
   245
        src_locked = 1;
slouken@1895
   246
    }
slouken@894
   247
slouken@1895
   248
    /* Set up the data... */
slouken@1895
   249
    pos = 0x10000;
slouken@1895
   250
    inc = (srcrect->h << 16) / dstrect->h;
slouken@1895
   251
    src_row = srcrect->y;
slouken@1895
   252
    dst_row = dstrect->y;
slouken@1895
   253
    dst_width = dstrect->w * bpp;
slouken@0
   254
slouken@0
   255
#ifdef USE_ASM_STRETCH
slouken@1895
   256
    /* Write the opcodes for this stretch */
slouken@1895
   257
    if ((bpp != 3) && (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
slouken@1895
   258
        return (-1);
slouken@1895
   259
    }
slouken@0
   260
#endif
slouken@0
   261
slouken@1895
   262
    /* Perform the stretch blit */
slouken@1895
   263
    for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
slouken@1895
   264
        dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
slouken@1895
   265
            + (dstrect->x * bpp);
slouken@1895
   266
        while (pos >= 0x10000L) {
slouken@1895
   267
            srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
slouken@1895
   268
                + (srcrect->x * bpp);
slouken@1895
   269
            ++src_row;
slouken@1895
   270
            pos -= 0x10000L;
slouken@1895
   271
        }
slouken@0
   272
#ifdef USE_ASM_STRETCH
slouken@1895
   273
        switch (bpp) {
slouken@1895
   274
        case 3:
slouken@1895
   275
            copy_row3(srcp, srcrect->w, dstp, dstrect->w);
slouken@1895
   276
            break;
slouken@1895
   277
        default:
slouken@0
   278
#ifdef __GNUC__
slouken@1895
   279
          __asm__ __volatile__("call *%4": "=&D"(u1), "=&S"(u2): "0"(dstp), "1"(srcp), "r"(copy_row):"memory");
slouken@1442
   280
#elif defined(_MSC_VER) || defined(__WATCOMC__)
slouken@1895
   281
            {
slouken@1895
   282
                void *code = copy_row;
slouken@1895
   283
                __asm {
slouken@1895
   284
                push edi
slouken@1895
   285
                        push esi
slouken@1895
   286
                        mov edi, dstp
slouken@1895
   287
                        mov esi, srcp call dword ptr code pop esi pop edi}
slouken@1895
   288
            }
slouken@0
   289
#else
slouken@0
   290
#error Need inline assembly for this compiler
slouken@0
   291
#endif
slouken@1895
   292
            break;
slouken@1895
   293
        }
slouken@0
   294
#else
slouken@1895
   295
        switch (bpp) {
slouken@1895
   296
        case 1:
slouken@1895
   297
            copy_row1(srcp, srcrect->w, dstp, dstrect->w);
slouken@1895
   298
            break;
slouken@1895
   299
        case 2:
slouken@1895
   300
            copy_row2((Uint16 *) srcp, srcrect->w,
slouken@1895
   301
                      (Uint16 *) dstp, dstrect->w);
slouken@1895
   302
            break;
slouken@1895
   303
        case 3:
slouken@1895
   304
            copy_row3(srcp, srcrect->w, dstp, dstrect->w);
slouken@1895
   305
            break;
slouken@1895
   306
        case 4:
slouken@1895
   307
            copy_row4((Uint32 *) srcp, srcrect->w,
slouken@1895
   308
                      (Uint32 *) dstp, dstrect->w);
slouken@1895
   309
            break;
slouken@1895
   310
        }
slouken@0
   311
#endif
slouken@1895
   312
        pos += inc;
slouken@1895
   313
    }
slouken@894
   314
slouken@1895
   315
    /* We need to unlock the surfaces if they're locked */
slouken@1895
   316
    if (dst_locked) {
slouken@1895
   317
        SDL_UnlockSurface(dst);
slouken@1895
   318
    }
slouken@1895
   319
    if (src_locked) {
slouken@1895
   320
        SDL_UnlockSurface(src);
slouken@1895
   321
    }
slouken@1895
   322
    return (0);
slouken@0
   323
}
slouken@0
   324
slouken@1895
   325
/* vi: set ts=4 sw=4 expandtab: */