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