src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Nov 2008 00:10:59 +0000
changeset 2801 5eb95b8adc1a
parent 2329 c1a390f6a819
child 2805 2ed5ff5373d8
permissions -rw-r--r--
Fixed copy blit detection
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@2263
    27
#include "SDL_blit_auto.h"
slouken@2254
    28
#include "SDL_blit_copy.h"
slouken@0
    29
#include "SDL_RLEaccel_c.h"
slouken@0
    30
#include "SDL_pixels_c.h"
slouken@0
    31
slouken@0
    32
/* The general purpose software blit routine */
slouken@1895
    33
static int
slouken@1895
    34
SDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
    35
             SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
    36
{
slouken@1895
    37
    int okay;
slouken@1895
    38
    int src_locked;
slouken@1895
    39
    int dst_locked;
slouken@0
    40
slouken@1895
    41
    /* Everything is okay at the beginning...  */
slouken@1895
    42
    okay = 1;
slouken@0
    43
slouken@1895
    44
    /* Lock the destination if it's in hardware */
slouken@1895
    45
    dst_locked = 0;
slouken@1895
    46
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
    47
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
    48
            okay = 0;
slouken@1895
    49
        } else {
slouken@1895
    50
            dst_locked = 1;
slouken@1895
    51
        }
slouken@1895
    52
    }
slouken@1895
    53
    /* Lock the source if it's in hardware */
slouken@1895
    54
    src_locked = 0;
slouken@1895
    55
    if (SDL_MUSTLOCK(src)) {
slouken@1895
    56
        if (SDL_LockSurface(src) < 0) {
slouken@1895
    57
            okay = 0;
slouken@1895
    58
        } else {
slouken@1895
    59
            src_locked = 1;
slouken@1895
    60
        }
slouken@1895
    61
    }
slouken@0
    62
slouken@1895
    63
    /* Set up source and destination buffer pointers, and BLIT! */
slouken@1895
    64
    if (okay && srcrect->w && srcrect->h) {
slouken@2263
    65
        SDL_BlitFunc RunBlit;
slouken@2262
    66
        SDL_BlitInfo *info = &src->map->info;
slouken@0
    67
slouken@1895
    68
        /* Set up the blit information */
slouken@2262
    69
        info->src = (Uint8 *) src->pixels +
slouken@1895
    70
            (Uint16) srcrect->y * src->pitch +
slouken@2262
    71
            (Uint16) srcrect->x * info->src_fmt->BytesPerPixel;
slouken@2263
    72
        info->src_w = srcrect->w;
slouken@2263
    73
        info->src_h = srcrect->h;
slouken@2267
    74
        info->src_skip =
slouken@2267
    75
            info->src_pitch - info->src_w * info->src_fmt->BytesPerPixel;
slouken@2267
    76
        info->dst =
slouken@2267
    77
            (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch +
slouken@2262
    78
            (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
slouken@2263
    79
        info->dst_w = dstrect->w;
slouken@2263
    80
        info->dst_h = dstrect->h;
slouken@2267
    81
        info->dst_skip =
slouken@2267
    82
            info->dst_pitch - info->dst_w * info->dst_fmt->BytesPerPixel;
slouken@2262
    83
        RunBlit = (SDL_BlitFunc) src->map->data;
slouken@0
    84
slouken@1895
    85
        /* Run the actual software blit */
slouken@2262
    86
        RunBlit(info);
slouken@1895
    87
    }
slouken@0
    88
slouken@1895
    89
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    90
    if (dst_locked) {
slouken@1895
    91
        SDL_UnlockSurface(dst);
slouken@1895
    92
    }
slouken@1895
    93
    if (src_locked) {
slouken@1895
    94
        SDL_UnlockSurface(src);
slouken@1895
    95
    }
slouken@1895
    96
    /* Blit is done! */
slouken@1895
    97
    return (okay ? 0 : -1);
slouken@0
    98
}
slouken@0
    99
slouken@2247
   100
#ifdef __MACOSX__
slouken@2247
   101
#include <sys/sysctl.h>
slouken@2247
   102
slouken@2249
   103
static SDL_bool
slouken@2249
   104
SDL_UseAltivecPrefetch()
slouken@2247
   105
{
slouken@2247
   106
    const char key[] = "hw.l3cachesize";
slouken@2247
   107
    u_int64_t result = 0;
slouken@2247
   108
    size_t typeSize = sizeof(result);
slouken@2247
   109
slouken@2247
   110
    if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
slouken@2247
   111
        return SDL_TRUE;
slouken@2247
   112
    } else {
slouken@2247
   113
        return SDL_FALSE;
slouken@2247
   114
    }
slouken@2247
   115
}
slouken@2247
   116
