gfx/skia/trunk/include/gpu/GrEffect.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/gpu/GrEffect.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,365 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef GrEffect_DEFINED
    1.12 +#define GrEffect_DEFINED
    1.13 +
    1.14 +#include "GrColor.h"
    1.15 +#include "GrEffectUnitTest.h"
    1.16 +#include "GrTexture.h"
    1.17 +#include "GrTextureAccess.h"
    1.18 +#include "GrTypesPriv.h"
    1.19 +
    1.20 +class GrBackendEffectFactory;
    1.21 +class GrContext;
    1.22 +class GrCoordTransform;
    1.23 +class GrEffect;
    1.24 +class GrVertexEffect;
    1.25 +class SkString;
    1.26 +
    1.27 +/**
    1.28 + * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue
    1.29 + * new draw operations separately from ownership within a deferred drawing queue. When the
    1.30 + * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled
    1.31 + * in service of later draws. However, the deferred draw queue may still own direct references to
    1.32 + * the underlying GrEffect.
    1.33 + *
    1.34 + * GrEffectRefs created by new are placed in a per-thread managed pool. The pool is destroyed when
    1.35 + * the thread ends. Therefore, all dynamically allocated GrEffectRefs must be unreffed before thread
    1.36 + * termination.
    1.37 + */
    1.38 +class GrEffectRef : public SkRefCnt {
    1.39 +public:
    1.40 +    SK_DECLARE_INST_COUNT(GrEffectRef);
    1.41 +    virtual ~GrEffectRef();
    1.42 +
    1.43 +    GrEffect* get() { return fEffect; }
    1.44 +    const GrEffect* get() const { return fEffect; }
    1.45 +
    1.46 +    const GrEffect* operator-> () { return fEffect; }
    1.47 +    const GrEffect* operator-> () const { return fEffect; }
    1.48 +
    1.49 +    void* operator new(size_t size);
    1.50 +    void operator delete(void* target);
    1.51 +
    1.52 +    void* operator new(size_t size, void* placement) {
    1.53 +        return ::operator new(size, placement);
    1.54 +    }
    1.55 +    void operator delete(void* target, void* placement) {
    1.56 +        ::operator delete(target, placement);
    1.57 +    }
    1.58 +
    1.59 +private:
    1.60 +    friend class GrEffect; // to construct these
    1.61 +
    1.62 +    explicit GrEffectRef(GrEffect* effect);
    1.63 +
    1.64 +    GrEffect* fEffect;
    1.65 +
    1.66 +    typedef SkRefCnt INHERITED;
    1.67 +};
    1.68 +
    1.69 +/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
    1.70 +    Ganesh shading pipeline.
    1.71 +    Subclasses must have a function that produces a human-readable name:
    1.72 +        static const char* Name();
    1.73 +    GrEffect objects *must* be immutable: after being constructed, their fields may not change.
    1.74 +
    1.75 +    GrEffect subclass objects should be created by factory functions that return GrEffectRef.
    1.76 +    There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
    1.77 +    member function of a GrEffect subclass.
    1.78 +
    1.79 +    Because almost no code should ever handle a GrEffect directly outside of a GrEffectRef, we
    1.80 +    privately inherit from SkRefCnt to help prevent accidental direct ref'ing/unref'ing of effects.
    1.81 +
    1.82 +    Dynamically allocated GrEffects and their corresponding GrEffectRefs are managed by a per-thread
    1.83 +    memory pool. The ref count of an effect must reach 0 before the thread terminates and the pool
    1.84 +    is destroyed. To create a static effect use the macro GR_CREATE_STATIC_EFFECT declared below.
    1.85 +  */
    1.86 +class GrEffect : private SkRefCnt {
    1.87 +public:
    1.88 +    SK_DECLARE_INST_COUNT(GrEffect)
    1.89 +
    1.90 +    virtual ~GrEffect();
    1.91 +
    1.92 +    /**
    1.93 +     * This function is used to perform optimizations. When called the color and validFlags params
    1.94 +     * indicate whether the input components to this effect in the FS will have known values.
    1.95 +     * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to
    1.96 +     * indicate known values of its output. A component of the color param only has meaning if the
    1.97 +     * corresponding bit in validFlags is set.
    1.98 +     */
    1.99 +    virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
   1.100 +
   1.101 +    /** Will this effect read the source color value? */
   1.102 +    bool willUseInputColor() const { return fWillUseInputColor; }
   1.103 +
   1.104 +    /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
   1.105 +        identification. The factory should be an instance of templated class,
   1.106 +        GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
   1.107 +        a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
   1.108 +        by the factory.
   1.109 +
   1.110 +        Example:
   1.111 +        class MyCustomEffect : public GrEffect {
   1.112 +        ...
   1.113 +            virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
   1.114 +                return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
   1.115 +            }
   1.116 +        ...
   1.117 +        };
   1.118 +     */
   1.119 +    virtual const GrBackendEffectFactory& getFactory() const = 0;
   1.120 +
   1.121 +    /** Returns true if this and other effect conservatively draw identically. It can only return
   1.122 +        true when the two effects are of the same subclass (i.e. they return the same object from
   1.123 +        from getFactory()).
   1.124 +
   1.125 +        A return value of true from isEqual() should not be used to test whether the effects would
   1.126 +        generate the same shader code. To test for identical code generation use the EffectKey
   1.127 +        computed by the GrBackendEffectFactory:
   1.128 +            effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB).
   1.129 +     */
   1.130 +    bool isEqual(const GrEffectRef& other) const {
   1.131 +        return this->isEqual(*other.get());
   1.132 +    }
   1.133 +
   1.134 +    /** Human-meaningful string to identify this effect; may be embedded
   1.135 +        in generated shader code. */
   1.136 +    const char* name() const;
   1.137 +
   1.138 +    int numTransforms() const { return fCoordTransforms.count(); }
   1.139 +
   1.140 +    /** Returns the coordinate transformation at index. index must be valid according to
   1.141 +        numTransforms(). */
   1.142 +    const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
   1.143 +
   1.144 +    int numTextures() const { return fTextureAccesses.count(); }
   1.145 +
   1.146 +    /** Returns the access pattern for the texture at index. index must be valid according to
   1.147 +        numTextures(). */
   1.148 +    const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
   1.149 +
   1.150 +    /** Shortcut for textureAccess(index).texture(); */
   1.151 +    GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
   1.152 +
   1.153 +    /** Will this effect read the destination pixel value? */
   1.154 +    bool willReadDstColor() const { return fWillReadDstColor; }
   1.155 +
   1.156 +    /** Will this effect read the fragment position? */
   1.157 +    bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
   1.158 +
   1.159 +    /** Will this effect emit custom vertex shader code?
   1.160 +        (To set this value the effect must inherit from GrVertexEffect.) */
   1.161 +    bool hasVertexCode() const { return fHasVertexCode; }
   1.162 +
   1.163 +    int numVertexAttribs() const {
   1.164 +        SkASSERT(0 == fVertexAttribTypes.count() || fHasVertexCode);
   1.165 +        return fVertexAttribTypes.count();
   1.166 +    }
   1.167 +
   1.168 +    GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
   1.169 +
   1.170 +    static const int kMaxVertexAttribs = 2;
   1.171 +
   1.172 +    /** Useful for effects that want to insert a texture matrix that is implied by the texture
   1.173 +        dimensions */
   1.174 +    static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
   1.175 +        SkASSERT(NULL != texture);
   1.176 +        SkMatrix mat;
   1.177 +        mat.setIDiv(texture->width(), texture->height());
   1.178 +        return mat;
   1.179 +    }
   1.180 +
   1.181 +    void* operator new(size_t size);
   1.182 +    void operator delete(void* target);
   1.183 +
   1.184 +    void* operator new(size_t size, void* placement) {
   1.185 +        return ::operator new(size, placement);
   1.186 +    }
   1.187 +    void operator delete(void* target, void* placement) {
   1.188 +        ::operator delete(target, placement);
   1.189 +    }
   1.190 +
   1.191 +    /** These functions are used when recording effects into a deferred drawing queue. The inc call
   1.192 +        keeps the effect alive outside of GrEffectRef while allowing any resources owned by the
   1.193 +        effect to be returned to the cache for reuse. The dec call must balance the inc call. */
   1.194 +    void incDeferredRefCounts() const {
   1.195 +        this->ref();
   1.196 +        int count = fTextureAccesses.count();
   1.197 +        for (int t = 0; t < count; ++t) {
   1.198 +            fTextureAccesses[t]->getTexture()->incDeferredRefCount();
   1.199 +        }
   1.200 +    }
   1.201 +    void decDeferredRefCounts() const {
   1.202 +        int count = fTextureAccesses.count();
   1.203 +        for (int t = 0; t < count; ++t) {
   1.204 +            fTextureAccesses[t]->getTexture()->decDeferredRefCount();
   1.205 +        }
   1.206 +        this->unref();
   1.207 +    }
   1.208 +
   1.209 +protected:
   1.210 +    /**
   1.211 +     * Subclasses call this from their constructor to register coordinate transformations. The
   1.212 +     * effect subclass manages the lifetime of the transformations (this function only stores a
   1.213 +     * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the
   1.214 +     * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll
   1.215 +     * have 2. This must only be called from the constructor because GrEffects are immutable.
   1.216 +     */
   1.217 +    void addCoordTransform(const GrCoordTransform* coordTransform);
   1.218 +
   1.219 +    /**
   1.220 +     * Subclasses call this from their constructor to register GrTextureAccesses. The effect
   1.221 +     * subclass manages the lifetime of the accesses (this function only stores a pointer). The
   1.222 +     * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be
   1.223 +     * called from the constructor because GrEffects are immutable.
   1.224 +     */
   1.225 +    void addTextureAccess(const GrTextureAccess* textureAccess);
   1.226 +
   1.227 +    GrEffect()
   1.228 +        : fWillReadDstColor(false)
   1.229 +        , fWillReadFragmentPosition(false)
   1.230 +        , fWillUseInputColor(true)
   1.231 +        , fHasVertexCode(false)
   1.232 +        , fEffectRef(NULL) {}
   1.233 +
   1.234 +    /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for
   1.235 +        an example factory function. */
   1.236 +    static GrEffectRef* CreateEffectRef(GrEffect* effect) {
   1.237 +        if (NULL == effect->fEffectRef) {
   1.238 +            effect->fEffectRef = SkNEW_ARGS(GrEffectRef, (effect));
   1.239 +        } else {
   1.240 +            effect->fEffectRef->ref();
   1.241 +        }
   1.242 +        return effect->fEffectRef;
   1.243 +    }
   1.244 +
   1.245 +    static const GrEffectRef* CreateEffectRef(const GrEffect* effect) {
   1.246 +        return CreateEffectRef(const_cast<GrEffect*>(effect));
   1.247 +    }
   1.248 +
   1.249 +    /** Used by GR_CREATE_STATIC_EFFECT below */
   1.250 +    static GrEffectRef* CreateStaticEffectRef(void* refStorage, GrEffect* effect) {
   1.251 +        SkASSERT(NULL == effect->fEffectRef);
   1.252 +        effect->fEffectRef = SkNEW_PLACEMENT_ARGS(refStorage, GrEffectRef, (effect));
   1.253 +        return effect->fEffectRef;
   1.254 +    }
   1.255 +
   1.256 +
   1.257 +    /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a
   1.258 +        GrEffectRef. E.g.:
   1.259 +
   1.260 +        class EffectSubclass : public GrEffect {
   1.261 +        public:
   1.262 +            GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
   1.263 +                AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...)));
   1.264 +                return CreateEffectRef(effect);
   1.265 +            }
   1.266 +     */
   1.267 +    class AutoEffectUnref {
   1.268 +    public:
   1.269 +        AutoEffectUnref(GrEffect* effect) : fEffect(effect) { }
   1.270 +        ~AutoEffectUnref() { fEffect->unref(); }
   1.271 +        operator GrEffect*() { return fEffect; }
   1.272 +    private:
   1.273 +        GrEffect* fEffect;
   1.274 +    };
   1.275 +
   1.276 +    /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass
   1.277 +      */
   1.278 +    template <typename T>
   1.279 +    static const T& CastEffect(const GrEffect& effectRef) {
   1.280 +        return *static_cast<const T*>(&effectRef);
   1.281 +    }
   1.282 +
   1.283 +    /**
   1.284 +     * If the effect subclass will read the destination pixel value then it must call this function
   1.285 +     * from its constructor. Otherwise, when its generated backend-specific effect class attempts
   1.286 +     * to generate code that reads the destination pixel it will fail.
   1.287 +     */
   1.288 +    void setWillReadDstColor() { fWillReadDstColor = true; }
   1.289 +
   1.290 +    /**
   1.291 +     * If the effect will generate a backend-specific effect that will read the fragment position
   1.292 +     * in the FS then it must call this method from its constructor. Otherwise, the request to
   1.293 +     * access the fragment position will be denied.
   1.294 +     */
   1.295 +    void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
   1.296 +
   1.297 +    /**
   1.298 +     * If the effect will generate a result that does not depend on the input color value then it must
   1.299 +     * call this function from its constructor. Otherwise, when its generated backend-specific code
   1.300 +     * might fail during variable binding due to unused variables.
   1.301 +     */
   1.302 +    void setWillNotUseInputColor() { fWillUseInputColor = false; }
   1.303 +
   1.304 +private:
   1.305 +    bool isEqual(const GrEffect& other) const {
   1.306 +        if (&this->getFactory() != &other.getFactory()) {
   1.307 +            return false;
   1.308 +        }
   1.309 +        bool result = this->onIsEqual(other);
   1.310 +#ifdef SK_DEBUG
   1.311 +        if (result) {
   1.312 +            this->assertEquality(other);
   1.313 +        }
   1.314 +#endif
   1.315 +        return result;
   1.316 +    }
   1.317 +
   1.318 +    SkDEBUGCODE(void assertEquality(const GrEffect& other) const;)
   1.319 +
   1.320 +    /** Subclass implements this to support isEqual(). It will only be called if it is known that
   1.321 +        the two effects are of the same subclass (i.e. they return the same object from
   1.322 +        getFactory()).*/
   1.323 +    virtual bool onIsEqual(const GrEffect& other) const = 0;
   1.324 +
   1.325 +    void EffectRefDestroyed() { fEffectRef = NULL; }
   1.326 +
   1.327 +    friend class GrEffectRef;    // to call EffectRefDestroyed()
   1.328 +    friend class GrEffectStage;  // to rewrap GrEffect in GrEffectRef when restoring an effect-stage
   1.329 +                                 // from deferred state, to call isEqual on naked GrEffects, and
   1.330 +                                 // to inc/dec deferred ref counts.
   1.331 +    friend class GrVertexEffect; // to set fHasVertexCode and build fVertexAttribTypes.
   1.332 +
   1.333 +    SkSTArray<4, const GrCoordTransform*, true>  fCoordTransforms;
   1.334 +    SkSTArray<4, const GrTextureAccess*, true>   fTextureAccesses;
   1.335 +    SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
   1.336 +    bool                                         fWillReadDstColor;
   1.337 +    bool                                         fWillReadFragmentPosition;
   1.338 +    bool                                         fWillUseInputColor;
   1.339 +    bool                                         fHasVertexCode;
   1.340 +    GrEffectRef*                                 fEffectRef;
   1.341 +
   1.342 +    typedef SkRefCnt INHERITED;
   1.343 +};
   1.344 +
   1.345 +inline GrEffectRef::GrEffectRef(GrEffect* effect) {
   1.346 +    SkASSERT(NULL != effect);
   1.347 +    effect->ref();
   1.348 +    fEffect = effect;
   1.349 +}
   1.350 +
   1.351 +/**
   1.352 + * This creates an effect outside of the effect memory pool. The effect's destructor will be called
   1.353 + * at global destruction time. NAME will be the name of the created GrEffectRef.
   1.354 + */
   1.355 +#define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS)                                         \
   1.356 +enum {                                                                                            \
   1.357 +    k_##NAME##_EffectRefOffset = GR_CT_ALIGN_UP(sizeof(EFFECT_CLASS), 8),                         \
   1.358 +    k_##NAME##_StorageSize = k_##NAME##_EffectRefOffset + sizeof(GrEffectRef)                     \
   1.359 +};                                                                                                \
   1.360 +static SkAlignedSStorage<k_##NAME##_StorageSize> g_##NAME##_Storage;                              \
   1.361 +static void* NAME##_RefLocation = (char*)g_##NAME##_Storage.get() + k_##NAME##_EffectRefOffset;   \
   1.362 +static GrEffect* NAME##_Effect SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS);\
   1.363 +static SkAutoTDestroy<GrEffect> NAME##_ad(NAME##_Effect);                                         \
   1.364 +static GrEffectRef* NAME(GrEffect::CreateStaticEffectRef(NAME##_RefLocation, NAME##_Effect));     \
   1.365 +static SkAutoTDestroy<GrEffectRef> NAME##_Ref_ad(NAME)
   1.366 +
   1.367 +
   1.368 +#endif

mercurial