src/video/SDL_fillrect.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 24 Dec 2008 12:17:25 +0000
changeset 2913 ffae53de58f4
parent 2888 32e8bbba1e94
child 3012 7e30c2dc7783
permissions -rw-r--r--
Don't unlock a surface we didn't lock
slouken@2888
     1
/*
slouken@2888
     2
    SDL - Simple DirectMedia Layer
slouken@2888
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@2888
     4
slouken@2888
     5
    This library is free software; you can redistribute it and/or
slouken@2888
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2888
     7
    License as published by the Free Software Foundation; either
slouken@2888
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2888
     9
slouken@2888
    10
    This library is distributed in the hope that it will be useful,
slouken@2888
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2888
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2888
    13
    Lesser General Public License for more details.
slouken@2888
    14
slouken@2888
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2888
    16
    License along with this library; if not, write to the Free Software
slouken@2888
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2888
    18
slouken@2888
    19
    Sam Lantinga
slouken@2888
    20
    slouken@libsdl.org
slouken@2888
    21
*/
slouken@2888
    22
#include "SDL_config.h"
slouken@2888
    23
slouken@2888
    24
#include "SDL_video.h"
slouken@2888
    25
#include "SDL_blit.h"
slouken@2888
    26
slouken@2888
    27
slouken@2888
    28
#ifdef __SSE__
slouken@2888
    29
/* *INDENT-OFF* */
slouken@2888
    30
slouken@2888
    31
#ifdef _MSC_VER
slouken@2888
    32
#define SSE_BEGIN \
slouken@2888
    33
    __m128 c128; \
slouken@2888
    34
    c128.m128_u32[0] = color; \
slouken@2888
    35
    c128.m128_u32[1] = color; \
slouken@2888
    36
    c128.m128_u32[2] = color; \
slouken@2888
    37
    c128.m128_u32[3] = color;
slouken@2888
    38
#else
slouken@2888
    39
#define SSE_BEGIN \
slouken@2888
    40
    DECLARE_ALIGNED(Uint32, cccc[4], 16); \
slouken@2888
    41
    cccc[0] = color; \
slouken@2888
    42
    cccc[1] = color; \
slouken@2888
    43
    cccc[2] = color; \
slouken@2888
    44
    cccc[3] = color; \
slouken@2888
    45
    __m128 c128 = *(__m128 *)cccc;
slouken@2888
    46
#endif
slouken@2888
    47
slouken@2888
    48
#define SSE_WORK \
slouken@2888
    49
    for (i = n / 64; i--;) { \
slouken@2888
    50
        _mm_stream_ps((float *)(p+0), c128); \
slouken@2888
    51
        _mm_stream_ps((float *)(p+16), c128); \
slouken@2888
    52
        _mm_stream_ps((float *)(p+32), c128); \
slouken@2888
    53
        _mm_stream_ps((float *)(p+48), c128); \
slouken@2888
    54
        p += 64; \
slouken@2888
    55
    }
slouken@2888
    56
slouken@2888
    57
#define SSE_END
slouken@2888
    58
slouken@2888
    59
#define DEFINE_SSE_FILLRECT(bpp, type) \
slouken@2888
    60
static void \
slouken@2888
    61
SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2888
    62
{ \
slouken@2888
    63
    SSE_BEGIN; \
slouken@2888
    64
 \
slouken@2888
    65
    while (h--) { \
slouken@2888
    66
        int i, n = w * bpp; \
slouken@2888
    67
        Uint8 *p = pixels; \
slouken@2888
    68
 \
slouken@2888
    69
        if (n > 15) { \
slouken@2888
    70
            int adjust = 16 - ((uintptr_t)p & 15); \
slouken@2888
    71
            if (adjust < 16) { \
slouken@2888
    72
                n -= adjust; \
slouken@2888
    73
                adjust /= bpp; \
slouken@2888
    74
                while (adjust--) { \
slouken@2888
    75
                    *((type *)p) = (type)color; \
slouken@2888
    76
                    p += bpp; \
slouken@2888
    77
                } \
slouken@2888
    78
            } \
slouken@2888
    79
            SSE_WORK; \
slouken@2888
    80
        } \
slouken@2888
    81
        if (n & 63) { \
slouken@2888
    82
            int remainder = (n & 63); \
slouken@2888
    83
            remainder /= bpp; \
slouken@2888
    84
            while (remainder--) { \
slouken@2888
    85
                *((type *)p) = (type)color; \
slouken@2888
    86
                p += bpp; \
slouken@2888
    87
            } \
slouken@2888
    88
        } \
slouken@2888
    89
        pixels += pitch; \
slouken@2888
    90
    } \
slouken@2888
    91
 \
slouken@2888
    92
    SSE_END; \
slouken@2888
    93
}
slouken@2888
    94
