src/video/SDL_blit_A.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6863 73be5b6b2a86
child 7502 6ff02ff3cf06
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
#include "SDL_video.h"
slouken@0
    24
#include "SDL_blit.h"
slouken@0
    25
slouken@0
    26
/* Functions to perform alpha blended blitting */
slouken@0
    27
slouken@0
    28
/* N->1 blending with per-surface alpha */
slouken@1895
    29
static void
slouken@1895
    30
BlitNto1SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
    31
{
slouken@2262
    32
    int width = info->dst_w;
slouken@2262
    33
    int height = info->dst_h;
slouken@2262
    34
    Uint8 *src = info->src;
slouken@2267
    35
    int srcskip = info->src_skip;
slouken@2262
    36
    Uint8 *dst = info->dst;
slouken@2267
    37
    int dstskip = info->dst_skip;
slouken@1895
    38
    Uint8 *palmap = info->table;
slouken@2267
    39
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
    40
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
    41
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    42
slouken@2267
    43
    const unsigned A = info->a;
slouken@0
    44
slouken@1895
    45
    while (height--) {
slouken@1895
    46
	    /* *INDENT-OFF* */
slouken@0
    47
	    DUFFS_LOOP4(
slouken@0
    48
	    {
icculus@1162
    49
		Uint32 Pixel;
slouken@0
    50
		unsigned sR;
slouken@0
    51
		unsigned sG;
slouken@0
    52
		unsigned sB;
slouken@0
    53
		unsigned dR;
slouken@0
    54
		unsigned dG;
slouken@0
    55
		unsigned dB;
icculus@1162
    56
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
slouken@0
    57
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
    58
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
    59
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
    60
		ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
    61
		dR &= 0xff;
slouken@0
    62
		dG &= 0xff;
slouken@0
    63
		dB &= 0xff;
slouken@0
    64
		/* Pack RGB into 8bit pixel */
slouken@0
    65
		if ( palmap == NULL ) {
slouken@0
    66
		    *dst =((dR>>5)<<(3+2))|
slouken@0
    67
			  ((dG>>5)<<(2))|
slouken@0
    68
			  ((dB>>6)<<(0));
slouken@0
    69
		} else {
slouken@0
    70
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
    71
				  ((dG>>5)<<(2))  |
slouken@0
    72
				  ((dB>>6)<<(0))];
slouken@0
    73
		}
slouken@0
    74
		dst++;
slouken@0
    75
		src += srcbpp;
slouken@0
    76
	    },
slouken@0
    77
	    width);
slouken@1895
    78
	    /* *INDENT-ON* */
slouken@1895
    79
        src += srcskip;
slouken@1895
    80
        dst += dstskip;
slouken@1895
    81
    }
slouken@0
    82
}
slouken@0
    83
slouken@0
    84
/* N->1 blending with pixel alpha */
slouken@1895
    85
static void
slouken@1895
    86
BlitNto1PixelAlpha(SDL_BlitInfo * info)
slouken@0
    87
{
slouken@2262
    88
    int width = info->dst_w;
slouken@2262
    89
    int height = info->dst_h;
slouken@2262
    90
    Uint8 *src = info->src;
slouken@2267
    91
    int srcskip = info->src_skip;
slouken@2262
    92
    Uint8 *dst = info->dst;
slouken@2267
    93
    int dstskip = info->dst_skip;
slouken@1895
    94
    Uint8 *palmap = info->table;
slouken@2267
    95
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
    96
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
    97
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    98
slouken@1895
    99
    while (height--) {
slouken@1895
   100
	    /* *INDENT-OFF* */
slouken@0
   101
	    DUFFS_LOOP4(
slouken@0
   102
	    {
icculus@1162
   103
		Uint32 Pixel;
slouken@0
   104
		unsigned sR;
slouken@0
   105
		unsigned sG;
slouken@0
   106
		unsigned sB;
slouken@0
   107
		unsigned sA;
slouken@0
   108
		unsigned dR;
slouken@0
   109
		unsigned dG;
slouken@0
   110
		unsigned dB;
icculus@1162
   111
		DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
slouken@0
   112
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
   113
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
   114
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
   115
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   116
		dR &= 0xff;
slouken@0
   117
		dG &= 0xff;
slouken@0
   118
		dB &= 0xff;
slouken@0
   119
		/* Pack RGB into 8bit pixel */
slouken@0
   120
		if ( palmap == NULL ) {
slouken@0
   121
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   122
			  ((dG>>5)<<(2))|
slouken@0
   123
			  ((dB>>6)<<(0));
slouken@0
   124
		} else {
slouken@0
   125
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   126
				  ((dG>>5)<<(2))  |
slouken@0
   127
				  ((dB>>6)<<(0))  ];
slouken@0
   128
		}
slouken@0
   129
		dst++;
slouken@0
   130
		src += srcbpp;
slouken@0
   131
	    },
slouken@0
   132
	    width);
slouken@1895
   133
	    /* *INDENT-ON* */
slouken@1895
   134
        src += srcskip;
slouken@1895
   135
        dst += dstskip;
slouken@1895
   136
    }
slouken@0
   137
}
slouken@0
   138
slouken@0
   139
/* colorkeyed N->1 blending with per-surface alpha */
slouken@1895
   140
static void
slouken@1895
   141
BlitNto1SurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
   142
{
slouken@2262
   143
    int width = info->dst_w;
slouken@2262
   144
    int height = info->dst_h;
slouken@2262
   145
    Uint8 *src = info->src;
slouken@2267
   146
    int srcskip = info->src_skip;
slouken@2262
   147
    Uint8 *dst = info->dst;
slouken@2267
   148
    int dstskip = info->dst_skip;
slouken@1895
   149
    Uint8 *palmap = info->table;
slouken@2267
   150
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
   151
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
   152
    int srcbpp = srcfmt->BytesPerPixel;
slouken@2267
   153
    Uint32 ckey = info->colorkey;
slouken@0
   154
slouken@2267
   155
    const int A = info->a;
slouken@0
   156
slouken@1895
   157
    while (height--) {
slouken@1895
   158
	    /* *INDENT-OFF* */
slouken@0
   159
	    DUFFS_LOOP(
slouken@0
   160
	    {
icculus@1162
   161
		Uint32 Pixel;
slouken@0
   162
		unsigned sR;
slouken@0
   163
		unsigned sG;
slouken@0
   164
		unsigned sB;
slouken@0
   165
		unsigned dR;
slouken@0
   166
		unsigned dG;
slouken@0
   167
		unsigned dB;
icculus@1162
   168
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
   169
		if ( Pixel != ckey ) {
slouken@0
   170
		    dR = dstfmt->palette->colors[*dst].r;
slouken@0
   171
		    dG = dstfmt->palette->colors[*dst].g;
slouken@0
   172
		    dB = dstfmt->palette->colors[*dst].b;
slouken@0
   173
		    ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   174
		    dR &= 0xff;
slouken@0
   175
		    dG &= 0xff;
slouken@0
   176
		    dB &= 0xff;
slouken@0
   177
		    /* Pack RGB into 8bit pixel */
slouken@0
   178
		    if ( palmap == NULL ) {
slouken@0
   179
			*dst =((dR>>5)<<(3+2))|
slouken@0
   180
			      ((dG>>5)<<(2)) |
slouken@0
   181
			      ((dB>>6)<<(0));
slouken@0
   182
		    } else {
slouken@0
   183
			*dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   184
				      ((dG>>5)<<(2))  |
slouken@0
   185
				      ((dB>>6)<<(0))  ];
slouken@0
   186
		    }
slouken@0
   187
		}
slouken@0
   188
		dst++;
slouken@0
   189
		src += srcbpp;
slouken@0
   190
	    },
slouken@0
   191
	    width);
slouken@1895
   192
	    /* *INDENT-ON* */
slouken@1895
   193
        src += srcskip;
slouken@1895
   194
        dst += dstskip;
slouken@1895
   195
    }
slouken@0
   196
}
slouken@0
   197
slouken@2255
   198
#ifdef __MMX__
slouken@1542
   199
slouken@1542
   200
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
   201
static void
slouken@1895
   202
BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo * info)
slouken@1542
   203
{
slouken@2262
   204
    int width = info->dst_w;
slouken@2262
   205
    int height = info->dst_h;
slouken@2262
   206
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   207
    int srcskip = info->src_skip >> 2;
slouken@2262
   208
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   209
    int dstskip = info->dst_skip >> 2;
slouken@2267
   210
    Uint32 dalpha = info->dst_fmt->Amask;
slouken@1542
   211
slouken@1895
   212
    __m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
slouken@1542
   213
slouken@1895
   214
    hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe);       /* alpha128 mask -> hmask */
slouken@1895
   215
    lmask = _mm_set_pi32(0x00010101, 0x00010101);       /* !alpha128 mask -> lmask */
slouken@1895
   216
    dsta = _mm_set_pi32(dalpha, dalpha);        /* dst alpha mask -> dsta */
slouken@1542
   217
slouken@1895
   218
    while (height--) {
slouken@1895
   219
        int n = width;
slouken@1895
   220
        if (n & 1) {
slouken@1895
   221
            Uint32 s = *srcp++;
slouken@1895
   222
            Uint32 d = *dstp;
slouken@1895
   223
            *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1895
   224
                       + (s & d & 0x00010101)) | dalpha;
slouken@1895
   225
            n--;
slouken@1895
   226
        }
slouken@1542
   227
slouken@1895
   228
        for (n >>= 1; n > 0; --n) {
slouken@1895
   229
            dst1 = *(__m64 *) dstp;     /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   230
            dst2 = dst1;        /* 2 x dst -> dst2(ARGBARGB) */
slouken@1542
   231
slouken@1895
   232
            src1 = *(__m64 *) srcp;     /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   233
            src2 = src1;        /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   234
slouken@1895
   235
            dst2 = _mm_and_si64(dst2, hmask);   /* dst & mask -> dst2 */
slouken@1895
   236
            src2 = _mm_and_si64(src2, hmask);   /* src & mask -> src2 */
slouken@1895
   237
            src2 = _mm_add_pi32(src2, dst2);    /* dst2 + src2 -> src2 */
slouken@1895
   238
            src2 = _mm_srli_pi32(src2, 1);      /* src2 >> 1 -> src2 */
slouken@1895
   239
slouken@1895
   240
            dst1 = _mm_and_si64(dst1, src1);    /* src & dst -> dst1 */
slouken@1895
   241
            dst1 = _mm_and_si64(dst1, lmask);   /* dst1 & !mask -> dst1 */
slouken@1895
   242
            dst1 = _mm_add_pi32(dst1, src2);    /* src2 + dst1 -> dst1 */
slouken@1895
   243
            dst1 = _mm_or_si64(dst1, dsta);     /* dsta(full alpha) | dst1 -> dst1 */
slouken@1895
   244
slouken@1895
   245
            *(__m64 *) dstp = dst1;     /* dst1 -> 2 x dst pixels */
slouken@1895
   246
            dstp += 2;
slouken@1895
   247
            srcp += 2;
slouken@1895
   248
        }
slouken@1895
   249
slouken@1895
   250
        srcp += srcskip;
slouken@1895
   251
        dstp += dstskip;
slouken@1895
   252
    }
slouken@1895
   253
    _mm_empty();
slouken@1542
   254
}
slouken@1542
   255
slouken@1542
   256
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
   257
static void
slouken@1895
   258
BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   259
{
slouken@2267
   260
    SDL_PixelFormat *df = info->dst_fmt;
slouken@6863
   261
    Uint32 chanmask;
slouken@2267
   262
    unsigned alpha = info->a;
slouken@1542
   263
slouken@1895
   264
    if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1895
   265
        /* only call a128 version when R,G,B occupy lower bits */
slouken@1895
   266
        BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@1895
   267
    } else {
slouken@2262
   268
        int width = info->dst_w;
slouken@2262
   269
        int height = info->dst_h;
slouken@2262
   270
        Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   271
        int srcskip = info->src_skip >> 2;
slouken@2262
   272
        Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   273
        int dstskip = info->dst_skip >> 2;
slouken@1895
   274
        Uint32 dalpha = df->Amask;
slouken@1895
   275
        Uint32 amult;
slouken@1542
   276
slouken@1895
   277
        __m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
slouken@1542
   278
slouken@1895
   279
        mm_zero = _mm_setzero_si64();   /* 0 -> mm_zero */
slouken@1895
   280
        /* form the alpha mult */
slouken@1895
   281
        amult = alpha | (alpha << 8);
slouken@1895
   282
        amult = amult | (amult << 16);
slouken@1895
   283
        chanmask =
slouken@3013
   284
            (0xff << df->Rshift) | (0xff << df->
slouken@3013
   285
                                    Gshift) | (0xff << df->Bshift);
slouken@1895
   286
        mm_alpha = _mm_set_pi32(0, amult & chanmask);   /* 0000AAAA -> mm_alpha, minus 1 chan */
slouken@1895
   287
        mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
slouken@1895
   288
        /* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1895
   289
        dsta = _mm_set_pi32(dalpha, dalpha);    /* dst alpha mask -> dsta */
slouken@1542
   290
slouken@1895
   291
        while (height--) {
slouken@1895
   292
            int n = width;
slouken@1895
   293
            if (n & 1) {
slouken@1895
   294
                /* One Pixel Blend */
slouken@1895
   295
                src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB) */
slouken@1895
   296
                src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
slouken@1542
   297
slouken@1895
   298
                dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
slouken@1895
   299
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   300
slouken@1895
   301
                src2 = _mm_sub_pi16(src2, dst1);        /* src2 - dst2 -> src2 */
slouken@1895
   302
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   303
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   304
                dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
slouken@1542
   305
slouken@1895
   306
                dst1 = _mm_packs_pu16(dst1, mm_zero);   /* 0000ARGB -> dst1 */
slouken@1895
   307
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1895
   308
                *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   309
slouken@1895
   310
                ++srcp;
slouken@1895
   311
                ++dstp;
slouken@1542
   312
slouken@1895
   313
                n--;
slouken@1895
   314
            }
slouken@1542
   315
slouken@1895
   316
            for (n >>= 1; n > 0; --n) {
slouken@1895
   317
                /* Two Pixels Blend */
slouken@1895
   318
                src1 = *(__m64 *) srcp; /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   319
                src2 = src1;    /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   320
                src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
slouken@1895
   321
                src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
slouken@1542
   322
slouken@1895
   323
                dst1 = *(__m64 *) dstp; /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   324
                dst2 = dst1;    /* 2 x dst -> dst2(ARGBARGB) */
slouken@1895
   325
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
slouken@1895
   326
                dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
slouken@1895
   327
slouken@1895
   328
                src1 = _mm_sub_pi16(src1, dst1);        /* src1 - dst1 -> src1 */
slouken@1895
   329
                src1 = _mm_mullo_pi16(src1, mm_alpha);  /* src1 * alpha -> src1 */
slouken@1895
   330
                src1 = _mm_srli_pi16(src1, 8);  /* src1 >> 8 -> src1 */
slouken@1895
   331
                dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
slouken@1895
   332
slouken@1895
   333
                src2 = _mm_sub_pi16(src2, dst2);        /* src2 - dst2 -> src2 */
slouken@1895
   334
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   335
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   336
                dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
slouken@1895
   337
slouken@1895
   338
                dst1 = _mm_packs_pu16(dst1, dst2);      /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
slouken@1895
   339
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1895
   340
slouken@1895
   341
                *(__m64 *) dstp = dst1; /* dst1 -> 2 x pixel */
slouken@1895
   342
slouken@1895
   343
                srcp += 2;
slouken@1895
   344
                dstp += 2;
slouken@1895
   345
            }
slouken@1895
   346
            srcp += srcskip;
slouken@1895
   347
            dstp += dstskip;
slouken@1895
   348
        }
slouken@1895
   349
        _mm_empty();
slouken@1895
   350
    }
slouken@1542
   351
}
slouken@1542
   352
slouken@1542
   353
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   354
static void
slouken@1895
   355
BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   356
{
slouken@2262
   357
    int width = info->dst_w;
slouken@2262
   358
    int height = info->dst_h;
slouken@2262
   359
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   360
    int srcskip = info->src_skip >> 2;
slouken@2262
   361
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   362
    int dstskip = info->dst_skip >> 2;
slouken@2267
   363
    SDL_PixelFormat *sf = info->src_fmt;
slouken@1895
   364
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
   365
    Uint32 amask = sf->Amask;
slouken@1895
   366
    Uint32 ashift = sf->Ashift;
slouken@1895
   367
    Uint64 multmask;
slouken@1542
   368
slouken@1895
   369
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
   370
slouken@1895
   371
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@2260
   372
    multmask = 0xFFFF;
slouken@2255
   373
    multmask <<= (ashift * 2);
slouken@2255
   374
    multmask = ~multmask;
slouken@1895
   375
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1542
   376
slouken@1895
   377
    while (height--) {
slouken@1895
   378
		/* *INDENT-OFF* */
slouken@1542
   379
		DUFFS_LOOP4({
slouken@1542
   380
		Uint32 alpha = *srcp & amask;
slouken@1542
   381
		if (alpha == 0) {
slouken@1542
   382
			/* do nothing */
slouken@1542
   383
		} else if (alpha == amask) {
slouken@1542
   384
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   385
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
   386
		} else {
slouken@1542
   387
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
   388
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
   389
slouken@1542
   390
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
   391
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   392
slouken@1542
   393
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
   394
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
   395
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
   396
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
   397
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
   398
slouken@1542
   399
			/* blend */		    
slouken@1542
   400
			src1 = _mm_sub_pi16(src1, dst1);/* src1 - dst1 -> src1 */
slouken@1542
   401
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src1 - dst1) * alpha -> src1 */
slouken@1542
   402
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
   403
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1 -> dst1(0A0R0G0B) */
slouken@1542
   404
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
   405
			
slouken@1542
   406
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   407
		}
slouken@1542
   408
		++srcp;
slouken@1542
   409
		++dstp;
slouken@1542
   410
	    }, width);
slouken@1895
   411
		/* *INDENT-ON* */
slouken@1895
   412
        srcp += srcskip;
slouken@1895
   413
        dstp += dstskip;
slouken@1895
   414
    }
slouken@1895
   415
    _mm_empty();
slouken@1542
   416
}
slouken@1895
   417
slouken@2255
   418
#endif /* __MMX__ */
slouken@689
   419
slouken@5389
   420
#if SDL_ALTIVEC_BLITTERS
slouken@5389
   421
#if __MWERKS__
slouken@5389
   422
#pragma altivec_model on
slouken@5389
   423
#endif
slouken@5389
   424
#if HAVE_ALTIVEC_H
slouken@5389
   425
#include <altivec.h>
slouken@5389
   426
#endif
slouken@5389
   427
slouken@5389
   428
#if (defined(__MACOSX__) && (__GNUC__ < 4))
slouken@5389
   429
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
slouken@5389
   430
        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
slouken@5389
   431
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
slouken@5389
   432
        (vector unsigned short) ( a,b,c,d,e,f,g,h )
slouken@5389
   433
#else
slouken@5389
   434
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
slouken@5389
   435
        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
slouken@5389
   436
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
slouken@5389
   437
        (vector unsigned short) { a,b,c,d,e,f,g,h }
slouken@5389
   438
#endif
slouken@5389
   439
slouken@5389
   440
#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
slouken@5389
   441
#define VECPRINT(msg, v) do { \
slouken@5389
   442
    vector unsigned int tmpvec = (vector unsigned int)(v); \
slouken@5389
   443
    unsigned int *vp = (unsigned int *)&tmpvec; \
slouken@5389
   444
    printf("%s = %08X %08X %08X %08X\n", msg, vp[0], vp[1], vp[2], vp[3]); \
slouken@5389
   445
} while (0)
slouken@5389
   446
slouken@5389
   447
/* the permuation vector that takes the high bytes out of all the appropriate shorts 
slouken@5389
   448
    (vector unsigned char)(
slouken@5389
   449
        0x00, 0x10, 0x02, 0x12,
slouken@5389
   450
        0x04, 0x14, 0x06, 0x16,
slouken@5389
   451
        0x08, 0x18, 0x0A, 0x1A,
slouken@5389
   452
        0x0C, 0x1C, 0x0E, 0x1E );
slouken@5389
   453
*/
slouken@5389
   454
#define VEC_MERGE_PERMUTE() (vec_add(vec_lvsl(0, (int*)NULL), (vector unsigned char)vec_splat_u16(0x0F)))
slouken@5389
   455
#define VEC_U32_24() (vec_add(vec_splat_u32(12), vec_splat_u32(12)))
slouken@5389
   456
#define VEC_ALPHA_MASK() ((vector unsigned char)vec_sl((vector unsigned int)vec_splat_s8(-1), VEC_U32_24()))
slouken@5389
   457
#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
slouken@5389
   458
    ? vec_lvsl(0, src) \
slouken@5389
   459
    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
slouken@5389
   460
slouken@5389
   461
slouken@5389
   462
#define VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1_16, v8_16) do { \
slouken@5389
   463
    /* vtemp1 contains source AAGGAAGGAAGGAAGG */ \
slouken@5389
   464
    vector unsigned short vtemp1 = vec_mule(vs, valpha); \
slouken@5389
   465
    /* vtemp2 contains source RRBBRRBBRRBBRRBB */ \
slouken@5389
   466
    vector unsigned short vtemp2 = vec_mulo(vs, valpha); \
slouken@5389
   467
    /* valpha2 is 255-alpha */ \
slouken@5389
   468
    vector unsigned char valpha2 = vec_nor(valpha, valpha); \
slouken@5389
   469
    /* vtemp3 contains dest AAGGAAGGAAGGAAGG */ \
slouken@5389
   470
    vector unsigned short vtemp3 = vec_mule(vd, valpha2); \
slouken@5389
   471
    /* vtemp4 contains dest RRBBRRBBRRBBRRBB */ \
slouken@5389
   472
    vector unsigned short vtemp4 = vec_mulo(vd, valpha2); \
slouken@5389
   473
    /* add source and dest */ \
slouken@5389
   474
    vtemp1 = vec_add(vtemp1, vtemp3); \
slouken@5389
   475
    vtemp2 = vec_add(vtemp2, vtemp4); \
slouken@5389
   476
    /* vtemp1 = (vtemp1 + 1) + ((vtemp1 + 1) >> 8) */ \
slouken@5389
   477
    vtemp1 = vec_add(vtemp1, v1_16); \
slouken@5389
   478
    vtemp3 = vec_sr(vtemp1, v8_16); \
slouken@5389
   479
    vtemp1 = vec_add(vtemp1, vtemp3); \
slouken@5389
   480
    /* vtemp2 = (vtemp2 + 1) + ((vtemp2 + 1) >> 8) */ \
slouken@5389
   481
    vtemp2 = vec_add(vtemp2, v1_16); \
slouken@5389
   482
    vtemp4 = vec_sr(vtemp2, v8_16); \
slouken@5389
   483
    vtemp2 = vec_add(vtemp2, vtemp4); \
slouken@5389
   484
    /* (>>8) and get ARGBARGBARGBARGB */ \
slouken@5389
   485
    vd = (vector unsigned char)vec_perm(vtemp1, vtemp2, mergePermute); \
slouken@5389
   486
} while (0)
slouken@5389
   487
slouken@5389
   488
/* Calculate the permute vector used for 32->32 swizzling */
slouken@5389
   489
static vector unsigned char
slouken@5389
   490
calc_swizzle32(const SDL_PixelFormat * srcfmt, const SDL_PixelFormat * dstfmt)
slouken@5389
   491
{
slouken@5389
   492
    /*
slouken@5389
   493
     * We have to assume that the bits that aren't used by other
slouken@5389
   494
     *  colors is alpha, and it's one complete byte, since some formats
slouken@5389
   495
     *  leave alpha with a zero mask, but we should still swizzle the bits.
slouken@5389
   496
     */
slouken@5389
   497
    /* ARGB */
slouken@5389
   498
    const static struct SDL_PixelFormat default_pixel_format = {
slouken@5407
   499
        0, NULL, 0, 0,
slouken@6165
   500
        {0, 0},
slouken@6165
   501
        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
slouken@5389
   502
        0, 0, 0, 0,
slouken@5389
   503
        16, 8, 0, 24,
slouken@5407
   504
        0, NULL
slouken@5389
   505
    };
slouken@5389
   506
    if (!srcfmt) {
slouken@5389
   507
        srcfmt = &default_pixel_format;
slouken@5389
   508
    }
slouken@5389
   509
    if (!dstfmt) {
slouken@5389
   510
        dstfmt = &default_pixel_format;
slouken@5389
   511
    }
slouken@5389
   512
    const vector unsigned char plus = VECUINT8_LITERAL(0x00, 0x00, 0x00, 0x00,
slouken@5389
   513
                                                       0x04, 0x04, 0x04, 0x04,
slouken@5389
   514
                                                       0x08, 0x08, 0x08, 0x08,
slouken@5389
   515
                                                       0x0C, 0x0C, 0x0C,
slouken@5389
   516
                                                       0x0C);
slouken@5389
   517
    vector unsigned char vswiz;
slouken@5389
   518
    vector unsigned int srcvec;
slouken@5389
   519
#define RESHIFT(X) (3 - ((X) >> 3))
slouken@5389
   520
    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
slouken@5389
   521
    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
slouken@5389
   522
    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
slouken@5389
   523
    Uint32 amask;
slouken@5389
   524
    /* Use zero for alpha if either surface doesn't have alpha */
slouken@5389
   525
    if (dstfmt->Amask) {
slouken@5389
   526
        amask =
slouken@5389
   527
            ((srcfmt->Amask) ? RESHIFT(srcfmt->
slouken@5389
   528
                                       Ashift) : 0x10) << (dstfmt->Ashift);
slouken@5389
   529
    } else {
slouken@5389
   530
        amask =
slouken@5389
   531
            0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^
slouken@5389
   532
                          0xFFFFFFFF);
slouken@5389
   533
    }
slouken@5389
   534
#undef RESHIFT
slouken@5389
   535
    ((unsigned int *) (char *) &srcvec)[0] = (rmask | gmask | bmask | amask);
slouken@5389
   536
    vswiz = vec_add(plus, (vector unsigned char) vec_splat(srcvec, 0));
slouken@5389
   537
    return (vswiz);
slouken@5389
   538
}
slouken@5389
   539
slouken@5389
   540
