blob: 0d15fe9c003aef1c9ae4bfdc53ff388f445afa47 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com8fe72472011-03-30 21:26:44 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com8fe72472011-03-30 21:26:44 +000010#ifndef GrResource_DEFINED
11#define GrResource_DEFINED
12
13#include "GrRefCnt.h"
14
15class GrGpu;
16
17class GrResource : public GrRefCnt {
18public:
19 explicit GrResource(GrGpu* gpu);
20
21 virtual ~GrResource() {
22 // subclass should have released this.
23 GrAssert(!isValid());
24 }
25
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.comcee661a2011-07-26 12:32:36 +000050 /**
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 */
56 virtual size_t sizeInBytes() const = 0;
57
bsalomon@google.com8fe72472011-03-30 21:26:44 +000058protected:
59
60 virtual void onRelease() = 0;
61 virtual void onAbandon() = 0;
62
63 GrGpu* getGpu() const { return fGpu; }
64
65private:
66 GrResource(); // unimpl
67
68 GrGpu* fGpu; // not reffed. This can outlive the GrGpu.
69
70 friend class GrGpu; // GrGpu manages list of resources.
71
72 GrResource* fNext; // dl-list of resources per-GrGpu
73 GrResource* fPrevious;
74
75 typedef GrRefCnt INHERITED;
76};
77
78#endif