Tunnel name requests through factory, forcing custom effect and custom prog stage to use same impl
Review URL: http://codereview.appspot.com/6220061/
git-svn-id: http://skia.googlecode.com/svn/trunk@4019 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrCustomStage.h b/include/gpu/GrCustomStage.h
index fd84425..9db4244 100644
--- a/include/gpu/GrCustomStage.h
+++ b/include/gpu/GrCustomStage.h
@@ -15,7 +15,10 @@
class GrContext;
/** Provides custom vertex shader, fragment shader, uniform data for a
- particular stage of the Ganesh shading pipeline. */
+ particular stage of the Ganesh shading pipeline.
+ Subclasses must have a function that produces a human-readable name:
+ static const char* Name();
+ */
class GrCustomStage : public GrRefCnt {
public:
@@ -24,10 +27,6 @@
GrCustomStage();
virtual ~GrCustomStage();
- /** Human-meaningful string to identify this effect; may be embedded
- in generated shader code. */
- virtual const char* name() const = 0;
-
/** If given an input texture that is/is not opaque, is this
stage guaranteed to produce an opaque output? */
virtual bool isOpaque(bool inputTextureIsOpaque) const;
@@ -60,6 +59,10 @@
of the stageKey produced by the GrProgramStageFactory. */
virtual bool isEqual(const GrCustomStage *) const = 0;
+ /** Human-meaningful string to identify this effect; may be embedded
+ in generated shader code. */
+ const char* name() const { return this->getFactory().name(); }
+
private:
typedef GrRefCnt INHERITED;
diff --git a/include/gpu/GrProgramStageFactory.h b/include/gpu/GrProgramStageFactory.h
index e5f05f7..1ce0393 100644
--- a/include/gpu/GrProgramStageFactory.h
+++ b/include/gpu/GrProgramStageFactory.h
@@ -37,6 +37,8 @@
return !(*this == b);
}
+ virtual const char* name() const = 0;
+
protected:
enum {
kIllegalStageClassID = 0,
@@ -66,7 +68,12 @@
public:
typedef typename StageClass::GLProgramStage GLProgramStage;
-
+
+ /** Returns a human-readable name that is accessible via GrCustomStage or
+ GrGLProgramStage and is consistent between the two of them.
+ */
+ virtual const char* name() const SK_OVERRIDE { return StageClass::Name(); }
+
/** Returns an value that idenitifes the shader code generated by
a GrCustomStage. This enables caching of generated shaders. Part of the
id identifies the GrCustomShader subclass. The remainder is based
@@ -88,9 +95,11 @@
the object. */
virtual GLProgramStage* createGLInstance(
const GrCustomStage* stage) const SK_OVERRIDE {
- return new GLProgramStage(stage);
+ return new GLProgramStage(*this, stage);
}
+ /** This class is a singleton. This function returns the single instance.
+ */
static const GrProgramStageFactory& getInstance() {
static SkAlignedSTStorage<1, GrTProgramStageFactory> gInstanceMem;
static const GrTProgramStageFactory* gInstance;
diff --git a/src/gpu/effects/GrConvolutionEffect.cpp b/src/gpu/effects/GrConvolutionEffect.cpp
index 563d1a4..29d1573 100644
--- a/src/gpu/effects/GrConvolutionEffect.cpp
+++ b/src/gpu/effects/GrConvolutionEffect.cpp
@@ -17,8 +17,8 @@
public:
- GrGLConvolutionEffect(const GrCustomStage* stage);
- virtual const char* name() const SK_OVERRIDE;
+ GrGLConvolutionEffect(const GrProgramStageFactory& factory,
+ const GrCustomStage* stage);
virtual void setupVSUnis(VarArray* vsUnis, int stage) SK_OVERRIDE;
virtual void setupFSUnis(VarArray* fsUnis, int stage) SK_OVERRIDE;
virtual void emitVS(GrStringBuilder* code,
@@ -49,18 +49,17 @@
typedef GrGLProgramStage INHERITED;
};
-GrGLConvolutionEffect::GrGLConvolutionEffect(const GrCustomStage* data)
- : fKernelVar(NULL)
+GrGLConvolutionEffect::GrGLConvolutionEffect(
+ const GrProgramStageFactory& factory,
+ const GrCustomStage* data)
+ : GrGLProgramStage(factory)
+ , fKernelVar(NULL)
, fImageIncrementVar(NULL)
, fKernelLocation(0)
, fImageIncrementLocation(0) {
fKernelWidth = static_cast<const GrConvolutionEffect*>(data)->width();
}
-const char* GrGLConvolutionEffect::name() const {
- return GrConvolutionEffect::Name();
-}
-
void GrGLConvolutionEffect::setupVSUnis(VarArray* vsUnis,
int stage) {
fImageIncrementVar = &vsUnis->push_back();
@@ -188,11 +187,6 @@
}
-const char* GrConvolutionEffect::name() const {
- return Name();
-}
-
-
const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
}
diff --git a/src/gpu/effects/GrConvolutionEffect.h b/src/gpu/effects/GrConvolutionEffect.h
index 4fdddd9..df164d5 100644
--- a/src/gpu/effects/GrConvolutionEffect.h
+++ b/src/gpu/effects/GrConvolutionEffect.h
@@ -24,12 +24,11 @@
unsigned int width() const { return fKernelWidth; }
const float* kernel() const { return fKernel; }
GrSamplerState::FilterDirection direction() const { return fDirection; }
-
+
static const char* Name() { return "Convolution"; }
typedef GrGLConvolutionEffect GLProgramStage;
-
- virtual const char* name() const SK_OVERRIDE;
+
virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
virtual bool isEqual(const GrCustomStage *) const SK_OVERRIDE;
diff --git a/src/gpu/gl/GrGLProgramStage.cpp b/src/gpu/gl/GrGLProgramStage.cpp
index 28a711d..1221631 100644
--- a/src/gpu/gl/GrGLProgramStage.cpp
+++ b/src/gpu/gl/GrGLProgramStage.cpp
@@ -8,6 +8,10 @@
#include "GrGLSL.h"
#include "GrGLProgramStage.h"
+GrGLProgramStage::GrGLProgramStage(const GrProgramStageFactory& factory)
+ : fFactory(factory) {
+}
+
GrGLProgramStage::~GrGLProgramStage() {
}
diff --git a/src/gpu/gl/GrGLProgramStage.h b/src/gpu/gl/GrGLProgramStage.h
index 2141640..df7bdb8 100644
--- a/src/gpu/gl/GrGLProgramStage.h
+++ b/src/gpu/gl/GrGLProgramStage.h
@@ -39,10 +39,10 @@
};
typedef GrTAllocator<GrGLShaderVar> VarArray;
-
- virtual ~GrGLProgramStage();
- virtual const char* name() const = 0;
+ GrGLProgramStage(const GrProgramStageFactory&);
+
+ virtual ~GrGLProgramStage();
/** Creates any uniform variables the vertex shader requires
and appends them to vsUnis;
@@ -107,6 +107,12 @@
updates the name of the sample coordinates. */
void emitTextureSetup(GrGLShaderBuilder* segments);
+ /** Human-meaningful string to identify this effect; may be embedded
+ in generated shader code. Because the implementation is delegated to
+ the factory, the name will be the same as that of the generating
+ GrCustomStage. */
+ const char* name() const { return fFactory.name(); }
+
protected:
/** Convenience function for subclasses to write texture2D() or
@@ -116,6 +122,8 @@
const char* coordName);
SamplerMode fSamplerMode;
+
+ const GrProgramStageFactory& fFactory;
};
#endif