Home | History | Annotate | Download | only in opts
      1 /*
      2  * Copyright 2009 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 "SkBitmapScaler.h"
     10 #include "SkBitmapProcState.h"
     11 #include "SkColorPriv.h"
     12 #include "SkPaint.h"
     13 #include "SkTypes.h"
     14 #include "SkUtils.h"
     15 #include "SkUtilsArm.h"
     16 
     17 #include "SkConvolver.h"
     18 
     19 #if !defined(SK_CPU_ARM64) && SK_ARM_ARCH >= 6 && !defined(SK_CPU_BENDIAN)
     20 void SI8_D16_nofilter_DX_arm(
     21     const SkBitmapProcState& s,
     22     const uint32_t* SK_RESTRICT xy,
     23     int count,
     24     uint16_t* SK_RESTRICT colors) SK_ATTRIBUTE_OPTIMIZE_O1;
     25 
     26 void SI8_D16_nofilter_DX_arm(const SkBitmapProcState& s,
     27                              const uint32_t* SK_RESTRICT xy,
     28                              int count, uint16_t* SK_RESTRICT colors) {
     29     SkASSERT(count > 0 && colors != NULL);
     30     SkASSERT(s.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
     31     SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel);
     32 
     33     const uint16_t* SK_RESTRICT table = s.fBitmap->getColorTable()->lock16BitCache();
     34     const uint8_t* SK_RESTRICT srcAddr = (const uint8_t*)s.fBitmap->getPixels();
     35 
     36     // buffer is y32, x16, x16, x16, x16, x16
     37     // bump srcAddr to the proper row, since we're told Y never changes
     38     SkASSERT((unsigned)xy[0] < (unsigned)s.fBitmap->height());
     39     srcAddr = (const uint8_t*)((const char*)srcAddr +
     40                                xy[0] * s.fBitmap->rowBytes());
     41 
     42     uint8_t src;
     43 
     44     if (1 == s.fBitmap->width()) {
     45         src = srcAddr[0];
     46         uint16_t dstValue = table[src];
     47         sk_memset16(colors, dstValue, count);
     48     } else {
     49         int i;
     50         int count8 = count >> 3;
     51         const uint16_t* SK_RESTRICT xx = (const uint16_t*)(xy + 1);
     52 
     53         asm volatile (
     54             "cmp        %[count8], #0                  \n\t"  // compare loop counter with 0
     55             "beq        2f                             \n\t"  // if loop counter == 0, exit
     56             "1:                                        \n\t"
     57             "ldmia      %[xx]!, {r5, r7, r9, r11}      \n\t"  // load ptrs to pixels 0-7
     58             "subs       %[count8], %[count8], #1       \n\t"  // decrement loop counter
     59             "uxth       r4, r5                         \n\t"  // extract ptr 0
     60             "mov        r5, r5, lsr #16                \n\t"  // extract ptr 1
     61             "uxth       r6, r7                         \n\t"  // extract ptr 2
     62             "mov        r7, r7, lsr #16                \n\t"  // extract ptr 3
     63             "ldrb       r4, [%[srcAddr], r4]           \n\t"  // load pixel 0 from image
     64             "uxth       r8, r9                         \n\t"  // extract ptr 4
     65             "ldrb       r5, [%[srcAddr], r5]           \n\t"  // load pixel 1 from image
     66             "mov        r9, r9, lsr #16                \n\t"  // extract ptr 5
     67             "ldrb       r6, [%[srcAddr], r6]           \n\t"  // load pixel 2 from image
     68             "uxth       r10, r11                       \n\t"  // extract ptr 6
     69             "ldrb       r7, [%[srcAddr], r7]           \n\t"  // load pixel 3 from image
     70             "mov        r11, r11, lsr #16              \n\t"  // extract ptr 7
     71             "ldrb       r8, [%[srcAddr], r8]           \n\t"  // load pixel 4 from image
     72             "add        r4, r4, r4                     \n\t"  // double pixel 0 for RGB565 lookup
     73             "ldrb       r9, [%[srcAddr], r9]           \n\t"  // load pixel 5 from image
     74             "add        r5, r5, r5                     \n\t"  // double pixel 1 for RGB565 lookup
     75             "ldrb       r10, [%[srcAddr], r10]         \n\t"  // load pixel 6 from image
     76             "add        r6, r6, r6                     \n\t"  // double pixel 2 for RGB565 lookup
     77             "ldrb       r11, [%[srcAddr], r11]         \n\t"  // load pixel 7 from image
     78             "add        r7, r7, r7                     \n\t"  // double pixel 3 for RGB565 lookup
     79             "ldrh       r4, [%[table], r4]             \n\t"  // load pixel 0 RGB565 from colmap
     80             "add        r8, r8, r8                     \n\t"  // double pixel 4 for RGB565 lookup
     81             "ldrh       r5, [%[table], r5]             \n\t"  // load pixel 1 RGB565 from colmap
     82             "add        r9, r9, r9                     \n\t"  // double pixel 5 for RGB565 lookup
     83             "ldrh       r6, [%[table], r6]             \n\t"  // load pixel 2 RGB565 from colmap
     84             "add        r10, r10, r10                  \n\t"  // double pixel 6 for RGB565 lookup
     85             "ldrh       r7, [%[table], r7]             \n\t"  // load pixel 3 RGB565 from colmap
     86             "add        r11, r11, r11                  \n\t"  // double pixel 7 for RGB565 lookup
     87             "ldrh       r8, [%[table], r8]             \n\t"  // load pixel 4 RGB565 from colmap
     88             "ldrh       r9, [%[table], r9]             \n\t"  // load pixel 5 RGB565 from colmap
     89             "ldrh       r10, [%[table], r10]           \n\t"  // load pixel 6 RGB565 from colmap
     90             "ldrh       r11, [%[table], r11]           \n\t"  // load pixel 7 RGB565 from colmap
     91             "pkhbt      r5, r4, r5, lsl #16            \n\t"  // pack pixels 0 and 1
     92             "pkhbt      r6, r6, r7, lsl #16            \n\t"  // pack pixels 2 and 3
     93             "pkhbt      r8, r8, r9, lsl #16            \n\t"  // pack pixels 4 and 5
     94             "pkhbt      r10, r10, r11, lsl #16         \n\t"  // pack pixels 6 and 7
     95             "stmia      %[colors]!, {r5, r6, r8, r10}  \n\t"  // store last 8 pixels
     96             "bgt        1b                             \n\t"  // loop if counter > 0
     97             "2:                                        \n\t"
     98             : [xx] "+r" (xx), [count8] "+r" (count8), [colors] "+r" (colors)
     99             : [table] "r" (table), [srcAddr] "r" (srcAddr)
    100             : "memory", "cc", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11"
    101         );
    102 
    103         for (i = (count & 7); i > 0; --i) {
    104             src = srcAddr[*xx++]; *colors++ = table[src];
    105         }
    106     }
    107 
    108     s.fBitmap->getColorTable()->unlock16BitCache();
    109 }
    110 
    111 void SI8_opaque_D32_nofilter_DX_arm(
    112     const SkBitmapProcState& s,
    113     const uint32_t* SK_RESTRICT xy,
    114     int count,
    115     SkPMColor* SK_RESTRICT colors) SK_ATTRIBUTE_OPTIMIZE_O1;
    116 
    117 void SI8_opaque_D32_nofilter_DX_arm(const SkBitmapProcState& s,
    118                                     const uint32_t* SK_RESTRICT xy,
    119                                     int count, SkPMColor* SK_RESTRICT colors) {
    120     SkASSERT(count > 0 && colors != NULL);
    121     SkASSERT(s.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
    122     SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel);
    123 
    124     const SkPMColor* SK_RESTRICT table = s.fBitmap->getColorTable()->lockColors();
    125     const uint8_t* SK_RESTRICT srcAddr = (const uint8_t*)s.fBitmap->getPixels();
    126 
    127     // buffer is y32, x16, x16, x16, x16, x16
    128     // bump srcAddr to the proper row, since we're told Y never changes
    129     SkASSERT((unsigned)xy[0] < (unsigned)s.fBitmap->height());
    130     srcAddr = (const uint8_t*)((const char*)srcAddr + xy[0] * s.fBitmap->rowBytes());
    131 
    132     if (1 == s.fBitmap->width()) {
    133         uint8_t src = srcAddr[0];
    134         SkPMColor dstValue = table[src];
    135         sk_memset32(colors, dstValue, count);
    136     } else {
    137         const uint16_t* xx = (const uint16_t*)(xy + 1);
    138 
    139         asm volatile (
    140             "subs       %[count], %[count], #8        \n\t"   // decrement count by 8, set flags
    141             "blt        2f                            \n\t"   // if count < 0, branch to singles
    142             "1:                                       \n\t"   // eights loop
    143             "ldmia      %[xx]!, {r5, r7, r9, r11}     \n\t"   // load ptrs to pixels 0-7
    144             "uxth       r4, r5                        \n\t"   // extract ptr 0
    145             "mov        r5, r5, lsr #16               \n\t"   // extract ptr 1
    146             "uxth       r6, r7                        \n\t"   // extract ptr 2
    147             "mov        r7, r7, lsr #16               \n\t"   // extract ptr 3
    148             "ldrb       r4, [%[srcAddr], r4]          \n\t"   // load pixel 0 from image
    149             "uxth       r8, r9                        \n\t"   // extract ptr 4
    150             "ldrb       r5, [%[srcAddr], r5]          \n\t"   // load pixel 1 from image
    151             "mov        r9, r9, lsr #16               \n\t"   // extract ptr 5
    152             "ldrb       r6, [%[srcAddr], r6]          \n\t"   // load pixel 2 from image
    153             "uxth       r10, r11                      \n\t"   // extract ptr 6
    154             "ldrb       r7, [%[srcAddr], r7]          \n\t"   // load pixel 3 from image
    155             "mov        r11, r11, lsr #16             \n\t"   // extract ptr 7
    156             "ldrb       r8, [%[srcAddr], r8]          \n\t"   // load pixel 4 from image
    157             "ldrb       r9, [%[srcAddr], r9]          \n\t"   // load pixel 5 from image
    158             "ldrb       r10, [%[srcAddr], r10]        \n\t"   // load pixel 6 from image
    159             "ldrb       r11, [%[srcAddr], r11]        \n\t"   // load pixel 7 from image
    160             "ldr        r4, [%[table], r4, lsl #2]    \n\t"   // load pixel 0 SkPMColor from colmap
    161             "ldr        r5, [%[table], r5, lsl #2]    \n\t"   // load pixel 1 SkPMColor from colmap
    162             "ldr        r6, [%[table], r6, lsl #2]    \n\t"   // load pixel 2 SkPMColor from colmap
    163             "ldr        r7, [%[table], r7, lsl #2]    \n\t"   // load pixel 3 SkPMColor from colmap
    164             "ldr        r8, [%[table], r8, lsl #2]    \n\t"   // load pixel 4 SkPMColor from colmap
    165             "ldr        r9, [%[table], r9, lsl #2]    \n\t"   // load pixel 5 SkPMColor from colmap
    166             "ldr        r10, [%[table], r10, lsl #2]  \n\t"   // load pixel 6 SkPMColor from colmap
    167             "ldr        r11, [%[table], r11, lsl #2]  \n\t"   // load pixel 7 SkPMColor from colmap
    168             "subs       %[count], %[count], #8        \n\t"   // decrement loop counter
    169             "stmia      %[colors]!, {r4-r11}          \n\t"   // store 8 pixels
    170             "bge        1b                            \n\t"   // loop if counter >= 0
    171             "2:                                       \n\t"
    172             "adds       %[count], %[count], #8        \n\t"   // fix up counter, set flags
    173             "beq        4f                            \n\t"   // if count == 0, branch to exit
    174             "3:                                       \n\t"   // singles loop
    175             "ldrh       r4, [%[xx]], #2               \n\t"   // load pixel ptr
    176             "subs       %[count], %[count], #1        \n\t"   // decrement loop counter
    177             "ldrb       r5, [%[srcAddr], r4]          \n\t"   // load pixel from image
    178             "ldr        r6, [%[table], r5, lsl #2]    \n\t"   // load SkPMColor from colmap
    179             "str        r6, [%[colors]], #4           \n\t"   // store pixel, update ptr
    180             "bne        3b                            \n\t"   // loop if counter != 0
    181             "4:                                       \n\t"   // exit
    182             : [xx] "+r" (xx), [count] "+r" (count), [colors] "+r" (colors)
    183             : [table] "r" (table), [srcAddr] "r" (srcAddr)
    184             : "memory", "cc", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11"
    185         );
    186     }
    187 
    188     s.fBitmap->getColorTable()->unlockColors();
    189 }
    190 #endif // !defined(SK_CPU_ARM64) && SK_ARM_ARCH >= 6 && !defined(SK_CPU_BENDIAN)
    191 
    192 ///////////////////////////////////////////////////////////////////////////////
    193 
    194 /*  If we replace a sampleproc, then we null-out the associated shaderproc,
    195     otherwise the shader won't even look at the matrix/sampler
    196  */
    197 void SkBitmapProcState::platformProcs() {
    198 #if !defined(SK_CPU_ARM64) && SK_ARM_ARCH >= 6 && !defined(SK_CPU_BENDIAN)
    199     bool isOpaque = 256 == fAlphaScale;
    200     bool justDx = false;
    201 
    202     if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
    203         justDx = true;
    204     }
    205 
    206     switch (fBitmap->colorType()) {
    207         case kIndex_8_SkColorType:
    208             if (justDx && SkPaint::kNone_FilterLevel == fFilterLevel) {
    209 #if 0   /* crashing on android device */
    210                 fSampleProc16 = SI8_D16_nofilter_DX_arm;
    211                 fShaderProc16 = NULL;
    212 #endif
    213                 if (isOpaque) {
    214                     // this one is only very slighty faster than the C version
    215                     fSampleProc32 = SI8_opaque_D32_nofilter_DX_arm;
    216                     fShaderProc32 = NULL;
    217                 }
    218             }
    219             break;
    220         default:
    221             break;
    222     }
    223 #endif
    224 }
    225 
    226 ///////////////////////////////////////////////////////////////////////////////
    227 
    228 extern void platformConvolutionProcs_arm_neon(SkConvolutionProcs* procs);
    229 
    230 void platformConvolutionProcs_arm(SkConvolutionProcs* procs) {
    231 }
    232 
    233 void SkBitmapScaler::PlatformConvolutionProcs(SkConvolutionProcs* procs) {
    234     SK_ARM_NEON_WRAP(platformConvolutionProcs_arm)(procs);
    235 }
    236