Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(881)

Side by Side Diff: src/gpu/GrResourceCache2.h

Issue 716143004: Replace GrResourceCache with GrResourceCache2. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix asserts Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/GrResourceCache.cpp ('k') | src/gpu/GrResourceCache2.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2014 Google Inc. 3 * Copyright 2014 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #ifndef GrResourceCache2_DEFINED 9 #ifndef GrResourceCache2_DEFINED
10 #define GrResourceCache2_DEFINED 10 #define GrResourceCache2_DEFINED
11 11
12 #include "GrGpuResource.h" 12 #include "GrGpuResource.h"
13 #include "GrGpuResourceCacheAccess.h" 13 #include "GrGpuResourceCacheAccess.h"
14 #include "GrResourceKey.h" 14 #include "GrResourceKey.h"
15 #include "SkRefCnt.h" 15 #include "SkRefCnt.h"
16 #include "SkTInternalLList.h" 16 #include "SkTInternalLList.h"
17 #include "SkTMultiMap.h" 17 #include "SkTMultiMap.h"
18 18
19 /** 19 /**
20 * Eventual replacement for GrResourceCache. Currently it simply holds a list 20 * Manages the lifetime of all GrGpuResource instances.
21 * of all GrGpuResource objects for a GrContext. It is used to invalidate all 21 *
22 * the resources when necessary. 22 * Resources may have optionally have two types of keys:
23 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
24 * Multiple resources can share the same scratch key. This is so a calle r can have two
25 * resource instances with the same properties (e.g. multipass rendering that ping pongs
26 * between two temporary surfaces. The scratch key is set at resource cr eation time and
27 * should never change. Resources need not have a scratch key.
28 * 2) A content key. This key represents the contents of the resource rathe r than just its
29 * allocation properties. They may not collide. The content key can be s et after resource
30 * creation. Currently it may only be set once and cannot be cleared. Th is restriction will
31 * be removed.
32 * If a resource has neither key type then it will be deleted as soon as the las t reference to it
33 * is dropped. If a key has both keys the content key takes precedence.
23 */ 34 */
24 class GrResourceCache2 { 35 class GrResourceCache2 {
25 public: 36 public:
26 GrResourceCache2() : fCount(0) {}; 37 GrResourceCache2();
27 ~GrResourceCache2(); 38 ~GrResourceCache2();
28 39
29 void insertResource(GrGpuResource*); 40 /** Used to access functionality needed by GrGpuResource for lifetime manage ment. */
41 class ResourceAccess;
42 ResourceAccess resourceAccess();
30 43
31 void removeResource(GrGpuResource*); 44 /**
45 * Sets the cache limits in terms of number of resources and max gpu memory byte size.
46 */
47 void setLimits(int count, size_t bytes);
32 48
33 // This currently returns a bool and fails when an existing resource has a k ey that collides 49 /**
34 // with the new content key. In the future it will null out the content key for the existing 50 * Returns the number of cached resources.
35 // resource. The failure is a temporary measure taken because duties are spl it between two 51 */
36 // cache objects currently. 52 int getResourceCount() const { return fCount; }
37 bool didSetContentKey(GrGpuResource*);
38 53
54 /**
55 * Returns the number of bytes consumed by cached resources.
56 */
57 size_t getResourceBytes() const { return fBytes; }
58
59 /**
60 * Returns the cached resources count budget.
61 */
62 int getMaxResourceCount() const { return fMaxCount; }
63
64 /**
65 * Returns the number of bytes consumed by cached resources.
66 */
67 size_t getMaxResourceBytes() const { return fMaxBytes; }
68
69 /**
70 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
71 * the cache.
72 */
39 void abandonAll(); 73 void abandonAll();
40 74
75 /**
76 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
77 * the cache.
78 */
41 void releaseAll(); 79 void releaseAll();
42 80
43 enum { 81 enum {
44 /** Preferentially returns scratch resources with no pending IO. */ 82 /** Preferentially returns scratch resources with no pending IO. */
45 kPreferNoPendingIO_ScratchFlag = 0x1, 83 kPreferNoPendingIO_ScratchFlag = 0x1,
46 /** Will not return any resources that match but have pending IO. */ 84 /** Will not return any resources that match but have pending IO. */
47 kRequireNoPendingIO_ScratchFlag = 0x2, 85 kRequireNoPendingIO_ScratchFlag = 0x2,
48 }; 86 };
87
88 /**
89 * Find a resource that matches a scratch key.
90 */
49 GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, ui nt32_t flags = 0); 91 GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, ui nt32_t flags = 0);
50 92
51 #ifdef SK_DEBUG 93 #ifdef SK_DEBUG
52 // This is not particularly fast and only used for validation, so debug only . 94 // This is not particularly fast and only used for validation, so debug only .
53 int countScratchEntriesForKey(const GrResourceKey& scratchKey) const { 95 int countScratchEntriesForKey(const GrResourceKey& scratchKey) const {
54 SkASSERT(scratchKey.isScratch()); 96 SkASSERT(scratchKey.isScratch());
55 return fScratchMap.countForKey(scratchKey); 97 return fScratchMap.countForKey(scratchKey);
56 } 98 }
57 #endif 99 #endif
58 100
101 /**
102 * Find a resource that matches a content key.
103 */
59 GrGpuResource* findAndRefContentResource(const GrResourceKey& contentKey) { 104 GrGpuResource* findAndRefContentResource(const GrResourceKey& contentKey) {
60 SkASSERT(!contentKey.isScratch()); 105 SkASSERT(!contentKey.isScratch());
61 return SkSafeRef(fContentHash.find(contentKey)); 106 GrGpuResource* resource = fContentHash.find(contentKey);
107 if (resource) {
108 resource->ref();
109 this->makeResourceMRU(resource);
110 }
111 return resource;
62 } 112 }
63 113
114 /**
115 * Query whether a content key exists in the cache.
116 */
64 bool hasContentKey(const GrResourceKey& contentKey) const { 117 bool hasContentKey(const GrResourceKey& contentKey) const {
65 SkASSERT(!contentKey.isScratch()); 118 SkASSERT(!contentKey.isScratch());
66 return SkToBool(fContentHash.find(contentKey)); 119 return SkToBool(fContentHash.find(contentKey));
67 } 120 }
68 121
122 /** Purges all resources that don't have external owners. */
123 void purgeAllUnlocked();
124
125 /**
126 * The callback function used by the cache when it is still over budget afte r a purge. The
127 * passed in 'data' is the same 'data' handed to setOverbudgetCallback.
128 */
129 typedef void (*PFOverBudgetCB)(void* data);
130
131 /**
132 * Set the callback the cache should use when it is still over budget after a purge. The 'data'
133 * provided here will be passed back to the callback. Note that the cache wi ll attempt to purge
134 * any resources newly freed by the callback.
135 */
136 void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) {
137 fOverBudgetCB = overBudgetCB;
138 fOverBudgetData = data;
139 }
140
141 #if GR_GPU_STATS
142 void printStats() const;
143 #endif
144
69 private: 145 private:
146 ///////////////////////////////////////////////////////////////////////////
147 /// @name Methods accessible via ResourceAccess
148 ////
149 void insertResource(GrGpuResource*);
150 void removeResource(GrGpuResource*);
151 void notifyPurgable(const GrGpuResource*);
152 void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize);
153 bool didSetContentKey(GrGpuResource*);
154 void makeResourceMRU(GrGpuResource*);
155 /// @}
156
157 void purgeAsNeeded() {
158 if (fPurging || (fCount <= fMaxCount && fBytes < fMaxBytes)) {
159 return;
160 }
161 this->internalPurgeAsNeeded();
162 }
163
164 void internalPurgeAsNeeded();
165
70 #ifdef SK_DEBUG 166 #ifdef SK_DEBUG
71 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r) ; } 167 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r) ; }
168 void validate() const;
169 #else
170 void validate() const {}
72 #endif 171 #endif
73 172
173 class AutoValidate;
174
74 class AvailableForScratchUse; 175 class AvailableForScratchUse;
75 176
76 struct ScratchMapTraits { 177 struct ScratchMapTraits {
77 static const GrResourceKey& GetKey(const GrGpuResource& r) { 178 static const GrResourceKey& GetKey(const GrGpuResource& r) {
78 return r.cacheAccess().getScratchKey(); 179 return r.cacheAccess().getScratchKey();
79 } 180 }
80 181
81 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } 182 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
82 }; 183 };
83 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchM ap; 184 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchM ap;
84 185
85 struct ContentHashTraits { 186 struct ContentHashTraits {
86 static const GrResourceKey& GetKey(const GrGpuResource& r) { 187 static const GrResourceKey& GetKey(const GrGpuResource& r) {
87 return *r.cacheAccess().getContentKey(); 188 return *r.cacheAccess().getContentKey();
88 } 189 }
89 190
90 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } 191 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
91 }; 192 };
92 typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> Cont entHash; 193 typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> Cont entHash;
93 194
94 int fCount; 195 typedef SkTInternalLList<GrGpuResource> ResourceList;
95 SkTInternalLList<GrGpuResource> fResources; 196
197 ResourceList fResources;
96 // This map holds all resources that can be used as scratch resources. 198 // This map holds all resources that can be used as scratch resources.
97 ScratchMap fScratchMap; 199 ScratchMap fScratchMap;
98 // This holds all resources that have content keys. 200 // This holds all resources that have content keys.
99 ContentHash fContentHash; 201 ContentHash fContentHash;
202
203 // our budget, used in purgeAsNeeded()
204 int fMaxCount;
205 size_t fMaxBytes;
206
207 #if GR_CACHE_STATS
208 int fHighWaterCount;
209 size_t fHighWaterBytes;
210 #endif
211
212 // our current stats, related to our budget
213 int fCount;
214 size_t fBytes;
215
216 // prevents recursive purging
217 bool fPurging;
218 bool fNewlyPurgableResourceWhilePurging;
219
220 PFOverBudgetCB fOverBudgetCB;
221 void* fOverBudgetData;
222
100 }; 223 };
101 224
225 class GrResourceCache2::ResourceAccess {
226 private:
227 ResourceAccess(GrResourceCache2* cache) : fCache(cache) { }
228 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
229 ResourceAccess& operator=(const ResourceAccess&); // unimpl
230
231 /**
232 * Insert a resource into the cache.
233 */
234 void insertResource(GrGpuResource* resource) { fCache->insertResource(resour ce); }
235
236 /**
237 * Removes a resource from the cache.
238 */
239 void removeResource(GrGpuResource* resource) { fCache->removeResource(resour ce); }
240
241 /**
242 * Called by GrGpuResources when they detects that they are newly purgable.
243 */
244 void notifyPurgable(const GrGpuResource* resource) { fCache->notifyPurgable( resource); }
245
246 /**
247 * Called by GrGpuResources when their sizes change.
248 */
249 void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
250 fCache->didChangeGpuMemorySize(resource, oldSize);
251 }
252
253 /**
254 * Called by GrGpuResources when their content keys change.
255 *
256 * This currently returns a bool and fails when an existing resource has a k ey that collides
257 * with the new content key. In the future it will null out the content key for the existing
258 * resource. The failure is a temporary measure taken because duties are spl it between two
259 * cache objects currently.
260 */
261 bool didSetContentKey(GrGpuResource* resource) { return fCache->didSetConten tKey(resource); }
262
263 // No taking addresses of this type.
264 const ResourceAccess* operator&() const;
265 ResourceAccess* operator&();
266
267 GrResourceCache2* fCache;
268
269 friend class GrGpuResource; // To access all the proxy inline methods.
270 friend class GrResourceCache2; // To create this type.
271 };
272
273 inline GrResourceCache2::ResourceAccess GrResourceCache2::resourceAccess() {
274 return ResourceAccess(this);
275 }
276
102 #endif 277 #endif
OLDNEW
« no previous file with comments | « src/gpu/GrResourceCache.cpp ('k') | src/gpu/GrResourceCache2.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698