src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
child 1670 eef792d31de8
permissions -rw-r--r--
more tweaking indent options
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@1662
    40
static int
slouken@1668
    41
SDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1668
    42
             SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
    43
{
slouken@1662
    44
    int okay;
slouken@1662
    45
    int src_locked;
slouken@1662
    46
    int dst_locked;
slouken@0
    47
slouken@1662
    48
    /* Everything is okay at the beginning...  */
slouken@1662
    49
    okay = 1;
slouken@0
    50
slouken@1662
    51
    /* Lock the destination if it's in hardware */
slouken@1662
    52
    dst_locked = 0;
slouken@1668
    53
    if (SDL_MUSTLOCK(dst)) {
slouken@1668
    54
        if (SDL_LockSurface(dst) < 0) {
slouken@1662
    55
            okay = 0;
slouken@1662
    56
        } else {
slouken@1662
    57
            dst_locked = 1;
slouken@1662
    58
        }
slouken@1662
    59
    }
slouken@1662
    60
    /* Lock the source if it's in hardware */
slouken@1662
    61
    src_locked = 0;
slouken@1668
    62
    if (SDL_MUSTLOCK(src)) {
slouken@1668
    63
        if (SDL_LockSurface(src) < 0) {
slouken@1662
    64
            okay = 0;
slouken@1662
    65
        } else {
slouken@1662
    66
            src_locked = 1;
slouken@1662
    67
        }
slouken@1662
    68
    }
slouken@0
    69
slouken@1662
    70
    /* Set up source and destination buffer pointers, and BLIT! */
slouken@1662
    71
    if (okay && srcrect->w && srcrect->h) {
slouken@1662
    72
        SDL_BlitInfo info;
slouken@1662
    73
        SDL_loblit RunBlit;
slouken@0
    74
slouken@1662
    75
        /* Set up the blit information */
slouken@1662
    76
        info.s_pixels = (Uint8 *) src->pixels +
slouken@1662
    77
            (Uint16) srcrect->y * src->pitch +
slouken@1662
    78
            (Uint16) srcrect->x * src->format->BytesPerPixel;
slouken@1662
    79
        info.s_width = srcrect->w;
slouken@1662
    80
        info.s_height = srcrect->h;
slouken@1662
    81
        info.s_skip = src->pitch - info.s_width * src->format->BytesPerPixel;
slouken@1662
    82
        info.d_pixels = (Uint8 *) dst->pixels +
slouken@1662
    83
            (Uint16) dstrect->y * dst->pitch +
slouken@1662
    84
            (Uint16) dstrect->x * dst->format->BytesPerPixel;
slouken@1662
    85
        info.d_width = dstrect->w;
slouken@1662
    86
        info.d_height = dstrect->h;
slouken@1662
    87
        info.d_skip = dst->pitch - info.d_width * dst->format->BytesPerPixel;
slouken@1662
    88
        info.aux_data = src->map->sw_data->aux_data;
slouken@1662
    89
        info.src = src->format;
slouken@1662
    90
        info.table = src->map->table;
slouken@1662
    91
        info.dst = dst->format;
slouken@1662
    92
        RunBlit = src->map->sw_data->blit;
slouken@0
    93
slouken@1662
    94
        /* Run the actual software blit */
slouken@1668
    95
        RunBlit(&info);
slouken@1662
    96
    }
slouken@0
    97
slouken@1662
    98
    /* We need to unlock the surfaces if they're locked */
slouken@1662
    99
    if (dst_locked) {
slouken@1668
   100
        SDL_UnlockSurface(dst);
slouken@1662
   101
    }
slouken@1662
   102
    if (src_locked) {
slouken@1668
   103
        SDL_UnlockSurface(src);
slouken@1662
   104
    }
slouken@1662
   105
    /* Blit is done! */
slouken@1662
   106
    return (okay ? 0 : -1);
slouken@0
   107
}
slouken@0
   108
slouken@880
   109
#ifdef MMX_ASMBLIT
slouken@1662
   110
static __inline__ void
slouken@1668
   111
