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