Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLProgramEffects.h
index 7ae925b..6140cde 100644 (file)
@@ -8,37 +8,28 @@
 #ifndef GrGLProgramEffects_DEFINED
 #define GrGLProgramEffects_DEFINED
 
-#include "GrBackendEffectFactory.h"
+#include "GrBackendProcessorFactory.h"
 #include "GrGLProgramDataManager.h"
+#include "GrGpu.h"
 #include "GrTexture.h"
 #include "GrTextureAccess.h"
 
-class GrEffect;
-class GrEffectStage;
+class GrProcessor;
+class GrProcessorStage;
 class GrGLVertexProgramEffectsBuilder;
-class GrGLShaderBuilder;
-class GrGLFullShaderBuilder;
-class GrGLFragmentOnlyShaderBuilder;
+class GrGLProgramBuilder;
+class GrGLFullProgramBuilder;
+class GrGLFragmentOnlyProgramBuilder;
 
 /**
- * This class encapsulates an array of GrGLEffects and their supporting data (coord transforms
+ * This class encapsulates an array of GrGLProcessors and their supporting data (coord transforms
  * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL
  * state and shader uniforms.
  */
 class GrGLProgramEffects : public SkRefCnt {
 public:
     typedef GrGLProgramDataManager::UniformHandle UniformHandle;
-
-    /**
-     * This class emits some of the code inserted into the shaders for an effect. The code it
-     * creates may be dependent on properties of the effect that the effect itself doesn't use
-     * in its key (e.g. the pixel format of textures used). So this class inserts a meta-key for
-     * every effect using this function. It is also responsible for inserting the effect's class ID
-     * which must be different for every GrEffect subclass. It can fail if an effect uses too many
-     * textures, attributes, etc for the space allotted in the meta-key.
-     */
-    static bool GenEffectMetaKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
-
+    typedef GrGLProgramDataManager::VaryingHandle VaryingHandle;
     virtual ~GrGLProgramEffects();
 
     /**
@@ -51,78 +42,27 @@ public:
      * Calls setData() on each effect, and sets their transformation matrices and texture bindings.
      */
     virtual void setData(GrGpuGL*,
+                         GrGpu::DrawType,
                          const GrGLProgramDataManager&,
-                         const GrEffectStage* effectStages[]) = 0;
-
-    /**
-     * Passed to GrGLEffects so they can add transformed coordinates to their shader code.
-     */
-    class TransformedCoords {
-    public:
-        TransformedCoords(const SkString& name, GrSLType type)
-            : fName(name), fType(type) {
-        }
-
-        const char* c_str() const { return fName.c_str(); }
-        GrSLType type() const { return fType; }
-        const SkString& getName() const { return fName; }
-
-    private:
-        SkString fName;
-        GrSLType fType;
-    };
-
-    typedef SkTArray<TransformedCoords> TransformedCoordsArray;
-
-    /**
-     * Passed to GrGLEffects so they can add texture reads to their shader code.
-     */
-    class TextureSampler {
-    public:
-        TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
-            : fSamplerUniform(uniform)
-            , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
-            SkASSERT(0 != fConfigComponentMask);
-            memcpy(fSwizzle, access.getSwizzle(), 5);
-        }
-
-        UniformHandle samplerUniform() const { return fSamplerUniform; }
-        // bitfield of GrColorComponentFlags present in the texture's config.
-        uint32_t configComponentMask() const { return fConfigComponentMask; }
-        const char* swizzle() const { return fSwizzle; }
-
-    private:
-        UniformHandle fSamplerUniform;
-        uint32_t      fConfigComponentMask;
-        char          fSwizzle[5];
-    };
+                         const GrGeometryStage* effectStages) {
+        SkFAIL("For geometry processor only");
+    }
 
-    typedef SkTArray<TextureSampler> TextureSamplerArray;
+    virtual void setData(GrGpuGL*,
+                         GrGpu::DrawType,
+                         const GrGLProgramDataManager&,
+                         const GrFragmentStage* effectStages[]) = 0;
 
 protected:
-    /**
-     * Helpers for GenEffectMetaKey.
-     */
-    static uint32_t GenAttribKey(const GrDrawEffect&);
-    static uint32_t GenTransformKey(const GrDrawEffect&);
-    static uint32_t GenTextureKey(const GrDrawEffect&, const GrGLCaps&);
-
     GrGLProgramEffects(int reserveCount)
-        : fGLEffects(reserveCount)
+        : fGLProcessors(reserveCount)
         , fSamplers(reserveCount) {
     }
 
     /**
-     * Helper for emitEffect() in a subclasses. Emits uniforms for an effect's texture accesses and
-     * appends the necessary data to the TextureSamplerArray* object so effects can add texture
-     * lookups to their code. This method is only meant to be called during the construction phase.
-     */
-    void emitSamplers(GrGLShaderBuilder*, const GrEffect*, TextureSamplerArray*);
-
-    /**
      * Helper for setData(). Binds all the textures for an effect.
      */