SDL_memcpyMMX(Uint8 * to, const Uint8 * from, int len)
slouken@689
   112
{
slouken@1662
   113
    int i;
slouken@689
   114
slouken@1662
   115
    for (i = 0; i < len / 8; i++) {
slouken@1668
   116
        __asm__ __volatile__("	movq (%0), %%mm0\n"
slouken@1668
   117
                             "	movq %%mm0, (%1)\n"::"r"(from),
slouken@1668
   118
                             "r"(to):"memory");
slouken@1662
   119
        from += 8;
slouken@1662
   120
        to += 8;
slouken@1662
   121
    }
slouken@1662
   122
    if (len & 7)
slouken@1668
   123
        SDL_memcpy(to, from, len & 7);
slouken@689
   124
}
slouken@689
   125
slouken@1662
   126
static __inline__ void
slouken@1668
   127
SDL_memcpySSE(Uint8 * to, const Uint8 * from, int len)
slouken@689
   128
{
slouken@1662
   129
    int i;
slouken@689
   130
slouken@1668
   131
    __asm__ __volatile__("	prefetchnta (%0)\n"
slouken@1668
   132
                         "	prefetchnta 64(%0)\n"
slouken@1668
   133
                         "	prefetchnta 128(%0)\n"
slouken@1668
   134
                         "	prefetchnta 192(%0)\n"::"r"(from));
slouken@689
   135
slouken@1662
   136
    for (i = 0; i < len / 8; i++) {
slouken@1668
   137
        __asm__ __volatile__("	prefetchnta 256(%0)\n"
slouken@1668
   138
                             "	movq (%0), %%mm0\n"
slouken@1668
   139
                             "	movntq %%mm0, (%1)\n"::"r"(from),
slouken@1668
   140
                             "r"(to):"memory");
slouken@1662
   141
        from += 8;
slouken@1662
   142
        to += 8;
slouken@1662
   143
    }
slouken@1662
   144
    if (len & 7)
slouken@1668
   145
        SDL_memcpy(to, from, len & 7);
slouken@689
   146
}
slouken@689
   147
#endif
slouken@689
   148
slouken@1662
   149
static void
slouken@1668
   150
SDL_BlitCopy(SDL_BlitInfo * info)
slouken@0
   151
{
slouken@1662
   152
    Uint8 *src, *dst;
slouken@1662
   153
    int w, h;
slouken@1662
   154
    int srcskip, dstskip;
slouken@0
   155
slouken@1662
   156
    w = info->d_width * info->dst->BytesPerPixel;
slouken@1662
   157
    h = info->d_height;
slouken@1662
   158
    src = info->s_pixels;
slouken@1662
   159
    dst = info->d_pixels;
slouken@1662
   160
    srcskip = w + info->s_skip;
slouken@1662
   161
    dstskip = w + info->d_skip;
slouken@880
   162
#ifdef MMX_ASMBLIT
slouken@1668
   163
    if (SDL_HasSSE()) {
slouken@1662
   164
        while (h--) {
slouken@1668
   165
            SDL_memcpySSE(dst, src, w);
slouken@1662
   166
            src += srcskip;
slouken@1662
   167
            dst += dstskip;
slouken@1662
   168
        }
slouken@1668
   169
        __asm__ __volatile__("	emms\n"::);
slouken@1668
   170
    } else if (SDL_HasMMX()) {
slouken@1662
   171
        while (h--) {
slouken@1668
   172
            SDL_memcpyMMX(dst, src, w);
slouken@1662
   173
            src += srcskip;
slouken@1662
   174
            dst += dstskip;
slouken@1662
   175
        }
slouken@1668
   176
        __asm__ __volatile__("	emms\n"::);
slouken@1662
   177
    } else
slouken@689
   178
#endif
slouken@1662
   179
        while (h--) {
slouken@1668
   180
            SDL_memcpy(dst, src, w);
slouken@1662
   181
            src += srcskip;
slouken@1662
   182
            dst += dstskip;
slouken@1662
   183
        }
slouken@0
   184
}
slouken@0
   185
slouken@1662
   186
static void
slouken@1668
   187
