diff options
author | joshualitt <joshualitt@chromium.org> | 2014-12-04 11:35:33 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-04 11:35:34 -0800 |
commit | eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9 (patch) | |
tree | 9567dc32598c1c0df5f5cf4a3d6594c61b95fd93 /include | |
parent | e109145bf31d63963b3f78c6af6e404d5464a55b (diff) |
Remove backend factories
BUG=skia:
Review URL: https://codereview.chromium.org/778453002
Diffstat (limited to 'include')
-rw-r--r-- | include/gpu/GrBackendProcessorFactory.h | 169 | ||||
-rw-r--r-- | include/gpu/GrFragmentProcessor.h | 27 | ||||
-rw-r--r-- | include/gpu/GrProcessor.h | 79 | ||||
-rw-r--r-- | include/gpu/GrTBackendProcessorFactory.h | 148 |
4 files changed, 78 insertions, 345 deletions
diff --git a/include/gpu/GrBackendProcessorFactory.h b/include/gpu/GrBackendProcessorFactory.h deleted file mode 100644 index dc0bbbe53f..0000000000 --- a/include/gpu/GrBackendProcessorFactory.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrBackendProcessorFactory_DEFINED -#define GrBackendProcessorFactory_DEFINED - -#include "GrTypes.h" -#include "SkTemplates.h" -#include "SkThread.h" -#include "SkTypes.h" -#include "SkTArray.h" - -class GrGLProcessor; -class GrGLCaps; -class GrProcessor; - -/** - * Used by processors to build their keys. It incorporates each per-processor key into a larger shader - * key. - */ -class GrProcessorKeyBuilder { -public: - GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { - SkASSERT(0 == fData->count() % sizeof(uint32_t)); - } - - void add32(uint32_t v) { - ++fCount; - fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); - } - - /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next - add*() call. */ - uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { - SkASSERT(count > 0); - fCount += count; - return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); - } - - size_t size() const { return sizeof(uint32_t) * fCount; } - -private: - SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. - int fCount; // number of uint32_ts added to fData by the processor. -}; - -/** - * Given a GrProcessor of a particular type, creates the corresponding graphics-backend-specific - * processor object. It also tracks equivalence of shaders generated via a key. The factory for an - * processor is accessed via GrProcessor::getFactory(). Each factory instance is assigned an ID at - * construction. The ID of GrProcessor::getFactory() is used as a type identifier. Thus, a - * GrProcessor subclass must always return the same object from getFactory() and that factory object - * must be unique to the GrProcessor subclass (and unique from any further derived subclasses). - * - * Rather than subclassing this class themselves, it is recommended that GrProcessor authors use - * the templated subclass GrTBackendProcessorFactory by writing their getFactory() method as: - * - * const GrBackendProcessorFactory& MyProcessor::getFactory() const { - * return GrTBackendProcessorFactory<MyProcessor>::getInstance(); - * } - * - * Using GrTBackendProcessorFactory places a few constraints on the processor. See that class's - * comments. - */ -class GrBackendProcessorFactory : SkNoncopyable { -public: - /** - * Produces a human-reable name for the v. - */ - virtual const char* name() const = 0; - - /** - * A unique value for every instance of this factory. It is automatically incorporated into the - * processor's key. This allows keys generated by getGLProcessorKey() to only be unique within a - * GrProcessor subclass and not necessarily across subclasses. - */ - uint32_t classID() const { return fProcessorClassID; } - -protected: - GrBackendProcessorFactory() : fProcessorClassID(GenClassID()) {} - virtual ~GrBackendProcessorFactory() {} - -private: - enum { - kIllegalProcessorClassID = 0, - }; - - static uint32_t GenClassID() { - // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The - // atomic inc returns the old value not the incremented value. So we add - // 1 to the returned value. - uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrProcessorClassID)) + 1; - if (!id) { - SkFAIL("This should never wrap as it should only be called once for each GrProcessor " - "subclass."); - } - return id; - } - - const uint32_t fProcessorClassID; - static int32_t fCurrProcessorClassID; -}; - -class GrFragmentProcessor; -class GrGeometryProcessor; -class GrXferProcessor; -class GrGLFragmentProcessor; -class GrGLGeometryProcessor; -class GrGLXferProcessor; - -/** - * Backend processor factory cannot actually create anything, it is up to subclasses to implement - * a create binding which matches Gr to GL in a type safe way - */ - -class GrBackendFragmentProcessorFactory : public GrBackendProcessorFactory { -public: - /** - * Generates an processor's key. The key is based on the aspects of the GrProcessor object's - * configuration that affect GLSL code generation. Two GrProcessor instances that would cause - * this->createGLInstance()->emitCode() to produce different code must produce different keys. - */ - virtual void getGLProcessorKey(const GrFragmentProcessor&, - const GrGLCaps&, - GrProcessorKeyBuilder*) const = 0; - - /** - * Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a - * GLSL program and to manage updating uniforms for the program when it is used. - */ - virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor&) const = 0; -}; - -class GrBackendXferProcessorFactory : public GrBackendProcessorFactory { -public: - /** - * Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a - * GLSL program and to manage updating uniforms for the program when it is used. - */ - virtual GrGLXferProcessor* createGLInstance(const GrXferProcessor&) const = 0; -}; - -class GrBatchTracker; - -class GrBackendGeometryProcessorFactory : public GrBackendProcessorFactory { -public: - /** - * Generates an processor's key. The key is based on the aspects of the GrProcessor object's - * configuration that affect GLSL code generation. Two GrProcessor instances that would cause - * this->createGLInstance()->emitCode() to produce different code must produce different keys. - */ - virtual void getGLProcessorKey(const GrGeometryProcessor&, - const GrBatchTracker&, - const GrGLCaps&, - GrProcessorKeyBuilder*) const = 0; - - /** - * Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a - * GLSL program and to manage updating uniforms for the program when it is used. - */ - virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor&, - const GrBatchTracker&) const = 0; -}; - -#endif diff --git a/include/gpu/GrFragmentProcessor.h b/include/gpu/GrFragmentProcessor.h index 044e807aae..3f308d7e75 100644 --- a/include/gpu/GrFragmentProcessor.h +++ b/include/gpu/GrFragmentProcessor.h @@ -11,6 +11,9 @@ #include "GrProcessor.h" class GrCoordTransform; +class GrGLCaps; +class GrGLFragmentProcessor; +class GrProcessorKeyBuilder; /** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and produce an output color. They may reference textures and uniforms. They may use @@ -24,7 +27,18 @@ public: , fWillReadDstColor(false) , fWillUseInputColor(true) {} - virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0; + /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ + virtual void getGLProcessorKey(const GrGLCaps& caps, + GrProcessorKeyBuilder* b) const = 0; + + /** Returns a new instance of the appropriate *GL* implementation class + for the given GrFragmentProcessor; caller is responsible for deleting + the object. */ + virtual GrGLFragmentProcessor* createGLInstance() const = 0; + + /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded + in generated shader code. */ + virtual const char* name() const = 0; int numTransforms() const { return fCoordTransforms.count(); } @@ -38,15 +52,14 @@ public: /** Will this prceossor read the source color value? */ bool willUseInputColor() const { return fWillUseInputColor; } - /** Returns true if this and other prceossor conservatively draw identically. It can only return - true when the two prceossor are of the same subclass (i.e. they return the same object from + /** Returns true if this and other processor conservatively draw identically. It can only return + true when the two processor are of the same subclass (i.e. they return the same object from from getFactory()). - A return value of true from isEqual() should not be used to test whether the prceossor would - generate the same shader code. To test for identical code generation use the prceossor' keys - computed by the GrBackendProcessorFactory. */ + A return value of true from isEqual() should not be used to test whether the processor would + generate the same shader code. To test for identical code generation use getGLProcessorKey*/ bool isEqual(const GrFragmentProcessor& that) const { - if (&this->getFactory() != &that.getFactory() || + if (this->classID() != that.classID() || !this->hasSameTransforms(that) || !this->hasSameTextureAccesses(that)) { return false; diff --git a/include/gpu/GrProcessor.h b/include/gpu/GrProcessor.h index 20123754e2..6a497e7514 100644 --- a/include/gpu/GrProcessor.h +++ b/include/gpu/GrProcessor.h @@ -8,7 +8,6 @@ #ifndef GrProcessor_DEFINED #define GrProcessor_DEFINED -#include "GrBackendProcessorFactory.h" #include "GrColor.h" #include "GrProcessorUnitTest.h" #include "GrProgramElement.h" @@ -19,6 +18,36 @@ class GrContext; class GrCoordTransform; class GrInvariantOutput; +/** + * Used by processors to build their keys. It incorporates each per-processor key into a larger + * shader key. + */ +class GrProcessorKeyBuilder { +public: + GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { + SkASSERT(0 == fData->count() % sizeof(uint32_t)); + } + + void add32(uint32_t v) { + ++fCount; + fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); + } + + /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next + add*() call. */ + uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { + SkASSERT(count > 0); + fCount += count; + return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); + } + + size_t size() const { return sizeof(uint32_t) * fCount; } + +private: + SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. + int fCount; // number of uint32_ts added to fData by the processor. +}; + /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be immutable: after being constructed, their fields may not change. @@ -42,26 +71,9 @@ public: */ void computeInvariantOutput(GrInvariantOutput* inout) const; - /** This object, besides creating back-end-specific helper objects, is used for run-time-type- - identification. The factory should be an instance of templated class, - GrTBackendProcessorFactory. It is templated on the subclass of GrProcessor. The subclass - must have a nested type (or typedef) named GLProcessor which will be the subclass of - GrGLProcessor created by the factory. - - Example: - class MyCustomProcessor : public GrProcessor { - ... - virtual const GrBackendProcessorFactory& getFactory() const SK_OVERRIDE { - return GrTBackendProcessorFactory<MyCustomProcessor>::getInstance(); - } - ... - }; - */ - virtual const GrBackendProcessorFactory& getFactory() const = 0; - /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader code. */ - const char* name() const; + virtual const char* name() const = 0; int numTextures() const { return fTextureAccesses.count(); } @@ -90,8 +102,10 @@ public: */ template <typename T> const T& cast() const { return *static_cast<const T*>(this); } + uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; } + protected: - GrProcessor() : fWillReadFragmentPosition(false) {} + GrProcessor() : fClassID(kIllegalProcessorClassID), fWillReadFragmentPosition(false) {} /** * Subclasses call this from their constructor to register GrTextureAccesses. The processor @@ -110,11 +124,35 @@ protected: */ void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } + template <typename PROC_SUBCLASS> void initClassID() { + static uint32_t kClassID = GenClassID(); + fClassID = kClassID; + } + + uint32_t fClassID; + private: /** * Subclass implements this to support getConstantColorComponents(...). */ virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; + + static uint32_t GenClassID() { + // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The + // atomic inc returns the old value not the incremented value. So we add + // 1 to the returned value. + uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1; + if (!id) { + SkFAIL("This should never wrap as it should only be called once for each GrProcessor " + "subclass."); + } + return id; + } + + enum { + kIllegalProcessorClassID = 0, + }; + static int32_t gCurrProcessorClassID; SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; bool fWillReadFragmentPosition; @@ -122,7 +160,6 @@ private: typedef GrProgramElement INHERITED; }; - /** * This creates a processor outside of the memory pool. The processor's destructor will be called * at global destruction time. NAME will be the name of the created instance. diff --git a/include/gpu/GrTBackendProcessorFactory.h b/include/gpu/GrTBackendProcessorFactory.h deleted file mode 100644 index 98b5d6cb7e..0000000000 --- a/include/gpu/GrTBackendProcessorFactory.h +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrTBackendProcessorFactory_DEFINED -#define GrTBackendProcessorFactory_DEFINED - -#include "GrBackendProcessorFactory.h" - -/** - * Implements GrBackendProcessorFactory for a GrProcessor subclass as a singleton. This can be used - * by most GrProcessor subclasses to implement the GrProcessor::getFactory() method: - * - * const GrBackendProcessorFactory& MyProcessor::getFactory() const { - * return GrTBackendProcessorFactory<MyProcessor>::getInstance(); - * } - * - * Using this class requires that the GrProcessor subclass always produces the same GrGLProcessor - * subclass. Additionally, it adds the following requirements to the GrProcessor and GrGLProcessor - * subclasses: - * - * 1. The GrGLProcessor used by GrProcessor subclass MyProcessor must be named or typedef'ed to - * MyProcessor::GLProcessor. - * 2. MyProcessor::GLProcessor must have a static function: - void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* b) - * which generates a key that maps 1 to 1 with code variations emitted by - * MyProcessor::GLProcessor::emitCode(). - * 3. MyProcessor must have a static function: - * const char* Name() - * which returns a human-readable name for the processor. - */ -template <class ProcessorClass, class BackEnd, class ProcessorBase, class GLProcessorBase> -class GrTBackendProcessorFactory : public BackEnd { -public: - typedef typename ProcessorClass::GLProcessor GLProcessor; - - /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as - * described in this class's comment. */ - virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } - - /** Returns a new instance of the appropriate *GL* implementation class - for the given GrProcessor; caller is responsible for deleting - the object. */ - virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE { - return SkNEW_ARGS(GLProcessor, (*this, processor)); - } - - /** This class is a singleton. This function returns the single instance. */ - static const BackEnd& getInstance() { - static SkAlignedSTStorage<1, GrTBackendProcessorFactory> gInstanceMem; - static const GrTBackendProcessorFactory* gInstance; - if (!gInstance) { - gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), - GrTBackendProcessorFactory); - } - return *gInstance; - } - -protected: - GrTBackendProcessorFactory() {} -}; - -/* - * Every processor so far derives from one of the following subclasses of - * GrTBackendProcessorFactory. All of this machinery is necessary to ensure that creatGLInstace is - * typesafe and does not require any casting. - */ -template <class ProcessorClass> -class GrTBackendGeometryProcessorFactory : public GrBackendGeometryProcessorFactory { -public: - typedef typename ProcessorClass::GLProcessor GLProcessor; - - /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as - * described in this class's comment. */ - virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } - - /** Implemented using GLProcessor::GenKey as described in this class's comment. */ - virtual void getGLProcessorKey(const GrGeometryProcessor& processor, - const GrBatchTracker& bt, - const GrGLCaps& caps, - GrProcessorKeyBuilder* b) const SK_OVERRIDE { - GLProcessor::GenKey(processor, bt, caps, b); - } - - - /** Returns a new instance of the appropriate *GL* implementation class - for the given GrProcessor; caller is responsible for deleting - the object. */ - virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor& gp, - const GrBatchTracker& bt) const SK_OVERRIDE { - return SkNEW_ARGS(GLProcessor, (*this, gp, bt)); - } - - /** This class is a singleton. This function returns the single instance. */ - static const GrBackendGeometryProcessorFactory& getInstance() { - static SkAlignedSTStorage<1, GrTBackendGeometryProcessorFactory> gInstanceMem; - static const GrTBackendGeometryProcessorFactory* gInstance; - if (!gInstance) { - gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), - GrTBackendGeometryProcessorFactory); - } - return *gInstance; - } -protected: - GrTBackendGeometryProcessorFactory() {} -}; - -template <class ProcessorClass> -class GrTBackendFragmentProcessorFactory : public GrBackendFragmentProcessorFactory { -public: - typedef typename ProcessorClass::GLProcessor GLProcessor; - - /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as - * described in this class's comment. */ - virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } - - /** Implemented using GLProcessor::GenKey as described in this class's comment. */ - virtual void getGLProcessorKey(const GrFragmentProcessor& processor, - const GrGLCaps& caps, - GrProcessorKeyBuilder* b) const SK_OVERRIDE { - GLProcessor::GenKey(processor, caps, b); - } - - /** Returns a new instance of the appropriate *GL* implementation class - for the given GrProcessor; caller is responsible for deleting - the object. */ - virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor& gp) const SK_OVERRIDE { - return SkNEW_ARGS(GLProcessor, (*this, gp)); - } - - /** This class is a singleton. This function returns the single instance. */ - static const GrBackendFragmentProcessorFactory& getInstance() { - static SkAlignedSTStorage<1, GrTBackendFragmentProcessorFactory> gInstanceMem; - static const GrTBackendFragmentProcessorFactory* gInstance; - if (!gInstance) { - gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), - GrTBackendFragmentProcessorFactory); - } - return *gInstance; - } -protected: - GrTBackendFragmentProcessorFactory() {} -}; - -#endif |