blob: f01d832bd5b2496ef35363c10fa133bd86528a19 [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
bsalomon@google.com77af6802013-10-02 13:04:56 +000011#include "SkMatrix.h"
Robert Phillips18166ee2017-06-01 12:55:44 -040012#include "GrSurfaceProxyPriv.h"
Robert Phillips18166ee2017-06-01 12:55:44 -040013#include "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 Phillips18166ee2017-06-01 12:55:44 -040024 : fProxy(nullptr)
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050025 , fNormalize(false)
Brian Osman5869ea92017-04-03 16:36:58 -040026 , fReverseY(false) {
Robert Phillips67c18d62017-01-20 12:44:06 -050027 SkDEBUGCODE(fInProcessor = false);
28 }
bsalomon@google.com77af6802013-10-02 13:04:56 +000029
Brian Salomonffc2ec42017-07-25 14:59:03 -040030 GrCoordTransform(const GrCoordTransform&) = default;
31
bsalomon@google.com77af6802013-10-02 13:04:56 +000032 /**
Brian Osman5869ea92017-04-03 16:36:58 -040033 * 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.com77af6802013-10-02 13:04:56 +000035 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040036 GrCoordTransform(GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050037 SkASSERT(proxy);
38 SkDEBUGCODE(fInProcessor = false);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040039 this->reset(SkMatrix::I(), proxy, true);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050040 }
41
bsalomon@google.com77af6802013-10-02 13:04:56 +000042 /**
Brian Osman5869ea92017-04-03 16:36:58 -040043 * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
44 * should be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000045 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040046 GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050047 SkASSERT(proxy);
48 SkDEBUGCODE(fInProcessor = false);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040049 this->reset(m, proxy, true);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050050 }
51
bsalomon17168df2014-12-09 09:00:49 -080052 /**
53 * Create a transformation that applies the matrix to a coord set.
54 */
Brian Osman5869ea92017-04-03 16:36:58 -040055 GrCoordTransform(const SkMatrix& m) {
bsalomon17168df2014-12-09 09:00:49 -080056 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040057 this->reset(m);
bsalomon17168df2014-12-09 09:00:49 -080058 }
59
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040060 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 Phillipsbc7a4fb2017-01-23 15:30:35 -050069
Brian Osman5869ea92017-04-03 16:36:58 -040070 void reset(const SkMatrix& m) {
Robert Phillips67c18d62017-01-20 12:44:06 -050071 SkASSERT(!fInProcessor);
72 fMatrix = m;
Robert Phillips18166ee2017-06-01 12:55:44 -040073 fProxy = nullptr;
Robert Phillips67c18d62017-01-20 12:44:06 -050074 fNormalize = false;
75 fReverseY = false;
Robert Phillips67c18d62017-01-20 12:44:06 -050076 }
Joe Gregorioa7d61a62017-01-17 19:20:54 +000077
bsalomon17168df2014-12-09 09:00:49 -080078 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070079 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080080 fMatrix = that.fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -040081 fProxy = that.fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -050082 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080083 fReverseY = that.fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000084 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() {
bsalomonf2765412014-10-15 18:34:46 -070092 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000093 return &fMatrix;
94 }
95
Robert Phillips67c18d62017-01-20 12:44:06 -050096 bool hasSameEffectAs(const GrCoordTransform& that) const {
97 if (fNormalize != that.fNormalize ||
98 fReverseY != that.fReverseY ||
Robert Phillips67c18d62017-01-20 12:44:06 -050099 !fMatrix.cheapEqualTo(that.fMatrix)) {
100 return false;
101 }
102
103 if (fNormalize) {
Robert Phillips159e3c62017-06-19 12:02:00 -0400104 if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400105 return false;
106 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500107 }
108
109 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000110 }
111
bsalomon@google.com77af6802013-10-02 13:04:56 +0000112 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips18166ee2017-06-01 12:55:44 -0400113 const GrTextureProxy* proxy() const { return fProxy; }
Robert Phillips67c18d62017-01-20 12:44:06 -0500114 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000115 bool reverseY() const { return fReverseY; }
116
Robert Phillips18166ee2017-06-01 12:55:44 -0400117 // This should only ever be called at flush time after the backing texture has been
118 // successfully instantiated
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400119 GrTexture* peekTexture() const { return fProxy->peekTexture(); }
Robert Phillips18166ee2017-06-01 12:55:44 -0400120
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000121private:
Robert Phillips67c18d62017-01-20 12:44:06 -0500122 // The textures' effect is to optionally normalize the final matrix, so a blind
123 // equality check could be misleading
124 bool operator==(const GrCoordTransform& that) const;
125 bool operator!=(const GrCoordTransform& that) const;
126
bsalomon17168df2014-12-09 09:00:49 -0800127 SkMatrix fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -0400128 const GrTextureProxy* fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -0500129 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800130 bool fReverseY;
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