#else
slouken@2249
   117
static SDL_bool
slouken@2249
   118
SDL_UseAltivecPrefetch()
slouken@2247
   119
{
slouken@2247
   120
    /* Just guess G4 */
slouken@2247
   121
    return SDL_TRUE;
slouken@2247
   122
}
slouken@2247
   123
#endif /* __MACOSX__ */
slouken@2247
   124
slouken@2262
   125
static SDL_BlitFunc
slouken@2267
   126
SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags,
slouken@2267
   127
                   SDL_BlitFuncEntry * entries)
slouken@689
   128
{
slouken@2265
   129
    int i, flagcheck;
slouken@2247
   130
    static Uint32 features = 0xffffffff;
slouken@689
   131
slouken@2262
   132
    /* Get the available CPU features */
slouken@2247
   133
    if (features == 0xffffffff) {
slouken@2262
   134
        const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
slouken@2250
   135
slouken@2262
   136
        features = SDL_CPU_ANY;
slouken@689
   137
slouken@2250
   138
        /* Allow an override for testing .. */
slouken@2247
   139
        if (override) {
slouken@2247
   140
            SDL_sscanf(override, "%u", &features);
slouken@2247
   141
        } else {
slouken@2247
   142
            if (SDL_HasMMX()) {
slouken@2262
   143
                features |= SDL_CPU_MMX;
slouken@2262
   144
            }
slouken@2262
   145
            if (SDL_Has3DNow()) {
slouken@2262
   146
                features |= SDL_CPU_3DNOW;
slouken@2247
   147
            }
slouken@2247
   148
            if (SDL_HasSSE()) {
slouken@2262
   149
                features |= SDL_CPU_SSE;
slouken@2262
   150
            }
slouken@2262
   151
            if (SDL_HasSSE2()) {
slouken@2262
   152
                features |= SDL_CPU_SSE2;
slouken@2247
   153
            }
slouken@2250
   154
            if (SDL_HasAltiVec()) {
slouken@2247
   155
                if (SDL_UseAltivecPrefetch()) {
slouken@2262
   156
                    features |= SDL_CPU_ALTIVEC_PREFETCH;
slouken@2247
   157
                } else {
slouken@2262
   158
                    features |= SDL_CPU_ALTIVEC_NOPREFETCH;
slouken@2247
   159
                }
slouken@2247
   160
            }
slouken@1895
   161
        }
slouken@1895
   162
    }
slouken@2247
   163
slouken@2263
   164
    for (i = 0; entries[i].func; ++i) {
slouken@2265
   165
        /* Check for matching pixel formats */
slouken@2262
   166
        if (src_format != entries[i].src_format) {
slouken@2262
   167
            continue;
slouken@2247
   168
        }
slouken@2262
   169
        if (dst_format != entries[i].dst_format) {
slouken@2262
   170
            continue;
slouken@2262
   171
        }
slouken@2265
   172
slouken@2265
   173
        /* Check modulation flags */
slouken@2267
   174
        flagcheck =
slouken@2267
   175
            (flags & (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_COLOR));
slouken@2265
   176
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   177
            continue;
slouken@2262
   178
        }
slouken@2265
   179
slouken@2265
   180
        /* Check blend flags */
slouken@2267
   181
        flagcheck =
slouken@2267
   182
            (flags &
slouken@2267
   183
             (SDL_COPY_MASK | SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD));
slouken@2265
   184
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   185
            continue;
slouken@2262
   186
        }
slouken@2265
   187
slouken@2265
   188
        /* Check colorkey flag */
slouken@2265
   189
        flagcheck = (flags & SDL_COPY_COLORKEY);
slouken@2265
   190
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   191
            continue;
slouken@2265
   192
        }
slouken@2265
   193
slouken@2265
   194
        /* Check scaling flags */
slouken@2265
   195
        flagcheck = (flags & SDL_COPY_NEAREST);
slouken@2265
   196
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   197
            continue;
slouken@2265
   198
        }
slouken@2265
   199
slouken@2265
   200
        /* Check CPU features */
slouken@2265
   201
        flagcheck = entries[i].cpu;