static void
slouken@5389
   541
Blit32to565PixelAlphaAltivec(SDL_BlitInfo * info)
slouken@5389
   542
{
slouken@5389
   543
    int height = info->dst_h;
slouken@5389
   544
    Uint8 *src = (Uint8 *) info->src;
slouken@5389
   545
    int srcskip = info->src_skip;
slouken@5389
   546
    Uint8 *dst = (Uint8 *) info->dst;
slouken@5389
   547
    int dstskip = info->dst_skip;
slouken@5389
   548
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@5389
   549
slouken@5389
   550
    vector unsigned char v0 = vec_splat_u8(0);
slouken@5389
   551
    vector unsigned short v8_16 = vec_splat_u16(8);
slouken@5389
   552
    vector unsigned short v1_16 = vec_splat_u16(1);
slouken@5389
   553
    vector unsigned short v2_16 = vec_splat_u16(2);
slouken@5389
   554
    vector unsigned short v3_16 = vec_splat_u16(3);
slouken@5389
   555
    vector unsigned int v8_32 = vec_splat_u32(8);
slouken@5389
   556
    vector unsigned int v16_32 = vec_add(v8_32, v8_32);
slouken@5389
   557
    vector unsigned short v3f =
slouken@5389
   558
        VECUINT16_LITERAL(0x003f, 0x003f, 0x003f, 0x003f,
slouken@5389
   559
                          0x003f, 0x003f, 0x003f, 0x003f);
slouken@5389
   560
    vector unsigned short vfc =
slouken@5389
   561
        VECUINT16_LITERAL(0x00fc, 0x00fc, 0x00fc, 0x00fc,
slouken@5389
   562
                          0x00fc, 0x00fc, 0x00fc, 0x00fc);
slouken@5389
   563
slouken@5389
   564
    /* 
slouken@5389
   565
       0x10 - 0x1f is the alpha
slouken@5389
   566
       0x00 - 0x0e evens are the red
slouken@5389
   567
       0x01 - 0x0f odds are zero
slouken@5389
   568
     */
slouken@5389
   569
    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
slouken@5389
   570
                                                       0x10, 0x02, 0x01, 0x01,
slouken@5389
   571
                                                       0x10, 0x04, 0x01, 0x01,
slouken@5389
   572
                                                       0x10, 0x06, 0x01,
slouken@5389
   573
                                                       0x01);
slouken@5389
   574
    vector unsigned char vredalpha2 =
slouken@5389
   575
        (vector unsigned char) (vec_add((vector unsigned int) vredalpha1,
slouken@5389
   576
                                        vec_sl(v8_32, v16_32))
slouken@5389
   577
        );
slouken@5389
   578
    /*
slouken@5389
   579
       0x00 - 0x0f is ARxx ARxx ARxx ARxx
slouken@5389
   580
       0x11 - 0x0f odds are blue
slouken@5389
   581
     */
slouken@5389
   582
    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
slouken@5389
   583
                                                   0x04, 0x05, 0x06, 0x13,
slouken@5389
   584
                                                   0x08, 0x09, 0x0a, 0x15,
slouken@5389
   585
                                                   0x0c, 0x0d, 0x0e, 0x17);
slouken@5389
   586
    vector unsigned char vblue2 =
slouken@5389
   587
        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8_32)
slouken@5389
   588
        );
slouken@5389
   589
    /*
slouken@5389
   590
       0x00 - 0x0f is ARxB ARxB ARxB ARxB
slouken@5389
   591
       0x10 - 0x0e evens are green
slouken@5389
   592
     */
slouken@5389
   593
    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
slouken@5389
   594
                                                    0x04, 0x05, 0x12, 0x07,
slouken@5389
   595
                                                    0x08, 0x09, 0x14, 0x0b,
slouken@5389
   596
                                                    0x0c, 0x0d, 0x16, 0x0f);
slouken@5389
   597
    vector unsigned char vgreen2 =
slouken@5389
   598
        (vector unsigned
slouken@5389
   599
         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8_32, v8_32))
slouken@5389
   600
        );
slouken@5389
   601
    vector unsigned char vgmerge = VECUINT8_LITERAL(0x00, 0x02, 0x00, 0x06,
slouken@5389
   602
                                                    0x00, 0x0a, 0x00, 0x0e,
slouken@5389
   603
                                                    0x00, 0x12, 0x00, 0x16,
slouken@5389
   604
                                                    0x00, 0x1a, 0x00, 0x1e);
slouken@5389
   605
    vector unsigned char mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
   606
    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
slouken@5389
   607
    vector unsigned char valphaPermute =
slouken@5389
   608
        vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@5389
   609
slouken@5389
   610
    vector unsigned short vf800 = (vector unsigned short) vec_splat_u8(-7);
slouken@5389
   611
    vf800 = vec_sl(vf800, vec_splat_u16(8));
slouken@5389
   612
slouken@5389
   613
    while (height--) {
slouken@5389
   614
        int extrawidth;
slouken@5389
   615
        vector unsigned char valigner;
slouken@5389
   616
        vector unsigned char vsrc;
slouken@5389
   617
        vector unsigned char voverflow;
slouken@5389
   618
        int width = info->dst_w;
slouken@5389
   619
slouken@5389
   620
#define ONE_PIXEL_BLEND(condition, widthvar) \
slouken@5389
   621
        while (condition) { \
slouken@5389
   622
            Uint32 Pixel; \
slouken@5389
   623
            unsigned sR, sG, sB, dR, dG, dB, sA; \
slouken@5389
   624
            DISEMBLE_RGBA(src, 4, srcfmt, Pixel, sR, sG, sB, sA); \
slouken@5389
   625
            if(sA) { \
slouken@5389
   626
                unsigned short dstpixel = *((unsigned short *)dst); \
slouken@5389
   627
                dR = (dstpixel >> 8) & 0xf8; \
slouken@5389
   628
                dG = (dstpixel >> 3) & 0xfc; \
slouken@5389
   629
                dB = (dstpixel << 3) & 0xf8; \
slouken@5389
   630
                ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
slouken@5389
   631
                *((unsigned short *)dst) = ( \
slouken@5389
   632
                    ((dR & 0xf8) << 8) | ((dG & 0xfc) << 3) | (dB >> 3) \
slouken@5389
   633
                ); \
slouken@5389
   634
            } \
slouken@5389
   635
            src += 4; \
slouken@5389
   636
            dst += 2; \
slouken@5389
   637
            widthvar--; \
slouken@5389
   638
        }
slouken@5389
   639
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dst)) && (width), width);
slouken@5389
   640
        extrawidth = (width % 8);
slouken@5389
   641
        valigner = VEC_ALIGNER(src);
slouken@5389
   642
        vsrc = (vector unsigned char) vec_ld(0, src);
slouken@5389
   643
        width -= extrawidth;
slouken@5389
   644
        while (width) {
slouken@5389
   645
            vector unsigned char valpha;
slouken@5389
   646
            vector unsigned char vsrc1, vsrc2;
slouken@5389
   647
            vector unsigned char vdst1, vdst2;
slouken@5389
   648
            vector unsigned short vR, vG, vB;
slouken@5389
   649
            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
slouken@5389
   650
slouken@5389
   651
            /* Load 8 pixels from src as ARGB */
slouken@5389
   652
            voverflow = (vector unsigned char) vec_ld(15, src);
slouken@5389
   653
            vsrc = vec_perm(vsrc, voverflow, valigner);
slouken@5389
   654
            vsrc1 = vec_perm(vsrc, vsrc, vpermute);
slouken@5389
   655
            src += 16;
slouken@5389
   656
            vsrc = (vector unsigned char) vec_ld(15, src);
slouken@5389
   657
            voverflow = vec_perm(voverflow, vsrc, valigner);
slouken@5389
   658
            vsrc2 = vec_perm(voverflow, voverflow, vpermute);
slouken@5389
   659
            src += 16;
slouken@5389
   660
slouken@5389
   661
            /* Load 8 pixels from dst as XRGB */
slouken@5389
   662
            voverflow = vec_ld(0, dst);
slouken@5389
   663
            vR = vec_and((vector unsigned short) voverflow, vf800);
slouken@5389
   664
            vB = vec_sl((vector unsigned short) voverflow, v3_16);
slouken@5389
   665
            vG = vec_sl(vB, v2_16);
slouken@5389
   666
            vdst1 =
slouken@5389
   667
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@5389
   668
                                                (vector unsigned char) vR,
slouken@5389
   669
                                                vredalpha1);
slouken@5389
   670
            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
slouken@5389
   671
            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
slouken@5389
   672
            vdst2 =
slouken@5389
   673
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@5389
   674
                                                (vector unsigned char) vR,
slouken@5389
   675
                                                vredalpha2);
slouken@5389
   676
            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
slouken@5389
   677
            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
slouken@5389
   678
slouken@5389
   679
            /* Alpha blend 8 pixels as ARGB */
slouken@5389
   680
            valpha = vec_perm(vsrc1, v0, valphaPermute);
slouken@5389
   681
            VEC_MULTIPLY_ALPHA(vsrc1, vdst1, valpha, mergePermute, v1_16,
slouken@5389
   682
                               v8_16);
slouken@5389
   683
            valpha = vec_perm(vsrc2, v0, valphaPermute);
slouken@5389
   684
            VEC_MULTIPLY_ALPHA(vsrc2, vdst2, valpha, mergePermute, v1_16,
slouken@5389
   685
                               v8_16);
slouken@5389
   686
slouken@5389
   687
            /* Convert 8 pixels to 565 */
slouken@5389
   688
            vpixel = (vector unsigned short) vec_packpx((vector unsigned int)
slouken@5389
   689
                                                        vdst1,
slouken@5389
   690
                                                        (vector unsigned int)
slouken@5389
   691
                                                        vdst2);
slouken@5389
   692
            vgpixel = (vector unsigned short) vec_perm(vdst1, vdst2, vgmerge);
slouken@5389
   693
            vgpixel = vec_and(vgpixel, vfc);
slouken@5389
   694
            vgpixel = vec_sl(vgpixel, v3_16);
slouken@5389
   695
            vrpixel = vec_sl(vpixel, v1_16);
slouken@5389
   696
            vrpixel = vec_and(vrpixel, vf800);
slouken@5389
   697
            vbpixel = vec_and(vpixel, v3f);
slouken@5389
   698
            vdst1 =
slouken@5389
   699
                vec_or((vector unsigned char) vrpixel,
slouken@5389
   700
                       (vector unsigned char) vgpixel);
slouken@5389
   701
            vdst1 = vec_or(vdst1, (vector unsigned char) vbpixel);
slouken@5389
   702
slouken@5389
   703
            /* Store 8 pixels */
slouken@5389
   704
            vec_st(vdst1, 0, dst);
slouken@5389
   705
slouken@5389
   706
            width -= 8;
slouken@5389
   707
            dst += 16;
slouken@5389
   708
        }
slouken@5389
   709
        ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
   710
#undef ONE_PIXEL_BLEND
slouken@5389
   711
        src += srcskip;
slouken@5389
   712
        dst += dstskip;
slouken@5389
   713
    }
slouken@5389
   714
}
slouken@5389
   715
slouken@5389
   716
static void
slouken@5389
   717
