1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9#include "SkBitmap.h"
10#include "SkColorPriv.h"
11#include "SkBlurImage_opts_SSE2.h"
12#include "SkRect.h"
13
14#include <emmintrin.h>
15
16namespace {
17
18enum BlurDirection {
19    kX, kY
20};
21
22/**
23 * Helper function to spread the components of a 32-bit integer into the
24 * lower 8 bits of each 32-bit element of an SSE register.
25 */
26
27inline __m128i expand(int a) {
28      const __m128i zero = _mm_setzero_si128();
29
30      // 0 0 0 0   0 0 0 0   0 0 0 0   A R G B
31      __m128i result = _mm_cvtsi32_si128(a);
32
33      // 0 0 0 0   0 0 0 0   0 A 0 R   0 G 0 B
34      result = _mm_unpacklo_epi8(result, zero);
35
36      // 0 0 0 A   0 0 0 R   0 0 0 G   0 0 0 B
37      return _mm_unpacklo_epi16(result, zero);
38}
39
40template<BlurDirection srcDirection, BlurDirection dstDirection>
41void SkBoxBlur_SSE2(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize,
42                    int leftOffset, int rightOffset, int width, int height)
43{
44    const int rightBorder = SkMin32(rightOffset + 1, width);
45    const int srcStrideX = srcDirection == kX ? 1 : srcStride;
46    const int dstStrideX = dstDirection == kX ? 1 : height;
47    const int srcStrideY = srcDirection == kX ? srcStride : 1;
48    const int dstStrideY = dstDirection == kX ? width : 1;
49    const __m128i scale = _mm_set1_epi32((1 << 24) / kernelSize);
50    const __m128i half = _mm_set1_epi32(1 << 23);
51    const __m128i zero = _mm_setzero_si128();
52    for (int y = 0; y < height; ++y) {
53        __m128i sum = zero;
54        const SkPMColor* p = src;
55        for (int i = 0; i < rightBorder; ++i) {
56            sum = _mm_add_epi32(sum, expand(*p));
57            p += srcStrideX;
58        }
59
60        const SkPMColor* sptr = src;
61        SkColor* dptr = dst;
62        for (int x = 0; x < width; ++x) {
63#if 0
64            // In SSE4.1, this would be
65            __m128i result = _mm_mullo_epi32(sum, scale);
66#else
67            // But SSE2 has no PMULLUD, so we must do AG and RB separately.
68            __m128i tmp1 = _mm_mul_epu32(sum, scale);
69            __m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(sum, 4),
70                                         _mm_srli_si128(scale, 4));
71            __m128i result = _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0,0,2,0)),
72                                                _mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0,0,2,0)));
73#endif
74            // sumA*scale+.5 sumB*scale+.5 sumG*scale+.5 sumB*scale+.5
75            result = _mm_add_epi32(result, half);
76
77            // 0 0 0 A   0 0 0 R   0 0 0 G   0 0 0 B
78            result = _mm_srli_epi32(result, 24);
79
80            // 0 0 0 0   0 0 0 0   0 A 0 R   0 G 0 B
81            result = _mm_packs_epi32(result, zero);
82
83            // 0 0 0 0   0 0 0 0   0 0 0 0   A R G B
84            result = _mm_packus_epi16(result, zero);
85            *dptr = _mm_cvtsi128_si32(result);
86            if (x >= leftOffset) {
87                SkColor l = *(sptr - leftOffset * srcStrideX);
88                sum = _mm_sub_epi32(sum, expand(l));
89            }
90            if (x + rightOffset + 1 < width) {
91                SkColor r = *(sptr + (rightOffset + 1) * srcStrideX);
92                sum = _mm_add_epi32(sum, expand(r));
93            }
94            sptr += srcStrideX;
95            if (srcDirection == kY) {
96                _mm_prefetch(reinterpret_cast<const char*>(sptr + (rightOffset + 1) * srcStrideX),
97                             _MM_HINT_T0);
98            }
99            dptr += dstStrideX;
100        }
101        src += srcStrideY;
102        dst += dstStrideY;
103    }
104}
105
106} // namespace
107
108bool SkBoxBlurGetPlatformProcs_SSE2(SkBoxBlurProc* boxBlurX,
109                                    SkBoxBlurProc* boxBlurY,
110                                    SkBoxBlurProc* boxBlurXY,
111                                    SkBoxBlurProc* boxBlurYX) {
112    *boxBlurX = SkBoxBlur_SSE2<kX, kX>;
113    *boxBlurY = SkBoxBlur_SSE2<kY, kY>;
114    *boxBlurXY = SkBoxBlur_SSE2<kX, kY>;
115    *boxBlurYX = SkBoxBlur_SSE2<kY, kX>;
116    return true;
117}
118