src/video/SDL_blit.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 30 Jan 2018 18:08:34 -0800
changeset 11839 971881e55d61
parent 11811 5d94cb6b24d3
child 11872 e917e911dab6
permissions -rw-r--r--
Fixed ISO C99 compatibility
SDL now builds with gcc 7.2 with the following command line options:
-Wall -pedantic-errors -Wno-deprecated-declarations -Wno-overlength-strings --std=c99
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
#include "SDL_video.h"
slouken@0
    24
#include "SDL_sysvideo.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@2263
    26
#include "SDL_blit_auto.h"
slouken@2254
    27
#include "SDL_blit_copy.h"
slouken@2824
    28
#include "SDL_blit_slow.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@11284
    33
static int SDLCALL
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@7549
    64
    if (okay && !SDL_RectEmpty(srcrect)) {
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@2850
    74
        info->src_pitch = src->pitch;
slouken@2267
    75
        info->src_skip =
slouken@2267
    76
            info->src_pitch - info->src_w * info->src_fmt->BytesPerPixel;
slouken@2267
    77
        info->dst =
slouken@2267
    78
            (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch +
slouken@2262
    79
            (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
slouken@2263
    80
        info->dst_w = dstrect->w;
slouken@2263
    81
        info->dst_h = dstrect->h;
slouken@2850
    82
        info->dst_pitch = dst->pitch;
slouken@2267
    83
        info->dst_skip =
slouken@2267
    84
            info->dst_pitch - info->dst_w * info->dst_fmt->BytesPerPixel;
slouken@11839
    85
        *(void**)&RunBlit = src->map->data;
slouken@0
    86
slouken@1895
    87
        /* Run the actual software blit */
slouken@2262
    88
        RunBlit(info);
slouken@1895
    89
    }
slouken@0
    90
slouken@1895
    91
    /* We need to unlock the surfaces if they're locked */
slouken@1895
    92
    if (dst_locked) {
slouken@1895
    93
        SDL_UnlockSurface(dst);
slouken@1895
    94
    }
slouken@1895
    95
    if (src_locked) {
slouken@1895
    96
        SDL_UnlockSurface(src);
slouken@1895
    97
    }
slouken@1895
    98
    /* Blit is done! */
slouken@1895
    99
    return (okay ? 0 : -1);
slouken@0
   100
}
slouken@0
   101
slouken@5389
   102
#ifdef __MACOSX__
slouken@5389
   103
#include <sys/sysctl.h>
slouken@5389
   104
slouken@5389
   105
static SDL_bool
slouken@5389
   106
SDL_UseAltivecPrefetch()
slouken@5389
   107
{
slouken@5389
   108
    const char key[] = "hw.l3cachesize";
slouken@5389
   109
    u_int64_t result = 0;
slouken@5389
   110
    size_t typeSize = sizeof(result);
slouken@5389
   111
slouken@5389
   112
    if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
slouken@5389
   113
        return SDL_TRUE;
slouken@5389
   114
    } else {
slouken@5389
   115
        return SDL_FALSE;
slouken@5389
   116
    }
slouken@5389
   117
}
slouken@5389
   118
#else
slouken@5389
   119
static SDL_bool
slouken@5389
   120
SDL_UseAltivecPrefetch()
slouken@5389
   121
{
slouken@5389
   122
    /* Just guess G4 */
slouken@5389
   123
    return SDL_TRUE;
slouken@5389
   124
}
slouken@5389
   125
#endif /* __MACOSX__ */
slouken@2247
   126
slouken@2262
   127
static SDL_BlitFunc
slouken@2267
   128
SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags,
slouken@2267
   129
                   SDL_BlitFuncEntry * entries)
