blob: a4d7d3565d395d7c4f50d833e17ab21c357e692b [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
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000012#include "SkMatrix.h"
13#include "GrTexture.h"
14#include "GrTypes.h"
bsalomon17168df2014-12-09 09:00:49 -080015#include "GrShaderVar.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000016
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050017class GrTextureProxy;
18
bsalomon@google.com77af6802013-10-02 13:04:56 +000019/**
Brian Salomon2ebd0c82016-10-03 17:15:28 -040020 * A class representing a linear transformation of local coordinates. GrFragnentProcessors
21 * these transformations, and the GrGeometryProcessor implements the transformation.
bsalomon@google.com77af6802013-10-02 13:04:56 +000022 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000023class GrCoordTransform : SkNoncopyable {
bsalomon@google.com77af6802013-10-02 13:04:56 +000024public:
Robert Phillips67c18d62017-01-20 12:44:06 -050025 GrCoordTransform()
26 : fTexture(nullptr)
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050027 , fNormalize(false)
28 , fReverseY(false)
29 , fPrecision(kDefault_GrSLPrecision) {
Robert Phillips67c18d62017-01-20 12:44:06 -050030 SkDEBUGCODE(fInProcessor = false);
31 }
bsalomon@google.com77af6802013-10-02 13:04:56 +000032
33 /**
bsalomon17168df2014-12-09 09:00:49 -080034 * Create a transformation that maps [0, 1] to a texture's boundaries. The precision is inferred
bsalomon9f876a32014-12-09 10:51:07 -080035 * from the texture size and filter. The texture origin also implies whether a y-reversal should
36 * be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000037 */
Brian Salomon514baff2016-11-17 15:17:07 -050038 GrCoordTransform(const GrTexture* texture, GrSamplerParams::FilterMode filter) {
bsalomon17168df2014-12-09 09:00:49 -080039 SkASSERT(texture);
bsalomonf2765412014-10-15 18:34:46 -070040 SkDEBUGCODE(fInProcessor = false);
Robert Phillips67c18d62017-01-20 12:44:06 -050041 this->reset(SkMatrix::I(), texture, filter);
bsalomon@google.com77af6802013-10-02 13:04:56 +000042 }
43
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050044 GrCoordTransform(GrContext* context, GrTextureProxy* proxy,
45 GrSamplerParams::FilterMode filter) {
46 SkASSERT(proxy);
47 SkDEBUGCODE(fInProcessor = false);
48 this->reset(context, SkMatrix::I(), proxy, filter);
49 }
50
bsalomon@google.com77af6802013-10-02 13:04:56 +000051 /**
bsalomon9f876a32014-12-09 10:51:07 -080052 * Create a transformation from a matrix. The precision is inferred from the texture size and
53 * filter. The texture origin also implies whether a y-reversal should be performed.
bsalomon@google.com77af6802013-10-02 13:04:56 +000054 */
Brian Salomon2ebd0c82016-10-03 17:15:28 -040055 GrCoordTransform(const SkMatrix& m, const GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -050056 GrSamplerParams::FilterMode filter) {
bsalomon17168df2014-12-09 09:00:49 -080057 SkASSERT(texture);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050058 SkDEBUGCODE(fInProcessor = false);
Brian Salomon2ebd0c82016-10-03 17:15:28 -040059 this->reset(m, texture, filter);
bsalomon@google.com77af6802013-10-02 13:04:56 +000060 }
61
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050062 GrCoordTransform(GrContext* context, const SkMatrix& m, GrTextureProxy* proxy,
63 GrSamplerParams::FilterMode filter) {
64 SkASSERT(proxy);
65 SkDEBUGCODE(fInProcessor = false);
66 this->reset(context, m, proxy, filter);
67 }
68
bsalomon17168df2014-12-09 09:00:49 -080069 /**
70 * Create a transformation that applies the matrix to a coord set.
71 */
Brian Salomon2ebd0c82016-10-03 17:15:28 -040072 GrCoordTransform(const SkMatrix& m, GrSLPrecision precision = kDefault_GrSLPrecision) {
bsalomon17168df2014-12-09 09:00:49 -080073 SkDEBUGCODE(fInProcessor = false);
Brian Salomon2ebd0c82016-10-03 17:15:28 -040074 this->reset(m, precision);
bsalomon17168df2014-12-09 09:00:49 -080075 }
76
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050077 // MDB TODO: rm the GrTexture* flavor of reset
Robert Phillips67c18d62017-01-20 12:44:06 -050078 void reset(const SkMatrix&, const GrTexture*, GrSamplerParams::FilterMode filter,
79 bool normalize = true);
bsalomon@google.com77af6802013-10-02 13:04:56 +000080
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050081 void reset(GrContext* context, const SkMatrix&, GrTextureProxy*,
82 GrSamplerParams::FilterMode filter, bool normalize = true);
83
Robert Phillips67c18d62017-01-20 12:44:06 -050084 void reset(const SkMatrix& m, GrSLPrecision precision = kDefault_GrSLPrecision) {
85 SkASSERT(!fInProcessor);
86 fMatrix = m;
87 fTexture = nullptr;
88 fNormalize = false;
89 fReverseY = false;
90 fPrecision = precision;
91 }
Joe Gregorioa7d61a62017-01-17 19:20:54 +000092
bsalomon17168df2014-12-09 09:00:49 -080093 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070094 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080095 fMatrix = that.fMatrix;
Robert Phillips67c18d62017-01-20 12:44:06 -050096 fTexture = that.fTexture;
97 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080098 fReverseY = that.fReverseY;
99 fPrecision = that.fPrecision;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000100 return *this;
101 }
102
103 /**
104 * Access the matrix for editing. Note, this must be done before adding the transform to an
105 * effect, since effects are immutable.
106 */
107 SkMatrix* accessMatrix() {
bsalomonf2765412014-10-15 18:34:46 -0700108 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000109 return &fMatrix;
110 }
111
Robert Phillips67c18d62017-01-20 12:44:06 -0500112 bool hasSameEffectAs(const GrCoordTransform& that) const {
113 if (fNormalize != that.fNormalize ||
114 fReverseY != that.fReverseY ||
115 fPrecision != that.fPrecision ||
116 !fMatrix.cheapEqualTo(that.fMatrix)) {
117 return false;
118 }
119
120 if (fNormalize) {
121 SkASSERT(fTexture && that.fTexture);
122 return fTexture->width() == that.fTexture->width() &&
123 fTexture->height() == that.fTexture->height();
124 }
125
126 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000127 }
128
bsalomon@google.com77af6802013-10-02 13:04:56 +0000129 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips67c18d62017-01-20 12:44:06 -0500130 const GrTexture* texture() const { return fTexture; }
131 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000132 bool reverseY() const { return fReverseY; }
bsalomonc0bd6482014-12-09 10:04:14 -0800133 GrSLPrecision precision() const { return fPrecision; }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000134
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000135private:
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 // The textures' effect is to optionally normalize the final matrix, so a blind
137 // equality check could be misleading
138 bool operator==(const GrCoordTransform& that) const;
139 bool operator!=(const GrCoordTransform& that) const;
140
bsalomon17168df2014-12-09 09:00:49 -0800141 SkMatrix fMatrix;
Robert Phillips67c18d62017-01-20 12:44:06 -0500142 const GrTexture* fTexture;
143 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800144 bool fReverseY;
bsalomonc0bd6482014-12-09 10:04:14 -0800145 GrSLPrecision fPrecision;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000146 typedef SkNoncopyable INHERITED;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000147
148#ifdef SK_DEBUG
149public:
bsalomonf2765412014-10-15 18:34:46 -0700150 void setInProcessor() const { fInProcessor = true; }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000151private:
bsalomonf2765412014-10-15 18:34:46 -0700152 mutable bool fInProcessor;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000153#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +0000154};
155
156#endif