blob: d9957bd578b9c76d45eeb4941cada749aa0d9308 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrSamplerState_DEFINED
12#define GrSamplerState_DEFINED
13
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000014#include "GrCustomStage.h"
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000015#include "GrMatrix.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +000018#define MAX_KERNEL_WIDTH 25
19
reed@google.comac10a2d2010-12-22 21:39:39 +000020class GrSamplerState {
21public:
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000022 enum Filter {
23 /**
24 * Read the closest src texel to the sample position
25 */
26 kNearest_Filter,
27 /**
28 * Blend between closest 4 src texels to sample position (tent filter)
29 */
30 kBilinear_Filter,
31 /**
32 * Average of 4 bilinear filterings spaced +/- 1 texel from sample
33 * position in x and y. Intended for averaging 16 texels in a downsample
34 * pass. (rasterizing such that texture samples fall exactly halfway
bsalomon@google.com1f221a72011-08-23 20:54:07 +000035 * between texels in x and y spaced 4 texels apart.) Only supported
36 * on shader backends.
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000037 */
38 k4x4Downsample_Filter,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +000039 /**
40 * Apply a separable convolution kernel.
41 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000042 kConvolution_Filter,
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000043 /**
44 * Apply a dilate filter (max over a 1D radius).
45 */
46 kDilate_Filter,
47 /**
48 * Apply an erode filter (min over a 1D radius).
49 */
50 kErode_Filter,
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000051
52 kDefault_Filter = kNearest_Filter
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000053 };
54
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000055 /**
56 * The intepretation of the texture matrix depends on the sample mode. The
57 * texture matrix is applied both when the texture coordinates are explicit
58 * and when vertex positions are used as texture coordinates. In the latter
59 * case the texture matrix is applied to the pre-view-matrix position
60 * values.
61 *
62 * kNormal_SampleMode
63 * The post-matrix texture coordinates are in normalize space with (0,0) at
64 * the top-left and (1,1) at the bottom right.
65 * kRadial_SampleMode
66 * The matrix specifies the radial gradient parameters.
67 * (0,0) in the post-matrix space is center of the radial gradient.
68 * kRadial2_SampleMode
69 * Matrix transforms to space where first circle is centered at the
70 * origin. The second circle will be centered (x, 0) where x may be
71 * 0 and is provided by setRadial2Params. The post-matrix space is
72 * normalized such that 1 is the second radius - first radius.
73 * kSweepSampleMode
74 * The angle from the origin of texture coordinates in post-matrix space
75 * determines the gradient value.
76 */
reed@google.comac10a2d2010-12-22 21:39:39 +000077 enum SampleMode {
78 kNormal_SampleMode, //!< sample color directly
reed@google.comac10a2d2010-12-22 21:39:39 +000079 kRadial_SampleMode, //!< treat as radial gradient
80 kRadial2_SampleMode, //!< treat as 2-point radial gradient
81 kSweep_SampleMode, //!< treat as sweep gradient
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000082
83 kDefault_SampleMode = kNormal_SampleMode
reed@google.comac10a2d2010-12-22 21:39:39 +000084 };
85
86 /**
87 * Describes how a texture is sampled when coordinates are outside the
88 * texture border
89 */
90 enum WrapMode {
91 kClamp_WrapMode,
92 kRepeat_WrapMode,
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000093 kMirror_WrapMode,
94
95 kDefault_WrapMode = kClamp_WrapMode
reed@google.comac10a2d2010-12-22 21:39:39 +000096 };
97
98 /**
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000099 * For the filters which perform more than one texture sample (convolution,
100 * erode, dilate), this determines the direction in which the texture
101 * coordinates will be incremented.
102 */
103 enum FilterDirection {
104 kX_FilterDirection,
105 kY_FilterDirection,
106
107 kDefault_FilterDirection = kX_FilterDirection,
108 };
109 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000110 * Default sampler state is set to clamp, use normal sampling mode, be
111 * unfiltered, and use identity matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 */
bsalomon@google.com6aab8e32011-06-21 20:32:12 +0000113 GrSamplerState()
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000114 : fCustomStage (NULL)
115 , fRadial2CenterX1()
bsalomon@google.com6aab8e32011-06-21 20:32:12 +0000116 , fRadial2Radius0()
117 , fRadial2PosRoot() {
bsalomon@google.com97912912011-12-06 16:30:36 +0000118 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 }
120
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000121 ~GrSamplerState() {
122 GrSafeUnref(fCustomStage);
123 }
124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 WrapMode getWrapX() const { return fWrapX; }
126 WrapMode getWrapY() const { return fWrapY; }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000127 FilterDirection getFilterDirection() const { return fFilterDirection; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 SampleMode getSampleMode() const { return fSampleMode; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000129 const GrMatrix& getMatrix() const { return fMatrix; }
junov@google.com6acc9b32011-05-16 18:32:07 +0000130 const GrRect& getTextureDomain() const { return fTextureDomain; }
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000131 bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();}
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000132 Filter getFilter() const { return fFilter; }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000133 int getKernelWidth() const { return fKernelWidth; }
134 const float* getKernel() const { return fKernel; }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000135 bool swapsRAndB() const { return fSwapRAndB; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
137 bool isGradient() const {
138 return kRadial_SampleMode == fSampleMode ||
139 kRadial2_SampleMode == fSampleMode ||
140 kSweep_SampleMode == fSampleMode;
141 }
142
143 void setWrapX(WrapMode mode) { fWrapX = mode; }
144 void setWrapY(WrapMode mode) { fWrapY = mode; }
145 void setSampleMode(SampleMode mode) { fSampleMode = mode; }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000146 void setFilterDirection(FilterDirection mode) { fFilterDirection = mode; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000147
148 /**
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000149 * Access the sampler's matrix. See SampleMode for explanation of
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000150 * relationship between the matrix and sample mode.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000151 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000152 GrMatrix* matrix() { return &fMatrix; }
153
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000154 /**
junov@google.com6acc9b32011-05-16 18:32:07 +0000155 * Sets the sampler's texture coordinate domain to a
156 * custom rectangle, rather than the default (0,1).
157 * This option is currently only supported with kClamp_WrapMode
158 */
159 void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; }
160
161 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000162 * Swaps the R and B components when reading from the texture. Has no effect
163 * if the texture is alpha only.
164 */
165 void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
166
167 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000168 * Multiplies the current sampler matrix a matrix
169 *
170 * After this call M' = M*m where M is the old matrix, m is the parameter
171 * to this function, and M' is the new matrix. (We consider points to
172 * be column vectors so tex cood vector t is transformed by matrix X as
173 * t' = X*t.)
174 *
175 * @param matrix the matrix used to modify the matrix.
176 */
177 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
178
179 /**
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000180 * Sets filtering type.
181 * @param filter type of filtering to apply
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000182 */
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000183 void setFilter(Filter filter) { fFilter = filter; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com97912912011-12-06 16:30:36 +0000185 void reset(WrapMode wrapXAndY,
186 Filter filter,
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000187 FilterDirection direction,
bsalomon@google.com97912912011-12-06 16:30:36 +0000188 const GrMatrix& matrix) {
189 fWrapX = wrapXAndY;
190 fWrapY = wrapXAndY;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000191 fSampleMode = kDefault_SampleMode;
bsalomon@google.com97912912011-12-06 16:30:36 +0000192 fFilter = filter;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000193 fFilterDirection = direction;
bsalomon@google.com97912912011-12-06 16:30:36 +0000194 fMatrix = matrix;
195 fTextureDomain.setEmpty();
196 fSwapRAndB = false;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000197 GrSafeSetNull(fCustomStage);
bsalomon@google.com97912912011-12-06 16:30:36 +0000198 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000199 void reset(WrapMode wrapXAndY, Filter filter, const GrMatrix& matrix) {
200 this->reset(wrapXAndY, filter, kDefault_FilterDirection, matrix);
201 }
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000202 void reset(WrapMode wrapXAndY,
203 Filter filter) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000204 this->reset(wrapXAndY, filter, kDefault_FilterDirection, GrMatrix::I());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000205 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000206 void reset(const GrMatrix& matrix) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000207 this->reset(kDefault_WrapMode, kDefault_Filter, kDefault_FilterDirection, matrix);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000208 }
209 void reset() {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000210 this->reset(kDefault_WrapMode, kDefault_Filter, kDefault_FilterDirection, GrMatrix::I());
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000211 }
bsalomon@google.com97912912011-12-06 16:30:36 +0000212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; }
214 GrScalar getRadial2Radius0() const { return fRadial2Radius0; }
bsalomon@google.come7160bf2011-11-10 15:28:16 +0000215 bool isRadial2PosRoot() const { return SkToBool(fRadial2PosRoot); }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000216 // do the radial gradient params lead to a linear (rather than quadratic)
217 // equation.
218 bool radial2IsDegenerate() const { return GR_Scalar1 == fRadial2CenterX1; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
220 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000221 * Sets the parameters for kRadial2_SampleMode. The texture
222 * matrix must be set so that the first point is at (0,0) and the second
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 * point lies on the x-axis. The second radius minus the first is 1 unit.
224 * The additional parameters to define the gradient are specified by this
225 * function.
226 */
227 void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) {
228 fRadial2CenterX1 = centerX1;
229 fRadial2Radius0 = radius0;
230 fRadial2PosRoot = posRoot;
231 }
232
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000233 void setConvolutionParams(int kernelWidth, const float* kernel) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000234 GrAssert(kernelWidth >= 0 && kernelWidth <= MAX_KERNEL_WIDTH);
235 fKernelWidth = kernelWidth;
236 if (NULL != kernel) {
237 memcpy(fKernel, kernel, kernelWidth * sizeof(float));
238 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000239 }
240
241 void setMorphologyRadius(int radius) {
242 GrAssert(radius >= 0 && radius <= MAX_KERNEL_WIDTH);
243 fKernelWidth = radius;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000244 }
245
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000246 void setCustomStage(GrCustomStage* stage) {
247 GrSafeAssign(fCustomStage, stage);
248 }
249 GrCustomStage* getCustomStage() const { return fCustomStage; }
250
reed@google.comac10a2d2010-12-22 21:39:39 +0000251private:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000252 WrapMode fWrapX : 8;
253 WrapMode fWrapY : 8;
254 FilterDirection fFilterDirection : 8;
255 SampleMode fSampleMode : 8;
256 Filter fFilter : 8;
257 GrMatrix fMatrix;
258 bool fSwapRAndB;
259 GrRect fTextureDomain;
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000261 /// BUG! Ganesh only works correctly so long as fCustomStage is
262 /// NULL; we need to have a complex ID system here so that we can
263 /// have an equality-like comparison to determine whether two
264 /// fCustomStages are equal.
265 GrCustomStage* fCustomStage;
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 // these are undefined unless fSampleMode == kRadial2_SampleMode
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000268 GrScalar fRadial2CenterX1;
269 GrScalar fRadial2Radius0;
270 SkBool8 fRadial2PosRoot;
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000272 // These are undefined unless fFilter == kConvolution_Filter
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000273 uint8_t fKernelWidth;
274 float fKernel[MAX_KERNEL_WIDTH];
reed@google.comac10a2d2010-12-22 21:39:39 +0000275};
276
277#endif
278