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); 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); 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 60 GrCoordTransform& operator= (const GrCoordTransform& that) { 61 SkASSERT(!fInProcessor); 62 fMatrix = that.fMatrix; 63 fProxy = that.fProxy; 64 fNormalize = that.fNormalize; 65 fReverseY = that.fReverseY; 66 return *this; 67 } 68 69 /** 70 * Access the matrix for editing. Note, this must be done before adding the transform to an 71 * effect, since effects are immutable. 72 */ accessMatrix()73 SkMatrix* accessMatrix() { 74 SkASSERT(!fInProcessor); 75 return &fMatrix; 76 } 77 hasSameEffectAs(const GrCoordTransform & that)78 bool hasSameEffectAs(const GrCoordTransform& that) const { 79 if (fNormalize != that.fNormalize || 80 fReverseY != that.fReverseY || 81 !fMatrix.cheapEqualTo(that.fMatrix)) { 82 return false; 83 } 84 85 if (fNormalize) { 86 if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) { 87 return false; 88 } 89 } 90 91 return true; 92 } 93 getMatrix()94 const SkMatrix& getMatrix() const { return fMatrix; } proxy()95 const GrTextureProxy* proxy() const { return fProxy; } normalize()96 bool normalize() const { return fNormalize; } reverseY()97 bool reverseY() const { return fReverseY; } 98 99 // This should only ever be called at flush time after the backing texture has been 100 // successfully instantiated peekTexture()101 GrTexture* peekTexture() const { return fProxy->peekTexture(); } 102 103 private: 104 void reset(const SkMatrix& m, GrTextureProxy* proxy = nullptr) { 105 SkASSERT(!fInProcessor); 106 107 fMatrix = m; 108 fProxy = proxy; 109 fNormalize = proxy && proxy->textureType() != GrTextureType::kRectangle; 110 fReverseY = proxy && kBottomLeft_GrSurfaceOrigin == proxy->origin(); 111 } 112 113 // The textures' effect is to optionally normalize the final matrix, so a blind 114 // equality check could be misleading 115 bool operator==(const GrCoordTransform& that) const; 116 bool operator!=(const GrCoordTransform& that) const; 117 118 SkMatrix fMatrix; 119 const GrTextureProxy* fProxy; 120 bool fNormalize; 121 bool fReverseY; 122 123 #ifdef SK_DEBUG 124 public: setInProcessor()125 void setInProcessor() const { fInProcessor = true; } 126 private: 127 mutable bool fInProcessor; 128 #endif 129 }; 130 131 #endif 132