2013-10-02 13:04:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrCoordTransform_DEFINED
|
|
|
|
#define GrCoordTransform_DEFINED
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
#include "GrProcessor.h"
|
2013-10-02 13:04:56 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "GrTypes.h"
|
2014-12-09 17:00:49 +00:00
|
|
|
#include "GrShaderVar.h"
|
2013-10-02 13:04:56 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Coordinates available to GrProcessor subclasses for requesting transformations. Transformed
|
2013-10-02 13:04:56 +00:00
|
|
|
* coordinates are made available in the the portion of fragment shader emitted by the effect.
|
2014-12-09 17:00:49 +00:00
|
|
|
*
|
|
|
|
* The precision of the shader var that interpolates the transformed coordinates can be specified.
|
2013-10-02 13:04:56 +00:00
|
|
|
*/
|
|
|
|
enum GrCoordSet {
|
|
|
|
/**
|
2014-12-18 18:17:44 +00:00
|
|
|
* The user-space coordinates that map to the fragment being rendered. This is the space in
|
|
|
|
* which SkShader operates. It is usually the space in which geometry passed to SkCanvas is
|
|
|
|
* specified (before the view matrix is applied). However, some draw calls take explicit local
|
|
|
|
* coords that map onto the geometry (e.g. drawVertices, drawBitmapRectToRect).
|
2013-10-02 13:04:56 +00:00
|
|
|
*/
|
|
|
|
kLocal_GrCoordSet,
|
|
|
|
|
|
|
|
/**
|
2014-12-18 18:17:44 +00:00
|
|
|
* The device space position of the fragment being shaded.
|
2014-12-18 15:47:16 +00:00
|
|
|
*/
|
|
|
|
kDevice_GrCoordSet,
|
2013-10-02 13:04:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class representing a linear transformation from one of the built-in coordinate sets (local or
|
2014-09-23 16:50:21 +00:00
|
|
|
* position). GrProcessors just define these transformations, and the framework does the rest of the
|
2013-10-02 13:04:56 +00:00
|
|
|
* work to make the transformed coordinates available in their fragment shader.
|
|
|
|
*/
|
2014-04-07 19:34:38 +00:00
|
|
|
class GrCoordTransform : SkNoncopyable {
|
2013-10-02 13:04:56 +00:00
|
|
|
public:
|
2014-10-16 01:34:46 +00:00
|
|
|
GrCoordTransform() { SkDEBUGCODE(fInProcessor = false); }
|
2013-10-02 13:04:56 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-09 17:00:49 +00:00
|
|
|
* Create a transformation that maps [0, 1] to a texture's boundaries. The precision is inferred
|
2014-12-09 18:51:07 +00:00
|
|
|
* from the texture size and filter. The texture origin also implies whether a y-reversal should
|
|
|
|
* be performed.
|
2013-10-02 13:04:56 +00:00
|
|
|
*/
|
2014-12-09 18:51:07 +00:00
|
|
|
GrCoordTransform(GrCoordSet sourceCoords,
|
|
|
|
const GrTexture* texture,
|
|
|
|
GrTextureParams::FilterMode filter) {
|
2014-12-09 17:00:49 +00:00
|
|
|
SkASSERT(texture);
|
2014-10-16 01:34:46 +00:00
|
|
|
SkDEBUGCODE(fInProcessor = false);
|
2014-12-09 18:51:07 +00:00
|
|
|
this->reset(sourceCoords, texture, filter);
|
2013-10-02 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-09 18:51:07 +00:00
|
|
|
* Create a transformation from a matrix. The precision is inferred from the texture size and
|
|
|
|
* filter. The texture origin also implies whether a y-reversal should be performed.
|
2013-10-02 13:04:56 +00:00
|
|
|
*/
|
2014-12-09 18:51:07 +00:00
|
|
|
GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m,
|
|
|
|
const GrTexture* texture, GrTextureParams::FilterMode filter) {
|
2014-10-16 01:34:46 +00:00
|
|
|
SkDEBUGCODE(fInProcessor = false);
|
2014-12-09 17:00:49 +00:00
|
|
|
SkASSERT(texture);
|
2014-12-09 18:51:07 +00:00
|
|
|
this->reset(sourceCoords, m, texture, filter);
|
2013-10-02 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:00:49 +00:00
|
|
|
/**
|
|
|
|
* Create a transformation that applies the matrix to a coord set.
|
|
|
|
*/
|
|
|
|
GrCoordTransform(GrCoordSet sourceCoords, const SkMatrix& m,
|
2014-12-09 18:04:14 +00:00
|
|
|
GrSLPrecision precision = kDefault_GrSLPrecision) {
|
2014-12-09 17:00:49 +00:00
|
|
|
SkDEBUGCODE(fInProcessor = false);
|
|
|
|
this->reset(sourceCoords, m, precision);
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:51:07 +00:00
|
|
|
void reset(GrCoordSet sourceCoords, const GrTexture* texture,
|
|
|
|
GrTextureParams::FilterMode filter) {
|
2014-10-16 01:34:46 +00:00
|
|
|
SkASSERT(!fInProcessor);
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(texture);
|
2014-12-09 18:51:07 +00:00
|
|
|
this->reset(sourceCoords, MakeDivByTextureWHMatrix(texture), texture, filter);
|
2013-10-02 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:51:07 +00:00
|
|
|
void reset(GrCoordSet, const SkMatrix&, const GrTexture*, GrTextureParams::FilterMode filter);
|
2014-12-09 17:00:49 +00:00
|
|
|
void reset(GrCoordSet sourceCoords, const SkMatrix& m,
|
2014-12-09 18:04:14 +00:00
|
|
|
GrSLPrecision precision = kDefault_GrSLPrecision);
|
2013-10-02 13:04:56 +00:00
|
|
|
|
2014-12-09 17:00:49 +00:00
|
|
|
GrCoordTransform& operator= (const GrCoordTransform& that) {
|
2014-10-16 01:34:46 +00:00
|
|
|
SkASSERT(!fInProcessor);
|
2014-12-09 17:00:49 +00:00
|
|
|
fSourceCoords = that.fSourceCoords;
|
|
|
|
fMatrix = that.fMatrix;
|
|
|
|
fReverseY = that.fReverseY;
|
|
|
|
fPrecision = that.fPrecision;
|
2013-10-04 01:20:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the matrix for editing. Note, this must be done before adding the transform to an
|
|
|
|
* effect, since effects are immutable.
|
|
|
|
*/
|
|
|
|
SkMatrix* accessMatrix() {
|
2014-10-16 01:34:46 +00:00
|
|
|
SkASSERT(!fInProcessor);
|
2013-10-04 01:20:09 +00:00
|
|
|
return &fMatrix;
|
|
|
|
}
|
|
|
|
|
2014-12-09 17:00:49 +00:00
|
|
|
bool operator==(const GrCoordTransform& that) const {
|
2014-10-16 02:06:21 +00:00
|
|
|
return fSourceCoords == that.fSourceCoords &&
|
|
|
|
fMatrix.cheapEqualTo(that.fMatrix) &&
|
2014-12-09 17:00:49 +00:00
|
|
|
fReverseY == that.fReverseY &&
|
|
|
|
fPrecision == that.fPrecision;
|
2013-10-02 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:00:49 +00:00
|
|
|
bool operator!=(const GrCoordTransform& that) const { return !(*this == that); }
|
2014-10-16 02:06:21 +00:00
|
|
|
|
2013-10-02 13:04:56 +00:00
|
|
|
GrCoordSet sourceCoords() const { return fSourceCoords; }
|
|
|
|
const SkMatrix& getMatrix() const { return fMatrix; }
|
|
|
|
bool reverseY() const { return fReverseY; }
|
2014-12-09 18:04:14 +00:00
|
|
|
GrSLPrecision precision() const { return fPrecision; }
|
2013-10-02 13:04:56 +00:00
|
|
|
|
2014-08-29 22:05:53 +00:00
|
|
|
/** Useful for effects that want to insert a texture matrix that is implied by the texture
|
|
|
|
dimensions */
|
|
|
|
static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(texture);
|
2014-08-29 22:05:53 +00:00
|
|
|
SkMatrix mat;
|
|
|
|
mat.setIDiv(texture->width(), texture->height());
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:04:56 +00:00
|
|
|
private:
|
2014-12-09 17:00:49 +00:00
|
|
|
GrCoordSet fSourceCoords;
|
|
|
|
SkMatrix fMatrix;
|
|
|
|
bool fReverseY;
|
2014-12-09 18:04:14 +00:00
|
|
|
GrSLPrecision fPrecision;
|
2013-10-02 13:04:56 +00:00
|
|
|
typedef SkNoncopyable INHERITED;
|
2013-10-04 01:20:09 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
public:
|
2014-10-16 01:34:46 +00:00
|
|
|
void setInProcessor() const { fInProcessor = true; }
|
2013-10-04 01:20:09 +00:00
|
|
|
private:
|
2014-10-16 01:34:46 +00:00
|
|
|
mutable bool fInProcessor;
|
2013-10-04 01:20:09 +00:00
|
|
|
#endif
|
2013-10-02 13:04:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|