src/video/SDL_blit.c
author Sam Lantinga
Sun, 13 Feb 2011 13:46:10 -0800
changeset 5288 d4381f3b0d1e
parent 5262 b530ef003506
child 5325 b9c224e16859
permissions -rw-r--r--
A few fixes:
Fixed creating render texture framebuffer.
Removed the need for palette watch, added surface format caching.
Added an SDL_DONTFREE flag so you can't free the window and 1.2 shadow surfaces.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@2824
    29
#include "SDL_blit_slow.h"
slouken@0
    30
#include "SDL_RLEaccel_c.h"
slouken@0
    31
#include "SDL_pixels_c.h"
slouken@0
    32
slouken@0
    33
/* The general purpose software blit routine */
slouken@1895
    34
static int
slouken@1895
    35
SDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
    36
             SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
    37
{
slouken@1895
    38
    int okay;
slouken@1895
    39
    int src_locked;
slouken@1895
    40
    int dst_locked;
slouken@0
    41
slouken@1895
    42
    /* Everything is okay at the beginning...  */
slouken@1895
    43
    okay = 1;
slouken@0
    44
slouken@1895
    45
    /* Lock the destination if it's in hardware */
slouken@1895
    46
    dst_locked = 0;
slouken@1895
    47
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
    48
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
    49
            okay = 0;
slouken@1895
    50
        } else {
slouken@1895
    51
            dst_locked = 1;
slouken@1895
    52
        }
slouken@1895
    53
    }
slouken@1895
    54
    /* Lock the source if it's in hardware */
slouken@1895
    55
    src_locked = 0;
slouken@1895
    56
    if (SDL_MUSTLOCK(src)) {
slouken@1895
    57
        if (SDL_LockSurface(src) < 0) {
slouken@1895
    58
            okay = 0;
slouken@1895
    59
        } else {
slouken@1895
    60
            src_locked = 1;
slouken@1895
    61
        }
slouken@1895
    62
    }
slouken@0
    63
slouken@1895
    64
    /* Set up source and destination buffer pointers, and BLIT! */
slouken@1895
    65
    if (okay && srcrect->w && srcrect->h) {
slouken@2263
    66
        SDL_BlitFunc RunBlit;
slouken@2262
    67
        SDL_BlitInfo *info = &src->map->info;
slouken@0
    68
slouken@1895
    69
        /* Set up the blit information */
slouken@2262
    70
        info->src = (Uint8 *) src->pixels +
slouken@1895
    71
            (Uint16) srcrect->y * src->pitch +
slouken@2262
    72
            (Uint16) srcrect->x * info->src_fmt->BytesPerPixel;
slouken@2263
    73
        info->src_w = srcrect->w;
slouken@2263
    74
        info->src_h = srcrect->h;
slouken@2850
    75
        info->src_pitch = src->pitch;
slouken@2267
    76
        info->src_skip =
slouken@2267
    77
            info->src_pitch - info->src_w * info->src_fmt->BytesPerPixel;
slouken@2267
    78
        info->dst =
slouken@2267
    79
            (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch +
slouken@2262
    80
            (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
slouken@2263
    81
        info->dst_w = dstrect->w;
slouken@2263
    82
        info->dst_h = dstrect->h;
slouken@2850
    83
        info->dst_pitch = dst->pitch;
slouken@2267
    84
        info->dst_skip =
slouken@2267
    85
            info->dst_pitch - info->dst_w * info->dst_fmt->BytesPerPixel;
slouken@2262
    86
        RunBlit = (SDL_BlitFunc) src->map->data;
slouken@0
    87
slouken@1895
    88
        /* Run the actual software blit */
slouken@2262
    89
        RunBlit(info);
slouken@1895
    90
    }
slouken@0
    91
slouken@1895
    92
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    93
    if (dst_locked) {
slouken@1895
    94
        SDL_UnlockSurface(dst);
slouken@1895
    95
    }
slouken@1895
    96
    if (src_locked) {
slouken@1895
    97
        SDL_UnlockSurface(src);
slouken@1895
    98
    }
slouken@1895
    99
    /* Blit is done! */
slouken@1895
   100
    return (okay ? 0 : -1);
slouken@0
   101
}
slouken@0
   102
slouken@689
   103
slouken@2262
   104
static SDL_BlitFunc
slouken@2267
   105
SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags,
slouken@2267
   106
                   SDL_BlitFuncEntry * entries)
slouken@689
   107
{
slouken@2265
   108
    int i, flagcheck;
slouken@2247
   109
    static Uint32 features = 0xffffffff;
slouken@689
   110
slouken@2262
   111
    /* Get the available CPU features */
slouken@2247
   112
    if (features == 0xffffffff) {
slouken@2262
   113
        const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
slouken@2250
   114
slouken@2262
   115
        features = SDL_CPU_ANY;
slouken@0
   116
slouken@2250
   117
        /* Allow an override for testing .. */
slouken@2247
   118
        if (override) {
slouken@2247
   119
            SDL_sscanf(override, "%u", &features);
slouken@2247
   120
        } else {
slouken@2247
   121
            if (SDL_HasMMX()) {
slouken@2262
   122
                features |= SDL_CPU_MMX;
slouken@2262
   123
            }
slouken@2247
   124
            if (SDL_HasSSE()) {
slouken@2262
   125
                features |= SDL_CPU_SSE;
slouken@2262
   126
            }
slouken@2262
   127
            if (SDL_HasSSE2()) {
slouken@2262
   128
                features |= SDL_CPU_SSE2;
slouken@2247
   129
            }
slouken@1895
   130
        }
slouken@1895
   131
    }
slouken@2247
   132
slouken@2263
   133
    for (i = 0; entries[i].func; ++i) {
slouken@2265
   134
        /* Check for matching pixel formats */
slouken@2262
   135
        if (src_format != entries[i].src_format) {
slouken@2262
   136
            continue;
slouken@2262
   137
        }
slouken@2262
   138
        if (dst_format != entries[i].dst_format) {
slouken@2262
   139
            continue;
slouken@2247
   140
        }
slouken@2265
   141
slouken@2265
   142
        /* Check modulation flags */
slouken@2267
   143
        flagcheck =
slouken@2837
   144
            (flags & (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA));
slouken@2265
   145
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   146
            continue;
slouken@2265
   147
        }
slouken@2265
   148
slouken@2265
   149
        /* Check blend flags */
slouken@5184
   150
        flagcheck =
slouken@5184
   151
            (flags &
slouken@5184
   152
             (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD));
slouken@2265
   153
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   154
            continue;
slouken@2262
   155
        }
