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" |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 12 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 13 | #include "GrTextureProxy.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 14 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 15 | class GrTexture; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 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 | */ |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 21 | class GrCoordTransform { |
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() |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 24 | : fProxy(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 | |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 30 | GrCoordTransform(const GrCoordTransform&) = default; |
| 31 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 32 | /** |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame] | 33 | * Create a transformation that maps [0, 1] to a proxy's boundaries. The proxy origin also |
| 34 | * implies whether a y-reversal should be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 35 | */ |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 36 | GrCoordTransform(GrTextureProxy* proxy) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 37 | SkASSERT(proxy); |
| 38 | SkDEBUGCODE(fInProcessor = false); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 39 | this->reset(SkMatrix::I(), proxy, true); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 40 | } |
| 41 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 42 | /** |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame] | 43 | * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal |
| 44 | * should be performed. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 45 | */ |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 46 | GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 47 | SkASSERT(proxy); |
| 48 | SkDEBUGCODE(fInProcessor = false); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 49 | this->reset(m, proxy, true); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Create a transformation that applies the matrix to a coord set. |
| 54 | */ |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame] | 55 | GrCoordTransform(const SkMatrix& m) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 56 | SkDEBUGCODE(fInProcessor = false); |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame] | 57 | this->reset(m); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 60 | void reset(const SkMatrix& m, GrTextureProxy* proxy, bool normalize) { |
| 61 | SkASSERT(proxy); |
| 62 | SkASSERT(!fInProcessor); |
| 63 | |
| 64 | fMatrix = m; |
| 65 | fProxy = proxy; |
| 66 | fNormalize = normalize; |
| 67 | fReverseY = kBottomLeft_GrSurfaceOrigin == proxy->origin(); |
| 68 | } |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 69 | |
Brian Osman | 5869ea9 | 2017-04-03 16:36:58 -0400 | [diff] [blame] | 70 | void reset(const SkMatrix& m) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 71 | SkASSERT(!fInProcessor); |
| 72 | fMatrix = m; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 73 | fProxy = nullptr; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 74 | fNormalize = false; |
| 75 | fReverseY = false; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 76 | } |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 77 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 78 | GrCoordTransform& operator= (const GrCoordTransform& that) { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 79 | SkASSERT(!fInProcessor); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 80 | fMatrix = that.fMatrix; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 81 | fProxy = that.fProxy; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 82 | fNormalize = that.fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 83 | fReverseY = that.fReverseY; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Access the matrix for editing. Note, this must be done before adding the transform to an |
| 89 | * effect, since effects are immutable. |
| 90 | */ |
| 91 | SkMatrix* accessMatrix() { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 92 | SkASSERT(!fInProcessor); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 93 | return &fMatrix; |
| 94 | } |
| 95 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 96 | bool hasSameEffectAs(const GrCoordTransform& that) const { |
| 97 | if (fNormalize != that.fNormalize || |
| 98 | fReverseY != that.fReverseY || |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 99 | !fMatrix.cheapEqualTo(that.fMatrix)) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (fNormalize) { |
Robert Phillips | 159e3c6 | 2017-06-19 12:02:00 -0400 | [diff] [blame] | 104 | if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 105 | return false; |
| 106 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 110 | } |
| 111 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 112 | const SkMatrix& getMatrix() const { return fMatrix; } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 113 | const GrTextureProxy* proxy() const { return fProxy; } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 114 | bool normalize() const { return fNormalize; } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 115 | bool reverseY() const { return fReverseY; } |
| 116 | |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 117 | // This should only ever be called at flush time after the backing texture has been |
| 118 | // successfully instantiated |
| 119 | GrTexture* peekTexture() const { |
| 120 | SkASSERT(fProxy->priv().peekTexture()); |
| 121 | return fProxy->priv().peekTexture(); |
| 122 | } |
| 123 | |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 124 | private: |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 125 | // The textures' effect is to optionally normalize the final matrix, so a blind |
| 126 | // equality check could be misleading |
| 127 | bool operator==(const GrCoordTransform& that) const; |
| 128 | bool operator!=(const GrCoordTransform& that) const; |
| 129 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 130 | SkMatrix fMatrix; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 131 | const GrTextureProxy* fProxy; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 132 | bool fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 133 | bool fReverseY; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 134 | |
| 135 | #ifdef SK_DEBUG |
| 136 | public: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 137 | void setInProcessor() const { fInProcessor = true; } |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 138 | private: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 139 | mutable bool fInProcessor; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 140 | #endif |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #endif |