blob: 1b990387c2ae707396459ba6e767eef32ac27438 [file] [log] [blame]
henrik.smiding5f7f9d02014-07-07 08:05:40 -07001/*
2 * Copyright 2014 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#include "SkBitmap.h"
9#include "SkBlurImage_opts_SSE4.h"
10#include "SkColorPriv.h"
11#include "SkRect.h"
12
bungeman2da0f852014-07-23 11:28:18 -070013/* With the exception of the Android framework we always build the SSE4 functions
14 * and enable the caller to determine SSE4 support. However, for the Android framework,
15 * if the device does not support SSE4x then the compiler will not supply the required
16 * -msse4* option needed to build this file, so instead we provide a stub implementation.
henrik.smiding5f7f9d02014-07-07 08:05:40 -070017 */
bungeman2da0f852014-07-23 11:28:18 -070018#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
henrik.smiding5f7f9d02014-07-07 08:05:40 -070019
20#include <smmintrin.h>
21
22namespace {
23enum BlurDirection {
24 kX, kY
25};
26
27/* Helper function to spread the components of a 32-bit integer into the
28 * lower 8 bits of each 32-bit element of an SSE register.
29 */
30inline __m128i expand(int a) {
31 const __m128i zero = _mm_setzero_si128();
32
33 // 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
34 __m128i result = _mm_cvtsi32_si128(a);
35
36 // 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
37 result = _mm_unpacklo_epi8(result, zero);
38
39 // 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
40 return _mm_unpacklo_epi16(result, zero);
41}
42
43template<BlurDirection srcDirection, BlurDirection dstDirection>
44void SkBoxBlur_SSE4(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize,
45 int leftOffset, int rightOffset, int width, int height)
46{
47 const int rightBorder = SkMin32(rightOffset + 1, width);
48 const int srcStrideX = srcDirection == kX ? 1 : srcStride;
49 const int dstStrideX = dstDirection == kX ? 1 : height;
50 const int srcStrideY = srcDirection == kX ? srcStride : 1;
51 const int dstStrideY = dstDirection == kX ? width : 1;
52 const __m128i scale = _mm_set1_epi32((1 << 24) / kernelSize);
53 const __m128i half = _mm_set1_epi32(1 << 23);
54 const __m128i zero = _mm_setzero_si128();
55 for (int y = 0; y < height; ++y) {
56 __m128i sum = zero;
57 const SkPMColor* p = src;
58 for (int i = 0; i < rightBorder; ++i) {
59 sum = _mm_add_epi32(sum, expand(*p));
60 p += srcStrideX;
61 }
62
63 const SkPMColor* sptr = src;
64 SkColor* dptr = dst;
65 for (int x = 0; x < width; ++x) {
66 __m128i result = _mm_mullo_epi32(sum, scale);
67
68 // sumA*scale+.5 sumB*scale+.5 sumG*scale+.5 sumB*scale+.5
69 result = _mm_add_epi32(result, half);
70
71 // 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
72 result = _mm_srli_epi32(result, 24);
73
74 // 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
75 result = _mm_packs_epi32(result, zero);
76
77 // 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
78 result = _mm_packus_epi16(result, zero);
79 *dptr = _mm_cvtsi128_si32(result);
80 if (x >= leftOffset) {
81 SkColor l = *(sptr - leftOffset * srcStrideX);
82 sum = _mm_sub_epi32(sum, expand(l));
83 }
84 if (x + rightOffset + 1 < width) {
85 SkColor r = *(sptr + (rightOffset + 1) * srcStrideX);
86 sum = _mm_add_epi32(sum, expand(r));
87 }
88 sptr += srcStrideX;
89 if (srcDirection == kY) {
90 _mm_prefetch(reinterpret_cast<const char*>(sptr + (rightOffset + 1) * srcStrideX),
91 _MM_HINT_T0);
92 }
93 dptr += dstStrideX;
94 }
95 src += srcStrideY;
96 dst += dstStrideY;
97 }
98}
99
100} // namespace
101
102bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
103 SkBoxBlurProc* boxBlurY,
104 SkBoxBlurProc* boxBlurXY,
105 SkBoxBlurProc* boxBlurYX) {
106 *boxBlurX = SkBoxBlur_SSE4<kX, kX>;
107 *boxBlurY = SkBoxBlur_SSE4<kY, kY>;
108 *boxBlurXY = SkBoxBlur_SSE4<kX, kY>;
109 *boxBlurYX = SkBoxBlur_SSE4<kY, kX>;
110 return true;
111}
112
bungeman2da0f852014-07-23 11:28:18 -0700113#else // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
henrik.smiding5f7f9d02014-07-07 08:05:40 -0700114
115bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
116 SkBoxBlurProc* boxBlurY,
117 SkBoxBlurProc* boxBlurXY,
118 SkBoxBlurProc* boxBlurYX) {
119 sk_throw();
scroggof05d6262014-07-07 11:41:39 -0700120 return false;
henrik.smiding5f7f9d02014-07-07 08:05:40 -0700121}
122
123
124#endif