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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | class GrSamplerState { |
| 21 | public: |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 22 | static const bool kBilerpDefault = false; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 24 | static const SkShader::TileMode kTileModeDefault = SkShader::kClamp_TileMode; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 27 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 28 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | */ |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 30 | GrSamplerState() |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 31 | : fCustomStage (NULL) { |
tomhudson@google.com | 194de08 | 2012-05-31 20:35:27 +0000 | [diff] [blame] | 32 | memset(this, 0, sizeof(GrSamplerState)); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 33 | this->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | } |
| 35 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 36 | ~GrSamplerState() { |
| 37 | GrSafeUnref(fCustomStage); |
| 38 | } |
| 39 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 40 | bool operator ==(const GrSamplerState& s) const { |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 41 | /* We must be bit-identical as far as the CustomStage; |
| 42 | there may be multiple CustomStages that will produce |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | the same shader code and so are equivalent. |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 44 | Can't take the address of fWrapX because it's :8 */ |
| 45 | int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this; |
| 46 | GrAssert(sizeof(GrSamplerState) == |
| 47 | bitwiseRegion + sizeof(fCustomStage)); |
| 48 | return !memcmp(this, &s, bitwiseRegion) && |
| 49 | ((fCustomStage == s.fCustomStage) || |
| 50 | (fCustomStage && s.fCustomStage && |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 51 | (fCustomStage->getFactory() == |
| 52 | s.fCustomStage->getFactory()) && |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 53 | fCustomStage->isEqual(*s.fCustomStage))); |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 54 | } |
| 55 | bool operator !=(const GrSamplerState& s) const { return !(*this == s); } |
| 56 | |
tomhudson@google.com | 0bdbed3 | 2012-06-01 19:50:02 +0000 | [diff] [blame] | 57 | GrSamplerState& operator =(const GrSamplerState& s) { |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 58 | fMatrix = s.fMatrix; |
tomhudson@google.com | 50e4ce0 | 2012-06-19 15:27:50 +0000 | [diff] [blame] | 59 | GrSafeAssign(fCustomStage, s.fCustomStage); |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 60 | return *this; |
| 61 | } |
| 62 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 63 | const GrMatrix& getMatrix() const { return fMatrix; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 65 | /** |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 66 | * Access the sampler's matrix. See SampleMode for explanation of |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 67 | * relationship between the matrix and sample mode. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 68 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 69 | GrMatrix* matrix() { return &fMatrix; } |
| 70 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Multiplies the current sampler matrix a matrix |
| 73 | * |
| 74 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 75 | * to this function, and M' is the new matrix. (We consider points to |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 76 | * be column vectors so tex cood vector t is transformed by matrix X as |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 77 | * t' = X*t.) |
| 78 | * |
| 79 | * @param matrix the matrix used to modify the matrix. |
| 80 | */ |
| 81 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 82 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame^] | 83 | void reset(const GrMatrix& matrix) { |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 84 | fMatrix = matrix; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 85 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 86 | } |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame^] | 87 | |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 88 | void reset() { |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame^] | 89 | fMatrix.reset(); |
| 90 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 91 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 92 | |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 93 | GrCustomStage* setCustomStage(GrCustomStage* stage) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 94 | GrSafeAssign(fCustomStage, stage); |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 95 | return stage; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 96 | } |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 97 | const GrCustomStage* getCustomStage() const { return fCustomStage; } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 98 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | private: |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 100 | GrMatrix fMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 102 | GrCustomStage* fCustomStage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | #endif |
| 106 | |