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