blob: 5e852a9a040a719a3c4a736d84b833632bbbcf6e [file] [log] [blame]
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00001/*
bsalomonc44be0e2014-07-25 07:32:33 -07002 * Copyright 2014 Google Inc.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon6d3fe022014-07-25 08:35:45 -07008#ifndef GrGpuResource_DEFINED
9#define GrGpuResource_DEFINED
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000010
bsalomonc44be0e2014-07-25 07:32:33 -070011#include "SkInstCnt.h"
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000012#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070013#include "GrResourceKey.h"
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000014
bsalomonc44be0e2014-07-25 07:32:33 -070015class GrResourceCacheEntry;
bsalomonc8dc1f72014-08-21 13:02:13 -070016class GrResourceCache2;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000017class GrGpu;
18class GrContext;
19
20/**
bsalomonc44be0e2014-07-25 07:32:33 -070021 * Base class for objects that can be kept in the GrResourceCache.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000022 */
bsalomon6d3fe022014-07-25 08:35:45 -070023class GrGpuResource : public SkNoncopyable {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000024public:
bsalomon6d3fe022014-07-25 08:35:45 -070025 SK_DECLARE_INST_COUNT_ROOT(GrGpuResource)
bsalomonc44be0e2014-07-25 07:32:33 -070026
27 // These method signatures are written to mirror SkRefCnt. However, we don't require
28 // thread safety as GrCacheable objects are not intended to cross thread boundaries.
29 // internal_dispose() exists because of GrTexture's reliance on it. It will be removed
30 // soon.
31 void ref() const { ++fRefCnt; }
32 void unref() const { --fRefCnt; if (0 == fRefCnt) { this->internal_dispose(); } }
33 virtual void internal_dispose() const { SkDELETE(this); }
34 bool unique() const { return 1 == fRefCnt; }
35#ifdef SK_DEBUG
36 void validate() const {
37 SkASSERT(fRefCnt > 0);
38 }
39#endif
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000040
41 /**
42 * Frees the object in the underlying 3D API. It must be safe to call this
43 * when the object has been previously abandoned.
44 */
45 void release();
46
47 /**
48 * Removes references to objects in the underlying 3D API without freeing
49 * them. Used when the API context has been torn down before the GrContext.
50 */
51 void abandon();
52
53 /**
54 * Tests whether a object has been abandoned or released. All objects will
55 * be in this state after their creating GrContext is destroyed or has
56 * contextLost called. It's up to the client to test wasDestroyed() before
57 * attempting to use an object if it holds refs on objects across
58 * ~GrContext, freeResources with the force flag, or contextLost.
59 *
60 * @return true if the object has been released or abandoned,
61 * false otherwise.
62 */
63 bool wasDestroyed() const { return NULL == fGpu; }
64
65 /**
66 * Retrieves the context that owns the object. Note that it is possible for
67 * this to return NULL. When objects have been release()ed or abandon()ed
68 * they no longer have an owning context. Destroying a GrContext
69 * automatically releases all its resources.
70 */
71 const GrContext* getContext() const;
72 GrContext* getContext();
73
bsalomonc44be0e2014-07-25 07:32:33 -070074 /**
75 * Retrieves the amount of GPU memory used by this resource in bytes. It is
76 * approximate since we aren't aware of additional padding or copies made
77 * by the driver.
78 *
79 * @return the amount of GPU memory used in bytes
80 */
81 virtual size_t gpuMemorySize() const = 0;
82
83 void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
84 GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; }
85
bsalomon744998e2014-08-28 09:54:34 -070086 /**
87 * If this resource can be used as a scratch resource this returns a valid
88 * scratch key. Otherwise it returns a key for which isNullScratch is true.
89 */
90 const GrResourceKey& getScratchKey() const { return fScratchKey; }
91
bsalomonc44be0e2014-07-25 07:32:33 -070092 /**
93 * Gets an id that is unique for this GrCacheable object. It is static in that it does
94 * not change when the content of the GrCacheable object changes. This will never return
95 * 0.
96 */
97 uint32_t getUniqueID() const { return fUniqueID; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000098
99protected:
bsalomon16961262014-08-26 14:01:07 -0700100 // This must be called by every GrGpuObject. It should be called once the object is fully
101 // initialized (i.e. not in a base class constructor).
102 void registerWithCache();
103
bsalomon6d3fe022014-07-25 08:35:45 -0700104 GrGpuResource(GrGpu*, bool isWrapped);
105 virtual ~GrGpuResource();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000106
bsalomonc44be0e2014-07-25 07:32:33 -0700107 bool isInCache() const { return NULL != fCacheEntry; }
108
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000109 GrGpu* getGpu() const { return fGpu; }
110
111 // Derived classes should always call their parent class' onRelease
112 // and onAbandon methods in their overrides.
113 virtual void onRelease() {};
114 virtual void onAbandon() {};
115
116 bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000117
bsalomonc44be0e2014-07-25 07:32:33 -0700118 /**
119 * This entry point should be called whenever gpuMemorySize() begins
120 * reporting a different size. If the object is in the cache, it will call
121 * gpuMemorySize() immediately and pass the new size on to the resource
122 * cache.
123 */
124 void didChangeGpuMemorySize() const;
125
bsalomon744998e2014-08-28 09:54:34 -0700126 /**
127 * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
128 * By default resources are not usable as scratch. This should only be called once.
129 **/
130 void setScratchKey(const GrResourceKey& scratchKey);
131
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000132private:
133#ifdef SK_DEBUG
134 friend class GrGpu; // for assert in GrGpu to access getGpu
135#endif
136
bsalomonc44be0e2014-07-25 07:32:33 -0700137 static uint32_t CreateUniqueID();
138
bsalomonc8dc1f72014-08-21 13:02:13 -0700139 // We're in an internal doubly linked list owned by GrResourceCache2
bsalomon6d3fe022014-07-25 08:35:45 -0700140 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000141
bsalomonc8dc1f72014-08-21 13:02:13 -0700142 // This is not ref'ed but abandon() or release() will be called before the GrGpu object
143 // is destroyed. Those calls set will this to NULL.
144 GrGpu* fGpu;
145
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000146 enum Flags {
147 /**
148 * This object wraps a GPU object given to us by the user.
149 * Lifetime management is left up to the user (i.e., we will not
150 * free it).
151 */
152 kWrapped_FlagBit = 0x1,
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000153 };
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000154
bsalomonc44be0e2014-07-25 07:32:33 -0700155 uint32_t fFlags;
156
157 mutable int32_t fRefCnt;
158 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache
159 const uint32_t fUniqueID;
160
bsalomon744998e2014-08-28 09:54:34 -0700161 GrResourceKey fScratchKey;
162
bsalomonc44be0e2014-07-25 07:32:33 -0700163 typedef SkNoncopyable INHERITED;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000164};
165
166#endif