2010-12-23 00:39:39 +03:00
|
|
|
|
2011-07-28 18:26:00 +04: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-23 00:39:39 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-28 18:26:00 +04:00
|
|
|
|
2012-10-26 21:53:18 +04:00
|
|
|
#ifndef GrEffectStage_DEFINED
|
|
|
|
#define GrEffectStage_DEFINED
|
2010-12-23 00:39:39 +03:00
|
|
|
|
2012-10-29 23:51:22 +04:00
|
|
|
#include "GrBackendEffectFactory.h"
|
2012-10-24 22:28:34 +04:00
|
|
|
#include "GrEffect.h"
|
2012-11-01 22:02:54 +04:00
|
|
|
#include "SkMatrix.h"
|
2012-04-20 22:35:38 +04:00
|
|
|
#include "GrTypes.h"
|
2010-12-23 00:39:39 +03:00
|
|
|
|
2012-07-26 01:27:09 +04:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
class GrEffectStage {
|
2012-07-26 01:27:09 +04:00
|
|
|
public:
|
2012-10-26 17:01:20 +04:00
|
|
|
GrEffectStage()
|
2013-01-16 19:51:47 +04:00
|
|
|
: fEffectRef (NULL) {
|
2012-10-17 16:53:54 +04:00
|
|
|
GR_DEBUGCODE(fSavedCoordChangeCnt = 0;)
|
2010-12-23 00:39:39 +03:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
~GrEffectStage() {
|
2013-01-16 19:51:47 +04:00
|
|
|
GrSafeUnref(fEffectRef);
|
2012-10-17 16:53:54 +04:00
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
2012-04-20 22:35:38 +04:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
bool operator ==(const GrEffectStage& other) const {
|
2012-10-24 23:07:10 +04:00
|
|
|
// first handle cases where one or the other has no effect
|
2013-01-16 19:51:47 +04:00
|
|
|
if (NULL == fEffectRef) {
|
|
|
|
return NULL == other.fEffectRef;
|
|
|
|
} else if (NULL == other.fEffectRef) {
|
2012-10-17 16:53:54 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-22 23:55:59 +04:00
|
|
|
if (!(*this->getEffect())->isEqual(*other.getEffect())) {
|
2012-10-17 16:53:54 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-05 20:36:02 +04:00
|
|
|
return fCoordChangeMatrix == other.fCoordChangeMatrix;
|
2012-05-01 00:19:07 +04:00
|
|
|
}
|
2012-10-17 16:53:54 +04:00
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
bool operator !=(const GrEffectStage& s) const { return !(*this == s); }
|
2012-05-01 00:19:07 +04:00
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
GrEffectStage& operator =(const GrEffectStage& other) {
|
2013-01-16 19:51:47 +04:00
|
|
|
GrSafeAssign(fEffectRef, other.fEffectRef);
|
|
|
|
if (NULL != fEffectRef) {
|
2012-10-17 16:53:54 +04:00
|
|
|
fCoordChangeMatrix = other.fCoordChangeMatrix;
|
|
|
|
}
|
2012-05-01 00:19:07 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-02-17 19:43:10 +03:00
|
|
|
/**
|
2012-10-17 16:53:54 +04:00
|
|
|
* This is called when the coordinate system in which the geometry is specified will change.
|
2011-02-17 19:43:10 +03:00
|
|
|
*
|
2013-03-20 21:47:16 +04:00
|
|
|
* @param matrix The transformation from the old coord system to the new one.
|
2011-02-17 19:43:10 +03:00
|
|
|
*/
|
2013-03-20 21:47:16 +04:00
|
|
|
void preConcatCoordChange(const SkMatrix& matrix) { fCoordChangeMatrix.preConcat(matrix); }
|
2012-10-17 16:53:54 +04:00
|
|
|
|
|
|
|
class SavedCoordChange {
|
|
|
|
private:
|
2012-11-01 22:02:54 +04:00
|
|
|
SkMatrix fCoordChangeMatrix;
|
2013-01-16 19:51:47 +04:00
|
|
|
GR_DEBUGCODE(mutable SkAutoTUnref<const GrEffectRef> fEffectRef;)
|
2012-10-17 16:53:54 +04:00
|
|
|
|
2012-10-26 17:01:20 +04:00
|
|
|
friend class GrEffectStage;
|
2012-10-17 16:53:54 +04:00
|
|
|
};
|
2011-02-17 19:43:10 +03:00
|
|
|
|
2012-10-16 19:19:45 +04:00
|
|
|
/**
|
2012-10-17 16:53:54 +04:00
|
|
|
* This gets the current coordinate system change. It is the accumulation of
|
2013-03-20 21:47:16 +04:00
|
|
|
* preConcatCoordChange calls since the effect was installed. It is used when then caller
|
2012-10-17 16:53:54 +04:00
|
|
|
* wants to temporarily change the source geometry coord system, draw something, and then
|
2012-10-26 21:53:18 +04:00
|
|
|
* restore the previous coord system (e.g. temporarily draw in device coords).
|
2012-10-16 19:19:45 +04:00
|
|
|
*/
|
2012-10-17 16:53:54 +04:00
|
|
|
void saveCoordChange(SavedCoordChange* savedCoordChange) const {
|
|
|
|
savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix;
|
2013-01-16 19:51:47 +04:00
|
|
|
GrAssert(NULL == savedCoordChange->fEffectRef.get());
|
|
|
|
GR_DEBUGCODE(GrSafeRef(fEffectRef);)
|
|
|
|
GR_DEBUGCODE(savedCoordChange->fEffectRef.reset(fEffectRef);)
|
2012-10-17 16:53:54 +04:00
|
|
|
GR_DEBUGCODE(++fSavedCoordChangeCnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This balances the saveCoordChange call.
|
|
|
|
*/
|
|
|
|
void restoreCoordChange(const SavedCoordChange& savedCoordChange) {
|
|
|
|
fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix;
|
2013-01-16 19:51:47 +04:00
|
|
|
GrAssert(savedCoordChange.fEffectRef.get() == fEffectRef);
|
2012-10-17 16:53:54 +04:00
|
|
|
GR_DEBUGCODE(--fSavedCoordChangeCnt);
|
2013-01-16 19:51:47 +04:00
|
|
|
GR_DEBUGCODE(savedCoordChange.fEffectRef.reset(NULL);)
|
2012-10-17 16:53:54 +04:00
|
|
|
}
|
|
|
|
|
2013-01-23 23:53:46 +04:00
|
|
|
/**
|
|
|
|
* Used when storing a deferred GrDrawState. The DeferredStage allows resources owned by its
|
|
|
|
* GrEffect to be recycled through the cache.
|
|
|
|
*/
|
|
|
|
class DeferredStage {
|
|
|
|
public:
|
|
|
|
DeferredStage() : fEffect(NULL) {
|
|
|
|
SkDEBUGCODE(fInitialized = false;)
|
|
|
|
}
|
|
|
|
|
2013-01-24 01:37:01 +04:00
|
|
|
~DeferredStage() {
|
|
|
|
if (NULL != fEffect) {
|
|
|
|
fEffect->decDeferredRefCounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 23:53:46 +04:00
|
|
|
void saveFrom(const GrEffectStage& stage) {
|
|
|
|
GrAssert(!fInitialized);
|
|
|
|
if (NULL != stage.fEffectRef) {
|
2013-01-24 01:37:01 +04:00
|
|
|
stage.fEffectRef->get()->incDeferredRefCounts();
|
2013-01-23 23:53:46 +04:00
|
|
|
fEffect = stage.fEffectRef->get();
|
|
|
|
fCoordChangeMatrix = stage.fCoordChangeMatrix;
|
2013-03-19 22:51:02 +04:00
|
|
|
fVertexAttribIndices[0] = stage.fVertexAttribIndices[0];
|
|
|
|
fVertexAttribIndices[1] = stage.fVertexAttribIndices[1];
|
2013-01-23 23:53:46 +04:00
|
|
|
}
|
|
|
|
SkDEBUGCODE(fInitialized = true;)
|
|
|
|
}
|
|
|
|
|
|
|
|
void restoreTo(GrEffectStage* stage) {
|
|
|
|
GrAssert(fInitialized);
|
|
|
|
const GrEffectRef* oldEffectRef = stage->fEffectRef;
|
|
|
|
if (NULL != fEffect) {
|
|
|
|
stage->fEffectRef = GrEffect::CreateEffectRef(fEffect);
|
|
|
|
stage->fCoordChangeMatrix = fCoordChangeMatrix;
|
2013-03-19 22:51:02 +04:00
|
|
|
stage->fVertexAttribIndices[0] = fVertexAttribIndices[0];
|
|
|
|
stage->fVertexAttribIndices[1] = fVertexAttribIndices[1];
|
2013-01-23 23:53:46 +04:00
|
|
|
} else {
|
|
|
|
stage->fEffectRef = NULL;
|
|
|
|
}
|
|
|
|
SkSafeUnref(oldEffectRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqual(const GrEffectStage& stage) const {
|
|
|
|
if (NULL == stage.fEffectRef) {
|
|
|
|
return NULL == fEffect;
|
|
|
|
} else if (NULL == fEffect) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
if (fVertexAttribIndices[0] != stage.fVertexAttribIndices[0]
|
|
|
|
|| fVertexAttribIndices[1] != stage.fVertexAttribIndices[1]) {
|
2013-01-23 23:53:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-13 11:01:04 +04:00
|
|
|
if (!(*stage.getEffect())->isEqual(*fEffect)) {
|
|
|
|
return false;
|
2013-03-12 16:26:08 +04:00
|
|
|
}
|
|
|
|
|
2013-01-23 23:53:46 +04:00
|
|
|
return fCoordChangeMatrix == stage.fCoordChangeMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const GrEffect* fEffect;
|
|
|
|
SkMatrix fCoordChangeMatrix;
|
2013-03-19 22:51:02 +04:00
|
|
|
int fVertexAttribIndices[2];
|
2013-01-23 23:53:46 +04:00
|
|
|
SkDEBUGCODE(bool fInitialized;)
|
|
|
|
};
|
|
|
|
|
2012-10-26 21:53:18 +04:00
|
|
|
/**
|
|
|
|
* Gets the matrix representing all changes of coordinate system since the GrEffect was
|
|
|
|
* installed in the stage.
|
|
|
|
*/
|
2012-11-01 22:02:54 +04:00
|
|
|
const SkMatrix& getCoordChangeMatrix() const { return fCoordChangeMatrix; }
|
2012-10-26 21:53:18 +04:00
|
|
|
|
2011-12-12 22:45:07 +04:00
|
|
|
void reset() {
|
2013-01-16 19:51:47 +04:00
|
|
|
GrSafeSetNull(fEffectRef);
|
2011-12-06 20:30:36 +04:00
|
|
|
}
|
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
const GrEffectRef* setEffect(const GrEffectRef* EffectRef) {
|
2012-10-17 16:53:54 +04:00
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
2013-01-16 19:51:47 +04:00
|
|
|
GrSafeAssign(fEffectRef, EffectRef);
|
2012-10-17 16:53:54 +04:00
|
|
|
fCoordChangeMatrix.reset();
|
2013-03-12 16:26:08 +04:00
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
fVertexAttribIndices[0] = -1;
|
|
|
|
fVertexAttribIndices[1] = -1;
|
2013-03-20 11:01:02 +04:00
|
|
|
|
2013-01-16 19:51:47 +04:00
|
|
|
return EffectRef;
|
2012-10-16 19:19:45 +04:00
|
|
|
}
|
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
const GrEffectRef* setEffect(const GrEffectRef* EffectRef, int attr0, int attr1 = -1) {
|
|
|
|
GrAssert(0 == fSavedCoordChangeCnt);
|
|
|
|
GrSafeAssign(fEffectRef, EffectRef);
|
|
|
|
fCoordChangeMatrix.reset();
|
2013-03-20 11:01:02 +04:00
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
fVertexAttribIndices[0] = attr0;
|
|
|
|
fVertexAttribIndices[1] = attr1;
|
2013-03-20 11:01:02 +04:00
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
return EffectRef;
|
|
|
|
}
|
2013-03-20 11:01:02 +04:00
|
|
|
|
2013-01-22 23:55:59 +04:00
|
|
|
const GrEffectRef* getEffect() const { return fEffectRef; }
|
2012-04-20 22:35:38 +04:00
|
|
|
|
2013-03-19 22:51:02 +04:00
|
|
|
const int* getVertexAttribIndices() const { return fVertexAttribIndices; }
|
|
|
|
int getVertexAttribIndexCount() const { return fEffectRef->get()->numVertexAttribs(); }
|
2013-03-12 16:26:08 +04:00
|
|
|
|
2010-12-23 00:39:39 +03:00
|
|
|
private:
|
2013-03-12 16:26:08 +04:00
|
|
|
SkMatrix fCoordChangeMatrix;
|
|
|
|
const GrEffectRef* fEffectRef;
|
2013-03-19 22:51:02 +04:00
|
|
|
int fVertexAttribIndices[2];
|
2012-10-17 16:53:54 +04:00
|
|
|
|
|
|
|
GR_DEBUGCODE(mutable int fSavedCoordChangeCnt;)
|
2010-12-23 00:39:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|