slouken@2888
    95
DEFINE_SSE_FILLRECT(1, Uint8)
slouken@2888
    96
DEFINE_SSE_FILLRECT(2, Uint16)
slouken@2888
    97
DEFINE_SSE_FILLRECT(4, Uint32)
slouken@2888
    98
slouken@2888
    99
/* *INDENT-ON* */
slouken@2888
   100
#endif /* __SSE__ */
slouken@2888
   101
slouken@2888
   102
#ifdef __MMX__
slouken@2888
   103
/* *INDENT-OFF* */
slouken@2888
   104
slouken@2888
   105
#define MMX_BEGIN \
slouken@2888
   106
    __m64 c64 = _mm_set_pi32(color, color)
slouken@2888
   107
slouken@2888
   108
#define MMX_WORK \
slouken@2888
   109
    for (i = n / 64; i--;) { \
slouken@2888
   110
        _mm_stream_pi((__m64 *)(p+0), c64); \
slouken@2888
   111
        _mm_stream_pi((__m64 *)(p+8), c64); \
slouken@2888
   112
        _mm_stream_pi((__m64 *)(p+16), c64); \
slouken@2888
   113
        _mm_stream_pi((__m64 *)(p+24), c64); \
slouken@2888
   114
        _mm_stream_pi((__m64 *)(p+32), c64); \
slouken@2888
   115
        _mm_stream_pi((__m64 *)(p+40), c64); \
slouken@2888
   116
        _mm_stream_pi((__m64 *)(p+48), c64); \
slouken@2888
   117
        _mm_stream_pi((__m64 *)(p+56), c64); \
slouken@2888
   118
        p += 64; \
slouken@2888
   119
    }
slouken@2888
   120
slouken@2888
   121
#define MMX_END \
slouken@2888
   122
    _mm_empty()
slouken@2888
   123
slouken@2888
   124
#define DEFINE_MMX_FILLRECT(bpp, type) \
slouken@2888
   125
static void \
slouken@2888
   126
SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2888
   127
{ \
slouken@2888
   128
    MMX_BEGIN; \
slouken@2888
   129
 \
slouken@2888
   130
    while (h--) { \
slouken@2888
   131
        int i, n = w * bpp; \
slouken@2888
   132
        Uint8 *p = pixels; \
slouken@2888
   133
 \
slouken@2888
   134
        if (n > 7) { \
slouken@2888
   135
            int adjust = 8 - ((uintptr_t)p & 7); \
slouken@2888
   136
            if (adjust < 8) { \
slouken@2888
   137
                n -= adjust; \
slouken@2888
   138
                adjust /= bpp; \
slouken@2888
   139
                while (adjust--) { \
slouken@2888
   140
                    *((type *)p) = (type)color; \
slouken@2888
   141
                    p += bpp; \
slouken@2888
   142
                } \
slouken@2888
   143
            } \
slouken@2888
   144
            MMX_WORK; \
slouken@2888
   145
        } \
slouken@2888
   146
        if (n & 63) { \
slouken@2888
   147
            int remainder = (n & 63); \
slouken@2888
   148
            remainder /= bpp; \
slouken@2888
   149
            while (remainder--) { \
slouken@2888
   150
                *((type *)p) = (type)color; \
slouken@2888
   151
                p += bpp; \
slouken@2888
   152
            } \
slouken@2888
   153
        } \
slouken@2888
   154
        pixels += pitch; \
slouken@2888
   155
    } \
slouken@2888
   156
 \
slouken@2888
   157
    MMX_END; \
slouken@2888
   158
}
slouken@2888
   159
slouken@2888
   160
DEFINE_MMX_FILLRECT(1, Uint8)
slouken@2888
   161
DEFINE_MMX_FILLRECT(2, Uint16)
slouken@2888
   162
DEFINE_MMX_FILLRECT(4, Uint32)
slouken@2888
   163
slouken@2888
   164
/* *INDENT-ON* */
slouken@2888
   165
#endif /* __MMX__ */
slouken@2888
   166
