Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2007 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 #ifndef SkBitmapProcState_DEFINED
      9 #define SkBitmapProcState_DEFINED
     10 
     11 #include "SkBitmap.h"
     12 #include "SkBitmapController.h"
     13 #include "SkBitmapFilter.h"
     14 #include "SkBitmapProvider.h"
     15 #include "SkFloatBits.h"
     16 #include "SkMatrix.h"
     17 #include "SkMipMap.h"
     18 #include "SkPaint.h"
     19 #include "SkShader.h"
     20 #include "SkTemplates.h"
     21 
     22 typedef SkFixed3232    SkFractionalInt;
     23 #define SkScalarToFractionalInt(x)  SkScalarToFixed3232(x)
     24 #define SkFractionalIntToFixed(x)   SkFixed3232ToFixed(x)
     25 #define SkFixedToFractionalInt(x)   SkFixedToFixed3232(x)
     26 #define SkFractionalIntToInt(x)     SkFixed3232ToInt(x)
     27 
     28 class SkPaint;
     29 
     30 struct SkBitmapProcState {
     31     SkBitmapProcState(const SkBitmapProvider&, SkShader::TileMode tmx, SkShader::TileMode tmy);
     32     SkBitmapProcState(const SkBitmap&, SkShader::TileMode tmx, SkShader::TileMode tmy);
     33     ~SkBitmapProcState();
     34 
     35     typedef void (*ShaderProc32)(const void* ctx, int x, int y, SkPMColor[], int count);
     36 
     37     typedef void (*ShaderProc16)(const void* ctx, int x, int y, uint16_t[], int count);
     38 
     39     typedef void (*MatrixProc)(const SkBitmapProcState&,
     40                                uint32_t bitmapXY[],
     41                                int count,
     42                                int x, int y);
     43 
     44     typedef void (*SampleProc32)(const SkBitmapProcState&,
     45                                  const uint32_t[],
     46                                  int count,
     47                                  SkPMColor colors[]);
     48 
     49     typedef U16CPU (*FixedTileProc)(SkFixed);   // returns 0..0xFFFF
     50     typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int);   // returns 0..0xF
     51     typedef U16CPU (*IntTileProc)(int value, int count);   // returns 0..count-1
     52 
     53     SkPixmap            fPixmap;
     54     SkMatrix            fInvMatrix;         // copy of what is in fBMState, can we remove the dup?
     55 
     56     SkMatrix::MapXYProc fInvProc;           // chooseProcs
     57 
     58     SkFractionalInt     fInvSxFractionalInt;
     59     SkFractionalInt     fInvKyFractionalInt;
     60 
     61     FixedTileProc       fTileProcX;         // chooseProcs
     62     FixedTileProc       fTileProcY;         // chooseProcs
     63     FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
     64     FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
     65     IntTileProc         fIntTileProcY;      // chooseProcs
     66     SkFixed             fFilterOneX;
     67     SkFixed             fFilterOneY;
     68 
     69     SkPMColor           fPaintPMColor;      // chooseProcs - A8 config
     70     SkFixed             fInvSx;             // chooseProcs
     71     SkFixed             fInvKy;             // chooseProcs
     72     uint16_t            fAlphaScale;        // chooseProcs
     73     uint8_t             fInvType;           // chooseProcs
     74     uint8_t             fTileModeX;         // CONSTRUCTOR
     75     uint8_t             fTileModeY;         // CONSTRUCTOR
     76     uint8_t             fFilterLevel;       // chooseProcs
     77 
     78     /** Platforms implement this, and can optionally overwrite only the
     79         following fields:
     80 
     81         fShaderProc32
     82         fShaderProc16
     83         fMatrixProc
     84         fSampleProc32
     85         fSampleProc32
     86 
     87         They will already have valid function pointers, so a platform that does
     88         not have an accelerated version can just leave that field as is. A valid
     89         implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
     90      */
     91     void platformProcs();
     92 
     93     /** Given the byte size of the index buffer to be passed to the matrix proc,
     94         return the maximum number of resulting pixels that can be computed
     95         (i.e. the number of SkPMColor values to be written by the sample proc).
     96         This routine takes into account that filtering and scale-vs-affine
     97         affect the amount of buffer space needed.
     98 
     99         Only valid to call after chooseProcs (setContext) has been called. It is
    100         safe to call this inside the shader's shadeSpan() method.
    101      */
    102     int maxCountForBufferSize(size_t bufferSize) const;
    103 
    104     // If a shader proc is present, then the corresponding matrix/sample procs
    105     // are ignored
    106     ShaderProc32 getShaderProc32() const { return fShaderProc32; }
    107     ShaderProc16 getShaderProc16() const { return fShaderProc16; }
    108 
    109 #ifdef SK_DEBUG
    110     MatrixProc getMatrixProc() const;
    111 #else
    112     MatrixProc getMatrixProc() const { return fMatrixProc; }
    113 #endif
    114     SampleProc32 getSampleProc32() const { return fSampleProc32; }
    115 
    116 private:
    117     friend class SkBitmapProcShader;
    118     friend class SkLightingShaderImpl;
    119 
    120     ShaderProc32        fShaderProc32;      // chooseProcs
    121     ShaderProc16        fShaderProc16;      // chooseProcs
    122     // These are used if the shaderproc is nullptr
    123     MatrixProc          fMatrixProc;        // chooseProcs
    124     SampleProc32        fSampleProc32;      // chooseProcs
    125 
    126     const SkBitmapProvider fProvider;
    127 
    128     enum {
    129         kBMStateSize = 136  // found by inspection. if too small, we will call new/delete
    130     };
    131     SkAlignedSStorage<kBMStateSize> fBMStateStorage;
    132     SkBitmapController::State* fBMState;
    133 
    134     MatrixProc chooseMatrixProc(bool trivial_matrix);
    135     bool chooseProcs(const SkMatrix& inv, const SkPaint&);
    136     bool chooseScanlineProcs(bool trivialMatrix, bool clampClamp, const SkPaint& paint);
    137     ShaderProc32 chooseShaderProc32();
    138 
    139     // Return false if we failed to setup for fast translate (e.g. overflow)
    140     bool setupForTranslate();
    141 
    142 #ifdef SK_DEBUG
    143     static void DebugMatrixProc(const SkBitmapProcState&,
    144                                 uint32_t[], int count, int x, int y);
    145 #endif
    146 };
    147 
    148 /*  Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
    149     Used to allow access to a stream of uint16_t either one at a time, or
    150     2 at a time by unpacking a uint32_t
    151  */
    152 #ifdef SK_CPU_BENDIAN
    153     #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
    154     #define UNPACK_PRIMARY_SHORT(packed)    ((uint32_t)(packed) >> 16)
    155     #define UNPACK_SECONDARY_SHORT(packed)  ((packed) & 0xFFFF)
    156 #else
    157     #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
    158     #define UNPACK_PRIMARY_SHORT(packed)    ((packed) & 0xFFFF)
    159     #define UNPACK_SECONDARY_SHORT(packed)  ((uint32_t)(packed) >> 16)
    160 #endif
    161 
    162 #ifdef SK_DEBUG
    163     static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
    164         SkASSERT((uint16_t)pri == pri);
    165         SkASSERT((uint16_t)sec == sec);
    166         return PACK_TWO_SHORTS(pri, sec);
    167     }
    168 #else
    169     #define pack_two_shorts(pri, sec)   PACK_TWO_SHORTS(pri, sec)
    170 #endif
    171 
    172 // These functions are generated via macros, but are exposed here so that
    173 // platformProcs may test for them by name.
    174 void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
    175                               int count, SkPMColor colors[]);
    176 void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
    177                              int count, SkPMColor colors[]);
    178 void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
    179                                 const uint32_t xy[], int count, SkPMColor colors[]);
    180 void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
    181                                const uint32_t xy[], int count, SkPMColor colors[]);
    182 void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
    183                                 int count, int x, int y);
    184 void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
    185                                   int count, int x, int y);
    186 void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
    187                                  uint32_t xy[], int count, int x, int y);
    188 void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
    189                                    uint32_t xy[], int count, int x, int y);
    190 
    191 // Helper class for mapping the middle of pixel (x, y) into SkFractionalInt bitmap space.
    192 class SkBitmapProcStateAutoMapper {
    193 public:
    194     SkBitmapProcStateAutoMapper(const SkBitmapProcState& s, int x, int y,
    195                                 SkPoint* scalarPoint = nullptr) {
    196         SkPoint pt;
    197         s.fInvProc(s.fInvMatrix,
    198                    SkIntToScalar(x) + SK_ScalarHalf,
    199                    SkIntToScalar(y) + SK_ScalarHalf, &pt);
    200 
    201         SkFixed biasX, biasY;
    202         if (s.fFilterLevel == kNone_SkFilterQuality) {
    203             // SkFixed epsilon bias to ensure inverse-mapped bitmap coordinates are rounded
    204             // consistently WRT geometry.  Note that we only need the bias for positive scales:
    205             // for negative scales, the rounding is intrinsically correct.
    206             // We scale it to persist SkFractionalInt -> SkFixed conversions.
    207             biasX = (s.fInvMatrix.getScaleX() > 0);
    208             biasY = (s.fInvMatrix.getScaleY() > 0);
    209         } else {
    210             biasX = s.fFilterOneX >> 1;
    211             biasY = s.fFilterOneY >> 1;
    212         }
    213 
    214         fX = SkScalarToFractionalInt(pt.x()) - SkFixedToFractionalInt(biasX);
    215         fY = SkScalarToFractionalInt(pt.y()) - SkFixedToFractionalInt(biasY);
    216 
    217         if (scalarPoint) {
    218             scalarPoint->set(pt.x() - SkFixedToScalar(biasX),
    219                              pt.y() - SkFixedToScalar(biasY));
    220         }
    221     }
    222 
    223     SkFractionalInt fractionalIntX() const { return fX; }
    224     SkFractionalInt fractionalIntY() const { return fY; }
    225 
    226     SkFixed fixedX() const { return SkFractionalIntToFixed(fX); }
    227     SkFixed fixedY() const { return SkFractionalIntToFixed(fY); }
    228 
    229     int intX() const { return SkFractionalIntToInt(fX); }
    230     int intY() const { return SkFractionalIntToInt(fY); }
    231 
    232 private:
    233     SkFractionalInt fX, fY;
    234 };
    235 
    236 #endif
    237