| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrCoordTransform_DEFINED | 8 #ifndef GrCoordTransform_DEFINED |
| 9 #define GrCoordTransform_DEFINED | 9 #define GrCoordTransform_DEFINED |
| 10 | 10 |
| 11 #include "GrProcessor.h" | 11 #include "GrProcessor.h" |
| 12 #include "SkMatrix.h" | 12 #include "SkMatrix.h" |
| 13 #include "GrTexture.h" | 13 #include "GrTexture.h" |
| 14 #include "GrTypes.h" | 14 #include "GrTypes.h" |
| 15 #include "GrShaderVar.h" |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Coordinates available to GrProcessor subclasses for requesting transformation
s. Transformed | 18 * Coordinates available to GrProcessor subclasses for requesting transformation
s. Transformed |
| 18 * coordinates are made available in the the portion of fragment shader emitted
by the effect. | 19 * coordinates are made available in the the portion of fragment shader emitted
by the effect. |
| 20 * |
| 21 * The precision of the shader var that interpolates the transformed coordinates
can be specified. |
| 19 */ | 22 */ |
| 20 enum GrCoordSet { | 23 enum GrCoordSet { |
| 21 /** | 24 /** |
| 22 * The user-space coordinates that map to the fragment being rendered. These
coords account for | 25 * The user-space coordinates that map to the fragment being rendered. These
coords account for |
| 23 * any change of coordinate system done on the CPU by GrContext before rende
ring, and also are | 26 * any change of coordinate system done on the CPU by GrContext before rende
ring, and also are |
| 24 * correct for draws that take explicit local coords rather than inferring t
hem from the | 27 * correct for draws that take explicit local coords rather than inferring t
hem from the |
| 25 * primitive's positions (e.g. drawVertices). These are usually the coords a
GrProcessor wants. | 28 * primitive's positions (e.g. drawVertices). These are usually the coords a
GrProcessor wants. |
| 26 */ | 29 */ |
| 27 kLocal_GrCoordSet, | 30 kLocal_GrCoordSet, |
| 28 | 31 |
| 29 /** | 32 /** |
| 30 * The actual vertex position. Note that GrContext may not draw using the or
iginal view matrix | 33 * The actual vertex position. Note that GrContext may not draw using the or
iginal view matrix |
| 31 * specified by the caller, as it may have transformed vertices into another
space. These are | 34 * specified by the caller, as it may have transformed vertices into another
space. These are |
| 32 * usually not the coordinates a GrProcessor wants. | 35 * usually not the coordinates a GrProcessor wants. |
| 33 */ | 36 */ |
| 34 kPosition_GrCoordSet | 37 kPosition_GrCoordSet |
| 35 }; | 38 }; |
| 36 | 39 |
| 37 /** | 40 /** |
| 38 * A class representing a linear transformation from one of the built-in coordin
ate sets (local or | 41 * A class representing a linear transformation from one of the built-in coordin
ate sets (local or |
| 39 * position). GrProcessors just define these transformations, and the framework
does the rest of the | 42 * position). GrProcessors just define these transformations, and the framework
does the rest of the |
| 40 * work to make the transformed coordinates available in their fragment shader. | 43 * work to make the transformed coordinates available in their fragment shader. |
| 41 */ | 44 */ |
| 42 class GrCoordTransform : SkNoncopyable { | 45 class GrCoordTransform : SkNoncopyable { |
| 43 public: | 46 public: |
| 44 GrCoordTransform() { SkDEBUGCODE(fInProcessor = false); } | 47 GrCoordTransform() { SkDEBUGCODE(fInProcessor = false); } |
| 45 | 48 |
| 46 /** | 49 /** |
| 47 * Create a transformation that maps [0, 1] to a texture's boundaries. | 50 * Create a transformation that maps [0, 1] to a texture's boundaries. The p
recision is inferred |
| 51 * from the texture size. |
| 48 */ | 52 */ |
| 49 GrCoordTransform(GrCoordSet sourceCoords, const GrTexture* texture) { | 53 GrCoordTransform(GrCoordSet sourceCoords, const GrTexture* texture) { |
| 54 SkASSERT(texture); |
| 50 SkDEBUGCODE(fInProcessor = false); | 55 SkDEBUGCODE(fInProcessor = false); |
| 51 this->reset(sourceCoords, texture); | 56 this->reset(sourceCoords, texture); |
| 52 } | 57 } |
| 53 | 58 |
| 54 /** | 59 /** |
| 55 * Create a transformation from a matrix. The optional texture parameter is
used to infer if the | 60 * Create a transformation from a matrix. The texture parameter is used to i
nfer if the |
| 56 * framework should internally do a y reversal to account for it being upsid
e down by Skia's | 61 * framework should internally do a y reversal to account for it being upsid
e down by Skia's |
| 57 * coord convention. | 62 * coord convention and the precision of the shader variables used to implem
ent the coord |
| 63 * transform. |
| 58 */ | 64 */ |
| 59 GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m, const GrTexture
* texture = NULL) { | 65 GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m, const GrTexture
* texture) { |
| 60 SkDEBUGCODE(fInProcessor = false); | 66 SkDEBUGCODE(fInProcessor = false); |
| 67 SkASSERT(texture); |
| 61 this->reset(sourceCoords, m, texture); | 68 this->reset(sourceCoords, m, texture); |
| 62 } | 69 } |
| 63 | 70 |
| 71 /** |
| 72 * Create a transformation that applies the matrix to a coord set. |
| 73 */ |
| 74 GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m, |
| 75 GrShaderVar::Precision precision = GrShaderVar::kDefault_Pr
ecision) { |
| 76 SkDEBUGCODE(fInProcessor = false); |
| 77 this->reset(sourceCoords, m, precision); |
| 78 } |
| 79 |
| 64 void reset(GrCoordSet sourceCoords, const GrTexture* texture) { | 80 void reset(GrCoordSet sourceCoords, const GrTexture* texture) { |
| 65 SkASSERT(!fInProcessor); | 81 SkASSERT(!fInProcessor); |
| 66 SkASSERT(texture); | 82 SkASSERT(texture); |
| 67 this->reset(sourceCoords, MakeDivByTextureWHMatrix(texture), texture); | 83 this->reset(sourceCoords, MakeDivByTextureWHMatrix(texture), texture); |
| 68 } | 84 } |
| 69 | 85 |
| 70 void reset(GrCoordSet sourceCoords, const SkMatrix& m, const GrTexture* text
ure = NULL) { | 86 void reset(GrCoordSet, const SkMatrix&, const GrTexture*); |
| 87 void reset(GrCoordSet sourceCoords, const SkMatrix& m, |
| 88 GrShaderVar::Precision precision = GrShaderVar::kDefault_Precisio
n); |
| 89 |
| 90 GrCoordTransform& operator= (const GrCoordTransform& that) { |
| 71 SkASSERT(!fInProcessor); | 91 SkASSERT(!fInProcessor); |
| 72 fSourceCoords = sourceCoords; | 92 fSourceCoords = that.fSourceCoords; |
| 73 fMatrix = m; | 93 fMatrix = that.fMatrix; |
| 74 fReverseY = texture && kBottomLeft_GrSurfaceOrigin == texture->origin(); | 94 fReverseY = that.fReverseY; |
| 75 } | 95 fPrecision = that.fPrecision; |
| 76 | |
| 77 GrCoordTransform& operator= (const GrCoordTransform& other) { | |
| 78 SkASSERT(!fInProcessor); | |
| 79 fSourceCoords = other.fSourceCoords; | |
| 80 fMatrix = other.fMatrix; | |
| 81 fReverseY = other.fReverseY; | |
| 82 return *this; | 96 return *this; |
| 83 } | 97 } |
| 84 | 98 |
| 85 /** | 99 /** |
| 86 * Access the matrix for editing. Note, this must be done before adding the
transform to an | 100 * Access the matrix for editing. Note, this must be done before adding the
transform to an |
| 87 * effect, since effects are immutable. | 101 * effect, since effects are immutable. |
| 88 */ | 102 */ |
| 89 SkMatrix* accessMatrix() { | 103 SkMatrix* accessMatrix() { |
| 90 SkASSERT(!fInProcessor); | 104 SkASSERT(!fInProcessor); |
| 91 return &fMatrix; | 105 return &fMatrix; |
| 92 } | 106 } |
| 93 | 107 |
| 94 bool operator== (const GrCoordTransform& that) const { | 108 bool operator==(const GrCoordTransform& that) const { |
| 95 return fSourceCoords == that.fSourceCoords && | 109 return fSourceCoords == that.fSourceCoords && |
| 96 fMatrix.cheapEqualTo(that.fMatrix) && | 110 fMatrix.cheapEqualTo(that.fMatrix) && |
| 97 fReverseY == that.fReverseY; | 111 fReverseY == that.fReverseY && |
| 112 fPrecision == that.fPrecision; |
| 98 } | 113 } |
| 99 | 114 |
| 100 bool operator!= (const GrCoordTransform& that) const { return !(*this == tha
t); } | 115 bool operator!=(const GrCoordTransform& that) const { return !(*this == that
); } |
| 101 | 116 |
| 102 GrCoordSet sourceCoords() const { return fSourceCoords; } | 117 GrCoordSet sourceCoords() const { return fSourceCoords; } |
| 103 const SkMatrix& getMatrix() const { return fMatrix; } | 118 const SkMatrix& getMatrix() const { return fMatrix; } |
| 104 bool reverseY() const { return fReverseY; } | 119 bool reverseY() const { return fReverseY; } |
| 120 GrShaderVar::Precision precision() const { return fPrecision; } |
| 105 | 121 |
| 106 /** Useful for effects that want to insert a texture matrix that is implied
by the texture | 122 /** Useful for effects that want to insert a texture matrix that is implied
by the texture |
| 107 dimensions */ | 123 dimensions */ |
| 108 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { | 124 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
| 109 SkASSERT(texture); | 125 SkASSERT(texture); |
| 110 SkMatrix mat; | 126 SkMatrix mat; |
| 111 mat.setIDiv(texture->width(), texture->height()); | 127 mat.setIDiv(texture->width(), texture->height()); |
| 112 return mat; | 128 return mat; |
| 113 } | 129 } |
| 114 | 130 |
| 115 private: | 131 private: |
| 116 GrCoordSet fSourceCoords; | 132 GrCoordSet fSourceCoords; |
| 117 SkMatrix fMatrix; | 133 SkMatrix fMatrix; |
| 118 bool fReverseY; | 134 bool fReverseY; |
| 119 | 135 GrShaderVar::Precision fPrecision; |
| 120 typedef SkNoncopyable INHERITED; | 136 typedef SkNoncopyable INHERITED; |
| 121 | 137 |
| 122 #ifdef SK_DEBUG | 138 #ifdef SK_DEBUG |
| 123 public: | 139 public: |
| 124 void setInProcessor() const { fInProcessor = true; } | 140 void setInProcessor() const { fInProcessor = true; } |
| 125 private: | 141 private: |
| 126 mutable bool fInProcessor; | 142 mutable bool fInProcessor; |
| 127 #endif | 143 #endif |
| 128 }; | 144 }; |
| 129 | 145 |
| 130 #endif | 146 #endif |
| OLD | NEW |