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
|
|
|
|
|
|
|
/**
|
2016-10-03 21:15:28 +00:00
|
|
|
* A class representing a linear transformation of local coordinates. GrFragnentProcessors
|
|
|
|
* these transformations, and the GrGeometryProcessor implements the transformation.
|
2013-10-02 13:04:56 +00:00
|
|
|
*/
|
2014-04-07 19:34:38 +00:00
|
|
|
class GrCoordTransform : SkNoncopyable {
|
2013-10-02 13:04:56 +00:00
|
|
|
public:
|
2016-10-03 21:15:28 +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
|
|
|
*/
|
2016-10-03 21:15:28 +00:00
|
|
|
GrCoordTransform(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);
|
2016-10-03 21:15:28 +00:00
|
|
|
this->reset(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
|
|
|
*/
|
2016-10-03 21:15:28 +00:00
|
|
|
GrCoordTransform(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);
|
2016-10-03 21:15:28 +00:00
|
|
|
this->reset(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.
|
|
|
|
*/
|
2016-10-03 21:15:28 +00:00
|
|
|
GrCoordTransform(const SkMatrix& m, GrSLPrecision precision = kDefault_GrSLPrecision) {
|
2014-12-09 17:00:49 +00:00
|
|
|
SkDEBUGCODE(fInProcessor = false);
|
2016-10-03 21:15:28 +00:00
|
|
|
this->reset(m, precision);
|
2014-12-09 17:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:15:28 +00:00
|
|
|
void reset(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);
|
2016-10-03 21:15:28 +00:00
|
|
|
this->reset(MakeDivByTextureWHMatrix(texture), texture, filter);
|
2013-10-02 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:15:28 +00:00
|
|
|
void reset(const SkMatrix&, const GrTexture*, GrTextureParams::FilterMode filter);
|
|
|
|
void reset(const SkMatrix& m, 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
|
|
|
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 {
|
2016-10-03 21:15:28 +00:00
|
|
|
return 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
|
|
|
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;
|
2015-03-13 13:08:28 +00:00
|
|
|
(void)mat.setIDiv(texture->width(), texture->height());
|
2014-08-29 22:05:53 +00:00
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:04:56 +00:00
|
|
|
private:
|
2014-12-09 17:00:49 +00:00
|
|
|
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
|