blob: eee1a81aeeb564289fa5bf35d46506d533172462 [file] [log] [blame]
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001/*
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
22class GrGpu;
23
24class GrResource : public GrRefCnt {
25public:
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
bsalomon@google.comcee661a2011-07-26 12:32:36 +000057 /**
58 * Retrieves the size of the object in GPU memory. This is approximate since
59 * we aren't aware of additional padding or copies made by the driver.
60 *
61 * @return the size of the buffer in bytes
62 */
63 virtual size_t sizeInBytes() const = 0;
64
bsalomon@google.com8fe72472011-03-30 21:26:44 +000065protected:
66
67 virtual void onRelease() = 0;
68 virtual void onAbandon() = 0;
69
70 GrGpu* getGpu() const { return fGpu; }
71
72private:
73 GrResource(); // unimpl
74
75 GrGpu* fGpu; // not reffed. This can outlive the GrGpu.
76
77 friend class GrGpu; // GrGpu manages list of resources.
78
79 GrResource* fNext; // dl-list of resources per-GrGpu
80 GrResource* fPrevious;
81
82 typedef GrRefCnt INHERITED;
83};
84
85#endif