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 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 14 | #include "GrCustomStage.h" |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 15 | #include "GrMatrix.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 16 | #include "GrTypes.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 18 | #include "SkShader.h" |
| 19 | |
| 20 | class GrTextureParams { |
| 21 | public: |
| 22 | GrTextureParams() { |
| 23 | this->reset(); |
| 24 | } |
| 25 | |
| 26 | GrTextureParams(const GrTextureParams& params) { |
| 27 | *this = params; |
| 28 | } |
| 29 | |
| 30 | GrTextureParams& operator =(const GrTextureParams& params) { |
| 31 | fTileModes[0] = params.fTileModes[0]; |
| 32 | fTileModes[1] = params.fTileModes[1]; |
| 33 | fBilerp = params.fBilerp; |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | void reset() { |
| 38 | this->reset(SkShader::kClamp_TileMode, false); |
| 39 | } |
| 40 | |
| 41 | void reset(SkShader::TileMode tileXAndY, bool filter) { |
| 42 | fTileModes[0] = fTileModes[1] = tileXAndY; |
| 43 | fBilerp = filter; |
| 44 | } |
| 45 | void reset(SkShader::TileMode tileModes[2], bool filter) { |
| 46 | fTileModes[0] = tileModes[0]; |
| 47 | fTileModes[1] = tileModes[1]; |
| 48 | fBilerp = filter; |
| 49 | } |
| 50 | |
| 51 | void setClampNoFilter() { |
| 52 | fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode; |
| 53 | fBilerp = false; |
| 54 | } |
| 55 | |
| 56 | void setClamp() { |
| 57 | fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode; |
| 58 | } |
| 59 | |
| 60 | void setBilerp(bool bilerp) { fBilerp = bilerp; } |
| 61 | |
| 62 | void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; } |
| 63 | void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; } |
| 64 | void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; } |
| 65 | |
| 66 | SkShader::TileMode getTileModeX() const { return fTileModes[0]; } |
| 67 | |
| 68 | SkShader::TileMode getTileModeY() const { return fTileModes[1]; } |
| 69 | |
| 70 | bool isTiled() const { |
| 71 | return SkShader::kClamp_TileMode != fTileModes[0] || |
| 72 | SkShader::kClamp_TileMode != fTileModes[1]; |
| 73 | } |
| 74 | |
| 75 | bool isBilerp() const { return fBilerp; } |
| 76 | |
| 77 | private: |
| 78 | |
| 79 | SkShader::TileMode fTileModes[2]; |
| 80 | bool fBilerp; |
| 81 | }; |
| 82 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | class GrSamplerState { |
| 84 | public: |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 85 | static const bool kBilerpDefault = false; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 87 | static const SkShader::TileMode kTileModeDefault = SkShader::kClamp_TileMode; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 90 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 91 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | */ |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 93 | GrSamplerState() |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 94 | : fCustomStage (NULL) { |
tomhudson@google.com | 194de08 | 2012-05-31 20:35:27 +0000 | [diff] [blame] | 95 | memset(this, 0, sizeof(GrSamplerState)); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 96 | this->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 99 | ~GrSamplerState() { |
| 100 | GrSafeUnref(fCustomStage); |
| 101 | } |
| 102 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 103 | bool operator ==(const GrSamplerState& s) const { |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 104 | /* We must be bit-identical as far as the CustomStage; |
| 105 | there may be multiple CustomStages that will produce |
| 106 | the same shader code and so are equivalent. |
| 107 | Can't take the address of fWrapX because it's :8 */ |
| 108 | int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this; |
| 109 | GrAssert(sizeof(GrSamplerState) == |
| 110 | bitwiseRegion + sizeof(fCustomStage)); |
| 111 | return !memcmp(this, &s, bitwiseRegion) && |
| 112 | ((fCustomStage == s.fCustomStage) || |
| 113 | (fCustomStage && s.fCustomStage && |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 114 | (fCustomStage->getFactory() == |
| 115 | s.fCustomStage->getFactory()) && |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 116 | fCustomStage->isEqual(*s.fCustomStage))); |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 117 | } |
| 118 | bool operator !=(const GrSamplerState& s) const { return !(*this == s); } |
| 119 | |
tomhudson@google.com | 0bdbed3 | 2012-06-01 19:50:02 +0000 | [diff] [blame] | 120 | GrSamplerState& operator =(const GrSamplerState& s) { |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 121 | // memcpy() breaks refcounting |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 122 | fTextureParams = s.fTextureParams; |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 123 | fMatrix = s.fMatrix; |
| 124 | fSwapRAndB = s.fSwapRAndB; |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 125 | |
tomhudson@google.com | 50e4ce0 | 2012-06-19 15:27:50 +0000 | [diff] [blame] | 126 | GrSafeAssign(fCustomStage, s.fCustomStage); |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 127 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 128 | return *this; |
| 129 | } |
| 130 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 131 | const GrMatrix& getMatrix() const { return fMatrix; } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 132 | bool swapsRAndB() const { return fSwapRAndB; } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 133 | bool premultiply() const { return fPremultiply; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 135 | GrTextureParams* textureParams() { return &fTextureParams; } |
| 136 | const GrTextureParams& getTextureParams() const { return fTextureParams; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 137 | /** |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 138 | * Access the sampler's matrix. See SampleMode for explanation of |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 139 | * relationship between the matrix and sample mode. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 140 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 141 | GrMatrix* matrix() { return &fMatrix; } |
| 142 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 143 | /** |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 144 | * Swaps the R and B components when reading from the texture. Has no effect |
| 145 | * if the texture is alpha only. |
| 146 | */ |
| 147 | void setRAndBSwap(bool swap) { fSwapRAndB = swap; } |
| 148 | |
| 149 | /** |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 150 | * If the texture is RGBA/BGRA 8888 config then its rgb components will be |
| 151 | * multiplied by its a component after the texture read. |
| 152 | **/ |
| 153 | void setPremultiply(bool premul) { fPremultiply = premul; } |
| 154 | |
| 155 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 156 | * Multiplies the current sampler matrix a matrix |
| 157 | * |
| 158 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 159 | * to this function, and M' is the new matrix. (We consider points to |
| 160 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 161 | * t' = X*t.) |
| 162 | * |
| 163 | * @param matrix the matrix used to modify the matrix. |
| 164 | */ |
| 165 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 166 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 167 | void reset(SkShader::TileMode tileXAndY, |
| 168 | bool filter, |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 169 | const GrMatrix& matrix) { |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 170 | fTextureParams.reset(tileXAndY, filter); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 171 | fMatrix = matrix; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 172 | fSwapRAndB = false; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 173 | fPremultiply = false; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 174 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 175 | } |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 176 | void reset(SkShader::TileMode wrapXAndY, bool filter) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 177 | this->reset(wrapXAndY, filter, GrMatrix::I()); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 178 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 179 | void reset(const GrMatrix& matrix) { |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 180 | this->reset(kTileModeDefault, kBilerpDefault, matrix); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 181 | } |
| 182 | void reset() { |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 183 | this->reset(kTileModeDefault, kBilerpDefault, GrMatrix::I()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 184 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 185 | |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 186 | GrCustomStage* setCustomStage(GrCustomStage* stage) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 187 | GrSafeAssign(fCustomStage, stage); |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 188 | return stage; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 189 | } |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 190 | const GrCustomStage* getCustomStage() const { return fCustomStage; } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 191 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 192 | private: |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 193 | GrTextureParams fTextureParams; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 194 | bool fSwapRAndB; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 195 | bool fPremultiply; // temporary, will be replaced soon by a custom stage. |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 196 | GrMatrix fMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 198 | GrCustomStage* fCustomStage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | #endif |
| 202 | |