src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Aug 2007 22:18:53 +0000
changeset 2255 17b2369756be
parent 2254 79e00f5561f4
child 2257 340942cfda48
permissions -rw-r--r--
Use MMX intrinsics over GCC inline assembly
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@1895
    64
        SDL_BlitInfo info;
slouken@1895
    65
        SDL_loblit RunBlit;
slouken@0
    66
slouken@1895
    67
        /* Set up the blit information */
slouken@1895
    68
        info.s_pixels = (Uint8 *) src->pixels +
slouken@1895
    69
            (Uint16) srcrect->y * src->pitch +
slouken@1895
    70
            (Uint16) srcrect->x * src->format->BytesPerPixel;
slouken@1895
    71
        info.s_width = srcrect->w;
slouken@1895
    72
        info.s_height = srcrect->h;
slouken@1895
    73
        info.s_skip = src->pitch - info.s_width * src->format->BytesPerPixel;
slouken@1895
    74
        info.d_pixels = (Uint8 *) dst->pixels +
slouken@1895
    75
            (Uint16) dstrect->y * dst->pitch +
slouken@1895
    76
            (Uint16) dstrect->x * dst->format->BytesPerPixel;
slouken@1895
    77
        info.d_width = dstrect->w;
slouken@1895
    78
        info.d_height = dstrect->h;
slouken@1895
    79
        info.d_skip = dst->pitch - info.d_width * dst->format->BytesPerPixel;
slouken@1895
    80
        info.aux_data = src->map->sw_data->aux_data;
slouken@1895
    81
        info.src = src->format;
slouken@1895
    82
        info.table = src->map->table;
slouken@1895
    83
        info.dst = dst->format;
slouken@1895
    84
        RunBlit = src->map->sw_data->blit;
slouken@0
    85
slouken@1895
    86
        /* Run the actual software blit */
slouken@1895
    87
        RunBlit(&info);
slouken@1895
    88
    }
slouken@0
    89
slouken@1895
    90
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    91
    if (dst_locked) {
slouken@1895
    92
        SDL_UnlockSurface(dst);
slouken@1895
    93
    }
slouken@1895
    94
    if (src_locked) {
slouken@1895
    95
        SDL_UnlockSurface(src);
slouken@1895
    96
    }
slouken@1895
    97
    /* Blit is done! */
slouken@1895
    98
    return (okay ? 0 : -1);
slouken@0
    99
}
slouken@0
   100
slouken@2247
   101
#ifdef __MACOSX__
slouken@2247
   102
#include <sys/sysctl.h>
slouken@2247
   103
slouken@2249
   104
static SDL_bool
slouken@2249
   105
SDL_UseAltivecPrefetch()
slouken@2247
   106
{
slouken@2247
   107
    const char key[] = "hw.l3cachesize";
slouken@2247
   108
    u_int64_t result = 0;
slouken@2247
   109
    size_t typeSize = sizeof(result);
slouken@2247
   110
slouken@2247
   111
    if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
slouken@2247
   112
        return SDL_TRUE;
slouken@2247
   113
    } else {
slouken@2247
   114
        return SDL_FALSE;
slouken@2247
   115
    }
slouken@2247
   116
}
slouken@2247
   117
#else
slouken@2249
   118
static SDL_bool
slouken@2249
   119
SDL_UseAltivecPrefetch()
slouken@2247
   120
{
slouken@2247
   121
    /* Just guess G4 */
slouken@2247
   122
    return SDL_TRUE;
slouken@2247
   123
}
slouken@2247
   124
#endif /* __MACOSX__ */
slouken@2247
   125
slouken@2249
   126
static SDL_loblit
slouken@2249
   127
SDL_ChooseBlitFunc(SDL_BlitEntry * entries, int count)
slouken@689
   128
{
slouken@1895
   129
    int i;
slouken@2247
   130
    static Uint32 features = 0xffffffff;
slouken@689
   131
slouken@2247
   132
    if (features == 0xffffffff) {
slouken@2250
   133
        const char *override = SDL_getenv("SDL_BLIT_FEATURES");
slouken@2250
   134
slouken@2247
   135
        features = SDL_BLIT_ANY;
slouken@689
   136
slouken@2250
   137
        /* Allow an override for testing .. */
slouken@2247
   138
        if (override) {
slouken@2247
   139
            SDL_sscanf(override, "%u", &features);
slouken@2247
   140
        } else {
slouken@2247
   141
            if (SDL_HasMMX()) {
slouken@2247
   142
                features |= SDL_BLIT_MMX;
slouken@2247
   143
            }
slouken@2247
   144
            if (SDL_HasSSE()) {
slouken@2247
   145
                features |= SDL_BLIT_SSE;
slouken@2247
   146
            }
slouken@2250
   147
            if (SDL_HasAltiVec()) {
slouken@2247
   148
                if (SDL_UseAltivecPrefetch()) {
slouken@2247
   149
                    features |= SDL_BLIT_ALTIVEC_PREFETCH;
slouken@2247
   150
                } else {
slouken@2247
   151
                    features |= SDL_BLIT_ALTIVEC_NOPREFETCH;
slouken@2247
   152
                }
slouken@2247
   153
            }
slouken@1895
   154
        }
slouken@1895
   155
    }
slouken@2247
   156
slouken@2247
   157
    for (i = count; i > 0; --i) {
slouken@2247
   158
        if (features & entries[i].features) {
slouken@2247
   159
            return entries[i].blit;
slouken@2247
   160
        }
slouken@2247
   161
    }
slouken@2247
   162
    return entries[0].blit;
slouken@0
   163
}
slouken@0
   164