slouken@689
   130
{
slouken@2265
   131
    int i, flagcheck;
slouken@2247
   132
    static Uint32 features = 0xffffffff;
slouken@689
   133
slouken@2262
   134
    /* Get the available CPU features */
slouken@2247
   135
    if (features == 0xffffffff) {
slouken@2262
   136
        const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
slouken@2250
   137
slouken@2262
   138
        features = SDL_CPU_ANY;
slouken@689
   139
slouken@2250
   140
        /* Allow an override for testing .. */
slouken@2247
   141
        if (override) {
slouken@2247
   142
            SDL_sscanf(override, "%u", &features);
slouken@2247
   143
        } else {
slouken@2247
   144
            if (SDL_HasMMX()) {
slouken@2262
   145
                features |= SDL_CPU_MMX;
slouken@2262
   146
            }
slouken@5389
   147
            if (SDL_Has3DNow()) {
slouken@5389
   148
                features |= SDL_CPU_3DNOW;
slouken@5389
   149
            }
slouken@2247
   150
            if (SDL_HasSSE()) {
slouken@2262
   151
                features |= SDL_CPU_SSE;
slouken@2262
   152
            }
slouken@2262
   153
            if (SDL_HasSSE2()) {
slouken@2262
   154
                features |= SDL_CPU_SSE2;
slouken@2247
   155
            }
slouken@5389
   156
            if (SDL_HasAltiVec()) {
slouken@5389
   157
                if (SDL_UseAltivecPrefetch()) {
slouken@5389
   158
                    features |= SDL_CPU_ALTIVEC_PREFETCH;
slouken@5389
   159
                } else {
slouken@5389
   160
                    features |= SDL_CPU_ALTIVEC_NOPREFETCH;
slouken@5389
   161
                }
slouken@5389
   162
            }
slouken@1895
   163
        }
slouken@1895
   164
    }
slouken@2247
   165
slouken@2263
   166
    for (i = 0; entries[i].func; ++i) {
slouken@2265
   167
        /* Check for matching pixel formats */
slouken@2262
   168
        if (src_format != entries[i].src_format) {
slouken@2262
   169
            continue;
slouken@2247
   170
        }
slouken@2262
   171
        if (dst_format != entries[i].dst_format) {
slouken@2262
   172
            continue;
slouken@2262
   173
        }
slouken@2265
   174
slouken@2265
   175
        /* Check modulation flags */
slouken@2267
   176
        flagcheck =
slouken@2837
   177
            (flags & (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA));
slouken@2265
   178
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   179
            continue;
slouken@2262
   180
        }
slouken@2265
   181
slouken@2265
   182
        /* Check blend flags */
slouken@5184
   183
        flagcheck =
slouken@5184
   184
            (flags &
slouken@5184
   185
             (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD));
slouken@2265
   186
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2262
   187
            continue;
slouken@2262
   188
        }
slouken@2265
   189
slouken@2265
   190
        /* Check colorkey flag */
slouken@2265
   191
        flagcheck = (flags & SDL_COPY_COLORKEY);
slouken@2265
   192
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   193
            continue;
slouken@2265
   194
        }
slouken@2265
   195
slouken@2265
   196
        /* Check scaling flags */
slouken@2265
   197
        flagcheck = (flags & SDL_COPY_NEAREST);
slouken@2265
   198
        if ((flagcheck & entries[i].flags) != flagcheck) {
slouken@2265
   199
            continue;
slouken@2265
   200
        }
slouken@2265
   201
slouken@2265
   202
        /* Check CPU features */
slouken@2265
   203
        flagcheck = entries[i].cpu;
slouken@2265
   204
        if ((flagcheck & features) != flagcheck) {
slouken@2265
   205
            continue;
slouken@2265
   206
        }
slouken@2265
   207
slouken@2265
   208
        /* We found the best one! */
slouken@2262
   209
        return entries[i].func;
slouken@2247
   210
    }
slouken@2262
   211
    return NULL;
slouken@0
   212
}
slouken@0
   213
slouken@0
   214
/* Figure out which of many blit routines to set up on a surface */
slouken@1895
   215
int
slouken@1895
   216
SDL_CalculateBlit(SDL_Surface * surface)
slouken@0
   217
{
slouken@2262
   218
    SDL_BlitFunc blit = NULL;
slouken@2266
   219
    SDL_BlitMap *map = surface->map;
slouken@2266
   220
    SDL_Surface *dst = map->dst;
slouken@0
   221
slouken@10735
   222
    /* We don't currently support blitting to < 8 bpp surfaces */
slouken@10735
   223
    if (dst->format->BitsPerPixel < 8) {
slouken@10735
   224
        SDL_InvalidateMap(map);
slouken@10735
   225
        return SDL_SetError("Blit combination not supported");
slouken@10735
   226
    }
slouken@10735
   227
slouken@1895
   228
    /* Clean everything out to start */
slouken@1895
   229
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   230
        SDL_UnRLESurface(surface, 1);
slouken@1895
   231
    }
