blob: 6b85729b5456a13ac6e4eed5e3d685dc79cc986f [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;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000017class GrResourceEntry;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000018
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000019/**
20 * Base class for the GPU resources created by a GrContext.
21 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000022class GrResource : public GrRefCnt {
23public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000024 SK_DECLARE_INST_COUNT(GrResource)
robertphillips@google.com977b9c82012-06-05 19:35:09 +000025
bsalomon@google.com8fe72472011-03-30 21:26:44 +000026 /**
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 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000056 virtual size_t sizeInBytes() const = 0;
bsalomon@google.comcee661a2011-07-26 12:32:36 +000057
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000058 /**
59 * Retrieves the context that owns the resource. Note that it is possible
60 * for this to return NULL. When resources have been release()ed or
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000061 * abandon()ed they no longer have an owning context. Destroying a
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000062 * GrContext automatically releases all its resources.
63 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000064 const GrContext* getContext() const;
65 GrContext* getContext();
66
67 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
68 GrResourceEntry* getCacheEntry() { return fCacheEntry; }
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000069
bsalomon@google.com8fe72472011-03-30 21:26:44 +000070protected:
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000071 explicit GrResource(GrGpu* gpu);
72 virtual ~GrResource();
73
74 GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075
76 virtual void onRelease() = 0;
77 virtual void onAbandon() = 0;
78
bsalomon@google.coma2921122012-08-28 12:34:17 +000079 bool isInCache() const { return NULL != fCacheEntry; }
80
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081private:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082
83 friend class GrGpu; // GrGpu manages list of resources.
84
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000085 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
86 // are still live GrResources. It will call
87 // release() on all such resources in its
88 // destructor.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 GrResource* fNext; // dl-list of resources per-GrGpu
90 GrResource* fPrevious;
91
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000092 GrResourceEntry* fCacheEntry; // NULL if not in cache
93
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094 typedef GrRefCnt INHERITED;
95};
96
97#endif