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