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