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 | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 24 | : fProxy(nullptr) |
| 25 | , fNormalize(false) |
| 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); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 39 | this->reset(SkMatrix::I(), proxy); |
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); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 49 | this->reset(m, proxy); |
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 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 60 | GrCoordTransform& operator= (const GrCoordTransform& that) { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 61 | SkASSERT(!fInProcessor); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 62 | fMatrix = that.fMatrix; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 63 | fProxy = that.fProxy; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 64 | fNormalize = that.fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 65 | fReverseY = that.fReverseY; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Access the matrix for editing. Note, this must be done before adding the transform to an |
| 71 | * effect, since effects are immutable. |
| 72 | */ |
| 73 | SkMatrix* accessMatrix() { |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 74 | SkASSERT(!fInProcessor); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 75 | return &fMatrix; |
| 76 | } |
| 77 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 78 | bool hasSameEffectAs(const GrCoordTransform& that) const { |
| 79 | if (fNormalize != that.fNormalize || |
| 80 | fReverseY != that.fReverseY || |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 81 | !fMatrix.cheapEqualTo(that.fMatrix)) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | if (fNormalize) { |
Robert Phillips | 159e3c6 | 2017-06-19 12:02:00 -0400 | [diff] [blame] | 86 | if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 87 | return false; |
| 88 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 94 | const SkMatrix& getMatrix() const { return fMatrix; } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 95 | const GrTextureProxy* proxy() const { return fProxy; } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 96 | bool normalize() const { return fNormalize; } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 97 | bool reverseY() const { return fReverseY; } |
| 98 | |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 99 | // This should only ever be called at flush time after the backing texture has been |
| 100 | // successfully instantiated |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 101 | GrTexture* peekTexture() const { return fProxy->peekTexture(); } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 102 | |
Joe Gregorio | a7d61a6 | 2017-01-17 19:20:54 +0000 | [diff] [blame] | 103 | private: |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 104 | void reset(const SkMatrix& m, GrTextureProxy* proxy = nullptr) { |
| 105 | SkASSERT(!fInProcessor); |
| 106 | |
| 107 | fMatrix = m; |
| 108 | fProxy = proxy; |
| 109 | fNormalize = proxy && proxy->textureType() != GrTextureType::kRectangle; |
| 110 | fReverseY = proxy && kBottomLeft_GrSurfaceOrigin == proxy->origin(); |
| 111 | } |
| 112 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 113 | // The textures' effect is to optionally normalize the final matrix, so a blind |
| 114 | // equality check could be misleading |
| 115 | bool operator==(const GrCoordTransform& that) const; |
| 116 | bool operator!=(const GrCoordTransform& that) const; |
| 117 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 118 | SkMatrix fMatrix; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 119 | const GrTextureProxy* fProxy; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 120 | bool fNormalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 121 | bool fReverseY; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef SK_DEBUG |
| 124 | public: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 125 | void setInProcessor() const { fInProcessor = true; } |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 126 | private: |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 127 | mutable bool fInProcessor; |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 128 | #endif |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | #endif |