• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "SkMatrix.h"
12 #include "GrSurfaceProxyPriv.h"
13 #include "GrTextureProxy.h"
14 
15 class GrTexture;
16 
17 /**
18  * A class representing a linear transformation of local coordinates. GrFragnentProcessors
19  * these transformations, and the GrGeometryProcessor implements the transformation.
20  */
21 class GrCoordTransform {
22 public:
GrCoordTransform()23     GrCoordTransform()
24         : fProxy(nullptr)
25         , fNormalize(false)
26         , fReverseY(false) {
27         SkDEBUGCODE(fInProcessor = false);
28     }
29 
30     GrCoordTransform(const GrCoordTransform&) = default;
31 
32     /**
33      * 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.
35      */
GrCoordTransform(GrTextureProxy * proxy)36     GrCoordTransform(GrTextureProxy* proxy) {
37         SkASSERT(proxy);
38         SkDEBUGCODE(fInProcessor = false);
39         this->reset(SkMatrix::I(), proxy, true);
40     }
41 
42     /**
43      * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
44      * should be performed.
45      */
GrCoordTransform(const SkMatrix & m,GrTextureProxy * proxy)46     GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) {
47         SkASSERT(proxy);
48         SkDEBUGCODE(fInProcessor = false);
49         this->reset(m, proxy, true);
50     }
51 
52     /**
53      * Create a transformation that applies the matrix to a coord set.
54      */
GrCoordTransform(const SkMatrix & m)55     GrCoordTransform(const SkMatrix& m) {
56         SkDEBUGCODE(fInProcessor = false);
57         this->reset(m);
58     }
59 
reset(const SkMatrix & m,GrTextureProxy * proxy,bool normalize)60     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     }
69 
reset(const SkMatrix & m)70     void reset(const SkMatrix& m) {
71         SkASSERT(!fInProcessor);
72         fMatrix = m;
73         fProxy = nullptr;
74         fNormalize = false;
75         fReverseY = false;
76     }
77 
78     GrCoordTransform& operator= (const GrCoordTransform& that) {
79         SkASSERT(!fInProcessor);
80         fMatrix = that.fMatrix;
81         fProxy = that.fProxy;
82         fNormalize = that.fNormalize;
83         fReverseY = that.fReverseY;
84         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      */
accessMatrix()91     SkMatrix* accessMatrix() {
92         SkASSERT(!fInProcessor);
93         return &fMatrix;
94     }
95 
hasSameEffectAs(const GrCoordTransform & that)96     bool hasSameEffectAs(const GrCoordTransform& that) const {
97         if (fNormalize != that.fNormalize ||
98             fReverseY != that.fReverseY ||
99             !fMatrix.cheapEqualTo(that.fMatrix)) {
100             return false;
101         }
102 
103         if (fNormalize) {
104             if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) {
105                 return false;
106             }
107         }
108 
109         return true;
110     }
111 
getMatrix()112     const SkMatrix& getMatrix() const { return fMatrix; }
proxy()113     const GrTextureProxy* proxy() const { return fProxy; }
normalize()114     bool normalize() const { return fNormalize; }
reverseY()115     bool reverseY() const { return fReverseY; }
116 
117     // This should only ever be called at flush time after the backing texture has been
118     // successfully instantiated
peekTexture()119     GrTexture* peekTexture() const {
120         SkASSERT(fProxy->priv().peekTexture());
121         return fProxy->priv().peekTexture();
122     }
123 
124 private:
125     // The textures' effect is to optionally normalize the final matrix, so a blind
126     // equality check could be misleading
127     bool operator==(const GrCoordTransform& that) const;
128     bool operator!=(const GrCoordTransform& that) const;
129 
130     SkMatrix                fMatrix;
131     const GrTextureProxy*   fProxy;
132     bool                    fNormalize;
133     bool                    fReverseY;
134 
135 #ifdef SK_DEBUG
136 public:
setInProcessor()137     void setInProcessor() const { fInProcessor = true; }
138 private:
139     mutable bool fInProcessor;
140 #endif
141 };
142 
143 #endif
144