1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/gpu/GrCoordTransform.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrCoordTransform_DEFINED 1.12 +#define GrCoordTransform_DEFINED 1.13 + 1.14 +#include "GrEffect.h" 1.15 +#include "SkMatrix.h" 1.16 +#include "GrTexture.h" 1.17 +#include "GrTypes.h" 1.18 + 1.19 +/** 1.20 + * Coordinates available to GrEffect subclasses for requesting transformations. Transformed 1.21 + * coordinates are made available in the the portion of fragment shader emitted by the effect. 1.22 + */ 1.23 +enum GrCoordSet { 1.24 + /** 1.25 + * The user-space coordinates that map to the fragment being rendered. These coords account for 1.26 + * any change of coordinate system done on the CPU by GrContext before rendering, and also are 1.27 + * correct for draws that take explicit local coords rather than inferring them from the 1.28 + * primitive's positions (e.g. drawVertices). These are usually the coords a GrEffect wants. 1.29 + */ 1.30 + kLocal_GrCoordSet, 1.31 + 1.32 + /** 1.33 + * The actual vertex position. Note that GrContext may not draw using the original view matrix 1.34 + * specified by the caller, as it may have transformed vertices into another space. These are 1.35 + * usually not the coordinates a GrEffect wants. 1.36 + */ 1.37 + kPosition_GrCoordSet 1.38 +}; 1.39 + 1.40 +/** 1.41 + * A class representing a linear transformation from one of the built-in coordinate sets (local or 1.42 + * position). GrEffects just define these transformations, and the framework does the rest of the 1.43 + * work to make the transformed coordinates available in their fragment shader. 1.44 + */ 1.45 +class GrCoordTransform : public SkNoncopyable { 1.46 +public: 1.47 + GrCoordTransform() { SkDEBUGCODE(fInEffect = false); } 1.48 + 1.49 + /** 1.50 + * Create a transformation that maps [0, 1] to a texture's boundaries. 1.51 + */ 1.52 + GrCoordTransform(GrCoordSet sourceCoords, const GrTexture* texture) { 1.53 + SkDEBUGCODE(fInEffect = false); 1.54 + this->reset(sourceCoords, texture); 1.55 + } 1.56 + 1.57 + /** 1.58 + * Create a transformation from a matrix. The optional texture parameter is used to infer if the 1.59 + * framework should internally do a y reversal to account for it being upside down by Skia's 1.60 + * coord convention. 1.61 + */ 1.62 + GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m, const GrTexture* texture = NULL) { 1.63 + SkDEBUGCODE(fInEffect = false); 1.64 + this->reset(sourceCoords, m, texture); 1.65 + } 1.66 + 1.67 + void reset(GrCoordSet sourceCoords, const GrTexture* texture) { 1.68 + SkASSERT(!fInEffect); 1.69 + SkASSERT(NULL != texture); 1.70 + this->reset(sourceCoords, GrEffect::MakeDivByTextureWHMatrix(texture), texture); 1.71 + } 1.72 + 1.73 + void reset(GrCoordSet sourceCoords, const SkMatrix& m, const GrTexture* texture = NULL) { 1.74 + SkASSERT(!fInEffect); 1.75 + fSourceCoords = sourceCoords; 1.76 + fMatrix = m; 1.77 + fReverseY = NULL != texture && kBottomLeft_GrSurfaceOrigin == texture->origin(); 1.78 + } 1.79 + 1.80 + GrCoordTransform& operator= (const GrCoordTransform& other) { 1.81 + SkASSERT(!fInEffect); 1.82 + fSourceCoords = other.fSourceCoords; 1.83 + fMatrix = other.fMatrix; 1.84 + fReverseY = other.fReverseY; 1.85 + return *this; 1.86 + } 1.87 + 1.88 + /** 1.89 + * Access the matrix for editing. Note, this must be done before adding the transform to an 1.90 + * effect, since effects are immutable. 1.91 + */ 1.92 + SkMatrix* accessMatrix() { 1.93 + SkASSERT(!fInEffect); 1.94 + return &fMatrix; 1.95 + } 1.96 + 1.97 + bool operator== (const GrCoordTransform& other) const { 1.98 + return fSourceCoords == other.fSourceCoords && 1.99 + fMatrix.cheapEqualTo(other.fMatrix) && 1.100 + fReverseY == other.fReverseY; 1.101 + } 1.102 + 1.103 + GrCoordSet sourceCoords() const { return fSourceCoords; } 1.104 + const SkMatrix& getMatrix() const { return fMatrix; } 1.105 + bool reverseY() const { return fReverseY; } 1.106 + 1.107 +private: 1.108 + GrCoordSet fSourceCoords; 1.109 + SkMatrix fMatrix; 1.110 + bool fReverseY; 1.111 + 1.112 + typedef SkNoncopyable INHERITED; 1.113 + 1.114 +#ifdef SK_DEBUG 1.115 +public: 1.116 + void setInEffect() const { fInEffect = true; } 1.117 +private: 1.118 + mutable bool fInEffect; 1.119 +#endif 1.120 +}; 1.121 + 1.122 +#endif