OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 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 | 9 |
10 | 10 |
11 #ifndef GrResourceCache_DEFINED | 11 #ifndef GrResourceCache_DEFINED |
12 #define GrResourceCache_DEFINED | 12 #define GrResourceCache_DEFINED |
13 | 13 |
14 #include "GrConfig.h" | 14 #include "GrConfig.h" |
15 #include "GrTypes.h" | 15 #include "GrTypes.h" |
16 #include "SkTMultiMap.h" | 16 #include "SkTMultiMap.h" |
17 #include "GrBinHashKey.h" | 17 #include "GrBinHashKey.h" |
18 #include "SkMessageBus.h" | 18 #include "SkMessageBus.h" |
19 #include "SkTInternalLList.h" | 19 #include "SkTInternalLList.h" |
20 | 20 |
21 class GrGpuObject; | 21 class GrGpuResource; |
22 class GrResourceCache; | 22 class GrResourceCache; |
23 class GrResourceCacheEntry; | 23 class GrResourceCacheEntry; |
24 | 24 |
25 class GrResourceKey { | 25 class GrResourceKey { |
26 public: | 26 public: |
27 static GrCacheID::Domain ScratchDomain() { | 27 static GrCacheID::Domain ScratchDomain() { |
28 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); | 28 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
29 return gDomain; | 29 return gDomain; |
30 } | 30 } |
31 | 31 |
32 /** Uniquely identifies the GrGpuObject subclass in the key to avoid collisi
ons | 32 /** Uniquely identifies the GrGpuResource subclass in the key to avoid colli
sions |
33 across resource types. */ | 33 across resource types. */ |
34 typedef uint8_t ResourceType; | 34 typedef uint8_t ResourceType; |
35 | 35 |
36 /** Flags set by the GrGpuObject subclass. */ | 36 /** Flags set by the GrGpuResource subclass. */ |
37 typedef uint8_t ResourceFlags; | 37 typedef uint8_t ResourceFlags; |
38 | 38 |
39 /** Generate a unique ResourceType */ | 39 /** Generate a unique ResourceType */ |
40 static ResourceType GenerateResourceType(); | 40 static ResourceType GenerateResourceType(); |
41 | 41 |
42 /** Creates a key for resource */ | 42 /** Creates a key for resource */ |
43 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 43 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
44 this->init(id.getDomain(), id.getKey(), type, flags); | 44 this->init(id.getDomain(), id.getKey(), type, flags); |
45 }; | 45 }; |
46 | 46 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 // The cache listens for these messages to purge junk resources proactively. | 112 // The cache listens for these messages to purge junk resources proactively. |
113 struct GrResourceInvalidatedMessage { | 113 struct GrResourceInvalidatedMessage { |
114 GrResourceKey key; | 114 GrResourceKey key; |
115 }; | 115 }; |
116 | 116 |
117 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
118 | 118 |
119 class GrResourceCacheEntry { | 119 class GrResourceCacheEntry { |
120 public: | 120 public: |
121 GrGpuObject* resource() const { return fResource; } | 121 GrGpuResource* resource() const { return fResource; } |
122 const GrResourceKey& key() const { return fKey; } | 122 const GrResourceKey& key() const { return fKey; } |
123 | 123 |
124 static const GrResourceKey& GetKey(const GrResourceCacheEntry& e) { return e
.key(); } | 124 static const GrResourceKey& GetKey(const GrResourceCacheEntry& e) { return e
.key(); } |
125 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } | 125 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
126 #ifdef SK_DEBUG | 126 #ifdef SK_DEBUG |
127 void validate() const; | 127 void validate() const; |
128 #else | 128 #else |
129 void validate() const {} | 129 void validate() const {} |
130 #endif | 130 #endif |
131 | 131 |
132 /** | 132 /** |
133 * Update the cached size for this entry and inform the resource cache that | 133 * Update the cached size for this entry and inform the resource cache that |
134 * it has changed. Usually invoked from GrGpuObject::didChangeGpuMemorySize
, | 134 * it has changed. Usually invoked from GrGpuResource::didChangeGpuMemorySi
ze, |
135 * not directly from here. | 135 * not directly from here. |
136 */ | 136 */ |
137 void didChangeResourceSize(); | 137 void didChangeResourceSize(); |
138 | 138 |
139 private: | 139 private: |
140 GrResourceCacheEntry(GrResourceCache* resourceCache, | 140 GrResourceCacheEntry(GrResourceCache* resourceCache, |
141 const GrResourceKey& key, | 141 const GrResourceKey& key, |
142 GrGpuObject* resource); | 142 GrGpuResource* resource); |
143 ~GrResourceCacheEntry(); | 143 ~GrResourceCacheEntry(); |
144 | 144 |
145 GrResourceCache* fResourceCache; | 145 GrResourceCache* fResourceCache; |
146 GrResourceKey fKey; | 146 GrResourceKey fKey; |
147 GrGpuObject* fResource; | 147 GrGpuResource* fResource; |
148 size_t fCachedSize; | 148 size_t fCachedSize; |
149 bool fIsExclusive; | 149 bool fIsExclusive; |
150 | 150 |
151 // Linked list for the LRU ordering. | 151 // Linked list for the LRU ordering. |
152 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceCacheEntry); | 152 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceCacheEntry); |
153 | 153 |
154 friend class GrResourceCache; | 154 friend class GrResourceCache; |
155 }; | 155 }; |
156 | 156 |
157 /////////////////////////////////////////////////////////////////////////////// | 157 /////////////////////////////////////////////////////////////////////////////// |
158 | 158 |
159 /** | 159 /** |
160 * Cache of GrGpuObject objects. | 160 * Cache of GrGpuResource objects. |
161 * | 161 * |
162 * These have a corresponding GrResourceKey, built from 128bits identifying the | 162 * These have a corresponding GrResourceKey, built from 128bits identifying the |
163 * resource. Multiple resources can map to same GrResourceKey. | 163 * resource. Multiple resources can map to same GrResourceKey. |
164 * | 164 * |
165 * The cache stores the entries in a double-linked list, which is its LRU. | 165 * The cache stores the entries in a double-linked list, which is its LRU. |
166 * When an entry is "locked" (i.e. given to the caller), it is moved to the | 166 * When an entry is "locked" (i.e. given to the caller), it is moved to the |
167 * head of the list. If/when we must purge some of the entries, we walk the | 167 * head of the list. If/when we must purge some of the entries, we walk the |
168 * list backwards from the tail, since those are the least recently used. | 168 * list backwards from the tail, since those are the least recently used. |
169 * | 169 * |
170 * For fast searches, we maintain a hash map based on the GrResourceKey. | 170 * For fast searches, we maintain a hash map based on the GrResourceKey. |
171 * | 171 * |
172 * It is a goal to make the GrResourceCache the central repository and bookkeep
er | 172 * It is a goal to make the GrResourceCache the central repository and bookkeep
er |
173 * of all resources. It should replace the linked list of GrGpuObjects that | 173 * of all resources. It should replace the linked list of GrGpuResources that |
174 * GrGpu uses to call abandon/release. | 174 * GrGpu uses to call abandon/release. |
175 */ | 175 */ |
176 class GrResourceCache { | 176 class GrResourceCache { |
177 public: | 177 public: |
178 GrResourceCache(int maxCount, size_t maxBytes); | 178 GrResourceCache(int maxCount, size_t maxBytes); |
179 ~GrResourceCache(); | 179 ~GrResourceCache(); |
180 | 180 |
181 /** | 181 /** |
182 * Return the current resource cache limits. | 182 * Return the current resource cache limits. |
183 * | 183 * |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 * Search for an entry with the same Key. If found, return it. | 239 * Search for an entry with the same Key. If found, return it. |
240 * If not found, return null. | 240 * If not found, return null. |
241 * If ownershipFlags includes kNoOtherOwners and a resource is returned | 241 * If ownershipFlags includes kNoOtherOwners and a resource is returned |
242 * then that resource has no other refs to it. | 242 * then that resource has no other refs to it. |
243 * If ownershipFlags includes kHide and a resource is returned then that | 243 * If ownershipFlags includes kHide and a resource is returned then that |
244 * resource will not be returned from future 'find' calls until it is | 244 * resource will not be returned from future 'find' calls until it is |
245 * 'freed' (and recycled) or makeNonExclusive is called. | 245 * 'freed' (and recycled) or makeNonExclusive is called. |
246 * For a resource to be completely exclusive to a caller both kNoOtherOwner
s | 246 * For a resource to be completely exclusive to a caller both kNoOtherOwner
s |
247 * and kHide must be specified. | 247 * and kHide must be specified. |
248 */ | 248 */ |
249 GrGpuObject* find(const GrResourceKey& key, | 249 GrGpuResource* find(const GrResourceKey& key, |
250 uint32_t ownershipFlags = 0); | 250 uint32_t ownershipFlags = 0); |
251 | 251 |
252 /** | 252 /** |
253 * Add the new resource to the cache (by creating a new cache entry based | 253 * Add the new resource to the cache (by creating a new cache entry based |
254 * on the provided key and resource). | 254 * on the provided key and resource). |
255 * | 255 * |
256 * Ownership of the resource is transferred to the resource cache, | 256 * Ownership of the resource is transferred to the resource cache, |
257 * which will unref() it when it is purged or deleted. | 257 * which will unref() it when it is purged or deleted. |
258 * | 258 * |
259 * If ownershipFlags includes kHide, subsequent calls to 'find' will not | 259 * If ownershipFlags includes kHide, subsequent calls to 'find' will not |
260 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive | 260 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive |
261 * is called. | 261 * is called. |
262 */ | 262 */ |
263 void addResource(const GrResourceKey& key, | 263 void addResource(const GrResourceKey& key, |
264 GrGpuObject* resource, | 264 GrGpuResource* resource, |
265 uint32_t ownershipFlags = 0); | 265 uint32_t ownershipFlags = 0); |
266 | 266 |
267 /** | 267 /** |
268 * Determines if the cache contains an entry matching a key. If a matching | 268 * Determines if the cache contains an entry matching a key. If a matching |
269 * entry exists but was detached then it will not be found. | 269 * entry exists but was detached then it will not be found. |
270 */ | 270 */ |
271 bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key
); } | 271 bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key
); } |
272 | 272 |
273 /** | 273 /** |
274 * Hide 'entry' so that future searches will not find it. Such | 274 * Hide 'entry' so that future searches will not find it. Such |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 GrResourceCache* fCache; | 394 GrResourceCache* fCache; |
395 }; | 395 }; |
396 #else | 396 #else |
397 class GrAutoResourceCacheValidate { | 397 class GrAutoResourceCacheValidate { |
398 public: | 398 public: |
399 GrAutoResourceCacheValidate(GrResourceCache*) {} | 399 GrAutoResourceCacheValidate(GrResourceCache*) {} |
400 }; | 400 }; |
401 #endif | 401 #endif |
402 | 402 |
403 #endif | 403 #endif |
OLD | NEW |