blob: 3ba4fd8ab678582d50c7e34f004164caa4ea0f8e [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 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000021class GrCoordTransform : SkNoncopyable {
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
30 /**
Brian Osman5869ea92017-04-03 16:36:58 -040031 * Create a transformation that maps [0, 1] to a proxy's boundaries. The proxy origin also
32 * implies whether a y-reversal should be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040034 GrCoordTransform(GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050035 SkASSERT(proxy);
36 SkDEBUGCODE(fInProcessor = false);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040037 this->reset(SkMatrix::I(), proxy, true);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050038 }
39
bsalomon@google.com77af6802013-10-02 13:04:56 +000040 /**
Brian Osman5869ea92017-04-03 16:36:58 -040041 * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
42 * should be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000043 */
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040044 GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050045 SkASSERT(proxy);
46 SkDEBUGCODE(fInProcessor = false);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040047 this->reset(m, proxy, true);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050048 }
49
bsalomon17168df2014-12-09 09:00:49 -080050 /**
51 * Create a transformation that applies the matrix to a coord set.
52 */
Brian Osman5869ea92017-04-03 16:36:58 -040053 GrCoordTransform(const SkMatrix& m) {
bsalomon17168df2014-12-09 09:00:49 -080054 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040055 this->reset(m);
bsalomon17168df2014-12-09 09:00:49 -080056 }
57
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040058 void reset(const SkMatrix& m, GrTextureProxy* proxy, bool normalize) {
59 SkASSERT(proxy);
60 SkASSERT(!fInProcessor);
61
62 fMatrix = m;
63 fProxy = proxy;
64 fNormalize = normalize;
65 fReverseY = kBottomLeft_GrSurfaceOrigin == proxy->origin();
66 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050067
Brian Osman5869ea92017-04-03 16:36:58 -040068 void reset(const SkMatrix& m) {
Robert Phillips67c18d62017-01-20 12:44:06 -050069 SkASSERT(!fInProcessor);
70 fMatrix = m;
Robert Phillips18166ee2017-06-01 12:55:44 -040071 fProxy = nullptr;
Robert Phillips67c18d62017-01-20 12:44:06 -050072 fNormalize = false;
73 fReverseY = false;
Robert Phillips67c18d62017-01-20 12:44:06 -050074 }
Joe Gregorioa7d61a62017-01-17 19:20:54 +000075
bsalomon17168df2014-12-09 09:00:49 -080076 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070077 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080078 fMatrix = that.fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -040079 fProxy = that.fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -050080 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080081 fReverseY = that.fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000082 return *this;
83 }
84
85 /**
86 * Access the matrix for editing. Note, this must be done before adding the transform to an
87 * effect, since effects are immutable.
88 */
89 SkMatrix* accessMatrix() {
bsalomonf2765412014-10-15 18:34:46 -070090 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000091 return &fMatrix;
92 }
93
Robert Phillips67c18d62017-01-20 12:44:06 -050094 bool hasSameEffectAs(const GrCoordTransform& that) const {
95 if (fNormalize != that.fNormalize ||
96 fReverseY != that.fReverseY ||
Robert Phillips67c18d62017-01-20 12:44:06 -050097 !fMatrix.cheapEqualTo(that.fMatrix)) {
98 return false;
99 }
100
101 if (fNormalize) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400102 if (fProxy != that.fProxy) {
103 return false;
104 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500105 }
106
107 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000108 }
109
bsalomon@google.com77af6802013-10-02 13:04:56 +0000110 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips18166ee2017-06-01 12:55:44 -0400111 const GrTextureProxy* proxy() const { return fProxy; }
Robert Phillips67c18d62017-01-20 12:44:06 -0500112 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000113 bool reverseY() const { return fReverseY; }
114
Robert Phillips18166ee2017-06-01 12:55:44 -0400115 // This should only ever be called at flush time after the backing texture has been
116 // successfully instantiated
117 GrTexture* peekTexture() const {
118 SkASSERT(fProxy->priv().peekTexture());
119 return fProxy->priv().peekTexture();
120 }
121
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000122private:
Robert Phillips67c18d62017-01-20 12:44:06 -0500123 // The textures' effect is to optionally normalize the final matrix, so a blind
124 // equality check could be misleading
125 bool operator==(const GrCoordTransform& that) const;
126 bool operator!=(const GrCoordTransform& that) const;
127
bsalomon17168df2014-12-09 09:00:49 -0800128 SkMatrix fMatrix;
Robert Phillips18166ee2017-06-01 12:55:44 -0400129 const GrTextureProxy* fProxy;
Robert Phillips67c18d62017-01-20 12:44:06 -0500130 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800131 bool fReverseY;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000132 typedef SkNoncopyable INHERITED;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000133
134#ifdef SK_DEBUG
135public:
bsalomonf2765412014-10-15 18:34:46 -0700136 void setInProcessor() const { fInProcessor = true; }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000137private:
bsalomonf2765412014-10-15 18:34:46 -0700138 mutable bool fInProcessor;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000139#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +0000140};
141
142#endif