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 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 11 | #ifndef GrEffectStage_DEFINED |
| 12 | #define GrEffectStage_DEFINED |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 14 | #include "GrBackendEffectFactory.h" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 15 | #include "GrEffect.h" |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 16 | #include "SkMatrix.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 17 | #include "GrTypes.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 19 | #include "SkShader.h" |
| 20 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 21 | class GrEffectStage { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | public: |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 23 | explicit GrEffectStage(const GrEffect* effect, int attrIndex0 = -1, int attrIndex1 = -1) |
| 24 | : fEffect(SkRef(effect)) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 25 | fCoordChangeMatrixSet = false; |
| 26 | fVertexAttribIndices[0] = attrIndex0; |
| 27 | fVertexAttribIndices[1] = attrIndex1; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 28 | } |
| 29 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 30 | GrEffectStage(const GrEffectStage& other) { |
| 31 | *this = other; |
| 32 | } |
| 33 | |
| 34 | GrEffectStage& operator= (const GrEffectStage& other) { |
| 35 | fCoordChangeMatrixSet = other.fCoordChangeMatrixSet; |
| 36 | if (other.fCoordChangeMatrixSet) { |
| 37 | fCoordChangeMatrix = other.fCoordChangeMatrix; |
| 38 | } |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 39 | fEffect.reset(SkRef(other.fEffect.get())); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 40 | memcpy(fVertexAttribIndices, other.fVertexAttribIndices, sizeof(fVertexAttribIndices)); |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | bool operator== (const GrEffectStage& other) const { |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 45 | SkASSERT(NULL != fEffect.get()); |
| 46 | SkASSERT(NULL != other.fEffect.get()); |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 47 | |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 48 | if (!this->getEffect()->isEqual(*other.getEffect())) { |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 52 | if (fCoordChangeMatrixSet != other.fCoordChangeMatrixSet) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (!fCoordChangeMatrixSet) { |
| 57 | return true; |
| 58 | } |
| 59 | |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 60 | return fCoordChangeMatrix == other.fCoordChangeMatrix; |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 61 | } |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 62 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 63 | bool operator!= (const GrEffectStage& s) const { return !(*this == s); } |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 65 | /** |
| 66 | * This is called when the coordinate system in which the geometry is specified will change. |
| 67 | * |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 68 | * @param matrix The transformation from the old coord system in which geometry is specified |
| 69 | * to the new one from which it will actually be drawn. |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 70 | */ |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 71 | void localCoordChange(const SkMatrix& matrix) { |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 72 | if (fCoordChangeMatrixSet) { |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 73 | fCoordChangeMatrix.preConcat(matrix); |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 74 | } else { |
| 75 | fCoordChangeMatrixSet = true; |
| 76 | fCoordChangeMatrix = matrix; |
| 77 | } |
| 78 | } |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 79 | |
| 80 | class SavedCoordChange { |
| 81 | private: |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 82 | bool fCoordChangeMatrixSet; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 83 | SkMatrix fCoordChangeMatrix; |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 84 | SkDEBUGCODE(mutable SkAutoTUnref<const GrEffect> fEffect;) |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 85 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 86 | friend class GrEffectStage; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * This gets the current coordinate system change. It is the accumulation of |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 91 | * localCoordChange calls since the effect was installed. It is used when then caller |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 92 | * wants to temporarily change the source geometry coord system, draw something, and then |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 93 | * restore the previous coord system (e.g. temporarily draw in device coords). |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 94 | */ |
| 95 | void saveCoordChange(SavedCoordChange* savedCoordChange) const { |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 96 | savedCoordChange->fCoordChangeMatrixSet = fCoordChangeMatrixSet; |
| 97 | if (fCoordChangeMatrixSet) { |
| 98 | savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix; |
| 99 | } |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 100 | SkASSERT(NULL == savedCoordChange->fEffect.get()); |
| 101 | SkDEBUGCODE(SkRef(fEffect.get());) |
| 102 | SkDEBUGCODE(savedCoordChange->fEffect.reset(fEffect.get());) |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * This balances the saveCoordChange call. |
| 107 | */ |
| 108 | void restoreCoordChange(const SavedCoordChange& savedCoordChange) { |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 109 | fCoordChangeMatrixSet = savedCoordChange.fCoordChangeMatrixSet; |
| 110 | if (fCoordChangeMatrixSet) { |
| 111 | fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix; |
| 112 | } |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 113 | SkASSERT(savedCoordChange.fEffect.get() == fEffect); |
| 114 | SkDEBUGCODE(savedCoordChange.fEffect.reset(NULL);) |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 118 | * Gets the matrix representing all changes of coordinate system since the GrEffect was |
| 119 | * installed in the stage. |
| 120 | */ |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 121 | const SkMatrix& getCoordChangeMatrix() const { |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 122 | if (fCoordChangeMatrixSet) { |
| 123 | return fCoordChangeMatrix; |
| 124 | } else { |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 125 | return SkMatrix::I(); |
robertphillips@google.com | 6ef912a | 2013-04-25 17:23:46 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 128 | |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 129 | const GrEffect* getEffect() const { return fEffect.get(); } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 130 | |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 131 | const int* getVertexAttribIndices() const { return fVertexAttribIndices; } |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 132 | int getVertexAttribIndexCount() const { return fEffect->numVertexAttribs(); } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 133 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | private: |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 135 | bool fCoordChangeMatrixSet; |
| 136 | SkMatrix fCoordChangeMatrix; |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame^] | 137 | SkAutoTUnref<const GrEffect> fEffect; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 138 | int fVertexAttribIndices[2]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #endif |