SDL_BlitCopyOverlap(SDL_BlitInfo * info)
slouken@0
   188
{
slouken@1662
   189
    Uint8 *src, *dst;
slouken@1662
   190
    int w, h;
slouken@1662
   191
    int srcskip, dstskip;
slouken@0
   192
slouken@1662
   193
    w = info->d_width * info->dst->BytesPerPixel;
slouken@1662
   194
    h = info->d_height;
slouken@1662
   195
    src = info->s_pixels;
slouken@1662
   196
    dst = info->d_pixels;
slouken@1662
   197
    srcskip = w + info->s_skip;
slouken@1662
   198
    dstskip = w + info->d_skip;
slouken@1662
   199
    if (dst < src) {
slouken@1662
   200
        while (h--) {
slouken@1668
   201
            SDL_memcpy(dst, src, w);
slouken@1662
   202
            src += srcskip;
slouken@1662
   203
            dst += dstskip;
slouken@1662
   204
        }
slouken@1662
   205
    } else {
slouken@1662
   206
        src += ((h - 1) * srcskip);
slouken@1662
   207
        dst += ((h - 1) * dstskip);
slouken@1662
   208
        while (h--) {
slouken@1668
   209
            SDL_revcpy(dst, src, w);
slouken@1662
   210
            src -= srcskip;
slouken@1662
   211
            dst -= dstskip;
slouken@1662
   212
        }
slouken@1662
   213
    }
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
/* Figure out which of many blit routines to set up on a surface */
slouken@1662
   217
int
slouken@1668
   218
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   219
{
slouken@1668
   220
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1668
   221
    const SDL_VideoInfo *info = SDL_GetVideoInfo();
slouken@1662
   222
    int blit_index;
slouken@0
   223
slouken@1662
   224
    /* Clean everything out to start */
slouken@1662
   225
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1668
   226
        SDL_UnRLESurface(surface, 1);
slouken@1662
   227
    }
slouken@1662
   228
    surface->map->sw_blit = NULL;
slouken@0
   229
slouken@1662
   230
    /* Figure out if an accelerated hardware blit is possible */
slouken@1662
   231
    surface->flags &= ~SDL_HWACCEL;
slouken@1662
   232
    if (surface->map->identity) {
slouken@1662
   233
        int hw_blit_ok;
slouken@0
   234
slouken@1662
   235
        if ((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1662
   236
            /* We only support accelerated blitting to hardware */
slouken@1662
   237
            if (surface->map->dst->flags & SDL_HWSURFACE) {
slouken@1662
   238
                hw_blit_ok = info->blit_hw;
slouken@1662
   239
            } else {
slouken@1662
   240
                hw_blit_ok = 0;
slouken@1662
   241
            }
slouken@1662
   242
            if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@1662
   243
                hw_blit_ok = info->blit_hw_CC;
slouken@1662
   244
            }
slouken@1662
   245
            if (hw_blit_ok && (surface->flags & SDL_SRCALPHA)) {
slouken@1662
   246
                hw_blit_ok = info->blit_hw_A;
slouken@1662
   247
            }
slouken@1662
   248
        } else {
slouken@1662
   249
            /* We only support accelerated blitting to hardware */
slouken@1662
   250
            if (surface->map->dst->flags & SDL_HWSURFACE) {
slouken@1662
   251
                hw_blit_ok = info->blit_sw;
slouken@1662
   252
            } else {
slouken@1662
   253
                hw_blit_ok = 0;
slouken@1662
   254
            }
slouken@1662
   255
            if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
slouken@1662
   256
                hw_blit_ok = info->blit_sw_CC;
slouken@1662
   257
            }
slouken@1662
   258
            if (hw_blit_ok && (surface->flags & SDL_SRCALPHA)) {
slouken@1662
   259
                hw_blit_ok = info->blit_sw_A;
slouken@1662
   260
            }
slouken@1662
   261
        }
slouken@1662
   262
        if (hw_blit_ok) {
slouken@1668
   263
            _this->CheckHWBlit(_this, surface, surface->map->dst);
slouken@1662
   264
        }
slouken@1662
   265
    }
slouken@0
   266
slouken@1662
   267
    /* if an alpha pixel format is specified, we can accelerate alpha blits */
