blob: 94d2173ec2d2fd7af330b641c529165d44e991c7 [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
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000018/**
19 * Base class for the GPU resources created by a GrContext.
20 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021class GrResource : public GrRefCnt {
22public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000023 SK_DECLARE_INST_COUNT(GrResource)
robertphillips@google.com977b9c82012-06-05 19:35:09 +000024
bsalomon@google.com8fe72472011-03-30 21:26:44 +000025 /**
26 * Frees the resource in the underlying 3D API. It must be safe to call this
27 * when the resource has been previously abandoned.
28 */
29 void release();
30
31 /**
32 * Removes references to objects in the underlying 3D API without freeing
33 * them. Used when the API context has been torn down before the GrContext.
34 */
35 void abandon();
36
37 /**
38 * Tests whether a resource has been abandoned or released. All resources
39 * will be in this state after their creating GrContext is destroyed or has
40 * contextLost called. It's up to the client to test isValid() before
41 * attempting to use a resource if it holds refs on resources across
42 * ~GrContext, freeResources with the force flag, or contextLost.
43 *
44 * @return true if the resource has been released or abandoned,
45 * false otherwise.
46 */
47 bool isValid() const { return NULL != fGpu; }
48
bsalomon@google.comcee661a2011-07-26 12:32:36 +000049 /**
50 * Retrieves the size of the object in GPU memory. This is approximate since
51 * we aren't aware of additional padding or copies made by the driver.
52 *
53 * @return the size of the buffer in bytes
54 */
55 virtual size_t sizeInBytes() const = 0;
56
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000057 /**
58 * Retrieves the context that owns the resource. Note that it is possible
59 * for this to return NULL. When resources have been release()ed or
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000060 * abandon()ed they no longer have an owning context. Destroying a
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000061 * GrContext automatically releases all its resources.
62 */
63 const GrContext* getContext() const;
64 GrContext* getContext();
65
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066protected:
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000067 explicit GrResource(GrGpu* gpu);
68 virtual ~GrResource();
69
70 GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000071
72 virtual void onRelease() = 0;
73 virtual void onAbandon() = 0;
74
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075private:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076
77 friend class GrGpu; // GrGpu manages list of resources.
78
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000079 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
80 // are still live GrResources. It will call
81 // release() on all such resources in its
82 // destructor.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 GrResource* fNext; // dl-list of resources per-GrGpu
84 GrResource* fPrevious;
85
86 typedef GrRefCnt INHERITED;
87};
88
89#endif