Blit32to32SurfaceAlphaKeyAltivec(SDL_BlitInfo * info)
slouken@5389
   718
{
slouken@5389
   719
    int height = info->dst_h;
slouken@5389
   720
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
   721
    int srcskip = info->src_skip >> 2;
slouken@5389
   722
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
   723
    int dstskip = info->dst_skip >> 2;
slouken@5389
   724
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@5389
   725
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@5389
   726
    unsigned sA = info->a;
slouken@5389
   727
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@5389
   728
    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
slouken@5389
   729
    Uint32 ckey = info->colorkey;
slouken@5389
   730
    vector unsigned char mergePermute;
slouken@5389
   731
    vector unsigned char vsrcPermute;
slouken@5389
   732
    vector unsigned char vdstPermute;
slouken@5389
   733
    vector unsigned char vsdstPermute;
slouken@5389
   734
    vector unsigned char valpha;
slouken@5389
   735
    vector unsigned char valphamask;
slouken@5389
   736
    vector unsigned char vbits;
slouken@5389
   737
    vector unsigned char v0;
slouken@5389
   738
    vector unsigned short v1;
slouken@5389
   739
    vector unsigned short v8;
slouken@5389
   740
    vector unsigned int vckey;
slouken@5389
   741
    vector unsigned int vrgbmask;
slouken@5389
   742
slouken@5389
   743
    mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
   744
    v0 = vec_splat_u8(0);
slouken@5389
   745
    v1 = vec_splat_u16(1);
slouken@5389
   746
    v8 = vec_splat_u16(8);
slouken@5389
   747
slouken@5389
   748
    /* set the alpha to 255 on the destination surf */
slouken@5389
   749
    valphamask = VEC_ALPHA_MASK();
slouken@5389
   750
slouken@5389
   751
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
slouken@5389
   752
    vdstPermute = calc_swizzle32(NULL, dstfmt);
slouken@5389
   753
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
slouken@5389
   754
slouken@5389
   755
    /* set a vector full of alpha and 255-alpha */
slouken@5389
   756
    ((unsigned char *) &valpha)[0] = sA;
slouken@5389
   757
    valpha = vec_splat(valpha, 0);
slouken@5389
   758
    vbits = (vector unsigned char) vec_splat_s8(-1);
slouken@5389
   759
slouken@5389
   760
    ckey &= rgbmask;
slouken@5389
   761
    ((unsigned int *) (char *) &vckey)[0] = ckey;
slouken@5389
   762
    vckey = vec_splat(vckey, 0);
slouken@5389
   763
    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
slouken@5389
   764
    vrgbmask = vec_splat(vrgbmask, 0);
slouken@5389
   765
slouken@5389
   766
    while (height--) {
slouken@5389
   767
        int width = info->dst_w;
slouken@5389
   768
#define ONE_PIXEL_BLEND(condition, widthvar) \
slouken@5389
   769
        while (condition) { \
slouken@5389
   770
            Uint32 Pixel; \
slouken@5389
   771
            unsigned sR, sG, sB, dR, dG, dB; \
slouken@5389
   772
            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
slouken@5389
   773
            if(sA && Pixel != ckey) { \
slouken@5389
   774
                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
slouken@5389
   775
                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
slouken@5389
   776
                ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
slouken@5389
   777
                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
slouken@5389
   778
            } \
slouken@5389
   779
            dstp++; \
slouken@5389
   780
            srcp++; \
slouken@5389
   781
            widthvar--; \
slouken@5389
   782
        }
slouken@5389
   783
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
slouken@5389
   784
        if (width > 0) {
slouken@5389
   785
            int extrawidth = (width % 4);
slouken@5389
   786
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@5389
   787
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
slouken@5389
   788
            width -= extrawidth;
slouken@5389
   789
            while (width) {
slouken@5389
   790
                vector unsigned char vsel;
slouken@5389
   791
                vector unsigned char voverflow;
slouken@5389
   792
                vector unsigned char vd;
slouken@5389
   793
                vector unsigned char vd_orig;
slouken@5389
   794
slouken@5389
   795
                /* s = *srcp */
slouken@5389
   796
                voverflow = (vector unsigned char) vec_ld(15, srcp);
slouken@5389
   797
                vs = vec_perm(vs, voverflow, valigner);
slouken@5389
   798
slouken@5389
   799
                /* vsel is set for items that match the key */
slouken@5389
   800
                vsel =
slouken@5389
   801
                    (vector unsigned char) vec_and((vector unsigned int) vs,
slouken@5389
   802
                                                   vrgbmask);
slouken@5389
   803
                vsel = (vector unsigned char) vec_cmpeq((vector unsigned int)
slouken@5389
   804
                                                        vsel, vckey);
slouken@5389
   805
slouken@5389
   806
                /* permute to source format */
slouken@5389
   807
                vs = vec_perm(vs, valpha, vsrcPermute);
slouken@5389
   808
slouken@5389
   809
                /* d = *dstp */
slouken@5389
   810
                vd = (vector unsigned char) vec_ld(0, dstp);
slouken@5389
   811
                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
slouken@5389
   812
slouken@5389
   813
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
slouken@5389
   814
slouken@5389
   815
                /* set the alpha channel to full on */
slouken@5389
   816
                vd = vec_or(vd, valphamask);
slouken@5389
   817
slouken@5389
   818
                /* mask out color key */
slouken@5389
   819
                vd = vec_sel(vd, vd_orig, vsel);
slouken@5389
   820
slouken@5389
   821
                /* permute to dest format */
slouken@5389
   822
                vd = vec_perm(vd, vbits, vdstPermute);
slouken@5389
   823
slouken@5389
   824
                /* *dstp = res */
slouken@5389
   825
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@5389
   826
slouken@5389
   827
                srcp += 4;
slouken@5389
   828
                dstp += 4;
slouken@5389
   829
                width -= 4;
slouken@5389
   830
                vs = voverflow;
slouken@5389
   831
            }
slouken@5389
   832
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
   833
        }
slouken@5389
   834
#undef ONE_PIXEL_BLEND
slouken@5389
   835
slouken@5389
   836
        srcp += srcskip;
slouken@5389
   837
        dstp += dstskip;
slouken@5389
   838
    }
slouken@5389
   839
}
slouken@5389
   840
slouken@5389
   841
slouken@5389
   842
static void
slouken@5389
   843
Blit32to32PixelAlphaAltivec(SDL_BlitInfo * info)
slouken@5389
   844
{
slouken@5389
   845
    int width = info->dst_w;
slouken@5389
   846
    int height = info->dst_h;
slouken@5389
   847
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
   848
    int srcskip = info->src_skip >> 2;
slouken@5389
   849
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
   850
    int dstskip = info->dst_skip >> 2;
slouken@5389
   851
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@5389
   852
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@5389
   853
    vector unsigned char mergePermute;
slouken@5389
   854
    vector unsigned char valphaPermute;
slouken@5389
   855
    vector unsigned char vsrcPermute;
slouken@5389
   856
    vector unsigned char vdstPermute;
slouken@5389
   857
    vector unsigned char vsdstPermute;
slouken@5389
   858
    vector unsigned char valphamask;
slouken@5389
   859
    vector unsigned char vpixelmask;
slouken@5389
   860
    vector unsigned char v0;
slouken@5389
   861
    vector unsigned short v1;
slouken@5389
   862
    vector unsigned short v8;
slouken@5389
   863
slouken@5389
   864
    v0 = vec_splat_u8(0);
slouken@5389
   865
    v1 = vec_splat_u16(1);
slouken@5389
   866
    v8 = vec_splat_u16(8);
slouken@5389
   867
    mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
   868
    valphamask = VEC_ALPHA_MASK();
slouken@5389
   869
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@5389
   870
    vpixelmask = vec_nor(valphamask, v0);
slouken@5389
   871
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
slouken@5389
   872
    vdstPermute = calc_swizzle32(NULL, dstfmt);
slouken@5389
   873
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
slouken@5389
   874
slouken@5389
   875
    while (height--) {
slouken@5389
   876
        width = info->dst_w;
slouken@5389
   877
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
slouken@5389
   878
            Uint32 Pixel; \
slouken@5389
   879
            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
slouken@5389
   880
            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
slouken@5389
   881
            if(sA) { \
slouken@5389
   882
              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
slouken@5389
   883
              ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
slouken@5389
   884
              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
slouken@5389
   885
            } \
slouken@5389
   886
            ++srcp; \
slouken@5389
   887
            ++dstp; \
slouken@5389
   888
            widthvar--; \
slouken@5389
   889
        }
slouken@5389
   890
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
slouken@5389
   891
        if (width > 0) {
slouken@5389
   892
            /* vsrcPermute */
slouken@5389
   893
            /* vdstPermute */
slouken@5389
   894
            int extrawidth = (width % 4);
slouken@5389
   895
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@5389
   896
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
slouken@5389
   897
            width -= extrawidth;
slouken@5389
   898
            while (width) {
slouken@5389
   899
                vector unsigned char voverflow;
slouken@5389
   900
                vector unsigned char vd;
slouken@5389
   901
                vector unsigned char valpha;
slouken@5389
   902
                vector unsigned char vdstalpha;
slouken@5389
   903
                /* s = *srcp */
slouken@5389
   904
                voverflow = (vector unsigned char) vec_ld(15, srcp);
slouken@5389
   905
                vs = vec_perm(vs, voverflow, valigner);
slouken@5389
   906
                vs = vec_perm(vs, v0, vsrcPermute);
slouken@5389
   907
slouken@5389
   908
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@5389
   909
slouken@5389
   910
                /* d = *dstp */
slouken@5389
   911
                vd = (vector unsigned char) vec_ld(0, dstp);
slouken@5389
   912
                vd = vec_perm(vd, v0, vsdstPermute);
slouken@5389
   913
                vdstalpha = vec_and(vd, valphamask);
slouken@5389
   914
slouken@5389
   915
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
slouken@5389
   916
slouken@5389
   917
                /* set the alpha to the dest alpha */
slouken@5389
   918
                vd = vec_and(vd, vpixelmask);
slouken@5389
   919
                vd = vec_or(vd, vdstalpha);
slouken@5389
   920
                vd = vec_perm(vd, v0, vdstPermute);
slouken@5389
   921
slouken@5389
   922
                /* *dstp = res */
slouken@5389
   923
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@5389
   924
slouken@5389
   925
                srcp += 4;
slouken@5389
   926
                dstp += 4;
slouken@5389
   927
                width -= 4;
slouken@5389
   928
                vs = voverflow;
slouken@5389
   929
slouken@5389
   930
            }
slouken@5389
   931
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
   932
        }
slouken@5389
   933
        srcp += srcskip;
slouken@5389
   934
        dstp += dstskip;
slouken@5389
   935
#undef ONE_PIXEL_BLEND
slouken@5389
   936
    }
slouken@5389
   937
}
slouken@5389
   938
slouken@5389
   939
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@5389
   940
static void
slouken@5389
   941
BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo * info)
slouken@5389
   942
{
slouken@5389
   943
    int width = info->dst_w;
slouken@5389
   944
    int height = info->dst_h;
slouken@5389
   945
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
   946
    int srcskip = info->src_skip >> 2;
slouken@5389
   947
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
   948
    int dstskip = info->dst_skip >> 2;
slouken@5389
   949
    vector unsigned char mergePermute;
slouken@5389
   950
    vector unsigned char valphaPermute;
slouken@5389
   951
    vector unsigned char valphamask;
slouken@5389
   952
    vector unsigned char vpixelmask;
slouken@5389
   953
    vector unsigned char v0;
slouken@5389
   954
    vector unsigned short v1;
slouken@5389
   955
    vector unsigned short v8;
slouken@5389
   956
    v0 = vec_splat_u8(0);
slouken@5389
   957
    v1 = vec_splat_u16(1);
slouken@5389
   958
    v8 = vec_splat_u16(8);
slouken@5389
   959
    mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
   960
    valphamask = VEC_ALPHA_MASK();
slouken@5389
   961
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@5389
   962
slouken@5389
   963
slouken@5389
   964
    vpixelmask = vec_nor(valphamask, v0);
slouken@5389
   965
    while (height--) {
slouken@5389
   966
        width = info->dst_w;
slouken@5389
   967
#define ONE_PIXEL_BLEND(condition, widthvar) \
slouken@5389
   968
        while ((condition)) { \
slouken@5389
   969
            Uint32 dalpha; \
slouken@5389
   970
            Uint32 d; \
slouken@5389
   971
            Uint32 s1; \
slouken@5389
   972
            Uint32 d1; \
slouken@5389
   973
            Uint32 s = *srcp; \
slouken@5389
   974
            Uint32 alpha = s >> 24; \
slouken@5389
   975
            if(alpha) { \
slouken@5389
   976
              if(alpha == SDL_ALPHA_OPAQUE) { \
slouken@5389
   977
                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
slouken@5389
   978
              } else { \
slouken@5389
   979
                d = *dstp; \
slouken@5389
   980
                dalpha = d & 0xff000000; \
slouken@5389
   981
                s1 = s & 0xff00ff; \
slouken@5389
   982
                d1 = d & 0xff00ff; \
slouken@5389
   983
                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
slouken@5389
   984
                s &= 0xff00; \
slouken@5389
   985
                d &= 0xff00; \
slouken@5389
   986
                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
slouken@5389
   987
                *dstp = d1 | d | dalpha; \
slouken@5389
   988
              } \
slouken@5389
   989
            } \
slouken@5389
   990
            ++srcp; \
slouken@5389
   991
            ++dstp; \
slouken@5389
   992
            widthvar--; \
slouken@5389
   993
	    }
slouken@5389
   994
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
slouken@5389
   995
        if (width > 0) {
slouken@5389
   996
            int extrawidth = (width % 4);
slouken@5389
   997
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@5389
   998
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
slouken@5389
   999
            width -= extrawidth;
slouken@5389
  1000
            while (width) {
slouken@5389
  1001
                vector unsigned char voverflow;
slouken@5389
  1002
                vector unsigned char vd;
slouken@5389
  1003
                vector unsigned char valpha;
slouken@5389
  1004
                vector unsigned char vdstalpha;
slouken@5389
  1005
                /* s = *srcp */
slouken@5389
  1006
                voverflow = (vector unsigned char) vec_ld(15, srcp);
slouken@5389
  1007
                vs = vec_perm(vs, voverflow, valigner);
slouken@5389
  1008
slouken@5389
  1009
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@5389
  1010
slouken@5389
  1011
                /* d = *dstp */
slouken@5389
  1012
                vd = (vector unsigned char) vec_ld(0, dstp);
slouken@5389
  1013
                vdstalpha = vec_and(vd, valphamask);
slouken@5389
  1014
slouken@5389
  1015
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
slouken@5389
  1016
slouken@5389
  1017
                /* set the alpha to the dest alpha */
slouken@5389
  1018
                vd = vec_and(vd, vpixelmask);
slouken@5389
  1019
                vd = vec_or(vd, vdstalpha);
slouken@5389
  1020
slouken@5389
  1021
                /* *dstp = res */
slouken@5389
  1022
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@5389
  1023
slouken@5389
  1024
                srcp += 4;
slouken@5389
  1025
                dstp += 4;
slouken@5389
  1026
                width -= 4;
slouken@5389
  1027
                vs = voverflow;
slouken@5389
  1028
            }
slouken@5389
  1029
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
  1030
        }
slouken@5389
  1031
        srcp += srcskip;
slouken@5389
  1032
        dstp += dstskip;
slouken@5389
  1033
    }
slouken@5389
  1034
#undef ONE_PIXEL_BLEND
slouken@5389
  1035
}
slouken@5389
  1036
