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