Line data Source code
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 "GrTexture.h"
13 :
14 : class GrResourceProvider;
15 : class GrTextureProxy;
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 : SkNoncopyable {
22 : public:
23 0 : GrCoordTransform()
24 0 : : fTexture(nullptr)
25 : , fNormalize(false)
26 0 : , fReverseY(false) {
27 0 : SkDEBUGCODE(fInProcessor = false);
28 0 : }
29 :
30 : /**
31 : * 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.
33 : */
34 : GrCoordTransform(GrResourceProvider* resourceProvider, GrTextureProxy* proxy) {
35 : SkASSERT(proxy);
36 : SkDEBUGCODE(fInProcessor = false);
37 : this->reset(resourceProvider, SkMatrix::I(), proxy);
38 : }
39 :
40 : /**
41 : * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
42 : * should be performed.
43 : */
44 0 : GrCoordTransform(GrResourceProvider* resourceProvider, const SkMatrix& m,
45 0 : GrTextureProxy* proxy) {
46 0 : SkASSERT(proxy);
47 0 : SkDEBUGCODE(fInProcessor = false);
48 0 : this->reset(resourceProvider, m, proxy);
49 0 : }
50 :
51 : /**
52 : * Create a transformation that applies the matrix to a coord set.
53 : */
54 : GrCoordTransform(const SkMatrix& m) {
55 : SkDEBUGCODE(fInProcessor = false);
56 : this->reset(m);
57 : }
58 :
59 : void reset(GrResourceProvider*, const SkMatrix&, GrTextureProxy*, bool normalize = true);
60 :
61 0 : void reset(const SkMatrix& m) {
62 0 : SkASSERT(!fInProcessor);
63 0 : fMatrix = m;
64 0 : fTexture = nullptr;
65 0 : fNormalize = false;
66 0 : fReverseY = false;
67 0 : }
68 :
69 0 : GrCoordTransform& operator= (const GrCoordTransform& that) {
70 0 : SkASSERT(!fInProcessor);
71 0 : fMatrix = that.fMatrix;
72 0 : fTexture = that.fTexture;
73 0 : fNormalize = that.fNormalize;
74 0 : fReverseY = that.fReverseY;
75 0 : 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 0 : SkMatrix* accessMatrix() {
83 0 : SkASSERT(!fInProcessor);
84 0 : return &fMatrix;
85 : }
86 :
87 0 : bool hasSameEffectAs(const GrCoordTransform& that) const {
88 0 : if (fNormalize != that.fNormalize ||
89 0 : fReverseY != that.fReverseY ||
90 0 : !fMatrix.cheapEqualTo(that.fMatrix)) {
91 0 : return false;
92 : }
93 :
94 0 : if (fNormalize) {
95 0 : SkASSERT(fTexture && that.fTexture);
96 0 : return fTexture->width() == that.fTexture->width() &&
97 0 : fTexture->height() == that.fTexture->height();
98 : }
99 :
100 0 : return true;
101 : }
102 :
103 0 : const SkMatrix& getMatrix() const { return fMatrix; }
104 0 : const GrTexture* texture() const { return fTexture; }
105 0 : bool normalize() const { return fNormalize; }
106 0 : bool reverseY() const { return fReverseY; }
107 :
108 : private:
109 : // 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 :
114 : SkMatrix fMatrix;
115 : const GrTexture* fTexture;
116 : bool fNormalize;
117 : bool fReverseY;
118 : typedef SkNoncopyable INHERITED;
119 :
120 : #ifdef SK_DEBUG
121 : public:
122 0 : void setInProcessor() const { fInProcessor = true; }
123 : private:
124 : mutable bool fInProcessor;
125 : #endif
126 : };
127 :
128 : #endif
|