blob: 2451363d3803ab22cc1ee54a0d12d2202eb8c60d [file] [log] [blame]
bsalomon@google.com77af6802013-10-02 13:04:56 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrTextureProxy.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000014
Robert Phillips646e4292017-06-13 12:44:56 -040015class GrTexture;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050016
bsalomon@google.com77af6802013-10-02 13:04:56 +000017/**
Brian Salomon2ebd0c82016-10-03 17:15:28 -040018 * A class representing a linear transformation of local coordinates. GrFragnentProcessors
19 * these transformations, and the GrGeometryProcessor implements the transformation.
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 */
Brian Salomonffc2ec42017-07-25 14:59:03 -040021class GrCoordTransform {
bsalomon@google.com77af6802013-10-02 13:04:56 +000022public:
Robert Phillips67c18d62017-01-20 12:44:06 -050023 GrCoordTransform()
Robert Phillips94ade752018-10-09 12:32:31 -040024 : fProxy(nullptr)
25 , fNormalize(false)
Ethan Nicholasd4efe682019-08-29 16:10:13 -040026 , fReverseY(false)
27 , fComputeInVertexShader(true) {
Robert Phillips67c18d62017-01-20 12:44:06 -050028 SkDEBUGCODE(fInProcessor = false);
29 }
bsalomon@google.com77af6802013-10-02 13:04:56 +000030
Brian Salomonffc2ec42017-07-25 14:59:03 -040031 GrCoordTransform(const GrCoordTransform&) = default;
32
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 /**
Brian Osman5869ea92017-04-03 16:36:58 -040034 * 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.com77af6802013-10-02 13:04:56 +000036 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040037 GrCoordTransform(GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050038 SkASSERT(proxy);
39 SkDEBUGCODE(fInProcessor = false);
Brian Salomon246bc3d2018-12-06 15:33:02 -050040 this->reset(SkMatrix::I(), proxy);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050041 }
42
bsalomon@google.com77af6802013-10-02 13:04:56 +000043 /**
Brian Osman5869ea92017-04-03 16:36:58 -040044 * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
45 * should be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000046 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040047 GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050048 SkASSERT(proxy);
49 SkDEBUGCODE(fInProcessor = false);
Brian Salomon246bc3d2018-12-06 15:33:02 -050050 this->reset(m, proxy);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050051 }
52
bsalomon17168df2014-12-09 09:00:49 -080053 /**
54 * Create a transformation that applies the matrix to a coord set.
55 */
Brian Osman5869ea92017-04-03 16:36:58 -040056 GrCoordTransform(const SkMatrix& m) {
bsalomon17168df2014-12-09 09:00:49 -080057 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040058 this->reset(m);
bsalomon17168df2014-12-09 09:00:49 -080059 }
60
bsalomon17168df2014-12-09 09:00:49 -080061 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070062 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080063 fMatrix = that.fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -040064 fProxy = that.fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -050065 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080066 fReverseY = that.fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000067 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() {
bsalomonf2765412014-10-15 18:34:46 -070075 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000076 return &fMatrix;
77 }
78
Robert Phillips67c18d62017-01-20 12:44:06 -050079 bool hasSameEffectAs(const GrCoordTransform& that) const {
80 if (fNormalize != that.fNormalize ||
81 fReverseY != that.fReverseY ||
Robert Phillips67c18d62017-01-20 12:44:06 -050082 !fMatrix.cheapEqualTo(that.fMatrix)) {
83 return false;
84 }
85
86 if (fNormalize) {
Robert Phillips159e3c62017-06-19 12:02:00 -040087 if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) {
Robert Phillips18166ee2017-06-01 12:55:44 -040088 return false;
89 }
Robert Phillips67c18d62017-01-20 12:44:06 -050090 }
91
92 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +000093 }
94
bsalomon@google.com77af6802013-10-02 13:04:56 +000095 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips18166ee2017-06-01 12:55:44 -040096 const GrTextureProxy* proxy() const { return fProxy; }
Robert Phillips67c18d62017-01-20 12:44:06 -050097 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +000098 bool reverseY() const { return fReverseY; }
99
Robert Phillips18166ee2017-06-01 12:55:44 -0400100 // This should only ever be called at flush time after the backing texture has been
101 // successfully instantiated
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400102 GrTexture* peekTexture() const { return fProxy->peekTexture(); }
Robert Phillips18166ee2017-06-01 12:55:44 -0400103
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400104 bool computeInVertexShader() const { return fComputeInVertexShader; }
105
106 void setComputeInVertexShader(bool computeInVertexShader) {
107 fComputeInVertexShader = computeInVertexShader;
108 }
109
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000110private:
Brian Salomon246bc3d2018-12-06 15:33:02 -0500111 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 Nicholasd4efe682019-08-29 16:10:13 -0400118 fComputeInVertexShader = true;
Brian Salomon246bc3d2018-12-06 15:33:02 -0500119 }
120
Robert Phillips67c18d62017-01-20 12:44:06 -0500121 // 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
bsalomon17168df2014-12-09 09:00:49 -0800126 SkMatrix fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -0400127 const GrTextureProxy* fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -0500128 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800129 bool fReverseY;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400130 bool fComputeInVertexShader;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000131
132#ifdef SK_DEBUG
133public:
bsalomonf2765412014-10-15 18:34:46 -0700134 void setInProcessor() const { fInProcessor = true; }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000135private:
bsalomonf2765412014-10-15 18:34:46 -0700136 mutable bool fInProcessor;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000137#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +0000138};
139
140#endif