src/video/SDL_blit.c
author Sam Lantinga
Fri, 17 Aug 2007 06:21:58 +0000
changeset 2262 bee005ace1bf
parent 2260 202ddfd1cfb1
child 2263 900c35d8e8fd
permissions -rw-r--r--
Work in progress: merging new texture features into SDL blit system
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@2254
    27
#include "SDL_blit_copy.h"
slouken@0
    28
#include "SDL_RLEaccel_c.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@0
    30
slouken@0
    31
/* The general purpose software blit routine */
slouken@1895
    32
static int
slouken@1895
    33
SDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
    34
             SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
    35
{
slouken@1895
    36
    int okay;
slouken@1895
    37
    int src_locked;
slouken@1895
    38
    int dst_locked;
slouken@0
    39
slouken@1895
    40
    /* Everything is okay at the beginning...  */
slouken@1895
    41
    okay = 1;
slouken@0
    42
slouken@1895
    43
    /* Lock the destination if it's in hardware */
slouken@1895
    44
    dst_locked = 0;
slouken@1895
    45
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
    46
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
    47
            okay = 0;
slouken@1895
    48
        } else {
slouken@1895
    49
            dst_locked = 1;
slouken@1895
    50
        }
slouken@1895
    51
    }
slouken@1895
    52
    /* Lock the source if it's in hardware */
slouken@1895
    53
    src_locked = 0;
slouken@1895
    54
    if (SDL_MUSTLOCK(src)) {
slouken@1895
    55
        if (SDL_LockSurface(src) < 0) {
slouken@1895
    56
            okay = 0;
slouken@1895
    57
        } else {
slouken@1895
    58
            src_locked = 1;
slouken@1895
    59
        }
slouken@1895
    60
    }
slouken@0
    61
slouken@1895
    62
    /* Set up source and destination buffer pointers, and BLIT! */
slouken@1895
    63
    if (okay && srcrect->w && srcrect->h) {
slouken@2262
    64
        SDL_BlitInfo *info = &src->map->info;
slouken@0
    65
slouken@1895
    66
        /* Set up the blit information */
slouken@2262
    67
        info->src = (Uint8 *) src->pixels +
slouken@1895
    68
            (Uint16) srcrect->y * src->pitch +
slouken@2262
    69
            (Uint16) srcrect->x * info->src_fmt->BytesPerPixel;
slouken@2262
    70
        info.src_w = srcrect->w;
slouken@2262
    71
        info.src_h = srcrect->h;
slouken@2262
    72
        info.dst = (Uint8 *) dst->pixels +
slouken@1895
    73
            (Uint16) dstrect->y * dst->pitch +
slouken@2262
    74
            (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
slouken@2262
    75
        info.dst_w = dstrect->w;
slouken@2262
    76
        info.dst_h = dstrect->h;
slouken@2262
    77
        RunBlit = (SDL_BlitFunc) src->map->data;
slouken@0
    78
slouken@1895
    79
        /* Run the actual software blit */
slouken@2262
    80
        RunBlit(info);
slouken@1895
    81
    }
slouken@0
    82
slouken@1895
    83
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    84
    if (dst_locked) {
slouken@1895
    85
        SDL_UnlockSurface(dst);
slouken@1895
    86
    }
slouken@1895
    87
    if (src_locked) {
slouken@1895
    88
        SDL_UnlockSurface(src);
slouken@1895
    89
    }
slouken@1895
    90
    /* Blit is done! */
slouken@1895
    91
    return (okay ? 0 : -1);
slouken@0
    92
}
slouken@0
    93
slouken@2247
    94
#ifdef __MACOSX__
slouken@2247
    95
#include <sys/sysctl.h>
slouken@2247
    96
slouken@2249
    97
static SDL_bool
slouken@2249
    98