slouken@2888
   167
static void
slouken@2888
   168
SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   169
{
slouken@2888
   170
    while (h--) {
slouken@2888
   171
        int n = w;
slouken@2888
   172
        Uint8 *p = pixels;
slouken@2888
   173
slouken@2888
   174
        if (n > 3) {
slouken@2888
   175
            switch ((uintptr_t) p & 3) {
slouken@2888
   176
            case 1:
slouken@2888
   177
                *p++ = (Uint8) color;
slouken@2888
   178
                --n;
slouken@2888
   179
            case 2:
slouken@2888
   180
                *p++ = (Uint8) color;
slouken@2888
   181
                --n;
slouken@2888
   182
            case 3:
slouken@2888
   183
                *p++ = (Uint8) color;
slouken@2888
   184
                --n;
slouken@2888
   185
            }
slouken@2888
   186
            SDL_memset4(p, color, (n >> 2));
slouken@2888
   187
        }
slouken@2888
   188
        if (n & 3) {
slouken@2888
   189
            p += (n & ~3);
slouken@2888
   190
            switch (n & 3) {
slouken@2888
   191
            case 3:
slouken@2888
   192
                *p++ = (Uint8) color;
slouken@2888
   193
            case 2:
slouken@2888
   194
                *p++ = (Uint8) color;
slouken@2888
   195
            case 1:
slouken@2888
   196
                *p++ = (Uint8) color;
slouken@2888
   197
            }
slouken@2888
   198
        }
slouken@2888
   199
        pixels += pitch;
slouken@2888
   200
    }
slouken@2888
   201
}
slouken@2888
   202
slouken@2888
   203
static void
slouken@2888
   204
