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 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 11 | #include "SkMatrix.h" |
| 12 | #include "GrTexture.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 13 | |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 14 | class GrResourceProvider; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 15 | class GrTextureProxy; |
| 16 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 17 | /** |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 18 | * A class representing a linear transformation of local coordinates. GrFragnentProcessors |
| 19 | * these transformations, and the GrGeometryProcessor implements the transformation. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 21 | class GrCoordTransform : SkNoncopyable { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 22 | public: |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 23 | GrCoordTransform() |
| 24 | : fTexture(nullptr) |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 25 | , fNormalize(false) |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 26 | , fReverseY(false) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 27 | SkDEBUGCODE(fInProcessor = false); |
| 28 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 31 | * Create a transformation that maps [0, 1] to a proxy's boundaries. The proxy origin also |
| 32 | * implies whether a y-reversal should be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 33 | */ |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 34 | GrCoordTransform(GrResourceProvider* resourceProvider, GrTextureProxy* proxy) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 35 | SkASSERT(proxy); |
| 36 | SkDEBUGCODE(fInProcessor = false); |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 37 | this->reset(resourceProvider, SkMatrix::I(), proxy); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 38 | } |
| 39 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 40 | /** |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 41 | * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal |
| 42 | * should be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 43 | */ |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 44 | GrCoordTransform(GrResourceProvider* resourceProvider, const SkMatrix& m, |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 45 | GrTextureProxy* proxy) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 46 | SkASSERT(proxy); |
| 47 | SkDEBUGCODE(fInProcessor = false); |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 48 | this->reset(resourceProvider, m, proxy); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 49 | } |
| 50 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Create a transformation that applies the matrix to a coord set. |
| 53 | */ |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 54 | GrCoordTransform(const SkMatrix& m) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 55 | SkDEBUGCODE(fInProcessor = false); |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 56 | this->reset(m); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 59 | void reset(GrResourceProvider*, const SkMatrix&, GrTextureProxy*, bool normalize = true); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 60 | |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame^] | 61 | void reset(const SkMatrix& m) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 62 | SkASSERT(!fInProcessor); |
| 63 | fMatrix = m; |
| 64 | fTexture = nullptr; |
| 65 | fNormalize = false; |
| 66 | fReverseY = false; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 67 | } |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 68 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 69 | GrCoordTransform& operator= (const GrCoordTransform& that) { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 70 | SkASSERT(!fInProcessor); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 71 | fMatrix = that.fMatrix; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 72 | fTexture = that.fTexture; |
| 73 | fNormalize = that.fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 74 | fReverseY = that.fReverseY; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Access the matrix for editing. Note, this must be done before adding the transform to an |
| 80 | * effect, since effects are immutable. |
| 81 | */ |
| 82 | SkMatrix* accessMatrix() { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 83 | SkASSERT(!fInProcessor); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 84 | return &fMatrix; |
| 85 | } |
| 86 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 87 | bool hasSameEffectAs(const GrCoordTransform& that) const { |
| 88 | if (fNormalize != that.fNormalize || |
| 89 | fReverseY != that.fReverseY || |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 90 | !fMatrix.cheapEqualTo(that.fMatrix)) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (fNormalize) { |
| 95 | SkASSERT(fTexture && that.fTexture); |
| 96 | return fTexture->width() == that.fTexture->width() && |
| 97 | fTexture->height() == that.fTexture->height(); |
| 98 | } |
| 99 | |
| 100 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 103 | const SkMatrix& getMatrix() const { return fMatrix; } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 104 | const GrTexture* texture() const { return fTexture; } |
| 105 | bool normalize() const { return fNormalize; } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 106 | bool reverseY() const { return fReverseY; } |
| 107 | |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 108 | private: |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 109 | // The textures' effect is to optionally normalize the final matrix, so a blind |
| 110 | // equality check could be misleading |
| 111 | bool operator==(const GrCoordTransform& that) const; |
| 112 | bool operator!=(const GrCoordTransform& that) const; |
| 113 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 114 | SkMatrix fMatrix; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 115 | const GrTexture* fTexture; |
| 116 | bool fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 117 | bool fReverseY; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 118 | typedef SkNoncopyable INHERITED; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 119 | |
| 120 | #ifdef SK_DEBUG |
| 121 | public: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 122 | void setInProcessor() const { fInProcessor = true; } |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 123 | private: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 124 | mutable bool fInProcessor; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 125 | #endif |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | #endif |