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