blob: e0033532d2bb48f81acdca8812518b013b2584c2 [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;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000016class GrContext;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000017
18class GrResource : public GrRefCnt {
19public:
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.comcee661a2011-07-26 12:32:36 +000051 /**
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.comf7b5c1e2011-11-15 19:42:07 +000059 /**
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.com8fe72472011-03-30 21:26:44 +000068protected:
69
70 virtual void onRelease() = 0;
71 virtual void onAbandon() = 0;
72
73 GrGpu* getGpu() const { return fGpu; }
74
75private:
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