blob: 8008f29e348ba3f13f2352052caf8dfabf67a7dd [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:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023 /**
24 * Frees the resource in the underlying 3D API. It must be safe to call this
25 * when the resource has been previously abandoned.
26 */
27 void release();
28
29 /**
30 * Removes references to objects in the underlying 3D API without freeing
31 * them. Used when the API context has been torn down before the GrContext.
32 */
33 void abandon();
34
35 /**
36 * Tests whether a resource has been abandoned or released. All resources
37 * will be in this state after their creating GrContext is destroyed or has
38 * contextLost called. It's up to the client to test isValid() before
39 * attempting to use a resource if it holds refs on resources across
40 * ~GrContext, freeResources with the force flag, or contextLost.
41 *
42 * @return true if the resource has been released or abandoned,
43 * false otherwise.
44 */
45 bool isValid() const { return NULL != fGpu; }
46
bsalomon@google.comcee661a2011-07-26 12:32:36 +000047 /**
48 * Retrieves the size of the object in GPU memory. This is approximate since
49 * we aren't aware of additional padding or copies made by the driver.
50 *
51 * @return the size of the buffer in bytes
52 */
53 virtual size_t sizeInBytes() const = 0;
54
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000055 /**
56 * Retrieves the context that owns the resource. Note that it is possible
57 * for this to return NULL. When resources have been release()ed or
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000058 * abandon()ed they no longer have an owning context. Destroying a
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000059 * GrContext automatically releases all its resources.
60 */
61 const GrContext* getContext() const;
62 GrContext* getContext();
63
bsalomon@google.com8fe72472011-03-30 21:26:44 +000064protected:
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000065 explicit GrResource(GrGpu* gpu);
66 virtual ~GrResource();
67
68 GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000069
70 virtual void onRelease() = 0;
71 virtual void onAbandon() = 0;
72
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073private:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000074
75 friend class GrGpu; // GrGpu manages list of resources.
76
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000077 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
78 // are still live GrResources. It will call
79 // release() on all such resources in its
80 // destructor.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 GrResource* fNext; // dl-list of resources per-GrGpu
82 GrResource* fPrevious;
83
84 typedef GrRefCnt INHERITED;
85};
86
87#endif