slouken@1662
   268
    if (((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@1662
   269
        && (_this->displayformatalphapixel)) {
slouken@1662
   270
        if ((surface->flags & SDL_SRCALPHA))
slouken@1662
   271
            if (info->blit_hw_A) {
slouken@1668
   272
                _this->CheckHWBlit(_this, surface, surface->map->dst);
slouken@1662
   273
            }
slouken@1662
   274
    }
slouken@0
   275
slouken@1662
   276
    /* Get the blit function index, based on surface mode */
slouken@1662
   277
    /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@1662
   278
    blit_index = 0;
slouken@1662
   279
    blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
slouken@1662
   280
    if (surface->flags & SDL_SRCALPHA
slouken@1662
   281
        && (surface->format->alpha != SDL_ALPHA_OPAQUE
slouken@1662
   282
            || surface->format->Amask)) {
slouken@1662
   283
        blit_index |= 2;
slouken@1662
   284
    }
slouken@0
   285
slouken@1662
   286
    /* Check for special "identity" case -- copy blit */
slouken@1662
   287
    if (surface->map->identity && blit_index == 0) {
slouken@1662
   288
        surface->map->sw_data->blit = SDL_BlitCopy;
slouken@0
   289
slouken@1662
   290
        /* Handle overlapping blits on the same surface */
slouken@1662
   291
        if (surface == surface->map->dst) {
slouken@1662
   292
            surface->map->sw_data->blit = SDL_BlitCopyOverlap;
slouken@1662
   293
        }
slouken@1662
   294
    } else {
slouken@1662
   295
        if (surface->format->BitsPerPixel < 8) {
slouken@1662
   296
            surface->map->sw_data->blit =
slouken@1668
   297
                SDL_CalculateBlit0(surface, blit_index);
slouken@1662
   298
        } else {
slouken@1662
   299
            switch (surface->format->BytesPerPixel) {
slouken@1662
   300
            case 1:
slouken@1662
   301
                surface->map->sw_data->blit =
slouken@1668
   302
                    SDL_CalculateBlit1(surface, blit_index);
slouken@1662
   303
                break;
slouken@1662
   304
            case 2:
slouken@1662
   305
            case 3:
slouken@1662
   306
            case 4:
slouken@1662
   307
                surface->map->sw_data->blit =
slouken@1668
   308
                    SDL_CalculateBlitN(surface, blit_index);
slouken@1662
   309
                break;
slouken@1662
   310
            default:
slouken@1662
   311
                surface->map->sw_data->blit = NULL;
slouken@1662
   312
                break;
slouken@1662
   313
            }
slouken@1662
   314
        }
slouken@1662
   315
    }
slouken@1662
   316
    /* Make sure we have a blit function */
slouken@1662
   317
    if (surface->map->sw_data->blit == NULL) {
slouken@1668
   318
        SDL_InvalidateMap(surface->map);
slouken@1668
   319
        SDL_SetError("Blit combination not supported");
slouken@1662
   320
        return (-1);
slouken@1662
   321
    }
slouken@1662
   322
slouken@1662
   323
    /* Choose software blitting function */
slouken@1662
   324
    if (surface->flags & SDL_RLEACCELOK
slouken@1662
   325
        && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
slouken@1662
   326
slouken@1662
   327
        if (surface->map->identity
slouken@1662
   328
            && (blit_index == 1
slouken@1662
   329
                || (blit_index == 3 && !surface->format->Amask))) {
slouken@1668
   330
            if (SDL_RLESurface(surface) == 0)
slouken@1662
   331
                surface->map->sw_blit = SDL_RLEBlit;
slouken@1662
   332
        } else if (blit_index == 2 && surface->format->Amask) {
slouken@1668
   333
            if (SDL_RLESurface(surface) == 0)
slouken@1662
   334
                surface->map->sw_blit = SDL_RLEAlphaBlit;
slouken@1662
   335
        }
slouken@1662
   336
    }
slouken@1662
   337
slouken@1662
   338
    if (surface->map->sw_blit == NULL) {
slouken@1662
   339
        surface->map->sw_blit = SDL_SoftBlit;
slouken@1662
   340
    }
slouken@1662
   341
    return (0);
slouken@0
   342
}
slouken@0
   343
slouken@1662
   344
/* vi: set ts=4 sw=4 expandtab: */