src/video/SDL_fillrect.c
author Gabriel Jacobo
Wed, 21 Aug 2013 09:47:10 -0300
changeset 7678 286c42d7c5ed
parent 7677 871d43c6968a
child 7679 b1fe132bc6a4
permissions -rw-r--r--
OCD fixes: Adds a space after /* (glory to regular expressions!)
slouken@2888
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@2888
     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@2888
     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@2888
    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@2888
    20
*/
slouken@2888
    21
#include "SDL_config.h"
slouken@2888
    22
slouken@2888
    23
#include "SDL_video.h"
slouken@2888
    24
#include "SDL_blit.h"
slouken@2888
    25
slouken@2888
    26
slouken@2888
    27
#ifdef __SSE__
slouken@2888
    28
/* *INDENT-OFF* */
slouken@2888
    29
slouken@2888
    30
#ifdef _MSC_VER
slouken@2888
    31
#define SSE_BEGIN \
slouken@2888
    32
    __m128 c128; \
slouken@2888
    33
    c128.m128_u32[0] = color; \
slouken@2888
    34
    c128.m128_u32[1] = color; \
slouken@2888
    35
    c128.m128_u32[2] = color; \
slouken@2888
    36
    c128.m128_u32[3] = color;
slouken@2888
    37
#else
slouken@2888
    38
#define SSE_BEGIN \
slouken@2888
    39
    DECLARE_ALIGNED(Uint32, cccc[4], 16); \
slouken@2888
    40
    cccc[0] = color; \
slouken@2888
    41
    cccc[1] = color; \
slouken@2888
    42
    cccc[2] = color; \
slouken@2888
    43
    cccc[3] = color; \
slouken@2888
    44
    __m128 c128 = *(__m128 *)cccc;
slouken@2888
    45
#endif
slouken@2888
    46
slouken@2888
    47
#define SSE_WORK \
slouken@2888
    48
    for (i = n / 64; i--;) { \
slouken@2888
    49
        _mm_stream_ps((float *)(p+0), c128); \
slouken@2888
    50
        _mm_stream_ps((float *)(p+16), c128); \
slouken@2888
    51
        _mm_stream_ps((float *)(p+32), c128); \
slouken@2888
    52
        _mm_stream_ps((float *)(p+48), c128); \
slouken@2888
    53
        p += 64; \
slouken@2888
    54
    }
slouken@2888
    55
slouken@2888
    56
#define SSE_END
slouken@2888
    57
slouken@2888
    58
#define DEFINE_SSE_FILLRECT(bpp, type) \
slouken@2888
    59
static void \
slouken@2888
    60
SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2888
    61
{ \
slouken@2888
    62
    SSE_BEGIN; \
slouken@2888
    63
 \
slouken@2888
    64
    while (h--) { \
slouken@2888
    65
        int i, n = w * bpp; \
slouken@2888
    66
        Uint8 *p = pixels; \
slouken@2888
    67
 \
slouken@3012
    68
        if (n > 63) { \
slouken@2888
    69
            int adjust = 16 - ((uintptr_t)p & 15); \
slouken@2888
    70
            if (adjust < 16) { \
slouken@2888
    71
                n -= adjust; \
slouken@2888
    72
                adjust /= bpp; \
slouken@2888
    73
                while (adjust--) { \
slouken@2888
    74
                    *((type *)p) = (type)color; \
slouken@2888
    75
                    p += bpp; \
slouken@2888
    76
                } \
slouken@2888
    77
            } \
slouken@2888
    78
            SSE_WORK; \
slouken@2888
    79
        } \
slouken@2888
    80
        if (n & 63) { \
slouken@2888
    81
            int remainder = (n & 63); \
slouken@2888
    82
            remainder /= bpp; \
slouken@2888
    83
            while (remainder--) { \
slouken@2888
    84
                *((type *)p) = (type)color; \
slouken@2888
    85
                p += bpp; \
slouken@2888
    86
            } \
slouken@2888
    87
        } \
slouken@2888
    88
        pixels += pitch; \
slouken@2888
    89
    } \
slouken@2888
    90
 \
slouken@2888
    91
    SSE_END; \
slouken@2888
    92
}
slouken@2888
    93
