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