2 * Copyright 2014 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurImage_opts_SSE4.h"
10 #include "SkColorPriv.h"
13 /* With the exception of the compilers that don't support it, we always build the
14 * SSE4 functions and enable the caller to determine SSE4 support. However for
15 * compilers that do not support SSE4x we provide a stub implementation.
17 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
19 #include <smmintrin.h>
26 /* Helper function to spread the components of a 32-bit integer into the
27 * lower 8 bits of each 32-bit element of an SSE register.
29 inline __m128i expand(int a) {
30 const __m128i zero = _mm_setzero_si128();
32 // 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
33 __m128i result = _mm_cvtsi32_si128(a);
35 // 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
36 result = _mm_unpacklo_epi8(result, zero);
38 // 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
39 return _mm_unpacklo_epi16(result, zero);
42 template<BlurDirection srcDirection, BlurDirection dstDirection>
43 void SkBoxBlur_SSE4(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize,
44 int leftOffset, int rightOffset, int width, int height)
46 const int rightBorder = SkMin32(rightOffset + 1, width);
47 const int srcStrideX = srcDirection == kX ? 1 : srcStride;
48 const int dstStrideX = dstDirection == kX ? 1 : height;
49 const int srcStrideY = srcDirection == kX ? srcStride : 1;
50 const int dstStrideY = dstDirection == kX ? width : 1;
51 const __m128i scale = _mm_set1_epi32((1 << 24) / kernelSize);
52 const __m128i half = _mm_set1_epi32(1 << 23);
53 const __m128i zero = _mm_setzero_si128();
54 for (int y = 0; y < height; ++y) {
56 const SkPMColor* p = src;
57 for (int i = 0; i < rightBorder; ++i) {
58 sum = _mm_add_epi32(sum, expand(*p));
62 const SkPMColor* sptr = src;
64 for (int x = 0; x < width; ++x) {
65 __m128i result = _mm_mullo_epi32(sum, scale);
67 // sumA*scale+.5 sumB*scale+.5 sumG*scale+.5 sumB*scale+.5
68 result = _mm_add_epi32(result, half);
70 // 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
71 result = _mm_srli_epi32(result, 24);
73 // 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
74 result = _mm_packs_epi32(result, zero);
76 // 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
77 result = _mm_packus_epi16(result, zero);
78 *dptr = _mm_cvtsi128_si32(result);
79 if (x >= leftOffset) {
80 SkColor l = *(sptr - leftOffset * srcStrideX);
81 sum = _mm_sub_epi32(sum, expand(l));
83 if (x + rightOffset + 1 < width) {
84 SkColor r = *(sptr + (rightOffset + 1) * srcStrideX);
85 sum = _mm_add_epi32(sum, expand(r));
88 if (srcDirection == kY) {
89 _mm_prefetch(reinterpret_cast<const char*>(sptr + (rightOffset + 1) * srcStrideX),
101 bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
102 SkBoxBlurProc* boxBlurY,
103 SkBoxBlurProc* boxBlurXY,
104 SkBoxBlurProc* boxBlurYX) {
105 *boxBlurX = SkBoxBlur_SSE4<kX, kX>;
106 *boxBlurY = SkBoxBlur_SSE4<kY, kY>;
107 *boxBlurXY = SkBoxBlur_SSE4<kX, kY>;
108 *boxBlurYX = SkBoxBlur_SSE4<kY, kX>;
112 #else // SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
114 bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
115 SkBoxBlurProc* boxBlurY,
116 SkBoxBlurProc* boxBlurXY,
117 SkBoxBlurProc* boxBlurYX) {