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 | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 22 | |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 23 | GrSamplerState() |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 24 | : fCustomStage (NULL) { |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 25 | GR_DEBUGCODE(fSavedCoordChangeCnt = 0;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | } |
| 27 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 28 | ~GrSamplerState() { |
| 29 | GrSafeUnref(fCustomStage); |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 30 | GrAssert(0 == fSavedCoordChangeCnt); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 31 | } |
| 32 | |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 33 | bool operator ==(const GrSamplerState& other) const { |
| 34 | // first handle cases where one or the other has no custom stage |
| 35 | if (NULL == fCustomStage) { |
| 36 | return NULL == other.fCustomStage; |
| 37 | } else if (NULL == other.fCustomStage) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | if (fCustomStage->getFactory() != other.fCustomStage->getFactory()) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if (!fCustomStage->isEqual(*other.fCustomStage)) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | return fMatrix == other.fMatrix && fCoordChangeMatrix == other.fCoordChangeMatrix; |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 50 | } |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 51 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 52 | bool operator !=(const GrSamplerState& s) const { return !(*this == s); } |
| 53 | |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 54 | GrSamplerState& operator =(const GrSamplerState& other) { |
| 55 | GrSafeAssign(fCustomStage, other.fCustomStage); |
| 56 | if (NULL != fCustomStage) { |
| 57 | fMatrix = other.fMatrix; |
| 58 | fCoordChangeMatrix = other.fCoordChangeMatrix; |
| 59 | } |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 60 | return *this; |
| 61 | } |
| 62 | |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 63 | /** |
| 64 | * This is called when the coordinate system in which the geometry is specified will change. |
| 65 | * |
| 66 | * @param matrix The transformation from the old coord system to the new one. |
| 67 | */ |
| 68 | void preConcatCoordChange(const GrMatrix& matrix) { fCoordChangeMatrix.preConcat(matrix); } |
| 69 | |
| 70 | class SavedCoordChange { |
| 71 | private: |
| 72 | GrMatrix fCoordChangeMatrix; |
| 73 | GR_DEBUGCODE(mutable SkAutoTUnref<GrCustomStage> fCustomStage;) |
| 74 | |
| 75 | friend class GrSamplerState; |
| 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * This gets the current coordinate system change. It is the accumulation of |
| 80 | * preConcatCoordChange calls since the custom stage was installed. It is used when then caller |
| 81 | * wants to temporarily change the source geometry coord system, draw something, and then |
| 82 | * restore the previous coord system (e.g. temporarily draw in device coords).s |
| 83 | */ |
| 84 | void saveCoordChange(SavedCoordChange* savedCoordChange) const { |
| 85 | savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix; |
| 86 | GrAssert(NULL == savedCoordChange->fCustomStage.get()); |
| 87 | GR_DEBUGCODE(GrSafeRef(fCustomStage);) |
| 88 | GR_DEBUGCODE(savedCoordChange->fCustomStage.reset(fCustomStage);) |
| 89 | GR_DEBUGCODE(++fSavedCoordChangeCnt); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This balances the saveCoordChange call. |
| 94 | */ |
| 95 | void restoreCoordChange(const SavedCoordChange& savedCoordChange) { |
| 96 | fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix; |
| 97 | GrAssert(savedCoordChange.fCustomStage.get() == fCustomStage); |
| 98 | GR_DEBUGCODE(--fSavedCoordChangeCnt); |
| 99 | GR_DEBUGCODE(savedCoordChange.fCustomStage.reset(NULL);) |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Gets the texture matrix. This is will be removed soon and be managed by GrCustomStage. |
| 104 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 105 | const GrMatrix& getMatrix() const { return fMatrix; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 107 | /** |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 108 | * Gets the matrix to apply at draw time. This is the original texture matrix combined with |
| 109 | * any coord system changes. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 110 | */ |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 111 | void getTotalMatrix(GrMatrix* matrix) const { |
| 112 | *matrix = fMatrix; |
| 113 | matrix->preConcat(fCoordChangeMatrix); |
| 114 | } |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 116 | void reset() { |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 117 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 118 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 119 | |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 120 | GrCustomStage* setCustomStage(GrCustomStage* stage) { |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 121 | GrAssert(0 == fSavedCoordChangeCnt); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 122 | GrSafeAssign(fCustomStage, stage); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 123 | fMatrix.reset(); |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 124 | fCoordChangeMatrix.reset(); |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 125 | return stage; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 126 | } |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 127 | |
| 128 | GrCustomStage* setCustomStage(GrCustomStage* stage, const GrMatrix& matrix) { |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 129 | GrAssert(0 == fSavedCoordChangeCnt); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 130 | GrSafeAssign(fCustomStage, stage); |
| 131 | fMatrix = matrix; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 132 | fCoordChangeMatrix.reset(); |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 133 | return stage; |
| 134 | } |
| 135 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 136 | const GrCustomStage* getCustomStage() const { return fCustomStage; } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 137 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 138 | private: |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 139 | GrMatrix fCoordChangeMatrix; |
| 140 | GrMatrix fMatrix; // TODO: remove this, store in GrCustomStage |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 141 | GrCustomStage* fCustomStage; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 142 | |
| 143 | GR_DEBUGCODE(mutable int fSavedCoordChangeCnt;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | #endif |
| 147 | |