slouken@5389
  1037
static void
slouken@5389
  1038
Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo * info)
slouken@5389
  1039
{
slouken@5389
  1040
    /* XXX : 6 */
slouken@5389
  1041
    int height = info->dst_h;
slouken@5389
  1042
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
  1043
    int srcskip = info->src_skip >> 2;
slouken@5389
  1044
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
  1045
    int dstskip = info->dst_skip >> 2;
slouken@5389
  1046
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@5389
  1047
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@5389
  1048
    unsigned sA = info->a;
slouken@5389
  1049
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@5389
  1050
    vector unsigned char mergePermute;
slouken@5389
  1051
    vector unsigned char vsrcPermute;
slouken@5389
  1052
    vector unsigned char vdstPermute;
slouken@5389
  1053
    vector unsigned char vsdstPermute;
slouken@5389
  1054
    vector unsigned char valpha;
slouken@5389
  1055
    vector unsigned char valphamask;
slouken@5389
  1056
    vector unsigned char vbits;
slouken@5389
  1057
    vector unsigned short v1;
slouken@5389
  1058
    vector unsigned short v8;
slouken@5389
  1059
slouken@5389
  1060
    mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
  1061
    v1 = vec_splat_u16(1);
slouken@5389
  1062
    v8 = vec_splat_u16(8);
slouken@5389
  1063
slouken@5389
  1064
    /* set the alpha to 255 on the destination surf */
slouken@5389
  1065
    valphamask = VEC_ALPHA_MASK();
slouken@5389
  1066
slouken@5389
  1067
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
slouken@5389
  1068
    vdstPermute = calc_swizzle32(NULL, dstfmt);
slouken@5389
  1069
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
slouken@5389
  1070
slouken@5389
  1071
    /* set a vector full of alpha and 255-alpha */
slouken@5389
  1072
    ((unsigned char *) &valpha)[0] = sA;
slouken@5389
  1073
    valpha = vec_splat(valpha, 0);
slouken@5389
  1074
    vbits = (vector unsigned char) vec_splat_s8(-1);
slouken@5389
  1075
slouken@5389
  1076
    while (height--) {
slouken@5389
  1077
        int width = info->dst_w;
slouken@5389
  1078
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
slouken@5389
  1079
            Uint32 Pixel; \
slouken@5389
  1080
            unsigned sR, sG, sB, dR, dG, dB; \
slouken@5389
  1081
            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
slouken@5389
  1082
            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
slouken@5389
  1083
            ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
slouken@5389
  1084
            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
slouken@5389
  1085
            ++srcp; \
slouken@5389
  1086
            ++dstp; \
slouken@5389
  1087
            widthvar--; \
slouken@5389
  1088
        }
slouken@5389
  1089
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
slouken@5389
  1090
        if (width > 0) {
slouken@5389
  1091
            int extrawidth = (width % 4);
slouken@5389
  1092
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@5389
  1093
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
slouken@5389
  1094
            width -= extrawidth;
slouken@5389
  1095
            while (width) {
slouken@5389
  1096
                vector unsigned char voverflow;
slouken@5389
  1097
                vector unsigned char vd;
slouken@5389
  1098
slouken@5389
  1099
                /* s = *srcp */
slouken@5389
  1100
                voverflow = (vector unsigned char) vec_ld(15, srcp);
slouken@5389
  1101
                vs = vec_perm(vs, voverflow, valigner);
slouken@5389
  1102
                vs = vec_perm(vs, valpha, vsrcPermute);
slouken@5389
  1103
slouken@5389
  1104
                /* d = *dstp */
slouken@5389
  1105
                vd = (vector unsigned char) vec_ld(0, dstp);
slouken@5389
  1106
                vd = vec_perm(vd, vd, vsdstPermute);
slouken@5389
  1107
slouken@5389
  1108
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
slouken@5389
  1109
slouken@5389
  1110
                /* set the alpha channel to full on */
slouken@5389
  1111
                vd = vec_or(vd, valphamask);
slouken@5389
  1112
                vd = vec_perm(vd, vbits, vdstPermute);
slouken@5389
  1113
slouken@5389
  1114
                /* *dstp = res */
slouken@5389
  1115
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@5389
  1116
slouken@5389
  1117
                srcp += 4;
slouken@5389
  1118
                dstp += 4;
slouken@5389
  1119
                width -= 4;
slouken@5389
  1120
                vs = voverflow;
slouken@5389
  1121
            }
slouken@5389
  1122
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
  1123
        }
slouken@5389
  1124
#undef ONE_PIXEL_BLEND
slouken@5389
  1125
slouken@5389
  1126
        srcp += srcskip;
slouken@5389
  1127
        dstp += dstskip;
slouken@5389
  1128
    }
slouken@5389
  1129
slouken@5389
  1130
}
slouken@5389
  1131
slouken@5389
  1132
slouken@5389
  1133
/* fast RGB888->(A)RGB888 blending */
slouken@5389
  1134
static void
slouken@5389
  1135
BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo * info)
slouken@5389
  1136
{
slouken@5389
  1137
    unsigned alpha = info->a;
slouken@5389
  1138
    int height = info->dst_h;
slouken@5389
  1139
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
  1140
    int srcskip = info->src_skip >> 2;
slouken@5389
  1141
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
  1142
    int dstskip = info->dst_skip >> 2;
slouken@5389
  1143
    vector unsigned char mergePermute;
slouken@5389
  1144
    vector unsigned char valpha;
slouken@5389
  1145
    vector unsigned char valphamask;
slouken@5389
  1146
    vector unsigned short v1;
slouken@5389
  1147
    vector unsigned short v8;
slouken@5389
  1148
slouken@5389
  1149
    mergePermute = VEC_MERGE_PERMUTE();
slouken@5389
  1150
    v1 = vec_splat_u16(1);
slouken@5389
  1151
    v8 = vec_splat_u16(8);
slouken@5389
  1152
slouken@5389
  1153
    /* set the alpha to 255 on the destination surf */
slouken@5389
  1154
    valphamask = VEC_ALPHA_MASK();
slouken@5389
  1155
slouken@5389
  1156
    /* set a vector full of alpha and 255-alpha */
slouken@5389
  1157
    ((unsigned char *) &valpha)[0] = alpha;
slouken@5389
  1158
    valpha = vec_splat(valpha, 0);
slouken@5389
  1159
slouken@5389
  1160
    while (height--) {
slouken@5389
  1161
        int width = info->dst_w;
slouken@5389
  1162
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
slouken@5389
  1163
            Uint32 s = *srcp; \
slouken@5389
  1164
            Uint32 d = *dstp; \
slouken@5389
  1165
            Uint32 s1 = s & 0xff00ff; \
slouken@5389
  1166
            Uint32 d1 = d & 0xff00ff; \
slouken@5389
  1167
            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
slouken@5389
  1168
                 & 0xff00ff; \
slouken@5389
  1169
            s &= 0xff00; \
slouken@5389
  1170
            d &= 0xff00; \
slouken@5389
  1171
            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
slouken@5389
  1172
            *dstp = d1 | d | 0xff000000; \
slouken@5389
  1173
            ++srcp; \
slouken@5389
  1174
            ++dstp; \
slouken@5389
  1175
            widthvar--; \
slouken@5389
  1176
        }
slouken@5389
  1177
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
slouken@5389
  1178
        if (width > 0) {
slouken@5389
  1179
            int extrawidth = (width % 4);
slouken@5389
  1180
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@5389
  1181
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
slouken@5389
  1182
            width -= extrawidth;
slouken@5389
  1183
            while (width) {
slouken@5389
  1184
                vector unsigned char voverflow;
slouken@5389
  1185
                vector unsigned char vd;
slouken@5389
  1186
slouken@5389
  1187
                /* s = *srcp */
slouken@5389
  1188
                voverflow = (vector unsigned char) vec_ld(15, srcp);
slouken@5389
  1189
                vs = vec_perm(vs, voverflow, valigner);
slouken@5389
  1190
slouken@5389
  1191
                /* d = *dstp */
slouken@5389
  1192
                vd = (vector unsigned char) vec_ld(0, dstp);
slouken@5389
  1193
slouken@5389
  1194
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
slouken@5389
  1195
slouken@5389
  1196
                /* set the alpha channel to full on */
slouken@5389
  1197
                vd = vec_or(vd, valphamask);
slouken@5389
  1198
slouken@5389
  1199
                /* *dstp = res */
slouken@5389
  1200
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@5389
  1201
slouken@5389
  1202
                srcp += 4;
slouken@5389
  1203
                dstp += 4;
slouken@5389
  1204
                width -= 4;
slouken@5389
  1205
                vs = voverflow;
slouken@5389
  1206
            }
slouken@5389
  1207
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
slouken@5389
  1208
        }
slouken@5389
  1209
#undef ONE_PIXEL_BLEND
slouken@5389
  1210
slouken@5389
  1211
        srcp += srcskip;
slouken@5389
  1212
        dstp += dstskip;
slouken@5389
  1213
    }
slouken@5389
  1214
}
slouken@5389
  1215
slouken@5389
  1216
#if __MWERKS__
slouken@5389
  1217
#pragma altivec_model off
slouken@5389
  1218
#endif
slouken@5389
  1219
#endif /* SDL_ALTIVEC_BLITTERS */
slouken@5389
  1220
slouken@1
  1221
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
  1222
static void
slouken@1895
  1223
BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
slouken@0
  1224
{
slouken@2262
  1225
    int width = info->dst_w;
slouken@2262
  1226
    int height = info->dst_h;
slouken@2262
  1227
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1228
    int srcskip = info->src_skip >> 2;
slouken@2262
  1229
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1230
    int dstskip = info->dst_skip >> 2;
slouken@0
  1231
slouken@1895
  1232
    while (height--) {
slouken@1895
  1233
	    /* *INDENT-OFF* */
slouken@0
  1234
	    DUFFS_LOOP4({
slouken@1
  1235
		    Uint32 s = *srcp++;
slouken@1
  1236
		    Uint32 d = *dstp;
slouken@1
  1237
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1238
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1239
	    }, width);
slouken@1895
  1240
	    /* *INDENT-ON* */
slouken@1895
  1241
        srcp += srcskip;
slouken@1895
  1242
        dstp += dstskip;
slouken@1895
  1243
    }
slouken@0
  1244
}
slouken@0
  1245
slouken@1
  1246
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
  1247
static void
slouken@1895
  1248
BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1249
{
slouken@2267
  1250
    unsigned alpha = info->a;
slouken@1895
  1251
    if (alpha == 128) {
slouken@1895
  1252
        BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1895
  1253
    } else {
slouken@2262
  1254
        int width = info->dst_w;
slouken@2262
  1255
        int height = info->dst_h;
slouken@2262
  1256
        Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1257
        int srcskip = info->src_skip >> 2;
slouken@2262
  1258
        Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1259
        int dstskip = info->dst_skip >> 2;
slouken@1895
  1260
        Uint32 s;
slouken@1895
  1261
        Uint32 d;
slouken@1895
  1262
        Uint32 s1;
slouken@1895
  1263
        Uint32 d1;
slouken@1
  1264
slouken@1895
  1265
        while (height--) {
slouken@1895
  1266
			/* *INDENT-OFF* */
slouken@3035
  1267
			DUFFS_LOOP4({
slouken@1
  1268
				s = *srcp;
slouken@1
  1269
				d = *dstp;
slouken@1
  1270
				s1 = s & 0xff00ff;
slouken@1
  1271
				d1 = d & 0xff00ff;
slouken@1
  1272
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1273
				     & 0xff00ff;
slouken@1
  1274
				s &= 0xff00;
slouken@1
  1275
				d &= 0xff00;
slouken@1
  1276
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1277
				*dstp = d1 | d | 0xff000000;
slouken@1
  1278
				++srcp;
slouken@1
  1279
				++dstp;
slouken@1
  1280
			}, width);
slouken@1895
  1281
			/* *INDENT-ON* */
slouken@1895
  1282
            srcp += srcskip;
slouken@1895
  1283
            dstp += dstskip;
slouken@1895
  1284
        }
slouken@1895
  1285
    }
slouken@1
  1286
}
slouken@1
  1287
slouken@0
  1288
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1289
static void
slouken@1895
  1290
