Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef GrEffect_DEFINED
      9 #define GrEffect_DEFINED
     10 
     11 #include "GrColor.h"
     12 #include "GrEffectUnitTest.h"
     13 #include "GrNoncopyable.h"
     14 #include "GrRefCnt.h"
     15 #include "GrTexture.h"
     16 #include "GrTextureAccess.h"
     17 
     18 class GrBackendEffectFactory;
     19 class GrContext;
     20 class GrEffect;
     21 class SkString;
     22 
     23 /**
     24  * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue
     25  * new draw operations separately from ownership within a deferred drawing queue. When the
     26  * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled
     27  * in service of later draws. However, the deferred draw queue may still own direct references to
     28  * the underlying GrEffect.
     29  */
     30 class GrEffectRef : public SkRefCnt {
     31 public:
     32     SK_DECLARE_INST_COUNT(GrEffectRef);
     33 
     34     GrEffect* get() { return fEffect; }
     35     const GrEffect* get() const { return fEffect; }
     36 
     37     const GrEffect* operator-> () { return fEffect; }
     38     const GrEffect* operator-> () const { return fEffect; }
     39 
     40     void* operator new(size_t size);
     41     void operator delete(void* target);
     42 
     43 private:
     44     friend class GrEffect; // to construct these
     45 
     46     explicit GrEffectRef(GrEffect* effect);
     47 
     48     virtual ~GrEffectRef();
     49 
     50     GrEffect* fEffect;
     51 
     52     typedef SkRefCnt INHERITED;
     53 };
     54 
     55 /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
     56     Ganesh shading pipeline.
     57     Subclasses must have a function that produces a human-readable name:
     58         static const char* Name();
     59     GrEffect objects *must* be immutable: after being constructed, their fields may not change.
     60 
     61     GrEffect subclass objects should be created by factory functions that return GrEffectRef.
     62     There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
     63     member function of a GrEffect subclass.
     64 
     65     Because almost no code should ever handle a GrEffect outside of a GrEffectRef, we privately
     66     inherit from GrRefCnt to help prevent accidental direct ref'ing/unref'ing of effects.
     67   */
     68 class GrEffect : private GrRefCnt {
     69 public:
     70     SK_DECLARE_INST_COUNT(GrEffect)
     71 
     72     virtual ~GrEffect();
     73 
     74     /**
     75      * Flags for getConstantColorComponents. They are defined so that the bit order reflects the
     76      * GrColor shift order.
     77      */
     78     enum ValidComponentFlags {
     79         kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8),
     80         kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8),
     81         kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8),
     82         kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8),
     83 
     84         kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag |
     85                                     kB_ValidComponentFlag | kA_ValidComponentFlag)
     86     };
     87 
     88     /**
     89      * This function is used to perform optimizations. When called the color and validFlags params
     90      * indicate whether the input components to this effect in the FS will have known values. The
     91      * function updates both params to indicate known values of its output. A component of the color
     92      * param only has meaning if the corresponding bit in validFlags is set.
     93      */
     94     virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
     95 
     96     /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
     97         identification. The factory should be an instance of templated class,
     98         GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
     99         a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
    100         by the factory.
    101 
    102         Example:
    103         class MyCustomEffect : public GrEffect {
    104         ...
    105             virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
    106                 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
    107             }
    108         ...
    109         };
    110      */
    111     virtual const GrBackendEffectFactory& getFactory() const = 0;
    112 
    113     /** Returns true if this and other effect conservatively draw identically. It can only return
    114         true when the two effects are of the same subclass (i.e. they return the same object from
    115         from getFactory()).
    116 
    117         A return value of true from isEqual() should not be used to test whether the effects would
    118         generate the same shader code. To test for identical code generation use the EffectKey
    119         computed by the GrBackendEffectFactory:
    120             effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB).
    121      */
    122     bool isEqual(const GrEffectRef& other) const {
    123         return this->isEqual(*other.get());
    124     }
    125 
    126     /** Human-meaningful string to identify this effect; may be embedded
    127         in generated shader code. */
    128     const char* name() const;
    129 
    130     int numTextures() const { return fTextureAccesses.count(); }
    131 
    132     /** Returns the access pattern for the texture at index. index must be valid according to
    133         numTextures(). */
    134     const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
    135 
    136     /** Shortcut for textureAccess(index).texture(); */
    137     GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
    138 
    139     /** Useful for effects that want to insert a texture matrix that is implied by the texture
    140         dimensions */
    141     static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
    142         GrAssert(NULL != texture);
    143         SkMatrix mat;
    144         mat.setIDiv(texture->width(), texture->height());
    145         return mat;
    146     }
    147 
    148     void* operator new(size_t size);
    149     void operator delete(void* target);
    150 
    151     /** These functions are used when recording effects into a deferred drawing queue. The inc call
    152         keeps the effect alive outside of GrEffectRef while allowing any resources owned by the
    153         effect to be returned to the cache for reuse. The dec call must balance the inc call. */
    154     void incDeferredRefCounts() const {
    155         this->ref();
    156         int count = fTextureAccesses.count();
    157         for (int t = 0; t < count; ++t) {
    158             fTextureAccesses[t]->getTexture()->incDeferredRefCount();
    159         }
    160     }
    161     void decDeferredRefCounts() const {
    162         int count = fTextureAccesses.count();
    163         for (int t = 0; t < count; ++t) {
    164             fTextureAccesses[t]->getTexture()->decDeferredRefCount();
    165         }
    166         this->unref();
    167     }
    168 
    169 protected:
    170     /**
    171      * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
    172      * manages the lifetime of the accesses (this function only stores a pointer). This must only be
    173      * called from the constructor because GrEffects are supposed to be immutable.
    174      */
    175     void addTextureAccess(const GrTextureAccess* textureAccess);
    176 
    177     GrEffect() : fEffectRef(NULL) {};
    178 
    179     /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for
    180         an example factory function. */
    181     static GrEffectRef* CreateEffectRef(GrEffect* effect) {
    182         if (NULL == effect->fEffectRef) {
    183             effect->fEffectRef = SkNEW_ARGS(GrEffectRef, (effect));
    184         } else {
    185             effect->fEffectRef->ref();
    186         }
    187         return effect->fEffectRef;
    188     }
    189 
    190     static const GrEffectRef* CreateEffectRef(const GrEffect* effect) {
    191         return CreateEffectRef(const_cast<GrEffect*>(effect));
    192     }
    193 
    194     /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a
    195         GrEffectRef. E.g.:
    196 
    197         class EffectSubclass : public GrEffect {
    198         public:
    199             GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
    200                 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...)));
    201                 return CreateEffectRef(effect);
    202             }
    203      */
    204     class AutoEffectUnref {
    205     public:
    206         AutoEffectUnref(GrEffect* effect) : fEffect(effect) { }
    207         ~AutoEffectUnref() { fEffect->unref(); }
    208         operator GrEffect*() { return fEffect; }
    209     private:
    210         GrEffect* fEffect;
    211     };
    212 
    213     /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass
    214       */
    215     template <typename T>
    216     static const T& CastEffect(const GrEffect& effectRef) {
    217         return *static_cast<const T*>(&effectRef);
    218     }
    219 
    220 private:
    221     bool isEqual(const GrEffect& other) const {
    222         if (&this->getFactory() != &other.getFactory()) {
    223             return false;
    224         }
    225         bool result = this->onIsEqual(other);
    226 #if GR_DEBUG
    227         if (result) {
    228             GrAssert(this->numTextures() == other.numTextures());
    229             for (int i = 0; i < this->numTextures(); ++i) {
    230                 GrAssert(*fTextureAccesses[i] == *other.fTextureAccesses[i]);
    231             }
    232         }
    233 #endif
    234         return result;
    235     }
    236 
    237     /** Subclass implements this to support isEqual(). It will only be called if it is known that
    238         the two effects are of the same subclass (i.e. they return the same object from
    239         getFactory()).*/
    240     virtual bool onIsEqual(const GrEffect& other) const = 0;
    241 
    242     void EffectRefDestroyed() { fEffectRef = NULL; }
    243 
    244     friend class GrEffectRef;   // to call EffectRefDestroyed()
    245     friend class GrEffectStage; // to rewrap GrEffect in GrEffectRef when restoring an effect-stage
    246                                 // from deferred state, to call isEqual on naked GrEffects, and
    247                                 // to inc/dec deferred ref counts.
    248 
    249     SkSTArray<4, const GrTextureAccess*, true>  fTextureAccesses;
    250     GrEffectRef*                                fEffectRef;
    251 
    252     typedef GrRefCnt INHERITED;
    253 };
    254 
    255 inline GrEffectRef::GrEffectRef(GrEffect* effect) {
    256     GrAssert(NULL != effect);
    257     effect->ref();
    258     fEffect = effect;
    259 }
    260 
    261 #endif
    262