2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#ifndef GrSamplerState_DEFINED
|
|
|
|
#define GrSamplerState_DEFINED
|
|
|
|
|
2012-04-20 18:35:38 +00:00
|
|
|
#include "GrCustomStage.h"
|
2011-02-17 16:43:10 +00:00
|
|
|
#include "GrMatrix.h"
|
2012-04-20 18:35:38 +00:00
|
|
|
#include "GrTypes.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-07-25 21:27:09 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
class GrTextureParams {
|
2010-12-22 21:39:39 +00:00
|
|
|
public:
|
2012-07-25 21:27:09 +00:00
|
|
|
GrTextureParams() {
|
|
|
|
this->reset();
|
|
|
|
}
|
2011-05-05 12:33:22 +00:00
|
|
|
|
2012-07-25 21:27:09 +00:00
|
|
|
GrTextureParams(const GrTextureParams& params) {
|
|
|
|
*this = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTextureParams& operator =(const GrTextureParams& params) {
|
|
|
|
fTileModes[0] = params.fTileModes[0];
|
|
|
|
fTileModes[1] = params.fTileModes[1];
|
|
|
|
fBilerp = params.fBilerp;
|
|
|
|
return *this;
|
|
|
|
}
|
2011-12-12 18:45:07 +00:00
|
|
|
|
2012-07-25 21:27:09 +00:00
|
|
|
void reset() {
|
|
|
|
this->reset(SkShader::kClamp_TileMode, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(SkShader::TileMode tileXAndY, bool filter) {
|
|
|
|
fTileModes[0] = fTileModes[1] = tileXAndY;
|
|
|
|
fBilerp = filter;
|
|
|
|
}
|
|
|
|
void reset(SkShader::TileMode tileModes[2], bool filter) {
|
|
|
|
fTileModes[0] = tileModes[0];
|
|
|
|
fTileModes[1] = tileModes[1];
|
|
|
|
fBilerp = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setClampNoFilter() {
|
|
|
|
fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
|
|
|
|
fBilerp = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setClamp() {
|
|
|
|
fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBilerp(bool bilerp) { fBilerp = bilerp; }
|
|
|
|
|
|
|
|
void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
|
|
|
|
void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
|
|
|
|
void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
|
|
|
|
|
|
|
|
SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
|
|
|
|
|
|
|
|
SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
|
|
|
|
|
|
|
|
bool isTiled() const {
|
|
|
|
return SkShader::kClamp_TileMode != fTileModes[0] ||
|
|
|
|
SkShader::kClamp_TileMode != fTileModes[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBilerp() const { return fBilerp; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
SkShader::TileMode fTileModes[2];
|
|
|
|
bool fBilerp;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GrSamplerState {
|
|
|
|
public:
|
|
|
|
static const bool kBilerpDefault = false;
|
|
|
|
|
|
|
|
static const SkShader::TileMode kTileModeDefault = SkShader::kClamp_TileMode;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2011-02-17 16:43:10 +00:00
|
|
|
* Default sampler state is set to clamp, use normal sampling mode, be
|
|
|
|
* unfiltered, and use identity matrix.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
2011-06-21 20:32:12 +00:00
|
|
|
GrSamplerState()
|
2012-06-01 20:42:15 +00:00
|
|
|
: fCustomStage (NULL) {
|
2012-05-31 20:35:27 +00:00
|
|
|
memset(this, 0, sizeof(GrSamplerState));
|
2011-12-06 16:30:36 +00:00
|
|
|
this->reset();
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 18:35:38 +00:00
|
|
|
~GrSamplerState() {
|
|
|
|
GrSafeUnref(fCustomStage);
|
|
|
|
}
|
|
|
|
|
2012-04-30 20:19:07 +00:00
|
|
|
bool operator ==(const GrSamplerState& s) const {
|
2012-05-01 12:48:07 +00:00
|
|
|
/* We must be bit-identical as far as the CustomStage;
|
|
|
|
there may be multiple CustomStages that will produce
|
|
|
|
the same shader code and so are equivalent.
|
|
|
|
Can't take the address of fWrapX because it's :8 */
|
|
|
|
int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this;
|
|
|
|
GrAssert(sizeof(GrSamplerState) ==
|
|
|
|
bitwiseRegion + sizeof(fCustomStage));
|
|
|
|
return !memcmp(this, &s, bitwiseRegion) &&
|
|
|
|
((fCustomStage == s.fCustomStage) ||
|
|
|
|
(fCustomStage && s.fCustomStage &&
|
2012-05-10 12:13:36 +00:00
|
|
|
(fCustomStage->getFactory() ==
|
|
|
|
s.fCustomStage->getFactory()) &&
|
2012-05-31 18:40:36 +00:00
|
|
|
fCustomStage->isEqual(*s.fCustomStage)));
|
2012-04-30 20:19:07 +00:00
|
|
|
}
|
|
|
|
bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
|
|
|
|
|
2012-06-01 19:50:02 +00:00
|
|
|
GrSamplerState& operator =(const GrSamplerState& s) {
|
2012-05-31 14:23:28 +00:00
|
|
|
// memcpy() breaks refcounting
|
2012-07-25 21:27:09 +00:00
|
|
|
fTextureParams = s.fTextureParams;
|
2012-05-31 14:23:28 +00:00
|
|
|
fMatrix = s.fMatrix;
|
|
|
|
fSwapRAndB = s.fSwapRAndB;
|
|
|
|
|
2012-06-19 15:27:50 +00:00
|
|
|
GrSafeAssign(fCustomStage, s.fCustomStage);
|
2012-05-31 14:23:28 +00:00
|
|
|
|
2012-04-30 20:19:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-02-17 16:43:10 +00:00
|
|
|
const GrMatrix& getMatrix() const { return fMatrix; }
|
2011-11-08 19:20:57 +00:00
|
|
|
bool swapsRAndB() const { return fSwapRAndB; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-07-25 21:27:09 +00:00
|
|
|
GrTextureParams* textureParams() { return &fTextureParams; }
|
|
|
|
const GrTextureParams& getTextureParams() const { return fTextureParams; }
|
2011-02-17 16:43:10 +00:00
|
|
|
/**
|
2011-12-12 18:45:07 +00:00
|
|
|
* Access the sampler's matrix. See SampleMode for explanation of
|
2011-02-17 16:43:10 +00:00
|
|
|
* relationship between the matrix and sample mode.
|
|
|
|
*/
|
2011-12-12 18:45:07 +00:00
|
|
|
GrMatrix* matrix() { return &fMatrix; }
|
|
|
|
|
2011-11-08 19:20:57 +00:00
|
|
|
/**
|
|
|
|
* Swaps the R and B components when reading from the texture. Has no effect
|
|
|
|
* if the texture is alpha only.
|
|
|
|
*/
|
|
|
|
void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
|
|
|
|
|
2011-02-17 16:43:10 +00:00
|
|
|
/**
|
|
|
|
* Multiplies the current sampler matrix a matrix
|
|
|
|
*
|
|
|
|
* After this call M' = M*m where M is the old matrix, m is the parameter
|
|
|
|
* to this function, and M' is the new matrix. (We consider points to
|
|
|
|
* be column vectors so tex cood vector t is transformed by matrix X as
|
|
|
|
* t' = X*t.)
|
|
|
|
*
|
|
|
|
* @param matrix the matrix used to modify the matrix.
|
|
|
|
*/
|
|
|
|
void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
|
|
|
|
|
2012-07-25 21:27:09 +00:00
|
|
|
void reset(SkShader::TileMode tileXAndY,
|
|
|
|
bool filter,
|
2011-12-06 16:30:36 +00:00
|
|
|
const GrMatrix& matrix) {
|
2012-07-25 21:27:09 +00:00
|
|
|
fTextureParams.reset(tileXAndY, filter);
|
2011-12-06 16:30:36 +00:00
|
|
|
fMatrix = matrix;
|
2011-12-12 16:11:33 +00:00
|
|
|
fSwapRAndB = false;
|
2012-04-20 18:35:38 +00:00
|
|
|
GrSafeSetNull(fCustomStage);
|
2011-12-12 16:11:33 +00:00
|
|
|
}
|
2012-07-25 21:27:09 +00:00
|
|
|
void reset(SkShader::TileMode wrapXAndY, bool filter) {
|
2012-05-31 18:40:36 +00:00
|
|
|
this->reset(wrapXAndY, filter, GrMatrix::I());
|
2011-12-12 18:45:07 +00:00
|
|
|
}
|
2011-12-12 16:11:33 +00:00
|
|
|
void reset(const GrMatrix& matrix) {
|
2012-07-25 21:27:09 +00:00
|
|
|
this->reset(kTileModeDefault, kBilerpDefault, matrix);
|
2011-12-12 18:45:07 +00:00
|
|
|
}
|
|
|
|
void reset() {
|
2012-07-25 21:27:09 +00:00
|
|
|
this->reset(kTileModeDefault, kBilerpDefault, GrMatrix::I());
|
2011-12-06 16:30:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 19:58:30 +00:00
|
|
|
GrCustomStage* setCustomStage(GrCustomStage* stage) {
|
2012-04-20 18:35:38 +00:00
|
|
|
GrSafeAssign(fCustomStage, stage);
|
2012-06-04 19:58:30 +00:00
|
|
|
return stage;
|
2012-04-20 18:35:38 +00:00
|
|
|
}
|
2012-07-30 13:09:05 +00:00
|
|
|
const GrCustomStage* getCustomStage() const { return fCustomStage; }
|
2012-04-20 18:35:38 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
private:
|
2012-07-25 21:27:09 +00:00
|
|
|
GrTextureParams fTextureParams;
|
2012-03-02 21:05:45 +00:00
|
|
|
bool fSwapRAndB;
|
2012-06-01 20:42:15 +00:00
|
|
|
GrMatrix fMatrix;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-04-30 20:19:07 +00:00
|
|
|
GrCustomStage* fCustomStage;
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|