blob: 54c21b7472a3905b3f13c4367ae5a3637d447846 [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"
12#include "GrTexture.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000013
Brian Osman5869ea92017-04-03 16:36:58 -040014class GrResourceProvider;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050015class GrTextureProxy;
16
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()
24 : fTexture(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 */
Brian Osman5869ea92017-04-03 16:36:58 -040034 GrCoordTransform(GrResourceProvider* resourceProvider, GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050035 SkASSERT(proxy);
36 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040037 this->reset(resourceProvider, SkMatrix::I(), proxy);
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 Phillips296b1cc2017-03-15 10:42:12 -040044 GrCoordTransform(GrResourceProvider* resourceProvider, const SkMatrix& m,
Brian Osman5869ea92017-04-03 16:36:58 -040045 GrTextureProxy* proxy) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050046 SkASSERT(proxy);
47 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040048 this->reset(resourceProvider, m, proxy);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050049 }
50
bsalomon17168df2014-12-09 09:00:49 -080051 /**
52 * Create a transformation that applies the matrix to a coord set.
53 */
Brian Osman5869ea92017-04-03 16:36:58 -040054 GrCoordTransform(const SkMatrix& m) {
bsalomon17168df2014-12-09 09:00:49 -080055 SkDEBUGCODE(fInProcessor = false);
Brian Osman5869ea92017-04-03 16:36:58 -040056 this->reset(m);
bsalomon17168df2014-12-09 09:00:49 -080057 }
58
Brian Osman5869ea92017-04-03 16:36:58 -040059 void reset(GrResourceProvider*, const SkMatrix&, GrTextureProxy*, bool normalize = true);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050060
Brian Osman5869ea92017-04-03 16:36:58 -040061 void reset(const SkMatrix& m) {
Robert Phillips67c18d62017-01-20 12:44:06 -050062 SkASSERT(!fInProcessor);
63 fMatrix = m;
64 fTexture = nullptr;
65 fNormalize = false;
66 fReverseY = false;
Robert Phillips67c18d62017-01-20 12:44:06 -050067 }
Joe Gregorioa7d61a62017-01-17 19:20:54 +000068
bsalomon17168df2014-12-09 09:00:49 -080069 GrCoordTransform& operator= (const GrCoordTransform& that) {
bsalomonf2765412014-10-15 18:34:46 -070070 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080071 fMatrix = that.fMatrix;
Robert Phillips67c18d62017-01-20 12:44:06 -050072 fTexture = that.fTexture;
73 fNormalize = that.fNormalize;
bsalomon17168df2014-12-09 09:00:49 -080074 fReverseY = that.fReverseY;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000075 return *this;
76 }
77
78 /**
79 * Access the matrix for editing. Note, this must be done before adding the transform to an
80 * effect, since effects are immutable.
81 */
82 SkMatrix* accessMatrix() {
bsalomonf2765412014-10-15 18:34:46 -070083 SkASSERT(!fInProcessor);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000084 return &fMatrix;
85 }
86
Robert Phillips67c18d62017-01-20 12:44:06 -050087 bool hasSameEffectAs(const GrCoordTransform& that) const {
88 if (fNormalize != that.fNormalize ||
89 fReverseY != that.fReverseY ||
Robert Phillips67c18d62017-01-20 12:44:06 -050090 !fMatrix.cheapEqualTo(that.fMatrix)) {
91 return false;
92 }
93
94 if (fNormalize) {
95 SkASSERT(fTexture && that.fTexture);
96 return fTexture->width() == that.fTexture->width() &&
97 fTexture->height() == that.fTexture->height();
98 }
99
100 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000101 }
102
bsalomon@google.com77af6802013-10-02 13:04:56 +0000103 const SkMatrix& getMatrix() const { return fMatrix; }
Robert Phillips67c18d62017-01-20 12:44:06 -0500104 const GrTexture* texture() const { return fTexture; }
105 bool normalize() const { return fNormalize; }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000106 bool reverseY() const { return fReverseY; }
107
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000108private:
Robert Phillips67c18d62017-01-20 12:44:06 -0500109 // The textures' effect is to optionally normalize the final matrix, so a blind
110 // equality check could be misleading
111 bool operator==(const GrCoordTransform& that) const;
112 bool operator!=(const GrCoordTransform& that) const;
113
bsalomon17168df2014-12-09 09:00:49 -0800114 SkMatrix fMatrix;
Robert Phillips67c18d62017-01-20 12:44:06 -0500115 const GrTexture* fTexture;
116 bool fNormalize;
bsalomon17168df2014-12-09 09:00:49 -0800117 bool fReverseY;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000118 typedef SkNoncopyable INHERITED;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000119
120#ifdef SK_DEBUG
121public:
bsalomonf2765412014-10-15 18:34:46 -0700122 void setInProcessor() const { fInProcessor = true; }
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000123private:
bsalomonf2765412014-10-15 18:34:46 -0700124 mutable bool fInProcessor;
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000125#endif
bsalomon@google.com77af6802013-10-02 13:04:56 +0000126};
127
128#endif