src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1402 d910939febfa
child 2247 93994f65c74c
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
#include "SDL_video.h"
slouken@0
    25
#include "SDL_sysvideo.h"
slouken@0
    26
#include "SDL_blit.h"
slouken@0
    27
#include "SDL_RLEaccel_c.h"
slouken@0
    28
#include "SDL_pixels_c.h"
slouken@0
    29
slouken@1402
    30
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && SDL_ASSEMBLY_ROUTINES
slouken@880
    31
#define MMX_ASMBLIT
slouken@880
    32
#endif
slouken@880
    33
slouken@880
    34
#if defined(MMX_ASMBLIT)
slouken@739
    35
#include "SDL_cpuinfo.h"
slouken@689
    36
#include "mmx.h"
slouken@689
    37
#endif
slouken@689
    38
slouken@0
    39
/* The general purpose software blit routine */
slouken@1895
    40
static int
slouken@1895
    41
SDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
    42
             SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
    43
{
slouken@1895
    44
    int okay;
slouken@1895
    45
    int src_locked;
slouken@1895
    46
    int dst_locked;
slouken@0
    47
slouken@1895
    48
    /* Everything is okay at the beginning...  */
slouken@1895
    49
    okay = 1;
slouken@0
    50
slouken@1895
    51
    /* Lock the destination if it's in hardware */
slouken@1895
    52
    dst_locked = 0;
slouken@1895
    53
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
    54
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
    55
            okay = 0;
slouken@1895
    56
        } else {
slouken@1895
    57
            dst_locked = 1;
slouken@1895
    58
        }
slouken@1895
    59
    }
slouken@1895
    60
    /* Lock the source if it's in hardware */
slouken@1895
    61
    src_locked = 0;
slouken@1895
    62
    if (SDL_MUSTLOCK(src)) {
slouken@1895
    63
        if (SDL_LockSurface(src) < 0) {
slouken@1895
    64
            okay = 0;
slouken@1895
    65
        } else {
slouken@1895
    66
            src_locked = 1;
slouken@1895
    67
        }
slouken@1895
    68
    }
slouken@0
    69
slouken@1895
    70
    /* Set up source and destination buffer pointers, and BLIT! */
slouken@1895
    71
    if (okay && srcrect->w && srcrect->h) {
slouken@1895
    72
        SDL_BlitInfo info;
slouken@1895
    73
        SDL_loblit RunBlit;
slouken@0
    74
slouken@1895
    75
        /* Set up the blit information */
slouken@1895
    76
        info.s_pixels = (Uint8 *) src->pixels +
slouken@1895
    77
            (Uint16) srcrect->y * src->pitch +
slouken@1895
    78
            (Uint16) srcrect->x * src->format->BytesPerPixel;
slouken@1895
    79
        info.s_width = srcrect->w;
slouken@1895
    80
        info.s_height = srcrect->h;
slouken@1895
    81
        info.s_skip = src->pitch - info.s_width * src->format->BytesPerPixel;
slouken@1895
    82
        info.d_pixels = (Uint8 *) dst->pixels +
slouken@1895
    83
            (Uint16) dstrect->y * dst->pitch +
slouken@1895
    84
            (Uint16) dstrect->x * dst->format->BytesPerPixel;
slouken@1895
    85
        info.d_width = dstrect->w;
slouken@1895
    86
        info.d_height = dstrect->h;
slouken@1895
    87
        info.d_skip = dst->pitch - info.d_width * dst->format->BytesPerPixel;
slouken@1895
    88
        info.aux_data = src->map->sw_data->aux_data;
slouken@1895
    89
        info.src = src->format;
slouken@1895
    90
        info.table = src->map->table;
slouken@1895
    91
        info.dst = dst->format;
slouken@1895
    92
        RunBlit = src->map->sw_data->blit;
slouken@0
    93
slouken@1895
    94
        /* Run the actual software blit */
slouken@1895
    95
        RunBlit(&info);
slouken@1895
    96
    }
slouken@0
    97
slouken@1895
    98
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    99
    if (dst_locked) {
slouken@1895
   100
        SDL_UnlockSurface(dst);
slouken@1895
   101
    }
slouken@1895
   102
    if (src_locked) {
slouken@1895
   103
        SDL_UnlockSurface(src);
slouken@1895
   104
    }
slouken@1895
   105
    /* Blit is done! */
slouken@1895
   106
    return (okay ? 0 : -1);
slouken@0
   107
}
slouken@0
   108
slouken@880
   109
#ifdef MMX_ASMBLIT
slouken@1895
   110
static __inline__ void
slouken@1895
   111
