src/video/SDL_blit.c
author Sam Lantinga
Fri, 17 Aug 2007 06:58:20 +0000
changeset 2265 265bb136af92
parent 2263 900c35d8e8fd
child 2266 e61ad15a205f
permissions -rw-r--r--
More work in progress...
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@2263
    74
        info->dst = (Uint8 *) dst->pixels +
slouken@1895
    75
            (Uint16) dstrect->y * dst->pitch +
slouken@2262
    76
            (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
slouken@2263
    77
        info->dst_w = dstrect->w;
slouken@2263
    78
        info->dst_h = dstrect->h;
slouken@2262
    79
        RunBlit = (SDL_BlitFunc) src->map->data;
slouken@0
    80
slouken@1895
    81
        /* Run the actual software blit */
slouken@2262
    82
        RunBlit(info);
slouken@1895
    83
    }
slouken@0
    84
slouken@1895
    85
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    86
    if (dst_locked) {
slouken@1895
    87
        SDL_UnlockSurface(dst);
slouken@1895
    88
    }
slouken@1895
    89
    if (src_locked) {
slouken@1895
    90
        SDL_UnlockSurface(src);
slouken@1895
    91
    }
slouken@1895
    92
    /* Blit is done! */
slouken@1895
    93
    return (okay ? 0 : -1);
slouken@0
    94
}
slouken@0
    95
slouken@2247
    96
#ifdef __MACOSX__
slouken@2247
    97
#include <sys/sysctl.h>
slouken@2247
    98
slouken@2249
    99
static SDL_bool
slouken@2249
   100
SDL_UseAltivecPrefetch()
slouken@689
   101
{
slouken@2247
   102
    const char key[] = "hw.l3cachesize";
slouken@2247
   103
    u_int64_t result = 0;
slouken@2247
   104
    size_t typeSize = sizeof(result);
slouken@689
   105
slouken@2247
   106
    if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
slouken@2247
   107
        return SDL_TRUE;
slouken@2247
   108
    } else {
slouken@2247
   109
        return SDL_FALSE;
slouken@1895
   110
    }
slouken@689
   111
}
slouken@2247
   112
#else
slouken@2249
   113
static SDL_bool
slouken@2249
   114
SDL_UseAltivecPrefetch()
slouken@2247
   115
{
slouken@2247
   116
    /* Just guess G4 */
slouken@2247
   117
    return SDL_TRUE;
slouken@2247
   118
}
slouken@2247
   119
#endif /* __MACOSX__ */
slouken@689
   120
slouken@2262
   121
static SDL_BlitFunc
slouken@2263
   122
SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags, SDL_BlitFuncEntry * entries)
slouken@689
   123
{
slouken@2265
   124
    int i, flagcheck;
slouken@2247
   125
    static Uint32 features = 0xffffffff;
slouken@689
   126
slouken@2262
   127
    /* Get the available CPU features */
slouken@2247
   128
    if (features == 0xffffffff) {
slouken@2262
   129
        const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
slouken@2250
   130
slouken@2262
   131
        features = SDL_CPU_ANY;
slouken@0
   132
slouken@2250
   133
        /* Allow an override for testing .. */
slouken@2247
   134
        if (override) {
slouken@2247
   135
            SDL_sscanf(override, "%u", &features);
slouken@2247
   136
        } else {
slouken@2247
   137
            if (SDL_HasMMX()) {
slouken@2262
   138
                features |= SDL_CPU_MMX;
slouken@2262
   139
            }
slouken@2262
   140
            if (SDL_Has3DNow()) {
slouken@2262
   141
                features |= SDL_CPU_3DNOW;
slouken@2247
   142
            }
slouken@2247
   143
            if (SDL_HasSSE()) {
slouken@2262
   144
                features |= SDL_CPU_SSE;
slouken@2262
   145
            }
slouken@2262
   146
            if (SDL_HasSSE2()) {
slouken@2262
   147
                features |= SDL_CPU_SSE2;
slouken@2247
   148
            }
slouken@2250
   149
            if (SDL_HasAltiVec()) {
slouken@2247
   150
                if (SDL_UseAltivecPrefetch()) {
slouken@2262
   151
                    features |= SDL_CPU_ALTIVEC_PREFETCH;
slouken@2247
   152
                } else {
slouken@2262
   153
                    features |= SDL_CPU_ALTIVEC_NOPREFETCH;
slouken@2247
   154
                }
slouken@2247
   155
            }
slouken@1895
   156
        }
slouken@1895
   157
    }
slouken@2247
   158
slouken@2263
   159
    for (i = 0; entries[i].func; ++i) {
slouken@2265
   160
        /* Check for matching pixel formats */
slouken@2262
   161
        if (src_format != entries[i].src_format) {
slouken@2262
   162
            continue;
slouken@2262
   163
        }
slouken@2262
   164
        if (dst_format != entries[i].dst_format) {
slouken@2262
   165
            continue;
slouken@2247
   166
        }
slouken@2265
   167
slouken@2265
   168
        /* Check modulation flags */
slouken@2265
   169
        flagcheck = (flags & (SDL_COPY_MODULATE_COLOR|SDL_COPY_MODULATE_COLOR));
slouken@2265
   170
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   171
            continue;
slouken@2265
   172
        }
slouken@2265
   173
slouken@2265
   174
        /* Check blend flags */
slouken@2265
   175
        flagcheck = (flags & (SDL_COPY_MASK|SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD));
slouken@2265
   176
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   177
            continue;
slouken@2262
   178
        }
slouken@2265
   179
slouken@2265
   180
        /* Check colorkey flag */
slouken@2265
   181
        flagcheck = (flags & SDL_COPY_COLORKEY);
