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 | |
| 15 | class GrGpu; |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 16 | class GrContext; |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 17 | class GrResourceEntry; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Base class for the GPU resources created by a GrContext. |
| 21 | */ |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 22 | class GrResource : public GrRefCnt { |
| 23 | public: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 24 | SK_DECLARE_INST_COUNT(GrResource) |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Frees the resource in the underlying 3D API. It must be safe to call this |
| 28 | * when the resource has been previously abandoned. |
| 29 | */ |
| 30 | void release(); |
| 31 | |
| 32 | /** |
| 33 | * Removes references to objects in the underlying 3D API without freeing |
| 34 | * them. Used when the API context has been torn down before the GrContext. |
| 35 | */ |
| 36 | void abandon(); |
| 37 | |
| 38 | /** |
| 39 | * Tests whether a resource has been abandoned or released. All resources |
| 40 | * will be in this state after their creating GrContext is destroyed or has |
| 41 | * contextLost called. It's up to the client to test isValid() before |
| 42 | * attempting to use a resource if it holds refs on resources across |
| 43 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 44 | * |
| 45 | * @return true if the resource has been released or abandoned, |
| 46 | * false otherwise. |
| 47 | */ |
| 48 | bool isValid() const { return NULL != fGpu; } |
| 49 | |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Retrieves the size of the object in GPU memory. This is approximate since |
| 52 | * we aren't aware of additional padding or copies made by the driver. |
| 53 | * |
| 54 | * @return the size of the buffer in bytes |
| 55 | */ |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 56 | virtual size_t sizeInBytes() const = 0; |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Retrieves the context that owns the resource. Note that it is possible |
| 60 | * for this to return NULL. When resources have been release()ed or |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 61 | * abandon()ed they no longer have an owning context. Destroying a |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 62 | * GrContext automatically releases all its resources. |
| 63 | */ |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 64 | const GrContext* getContext() const; |
| 65 | GrContext* getContext(); |
| 66 | |
| 67 | void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; } |
| 68 | GrResourceEntry* getCacheEntry() { return fCacheEntry; } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 70 | protected: |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 71 | explicit GrResource(GrGpu* gpu); |
| 72 | virtual ~GrResource(); |
| 73 | |
| 74 | GrGpu* getGpu() const { return fGpu; } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 75 | |
| 76 | virtual void onRelease() = 0; |
| 77 | virtual void onAbandon() = 0; |
| 78 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 79 | private: |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 80 | |
| 81 | friend class GrGpu; // GrGpu manages list of resources. |
| 82 | |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 83 | GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there |
| 84 | // are still live GrResources. It will call |
| 85 | // release() on all such resources in its |
| 86 | // destructor. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 87 | GrResource* fNext; // dl-list of resources per-GrGpu |
| 88 | GrResource* fPrevious; |
| 89 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 90 | GrResourceEntry* fCacheEntry; // NULL if not in cache |
| 91 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 92 | typedef GrRefCnt INHERITED; |
| 93 | }; |
| 94 | |
| 95 | #endif |