slouken@2265
   202
        if ((flagcheck & features) != flagcheck) {
slouken@2265
   203
            continue;
slouken@2265
   204
        }
slouken@2265
   205
slouken@2265
   206
        /* We found the best one! */
slouken@2262
   207
        return entries[i].func;
slouken@2247
   208
    }
slouken@2262
   209
    return NULL;
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   213
int
slouken@1895
   214
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   215
{
slouken@2262
   216
    SDL_BlitFunc blit = NULL;
slouken@2266
   217
    SDL_BlitMap *map = surface->map;
slouken@2266
   218
    SDL_Surface *dst = map->dst;
slouken@0
   219
slouken@1895
   220
    /* Clean everything out to start */
slouken@1895
   221
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   222
        SDL_UnRLESurface(surface, 1);
slouken@1895
   223
    }
slouken@2266
   224
    map->blit = SDL_SoftBlit;
slouken@2266
   225
    map->info.src_fmt = surface->format;
slouken@2266
   226
    map->info.src_pitch = surface->pitch;
slouken@2266
   227
    map->info.dst_fmt = dst->format;
slouken@2266
   228
    map->info.dst_pitch = dst->pitch;
slouken@0
   229
slouken@2266
   230
    /* See if we can do RLE acceleration */
slouken@2267
   231
    if (surface->map->info.flags & SDL_COPY_RLE_DESIRED) {
slouken@2266
   232
        if (SDL_RLESurface(surface) == 0) {
bob@2329
   233
            surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@2266
   234
            return 0;
slouken@2266
   235
        }
slouken@2266
   236
    }
slouken@2266
   237
slouken@2266
   238
    /* Choose a standard blit function */
slouken@2801
   239
    if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
slouken@1895
   240
        /* Handle overlapping blits on the same surface */
slouken@2263
   241
        if (surface == dst) {
slouken@2257
   242
            blit = SDL_BlitCopyOverlap;
slouken@2247
   243
        } else {
slouken@2257
   244
            blit = SDL_BlitCopy;
slouken@1895
   245
        }
slouken@2266
   246
    } else if (surface->format->BitsPerPixel < 8) {
slouken@2267
   247
        blit = SDL_CalculateBlit0(surface);
slouken@2266
   248
    } else if (surface->format->BytesPerPixel == 1) {
slouken@2267
   249
        blit = SDL_CalculateBlit1(surface);
slouken@2267
   250
    } else if (map->info.flags & SDL_COPY_BLEND) {
slouken@2267
   251
        blit = SDL_CalculateBlitA(surface);
slouken@1895
   252
    } else {
slouken@2267
   253
        blit = SDL_CalculateBlitN(surface);
slouken@1895
   254
    }
slouken@2262
   255
    if (blit == NULL) {
slouken@2267
   256
        Uint32 src_format =
slouken@2267
   257
            SDL_MasksToPixelFormatEnum(surface->format->BitsPerPixel,
slouken@2267
   258
                                       surface->format->Rmask,
slouken@2267
   259
                                       surface->format->Gmask,
slouken@2267
   260
                                       surface->format->Bmask,
slouken@2267
   261
                                       surface->format->Amask);
slouken@2267
   262
        Uint32 dst_format =
slouken@2267
   263
            SDL_MasksToPixelFormatEnum(dst->format->BitsPerPixel,
bob@2329
   264
                                       dst->format->Rmask,
bob@2328
   265
                                       dst->format->Gmask,
slouken@2267
   266
                                       dst->format->Bmask,
slouken@2267
   267
                                       dst->format->Amask);
slouken@2267
   268
slouken@2267
   269
        blit =
slouken@2267
   270
            SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
slouken@2267
   271
                               SDL_GeneratedBlitFuncTable);
slouken@2262
   272
    }
slouken@2267
   273
    map->data = blit;
slouken@2262
   274
slouken@1895
   275
    /* Make sure we have a blit function */
slouken@2257
   276
    if (blit == NULL) {
slouken@2266
   277
        SDL_InvalidateMap(map);
slouken@1895
   278
        SDL_SetError("Blit combination not supported");
slouken@1895
   279
        return (-1);
slouken@1895
   280
    }
slouken@0
   281
slouken@1895
   282
    return (0);
slouken@0
   283
}
slouken@0
   284
slouken@1895
   285
/* vi: set ts=4 sw=4 expandtab: */