SkBitmapProcState.h revision 011f39aeb2b9715546eb74d9ebb71be7baf95fde
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
145    MatrixProc chooseMatrixProc(bool trivial_matrix);
146    bool chooseProcs(const SkMatrix& inv, const SkPaint&);
147    ShaderProc32 chooseShaderProc32();
148
149    // returns false if we did not try to scale the image. In that case, we
150    // will need to "lock" its pixels some other way.
151    bool possiblyScaleImage();
152
153    // returns false if we failed to "lock" the pixels at all. Typically this
154    // means we have to abort the shader.
155    bool lockBaseBitmap();
156
157    SkBitmapFilter* fBitmapFilter;
158
159    // If supported, sets fShaderProc32 and fShaderProc16 and returns true,
160    // otherwise returns false.
161    bool setBitmapFilterProcs();
162
163    // Return false if we failed to setup for fast translate (e.g. overflow)
164    bool setupForTranslate();
165
166#ifdef SK_DEBUG
167    static void DebugMatrixProc(const SkBitmapProcState&,
168                                uint32_t[], int count, int x, int y);
169#endif
170};
171
172/*  Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
173    Used to allow access to a stream of uint16_t either one at a time, or
174    2 at a time by unpacking a uint32_t
175 */
176#ifdef SK_CPU_BENDIAN
177    #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
178    #define UNPACK_PRIMARY_SHORT(packed)    ((uint32_t)(packed) >> 16)
179    #define UNPACK_SECONDARY_SHORT(packed)  ((packed) & 0xFFFF)
180#else
181    #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
182    #define UNPACK_PRIMARY_SHORT(packed)    ((packed) & 0xFFFF)
183    #define UNPACK_SECONDARY_SHORT(packed)  ((uint32_t)(packed) >> 16)
184#endif
185
186#ifdef SK_DEBUG
187    static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
188        SkASSERT((uint16_t)pri == pri);
189        SkASSERT((uint16_t)sec == sec);
190        return PACK_TWO_SHORTS(pri, sec);
191    }
192#else
193    #define pack_two_shorts(pri, sec)   PACK_TWO_SHORTS(pri, sec)
194#endif
195
196// These functions are generated via macros, but are exposed here so that
197// platformProcs may test for them by name.
198void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
199                              int count, SkPMColor colors[]);
200void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
201                             int count, SkPMColor colors[]);
202void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
203                                const uint32_t xy[], int count, SkPMColor colors[]);
204void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
205                               const uint32_t xy[], int count, SkPMColor colors[]);
206void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
207                                int count, int x, int y);
208void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
209                                  int count, int x, int y);
210void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
211                                 uint32_t xy[], int count, int x, int y);
212void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
213                                   uint32_t xy[], int count, int x, int y);
214void S32_D16_filter_DX(const SkBitmapProcState& s,
215                       const uint32_t* xy, int count, uint16_t* colors);
216
217void highQualityFilter32(const SkBitmapProcState &s, int x, int y,
218                         SkPMColor *SK_RESTRICT colors, int count);
219void highQualityFilter16(const SkBitmapProcState &s, int x, int y,
220                         uint16_t *SK_RESTRICT colors, int count);
221
222
223#endif
224