GrResource.h revision 80bacfeb4bda06541e8695bd502229727bccfeab
1
2/*
3 * 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.
7 */
8
9
10#ifndef GrResource_DEFINED
11#define GrResource_DEFINED
12
13#include "GrRefCnt.h"
14
15#include "SkTDLinkedList.h"
16
17class GrGpu;
18class GrContext;
19class GrResourceEntry;
20
21/**
22 * Base class for the GPU resources created by a GrContext.
23 */
24class GrResource : public GrRefCnt {
25public:
26    SK_DECLARE_INST_COUNT(GrResource)
27
28    /**
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
52    /**
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     */
58    virtual size_t sizeInBytes() const = 0;
59
60     /**
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
63      * abandon()ed they no longer have an owning context. Destroying a
64      * GrContext automatically releases all its resources.
65      */
66    const GrContext* getContext() const;
67    GrContext* getContext();
68
69    void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
70    GrResourceEntry* getCacheEntry() { return fCacheEntry; }
71
72protected:
73    explicit GrResource(GrGpu* gpu);
74    virtual ~GrResource();
75
76    GrGpu* getGpu() const { return fGpu; }
77
78    // Derived classes should always call their parent class' onRelease
79    // and onAbandon methods in their overrides.
80    virtual void onRelease() {};
81    virtual void onAbandon() {};
82
83    bool isInCache() const { return NULL != fCacheEntry; }
84
85private:
86
87#if GR_DEBUG
88    friend class GrGpu; // for assert in GrGpu to access getGpu
89#endif
90
91    GrGpu*      fGpu;       // not reffed. The GrGpu can be deleted while there
92                            // are still live GrResources. It will call
93                            // release() on all such resources in its
94                            // destructor.
95
96    // we're a dlinklist
97    SK_DEFINE_DLINKEDLIST_INTERFACE(GrResource);
98
99    GrResourceEntry* fCacheEntry;  // NULL if not in cache
100
101    typedef GrRefCnt INHERITED;
102};
103
104#endif
105