BlitRGBtoRGBPixelAlpha(SDL_BlitInfo * info)
slouken@0
  1291
{
slouken@2262
  1292
    int width = info->dst_w;
slouken@2262
  1293
    int height = info->dst_h;
slouken@2262
  1294
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1295
    int srcskip = info->src_skip >> 2;
slouken@2262
  1296
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1297
    int dstskip = info->dst_skip >> 2;
slouken@0
  1298
slouken@1895
  1299
    while (height--) {
slouken@1895
  1300
	    /* *INDENT-OFF* */
slouken@0
  1301
	    DUFFS_LOOP4({
slouken@0
  1302
		Uint32 dalpha;
slouken@0
  1303
		Uint32 d;
slouken@0
  1304
		Uint32 s1;
slouken@0
  1305
		Uint32 d1;
slouken@0
  1306
		Uint32 s = *srcp;
slouken@0
  1307
		Uint32 alpha = s >> 24;
slouken@0
  1308
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1309
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1310
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1311
		   Benchmark this! */
slouken@689
  1312
		if(alpha) {   
slouken@689
  1313
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1314
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1315
		  } else {
slouken@0
  1316
		    /*
slouken@0
  1317
		     * take out the middle component (green), and process
slouken@0
  1318
		     * the other two in parallel. One multiply less.
slouken@0
  1319
		     */
slouken@0
  1320
		    d = *dstp;
slouken@0
  1321
		    dalpha = d & 0xff000000;
slouken@0
  1322
		    s1 = s & 0xff00ff;
slouken@0
  1323
		    d1 = d & 0xff00ff;
slouken@0
  1324
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1325
		    s &= 0xff00;
slouken@0
  1326
		    d &= 0xff00;
slouken@0
  1327
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1328
		    *dstp = d1 | d | dalpha;
slouken@689
  1329
		  }
slouken@0
  1330
		}
slouken@0
  1331
		++srcp;
slouken@0
  1332
		++dstp;
slouken@0
  1333
	    }, width);
slouken@1895
  1334
	    /* *INDENT-ON* */
slouken@1895
  1335
        srcp += srcskip;
slouken@1895
  1336
        dstp += dstskip;
slouken@1895
  1337
    }
slouken@0
  1338
}
slouken@0
  1339
slouken@5389
  1340
#ifdef __3dNOW__
slouken@5389
  1341
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@5389
  1342
static void
slouken@5389
  1343
BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
slouken@5389
  1344
{
slouken@5389
  1345
    int width = info->dst_w;
slouken@5389
  1346
    int height = info->dst_h;
slouken@5389
  1347
    Uint32 *srcp = (Uint32 *) info->src;
slouken@5389
  1348
    int srcskip = info->src_skip >> 2;
slouken@5389
  1349
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@5389
  1350
    int dstskip = info->dst_skip >> 2;
slouken@5389
  1351
    SDL_PixelFormat *sf = info->src_fmt;
slouken@5389
  1352
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@5389
  1353
    Uint32 amask = sf->Amask;
slouken@5389
  1354
    Uint32 ashift = sf->Ashift;
slouken@5389
  1355
    Uint64 multmask;
slouken@5389
  1356
slouken@5389
  1357
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@5389
  1358
slouken@5389
  1359
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@5389
  1360
    multmask = 0xFFFF;
slouken@5389
  1361
    multmask <<= (ashift * 2);
slouken@5389
  1362
    multmask = ~multmask;
slouken@5389
  1363
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@5389
  1364
slouken@5389
  1365
    while (height--) {
slouken@5389
  1366
	    /* *INDENT-OFF* */
slouken@5389
  1367
	    DUFFS_LOOP4({
slouken@5389
  1368
		Uint32 alpha;
slouken@5389
  1369
slouken@5389
  1370
		_m_prefetch(srcp + 16);
slouken@5389
  1371
		_m_prefetch(dstp + 16);
slouken@5389
  1372
slouken@5389
  1373
		alpha = *srcp & amask;
slouken@5389
  1374
		if (alpha == 0) {
slouken@5389
  1375
			/* do nothing */
slouken@5389
  1376
		} else if (alpha == amask) {
slouken@5389
  1377
			/* copy RGB, keep dst alpha */
slouken@5389
  1378
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@5389
  1379
		} else {
slouken@5389
  1380
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@5389
  1381
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@5389
  1382
slouken@5389
  1383
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@5389
  1384
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@5389
  1385
slouken@5389
  1386
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@5389
  1387
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@5389
  1388
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@5389
  1389
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@5389
  1390
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@5389
  1391
slouken@5389
  1392
			/* blend */		    
slouken@5389
  1393
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@5389
  1394
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@5389
  1395
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@5389
  1396
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@5389
  1397
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@5389
  1398
			
slouken@5389
  1399
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@5389
  1400
		}
slouken@5389
  1401
		++srcp;
slouken@5389
  1402
		++dstp;
slouken@5389
  1403
	    }, width);
slouken@5389
  1404
	    /* *INDENT-ON* */
slouken@5389
  1405
        srcp += srcskip;
slouken@5389
  1406
        dstp += dstskip;
slouken@5389
  1407
    }
slouken@5389
  1408
    _mm_empty();
slouken@5389
  1409
}
slouken@5389
  1410
slouken@5389
  1411
#endif /* __MMX__ */
slouken@5389
  1412
slouken@1
  1413
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1414
slouken@1
  1415
/* blend a single 16 bit pixel at 50% */
slouken@1
  1416
#define BLEND16_50(d, s, mask)						\
slouken@1
  1417
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1418
slouken@1
  1419
/* blend two 16 bit pixels at 50% */
slouken@1
  1420
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1421
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1422
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1423
slouken@1895
  1424
static void
slouken@1895
  1425
Blit16to16SurfaceAlpha128(SDL_BlitInfo * info, Uint16 mask)
slouken@0
  1426
{
slouken@2262
  1427
    int width = info->dst_w;
slouken@2262
  1428
    int height = info->dst_h;
slouken@2262
  1429
    Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1430
    int srcskip = info->src_skip >> 1;
slouken@2262
  1431
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1432
    int dstskip = info->dst_skip >> 1;
slouken@0
  1433
slouken@1895
  1434
    while (height--) {
slouken@1895
  1435
        if (((uintptr_t) srcp ^ (uintptr_t) dstp) & 2) {
slouken@1895
  1436
            /*
slouken@1895
  1437
             * Source and destination not aligned, pipeline it.
slouken@1895
  1438
             * This is mostly a win for big blits but no loss for
slouken@1895
  1439
             * small ones
slouken@1895
  1440
             */
slouken@1895
  1441
            Uint32 prev_sw;
slouken@1895
  1442
            int w = width;
slouken@1
  1443
slouken@1895
  1444
            /* handle odd destination */
slouken@1895
  1445
            if ((uintptr_t) dstp & 2) {
slouken@1895
  1446
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1447
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1448
                dstp++;
slouken@1895
  1449
                srcp++;
slouken@1895
  1450
                w--;
slouken@1895
  1451
            }
slouken@1895
  1452
            srcp++;             /* srcp is now 32-bit aligned */
slouken@1
  1453
slouken@1895
  1454
            /* bootstrap pipeline with first halfword */
slouken@1895
  1455
            prev_sw = ((Uint32 *) srcp)[-1];
slouken@1
  1456
slouken@1895
  1457
            while (w > 1) {
slouken@1895
  1458
                Uint32 sw, dw, s;
slouken@1895
  1459
                sw = *(Uint32 *) srcp;
slouken@1895
  1460
                dw = *(Uint32 *) dstp;
slouken@1443
  1461
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1462
                s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1463
#else
slouken@1895
  1464
                s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1465
#endif
slouken@1895
  1466
                prev_sw = sw;
slouken@1895
  1467
                *(Uint32 *) dstp = BLEND2x16_50(dw, s, mask);
slouken@1895
  1468
                dstp += 2;
slouken@1895
  1469
                srcp += 2;
slouken@1895
  1470
                w -= 2;
slouken@1895
  1471
            }
slouken@1
  1472
slouken@1895
  1473
            /* final pixel if any */
slouken@1895
  1474
            if (w) {
slouken@1895
  1475
                Uint16 d = *dstp, s;
slouken@1443
  1476
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1477
                s = (Uint16) prev_sw;
slouken@1443
  1478
#else
slouken@1895
  1479
                s = (Uint16) (prev_sw >> 16);
slouken@1443
  1480
#endif
slouken@1895
  1481
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1482
                srcp++;
slouken@1895
  1483
                dstp++;
slouken@1895
  1484
            }
slouken@1895
  1485
            srcp += srcskip - 1;
slouken@1895
  1486
            dstp += dstskip;
slouken@1895
  1487
        } else {
slouken@1895
  1488
            /* source and destination are aligned */
slouken@1895
  1489
            int w = width;
slouken@1
  1490
slouken@1895
  1491
            /* first odd pixel? */
slouken@1895
  1492
            if ((uintptr_t) srcp & 2) {
slouken@1895
  1493
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1494
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1495
                srcp++;
slouken@1895
  1496
                dstp++;
slouken@1895
  1497
                w--;
slouken@1895
  1498
            }
slouken@1895
  1499
            /* srcp and dstp are now 32-bit aligned */
slouken@1
  1500
slouken@1895
  1501
            while (w > 1) {
slouken@1895
  1502
                Uint32 sw = *(Uint32 *) srcp;
slouken@1895
  1503
                Uint32 dw = *(Uint32 *) dstp;
slouken@1895
  1504
                *(Uint32 *) dstp = BLEND2x16_50(dw, sw, mask);
slouken@1895
  1505
                srcp += 2;
slouken@1895
  1506
                dstp += 2;
slouken@1895
  1507
                w -= 2;
slouken@1895
  1508
            }
slouken@1
  1509
slouken@1895
  1510
            /* last odd pixel? */
slouken@1895
  1511
            if (w) {
slouken@1895
  1512
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1513
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1514
                srcp++;
slouken@1895
  1515
                dstp++;
slouken@1895
  1516
            }
slouken@1895
  1517
            srcp += srcskip;
slouken@1895
  1518
            dstp += dstskip;
slouken@1895
  1519
        }
slouken@1895
  1520
    }
slouken@1
  1521
}
slouken@1
  1522
slouken@2255
  1523
#ifdef __MMX__
slouken@689
  1524
slouken@1542
  1525
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1526
static void
slouken@1895
  1527
Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1528
{
slouken@2267
  1529
    unsigned alpha = info->a;
slouken@1895
  1530
    if (alpha == 128) {
slouken@1895
  1531
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1532
    } else {
slouken@2262
  1533
        int width = info->dst_w;
slouken@2262
  1534
        int height = info->dst_h;
slouken@2262
  1535
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1536
        int srcskip = info->src_skip >> 1;
slouken@2262
  1537
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1538
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1539
        Uint32 s, d;
slouken@1542
  1540
slouken@1895
  1541
        __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1542
slouken@1895
  1543
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1544
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1545
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1895
  1546
slouken@1895
  1547
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1548
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1549
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1550
           to reduce the number of operations */
slouken@1895
  1551
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1552
slouken@1895
  1553
        /* Setup the 565 color channel masks */
slouken@1895
  1554
        gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0);   /* MASKGREEN -> gmask */
slouken@1895
  1555
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1556
slouken@1895
  1557
        while (height--) {
slouken@1895
  1558
			/* *INDENT-OFF* */
slouken@3035
  1559
			DUFFS_LOOP_124(
slouken@1542
  1560
			{
slouken@1542
  1561
				s = *srcp++;
slouken@1542
  1562
				d = *dstp;
slouken@1542
  1563
				/*
slouken@1542
  1564
				 * shift out the middle component (green) to
slouken@1542
  1565
				 * the high 16 bits, and process all three RGB
slouken@1542
  1566
				 * components at the same time.
slouken@1542
  1567
				 */
slouken@1542
  1568
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1569
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1570
				d += (s - d) * alpha >> 5;
slouken@1542
  1571
				d &= 0x07e0f81f;
slouken@1546
  1572
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1573
			},{
slouken@1542
  1574
				s = *srcp++;
slouken@1542
  1575
				d = *dstp;
slouken@1542
  1576
				/*
slouken@1542
  1577
				 * shift out the middle component (green) to
slouken@1542
  1578
				 * the high 16 bits, and process all three RGB
slouken@1542
  1579
				 * components at the same time.
slouken@1542
  1580
				 */
slouken@1542
  1581
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1582
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1583
				d += (s - d) * alpha >> 5;
slouken@1542
  1584
				d &= 0x07e0f81f;
slouken@1546
  1585
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1586
				s = *srcp++;
slouken@1542
  1587
				d = *dstp;
slouken@1542
  1588
				/*
slouken@1542
  1589
				 * shift out the middle component (green) to
slouken@1542
  1590
				 * the high 16 bits, and process all three RGB
slouken@1542
  1591
				 * components at the same time.
slouken@1542
  1592
				 */
slouken@1542
  1593
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1594
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1595
				d += (s - d) * alpha >> 5;
slouken@1542
  1596
				d &= 0x07e0f81f;
slouken@1546
  1597
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1598
			},{
slouken@1542
  1599
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1600
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1601
slouken@1542
  1602
				/* red */
slouken@1542
  1603
				src2 = src1;
slouken@1542
  1604
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  1605
slouken@1542
  1606
				dst2 = dst1;
slouken@1542
  1607
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  1608
slouken@1542
  1609
				/* blend */
slouken@1542
  1610
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1611
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1612
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1613
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1614
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  1615
slouken@1542
  1616
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1617
slouken@1542
  1618
				/* green -- process the bits in place */
slouken@1542
  1619
				src2 = src1;
slouken@1542
  1620
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1621
slouken@1542
  1622
				dst2 = dst1;
slouken@1542
  1623
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1624
slouken@1542
  1625
				/* blend */
slouken@1542
  1626
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1627
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1628
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1629
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1630
slouken@1542
  1631
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1632
slouken@1542
  1633
				/* blue */
slouken@1542
  1634
				src2 = src1;
slouken@1542
  1635
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1636
slouken@1542
  1637
				dst2 = dst1;
slouken@1542
  1638
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1639
slouken@1542
  1640
				/* blend */
slouken@1542
  1641
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1642
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1643
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1644
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1645
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1646
slouken@1542
  1647
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1648
slouken@1542
  1649
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1650
slouken@1542
  1651
				srcp += 4;
slouken@1542
  1652
				dstp += 4;
slouken@1895
  1653
			}, width);
slouken@1895
  1654
			/* *INDENT-ON* */
slouken@1895
  1655
            srcp += srcskip;
slouken@1895
  1656
            dstp += dstskip;
slouken@1895
  1657
        }
slouken@1895
  1658
        _mm_empty();
slouken@1895
  1659
    }
