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