| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 2 | /* | 
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 7 |  */ | 
 | 8 |  | 
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 10 | #ifndef GrResource_DEFINED | 
 | 11 | #define GrResource_DEFINED | 
 | 12 |  | 
 | 13 | #include "GrRefCnt.h" | 
 | 14 |  | 
| bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 15 | #include "SkTInternalLList.h" | 
| robertphillips@google.com | 9474ed0 | 2012-09-04 13:34:32 +0000 | [diff] [blame] | 16 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 17 | class GrGpu; | 
| bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 18 | class GrContext; | 
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 19 | class GrResourceEntry; | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 20 |  | 
| bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 21 | /** | 
 | 22 |  * Base class for the GPU resources created by a GrContext. | 
 | 23 |  */ | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 24 | class GrResource : public GrRefCnt { | 
 | 25 | public: | 
| robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 26 |     SK_DECLARE_INST_COUNT(GrResource) | 
| robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 27 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 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 |  | 
| bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 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 |      */ | 
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 58 |     virtual size_t sizeInBytes() const = 0; | 
| bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 59 |  | 
| bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 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 |      */ | 
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 66 |     const GrContext* getContext() const; | 
 | 67 |     GrContext* getContext(); | 
 | 68 |  | 
 | 69 |     void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; } | 
 | 70 |     GrResourceEntry* getCacheEntry() { return fCacheEntry; } | 
| bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 71 |  | 
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 72 |     void incDeferredRefCount() const { SkASSERT(fDeferredRefCount >= 0); ++fDeferredRefCount; } | 
 | 73 |     void decDeferredRefCount() const { SkASSERT(fDeferredRefCount > 0); --fDeferredRefCount; } | 
| bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 74 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 75 | protected: | 
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 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); | 
| bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 82 |     virtual ~GrResource(); | 
 | 83 |  | 
 | 84 |     GrGpu* getGpu() const { return fGpu; } | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 85 |  | 
| robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 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() {}; | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 90 |  | 
| bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 91 |     bool isInCache() const { return NULL != fCacheEntry; } | 
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 92 |     bool isWrapped() const { return kWrapped_Flag & fFlags; } | 
| bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 93 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 94 | private: | 
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame^] | 95 | #ifdef SK_DEBUG | 
| robertphillips@google.com | 9474ed0 | 2012-09-04 13:34:32 +0000 | [diff] [blame] | 96 |     friend class GrGpu; // for assert in GrGpu to access getGpu | 
 | 97 | #endif | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 98 |  | 
| bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 99 |     // We're in an internal doubly linked list | 
 | 100 |     SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 101 |  | 
| bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 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. | 
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 108 |  | 
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 109 |     enum Flags { | 
| bsalomon@google.com | 1f0f1a3 | 2013-01-23 21:32:32 +0000 | [diff] [blame] | 110 |         kWrapped_Flag = 0x1, | 
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 111 |     }; | 
 | 112 |     uint32_t         fFlags; | 
 | 113 |  | 
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 114 |     typedef GrRefCnt INHERITED; | 
 | 115 | }; | 
 | 116 |  | 
 | 117 | #endif |