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