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 GrSamplerState {
|
|
|
|
public:
|
|
|
|
|
2011-06-21 20:32:12 +00:00
|
|
|
GrSamplerState()
|
2012-06-01 20:42:15 +00:00
|
|
|
: fCustomStage (NULL) {
|
2012-10-17 12:53:54 +00:00
|
|
|
GR_DEBUGCODE(fSavedCoordChangeCnt = 0;)
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 18:35:38 +00:00
|
|
|
~GrSamplerState() {
|
|
|
|
GrSafeUnref(fCustomStage);
|
2012-10-17 12:53:54 +00:00
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
2012-04-20 18:35:38 +00:00
|
|
|
}
|
|
|
|
|
2012-10-17 12:53:54 +00:00
|
|
|
bool operator ==(const GrSamplerState& other) const {
|
|
|
|
// first handle cases where one or the other has no custom stage
|
|
|
|
if (NULL == fCustomStage) {
|
|
|
|
return NULL == other.fCustomStage;
|
|
|
|
} else if (NULL == other.fCustomStage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fCustomStage->getFactory() != other.fCustomStage->getFactory()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fCustomStage->isEqual(*other.fCustomStage)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fMatrix == other.fMatrix && fCoordChangeMatrix == other.fCoordChangeMatrix;
|
2012-04-30 20:19:07 +00:00
|
|
|
}
|
2012-10-17 12:53:54 +00:00
|
|
|
|
2012-04-30 20:19:07 +00:00
|
|
|
bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
|
|
|
|
|
2012-10-17 12:53:54 +00:00
|
|
|
GrSamplerState& operator =(const GrSamplerState& other) {
|
|
|
|
GrSafeAssign(fCustomStage, other.fCustomStage);
|
|
|
|
if (NULL != fCustomStage) {
|
|
|
|
fMatrix = other.fMatrix;
|
|
|
|
fCoordChangeMatrix = other.fCoordChangeMatrix;
|
|
|
|
}
|
2012-04-30 20:19:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-02-17 16:43:10 +00:00
|
|
|
/**
|
2012-10-17 12:53:54 +00:00
|
|
|
* This is called when the coordinate system in which the geometry is specified will change.
|
2011-02-17 16:43:10 +00:00
|
|
|
*
|
2012-10-18 02:01:23 +00:00
|
|
|
* @param matrix The transformation from the old coord system to the new one.
|
2011-02-17 16:43:10 +00:00
|
|
|
*/
|
2012-10-17 12:53:54 +00:00
|
|
|
void preConcatCoordChange(const GrMatrix& matrix) { fCoordChangeMatrix.preConcat(matrix); }
|
|
|
|
|
|
|
|
class SavedCoordChange {
|
|
|
|
private:
|
|
|
|
GrMatrix fCoordChangeMatrix;
|
|
|
|
GR_DEBUGCODE(mutable SkAutoTUnref<GrCustomStage> fCustomStage;)
|
|
|
|
|
|
|
|
friend class GrSamplerState;
|
|
|
|
};
|
2011-02-17 16:43:10 +00:00
|
|
|
|
2012-10-16 15:19:45 +00:00
|
|
|
/**
|
2012-10-17 12:53:54 +00:00
|
|
|
* This gets the current coordinate system change. It is the accumulation of
|
|
|
|
* preConcatCoordChange calls since the custom stage was installed. It is used when then caller
|
|
|
|
* wants to temporarily change the source geometry coord system, draw something, and then
|
|
|
|
* restore the previous coord system (e.g. temporarily draw in device coords).s
|
2012-10-16 15:19:45 +00:00
|
|
|
*/
|
2012-10-17 12:53:54 +00:00
|
|
|
void saveCoordChange(SavedCoordChange* savedCoordChange) const {
|
|
|
|
savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix;
|
|
|
|
GrAssert(NULL == savedCoordChange->fCustomStage.get());
|
|
|
|
GR_DEBUGCODE(GrSafeRef(fCustomStage);)
|
|
|
|
GR_DEBUGCODE(savedCoordChange->fCustomStage.reset(fCustomStage);)
|
|
|
|
GR_DEBUGCODE(++fSavedCoordChangeCnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This balances the saveCoordChange call.
|
|
|
|
*/
|
|
|
|
void restoreCoordChange(const SavedCoordChange& savedCoordChange) {
|
|
|
|
fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix;
|
|
|
|
GrAssert(savedCoordChange.fCustomStage.get() == fCustomStage);
|
|
|
|
GR_DEBUGCODE(--fSavedCoordChangeCnt);
|
|
|
|
GR_DEBUGCODE(savedCoordChange.fCustomStage.reset(NULL);)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the texture matrix. This is will be removed soon and be managed by GrCustomStage.
|
|
|
|
*/
|
|
|
|
const GrMatrix& getMatrix() const { return fMatrix; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the matrix to apply at draw time. This is the original texture matrix combined with
|
|
|
|
* any coord system changes.
|
|
|
|
*/
|
|
|
|
void getTotalMatrix(GrMatrix* matrix) const {
|
|
|
|
*matrix = fMatrix;
|
|
|
|
matrix->preConcat(fCoordChangeMatrix);
|
|
|
|
}
|
2012-09-18 14:14:49 +00:00
|
|
|
|
2011-12-12 18:45:07 +00:00
|
|
|
void reset() {
|
2012-09-18 14:14:49 +00:00
|
|
|
GrSafeSetNull(fCustomStage);
|
2011-12-06 16:30:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 19:58:30 +00:00
|
|
|
GrCustomStage* setCustomStage(GrCustomStage* stage) {
|
2012-10-17 12:53:54 +00:00
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
2012-04-20 18:35:38 +00:00
|
|
|
GrSafeAssign(fCustomStage, stage);
|
2012-10-16 15:19:45 +00:00
|
|
|
fMatrix.reset();
|
2012-10-17 12:53:54 +00:00
|
|
|
fCoordChangeMatrix.reset();
|
2012-06-04 19:58:30 +00:00
|
|
|
return stage;
|
2012-04-20 18:35:38 +00:00
|
|
|
}
|
2012-10-16 15:19:45 +00:00
|
|
|
|
|
|
|
GrCustomStage* setCustomStage(GrCustomStage* stage, const GrMatrix& matrix) {
|
2012-10-17 12:53:54 +00:00
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
2012-10-16 15:19:45 +00:00
|
|
|
GrSafeAssign(fCustomStage, stage);
|
|
|
|
fMatrix = matrix;
|
2012-10-17 12:53:54 +00:00
|
|
|
fCoordChangeMatrix.reset();
|
2012-10-16 15:19:45 +00:00
|
|
|
return stage;
|
|
|
|
}
|
|
|
|
|
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-10-17 12:53:54 +00:00
|
|
|
GrMatrix fCoordChangeMatrix;
|
|
|
|
GrMatrix fMatrix; // TODO: remove this, store in GrCustomStage
|
2012-04-30 20:19:07 +00:00
|
|
|
GrCustomStage* fCustomStage;
|
2012-10-17 12:53:54 +00:00
|
|
|
|
|
|
|
GR_DEBUGCODE(mutable int fSavedCoordChangeCnt;)
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|