epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrSamplerState_DEFINED |
| 12 | #define GrSamplerState_DEFINED |
| 13 | |
| 14 | #include "GrTypes.h" |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 15 | #include "GrMatrix.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 17 | #define MAX_KERNEL_WIDTH 25 |
| 18 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | class GrSamplerState { |
| 20 | public: |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 21 | enum Filter { |
| 22 | /** |
| 23 | * Read the closest src texel to the sample position |
| 24 | */ |
| 25 | kNearest_Filter, |
| 26 | /** |
| 27 | * Blend between closest 4 src texels to sample position (tent filter) |
| 28 | */ |
| 29 | kBilinear_Filter, |
| 30 | /** |
| 31 | * Average of 4 bilinear filterings spaced +/- 1 texel from sample |
| 32 | * position in x and y. Intended for averaging 16 texels in a downsample |
| 33 | * pass. (rasterizing such that texture samples fall exactly halfway |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 34 | * between texels in x and y spaced 4 texels apart.) Only supported |
| 35 | * on shader backends. |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 36 | */ |
| 37 | k4x4Downsample_Filter, |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Apply a separable convolution kernel. |
| 40 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 41 | kConvolution_Filter, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 42 | /** |
| 43 | * Apply a dilate filter (max over a 1D radius). |
| 44 | */ |
| 45 | kDilate_Filter, |
| 46 | /** |
| 47 | * Apply an erode filter (min over a 1D radius). |
| 48 | */ |
| 49 | kErode_Filter, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 50 | |
| 51 | kDefault_Filter = kNearest_Filter |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The intepretation of the texture matrix depends on the sample mode. The |
| 56 | * texture matrix is applied both when the texture coordinates are explicit |
| 57 | * and when vertex positions are used as texture coordinates. In the latter |
| 58 | * case the texture matrix is applied to the pre-view-matrix position |
| 59 | * values. |
| 60 | * |
| 61 | * kNormal_SampleMode |
| 62 | * The post-matrix texture coordinates are in normalize space with (0,0) at |
| 63 | * the top-left and (1,1) at the bottom right. |
| 64 | * kRadial_SampleMode |
| 65 | * The matrix specifies the radial gradient parameters. |
| 66 | * (0,0) in the post-matrix space is center of the radial gradient. |
| 67 | * kRadial2_SampleMode |
| 68 | * Matrix transforms to space where first circle is centered at the |
| 69 | * origin. The second circle will be centered (x, 0) where x may be |
| 70 | * 0 and is provided by setRadial2Params. The post-matrix space is |
| 71 | * normalized such that 1 is the second radius - first radius. |
| 72 | * kSweepSampleMode |
| 73 | * The angle from the origin of texture coordinates in post-matrix space |
| 74 | * determines the gradient value. |
| 75 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | enum SampleMode { |
| 77 | kNormal_SampleMode, //!< sample color directly |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | kRadial_SampleMode, //!< treat as radial gradient |
| 79 | kRadial2_SampleMode, //!< treat as 2-point radial gradient |
| 80 | kSweep_SampleMode, //!< treat as sweep gradient |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 81 | |
| 82 | kDefault_SampleMode = kNormal_SampleMode |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Describes how a texture is sampled when coordinates are outside the |
| 87 | * texture border |
| 88 | */ |
| 89 | enum WrapMode { |
| 90 | kClamp_WrapMode, |
| 91 | kRepeat_WrapMode, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 92 | kMirror_WrapMode, |
| 93 | |
| 94 | kDefault_WrapMode = kClamp_WrapMode |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /** |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 98 | * For the filters which perform more than one texture sample (convolution, |
| 99 | * erode, dilate), this determines the direction in which the texture |
| 100 | * coordinates will be incremented. |
| 101 | */ |
| 102 | enum FilterDirection { |
| 103 | kX_FilterDirection, |
| 104 | kY_FilterDirection, |
| 105 | |
| 106 | kDefault_FilterDirection = kX_FilterDirection, |
| 107 | }; |
| 108 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 109 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 110 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 111 | */ |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 112 | GrSamplerState() |
| 113 | : fRadial2CenterX1() |
| 114 | , fRadial2Radius0() |
| 115 | , fRadial2PosRoot() { |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 116 | this->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | } |
| 118 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | WrapMode getWrapX() const { return fWrapX; } |
| 120 | WrapMode getWrapY() const { return fWrapY; } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 121 | FilterDirection getFilterDirection() const { return fFilterDirection; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | SampleMode getSampleMode() const { return fSampleMode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 123 | const GrMatrix& getMatrix() const { return fMatrix; } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 124 | const GrRect& getTextureDomain() const { return fTextureDomain; } |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 125 | bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();} |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 126 | Filter getFilter() const { return fFilter; } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 127 | int getKernelWidth() const { return fKernelWidth; } |
| 128 | const float* getKernel() const { return fKernel; } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 129 | bool swapsRAndB() const { return fSwapRAndB; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | |
| 131 | bool isGradient() const { |
| 132 | return kRadial_SampleMode == fSampleMode || |
| 133 | kRadial2_SampleMode == fSampleMode || |
| 134 | kSweep_SampleMode == fSampleMode; |
| 135 | } |
| 136 | |
| 137 | void setWrapX(WrapMode mode) { fWrapX = mode; } |
| 138 | void setWrapY(WrapMode mode) { fWrapY = mode; } |
| 139 | void setSampleMode(SampleMode mode) { fSampleMode = mode; } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 140 | void setFilterDirection(FilterDirection mode) { fFilterDirection = mode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 143 | * Access the sampler's matrix. See SampleMode for explanation of |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 144 | * relationship between the matrix and sample mode. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 145 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 146 | GrMatrix* matrix() { return &fMatrix; } |
| 147 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 148 | /** |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 149 | * Sets the sampler's texture coordinate domain to a |
| 150 | * custom rectangle, rather than the default (0,1). |
| 151 | * This option is currently only supported with kClamp_WrapMode |
| 152 | */ |
| 153 | void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; } |
| 154 | |
| 155 | /** |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 156 | * Swaps the R and B components when reading from the texture. Has no effect |
| 157 | * if the texture is alpha only. |
| 158 | */ |
| 159 | void setRAndBSwap(bool swap) { fSwapRAndB = swap; } |
| 160 | |
| 161 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 162 | * Multiplies the current sampler matrix a matrix |
| 163 | * |
| 164 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 165 | * to this function, and M' is the new matrix. (We consider points to |
| 166 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 167 | * t' = X*t.) |
| 168 | * |
| 169 | * @param matrix the matrix used to modify the matrix. |
| 170 | */ |
| 171 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 172 | |
| 173 | /** |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 174 | * Sets filtering type. |
| 175 | * @param filter type of filtering to apply |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 176 | */ |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 177 | void setFilter(Filter filter) { fFilter = filter; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 179 | void reset(WrapMode wrapXAndY, |
| 180 | Filter filter, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 181 | FilterDirection direction, |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 182 | const GrMatrix& matrix) { |
| 183 | fWrapX = wrapXAndY; |
| 184 | fWrapY = wrapXAndY; |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 185 | fSampleMode = kDefault_SampleMode; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 186 | fFilter = filter; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 187 | fFilterDirection = direction; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 188 | fMatrix = matrix; |
| 189 | fTextureDomain.setEmpty(); |
| 190 | fSwapRAndB = false; |
| 191 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 192 | void reset(WrapMode wrapXAndY, Filter filter, const GrMatrix& matrix) { |
| 193 | this->reset(wrapXAndY, filter, kDefault_FilterDirection, matrix); |
| 194 | } |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 195 | void reset(WrapMode wrapXAndY, |
| 196 | Filter filter) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 197 | this->reset(wrapXAndY, filter, kDefault_FilterDirection, GrMatrix::I()); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 198 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 199 | void reset(const GrMatrix& matrix) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 200 | this->reset(kDefault_WrapMode, kDefault_Filter, kDefault_FilterDirection, matrix); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 201 | } |
| 202 | void reset() { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 203 | this->reset(kDefault_WrapMode, kDefault_Filter, kDefault_FilterDirection, GrMatrix::I()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 204 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 205 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; } |
| 207 | GrScalar getRadial2Radius0() const { return fRadial2Radius0; } |
bsalomon@google.com | e7160bf | 2011-11-10 15:28:16 +0000 | [diff] [blame] | 208 | bool isRadial2PosRoot() const { return SkToBool(fRadial2PosRoot); } |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 209 | // do the radial gradient params lead to a linear (rather than quadratic) |
| 210 | // equation. |
| 211 | bool radial2IsDegenerate() const { return GR_Scalar1 == fRadial2CenterX1; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 214 | * Sets the parameters for kRadial2_SampleMode. The texture |
| 215 | * matrix must be set so that the first point is at (0,0) and the second |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | * point lies on the x-axis. The second radius minus the first is 1 unit. |
| 217 | * The additional parameters to define the gradient are specified by this |
| 218 | * function. |
| 219 | */ |
| 220 | void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) { |
| 221 | fRadial2CenterX1 = centerX1; |
| 222 | fRadial2Radius0 = radius0; |
| 223 | fRadial2PosRoot = posRoot; |
| 224 | } |
| 225 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 226 | void setConvolutionParams(int kernelWidth, const float* kernel) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 227 | GrAssert(kernelWidth >= 0 && kernelWidth <= MAX_KERNEL_WIDTH); |
| 228 | fKernelWidth = kernelWidth; |
| 229 | if (NULL != kernel) { |
| 230 | memcpy(fKernel, kernel, kernelWidth * sizeof(float)); |
| 231 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 232 | } |
| 233 | |
| 234 | void setMorphologyRadius(int radius) { |
| 235 | GrAssert(radius >= 0 && radius <= MAX_KERNEL_WIDTH); |
| 236 | fKernelWidth = radius; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 237 | } |
| 238 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | private: |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 240 | WrapMode fWrapX : 8; |
| 241 | WrapMode fWrapY : 8; |
| 242 | FilterDirection fFilterDirection : 8; |
| 243 | SampleMode fSampleMode : 8; |
| 244 | Filter fFilter : 8; |
| 245 | GrMatrix fMatrix; |
| 246 | bool fSwapRAndB; |
| 247 | GrRect fTextureDomain; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | |
| 249 | // these are undefined unless fSampleMode == kRadial2_SampleMode |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 250 | GrScalar fRadial2CenterX1; |
| 251 | GrScalar fRadial2Radius0; |
| 252 | SkBool8 fRadial2PosRoot; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 254 | // These are undefined unless fFilter == kConvolution_Filter |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 255 | uint8_t fKernelWidth; |
| 256 | float fKernel[MAX_KERNEL_WIDTH]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | #endif |
| 260 | |