SDL_memcpyMMX(Uint8 * to, const Uint8 * from, int len)
slouken@689
   112
{
slouken@1895
   113
    int i;
slouken@689
   114
slouken@1895
   115
    for (i = 0; i < len / 8; i++) {
slouken@1895
   116
        __asm__ __volatile__("	movq (%0), %%mm0\n"
slouken@1895
   117
                             "	movq %%mm0, (%1)\n"::"r"(from),
slouken@1895
   118
                             "r"(to):"memory");
slouken@1895
   119
        from += 8;
slouken@1895
   120
        to += 8;
slouken@1895
   121
    }
slouken@1895
   122
    if (len & 7)
slouken@1895
   123
        SDL_memcpy(to, from, len & 7);
slouken@689
   124
}
slouken@689
   125
slouken@1895
   126
static __inline__ void
slouken@1895
   127
SDL_memcpySSE(Uint8 * to, const Uint8 * from, int len)
slouken@689
   128
{
slouken@1895
   129
    int i;
slouken@689
   130
slouken@1895
   131
    __asm__ __volatile__("	prefetchnta (%0)\n"
slouken@1895
   132
                         "	prefetchnta 64(%0)\n"
slouken@1895
   133
                         "	prefetchnta 128(%0)\n"
slouken@1895
   134
                         "	prefetchnta 192(%0)\n"::"r"(from));
slouken@689
   135
slouken@1895
   136
    for (i = 0; i < len / 8; i++) {
slouken@1895
   137
        __asm__ __volatile__("	prefetchnta 256(%0)\n"
slouken@1895
   138
                             "	movq (%0), %%mm0\n"
slouken@1895
   139
                             "	movntq %%mm0, (%1)\n"::"r"(from),
slouken@1895
   140
                             "r"(to):"memory");
slouken@1895
   141
        from += 8;
slouken@1895
   142
        to += 8;
slouken@1895
   143
    }
slouken@1895
   144
    if (len & 7)
slouken@1895
   145
        SDL_memcpy(to, from, len & 7);
slouken@689
   146
}
slouken@689
   147
#endif
slouken@689
   148
slouken@1895
   149
static void
slouken@1895
   150
SDL_BlitCopy(SDL_BlitInfo * info)
slouken@0
   151
{
slouken@1895
   152
    Uint8 *src, *dst;
slouken@1895
   153
    int w, h;
slouken@1895
   154
    int srcskip, dstskip;
slouken@0
   155
slouken@1895
   156
    w = info->d_width * info->dst->BytesPerPixel;
slouken@1895
   157
    h = info->d_height;
slouken@1895
   158
    src = info->s_pixels;
slouken@1895
   159
    dst = info->d_pixels;
slouken@1895
   160
    srcskip = w + info->s_skip;
slouken@1895
   161
    dstskip = w + info->d_skip;
slouken@880
   162
#ifdef MMX_ASMBLIT
slouken@1895
   163
    if (SDL_HasSSE()) {
slouken@1895
   164
        while (h--) {
slouken@1895
   165
            SDL_memcpySSE(dst, src, w);
slouken@1895
   166
            src += srcskip;
slouken@1895
   167
            dst += dstskip;
slouken@1895
   168
        }
slouken@1895
   169
        __asm__ __volatile__("	emms\n"::);
slouken@1895
   170
    } else if (SDL_HasMMX()) {
slouken@1895
   171
        while (h--) {
slouken@1895
   172
            SDL_memcpyMMX(dst, src, w);
slouken@1895
   173
            src += srcskip;
slouken@1895
   174
            dst += dstskip;
slouken@1895
   175
        }
slouken@1895
   176
        __asm__ __volatile__("	emms\n"::);
slouken@1895
   177
    } else
slouken@689
   178
#endif
slouken@1895
   179
        while (h--) {
slouken@1895
   180
            SDL_memcpy(dst, src, w);
slouken@1895
   181
            src += srcskip;
slouken@1895
   182
            dst += dstskip;
slouken@1895
   183
        }
slouken@0
   184
}
slouken@0
   185
slouken@1895
   186
static void
slouken@1895
   187
