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