slouken@3012
    94
static void
slouken@3012
    95
SDL_FillRect1SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
slouken@3012
    96
{
slouken@3012
    97
    SSE_BEGIN;
slouken@3012
    98
slouken@3012
    99
    while (h--) {
slouken@3012
   100
        int i, n = w;
slouken@3012
   101
        Uint8 *p = pixels;
slouken@3012
   102
slouken@3012
   103
        if (n > 63) {
slouken@3012
   104
            int adjust = 16 - ((uintptr_t)p & 15);
slouken@3012
   105
            if (adjust) {
slouken@3012
   106
                n -= adjust;
slouken@3012
   107
                SDL_memset(p, color, adjust);
slouken@3012
   108
                p += adjust;
slouken@3012
   109
            }
slouken@3012
   110
            SSE_WORK;
slouken@3012
   111
        }
slouken@3012
   112
        if (n & 63) {
slouken@3012
   113
            int remainder = (n & 63);
slouken@3012
   114
            SDL_memset(p, color, remainder);
slouken@3012
   115
            p += remainder;
slouken@3012
   116
        }
slouken@3012
   117
        pixels += pitch;
slouken@3012
   118
    }
slouken@3012
   119
slouken@3012
   120
    SSE_END;
slouken@3012
   121
}
gabomdq@7678
   122
/* DEFINE_SSE_FILLRECT(1, Uint8) */
slouken@2888
   123
DEFINE_SSE_FILLRECT(2, Uint16)
slouken@2888
   124
DEFINE_SSE_FILLRECT(4, Uint32)
slouken@2888
   125
slouken@2888
   126
/* *INDENT-ON* */
slouken@2888
   127
#endif /* __SSE__ */
slouken@2888
   128
slouken@2888
   129
#ifdef __MMX__
slouken@2888
   130
/* *INDENT-OFF* */
slouken@2888
   131
slouken@2888
   132
#define MMX_BEGIN \
slouken@2888
   133
    __m64 c64 = _mm_set_pi32(color, color)
slouken@2888
   134
slouken@2888
   135
#define MMX_WORK \
slouken@2888
   136
    for (i = n / 64; i--;) { \
slouken@2888
   137
        _mm_stream_pi((__m64 *)(p+0), c64); \
slouken@2888
   138
        _mm_stream_pi((__m64 *)(p+8), c64); \
slouken@2888
   139
        _mm_stream_pi((__m64 *)(p+16), c64); \
slouken@2888
   140
        _mm_stream_pi((__m64 *)(p+24), c64); \
slouken@2888
   141
        _mm_stream_pi((__m64 *)(p+32), c64); \
slouken@2888
   142
        _mm_stream_pi((__m64 *)(p+40), c64); \
slouken@2888
   143
        _mm_stream_pi((__m64 *)(p+48), c64); \
slouken@2888
   144
        _mm_stream_pi((__m64 *)(p+56), c64); \
slouken@2888
   145
        p += 64; \
slouken@2888
   146
    }
slouken@2888
   147
slouken@2888
   148
#define MMX_END \
slouken@2888
   149
    _mm_empty()
slouken@2888
   150
slouken@2888
   151
#define DEFINE_MMX_FILLRECT(bpp, type) \
slouken@2888
   152
static void \
slouken@2888
   153
SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2888
   154
{ \
slouken@2888
   155
    MMX_BEGIN; \
slouken@2888
   156
 \
slouken@2888
   157
    while (h--) { \
slouken@2888
   158
        int i, n = w * bpp; \
slouken@2888
   159
        Uint8 *p = pixels; \
slouken@2888
   160
 \
slouken@3012
   161
        if (n > 63) { \
slouken@2888
   162
            int adjust = 8 - ((uintptr_t)p & 7); \
slouken@2888
   163
            if (adjust < 8) { \
slouken@2888
   164
                n -= adjust; \
slouken@2888
   165
                adjust /= bpp; \
slouken@2888
   166
                while (adjust--) { \
slouken@2888
   167
                    *((type *)p) = (type)color; \
slouken@2888
   168
                    p += bpp; \
slouken@2888
   169
                } \
slouken@2888
   170
            } \
slouken@2888
   171
            MMX_WORK; \
slouken@2888
   172
        } \
slouken@2888
   173
        if (n & 63) { \
slouken@2888
   174
            int remainder = (n & 63); \
slouken@2888
   175
            remainder /= bpp; \
slouken@2888
   176
            while (remainder--) { \
slouken@2888
   177
                *((type *)p) = (type)color; \
slouken@2888
   178
                p += bpp; \
slouken@2888
   179
            } \
slouken@2888
   180
        } \
slouken@2888
   181
        pixels += pitch; \
slouken@2888
   182
    } \
slouken@2888
   183
 \
slouken@2888
   184
    MMX_END; \
slouken@2888
   185
}
slouken@2888
   186
slouken@3012
   187
static void
slouken@3012
   188
SDL_FillRect1MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
slouken@3012
   189
{
slouken@3012
   190
    MMX_BEGIN;
slouken@3012
   191
slouken@3012
   192
    while (h--) {
slouken@3012
   193
        int i, n = w;
slouken@3012
   194
        Uint8 *p = pixels;
slouken@3012
   195
slouken@3012
   196
        if (n > 63) {
slouken@3012
   197
            int adjust = 8 - ((uintptr_t)p & 7);
slouken@3012
   198
            if (adjust) {
slouken@3012
   199
                n -= adjust;
slouken@3012
   200
                SDL_memset(p, color, adjust);
slouken@3012
   201
                p += adjust;
slouken@3012
   202
            }
slouken@3012
   203
            MMX_WORK;
slouken@3012
   204
        }
slouken@3012
   205
        if (n & 63) {
slouken@3012
   206
            int remainder = (n & 63);
slouken@3012
   207
            SDL_memset(p, color, remainder);
slouken@3012
   208
            p += remainder;
slouken@3012
   209
        }
slouken@3012
   210
        pixels += pitch;
slouken@3012
   211
    }
slouken@3012
   212
slouken@3012
   213
    MMX_END;
slouken@3012
   214
}
gabomdq@7678
   215
/* DEFINE_MMX_FILLRECT(1, Uint8) */
slouken@2888
   216
DEFINE_MMX_FILLRECT(2, Uint16)
slouken@2888
   217
DEFINE_MMX_FILLRECT(4, Uint32)
slouken@2888
   218
slouken@2888
   219
/* *INDENT-ON* */
slouken@2888
   220
#endif /* __MMX__ */
slouken@2888
   221
slouken@2888
   222
static void
slouken@2888
   223
SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   224
{
slouken@2888
   225
    while (h--) {
slouken@2888
   226
        int n = w;
slouken@2888
   227
        Uint8 *p = pixels;
slouken@2888
   228
slouken@2888
   229
        if (n > 3) {
slouken@2888
   230
            switch ((uintptr_t) p & 3) {
slouken@2888
   231
            case 1:
slouken@2888
   232
                *p++ = (Uint8) color;
slouken@2888
   233
                --n;
slouken@2888
   234
            case 2:
slouken@2888
   235
                *p++ = (Uint8) color;
slouken@2888
   236
                --n;
slouken@2888
   237
            case 3:
slouken@2888
   238
                *p++ = (Uint8) color;
slouken@2888
   239
                --n;
slouken@2888
   240
            }
slouken@2888
   241
            SDL_memset4(p, color, (n >> 2));
slouken@2888
   242
        }
slouken@2888
   243
        if (n & 3) {
slouken@2888
   244
            p += (n & ~3);
slouken@2888
   245
            switch (n & 3) {
slouken@2888
   246
            case 3:
slouken@2888
   247
                *p++ = (Uint8) color;
slouken@2888
   248
            case 2:
slouken@2888
   249
                *p++ = (Uint8) color;
slouken@2888
   250
            case 1:
slouken@2888
   251
                *p++ = (Uint8) color;
slouken@2888
   252
            }
slouken@2888
   253
        }
slouken@2888
   254
        pixels += pitch;
slouken@2888
   255
    }
slouken@2888
   256
}
slouken@2888
   257