SDL_BlitCopyOverlap(SDL_BlitInfo * info)
slouken@0
   188
{
slouken@1895
   189
    Uint8 *src, *dst;
slouken@1895
   190
    int w, h;
slouken@1895
   191
    int srcskip, dstskip;
slouken@0
   192
slouken@1895
   193
    w = info->d_width * info->dst->BytesPerPixel;
slouken@1895
   194
    h = info->d_height;
slouken@1895
   195
    src = info->s_pixels;
slouken@1895
   196
    dst = info->d_pixels;
slouken@1895
   197
    srcskip = w + info->s_skip;
slouken@1895
   198
    dstskip = w + info->d_skip;
slouken@1895
   199
    if (dst < src) {
slouken@1895
   200
        while (h--) {
slouken@1895
   201
            SDL_memcpy(dst, src, w);
slouken@1895
   202
            src += srcskip;
slouken@1895
   203
            dst += dstskip;
slouken@1895
   204
        }
slouken@1895
   205
    } else {
slouken@1895
   206
        src += ((h - 1) * srcskip);
slouken@1895
   207
        dst += ((h - 1) * dstskip);
slouken@1895
   208
        while (h--) {
slouken@1895
   209
            SDL_revcpy(dst, src, w);
slouken@1895
   210
            src -= srcskip;
slouken@1895
   211
            dst -= dstskip;
slouken@1895
   212
        }
slouken@1895
   213
    }
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   217
int
slouken@1895
   218
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   219
{
slouken@1895
   220
    int blit_index;
slouken@0
   221
slouken@1895
   222
    /* Clean everything out to start */
slouken@1895
   223
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   224
        SDL_UnRLESurface(surface, 1);
slouken@1895
   225
    }
slouken@1895
   226
    surface->map->sw_blit = NULL;
slouken@0
   227
slouken@1895
   228
    /* Get the blit function index, based on surface mode */
slouken@1895
   229
    /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@1895
   230
    blit_index = 0;
slouken@1895
   231
    blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
slouken@1895
   232
    if (surface->flags & SDL_SRCALPHA
slouken@1895
   233
        && (surface->format->alpha != SDL_ALPHA_OPAQUE
slouken@1895
   234
            || surface->format->Amask)) {
slouken@1895
   235
        blit_index |= 2;
slouken@1895
   236
    }
slouken@0
   237
slouken@1895
   238
    /* Check for special "identity" case -- copy blit */
slouken@1895
   239
    if (surface->map->identity && blit_index == 0) {
slouken@1895
   240
        surface->map->sw_data->blit = SDL_BlitCopy;
slouken@0
   241
slouken@1895
   242
        /* Handle overlapping blits on the same surface */
slouken@1895
   243
        if (surface == surface->map->dst) {
slouken@1895
   244
            surface->map->sw_data->blit = SDL_BlitCopyOverlap;
slouken@1895
   245
        }
slouken@1895
   246
    } else {
slouken@1895
   247
        if (surface->format->BitsPerPixel < 8) {
slouken@1895
   248
            surface->map->sw_data->blit =
slouken@1895
   249
                SDL_CalculateBlit0(surface, blit_index);
slouken@1895
   250
        } else {
slouken@1895
   251
            switch (surface->format->BytesPerPixel) {
slouken@1895
   252
            case 1:
slouken@1895
   253
                surface->map->sw_data->blit =
slouken@1895
   254
                    SDL_CalculateBlit1(surface, blit_index);
slouken@1895
   255
                break;
slouken@1895
   256
            case 2:
slouken@1895
   257
            case 3:
slouken@1895
   258
            case 4:
slouken@1895
   259
                surface->map->sw_data->blit =
slouken@1895
   260
                    SDL_CalculateBlitN(surface, blit_index);
slouken@1895
   261
                break;
slouken@1895
   262
            default:
slouken@1895
   263
                surface->map->sw_data->blit = NULL;
slouken@1895
   264
                break;
slouken@1895
   265
            }
slouken@1895
   266
        }
slouken@1895
   267
    }
slouken@1895
   268
    /* Make sure we have a blit function */
slouken@1895
   269
    if (surface->map->sw_data->blit == NULL) {
slouken@1895
   270
        SDL_InvalidateMap(surface->map);
slouken@1895
   271
        SDL_SetError("Blit combination not supported");
slouken@1895
   272
        return (-1);
slouken@1895
   273
    }
slouken@0
   274
slouken@1895
   275
    /* Choose software blitting function */
slouken@1895
   276
    if (surface->flags & SDL_RLEACCELOK) {
slouken@1895
   277
        if (surface->map->identity
slouken@1895
   278
            && (blit_index == 1
slouken@1895
   279
                || (blit_index == 3 && !surface->format->Amask))) {
slouken@1895
   280
            if (SDL_RLESurface(surface) == 0)
slouken@1895
   281
                surface->map->sw_blit = SDL_RLEBlit;
slouken@1895
   282
        } else if (blit_index == 2 && surface->format->Amask) {
slouken@1895
   283
            if (SDL_RLESurface(surface) == 0)
slouken@1895
   284
                surface->map->sw_blit = SDL_RLEAlphaBlit;
slouken@1895
   285
        }
slouken@1895
   286
    }
slouken@0
   287
slouken@1895
   288
    if (surface->map->sw_blit == NULL) {
slouken@1895
   289
        surface->map->sw_blit = SDL_SoftBlit;
slouken@1895
   290
    }
slouken@1895
   291
    return (0);
slouken@0
   292
}
slouken@0
   293
slouken@1895
   294
/* vi: set ts=4 sw=4 expandtab: */