blob: 5c3e24aac7f372288ed64c4c05c3d1b311d3a366 [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
robertphillips@google.com9474ed02012-09-04 13:34:32 +000015#include "SkTDLinkedList.h"
16
bsalomon@google.com8fe72472011-03-30 21:26:44 +000017class GrGpu;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000018class GrContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000019class GrResourceEntry;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000021/**
22 * Base class for the GPU resources created by a GrContext.
23 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000024class GrResource : public GrRefCnt {
25public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000026 SK_DECLARE_INST_COUNT(GrResource)
robertphillips@google.com977b9c82012-06-05 19:35:09 +000027
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028 /**
29 * Frees the resource in the underlying 3D API. It must be safe to call this
30 * when the resource has been previously abandoned.
31 */
32 void release();
33
34 /**
35 * Removes references to objects in the underlying 3D API without freeing
36 * them. Used when the API context has been torn down before the GrContext.
37 */
38 void abandon();
39
40 /**
41 * Tests whether a resource has been abandoned or released. All resources
42 * will be in this state after their creating GrContext is destroyed or has
43 * contextLost called. It's up to the client to test isValid() before
44 * attempting to use a resource if it holds refs on resources across
45 * ~GrContext, freeResources with the force flag, or contextLost.
46 *
47 * @return true if the resource has been released or abandoned,
48 * false otherwise.
49 */
50 bool isValid() const { return NULL != fGpu; }
51
bsalomon@google.comcee661a2011-07-26 12:32:36 +000052 /**
53 * Retrieves the size of the object in GPU memory. This is approximate since
54 * we aren't aware of additional padding or copies made by the driver.
55 *
56 * @return the size of the buffer in bytes
57 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000058 virtual size_t sizeInBytes() const = 0;
bsalomon@google.comcee661a2011-07-26 12:32:36 +000059
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000060 /**
61 * Retrieves the context that owns the resource. Note that it is possible
62 * for this to return NULL. When resources have been release()ed or
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000063 * abandon()ed they no longer have an owning context. Destroying a
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000064 * GrContext automatically releases all its resources.
65 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000066 const GrContext* getContext() const;
67 GrContext* getContext();
68
69 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
70 GrResourceEntry* getCacheEntry() { return fCacheEntry; }
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000071
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072protected:
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000073 explicit GrResource(GrGpu* gpu);
74 virtual ~GrResource();
75
76 GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000077
78 virtual void onRelease() = 0;
79 virtual void onAbandon() = 0;
80
bsalomon@google.coma2921122012-08-28 12:34:17 +000081 bool isInCache() const { return NULL != fCacheEntry; }
82
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083private:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084
robertphillips@google.com9474ed02012-09-04 13:34:32 +000085#if GR_DEBUG
86 friend class GrGpu; // for assert in GrGpu to access getGpu
87#endif
bsalomon@google.com8fe72472011-03-30 21:26:44 +000088
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000089 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
90 // are still live GrResources. It will call
91 // release() on all such resources in its
92 // destructor.
robertphillips@google.com9474ed02012-09-04 13:34:32 +000093
94 // we're a dlinklist
95 SK_DEFINE_DLINKEDLIST_INTERFACE(GrResource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000097 GrResourceEntry* fCacheEntry; // NULL if not in cache
98
bsalomon@google.com8fe72472011-03-30 21:26:44 +000099 typedef GrRefCnt INHERITED;
100};
101
102#endif