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