SDL_UseAltivecPrefetch()
slouken@689
    99
{
slouken@2247
   100
    const char key[] = "hw.l3cachesize";
slouken@2247
   101
    u_int64_t result = 0;
slouken@2247
   102
    size_t typeSize = sizeof(result);
slouken@689
   103
slouken@2247
   104
    if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
slouken@2247
   105
        return SDL_TRUE;
slouken@2247
   106
    } else {
slouken@2247
   107
        return SDL_FALSE;
slouken@1895
   108
    }
slouken@689
   109
}
slouken@2247
   110
#else
slouken@2249
   111
static SDL_bool
slouken@2249
   112
SDL_UseAltivecPrefetch()
slouken@2247
   113
{
slouken@2247
   114
    /* Just guess G4 */
slouken@2247
   115
    return SDL_TRUE;
slouken@2247
   116
}
slouken@2247
   117
#endif /* __MACOSX__ */
slouken@689
   118
slouken@2262
   119
static SDL_BlitFunc
slouken@2262
   120
SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags, SDL_BlitEntry * entries)
slouken@689
   121
{
slouken@1895
   122
    int i;
slouken@2247
   123
    static Uint32 features = 0xffffffff;
slouken@689
   124
slouken@2262
   125
    /* Get the available CPU features */
slouken@2247
   126
    if (features == 0xffffffff) {
slouken@2262
   127
        const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
slouken@2250
   128
slouken@2262
   129
        features = SDL_CPU_ANY;
slouken@0
   130
slouken@2250
   131
        /* Allow an override for testing .. */
slouken@2247
   132
        if (override) {
slouken@2247
   133
            SDL_sscanf(override, "%u", &features);
slouken@2247
   134
        } else {
slouken@2247
   135
            if (SDL_HasMMX()) {
slouken@2262
   136
                features |= SDL_CPU_MMX;
slouken@2262
   137
            }
slouken@2262
   138
            if (SDL_Has3DNow()) {
slouken@2262
   139
                features |= SDL_CPU_3DNOW;
slouken@2247
   140
            }
slouken@2247
   141
            if (SDL_HasSSE()) {
slouken@2262
   142
                features |= SDL_CPU_SSE;
slouken@2262
   143
            }
slouken@2262
   144
            if (SDL_HasSSE2()) {
slouken@2262
   145
                features |= SDL_CPU_SSE2;
slouken@2247
   146
            }
slouken@2250
   147
            if (SDL_HasAltiVec()) {
slouken@2247
   148
                if (SDL_UseAltivecPrefetch()) {
slouken@2262
   149
                    features |= SDL_CPU_ALTIVEC_PREFETCH;
slouken@2247
   150
                } else {
slouken@2262
   151
                    features |= SDL_CPU_ALTIVEC_NOPREFETCH;
slouken@2247
   152
                }
slouken@2247
   153
            }
slouken@1895
   154
        }
slouken@1895
   155
    }
slouken@2247
   156
slouken@2262
   157
    for (i = 0; entries[i].blit; ++i) {
slouken@2262
   158
        if (src_format != entries[i].src_format) {
slouken@2262
   159
            continue;
slouken@2262
   160
        }
slouken@2262
   161
        if (dst_format != entries[i].dst_format) {
slouken@2262
   162
            continue;
slouken@2247
   163
        }
slouken@2262
   164
        if ((flags & entries[i].flags) != flags) {
slouken@2262
   165
            continue;
slouken@2262
   166
        }
slouken@2262
   167
        if (!(features & entries[i].cpu)) {
slouken@2262
   168
            continue;
slouken@2262
   169
        }
slouken@2262
   170
        return entries[i].func;
slouken@2247
   171
    }
slouken@2262
   172
    return NULL;
slouken@0
   173
}
slouken@0
   174
slouken@0
   175
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   176
int
slouken@1895
   177
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   178
{
slouken@2262
   179
    SDL_BlitFunc blit = NULL;
slouken@1895
   180
    int blit_index;
slouken@0
   181
slouken@1895
   182
    /* Clean everything out to start */
slouken@1895
   183
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   184
        SDL_UnRLESurface(surface, 1);
slouken@1895
   185
    }
