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