bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrCoordTransform_DEFINED |
| 9 | #define GrCoordTransform_DEFINED |
| 10 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 12 | #include "SkMatrix.h" |
| 13 | #include "GrTexture.h" |
| 14 | #include "GrTypes.h" |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 15 | #include "GrShaderVar.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 16 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 17 | class GrTextureProxy; |
| 18 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 19 | /** |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 20 | * A class representing a linear transformation of local coordinates. GrFragnentProcessors |
| 21 | * these transformations, and the GrGeometryProcessor implements the transformation. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 22 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 23 | class GrCoordTransform : SkNoncopyable { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 24 | public: |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 25 | GrCoordTransform() |
| 26 | : fTexture(nullptr) |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 27 | , fNormalize(false) |
| 28 | , fReverseY(false) |
| 29 | , fPrecision(kDefault_GrSLPrecision) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 30 | SkDEBUGCODE(fInProcessor = false); |
| 31 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 34 | * Create a transformation that maps [0, 1] to a texture's boundaries. The precision is inferred |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 35 | * from the texture size and filter. The texture origin also implies whether a y-reversal should |
| 36 | * be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 37 | */ |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 38 | GrCoordTransform(const GrTexture* texture, GrSamplerParams::FilterMode filter) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 39 | SkASSERT(texture); |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 40 | SkDEBUGCODE(fInProcessor = false); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 41 | this->reset(SkMatrix::I(), texture, filter); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 44 | GrCoordTransform(GrContext* context, GrTextureProxy* proxy, |
| 45 | GrSamplerParams::FilterMode filter) { |
| 46 | SkASSERT(proxy); |
| 47 | SkDEBUGCODE(fInProcessor = false); |
| 48 | this->reset(context, SkMatrix::I(), proxy, filter); |
| 49 | } |
| 50 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 51 | /** |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 52 | * Create a transformation from a matrix. The precision is inferred from the texture size and |
| 53 | * filter. The texture origin also implies whether a y-reversal should be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 54 | */ |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 55 | GrCoordTransform(const SkMatrix& m, const GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 56 | GrSamplerParams::FilterMode filter) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 57 | SkASSERT(texture); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 58 | SkDEBUGCODE(fInProcessor = false); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 59 | this->reset(m, texture, filter); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 62 | GrCoordTransform(GrContext* context, const SkMatrix& m, GrTextureProxy* proxy, |
| 63 | GrSamplerParams::FilterMode filter) { |
| 64 | SkASSERT(proxy); |
| 65 | SkDEBUGCODE(fInProcessor = false); |
| 66 | this->reset(context, m, proxy, filter); |
| 67 | } |
| 68 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Create a transformation that applies the matrix to a coord set. |
| 71 | */ |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 72 | GrCoordTransform(const SkMatrix& m, GrSLPrecision precision = kDefault_GrSLPrecision) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 73 | SkDEBUGCODE(fInProcessor = false); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 74 | this->reset(m, precision); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 77 | // MDB TODO: rm the GrTexture* flavor of reset |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 78 | void reset(const SkMatrix&, const GrTexture*, GrSamplerParams::FilterMode filter, |
| 79 | bool normalize = true); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 80 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame^] | 81 | void reset(GrContext* context, const SkMatrix&, GrTextureProxy*, |
| 82 | GrSamplerParams::FilterMode filter, bool normalize = true); |
| 83 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 84 | void reset(const SkMatrix& m, GrSLPrecision precision = kDefault_GrSLPrecision) { |
| 85 | SkASSERT(!fInProcessor); |
| 86 | fMatrix = m; |
| 87 | fTexture = nullptr; |
| 88 | fNormalize = false; |
| 89 | fReverseY = false; |
| 90 | fPrecision = precision; |
| 91 | } |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 92 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 93 | GrCoordTransform& operator= (const GrCoordTransform& that) { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 94 | SkASSERT(!fInProcessor); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 95 | fMatrix = that.fMatrix; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 96 | fTexture = that.fTexture; |
| 97 | fNormalize = that.fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 98 | fReverseY = that.fReverseY; |
| 99 | fPrecision = that.fPrecision; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Access the matrix for editing. Note, this must be done before adding the transform to an |
| 105 | * effect, since effects are immutable. |
| 106 | */ |
| 107 | SkMatrix* accessMatrix() { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 108 | SkASSERT(!fInProcessor); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 109 | return &fMatrix; |
| 110 | } |
| 111 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 112 | bool hasSameEffectAs(const GrCoordTransform& that) const { |
| 113 | if (fNormalize != that.fNormalize || |
| 114 | fReverseY != that.fReverseY || |
| 115 | fPrecision != that.fPrecision || |
| 116 | !fMatrix.cheapEqualTo(that.fMatrix)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (fNormalize) { |
| 121 | SkASSERT(fTexture && that.fTexture); |
| 122 | return fTexture->width() == that.fTexture->width() && |
| 123 | fTexture->height() == that.fTexture->height(); |
| 124 | } |
| 125 | |
| 126 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 129 | const SkMatrix& getMatrix() const { return fMatrix; } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 130 | const GrTexture* texture() const { return fTexture; } |
| 131 | bool normalize() const { return fNormalize; } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 132 | bool reverseY() const { return fReverseY; } |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 133 | GrSLPrecision precision() const { return fPrecision; } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 134 | |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 135 | private: |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 136 | // The textures' effect is to optionally normalize the final matrix, so a blind |
| 137 | // equality check could be misleading |
| 138 | bool operator==(const GrCoordTransform& that) const; |
| 139 | bool operator!=(const GrCoordTransform& that) const; |
| 140 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 141 | SkMatrix fMatrix; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 142 | const GrTexture* fTexture; |
| 143 | bool fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 144 | bool fReverseY; |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 145 | GrSLPrecision fPrecision; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 146 | typedef SkNoncopyable INHERITED; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 147 | |
| 148 | #ifdef SK_DEBUG |
| 149 | public: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 150 | void setInProcessor() const { fInProcessor = true; } |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 151 | private: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 152 | mutable bool fInProcessor; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 153 | #endif |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #endif |