slouken@2257
   186
    surface->map->blit = NULL;
slouken@0
   187
slouken@1895
   188
    /* Get the blit function index, based on surface mode */
slouken@1895
   189
    /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@1895
   190
    blit_index = 0;
slouken@1895
   191
    blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
slouken@1895
   192
    if (surface->flags & SDL_SRCALPHA
slouken@2257
   193
        && ((surface->map->cmod >> 24) != SDL_ALPHA_OPAQUE
slouken@1895
   194
            || surface->format->Amask)) {
slouken@1895
   195
        blit_index |= 2;
slouken@1895
   196
    }
slouken@1895
   197
slouken@1895
   198
    /* Check for special "identity" case -- copy blit */
slouken@1895
   199
    if (surface->map->identity && blit_index == 0) {
slouken@1895
   200
        /* Handle overlapping blits on the same surface */
slouken@1895
   201
        if (surface == surface->map->dst) {
slouken@2257
   202
            blit = SDL_BlitCopyOverlap;
slouken@2247
   203
        } else {
slouken@2257
   204
            blit = SDL_BlitCopy;
slouken@1895
   205
        }
slouken@1895
   206
    } else {
slouken@1895
   207
        if (surface->format->BitsPerPixel < 8) {
slouken@2257
   208
            blit = SDL_CalculateBlit0(surface, blit_index);
slouken@1895
   209
        } else {
slouken@1895
   210
            switch (surface->format->BytesPerPixel) {
slouken@1895
   211
            case 1:
slouken@2257
   212
                blit = SDL_CalculateBlit1(surface, blit_index);
slouken@1895
   213
                break;
slouken@1895
   214
            case 2:
slouken@1895
   215
            case 3:
slouken@1895
   216
            case 4:
slouken@2257
   217
                blit = SDL_CalculateBlitN(surface, blit_index);
slouken@1895
   218
                break;
slouken@1895
   219
            }
slouken@1895
   220
        }
slouken@1895
   221
    }
slouken@2262
   222
    if (blit == NULL) {
slouken@2262
   223
        blit = SDL_ChooseBlitFunc(src_format, dst_format, surface->map->info.flags, SDL_GeneratedBlitFuncTable);
slouken@2262
   224
    }
slouken@2262
   225
slouken@1895
   226
    /* Make sure we have a blit function */
slouken@2257
   227
    if (blit == NULL) {
slouken@1895
   228
        SDL_InvalidateMap(surface->map);
slouken@1895
   229
        SDL_SetError("Blit combination not supported");
slouken@1895
   230
        return (-1);
slouken@1895
   231
    }
slouken@0
   232
slouken@1895
   233
    /* Choose software blitting function */
slouken@1895
   234
    if (surface->flags & SDL_RLEACCELOK) {
slouken@1895
   235
        if (surface->map->identity
slouken@1895
   236
            && (blit_index == 1
slouken@1895
   237
                || (blit_index == 3 && !surface->format->Amask))) {
slouken@1895
   238
            if (SDL_RLESurface(surface) == 0)
slouken@2257
   239
                surface->map->blit = SDL_RLEBlit;
slouken@1895
   240
        } else if (blit_index == 2 && surface->format->Amask) {
slouken@1895
   241
            if (SDL_RLESurface(surface) == 0)
slouken@2257
   242
                surface->map->blit = SDL_RLEAlphaBlit;
slouken@1895
   243
        }
slouken@1895
   244
    }
slouken@0
   245
slouken@2257
   246
    if (surface->map->blit == NULL) {
slouken@2257
   247
        surface->map->blit = SDL_SoftBlit;
slouken@2257
   248
        surface->map->data = blit;
slouken@1895
   249
    }
slouken@1895
   250
    return (0);
slouken@0
   251
}
slouken@0
   252
slouken@1895
   253
/* vi: set ts=4 sw=4 expandtab: */