slouken@0
   165
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   166
int
slouken@1895
   167
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   168
{
slouken@1895
   169
    int blit_index;
slouken@0
   170
slouken@1895
   171
    /* Clean everything out to start */
slouken@1895
   172
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   173
        SDL_UnRLESurface(surface, 1);
slouken@1895
   174
    }
slouken@1895
   175
    surface->map->sw_blit = NULL;
slouken@0
   176
slouken@1895
   177
    /* Get the blit function index, based on surface mode */
slouken@1895
   178
    /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
slouken@1895
   179
    blit_index = 0;
slouken@1895
   180
    blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
slouken@1895
   181
    if (surface->flags & SDL_SRCALPHA
slouken@1895
   182
        && (surface->format->alpha != SDL_ALPHA_OPAQUE
slouken@1895
   183
            || surface->format->Amask)) {
slouken@1895
   184
        blit_index |= 2;
slouken@1895
   185
    }
slouken@0
   186
slouken@1895
   187
    /* Check for special "identity" case -- copy blit */
slouken@1895
   188
    if (surface->map->identity && blit_index == 0) {
slouken@1895
   189
        /* Handle overlapping blits on the same surface */
slouken@1895
   190
        if (surface == surface->map->dst) {
slouken@1895
   191
            surface->map->sw_data->blit = SDL_BlitCopyOverlap;
slouken@2247
   192
        } else {
slouken@2247
   193
            surface->map->sw_data->blit = SDL_BlitCopy;
slouken@1895
   194
        }
slouken@1895
   195
    } else {
slouken@1895
   196
        if (surface->format->BitsPerPixel < 8) {
slouken@1895
   197
            surface->map->sw_data->blit =
slouken@1895
   198
                SDL_CalculateBlit0(surface, blit_index);
slouken@1895
   199
        } else {
slouken@1895
   200
            switch (surface->format->BytesPerPixel) {
slouken@1895
   201
            case 1:
slouken@1895
   202
                surface->map->sw_data->blit =
slouken@1895
   203
                    SDL_CalculateBlit1(surface, blit_index);
slouken@1895
   204
                break;
slouken@1895
   205
            case 2:
slouken@1895
   206
            case 3:
slouken@1895
   207
            case 4:
slouken@1895
   208
                surface->map->sw_data->blit =
slouken@1895
   209
                    SDL_CalculateBlitN(surface, blit_index);
slouken@1895
   210
                break;
slouken@1895
   211
            default:
slouken@1895
   212
                surface->map->sw_data->blit = NULL;
slouken@1895
   213
                break;
slouken@1895
   214
            }
slouken@1895
   215
        }
slouken@1895
   216
    }
slouken@1895
   217
    /* Make sure we have a blit function */
slouken@1895
   218
    if (surface->map->sw_data->blit == NULL) {
slouken@1895
   219
        SDL_InvalidateMap(surface->map);
slouken@1895
   220
        SDL_SetError("Blit combination not supported");
slouken@1895
   221
        return (-1);
slouken@1895
   222
    }
slouken@0
   223
slouken@1895
   224
    /* Choose software blitting function */
slouken@1895
   225
    if (surface->flags & SDL_RLEACCELOK) {
slouken@1895
   226
        if (surface->map->identity
slouken@1895
   227
            && (blit_index == 1
slouken@1895
   228
                || (blit_index == 3 && !surface->format->Amask))) {
slouken@1895
   229
            if (SDL_RLESurface(surface) == 0)
slouken@1895
   230
                surface->map->sw_blit = SDL_RLEBlit;
slouken@1895
   231
        } else if (blit_index == 2 && surface->format->Amask) {
slouken@1895
   232
            if (SDL_RLESurface(surface) == 0)
slouken@1895
   233
                surface->map->sw_blit = SDL_RLEAlphaBlit;
slouken@1895
   234
        }
slouken@1895
   235
    }
slouken@0
   236
slouken@1895
   237
    if (surface->map->sw_blit == NULL) {
slouken@1895
   238
        surface->map->sw_blit = SDL_SoftBlit;
slouken@1895
   239
    }
slouken@1895
   240
    return (0);
slouken@0
   241
}
slouken@0
   242
slouken@1895
   243
/* vi: set ts=4 sw=4 expandtab: */