slouken@2265
   156
slouken@2265
   157
        /* Check colorkey flag */
slouken@2265
   158
        flagcheck = (flags & SDL_COPY_COLORKEY);
slouken@2265
   159
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   160
            continue;
slouken@2262
   161
        }
slouken@2265
   162
slouken@2265
   163
        /* Check scaling flags */
slouken@2265
   164
        flagcheck = (flags & SDL_COPY_NEAREST);
slouken@2265
   165
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   166
            continue;
slouken@2265
   167
        }
slouken@2265
   168
slouken@2265
   169
        /* Check CPU features */
slouken@2265
   170
        flagcheck = entries[i].cpu;
slouken@2265
   171
        if ((flagcheck & features) != flagcheck) {
slouken@2265
   172
            continue;
slouken@2265
   173
        }
slouken@2265
   174
slouken@2265
   175
        /* We found the best one! */
slouken@2262
   176
        return entries[i].func;
slouken@2247
   177
    }
slouken@2262
   178
    return NULL;
slouken@0
   179
}
slouken@0
   180
slouken@0
   181
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   182
int
slouken@1895
   183
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   184
{
slouken@2262
   185
    SDL_BlitFunc blit = NULL;
slouken@2266
   186
    SDL_BlitMap *map = surface->map;
slouken@2266
   187
    SDL_Surface *dst = map->dst;
slouken@0
   188
slouken@1895
   189
    /* Clean everything out to start */
slouken@1895
   190
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   191
        SDL_UnRLESurface(surface, 1);
slouken@1895
   192
    }
slouken@2266
   193
    map->blit = SDL_SoftBlit;
slouken@2266
   194
    map->info.src_fmt = surface->format;
slouken@2266
   195
    map->info.src_pitch = surface->pitch;
slouken@2266
   196
    map->info.dst_fmt = dst->format;
slouken@2266
   197
    map->info.dst_pitch = dst->pitch;
slouken@0
   198
slouken@2266
   199
    /* See if we can do RLE acceleration */
slouken@2805
   200
    if (map->info.flags & SDL_COPY_RLE_DESIRED) {
slouken@2266
   201
        if (SDL_RLESurface(surface) == 0) {
slouken@2266
   202
            return 0;
slouken@2266
   203
        }
slouken@2266
   204
    }
slouken@2266
   205
slouken@2266
   206
    /* Choose a standard blit function */
slouken@2801
   207
    if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
slouken@1895
   208
        /* Handle overlapping blits on the same surface */
slouken@2263
   209
        if (surface == dst) {
slouken@2257
   210
            blit = SDL_BlitCopyOverlap;
slouken@2247
   211
        } else {
slouken@2257
   212
            blit = SDL_BlitCopy;
slouken@1895
   213
        }
slouken@2266
   214
    } else if (surface->format->BitsPerPixel < 8) {
slouken@2267
   215
        blit = SDL_CalculateBlit0(surface);
slouken@2266
   216
    } else if (surface->format->BytesPerPixel == 1) {
slouken@2267
   217
        blit = SDL_CalculateBlit1(surface);
slouken@2267
   218
    } else if (map->info.flags & SDL_COPY_BLEND) {
slouken@2267
   219
        blit = SDL_CalculateBlitA(surface);
slouken@1895
   220
    } else {
slouken@2267
   221
        blit = SDL_CalculateBlitN(surface);
slouken@1895
   222
    }
slouken@2262
   223
    if (blit == NULL) {
slouken@5288
   224
        Uint32 src_format = surface->format->format;
slouken@5288
   225
        Uint32 dst_format = dst->format->format;
slouken@2267
   226
slouken@2267
   227
        blit =
slouken@2267
   228
            SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
slouken@2267
   229
                               SDL_GeneratedBlitFuncTable);
slouken@2262
   230
    }
slouken@2824
   231
#ifndef TEST_SLOW_BLIT
slouken@2824
   232
    if (blit == NULL)
slouken@2824
   233
#endif
slouken@2824
   234
    {
slouken@2824
   235
        if (surface->format->BytesPerPixel > 1
slouken@2824
   236
            && dst->format->BytesPerPixel > 1) {
slouken@2824
   237
            blit = SDL_Blit_Slow;
slouken@2824
   238
        }
slouken@2824
   239
    }
slouken@2267
   240
    map->data = blit;
slouken@2262
   241
slouken@1895
   242
    /* Make sure we have a blit function */
slouken@2257
   243
    if (blit == NULL) {
slouken@2266
   244
        SDL_InvalidateMap(map);
slouken@1895
   245
        SDL_SetError("Blit combination not supported");
slouken@1895
   246
        return (-1);
slouken@1895
   247
    }
slouken@0
   248
slouken@1895
   249
    return (0);
slouken@0
   250
}
slouken@0
   251
slouken@1895
   252
/* vi: set ts=4 sw=4 expandtab: */