SDL_FillRect2(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   205
{
slouken@2888
   206
    while (h--) {
slouken@2888
   207
        int n = w;
slouken@2888
   208
        Uint16 *p = (Uint16 *) pixels;
slouken@2888
   209
slouken@2888
   210
        if (n > 1) {
slouken@2888
   211
            if ((uintptr_t) p & 2) {
slouken@2888
   212
                *p++ = (Uint16) color;
slouken@2888
   213
                --n;
slouken@2888
   214
            }
slouken@2888
   215
            SDL_memset4(p, color, (n >> 1));
slouken@2888
   216
        }
slouken@2888
   217
        if (n & 1) {
slouken@2888
   218
            p[n - 1] = (Uint16) color;
slouken@2888
   219
        }
slouken@2888
   220
        pixels += pitch;
slouken@2888
   221
    }
slouken@2888
   222
}
slouken@2888
   223
slouken@2888
   224
static void
slouken@2888
   225
SDL_FillRect3(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   226
{
slouken@2888
   227
    Uint8 r = (Uint8) ((color >> 16) & 0xFF);
slouken@2888
   228
    Uint8 g = (Uint8) ((color >> 8) & 0xFF);
slouken@2888
   229
    Uint8 b = (Uint8) (color & 0xFF);
slouken@2888
   230
slouken@2888
   231
    while (h--) {
slouken@2888
   232
        int n = w;
slouken@2888
   233
        Uint8 *p = pixels;
slouken@2888
   234
slouken@2888
   235
        while (n--) {
slouken@2888
   236
            *p++ = r;
slouken@2888
   237
            *p++ = g;
slouken@2888
   238
            *p++ = b;
slouken@2888
   239
        }
slouken@2888
   240
        pixels += pitch;
slouken@2888
   241
    }
slouken@2888
   242
}
slouken@2888
   243
slouken@2888
   244
static void
slouken@2888
   245
SDL_FillRect4(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   246
{
slouken@2888
   247
    while (h--) {
slouken@2888
   248
        SDL_memset4(pixels, color, w);
slouken@2888
   249
        pixels += pitch;
slouken@2888
   250
    }
slouken@2888
   251
}
slouken@2888
   252
slouken@2888
   253
/* 
slouken@2888
   254
 * This function performs a fast fill of the given rectangle with 'color'
slouken@2888
   255
 */
slouken@2888
   256
int
slouken@2888
   257
SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@2888
   258
{
slouken@2888
   259
    Uint8 *pixels;
slouken@2888
   260
slouken@2888
   261
    /* This function doesn't work on surfaces < 8 bpp */
slouken@2888
   262
    if (dst->format->BitsPerPixel < 8) {
slouken@2888
   263
        SDL_SetError("SDL_FillRect(): Unsupported surface format");
slouken@2888
   264
        return (-1);
slouken@2888
   265
    }
slouken@2888
   266
slouken@2888
   267
    /* If 'dstrect' == NULL, then fill the whole surface */
slouken@2888
   268
    if (dstrect) {
slouken@2888
   269
        /* Perform clipping */
slouken@2888
   270
        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@2888
   271
            return (0);
slouken@2888
   272
        }
slouken@2888
   273
    } else {
slouken@2888
   274
        dstrect = &dst->clip_rect;
slouken@2888
   275
    }
slouken@2888
   276
slouken@2888
   277
    /* Perform software fill */
slouken@2888
   278
    if (!dst->pixels) {
slouken@2888
   279
        SDL_SetError("SDL_FillRect(): You must lock the surface");
slouken@2888
   280
        return (-1);
slouken@2888
   281
    }
slouken@2888
   282
slouken@2888
   283
    pixels =
slouken@2888
   284
        (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
slouken@2888
   285
        dstrect->x * dst->format->BytesPerPixel;
slouken@2888
   286
slouken@2888
   287
    switch (dst->format->BytesPerPixel) {
slouken@2888
   288
    case 1:
slouken@2888
   289
        {
slouken@2888
   290
            color |= (color << 8);
slouken@2888
   291
            color |= (color << 16);
slouken@2888
   292
#ifdef __SSE__
slouken@2888
   293
            if (SDL_HasSSE()) {
slouken@2888
   294
                SDL_FillRect1SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   295
                                 dstrect->h);
slouken@2888
   296
                break;
slouken@2888
   297
            }
slouken@2888
   298
#endif
slouken@2888
   299
#ifdef __MMX__
slouken@2888
   300
            if (SDL_HasMMX()) {
slouken@2888
   301
                SDL_FillRect1MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   302
                                 dstrect->h);
slouken@2888
   303
                break;
slouken@2888
   304
            }
slouken@2888
   305
#endif
slouken@2888
   306
            SDL_FillRect1(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2888
   307
            break;
slouken@2888
   308
        }
slouken@2888
   309
slouken@2888
   310
    case 2:
slouken@2888
   311
        {
slouken@2888
   312
            color |= (color << 16);
slouken@2888
   313
#ifdef __SSE__
slouken@2888
   314
            if (SDL_HasSSE()) {
slouken@2888
   315
                SDL_FillRect2SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   316
                                 dstrect->h);
slouken@2888
   317
                break;
slouken@2888
   318
            }
slouken@2888
   319
#endif
slouken@2888
   320
#ifdef __MMX__
slouken@2888
   321
            if (SDL_HasMMX()) {
slouken@2888
   322
                SDL_FillRect2MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   323
                                 dstrect->h);
slouken@2888
   324
                break;
slouken@2888
   325
            }
slouken@2888
   326
#endif
slouken@2888
   327
            SDL_FillRect2(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2888
   328
            break;
slouken@2888
   329
        }
slouken@2888
   330
slouken@2888
   331
    case 3:
slouken@2888
   332
        /* 24-bit RGB is a slow path, at least for now. */
slouken@2888
   333
        {
slouken@2888
   334
            SDL_FillRect3(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2888
   335
            break;
slouken@2888
   336
        }
slouken@2888
   337
slouken@2888
   338
    case 4:
slouken@2888
   339
        {
slouken@2888
   340
#ifdef __SSE__
slouken@2888
   341
            if (SDL_HasSSE()) {
slouken@2888
   342
                SDL_FillRect4SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   343
                                 dstrect->h);
slouken@2888
   344
                break;
slouken@2888
   345
            }
slouken@2888
   346
#endif
slouken@2888
   347
#ifdef __MMX__
slouken@2888
   348
            if (SDL_HasMMX()) {
slouken@2888
   349
                SDL_FillRect4MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2888
   350
                                 dstrect->h);
slouken@2888
   351
                break;
slouken@2888
   352
            }
slouken@2888
   353
#endif
slouken@2888
   354
            SDL_FillRect4(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2888
   355
            break;
slouken@2888
   356
        }
slouken@2888
   357
    }
slouken@2888
   358
slouken@2888
   359
    /* We're done! */
slouken@2888
   360
    return (0);
slouken@2888
   361
}
slouken@2888
   362
slouken@2888
   363
/* vi: set ts=4 sw=4 expandtab: */