blob: aee2253ea411f01e1cb1ec74ac67ef35a767928f [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 Phillips94ade752018-10-09 12:32:31 -040024 : fProxy(nullptr)
25 , fNormalize(false)
26 , 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);
Brian Salomon246bc3d2018-12-06 15:33:02 -050039 this->reset(SkMatrix::I(), proxy);
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);
Brian Salomon246bc3d2018-12-06 15:33:02 -050049 this->reset(m, proxy);
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
bsalomon17168df2014-12-09 09:00:49 -080060 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070061 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080062 fMatrix = that.fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -040063 fProxy = that.fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -050064 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080065 fReverseY = that.fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000066 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() {
bsalomonf2765412014-10-15 18:34:46 -070074 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000075 return &fMatrix;
76 }
77
Robert Phillips67c18d62017-01-20 12:44:06 -050078 bool hasSameEffectAs(const GrCoordTransform& that) const {
79 if (fNormalize != that.fNormalize ||
80 fReverseY != that.fReverseY ||
Robert Phillips67c18d62017-01-20 12:44:06 -050081 !fMatrix.cheapEqualTo(that.fMatrix)) {
82 return false;
83 }
84
85 if (fNormalize) {
Robert Phillips159e3c62017-06-19 12:02:00 -040086 if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) {
Robert Phillips18166ee2017-06-01 12:55:44 -040087 return false;
88 }
Robert Phillips67c18d62017-01-20 12:44:06 -050089 }
90
91 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +000092 }
93
bsalomon@google.com77af6802013-10-02 13:04:56 +000094 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips18166ee2017-06-01 12:55:44 -040095 const GrTextureProxy* proxy() const { return fProxy; }
Robert Phillips67c18d62017-01-20 12:44:06 -050096 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +000097 bool reverseY() const { return fReverseY; }
98
Robert Phillips18166ee2017-06-01 12:55:44 -040099 // This should only ever be called at flush time after the backing texture has been
100 // successfully instantiated
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400101 GrTexture* peekTexture() const { return fProxy->peekTexture(); }
Robert Phillips18166ee2017-06-01 12:55:44 -0400102
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000103private:
Brian Salomon246bc3d2018-12-06 15:33:02 -0500104 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 Phillips67c18d62017-01-20 12:44:06 -0500113 // 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
bsalomon17168df2014-12-09 09:00:49 -0800118 SkMatrix fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -0400119 const GrTextureProxy* fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -0500120 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800121 bool fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000122
123#ifdef SK_DEBUG
124public:
bsalomonf2765412014-10-15 18:34:46 -0700125 void setInProcessor() const { fInProcessor = true; }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000126private:
bsalomonf2765412014-10-15 18:34:46 -0700127 mutable bool fInProcessor;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000128#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +0000129};
130
131#endif