slouken@2888
   258
static void
slouken@2888
   259
SDL_FillRect2(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   260
{
slouken@2888
   261
    while (h--) {
slouken@2888
   262
        int n = w;
slouken@2888
   263
        Uint16 *p = (Uint16 *) pixels;
slouken@2888
   264
slouken@2888
   265
        if (n > 1) {
slouken@2888
   266
            if ((uintptr_t) p & 2) {
slouken@2888
   267
                *p++ = (Uint16) color;
slouken@2888
   268
                --n;
slouken@2888
   269
            }
slouken@2888
   270
            SDL_memset4(p, color, (n >> 1));
slouken@2888
   271
        }
slouken@2888
   272
        if (n & 1) {
slouken@2888
   273
            p[n - 1] = (Uint16) color;
slouken@2888
   274
        }
slouken@2888
   275
        pixels += pitch;
slouken@2888
   276
    }
slouken@2888
   277
}
slouken@2888
   278
slouken@2888
   279
static void
slouken@2888
   280
SDL_FillRect3(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   281
{
slouken@2888
   282
    Uint8 r = (Uint8) ((color >> 16) & 0xFF);
slouken@2888
   283
    Uint8 g = (Uint8) ((color >> 8) & 0xFF);
slouken@2888
   284
    Uint8 b = (Uint8) (color & 0xFF);
slouken@2888
   285
slouken@2888
   286
    while (h--) {
slouken@2888
   287
        int n = w;
slouken@2888
   288
        Uint8 *p = pixels;
slouken@2888
   289
slouken@2888
   290
        while (n--) {
slouken@2888
   291
            *p++ = r;
slouken@2888
   292
            *p++ = g;
slouken@2888
   293
            *p++ = b;
slouken@2888
   294
        }
slouken@2888
   295
        pixels += pitch;
slouken@2888
   296
    }
slouken@2888
   297
}
slouken@2888
   298
slouken@2888
   299
static void
slouken@2888
   300
SDL_FillRect4(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2888
   301
{
slouken@2888
   302
    while (h--) {
slouken@2888
   303
        SDL_memset4(pixels, color, w);
slouken@2888
   304
        pixels += pitch;
slouken@2888
   305
    }
slouken@2888
   306
}
slouken@2888
   307
slouken@2888
   308
/* 
slouken@2888
   309
 * This function performs a fast fill of the given rectangle with 'color'
slouken@2888
   310
 */
slouken@2888
   311
int
slouken@3536
   312
SDL_FillRect(SDL_Surface * dst, const SDL_Rect * rect, Uint32 color)
slouken@2888
   313
{
slouken@3536
   314
    SDL_Rect clipped;
slouken@2888
   315
    Uint8 *pixels;
slouken@2888
   316
slouken@3536
   317
    if (!dst) {
icculus@7037
   318
        return SDL_SetError("Passed NULL destination surface");
slouken@3536
   319
    }
slouken@3536
   320
slouken@2888
   321
    /* This function doesn't work on surfaces < 8 bpp */
slouken@2888
   322
    if (dst->format->BitsPerPixel < 8) {
icculus@7037
   323
        return SDL_SetError("SDL_FillRect(): Unsupported surface format");
slouken@2888
   324
    }
slouken@2888
   325
slouken@3536
   326
    /* If 'rect' == NULL, then fill the whole surface */
slouken@3536
   327
    if (rect) {
slouken@2888
   328
        /* Perform clipping */
slouken@3536
   329
        if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
slouken@3536
   330
            return 0;
slouken@2888
   331
        }
slouken@3536
   332
        rect = &clipped;
slouken@2888
   333
    } else {
slouken@3536
   334
        rect = &dst->clip_rect;
slouken@2888
   335
    }
slouken@2888
   336
slouken@2888
   337
    /* Perform software fill */
slouken@2888
   338
    if (!dst->pixels) {
icculus@7037
   339
        return SDL_SetError("SDL_FillRect(): You must lock the surface");
slouken@2888
   340
    }
slouken@2888
   341
slouken@3536
   342
    pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
slouken@3536
   343
                                     rect->x * dst->format->BytesPerPixel;
slouken@2888
   344
slouken@2888
   345
    switch (dst->format->BytesPerPixel) {
slouken@2888
   346
    case 1:
slouken@2888
   347
        {
slouken@2888
   348
            color |= (color << 8);
slouken@2888
   349
            color |= (color << 16);
slouken@2888
   350
#ifdef __SSE__
slouken@2888
   351
            if (SDL_HasSSE()) {
slouken@3536
   352
                SDL_FillRect1SSE(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   353
                break;
slouken@2888
   354
            }
slouken@2888
   355
#endif
slouken@2888
   356
#ifdef __MMX__
slouken@2888
   357
            if (SDL_HasMMX()) {
slouken@3536
   358
                SDL_FillRect1MMX(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   359
                break;
slouken@2888
   360
            }
slouken@2888
   361
#endif
slouken@3536
   362
            SDL_FillRect1(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   363
            break;
slouken@2888
   364
        }
slouken@2888
   365
slouken@2888
   366
    case 2:
slouken@2888
   367
        {
slouken@2888
   368
            color |= (color << 16);
slouken@2888
   369
#ifdef __SSE__
slouken@2888
   370
            if (SDL_HasSSE()) {
slouken@3536
   371
                SDL_FillRect2SSE(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   372
                break;
slouken@2888
   373
            }
slouken@2888
   374
#endif
slouken@2888
   375
#ifdef __MMX__
slouken@2888
   376
            if (SDL_HasMMX()) {
slouken@3536
   377
                SDL_FillRect2MMX(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   378
                break;
slouken@2888
   379
            }
slouken@2888
   380
#endif
slouken@3536
   381
            SDL_FillRect2(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   382
            break;
slouken@2888
   383
        }
slouken@2888
   384
slouken@2888
   385
    case 3:
slouken@2888
   386
        /* 24-bit RGB is a slow path, at least for now. */
slouken@2888
   387
        {
slouken@3536
   388
            SDL_FillRect3(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   389
            break;
slouken@2888
   390
        }
slouken@2888
   391
slouken@2888
   392
    case 4:
slouken@2888
   393
        {
slouken@2888
   394
#ifdef __SSE__
slouken@2888
   395
            if (SDL_HasSSE()) {
slouken@3536
   396
                SDL_FillRect4SSE(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   397
                break;
slouken@2888
   398
            }
slouken@2888
   399
#endif
slouken@2888
   400
#ifdef __MMX__
slouken@2888
   401
            if (SDL_HasMMX()) {
slouken@3536
   402
                SDL_FillRect4MMX(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   403
                break;
slouken@2888
   404
            }
slouken@2888
   405
#endif
slouken@3536
   406
            SDL_FillRect4(pixels, dst->pitch, color, rect->w, rect->h);
slouken@2888
   407
            break;
slouken@2888
   408
        }
slouken@2888
   409
    }
slouken@2888
   410
slouken@2888
   411
    /* We're done! */
slouken@3536
   412
    return 0;
slouken@3536
   413
}
slouken@3536
   414
slouken@3536
   415
int
slouken@5297
   416
SDL_FillRects(SDL_Surface * dst, const SDL_Rect * rects, int count,
slouken@3536
   417
              Uint32 color)
slouken@3536
   418
{
slouken@3536
   419
    int i;
slouken@3536
   420
    int status = 0;
slouken@3536
   421
slouken@5297
   422
    if (!rects) {
icculus@7037
   423
        return SDL_SetError("SDL_FillRects() passed NULL rects");
slouken@5297
   424
    }
slouken@5297
   425
slouken@3536
   426
    for (i = 0; i < count; ++i) {
slouken@5297
   427
        status += SDL_FillRect(dst, &rects[i], color);
slouken@3536
   428
    }
slouken@3536
   429
    return status;
slouken@2888
   430
}
slouken@2888
   431
slouken@2888
   432
/* vi: set ts=4 sw=4 expandtab: */