SkBitmapProcState.h revision 9e1ec1a52985cce9db3a0d0e8d448b82a32e70cb
1
2/*
3 * Copyright 2007 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkBitmapProcState_DEFINED
11#define SkBitmapProcState_DEFINED
12
13#include "SkBitmap.h"
14#include "SkBitmapFilter.h"
15#include "SkMatrix.h"
16
17#define FractionalInt_IS_64BIT
18
19#ifdef FractionalInt_IS_64BIT
20    typedef SkFixed48    SkFractionalInt;
21    #define SkScalarToFractionalInt(x)  SkScalarToFixed48(x)
22    #define SkFractionalIntToFixed(x)   SkFixed48ToFixed(x)
23    #define SkFixedToFractionalInt(x)   SkFixedToFixed48(x)
24    #define SkFractionalIntToInt(x)     SkFixed48ToInt(x)
25#else
26    typedef SkFixed    SkFractionalInt;
27    #define SkScalarToFractionalInt(x)  SkScalarToFixed(x)
28    #define SkFractionalIntToFixed(x)   (x)
29    #define SkFixedToFractionalInt(x)   (x)
30    #define SkFractionalIntToInt(x)     ((x) >> 16)
31#endif
32
33class SkPaint;
34
35struct SkBitmapProcState {
36
37    typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
38                                 SkPMColor[], int count);
39
40    typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y,
41                                 uint16_t[], int count);
42
43    typedef void (*MatrixProc)(const SkBitmapProcState&,
44                               uint32_t bitmapXY[],
45                               int count,
46                               int x, int y);
47
48    typedef void (*SampleProc32)(const SkBitmapProcState&,
49                                 const uint32_t[],
50                                 int count,
51                                 SkPMColor colors[]);
52
53    typedef void (*SampleProc16)(const SkBitmapProcState&,
54                                 const uint32_t[],
55                                 int count,
56                                 uint16_t colors[]);
57
58    typedef U16CPU (*FixedTileProc)(SkFixed);   // returns 0..0xFFFF
59    typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int);   // returns 0..0xF
60    typedef U16CPU (*IntTileProc)(int value, int count);   // returns 0..count-1
61
62    const SkBitmap*     fBitmap;            // chooseProcs - orig or mip
63    const SkMatrix*     fInvMatrix;         // chooseProcs
64    SkMatrix::MapXYProc fInvProc;           // chooseProcs
65
66    SkFractionalInt     fInvSxFractionalInt;
67    SkFractionalInt     fInvKyFractionalInt;
68
69    FixedTileProc       fTileProcX;         // chooseProcs
70    FixedTileProc       fTileProcY;         // chooseProcs
71    FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
72    FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
73    IntTileProc         fIntTileProcY;      // chooseProcs
74    SkFixed             fFilterOneX;
75    SkFixed             fFilterOneY;
76
77    SkPMColor           fPaintPMColor;      // chooseProcs - A8 config
78    SkFixed             fInvSx;             // chooseProcs
79    SkFixed             fInvKy;             // chooseProcs
80    uint16_t            fAlphaScale;        // chooseProcs
81    uint8_t             fInvType;           // chooseProcs
82    uint8_t             fTileModeX;         // CONSTRUCTOR
83    uint8_t             fTileModeY;         // CONSTRUCTOR
84    SkBool8             fDoFilter;          // chooseProcs
85
86    /** Platforms implement this, and can optionally overwrite only the
87        following fields:
88
89        fShaderProc32
90        fShaderProc16
91        fMatrixProc
92        fSampleProc32
93        fSampleProc32
94
95        They will already have valid function pointers, so a platform that does
96        not have an accelerated version can just leave that field as is. A valid
97        implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
98     */
99    void platformProcs();
100
101
102    /** Given the byte size of the index buffer to be passed to the matrix proc,
103        return the maximum number of resulting pixels that can be computed
104        (i.e. the number of SkPMColor values to be written by the sample proc).
105        This routine takes into account that filtering and scale-vs-affine
106        affect the amount of buffer space needed.
107
108        Only valid to call after chooseProcs (setContext) has been called. It is
109        safe to call this inside the shader's shadeSpan() method.
110     */
111    int maxCountForBufferSize(size_t bufferSize) const;
112
113    // If a shader proc is present, then the corresponding matrix/sample procs
114    // are ignored
115    ShaderProc32 getShaderProc32() const { return fShaderProc32; }
116    ShaderProc16 getShaderProc16() const { return fShaderProc16; }
117
118    SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
119
120#ifdef SK_DEBUG
121    MatrixProc getMatrixProc() const;
122#else
123    MatrixProc getMatrixProc() const { return fMatrixProc; }
124#endif
125    SampleProc32 getSampleProc32() const { return fSampleProc32; }
126    SampleProc16 getSampleProc16() const { return fSampleProc16; }
127
128private:
129    friend class SkBitmapProcShader;
130
131    ShaderProc32        fShaderProc32;      // chooseProcs
132    ShaderProc16        fShaderProc16;      // chooseProcs
133    // These are used if the shaderproc is NULL
134    MatrixProc          fMatrixProc;        // chooseProcs
135    SampleProc32        fSampleProc32;      // chooseProcs
136    SampleProc16        fSampleProc16;      // chooseProcs
137
138    SkMatrix            fUnitInvMatrix;     // chooseProcs
139    SkBitmap            fOrigBitmap;        // CONSTRUCTOR
140    SkBitmap            fMipBitmap;
141
142    MatrixProc chooseMatrixProc(bool trivial_matrix);
143    bool chooseProcs(const SkMatrix& inv, const SkPaint&);
144    ShaderProc32 chooseShaderProc32();
145
146    void buildFilterCoefficients(SkFixed dst[4], float t) const;
147    SkBitmapFilter *fBitmapFilter;
148
149    ShaderProc32 chooseBitmapFilterProc(const SkPaint &paint);
150
151    // Return false if we failed to setup for fast translate (e.g. overflow)
152    bool setupForTranslate();
153
154#ifdef SK_DEBUG
155    static void DebugMatrixProc(const SkBitmapProcState&,
156                                uint32_t[], int count, int x, int y);
157#endif
158};
159
160/*  Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
161    Used to allow access to a stream of uint16_t either one at a time, or
162    2 at a time by unpacking a uint32_t
163 */
164#ifdef SK_CPU_BENDIAN
165    #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
166    #define UNPACK_PRIMARY_SHORT(packed)    ((uint32_t)(packed) >> 16)
167    #define UNPACK_SECONDARY_SHORT(packed)  ((packed) & 0xFFFF)
168#else
169    #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
170    #define UNPACK_PRIMARY_SHORT(packed)    ((packed) & 0xFFFF)
171    #define UNPACK_SECONDARY_SHORT(packed)  ((uint32_t)(packed) >> 16)
172#endif
173
174#ifdef SK_DEBUG
175    static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
176        SkASSERT((uint16_t)pri == pri);
177        SkASSERT((uint16_t)sec == sec);
178        return PACK_TWO_SHORTS(pri, sec);
179    }
180#else
181    #define pack_two_shorts(pri, sec)   PACK_TWO_SHORTS(pri, sec)
182#endif
183
184// These functions are generated via macros, but are exposed here so that
185// platformProcs may test for them by name.
186void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
187                              int count, SkPMColor colors[]);
188void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
189                             int count, SkPMColor colors[]);
190void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
191                           const uint32_t xy[], int count, SkPMColor colors[]);
192void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
193                           const uint32_t xy[], int count, SkPMColor colors[]);
194void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
195                                int count, int x, int y);
196void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
197                                  int count, int x, int y);
198void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
199                                 uint32_t xy[], int count, int x, int y);
200void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
201                                   uint32_t xy[], int count, int x, int y);
202void S32_D16_filter_DX(const SkBitmapProcState& s,
203                                   const uint32_t* xy, int count, uint16_t* colors);
204
205void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
206                             SkPMColor *SK_RESTRICT colors, int count);
207void highQualityFilter(const SkBitmapProcState &s, int x, int y,
208                   SkPMColor *SK_RESTRICT colors, int count);
209
210
211#endif
212