Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | |
michael@0 | 9 | #include "SkBitmap.h" |
michael@0 | 10 | #include "SkColorPriv.h" |
michael@0 | 11 | #include "SkBlurImage_opts.h" |
michael@0 | 12 | #include "SkRect.h" |
michael@0 | 13 | |
michael@0 | 14 | #include <arm_neon.h> |
michael@0 | 15 | |
michael@0 | 16 | namespace { |
michael@0 | 17 | |
michael@0 | 18 | enum BlurDirection { |
michael@0 | 19 | kX, kY |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * Helper function to load 2 pixels from diffent rows to a 8x8 NEON register |
michael@0 | 24 | * and also pre-load pixels for future read |
michael@0 | 25 | */ |
michael@0 | 26 | template<BlurDirection srcDirection> |
michael@0 | 27 | inline uint8x8_t load_2_pixels(const SkPMColor* src, int srcStride) { |
michael@0 | 28 | if (srcDirection == kX) { |
michael@0 | 29 | uint32x2_t temp = vdup_n_u32(0); |
michael@0 | 30 | // 10% faster by adding these 2 prefetches |
michael@0 | 31 | SK_PREFETCH(src + 16); |
michael@0 | 32 | SK_PREFETCH(src + srcStride + 16); |
michael@0 | 33 | return vreinterpret_u8_u32(vld1_lane_u32(src + srcStride, vld1_lane_u32(src, temp, 0), 1)); |
michael@0 | 34 | } else { |
michael@0 | 35 | return vld1_u8((uint8_t*)src); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Helper function to store the low 8-bits from a 16x8 NEON register to 2 rows |
michael@0 | 41 | */ |
michael@0 | 42 | template<BlurDirection dstDirection> |
michael@0 | 43 | inline void store_2_pixels(uint16x8_t result16x8, SkPMColor* dst, int dstStride) { |
michael@0 | 44 | if (dstDirection == kX) { |
michael@0 | 45 | uint32x2_t temp = vreinterpret_u32_u8(vmovn_u16(result16x8)); |
michael@0 | 46 | vst1_lane_u32(dst, temp, 0); |
michael@0 | 47 | vst1_lane_u32(dst + dstStride, temp, 1); |
michael@0 | 48 | } else { |
michael@0 | 49 | uint8x8_t temp = vmovn_u16(result16x8); |
michael@0 | 50 | vst1_u8((uint8_t*)dst, temp); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * fast path for kernel size less than 128 |
michael@0 | 56 | */ |
michael@0 | 57 | template<BlurDirection srcDirection, BlurDirection dstDirection> |
michael@0 | 58 | void SkDoubleRowBoxBlur_NEON(const SkPMColor** src, int srcStride, SkPMColor** dst, int kernelSize, |
michael@0 | 59 | int leftOffset, int rightOffset, int width, int* height) |
michael@0 | 60 | { |
michael@0 | 61 | const int rightBorder = SkMin32(rightOffset + 1, width); |
michael@0 | 62 | const int srcStrideX = srcDirection == kX ? 1 : srcStride; |
michael@0 | 63 | const int dstStrideX = dstDirection == kX ? 1 : *height; |
michael@0 | 64 | const int srcStrideY = srcDirection == kX ? srcStride : 1; |
michael@0 | 65 | const int dstStrideY = dstDirection == kX ? width : 1; |
michael@0 | 66 | const uint16x8_t scale = vdupq_n_u16((1 << 15) / kernelSize); |
michael@0 | 67 | |
michael@0 | 68 | for (; *height >= 2; *height -= 2) { |
michael@0 | 69 | uint16x8_t sum = vdupq_n_u16(0); |
michael@0 | 70 | const SkPMColor* p = *src; |
michael@0 | 71 | for (int i = 0; i < rightBorder; i++) { |
michael@0 | 72 | sum = vaddw_u8(sum, |
michael@0 | 73 | load_2_pixels<srcDirection>(p, srcStride)); |
michael@0 | 74 | p += srcStrideX; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | const SkPMColor* sptr = *src; |
michael@0 | 78 | SkPMColor* dptr = *dst; |
michael@0 | 79 | for (int x = 0; x < width; x++) { |
michael@0 | 80 | // val = (sum * scale * 2 + 0x8000) >> 16 |
michael@0 | 81 | uint16x8_t resultPixels = vreinterpretq_u16_s16(vqrdmulhq_s16( |
michael@0 | 82 | vreinterpretq_s16_u16(sum), vreinterpretq_s16_u16(scale))); |
michael@0 | 83 | store_2_pixels<dstDirection>(resultPixels, dptr, width); |
michael@0 | 84 | |
michael@0 | 85 | if (x >= leftOffset) { |
michael@0 | 86 | sum = vsubw_u8(sum, |
michael@0 | 87 | load_2_pixels<srcDirection>(sptr - leftOffset * srcStrideX, srcStride)); |
michael@0 | 88 | } |
michael@0 | 89 | if (x + rightOffset + 1 < width) { |
michael@0 | 90 | sum = vaddw_u8(sum, |
michael@0 | 91 | load_2_pixels<srcDirection>(sptr + (rightOffset + 1) * srcStrideX, srcStride)); |
michael@0 | 92 | } |
michael@0 | 93 | sptr += srcStrideX; |
michael@0 | 94 | dptr += dstStrideX; |
michael@0 | 95 | } |
michael@0 | 96 | *src += srcStrideY * 2; |
michael@0 | 97 | *dst += dstStrideY * 2; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Helper function to spread the components of a 32-bit integer into the |
michael@0 | 104 | * lower 8 bits of each 16-bit element of a NEON register. |
michael@0 | 105 | */ |
michael@0 | 106 | |
michael@0 | 107 | static inline uint16x4_t expand(uint32_t a) { |
michael@0 | 108 | // ( ARGB ) -> ( ARGB ARGB ) -> ( A R G B A R G B ) |
michael@0 | 109 | uint8x8_t v8 = vreinterpret_u8_u32(vdup_n_u32(a)); |
michael@0 | 110 | // ( A R G B A R G B ) -> ( 0A 0R 0G 0B 0A 0R 0G 0B ) -> ( 0A 0R 0G 0B ) |
michael@0 | 111 | return vget_low_u16(vmovl_u8(v8)); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | template<BlurDirection srcDirection, BlurDirection dstDirection> |
michael@0 | 115 | void SkBoxBlur_NEON(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize, |
michael@0 | 116 | int leftOffset, int rightOffset, int width, int height) |
michael@0 | 117 | { |
michael@0 | 118 | const int rightBorder = SkMin32(rightOffset + 1, width); |
michael@0 | 119 | const int srcStrideX = srcDirection == kX ? 1 : srcStride; |
michael@0 | 120 | const int dstStrideX = dstDirection == kX ? 1 : height; |
michael@0 | 121 | const int srcStrideY = srcDirection == kX ? srcStride : 1; |
michael@0 | 122 | const int dstStrideY = dstDirection == kX ? width : 1; |
michael@0 | 123 | const uint32x4_t scale = vdupq_n_u32((1 << 24) / kernelSize); |
michael@0 | 124 | const uint32x4_t half = vdupq_n_u32(1 << 23); |
michael@0 | 125 | |
michael@0 | 126 | if (kernelSize < 128) |
michael@0 | 127 | { |
michael@0 | 128 | SkDoubleRowBoxBlur_NEON<srcDirection, dstDirection>(&src, srcStride, &dst, kernelSize, |
michael@0 | 129 | leftOffset, rightOffset, width, &height); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | for (; height > 0; height--) { |
michael@0 | 133 | uint32x4_t sum = vdupq_n_u32(0); |
michael@0 | 134 | const SkPMColor* p = src; |
michael@0 | 135 | for (int i = 0; i < rightBorder; ++i) { |
michael@0 | 136 | sum = vaddw_u16(sum, expand(*p)); |
michael@0 | 137 | p += srcStrideX; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | const SkPMColor* sptr = src; |
michael@0 | 141 | SkPMColor* dptr = dst; |
michael@0 | 142 | for (int x = 0; x < width; ++x) { |
michael@0 | 143 | // ( half+sumA*scale half+sumR*scale half+sumG*scale half+sumB*scale ) |
michael@0 | 144 | uint32x4_t result = vmlaq_u32(half, sum, scale); |
michael@0 | 145 | |
michael@0 | 146 | // Saturated conversion to 16-bit. |
michael@0 | 147 | // ( AAAA RRRR GGGG BBBB ) -> ( 0A 0R 0G 0B ) |
michael@0 | 148 | uint16x4_t result16 = vqshrn_n_u32(result, 16); |
michael@0 | 149 | |
michael@0 | 150 | // Saturated conversion to 8-bit. |
michael@0 | 151 | // ( 0A 0R 0G 0B ) -> ( 0A 0R 0G 0B 0A 0R 0G 0B ) -> ( A R G B A R G B ) |
michael@0 | 152 | uint8x8_t result8 = vqshrn_n_u16(vcombine_u16(result16, result16), 8); |
michael@0 | 153 | |
michael@0 | 154 | // ( A R G B A R G B ) -> ( ARGB ARGB ) -> ( ARGB ) |
michael@0 | 155 | // Store low 32 bits to destination. |
michael@0 | 156 | vst1_lane_u32(dptr, vreinterpret_u32_u8(result8), 0); |
michael@0 | 157 | |
michael@0 | 158 | if (x >= leftOffset) { |
michael@0 | 159 | const SkPMColor* l = sptr - leftOffset * srcStrideX; |
michael@0 | 160 | sum = vsubw_u16(sum, expand(*l)); |
michael@0 | 161 | } |
michael@0 | 162 | if (x + rightOffset + 1 < width) { |
michael@0 | 163 | const SkPMColor* r = sptr + (rightOffset + 1) * srcStrideX; |
michael@0 | 164 | sum = vaddw_u16(sum, expand(*r)); |
michael@0 | 165 | } |
michael@0 | 166 | sptr += srcStrideX; |
michael@0 | 167 | if (srcDirection == kX) { |
michael@0 | 168 | SK_PREFETCH(sptr + (rightOffset + 16) * srcStrideX); |
michael@0 | 169 | } |
michael@0 | 170 | dptr += dstStrideX; |
michael@0 | 171 | } |
michael@0 | 172 | src += srcStrideY; |
michael@0 | 173 | dst += dstStrideY; |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | } // namespace |
michael@0 | 178 | |
michael@0 | 179 | bool SkBoxBlurGetPlatformProcs_NEON(SkBoxBlurProc* boxBlurX, |
michael@0 | 180 | SkBoxBlurProc* boxBlurY, |
michael@0 | 181 | SkBoxBlurProc* boxBlurXY, |
michael@0 | 182 | SkBoxBlurProc* boxBlurYX) { |
michael@0 | 183 | *boxBlurX = SkBoxBlur_NEON<kX, kX>; |
michael@0 | 184 | *boxBlurY = SkBoxBlur_NEON<kY, kY>; |
michael@0 | 185 | *boxBlurXY = SkBoxBlur_NEON<kX, kY>; |
michael@0 | 186 | *boxBlurYX = SkBoxBlur_NEON<kY, kX>; |
michael@0 | 187 | return true; |
michael@0 | 188 | } |