-    void bindTextures(GrGpuGL*, const GrEffect*, int effectIdx);
+    void bindTextures(GrGpuGL*, const GrProcessor&, int effectIdx);
 
     struct Sampler {
         SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
@@ -130,28 +70,21 @@ protected:
         int           fTextureUnit;
     };
 
-    SkTArray<GrGLEffect*>                  fGLEffects;
+    /*
+     * Helpers for shader builders to build up program effects objects alongside shader code
+     */
+    void addEffect(GrGLProcessor* effect) { fGLProcessors.push_back(effect); }
+    SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); }
+
+    SkTArray<GrGLProcessor*>               fGLProcessors;
     SkTArray<SkSTArray<4, Sampler, true> > fSamplers;
 
 private:
-    typedef SkRefCnt INHERITED;
-};
-
-/**
- * This is an abstract base class for constructing different types of GrGLProgramEffects objects.
- */
-class GrGLProgramEffectsBuilder {
-public:
-    virtual ~GrGLProgramEffectsBuilder() { }
+    friend class GrGLProgramBuilder;
+    friend class GrGLFullProgramBuilder;
+    friend class GrGLFragmentOnlyShaderBuilder;
 
-    /**
-     * Emits the effect's shader code, and stores the necessary uniforms internally.
-     */
-    virtual void emitEffect(const GrEffectStage&,
-                            const GrEffectKey&,
-                            const char* outColor,
-                            const char* inColor,
-                            int stageIndex) = 0;
+    typedef SkRefCnt INHERITED;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -162,87 +95,63 @@ public:
 class GrGLVertexProgramEffects : public GrGLProgramEffects {
 public:
     virtual void setData(GrGpuGL*,
+                         GrGpu::DrawType,
                          const GrGLProgramDataManager&,
-                         const GrEffectStage* effectStages[]) SK_OVERRIDE;
+                         const GrGeometryStage* effectStages) SK_OVERRIDE;
 
-private:
-    friend class GrGLVertexProgramEffectsBuilder;
+    virtual void setData(GrGpuGL*,
+                         GrGpu::DrawType,
+                         const GrGLProgramDataManager&,
+                         const GrFragmentStage* effectStages[]) SK_OVERRIDE;
 
+private:
     GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords)
         : INHERITED(reserveCount)
         , fTransforms(reserveCount)
         , fHasExplicitLocalCoords(explicitLocalCoords) {
     }
 
-    /**
-     * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called
-     * during the construction phase.
-     */
-    void emitEffect(GrGLFullShaderBuilder*,
-                    const GrEffectStage&,
-                    const GrEffectKey&,
-                    const char* outColor,
-                    const char* inColor,
-                    int stageIndex);
+    struct Transform {
+        Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
+        UniformHandle fHandle;
+        SkMatrix      fCurrentValue;
+    };
 
-    /**
-     * Helper for emitEffect(). Emits any attributes an effect may have.
-     */
-    void emitAttributes(GrGLFullShaderBuilder*, const GrEffectStage&);
+    struct PathTransform {
+        PathTransform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
+        VaryingHandle fHandle;
+        SkMatrix fCurrentValue;
+        GrSLType fType;
+    };
 
-    /**
-     * Helper for emitEffect(). Emits code to implement an effect's coord transforms in the VS.
-     * Varyings are added as an outputs of the VS and inputs to the FS. The varyings may be either a
-     * vec2f or vec3f depending upon whether perspective interpolation is required or not. The names
-     * of the varyings in the VS and FS as well their types are appended to the
-     * TransformedCoordsArray* object, which is in turn passed to the effect's emitCode() function.
+    /*
+     * These functions are used by the builders to build up program effects along side the shader
+     * code itself
      */
-    void emitTransforms(GrGLFullShaderBuilder*,
-                        const GrDrawEffect&,
-                        TransformedCoordsArray*);
+    SkSTArray<2, Transform, true>& addTransforms() { return fTransforms.push_back(); }
+    SkTArray<PathTransform, true>& addPathTransforms() { return fPathTransforms.push_back(); }
 
     /**
      * Helper for setData(). Sets all the transform matrices for an effect.
      */
-    void setTransformData(const GrGLProgramDataManager&, const GrDrawEffect&, int effectIdx);
+    void setDataInternal(GrGpuGL* gpu,
+                         GrGpu::DrawType drawType,
+                         const GrGLProgramDataManager& programDataManager,
+                         const GrProcessorStage& effectStage,
+                         int index);
+    void setTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrProcessorStage&,
+                          int effectIdx);
+    void setPathTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&,
+                              const GrProcessorStage&, int effectIdx);
 
-    struct Transform {
-        Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
-        UniformHandle fHandle;
-        SkMatrix      fCurrentValue;
-    };
 
     SkTArray<SkSTArray<2, Transform, true> > fTransforms;
+    SkTArray<SkTArray<PathTransform, true> > fPathTransforms;
     bool                                     fHasExplicitLocalCoords;
 
-    typedef GrGLProgramEffects INHERITED;
-};
+    friend class GrGLFullProgramBuilder;
 
