Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef GrResource_DEFINED
     11 #define GrResource_DEFINED
     12 
     13 #include "GrRefCnt.h"
     14 
     15 #include "SkTInternalLList.h"
     16 
     17 class GrGpu;
     18 class GrContext;
     19 class GrResourceEntry;
     20 
     21 /**
     22  * Base class for the GPU resources created by a GrContext.
     23  */
     24 class GrResource : public GrRefCnt {
     25 public:
     26     SK_DECLARE_INST_COUNT(GrResource)
     27 
     28     /**
     29      * Frees the resource in the underlying 3D API. It must be safe to call this
     30      * when the resource has been previously abandoned.
     31      */
     32     void release();
     33 
     34     /**
     35      * Removes references to objects in the underlying 3D API without freeing
     36      * them. Used when the API context has been torn down before the GrContext.
     37      */
     38     void abandon();
     39 
     40     /**
     41      * Tests whether a resource has been abandoned or released. All resources
     42      * will be in this state after their creating GrContext is destroyed or has
     43      * contextLost called. It's up to the client to test isValid() before
     44      * attempting to use a resource if it holds refs on resources across
     45      * ~GrContext, freeResources with the force flag, or contextLost.
     46      *
     47      * @return true if the resource has been released or abandoned,
     48      *         false otherwise.
     49      */
     50     bool isValid() const { return NULL != fGpu; }
     51 
     52     /**
     53      * Retrieves the size of the object in GPU memory. This is approximate since
     54      * we aren't aware of additional padding or copies made by the driver.
     55      *
     56      * @return the size of the buffer in bytes
     57      */
     58     virtual size_t sizeInBytes() const = 0;
     59 
     60     /**
     61      * Retrieves the context that owns the resource. Note that it is possible
     62      * for this to return NULL. When resources have been release()ed or
     63      * abandon()ed they no longer have an owning context. Destroying a
     64      * GrContext automatically releases all its resources.
     65      */
     66     const GrContext* getContext() const;
     67     GrContext* getContext();
     68 
     69     void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
     70     GrResourceEntry* getCacheEntry() { return fCacheEntry; }
     71 
     72     void incDeferredRefCount() const { GrAssert(fDeferredRefCount >= 0); ++fDeferredRefCount; }
     73     void decDeferredRefCount() const { GrAssert(fDeferredRefCount > 0); --fDeferredRefCount; }
     74 
     75 protected:
     76     /**
     77      * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it
     78      * is true then the client is responsible for the lifetime of the underlying backend resource.
     79      * Otherwise, our onRelease() should free the resource.
     80      */
     81     GrResource(GrGpu* gpu, bool isWrapped);
     82     virtual ~GrResource();
     83 
     84     GrGpu* getGpu() const { return fGpu; }
     85 
     86     // Derived classes should always call their parent class' onRelease
     87     // and onAbandon methods in their overrides.
     88     virtual void onRelease() {};
     89     virtual void onAbandon() {};
     90 
     91     bool isInCache() const { return NULL != fCacheEntry; }
     92     bool isWrapped() const { return kWrapped_Flag & fFlags; }
     93 
     94 private:
     95 #if GR_DEBUG
     96     friend class GrGpu; // for assert in GrGpu to access getGpu
     97 #endif
     98 
     99     // We're in an internal doubly linked list
    100     SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource);
    101 
    102     GrGpu*              fGpu;               // not reffed. The GrGpu can be deleted while there
    103                                             // are still live GrResources. It will call
    104                                             // release() on all such resources in its
    105                                             // destructor.
    106     GrResourceEntry*    fCacheEntry;        // NULL if not in cache
    107     mutable int         fDeferredRefCount;  // How many references in deferred drawing buffers.
    108 
    109     enum Flags {
    110         kWrapped_Flag = 0x1,
    111     };
    112     uint32_t         fFlags;
    113 
    114     typedef GrRefCnt INHERITED;
    115 };
    116 
    117 #endif
    118