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