-/**
- * This class is used to construct a GrGLVertexProgramEffects* object.
- */
-class GrGLVertexProgramEffectsBuilder : public GrGLProgramEffectsBuilder {
-public:
-    GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder*, int reserveCount);
-    virtual ~GrGLVertexProgramEffectsBuilder() { }
-
-    virtual void emitEffect(const GrEffectStage&,
-                            const GrEffectKey&,
-                            const char* outColor,
-                            const char* inColor,
-                            int stageIndex) SK_OVERRIDE;
-
-    /**
-     * Finalizes the building process and returns the effect array. After this call, the builder
-     * becomes invalid.
-     */
-    GrGLProgramEffects* finish() { return fProgramEffects.detach(); }
-
-private:
-    GrGLFullShaderBuilder*                  fBuilder;
-    SkAutoTDelete<GrGLVertexProgramEffects> fProgramEffects;
-
-    typedef GrGLProgramEffectsBuilder INHERITED;
+    typedef GrGLProgramEffects INHERITED;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -254,82 +163,39 @@ private:
 class GrGLPathTexGenProgramEffects : public GrGLProgramEffects {
 public:
     virtual void setData(GrGpuGL*,
+                         GrGpu::DrawType,
                          const GrGLProgramDataManager&,
-                         const GrEffectStage* effectStages[]) SK_OVERRIDE;
+                         const GrFragmentStage* effectStages[]) SK_OVERRIDE;
 
 private:
-    friend class GrGLPathTexGenProgramEffectsBuilder;
-
     GrGLPathTexGenProgramEffects(int reserveCount)
         : INHERITED(reserveCount)
         , fTransforms(reserveCount) {
     }
 
     /**
-     * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called
-     * during the construction phase.
-     */
-    void emitEffect(GrGLFragmentOnlyShaderBuilder*,
-                    const GrEffectStage&,
-                    const GrEffectKey&,
-                    const char* outColor,
-                    const char* inColor,
-                    int stageIndex);
-
-    /**
-     * Helper for emitEffect(). Allocates texture units from the builder for each transform in an
-     * effect. The transforms all use adjacent texture units. They either use two or three of the
-     * coordinates at a given texture unit, depending on if they need perspective interpolation.
-     * The expressions to access the transformed coords (i.e. 'vec2(gl_TexCoord[0])') as well as the
-     * types are appended to the TransformedCoordsArray* object, which is in turn passed to the
-     * effect's emitCode() function.
-     */
-    void setupPathTexGen(GrGLFragmentOnlyShaderBuilder*,
-                         const GrDrawEffect&,
-                         TransformedCoordsArray*);
-
-    /**
      * Helper for setData(). Sets the PathTexGen state for each transform in an effect.
      */
-    void setPathTexGenState(GrGpuGL*, const GrDrawEffect&, int effectIdx);
+    void setPathTexGenState(GrGpuGL*, const GrProcessorStage&, int effectIdx);
 
     struct Transforms {
-        Transforms(uint32_t transformKey, int texCoordIndex)
-            : fTransformKey(transformKey), fTexCoordIndex(texCoordIndex) {}
-        uint32_t    fTransformKey;
-        int         fTexCoordIndex;
+        Transforms(int texCoordIndex)
+            : fTexCoordIndex(texCoordIndex) {}
+        int fTexCoordIndex;
     };
 
-    SkTArray<Transforms> fTransforms;
-
-    typedef GrGLProgramEffects INHERITED;
-};
-
-/**
- * This class is used to construct a GrGLPathTexGenProgramEffects* object.
- */
-class GrGLPathTexGenProgramEffectsBuilder : public GrGLProgramEffectsBuilder {
-public:
-    GrGLPathTexGenProgramEffectsBuilder(GrGLFragmentOnlyShaderBuilder*, int reserveCount);
-    virtual ~GrGLPathTexGenProgramEffectsBuilder() { }
-
-    virtual void emitEffect(const GrEffectStage&,
-                            const GrEffectKey&,
-                            const char* outColor,
-                            const char* inColor,
-                            int stageIndex) SK_OVERRIDE;
-
-    /**
-     * Finalizes the building process and returns the effect array. After this call, the builder
-     * becomes invalid.
+    /*
+     * Helper for fragment only shader builder to build up the program effects alongside the shader
      */
-    GrGLProgramEffects* finish() { return fProgramEffects.detach(); }
+    void addTransforms(int coordIndex) {
+        fTransforms.push_back(Transforms(coordIndex));
+    }
 
-private:
-    GrGLFragmentOnlyShaderBuilder*          fBuilder;
-    SkAutoTDelete<GrGLPathTexGenProgramEffects> fProgramEffects;
+    SkTArray<Transforms> fTransforms;
 
-    typedef GrGLProgramEffectsBuilder INHERITED;
+    friend class GrGLFragmentOnlyProgramBuilder;
+
+    typedef GrGLProgramEffects INHERITED;
 };
 
 #endif