Home | History | Annotate | Download | only in opts
      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.h"
     12 #include "SkRect.h"
     13 
     14 #include <arm_neon.h>
     15 
     16 namespace {
     17 
     18 enum 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 16-bit element of a NEON register.
     25  */
     26 
     27 static inline uint16x4_t expand(uint32_t a) {
     28     // ( ARGB ) -> ( ARGB ARGB ) -> ( A R G B A R G B )
     29     uint8x8_t v8 = vreinterpret_u8_u32(vdup_n_u32(a));
     30     // ( A R G B A R G B ) -> ( 0A 0R 0G 0B 0A 0R 0G 0B ) -> ( 0A 0R 0G 0B )
     31     return vget_low_u16(vmovl_u8(v8));
     32 }
     33 
     34 template<BlurDirection srcDirection, BlurDirection dstDirection>
     35 void SkBoxBlur_NEON(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize,
     36                     int leftOffset, int rightOffset, int width, int height)
     37 {
     38     const int rightBorder = SkMin32(rightOffset + 1, width);
     39     const int srcStrideX = srcDirection == kX ? 1 : srcStride;
     40     const int dstStrideX = dstDirection == kX ? 1 : height;
     41     const int srcStrideY = srcDirection == kX ? srcStride : 1;
     42     const int dstStrideY = dstDirection == kX ? width : 1;
     43     const uint32x4_t scale = vdupq_n_u32((1 << 24) / kernelSize);
     44     const uint32x4_t half = vdupq_n_u32(1 << 23);
     45     for (int y = 0; y < height; ++y) {
     46         uint32x4_t sum = vdupq_n_u32(0);
     47         const SkPMColor* p = src;
     48         for (int i = 0; i < rightBorder; ++i) {
     49             sum = vaddw_u16(sum, expand(*p));
     50             p += srcStrideX;
     51         }
     52 
     53         const SkPMColor* sptr = src;
     54         SkPMColor* dptr = dst;
     55         for (int x = 0; x < width; ++x) {
     56             // ( half+sumA*scale half+sumR*scale half+sumG*scale half+sumB*scale )
     57             uint32x4_t result = vmlaq_u32(half, sum, scale);
     58 
     59             // Saturated conversion to 16-bit.
     60             // ( AAAA RRRR GGGG BBBB ) -> ( 0A 0R 0G 0B )
     61             uint16x4_t result16 = vqshrn_n_u32(result, 16);
     62 
     63             // Saturated conversion to 8-bit.
     64             // ( 0A 0R 0G 0B ) -> ( 0A 0R 0G 0B 0A 0R 0G 0B ) -> ( A R G B A R G B )
     65             uint8x8_t result8 = vqshrn_n_u16(vcombine_u16(result16, result16), 8);
     66 
     67             // ( A R G B A R G B ) -> ( ARGB ARGB ) -> ( ARGB )
     68             // Store low 32 bits to destination.
     69             vst1_lane_u32(dptr, vreinterpret_u32_u8(result8), 0);
     70 
     71             if (x >= leftOffset) {
     72                 const SkPMColor* l = sptr - leftOffset * srcStrideX;
     73                 sum = vsubw_u16(sum, expand(*l));
     74             }
     75             if (x + rightOffset + 1 < width) {
     76                 const SkPMColor* r = sptr + (rightOffset + 1) * srcStrideX;
     77                 sum = vaddw_u16(sum, expand(*r));
     78             }
     79             sptr += srcStrideX;
     80             if (srcDirection == kY) {
     81                 SK_PREFETCH(sptr + (rightOffset + 1) * srcStrideX);
     82             }
     83             dptr += dstStrideX;
     84         }
     85         src += srcStrideY;
     86         dst += dstStrideY;
     87     }
     88 }
     89 
     90 } // namespace
     91 
     92 bool SkBoxBlurGetPlatformProcs_NEON(SkBoxBlurProc* boxBlurX,
     93                                     SkBoxBlurProc* boxBlurY,
     94                                     SkBoxBlurProc* boxBlurXY,
     95                                     SkBoxBlurProc* boxBlurYX) {
     96     *boxBlurX = SkBoxBlur_NEON<kX, kX>;
     97     *boxBlurY = SkBoxBlur_NEON<kY, kY>;
     98     *boxBlurXY = SkBoxBlur_NEON<kX, kY>;
     99     *boxBlurYX = SkBoxBlur_NEON<kY, kX>;
    100     return true;
    101 }
    102