slouken@1542
  1660
}
slouken@1542
  1661
slouken@1542
  1662
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1663
static void
slouken@1895
  1664
Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1665
{
slouken@2267
  1666
    unsigned alpha = info->a;
slouken@1895
  1667
    if (alpha == 128) {
slouken@1895
  1668
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1669
    } else {
slouken@2262
  1670
        int width = info->dst_w;
slouken@2262
  1671
        int height = info->dst_h;
slouken@2262
  1672
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1673
        int srcskip = info->src_skip >> 1;
slouken@2262
  1674
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1675
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1676
        Uint32 s, d;
slouken@1542
  1677
slouken@1895
  1678
        __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1679
slouken@1895
  1680
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1681
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1682
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  1683
slouken@1895
  1684
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1685
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1686
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1687
           to reduce the number of operations */
slouken@1895
  1688
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1689
slouken@1895
  1690
        /* Setup the 555 color channel masks */
slouken@1895
  1691
        rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00);   /* MASKRED -> rmask */
slouken@1895
  1692
        gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0);   /* MASKGREEN -> gmask */
slouken@1895
  1693
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1694
slouken@1895
  1695
        while (height--) {
slouken@1895
  1696
			/* *INDENT-OFF* */
slouken@3035
  1697
			DUFFS_LOOP_124(
slouken@1542
  1698
			{
slouken@1542
  1699
				s = *srcp++;
slouken@1542
  1700
				d = *dstp;
slouken@1542
  1701
				/*
slouken@1542
  1702
				 * shift out the middle component (green) to
slouken@1542
  1703
				 * the high 16 bits, and process all three RGB
slouken@1542
  1704
				 * components at the same time.
slouken@1542
  1705
				 */
slouken@1542
  1706
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1707
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1708
				d += (s - d) * alpha >> 5;
slouken@1542
  1709
				d &= 0x03e07c1f;
slouken@1546
  1710
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1711
			},{
slouken@1542
  1712
				s = *srcp++;
slouken@1542
  1713
				d = *dstp;
slouken@1542
  1714
				/*
slouken@1542
  1715
				 * shift out the middle component (green) to
slouken@1542
  1716
				 * the high 16 bits, and process all three RGB
slouken@1542
  1717
				 * components at the same time.
slouken@1542
  1718
				 */
slouken@1542
  1719
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1720
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1721
				d += (s - d) * alpha >> 5;
slouken@1542
  1722
				d &= 0x03e07c1f;
slouken@1546
  1723
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1724
			        s = *srcp++;
slouken@1542
  1725
				d = *dstp;
slouken@1542
  1726
				/*
slouken@1542
  1727
				 * shift out the middle component (green) to
slouken@1542
  1728
				 * the high 16 bits, and process all three RGB
slouken@1542
  1729
				 * components at the same time.
slouken@1542
  1730
				 */
slouken@1542
  1731
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1732
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1733
				d += (s - d) * alpha >> 5;
slouken@1542
  1734
				d &= 0x03e07c1f;
slouken@1546
  1735
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1736
			},{
slouken@1542
  1737
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1738
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1739
slouken@1542
  1740
				/* red -- process the bits in place */
slouken@1542
  1741
				src2 = src1;
slouken@1542
  1742
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  1743
slouken@1542
  1744
				dst2 = dst1;
slouken@1542
  1745
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  1746
slouken@1542
  1747
				/* blend */
slouken@1542
  1748
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1749
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1750
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1751
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1752
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  1753
slouken@1542
  1754
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1755
				
slouken@1542
  1756
				/* green -- process the bits in place */
slouken@1542
  1757
				src2 = src1;
slouken@1542
  1758
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1759
slouken@1542
  1760
				dst2 = dst1;
slouken@1542
  1761
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1762
slouken@1542
  1763
				/* blend */
slouken@1542
  1764
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1765
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1766
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1767
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1768
slouken@1542
  1769
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1770
slouken@1542
  1771
				/* blue */
slouken@1542
  1772
				src2 = src1; /* src -> src2 */
slouken@1542
  1773
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1774
slouken@1542
  1775
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  1776
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1777
slouken@1542
  1778
				/* blend */
slouken@1542
  1779
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1780
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1781
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1782
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1783
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1784
slouken@1542
  1785
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1786
slouken@1542
  1787
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1788
slouken@1542
  1789
				srcp += 4;
slouken@1542
  1790
				dstp += 4;
slouken@1895
  1791
			}, width);
slouken@1895
  1792
			/* *INDENT-ON* */
slouken@1895
  1793
            srcp += srcskip;
slouken@1895
  1794
            dstp += dstskip;
slouken@1895
  1795
        }
slouken@1895
  1796
        _mm_empty();
slouken@1895
  1797
    }
slouken@1542
  1798
}
slouken@2255
  1799
slouken@2255
  1800
#endif /* __MMX__ */
slouken@689
  1801
slouken@1
  1802
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1803
static void
slouken@1895
  1804
Blit565to565SurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1805
{
slouken@2267
  1806
    unsigned alpha = info->a;
slouken@1895
  1807
    if (alpha == 128) {
slouken@1895
  1808
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1809
    } else {
slouken@2262
  1810
        int width = info->dst_w;
slouken@2262
  1811
        int height = info->dst_h;
slouken@2262
  1812
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1813
        int srcskip = info->src_skip >> 1;
slouken@2262
  1814
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1815
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1816
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1
  1817
slouken@1895
  1818
        while (height--) {
slouken@1895
  1819
			/* *INDENT-OFF* */
slouken@1
  1820
			DUFFS_LOOP4({
slouken@1
  1821
				Uint32 s = *srcp++;
slouken@1
  1822
				Uint32 d = *dstp;
slouken@1
  1823
				/*
slouken@1
  1824
				 * shift out the middle component (green) to
slouken@1
  1825
				 * the high 16 bits, and process all three RGB
slouken@1
  1826
				 * components at the same time.
slouken@1
  1827
				 */
slouken@1
  1828
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  1829
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  1830
				d += (s - d) * alpha >> 5;
slouken@1
  1831
				d &= 0x07e0f81f;
slouken@1428
  1832
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1833
			}, width);
slouken@1895
  1834
			/* *INDENT-ON* */
slouken@1895
  1835
            srcp += srcskip;
slouken@1895
  1836
            dstp += dstskip;
slouken@1895
  1837
        }
slouken@1895
  1838
    }
slouken@0
  1839
}
slouken@0
  1840
slouken@0
  1841
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1842
static void
slouken@1895
  1843
Blit555to555SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  1844
{
slouken@2267
  1845
    unsigned alpha = info->a;   /* downscale alpha to 5 bits */
slouken@1895
  1846
    if (alpha == 128) {
slouken@1895
  1847
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1848
    } else {
slouken@2262
  1849
        int width = info->dst_w;
slouken@2262
  1850
        int height = info->dst_h;
slouken@2262
  1851
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1852
        int srcskip = info->src_skip >> 1;
slouken@2262
  1853
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1854
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1855
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@0
  1856
slouken@1895
  1857
        while (height--) {
slouken@1895
  1858
			/* *INDENT-OFF* */
slouken@1
  1859
			DUFFS_LOOP4({
slouken@1
  1860
				Uint32 s = *srcp++;
slouken@1
  1861
				Uint32 d = *dstp;
slouken@1
  1862
				/*
slouken@1
  1863
				 * shift out the middle component (green) to
slouken@1
  1864
				 * the high 16 bits, and process all three RGB
slouken@1
  1865
				 * components at the same time.
slouken@1
  1866
				 */
slouken@1
  1867
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  1868
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  1869
				d += (s - d) * alpha >> 5;
slouken@1
  1870
				d &= 0x03e07c1f;
slouken@1428
  1871
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1872
			}, width);
slouken@1895
  1873
			/* *INDENT-ON* */
slouken@1895
  1874
            srcp += srcskip;
slouken@1895
  1875
            dstp += dstskip;
slouken@1895
  1876
        }
slouken@1895
  1877
    }
slouken@0
  1878
}
slouken@0
  1879
slouken@0
  1880
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@1895
  1881
static void
slouken@1895
  1882
BlitARGBto565PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1883
{
slouken@2262
  1884
    int width = info->dst_w;
slouken@2262
  1885
    int height = info->dst_h;
slouken@2262
  1886
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1887
    int srcskip = info->src_skip >> 2;
slouken@2262
  1888
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1889
    int dstskip = info->dst_skip >> 1;
slouken@0
  1890
slouken@1895
  1891
    while (height--) {
slouken@1895
  1892
	    /* *INDENT-OFF* */
slouken@0
  1893
	    DUFFS_LOOP4({
slouken@0
  1894
		Uint32 s = *srcp;
slouken@0
  1895
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1896
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1897
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1898
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1899
		   Benchmark this! */
slouken@689
  1900
		if(alpha) {   
slouken@689
  1901
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1902
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  1903
		  } else {
slouken@0
  1904
		    Uint32 d = *dstp;
slouken@0
  1905
		    /*
slouken@0
  1906
		     * convert source and destination to G0RAB65565
slouken@0
  1907
		     * and blend all components at the same time
slouken@0
  1908
		     */
slouken@0
  1909
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  1910
		      + (s >> 3 & 0x1f);
slouken@0
  1911
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  1912
		    d += (s - d) * alpha >> 5;
slouken@0
  1913
		    d &= 0x07e0f81f;
slouken@1428
  1914
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1915
		  }
slouken@0
  1916
		}
slouken@0
  1917
		srcp++;
slouken@0
  1918
		dstp++;
slouken@0
  1919
	    }, width);
slouken@1895
  1920
	    /* *INDENT-ON* */
slouken@1895
  1921
        srcp += srcskip;
slouken@1895
  1922
        dstp += dstskip;
slouken@1895
  1923
    }
slouken@0
  1924
}
slouken@0
  1925
slouken@0
  1926
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@1895
  1927
static void
slouken@1895
  1928
BlitARGBto555PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1929
{
slouken@2262
  1930
    int width = info->dst_w;
slouken@2262
  1931
    int height = info->dst_h;
slouken@2262
  1932
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1933
    int srcskip = info->src_skip >> 2;
slouken@2262
  1934
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1935
    int dstskip = info->dst_skip >> 1;
slouken@0
  1936
slouken@1895
  1937
    while (height--) {
slouken@1895
  1938
	    /* *INDENT-OFF* */
slouken@0
  1939
	    DUFFS_LOOP4({
slouken@0
  1940
		unsigned alpha;
slouken@0
  1941
		Uint32 s = *srcp;
slouken@0
  1942
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1943
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1944
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1945
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1946
		   Benchmark this! */
slouken@689
  1947
		if(alpha) {   
slouken@689
  1948
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1949
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  1950
		  } else {
slouken@0
  1951
		    Uint32 d = *dstp;
slouken@0
  1952
		    /*
slouken@0
  1953
		     * convert source and destination to G0RAB65565
slouken@0
  1954
		     * and blend all components at the same time
slouken@0
  1955
		     */
slouken@0
  1956
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  1957
		      + (s >> 3 & 0x1f);
slouken@0
  1958
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  1959
		    d += (s - d) * alpha >> 5;
slouken@0
  1960
		    d &= 0x03e07c1f;
slouken@1428
  1961
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1962
		  }
slouken@0
  1963
		}
slouken@0
  1964
		srcp++;
slouken@0
  1965
		dstp++;
slouken@0
  1966
	    }, width);
slouken@1895
  1967
	    /* *INDENT-ON* */
slouken@1895
  1968
        srcp += srcskip;
slouken@1895
  1969
        dstp += dstskip;
slouken@1895
  1970
    }
slouken@0
  1971
}
slouken@0
  1972
slouken@0
  1973
/* General (slow) N->N blending with per-surface alpha */
slouken@1895
  1974
static void
slouken@1895
  1975
BlitNtoNSurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  1976
{
slouken@2262
  1977
    int width = info->dst_w;
slouken@2262
  1978
    int height = info->dst_h;
slouken@2262
  1979
    Uint8 *src = info->src;
slouken@2267
  1980
    int srcskip = info->src_skip;
slouken@2262
  1981
    Uint8 *dst = info->dst;
slouken@2267
  1982
    int dstskip = info->dst_skip;
slouken@2267
  1983
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  1984
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
  1985
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  1986
    int dstbpp = dstfmt->BytesPerPixel;
slouken@2267
  1987
    unsigned sA = info->a;
slouken@1895
  1988
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  1989
slouken@1895
  1990
    if (sA) {
slouken@1895
  1991
        while (height--) {
slouken@1895
  1992
	    /* *INDENT-OFF* */
slouken@0
  1993
	    DUFFS_LOOP4(
slouken@0
  1994
	    {
icculus@1162
  1995
		Uint32 Pixel;
slouken@0
  1996
		unsigned sR;
slouken@0
  1997
		unsigned sG;
slouken@0
  1998
		unsigned sB;
slouken@0
  1999
		unsigned dR;
slouken@0
  2000
		unsigned dG;
slouken@0
  2001
		unsigned dB;
icculus@1162
  2002
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2003
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2004
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2005
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2006
		src += srcbpp;
slouken@0
  2007
		dst += dstbpp;
slouken@0
  2008
	    },
slouken@0
  2009
	    width);
slouken@1895
  2010
	    /* *INDENT-ON* */
slouken@1895
  2011
            src += srcskip;
slouken@1895
  2012
            dst += dstskip;
slouken@1895
  2013
        }
slouken@1895
  2014
    }
slouken@0
  2015
}
slouken@0
  2016
slouken@0
  2017
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@1895
  2018
static void
slouken@1895
  2019
BlitNtoNSurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
  2020
{
slouken@2262
  2021
    int width = info->dst_w;
slouken@2262
  2022
    int height = info->dst_h;
slouken@2262
  2023
    Uint8 *src = info->src;
slouken@2267
  2024
    int srcskip = info->src_skip;
slouken@2262
  2025
    Uint8 *dst = info->dst;
slouken@2267
  2026
    int dstskip = info->dst_skip;
slouken@2267
  2027
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  2028
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@2267
  2029
    Uint32 ckey = info->colorkey;
slouken@1895
  2030
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2031
    int dstbpp = dstfmt->BytesPerPixel;
slouken@2267
  2032
    unsigned sA = info->a;
slouken@1895
  2033
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2034
slouken@1895
  2035
    while (height--) {
slouken@1895
  2036
	    /* *INDENT-OFF* */
slouken@0
  2037
	    DUFFS_LOOP4(
slouken@0
  2038
	    {
icculus@1162
  2039
		Uint32 Pixel;
slouken@0
  2040
		unsigned sR;
slouken@0
  2041
		unsigned sG;
slouken@0
  2042
		unsigned sB;
slouken@0
  2043
		unsigned dR;
slouken@0
  2044
		unsigned dG;
slouken@0
  2045
		unsigned dB;
icculus@1162
  2046
		RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
icculus@1162
  2047
		if(sA && Pixel != ckey) {
icculus@1162
  2048
		    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
icculus@1162
  2049
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2050
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2051
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2052
		}
slouken@0
  2053
		src += srcbpp;
slouken@0
  2054
		dst += dstbpp;
slouken@0
  2055
	    },
slouken@0
  2056
	    width);
slouken@1895
  2057
	    /* *INDENT-ON* */
slouken@1895
  2058
        src += srcskip;
slouken@1895
  2059
        dst += dstskip;
slouken@1895
  2060
    }
slouken@0
  2061
}
slouken@0
  2062
slouken@0
  2063
/* General (slow) N->N blending with pixel alpha */
slouken@1895
  2064
static void
slouken@1895
  2065
BlitNtoNPixelAlpha(SDL_BlitInfo * info)
slouken@0
  2066
{
slouken@2262
  2067
    int width = info->dst_w;
slouken@2262
  2068
    int height = info->dst_h;
slouken@2262
  2069
    Uint8 *src = info->src;
slouken@2267
  2070
    int srcskip = info->src_skip;
slouken@2262
  2071
    Uint8 *dst = info->dst;
slouken@2267
  2072
    int dstskip = info->dst_skip;
slouken@2267
  2073
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  2074
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@0
  2075
slouken@1895
  2076
    int srcbpp;
slouken@1895
  2077
    int dstbpp;
slouken@0
  2078
slouken@1895
  2079
    /* Set up some basic variables */
slouken@1895
  2080
    srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2081
    dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2082
slouken@1895
  2083
    while (height--) {
slouken@1895
  2084
	    /* *INDENT-OFF* */
slouken@0
  2085
	    DUFFS_LOOP4(
slouken@0
  2086
	    {
icculus@1162
  2087
		Uint32 Pixel;
slouken@0
  2088
		unsigned sR;
slouken@0
  2089
		unsigned sG;
slouken@0
  2090
		unsigned sB;
slouken@0
  2091
		unsigned dR;
slouken@0
  2092
		unsigned dG;
slouken@0
  2093
		unsigned dB;
slouken@0
  2094
		unsigned sA;
slouken@0
  2095
		unsigned dA;
icculus@1162
  2096
		DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
slouken@689
  2097
		if(sA) {
icculus@1162
  2098
		  DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
slouken@689
  2099
		  ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@689
  2100
		  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@689
  2101
		}
slouken@0
  2102
		src += srcbpp;
slouken@0
  2103
		dst += dstbpp;
slouken@0
  2104
	    },
slouken@0
  2105
	    width);
slouken@1895
  2106
	    /* *INDENT-ON* */
slouken@1895
  2107
        src += srcskip;
slouken@1895
  2108
        dst += dstskip;
slouken@1895
  2109
    }
slouken@0
  2110
}
slouken@0
  2111
slouken@0
  2112
slouken@2267
  2113
SDL_BlitFunc
slouken@2267
  2114
SDL_CalculateBlitA(SDL_Surface * surface)
slouken@0
  2115
{
slouken@0
  2116
    SDL_PixelFormat *sf = surface->format;
slouken@0
  2117
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
  2118
slouken@2853
  2119
    switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
slouken@2267
  2120
    case SDL_COPY_BLEND:
slouken@1895
  2121
        /* Per-pixel alpha blits */
slouken@1895
  2122
        switch (df->BytesPerPixel) {
slouken@1895
  2123
        case 1:
slouken@1895
  2124
            return BlitNto1PixelAlpha;
slouken@0
  2125
slouken@1895
  2126
        case 2:
slouken@5389
  2127
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2128
            if (sf->BytesPerPixel == 4
slouken@5389
  2129
                && df->Gmask == 0x7e0 && df->Bmask == 0x1f
slouken@5389
  2130
                && SDL_HasAltiVec())
slouken@5389
  2131
                return Blit32to565PixelAlphaAltivec;
slouken@5389
  2132
            else
slouken@5389
  2133
#endif
slouken@5389
  2134
                if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@5389
  2135
                    && sf->Gmask == 0xff00
slouken@5389
  2136
                    && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@5389
  2137
                        || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@1895
  2138
                if (df->Gmask == 0x7e0)
slouken@1895
  2139
                    return BlitARGBto565PixelAlpha;
slouken@1895
  2140
                else if (df->Gmask == 0x3e0)
slouken@1895
  2141
                    return BlitARGBto555PixelAlpha;
slouken@1895
  2142
            }
slouken@1895
  2143
            return BlitNtoNPixelAlpha;
slouken@0
  2144
slouken@1895
  2145
        case 4:
slouken@1895
  2146
            if (sf->Rmask == df->Rmask
slouken@1895
  2147
                && sf->Gmask == df->Gmask
slouken@1895
  2148
                && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@5389
  2149
#if defined(__MMX__) || defined(__3dNOW__)
slouken@1895
  2150
                if (sf->Rshift % 8 == 0
slouken@1895
  2151
                    && sf->Gshift % 8 == 0
slouken@1895
  2152
                    && sf->Bshift % 8 == 0
slouken@1895
  2153
                    && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
slouken@5389
  2154
#ifdef __3dNOW__
slouken@5389
  2155
                    if (SDL_Has3DNow())
slouken@5389
  2156
                        return BlitRGBtoRGBPixelAlphaMMX3DNOW;
slouken@5389
  2157
#endif
slouken@5389
  2158
#ifdef __MMX__
slouken@1895
  2159
                    if (SDL_HasMMX())
slouken@1895
  2160
                        return BlitRGBtoRGBPixelAlphaMMX;
slouken@5389
  2161
#endif
slouken@1895
  2162
                }
slouken@5389
  2163
#endif /* __MMX__ || __3dNOW__ */
slouken@1895
  2164
                if (sf->Amask == 0xff000000) {
slouken@5389
  2165
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2166
                    if (SDL_HasAltiVec())
slouken@5389
  2167
                        return BlitRGBtoRGBPixelAlphaAltivec;
slouken@5389
  2168
#endif
slouken@1895
  2169
                    return BlitRGBtoRGBPixelAlpha;
slouken@1895
  2170
                }
slouken@1895
  2171
            }
slouken@5389
  2172
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2173
            if (sf->Amask && sf->BytesPerPixel == 4 && SDL_HasAltiVec())
slouken@5389
  2174
                return Blit32to32PixelAlphaAltivec;
slouken@5389
  2175
            else
slouken@5389
  2176
#endif
slouken@5389
  2177
                return BlitNtoNPixelAlpha;
slouken@0
  2178
slouken@1895
  2179
        case 3:
slouken@1895
  2180
        default:
slouken@1895
  2181
            return BlitNtoNPixelAlpha;
slouken@1895
  2182
        }
slouken@2267
  2183
        break;
slouken@2267
  2184
slouken@2267
  2185
    case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
slouken@2267
  2186
        if (sf->Amask == 0) {
slouken@2267
  2187
            /* Per-surface alpha blits */
slouken@2267
  2188
            switch (df->BytesPerPixel) {
slouken@2267
  2189
            case 1:
slouken@2267
  2190
                return BlitNto1SurfaceAlpha;
slouken@2267
  2191
slouken@2267
  2192
            case 2:
slouken@2267
  2193
                if (surface->map->identity) {
slouken@2267
  2194
                    if (df->Gmask == 0x7e0) {
slouken@2267
  2195
#ifdef __MMX__
slouken@2267
  2196
                        if (SDL_HasMMX())
slouken@2267
  2197
                            return Blit565to565SurfaceAlphaMMX;
slouken@2267
  2198
                        else
slouken@2267
  2199
#endif
slouken@2267
  2200
                            return Blit565to565SurfaceAlpha;
slouken@2267
  2201
                    } else if (df->Gmask == 0x3e0) {
slouken@2267
  2202
#ifdef __MMX__
slouken@2267
  2203
                        if (SDL_HasMMX())
slouken@2267
  2204
                            return Blit555to555SurfaceAlphaMMX;
slouken@2267
  2205
                        else
slouken@2267
  2206
#endif
slouken@2267
  2207
                            return Blit555to555SurfaceAlpha;
slouken@2267
  2208
                    }
slouken@2267
  2209
                }
slouken@2267
  2210
                return BlitNtoNSurfaceAlpha;
slouken@2267
  2211
slouken@2267
  2212
            case 4:
slouken@2267
  2213
                if (sf->Rmask == df->Rmask
slouken@2267
  2214
                    && sf->Gmask == df->Gmask
slouken@2267
  2215
                    && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@2267
  2216
#ifdef __MMX__
slouken@2267
  2217
                    if (sf->Rshift % 8 == 0
slouken@2267
  2218
                        && sf->Gshift % 8 == 0
slouken@2267
  2219
                        && sf->Bshift % 8 == 0 && SDL_HasMMX())
slouken@2267
  2220
                        return BlitRGBtoRGBSurfaceAlphaMMX;
slouken@2267
  2221
#endif
slouken@2267
  2222
                    if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
slouken@5389
  2223
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2224
                        if (SDL_HasAltiVec())
slouken@5389
  2225
                            return BlitRGBtoRGBSurfaceAlphaAltivec;
slouken@5389
  2226
#endif
slouken@2267
  2227
                        return BlitRGBtoRGBSurfaceAlpha;
slouken@2267
  2228
                    }
slouken@2267
  2229
                }
slouken@5389
  2230
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2231
                if ((sf->BytesPerPixel == 4) && SDL_HasAltiVec())
slouken@5389
  2232
                    return Blit32to32SurfaceAlphaAltivec;
slouken@5389
  2233
                else
slouken@5389
  2234
#endif
slouken@5389
  2235
                    return BlitNtoNSurfaceAlpha;
slouken@2267
  2236
slouken@2267
  2237
            case 3:
slouken@2267
  2238
            default:
slouken@2267
  2239
                return BlitNtoNSurfaceAlpha;
slouken@2267
  2240
            }
slouken@2267
  2241
        }
slouken@2267
  2242
        break;
slouken@2267
  2243
slouken@2267
  2244
    case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
slouken@2267
  2245
        if (sf->Amask == 0) {
slouken@2267
  2246
            if (df->BytesPerPixel == 1)
slouken@2267
  2247
                return BlitNto1SurfaceAlphaKey;
slouken@2267
  2248
            else
slouken@5389
  2249
#if SDL_ALTIVEC_BLITTERS
slouken@5389
  2250
            if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
slouken@5389
  2251
                    SDL_HasAltiVec())
slouken@5389
  2252
                return Blit32to32SurfaceAlphaKeyAltivec;
slouken@5389
  2253
            else
slouken@5389
  2254
#endif
slouken@2267
  2255
                return BlitNtoNSurfaceAlphaKey;
slouken@2267
  2256
        }
slouken@2267
  2257
        break;
slouken@0
  2258
    }
slouken@2267
  2259
slouken@2267
  2260
    return NULL;
slouken@0
  2261
}
slouken@0
  2262
slouken@1895
  2263
/* vi: set ts=4 sw=4 expandtab: */