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:
|
2013-06-13 23:34:18 +04:00
|
|
|
explicit GrEffectStage(const GrEffectRef* effectRef, int attrIndex0 = -1, int attrIndex1 = -1)
|
2013-07-15 17:54:06 +04:00
|
|
|
: fEffectRef(SkRef(effectRef)) {
|
2013-06-13 23:34:18 +04:00
|
|
|
fCoordChangeMatrixSet = false;
|
|
|
|
fVertexAttribIndices[0] = attrIndex0;
|
|
|
|
fVertexAttribIndices[1] = attrIndex1;
|
2010-12-23 00:39:39 +03:00
|
|
|
}
|
|
|
|
|
2013-06-13 23:34:18 +04:00
|
|
|
GrEffectStage(const GrEffectStage& other) {
|
|
|
|
*this = other;
|
2012-04-20 22:35:38 +04:00
|
|
|
}
|
|
|
|
|
2013-07-15 17:54:06 +04:00
|
|
|
class DeferredStage;
|
|
|
|
// This constructor balances DeferredStage::saveFrom().
|
|
|
|
explicit GrEffectStage(const DeferredStage& deferredStage) {
|
|
|
|
deferredStage.restoreTo(this);
|
|
|
|
}
|
|
|
|
|
2013-06-13 23:34:18 +04:00
|
|
|
GrEffectStage& operator= (const GrEffectStage& other) {
|
|
|
|
fCoordChangeMatrixSet = other.fCoordChangeMatrixSet;
|
|
|
|
if (other.fCoordChangeMatrixSet) {
|
|
|
|
fCoordChangeMatrix = other.fCoordChangeMatrix;
|
|
|
|
}
|
2013-07-15 17:54:06 +04:00
|
|
|
fEffectRef.reset(SkRef(other.fEffectRef.get()));
|
2013-06-13 23:34:18 +04:00
|
|
|
memcpy(fVertexAttribIndices, other.fVertexAttribIndices, sizeof(fVertexAttribIndices));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (const GrEffectStage& other) const {
|
2013-08-17 04:02:59 +04:00
|
|
|
SkASSERT(NULL != fEffectRef.get());
|
|
|
|
SkASSERT(NULL != other.fEffectRef.get());
|
2012-10-17 16:53:54 +04:00
|
|
|
|
2013-01-22 23:55:59 +04:00
|
|
|
if (!(*this->getEffect())->isEqual(*other.getEffect())) {
|
2012-10-17 16:53:54 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-25 21:23:46 +04:00
|
|
|
if (fCoordChangeMatrixSet != other.fCoordChangeMatrixSet) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fCoordChangeMatrixSet) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-05-28 21:25:49 +04:00
|
|
|
bool operator!= (const GrEffectStage& s) const { return !(*this == s); }
|
2012-05-01 00:19:07 +04:00
|
|
|
|
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 23:19:53 +04:00
|
|
|
* @param matrix The transformation from the old coord system in which geometry is specified
|
|
|
|
* to the new one from which it will actually be drawn.
|
2011-02-17 19:43:10 +03:00
|
|
|
*/
|
2013-04-26 11:00:58 +04:00
|
|
|
void localCoordChange(const SkMatrix& matrix) {
|
2013-04-25 21:23:46 +04:00
|
|
|
if (fCoordChangeMatrixSet) {
|
2013-04-26 11:00:58 +04:00
|
|
|
fCoordChangeMatrix.preConcat(matrix);
|
2013-04-25 21:23:46 +04:00
|
|
|
} else {
|
|
|
|
fCoordChangeMatrixSet = true;
|
|
|
|
fCoordChangeMatrix = matrix;
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 16:53:54 +04:00
|
|
|
|
|
|
|
class SavedCoordChange {
|
|
|
|
private:
|
2013-04-25 21:23:46 +04:00
|
|
|
bool fCoordChangeMatrixSet;
|
2012-11-01 22:02:54 +04:00
|
|
|
SkMatrix fCoordChangeMatrix;
|
2013-09-07 03:13:05 +04:00
|
|
|
SkDEBUGCODE(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 23:19:53 +04:00
|
|
|
* localCoordChange 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 {
|
2013-04-25 21:23:46 +04:00
|
|
|
savedCoordChange->fCoordChangeMatrixSet = fCoordChangeMatrixSet;
|
|
|
|
if (fCoordChangeMatrixSet) {
|
|
|
|
savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix;
|
|
|
|
}
|
2013-08-17 04:02:59 +04:00
|
|
|
SkASSERT(NULL == savedCoordChange->fEffectRef.get());
|
2013-09-07 03:13:05 +04:00
|
|
|
SkDEBUGCODE(SkRef(fEffectRef.get());)
|
|
|
|
SkDEBUGCODE(savedCoordChange->fEffectRef.reset(fEffectRef.get());)
|
2012-10-17 16:53:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This balances the saveCoordChange call.
|
|
|
|
*/
|
|
|
|
void restoreCoordChange(const SavedCoordChange& savedCoordChange) {
|
2013-04-25 21:23:46 +04:00
|
|
|
fCoordChangeMatrixSet = savedCoordChange.fCoordChangeMatrixSet;
|
|
|
|
if (fCoordChangeMatrixSet) {
|
|
|
|
fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix;
|
|
|
|
}
|
2013-08-17 04:02:59 +04:00
|
|
|
SkASSERT(savedCoordChange.fEffectRef.get() == fEffectRef);
|
2013-09-07 03:13:05 +04:00
|
|
|
SkDEBUGCODE(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) {
|
2013-08-17 04:02:59 +04:00
|
|
|
SkASSERT(!fInitialized);
|
|
|
|
SkASSERT(NULL != stage.fEffectRef.get());
|
2013-07-15 17:54:06 +04:00
|
|
|
stage.fEffectRef->get()->incDeferredRefCounts();
|
|
|
|
fEffect = stage.fEffectRef->get();
|
|
|
|
fCoordChangeMatrixSet = stage.fCoordChangeMatrixSet;
|
|
|
|
if (fCoordChangeMatrixSet) {
|
|
|
|
fCoordChangeMatrix = stage.fCoordChangeMatrix;
|
2013-01-23 23:53:46 +04:00
|
|
|
}
|
2013-07-15 17:54:06 +04:00
|
|
|
fVertexAttribIndices[0] = stage.fVertexAttribIndices[0];
|
|
|
|
fVertexAttribIndices[1] = stage.fVertexAttribIndices[1];
|
2013-01-23 23:53:46 +04:00
|
|
|
SkDEBUGCODE(fInitialized = true;)
|
|
|
|
}
|
|
|
|
|
2013-07-15 17:54:06 +04:00
|
|
|
void restoreTo(GrEffectStage* stage) const {
|
2013-08-17 04:02:59 +04:00
|
|
|
SkASSERT(fInitialized);
|
2013-07-15 17:54:06 +04:00
|
|
|
stage->fEffectRef.reset(GrEffect::CreateEffectRef(fEffect));
|
|
|
|
stage->fCoordChangeMatrixSet = fCoordChangeMatrixSet;
|
|
|
|
if (fCoordChangeMatrixSet) {
|
|
|
|
stage->fCoordChangeMatrix = fCoordChangeMatrix;
|
2013-01-23 23:53:46 +04:00
|
|
|
}
|
2013-07-15 17:54:06 +04:00
|
|
|
stage->fVertexAttribIndices[0] = fVertexAttribIndices[0];
|
|
|
|
stage->fVertexAttribIndices[1] = fVertexAttribIndices[1];
|
2013-01-23 23:53:46 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 02:07:19 +04:00
|
|
|
bool isEqual(const GrEffectStage& stage, bool ignoreCoordChange) const {
|
2013-07-15 17:54:06 +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-06-11 02:07:19 +04:00
|
|
|
if (ignoreCoordChange) {
|
|
|
|
// ignore the coordinate change matrix since there are
|
|
|
|
// explicit uv coordinates
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-25 21:23:46 +04:00
|
|
|
if (fCoordChangeMatrixSet != stage.fCoordChangeMatrixSet) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fCoordChangeMatrixSet) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-23 23:53:46 +04:00
|
|
|
return fCoordChangeMatrix == stage.fCoordChangeMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const GrEffect* fEffect;
|
2013-04-25 21:23:46 +04:00
|
|
|
bool fCoordChangeMatrixSet;
|
2013-01-23 23:53:46 +04:00
|
|
|
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.
|
|
|
|
*/
|
2013-04-26 11:00:58 +04:00
|
|
|
const SkMatrix& getCoordChangeMatrix() const {
|
2013-04-25 21:23:46 +04:00
|
|
|
if (fCoordChangeMatrixSet) {
|
|
|
|
return fCoordChangeMatrix;
|
|
|
|
} else {
|
2013-04-26 11:00:58 +04:00
|
|
|
return SkMatrix::I();
|
2013-04-25 21:23:46 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-26 21:53:18 +04:00
|
|
|
|
2013-06-13 23:34:18 +04:00
|
|
|
const GrEffectRef* getEffect() const { return fEffectRef.get(); }
|
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-06-13 23:34:18 +04:00
|
|
|
bool fCoordChangeMatrixSet;
|
|
|
|
SkMatrix fCoordChangeMatrix;
|
|
|
|
SkAutoTUnref<const GrEffectRef> fEffectRef;
|
|
|
|
int fVertexAttribIndices[2];
|
2010-12-23 00:39:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|