OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 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 #include "GrResourceCache.h" | 11 #include "GrResourceCache.h" |
12 #include "GrGpuObject.h" | 12 #include "GrGpuResource.h" |
13 | 13 |
14 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); | 14 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); |
15 | 15 |
16 /////////////////////////////////////////////////////////////////////////////// | 16 /////////////////////////////////////////////////////////////////////////////// |
17 | 17 |
18 void GrGpuObject::didChangeGpuMemorySize() const { | 18 void GrGpuResource::didChangeGpuMemorySize() const { |
19 if (this->isInCache()) { | 19 if (this->isInCache()) { |
20 fCacheEntry->didChangeResourceSize(); | 20 fCacheEntry->didChangeResourceSize(); |
21 } | 21 } |
22 } | 22 } |
23 | 23 |
24 /////////////////////////////////////////////////////////////////////////////// | 24 /////////////////////////////////////////////////////////////////////////////// |
25 | 25 |
26 GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { | 26 GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { |
27 static int32_t gNextType = 0; | 27 static int32_t gNextType = 0; |
28 | 28 |
29 int32_t type = sk_atomic_inc(&gNextType); | 29 int32_t type = sk_atomic_inc(&gNextType); |
30 if (type >= (1 << 8 * sizeof(ResourceType))) { | 30 if (type >= (1 << 8 * sizeof(ResourceType))) { |
31 SkFAIL("Too many Resource Types"); | 31 SkFAIL("Too many Resource Types"); |
32 } | 32 } |
33 | 33 |
34 return static_cast<ResourceType>(type); | 34 return static_cast<ResourceType>(type); |
35 } | 35 } |
36 | 36 |
37 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
38 | 38 |
39 GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache, | 39 GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache, |
40 const GrResourceKey& key, | 40 const GrResourceKey& key, |
41 GrGpuObject* resource) | 41 GrGpuResource* resource) |
42 : fResourceCache(resourceCache), | 42 : fResourceCache(resourceCache), |
43 fKey(key), | 43 fKey(key), |
44 fResource(resource), | 44 fResource(resource), |
45 fCachedSize(resource->gpuMemorySize()), | 45 fCachedSize(resource->gpuMemorySize()), |
46 fIsExclusive(false) { | 46 fIsExclusive(false) { |
47 // we assume ownership of the resource, and will unref it when we die | 47 // we assume ownership of the resource, and will unref it when we die |
48 SkASSERT(resource); | 48 SkASSERT(resource); |
49 resource->ref(); | 49 resource->ref(); |
50 } | 50 } |
51 | 51 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // This functor just searches for an entry with only a single ref (from | 190 // This functor just searches for an entry with only a single ref (from |
191 // the texture cache itself). Presumably in this situation no one else | 191 // the texture cache itself). Presumably in this situation no one else |
192 // is relying on the texture. | 192 // is relying on the texture. |
193 class GrTFindUnreffedFunctor { | 193 class GrTFindUnreffedFunctor { |
194 public: | 194 public: |
195 bool operator()(const GrResourceCacheEntry* entry) const { | 195 bool operator()(const GrResourceCacheEntry* entry) const { |
196 return entry->resource()->unique(); | 196 return entry->resource()->unique(); |
197 } | 197 } |
198 }; | 198 }; |
199 | 199 |
200 GrGpuObject* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipF
lags) { | 200 GrGpuResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershi
pFlags) { |
201 GrAutoResourceCacheValidate atcv(this); | 201 GrAutoResourceCacheValidate atcv(this); |
202 | 202 |
203 GrResourceCacheEntry* entry = NULL; | 203 GrResourceCacheEntry* entry = NULL; |
204 | 204 |
205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) { | 205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) { |
206 GrTFindUnreffedFunctor functor; | 206 GrTFindUnreffedFunctor functor; |
207 | 207 |
208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor); | 208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor); |
209 } else { | 209 } else { |
210 entry = fCache.find(key); | 210 entry = fCache.find(key); |
211 } | 211 } |
212 | 212 |
213 if (NULL == entry) { | 213 if (NULL == entry) { |
214 return NULL; | 214 return NULL; |
215 } | 215 } |
216 | 216 |
217 if (ownershipFlags & kHide_OwnershipFlag) { | 217 if (ownershipFlags & kHide_OwnershipFlag) { |
218 this->makeExclusive(entry); | 218 this->makeExclusive(entry); |
219 } else { | 219 } else { |
220 // Make this resource MRU | 220 // Make this resource MRU |
221 this->internalDetach(entry); | 221 this->internalDetach(entry); |
222 this->attachToHead(entry); | 222 this->attachToHead(entry); |
223 } | 223 } |
224 | 224 |
225 return entry->fResource; | 225 return entry->fResource; |
226 } | 226 } |
227 | 227 |
228 void GrResourceCache::addResource(const GrResourceKey& key, | 228 void GrResourceCache::addResource(const GrResourceKey& key, |
229 GrGpuObject* resource, | 229 GrGpuResource* resource, |
230 uint32_t ownershipFlags) { | 230 uint32_t ownershipFlags) { |
231 SkASSERT(NULL == resource->getCacheEntry()); | 231 SkASSERT(NULL == resource->getCacheEntry()); |
232 // we don't expect to create new resources during a purge. In theory | 232 // we don't expect to create new resources during a purge. In theory |
233 // this could cause purgeAsNeeded() into an infinite loop (e.g. | 233 // this could cause purgeAsNeeded() into an infinite loop (e.g. |
234 // each resource destroyed creates and locks 2 resources and | 234 // each resource destroyed creates and locks 2 resources and |
235 // unlocks 1 thereby causing a new purge). | 235 // unlocks 1 thereby causing a new purge). |
236 SkASSERT(!fPurging); | 236 SkASSERT(!fPurging); |
237 GrAutoResourceCacheValidate atcv(this); | 237 GrAutoResourceCacheValidate atcv(this); |
238 | 238 |
239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r
esource)); | 239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r
esource)); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 fEntryBytes, fHighWaterEntryBytes); | 536 fEntryBytes, fHighWaterEntryBytes); |
537 SkDebugf("\t\tDetached Entry Count: current %d high %d\n", | 537 SkDebugf("\t\tDetached Entry Count: current %d high %d\n", |
538 fClientDetachedCount, fHighWaterClientDetachedCount); | 538 fClientDetachedCount, fHighWaterClientDetachedCount); |
539 SkDebugf("\t\tDetached Bytes: current %d high %d\n", | 539 SkDebugf("\t\tDetached Bytes: current %d high %d\n", |
540 fClientDetachedBytes, fHighWaterClientDetachedBytes); | 540 fClientDetachedBytes, fHighWaterClientDetachedBytes); |
541 } | 541 } |
542 | 542 |
543 #endif | 543 #endif |
544 | 544 |
545 /////////////////////////////////////////////////////////////////////////////// | 545 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |