bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef GrResource_DEFINED |
| 18 | #define GrResource_DEFINED |
| 19 | |
| 20 | #include "GrRefCnt.h" |
| 21 | |
| 22 | class GrGpu; |
| 23 | |
| 24 | class GrResource : public GrRefCnt { |
| 25 | public: |
| 26 | explicit GrResource(GrGpu* gpu); |
| 27 | |
| 28 | virtual ~GrResource() { |
| 29 | // subclass should have released this. |
| 30 | GrAssert(!isValid()); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Frees the resource in the underlying 3D API. It must be safe to call this |
| 35 | * when the resource has been previously abandoned. |
| 36 | */ |
| 37 | void release(); |
| 38 | |
| 39 | /** |
| 40 | * Removes references to objects in the underlying 3D API without freeing |
| 41 | * them. Used when the API context has been torn down before the GrContext. |
| 42 | */ |
| 43 | void abandon(); |
| 44 | |
| 45 | /** |
| 46 | * Tests whether a resource has been abandoned or released. All resources |
| 47 | * will be in this state after their creating GrContext is destroyed or has |
| 48 | * contextLost called. It's up to the client to test isValid() before |
| 49 | * attempting to use a resource if it holds refs on resources across |
| 50 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 51 | * |
| 52 | * @return true if the resource has been released or abandoned, |
| 53 | * false otherwise. |
| 54 | */ |
| 55 | bool isValid() const { return NULL != fGpu; } |
| 56 | |
| 57 | protected: |
| 58 | |
| 59 | virtual void onRelease() = 0; |
| 60 | virtual void onAbandon() = 0; |
| 61 | |
| 62 | GrGpu* getGpu() const { return fGpu; } |
| 63 | |
| 64 | private: |
| 65 | GrResource(); // unimpl |
| 66 | |
| 67 | GrGpu* fGpu; // not reffed. This can outlive the GrGpu. |
| 68 | |
| 69 | friend class GrGpu; // GrGpu manages list of resources. |
| 70 | |
| 71 | GrResource* fNext; // dl-list of resources per-GrGpu |
| 72 | GrResource* fPrevious; |
| 73 | |
| 74 | typedef GrRefCnt INHERITED; |
| 75 | }; |
| 76 | |
| 77 | #endif |