blob: b4fae045f8b606687bc35c87f9a3e2403bbddb07 [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"
humper@google.comb0889472013-07-09 21:37:14 +000014#include "SkBitmapFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMatrix.h"
16
reed@google.com77613a52012-03-14 12:21:17 +000017#define FractionalInt_IS_64BIT
reed@google.com4bc0a9d2012-03-07 21:47:41 +000018
19#ifdef FractionalInt_IS_64BIT
20 typedef SkFixed48 SkFractionalInt;
21 #define SkScalarToFractionalInt(x) SkScalarToFixed48(x)
22 #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x)
reed@google.com411215a2012-03-08 20:13:46 +000023 #define SkFixedToFractionalInt(x) SkFixedToFixed48(x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000024 #define SkFractionalIntToInt(x) SkFixed48ToInt(x)
25#else
26 typedef SkFixed SkFractionalInt;
27 #define SkScalarToFractionalInt(x) SkScalarToFixed(x)
28 #define SkFractionalIntToFixed(x) (x)
reed@google.com411215a2012-03-08 20:13:46 +000029 #define SkFixedToFractionalInt(x) (x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000030 #define SkFractionalIntToInt(x) ((x) >> 16)
31#endif
32
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkPaint;
humper@google.comdd850882013-07-19 20:52:12 +000034struct SkConvolutionProcs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
36struct SkBitmapProcState {
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +000037
commit-bot@chromium.orge81d1bc2013-07-10 20:42:12 +000038 SkBitmapProcState(): fBitmapFilter(NULL) {}
39 ~SkBitmapProcState() {
40 SkDELETE(fBitmapFilter);
41 }
42
reed@android.com7a99eb12009-07-16 01:13:14 +000043 typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
44 SkPMColor[], int count);
45
46 typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y,
47 uint16_t[], int count);
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 typedef void (*MatrixProc)(const SkBitmapProcState&,
50 uint32_t bitmapXY[],
51 int count,
52 int x, int y);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 typedef void (*SampleProc32)(const SkBitmapProcState&,
55 const uint32_t[],
56 int count,
57 SkPMColor colors[]);
58
59 typedef void (*SampleProc16)(const SkBitmapProcState&,
60 const uint32_t[],
61 int count,
62 uint16_t colors[]);
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF
reed@google.comf444e8c2012-03-09 19:59:46 +000065 typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF
reed@android.com07d1f002009-08-13 19:35:48 +000066 typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1
reed@android.com7a99eb12009-07-16 01:13:14 +000067
humper@google.com9c96d4b2013-07-14 01:44:59 +000068 const SkBitmap* fBitmap; // chooseProcs - orig or scaled
69 SkMatrix fInvMatrix; // chooseProcs
reed@android.comeef375b2009-08-03 14:45:45 +000070 SkMatrix::MapXYProc fInvProc; // chooseProcs
71
reed@google.com4bc0a9d2012-03-07 21:47:41 +000072 SkFractionalInt fInvSxFractionalInt;
reed@google.com411215a2012-03-08 20:13:46 +000073 SkFractionalInt fInvKyFractionalInt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 FixedTileProc fTileProcX; // chooseProcs
76 FixedTileProc fTileProcY; // chooseProcs
reed@google.comf444e8c2012-03-09 19:59:46 +000077 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
78 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
reed@android.com07d1f002009-08-13 19:35:48 +000079 IntTileProc fIntTileProcY; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkFixed fFilterOneX;
81 SkFixed fFilterOneY;
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +000082
humper@google.com138ebc32013-07-19 20:20:04 +000083 SkConvolutionProcs* fConvolutionProcs; // possiblyScaleImage
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkPMColor fPaintPMColor; // chooseProcs - A8 config
reed@android.comeef375b2009-08-03 14:45:45 +000086 SkFixed fInvSx; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkFixed fInvKy; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 uint16_t fAlphaScale; // chooseProcs
89 uint8_t fInvType; // chooseProcs
90 uint8_t fTileModeX; // CONSTRUCTOR
91 uint8_t fTileModeY; // CONSTRUCTOR
humper@google.com9c96d4b2013-07-14 01:44:59 +000092
93 enum {
94 kNone_BitmapFilter,
95 kBilerp_BitmapFilter,
96 kHQ_BitmapFilter
97 } fFilterQuality; // chooseProcs
98
99 /** The shader will let us know when we can release some of our resources
100 * like scaled bitmaps.
101 */
102
103 void endContext();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
reed@android.comc9a1d4b2009-08-03 15:05:55 +0000105 /** Platforms implement this, and can optionally overwrite only the
106 following fields:
107
108 fShaderProc32
109 fShaderProc16
110 fMatrixProc
111 fSampleProc32
112 fSampleProc32
113
114 They will already have valid function pointers, so a platform that does
115 not have an accelerated version can just leave that field as is. A valid
116 implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
117 */
118 void platformProcs();
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000119
humper@google.com138ebc32013-07-19 20:20:04 +0000120 /** Platforms can also optionally overwrite the convolution functions
121 if we have SIMD versions of them.
122 */
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000123
humper@google.com138ebc32013-07-19 20:20:04 +0000124 void platformConvolutionProcs();
reed@google.com4c69a062013-05-23 20:11:56 +0000125
reed@android.com258cb222010-04-14 13:36:33 +0000126 /** Given the byte size of the index buffer to be passed to the matrix proc,
127 return the maximum number of resulting pixels that can be computed
128 (i.e. the number of SkPMColor values to be written by the sample proc).
129 This routine takes into account that filtering and scale-vs-affine
130 affect the amount of buffer space needed.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000131
reed@android.com4c128c42009-08-14 13:54:37 +0000132 Only valid to call after chooseProcs (setContext) has been called. It is
133 safe to call this inside the shader's shadeSpan() method.
134 */
135 int maxCountForBufferSize(size_t bufferSize) const;
136
reed@google.com9fe287b2012-03-27 15:54:28 +0000137 // If a shader proc is present, then the corresponding matrix/sample procs
138 // are ignored
139 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
140 ShaderProc16 getShaderProc16() const { return fShaderProc16; }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000141
humper@google.comb0889472013-07-09 21:37:14 +0000142 SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
reed@google.com9fe287b2012-03-27 15:54:28 +0000143
144#ifdef SK_DEBUG
145 MatrixProc getMatrixProc() const;
146#else
147 MatrixProc getMatrixProc() const { return fMatrixProc; }
148#endif
149 SampleProc32 getSampleProc32() const { return fSampleProc32; }
150 SampleProc16 getSampleProc16() const { return fSampleProc16; }
151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152private:
reed@android.comeef375b2009-08-03 14:45:45 +0000153 friend class SkBitmapProcShader;
154
reed@google.com9fe287b2012-03-27 15:54:28 +0000155 ShaderProc32 fShaderProc32; // chooseProcs
156 ShaderProc16 fShaderProc16; // chooseProcs
157 // These are used if the shaderproc is NULL
158 MatrixProc fMatrixProc; // chooseProcs
159 SampleProc32 fSampleProc32; // chooseProcs
160 SampleProc16 fSampleProc16; // chooseProcs
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000161
reed@android.comeef375b2009-08-03 14:45:45 +0000162 SkBitmap fOrigBitmap; // CONSTRUCTOR
humper@google.com9c96d4b2013-07-14 01:44:59 +0000163 SkBitmap fScaledBitmap; // chooseProcs
reed@android.comeef375b2009-08-03 14:45:45 +0000164
reed@android.com07d1f002009-08-13 19:35:48 +0000165 MatrixProc chooseMatrixProc(bool trivial_matrix);
reed@android.comeef375b2009-08-03 14:45:45 +0000166 bool chooseProcs(const SkMatrix& inv, const SkPaint&);
reed@google.com9a4c7462012-10-12 18:21:37 +0000167 ShaderProc32 chooseShaderProc32();
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +0000168
humper@google.com9c96d4b2013-07-14 01:44:59 +0000169 void possiblyScaleImage();
170
humper@google.com138ebc32013-07-19 20:20:04 +0000171 SkBitmapFilter* fBitmapFilter;
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +0000172
humper@google.com9c96d4b2013-07-14 01:44:59 +0000173 ShaderProc32 chooseBitmapFilterProc();
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +0000174
reed@google.com6bb92bc2012-11-20 19:45:16 +0000175 // Return false if we failed to setup for fast translate (e.g. overflow)
176 bool setupForTranslate();
reed@google.com9fe287b2012-03-27 15:54:28 +0000177
178#ifdef SK_DEBUG
179 static void DebugMatrixProc(const SkBitmapProcState&,
180 uint32_t[], int count, int x, int y);
181#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182};
183
reed@android.com07d1f002009-08-13 19:35:48 +0000184/* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
185 Used to allow access to a stream of uint16_t either one at a time, or
186 2 at a time by unpacking a uint32_t
187 */
188#ifdef SK_CPU_BENDIAN
189 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
190 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
191 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
192#else
193 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
194 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
195 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
196#endif
197
198#ifdef SK_DEBUG
199 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
200 SkASSERT((uint16_t)pri == pri);
201 SkASSERT((uint16_t)sec == sec);
202 return PACK_TWO_SHORTS(pri, sec);
203 }
204#else
205 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
206#endif
207
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000208// These functions are generated via macros, but are exposed here so that
209// platformProcs may test for them by name.
210void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
211 int count, SkPMColor colors[]);
senorblanco@chromium.orgf3f0bd72009-12-10 22:46:31 +0000212void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
213 int count, SkPMColor colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000214void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
215 const uint32_t xy[], int count, SkPMColor colors[]);
216void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
tomhudson@google.comae29b882012-03-06 14:59:04 +0000217 const uint32_t xy[], int count, SkPMColor colors[]);
tomhudson@google.com06a73132012-02-22 18:30:43 +0000218void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
219 int count, int x, int y);
220void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
221 int count, int x, int y);
tomhudson@google.com5efaf262012-02-28 15:41:49 +0000222void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
223 uint32_t xy[], int count, int x, int y);
224void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
225 uint32_t xy[], int count, int x, int y);
reed@google.com78662282012-07-24 13:53:23 +0000226void S32_D16_filter_DX(const SkBitmapProcState& s,
227 const uint32_t* xy, int count, uint16_t* colors);
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000228
humper@google.comb0889472013-07-09 21:37:14 +0000229void highQualityFilter(const SkBitmapProcState &s, int x, int y,
230 SkPMColor *SK_RESTRICT colors, int count);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000231
humper@google.comb0889472013-07-09 21:37:14 +0000232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233#endif