slouken@2265
   182
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   183
            continue;
slouken@2262
   184
        }
slouken@2265
   185
slouken@2265
   186
        /* Check scaling flags */
slouken@2265
   187
        flagcheck = (flags & SDL_COPY_NEAREST);
slouken@2265
   188
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   189
            continue;
slouken@2265
   190
        }
slouken@2265
   191
slouken@2265
   192
        /* Check CPU features */
slouken@2265
   193
        flagcheck = entries[i].cpu;
slouken@2265
   194
        if ((flagcheck & features) != flagcheck) {
slouken@2265
   195
            continue;
slouken@2265
   196
        }
slouken@2265
   197
slouken@2265
   198
        /* We found the best one! */
slouken@2262
   199
        return entries[i].func;
slouken@2247
   200
    }
slouken@2262
   201
    return NULL;
slouken@0
   202
}
slouken@0
   203
slouken@0
   204
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   205
int
slouken@1895
   206
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   207
{
slouken@2262
   208
    SDL_BlitFunc blit = NULL;
slouken@2263
   209
    SDL_Surface *dst = surface->map->dst;
slouken@2263
   210
    Uint32 src_format;
slouken@2263
   211
    Uint32 dst_format;
slouken@0
   212
slouken@1895
   213
    /* Clean everything out to start */
slouken@1895
   214
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   215
        SDL_UnRLESurface(surface, 1);
slouken@1895
   216
    }
slouken@2257
   217
    surface->map->blit = NULL;
slouken@2263
   218
    surface->map->info.src_fmt = surface->format;
slouken@2263
   219
    surface->map->info.src_pitch = surface->pitch;
slouken@2263
   220
    surface->map->info.dst_fmt = dst->format;
slouken@2263
   221
    surface->map->info.dst_pitch = dst->pitch;
slouken@0
   222
slouken@2263
   223
    src_format = SDL_MasksToPixelFormatEnum(surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
slouken@2263
   224
    dst_format = SDL_MasksToPixelFormatEnum(dst->format->BitsPerPixel, dst->format->Rmask, dst->format->Gmask, dst->format->Bmask, dst->format->Amask);
slouken@1895
   225
slouken@1895
   226
    /* Check for special "identity" case -- copy blit */
slouken@2263
   227
    if (surface->map->identity && !surface->map->info.flags) {
slouken@1895
   228
        /* Handle overlapping blits on the same surface */
slouken@2263
   229
        if (surface == dst) {
slouken@2257
   230
            blit = SDL_BlitCopyOverlap;
slouken@2247
   231
        } else {
slouken@2257
   232
            blit = SDL_BlitCopy;
slouken@1895
   233
        }
slouken@1895
   234
    } else {
slouken@1895
   235
        if (surface->format->BitsPerPixel < 8) {
slouken@2263
   236
            blit = SDL_ChooseBlitFunc(src_format, dst_format, surface->map->info.flags, SDL_BlitFuncTable0);
slouken@1895
   237
        } else {
slouken@1895
   238
            switch (surface->format->BytesPerPixel) {
slouken@1895
   239
            case 1:
slouken@2263
   240
                blit = SDL_ChooseBlitFunc(src_format, dst_format, surface->map->info.flags, SDL_BlitFuncTable1);
slouken@1895
   241
                break;
slouken@1895
   242
            case 2:
slouken@1895
   243
            case 3:
slouken@1895
   244
            case 4:
slouken@2263
   245
                blit = SDL_ChooseBlitFunc(src_format, dst_format, surface->map->info.flags, SDL_BlitFuncTableN);
slouken@1895
   246
                break;
slouken@1895
   247
            }
slouken@1895
   248
        }
slouken@1895
   249
    }
slouken@2262
   250
    if (blit == NULL) {
slouken@2262
   251
        blit = SDL_ChooseBlitFunc(src_format, dst_format, surface->map->info.flags, SDL_GeneratedBlitFuncTable);
slouken@2262
   252
    }
slouken@2262
   253
slouken@1895
   254
    /* Make sure we have a blit function */
slouken@2257
   255
    if (blit == NULL) {
slouken@1895
   256
        SDL_InvalidateMap(surface->map);
slouken@1895
   257
        SDL_SetError("Blit combination not supported");
slouken@1895
   258
        return (-1);
slouken@1895
   259
    }
slouken@0
   260
slouken@1895
   261
    /* Choose software blitting function */
slouken@2265
   262
    if ((surface->flags & SDL_RLEACCELOK) && !(surface->map->flags & () {
slouken@2265
   263
        if (surface->map->identity && (surface->map->flags & SDL_COPY_COLORKEY)
slouken@1895
   264
            && (blit_index == 1
slouken@1895
   265
                || (blit_index == 3 && !surface->format->Amask))) {
slouken@1895
   266
            if (SDL_RLESurface(surface) == 0)
slouken@2257
   267
                surface->map->blit = SDL_RLEBlit;
slouken@1895
   268
        } else if (blit_index == 2 && surface->format->Amask) {
slouken@1895
   269
            if (SDL_RLESurface(surface) == 0)
slouken@2257
   270
                surface->map->blit = SDL_RLEAlphaBlit;
slouken@1895
   271
        }
slouken@1895
   272
    }
slouken@0
   273
slouken@2257
   274
    if (surface->map->blit == NULL) {
slouken@2257
   275
        surface->map->blit = SDL_SoftBlit;
slouken@2257
   276
        surface->map->data = blit;
slouken@1895
   277
    }
slouken@1895
   278
    return (0);
slouken@0
   279
}
slouken@0
   280
slouken@1895
   281
/* vi: set ts=4 sw=4 expandtab: */