slouken@2266
   232
    map->blit = SDL_SoftBlit;
slouken@2266
   233
    map->info.src_fmt = surface->format;
slouken@2266
   234
    map->info.src_pitch = surface->pitch;
slouken@2266
   235
    map->info.dst_fmt = dst->format;
slouken@2266
   236
    map->info.dst_pitch = dst->pitch;
slouken@0
   237
slouken@2266
   238
    /* See if we can do RLE acceleration */
slouken@2805
   239
    if (map->info.flags & SDL_COPY_RLE_DESIRED) {
slouken@2266
   240
        if (SDL_RLESurface(surface) == 0) {
slouken@2266
   241
            return 0;
slouken@2266
   242
        }
slouken@2266
   243
    }
slouken@2266
   244
slouken@2266
   245
    /* Choose a standard blit function */
slouken@2801
   246
    if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
slouken@5325
   247
        blit = SDL_BlitCopy;
slouken@11226
   248
    } else if (surface->format->Rloss > 8 || dst->format->Rloss > 8) {
slouken@11226
   249
        /* Greater than 8 bits per channel not supported yet */
slouken@11226
   250
        SDL_InvalidateMap(map);
slouken@11226
   251
        return SDL_SetError("Blit combination not supported");
slouken@7208
   252
    } else if (surface->format->BitsPerPixel < 8 &&
slouken@7208
   253
               SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
slouken@2267
   254
        blit = SDL_CalculateBlit0(surface);
slouken@7208
   255
    } else if (surface->format->BytesPerPixel == 1 &&
slouken@7208
   256
               SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
slouken@2267
   257
        blit = SDL_CalculateBlit1(surface);
slouken@2267
   258
    } else if (map->info.flags & SDL_COPY_BLEND) {
slouken@2267
   259
        blit = SDL_CalculateBlitA(surface);
slouken@1895
   260
    } else {
slouken@2267
   261
        blit = SDL_CalculateBlitN(surface);
slouken@1895
   262
    }
slouken@2262
   263
    if (blit == NULL) {
slouken@5288
   264
        Uint32 src_format = surface->format->format;
slouken@5288
   265
        Uint32 dst_format = dst->format->format;
slouken@2267
   266
slouken@2267
   267
        blit =
slouken@2267
   268
            SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
slouken@2267
   269
                               SDL_GeneratedBlitFuncTable);
slouken@2262
   270
    }
slouken@2824
   271
#ifndef TEST_SLOW_BLIT
slouken@2824
   272
    if (blit == NULL)
slouken@2824
   273
#endif
slouken@2824
   274
    {
slouken@7208
   275
        Uint32 src_format = surface->format->format;
slouken@7208
   276
        Uint32 dst_format = dst->format->format;
slouken@7208
   277
slouken@7208
   278
        if (!SDL_ISPIXELFORMAT_INDEXED(src_format) &&
slouken@7208
   279
            !SDL_ISPIXELFORMAT_FOURCC(src_format) &&
slouken@7208
   280
            !SDL_ISPIXELFORMAT_INDEXED(dst_format) &&
slouken@7208
   281
            !SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
slouken@2824
   282
            blit = SDL_Blit_Slow;
slouken@2824
   283
        }
slouken@2824
   284
    }
slouken@11839
   285
    *(SDL_BlitFunc *)&map->data = blit;
slouken@2262
   286
slouken@1895
   287
    /* Make sure we have a blit function */
slouken@2257
   288
    if (blit == NULL) {
slouken@2266
   289
        SDL_InvalidateMap(map);
icculus@7037
   290
        return SDL_SetError("Blit combination not supported");
slouken@1895
   291
    }
slouken@0
   292
icculus@7037
   293
    return 0;
slouken@0
   294
}
slouken@0
   295
slouken@1895
   296
/* vi: set ts=4 sw=4 expandtab: */