blob: 3020334f19b63b5e559efdda4165c5bf77d6e544 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#ifndef SkBitmapProcState_DEFINED
11#define SkBitmapProcState_DEFINED
12
13#include "SkBitmap.h"
14#include "SkMatrix.h"
15
reed@google.com77613a52012-03-14 12:21:17 +000016#define FractionalInt_IS_64BIT
reed@google.com4bc0a9d2012-03-07 21:47:41 +000017
18#ifdef FractionalInt_IS_64BIT
19 typedef SkFixed48 SkFractionalInt;
20 #define SkScalarToFractionalInt(x) SkScalarToFixed48(x)
21 #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x)
reed@google.com411215a2012-03-08 20:13:46 +000022 #define SkFixedToFractionalInt(x) SkFixedToFixed48(x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000023 #define SkFractionalIntToInt(x) SkFixed48ToInt(x)
24#else
25 typedef SkFixed SkFractionalInt;
26 #define SkScalarToFractionalInt(x) SkScalarToFixed(x)
27 #define SkFractionalIntToFixed(x) (x)
reed@google.com411215a2012-03-08 20:13:46 +000028 #define SkFixedToFractionalInt(x) (x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000029 #define SkFractionalIntToInt(x) ((x) >> 16)
30#endif
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkPaint;
33
34struct SkBitmapProcState {
35
reed@android.com7a99eb12009-07-16 01:13:14 +000036 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
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 typedef void (*MatrixProc)(const SkBitmapProcState&,
43 uint32_t bitmapXY[],
44 int count,
45 int x, int y);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 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[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF
reed@google.comf444e8c2012-03-09 19:59:46 +000058 typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF
reed@android.com07d1f002009-08-13 19:35:48 +000059 typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1
reed@android.com7a99eb12009-07-16 01:13:14 +000060
reed@android.comeef375b2009-08-03 14:45:45 +000061 const SkBitmap* fBitmap; // chooseProcs - orig or mip
62 const SkMatrix* fInvMatrix; // chooseProcs
63 SkMatrix::MapXYProc fInvProc; // chooseProcs
64
reed@google.com4bc0a9d2012-03-07 21:47:41 +000065 SkFractionalInt fInvSxFractionalInt;
reed@google.com411215a2012-03-08 20:13:46 +000066 SkFractionalInt fInvKyFractionalInt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 FixedTileProc fTileProcX; // chooseProcs
69 FixedTileProc fTileProcY; // chooseProcs
reed@google.comf444e8c2012-03-09 19:59:46 +000070 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
71 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
reed@android.com07d1f002009-08-13 19:35:48 +000072 IntTileProc fIntTileProcY; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 SkFixed fFilterOneX;
74 SkFixed fFilterOneY;
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkPMColor fPaintPMColor; // chooseProcs - A8 config
reed@android.comeef375b2009-08-03 14:45:45 +000077 SkFixed fInvSx; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkFixed fInvKy; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 uint16_t fAlphaScale; // chooseProcs
80 uint8_t fInvType; // chooseProcs
81 uint8_t fTileModeX; // CONSTRUCTOR
82 uint8_t fTileModeY; // CONSTRUCTOR
83 SkBool8 fDoFilter; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
reed@android.comc9a1d4b2009-08-03 15:05:55 +000085 /** 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
reed@google.com4c69a062013-05-23 20:11:56 +0000100
reed@android.com258cb222010-04-14 13:36:33 +0000101 /** Given the byte size of the index buffer to be passed to the matrix proc,
102 return the maximum number of resulting pixels that can be computed
103 (i.e. the number of SkPMColor values to be written by the sample proc).
104 This routine takes into account that filtering and scale-vs-affine
105 affect the amount of buffer space needed.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
reed@android.com4c128c42009-08-14 13:54:37 +0000107 Only valid to call after chooseProcs (setContext) has been called. It is
108 safe to call this inside the shader's shadeSpan() method.
109 */
110 int maxCountForBufferSize(size_t bufferSize) const;
111
reed@google.com9fe287b2012-03-27 15:54:28 +0000112 // If a shader proc is present, then the corresponding matrix/sample procs
113 // are ignored
114 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
115 ShaderProc16 getShaderProc16() const { return fShaderProc16; }
116
117#ifdef SK_DEBUG
118 MatrixProc getMatrixProc() const;
119#else
120 MatrixProc getMatrixProc() const { return fMatrixProc; }
121#endif
122 SampleProc32 getSampleProc32() const { return fSampleProc32; }
123 SampleProc16 getSampleProc16() const { return fSampleProc16; }
124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125private:
reed@android.comeef375b2009-08-03 14:45:45 +0000126 friend class SkBitmapProcShader;
127
reed@google.com9fe287b2012-03-27 15:54:28 +0000128 ShaderProc32 fShaderProc32; // chooseProcs
129 ShaderProc16 fShaderProc16; // chooseProcs
130 // These are used if the shaderproc is NULL
131 MatrixProc fMatrixProc; // chooseProcs
132 SampleProc32 fSampleProc32; // chooseProcs
133 SampleProc16 fSampleProc16; // chooseProcs
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134
reed@android.comeef375b2009-08-03 14:45:45 +0000135 SkMatrix fUnitInvMatrix; // chooseProcs
136 SkBitmap fOrigBitmap; // CONSTRUCTOR
137 SkBitmap fMipBitmap;
138
reed@android.com07d1f002009-08-13 19:35:48 +0000139 MatrixProc chooseMatrixProc(bool trivial_matrix);
reed@android.comeef375b2009-08-03 14:45:45 +0000140 bool chooseProcs(const SkMatrix& inv, const SkPaint&);
reed@google.com9a4c7462012-10-12 18:21:37 +0000141 ShaderProc32 chooseShaderProc32();
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +0000142
reed@google.com4c69a062013-05-23 20:11:56 +0000143
144 /** test method for choosing a bicubic shading filter
145 */
146
147 ShaderProc32 chooseBicubicFilterProc(const SkPaint &paint);
148
reed@google.com6bb92bc2012-11-20 19:45:16 +0000149 // Return false if we failed to setup for fast translate (e.g. overflow)
150 bool setupForTranslate();
reed@google.com9fe287b2012-03-27 15:54:28 +0000151
152#ifdef SK_DEBUG
153 static void DebugMatrixProc(const SkBitmapProcState&,
154 uint32_t[], int count, int x, int y);
155#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156};
157
reed@android.com07d1f002009-08-13 19:35:48 +0000158/* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
159 Used to allow access to a stream of uint16_t either one at a time, or
160 2 at a time by unpacking a uint32_t
161 */
162#ifdef SK_CPU_BENDIAN
163 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
164 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
165 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
166#else
167 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
168 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
169 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
170#endif
171
172#ifdef SK_DEBUG
173 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
174 SkASSERT((uint16_t)pri == pri);
175 SkASSERT((uint16_t)sec == sec);
176 return PACK_TWO_SHORTS(pri, sec);
177 }
178#else
179 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
180#endif
181
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000182// These functions are generated via macros, but are exposed here so that
183// platformProcs may test for them by name.
184void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
185 int count, SkPMColor colors[]);
senorblanco@chromium.orgf3f0bd72009-12-10 22:46:31 +0000186void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
187 int count, SkPMColor colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000188void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
189 const uint32_t xy[], int count, SkPMColor colors[]);
190void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
tomhudson@google.comae29b882012-03-06 14:59:04 +0000191 const uint32_t xy[], int count, SkPMColor colors[]);
tomhudson@google.com06a73132012-02-22 18:30:43 +0000192void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
193 int count, int x, int y);
194void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
195 int count, int x, int y);
tomhudson@google.com5efaf262012-02-28 15:41:49 +0000196void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
197 uint32_t xy[], int count, int x, int y);
198void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
199 uint32_t xy[], int count, int x, int y);
reed@google.com78662282012-07-24 13:53:23 +0000200void S32_D16_filter_DX(const SkBitmapProcState& s,
201 const uint32_t* xy, int count, uint16_t* colors);
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203#endif