| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrGpuResource.h" | 8 #include "GrGpuResource.h" |
| 9 #include "GrContext.h" | 9 #include "GrContext.h" |
| 10 #include "GrResourceCache.h" | 10 #include "GrResourceCache.h" |
| 11 #include "GrGpu.h" | 11 #include "GrGpu.h" |
| 12 #include "GrGpuResourcePriv.h" | 12 #include "GrGpuResourcePriv.h" |
| 13 #include "SkTraceMemoryDump.h" | 13 #include "SkTraceMemoryDump.h" |
| 14 | 14 |
| 15 static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { | 15 static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
| 16 SkASSERT(gpu); | 16 SkASSERT(gpu); |
| 17 SkASSERT(gpu->getContext()); | 17 SkASSERT(gpu->getContext()); |
| 18 SkASSERT(gpu->getContext()->getResourceCache()); | 18 SkASSERT(gpu->getContext()->getResourceCache()); |
| 19 return gpu->getContext()->getResourceCache(); | 19 return gpu->getContext()->getResourceCache(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle) | 22 GrGpuResource::GrGpuResource(GrGpu* gpu) |
| 23 : fGpu(gpu) | 23 : fGpu(gpu) |
| 24 , fGpuMemorySize(kInvalidGpuMemorySize) | 24 , fGpuMemorySize(kInvalidGpuMemorySize) |
| 25 , fLifeCycle(lifeCycle) | 25 , fBudgeted(SkBudgeted::kNo) |
| 26 , fRefsWrappedObjects(false) |
| 26 , fUniqueID(CreateUniqueID()) { | 27 , fUniqueID(CreateUniqueID()) { |
| 27 SkDEBUGCODE(fCacheArrayIndex = -1); | 28 SkDEBUGCODE(fCacheArrayIndex = -1); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void GrGpuResource::registerWithCache() { | 31 void GrGpuResource::registerWithCache(SkBudgeted budgeted) { |
| 32 SkASSERT(fBudgeted == SkBudgeted::kNo); |
| 33 fBudgeted = budgeted; |
| 34 this->computeScratchKey(&fScratchKey); |
| 31 get_resource_cache(fGpu)->resourceAccess().insertResource(this); | 35 get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
| 32 } | 36 } |
| 33 | 37 |
| 38 void GrGpuResource::registerWithCacheWrapped() { |
| 39 SkASSERT(fBudgeted == SkBudgeted::kNo); |
| 40 // Currently resources referencing wrapped objects are not budgeted. |
| 41 fRefsWrappedObjects = true; |
| 42 get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
| 43 } |
| 44 |
| 34 GrGpuResource::~GrGpuResource() { | 45 GrGpuResource::~GrGpuResource() { |
| 35 // The cache should have released or destroyed this resource. | 46 // The cache should have released or destroyed this resource. |
| 36 SkASSERT(this->wasDestroyed()); | 47 SkASSERT(this->wasDestroyed()); |
| 37 } | 48 } |
| 38 | 49 |
| 39 void GrGpuResource::release() { | 50 void GrGpuResource::release() { |
| 40 SkASSERT(fGpu); | 51 SkASSERT(fGpu); |
| 41 this->onRelease(); | 52 this->onRelease(); |
| 42 get_resource_cache(fGpu)->resourceAccess().removeResource(this); | 53 get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
| 43 fGpu = nullptr; | 54 fGpu = nullptr; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 if (!this->internalHasPendingIO()) { | 168 if (!this->internalHasPendingIO()) { |
| 158 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotific
ationFlag; | 169 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotific
ationFlag; |
| 159 } | 170 } |
| 160 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis,
flags); | 171 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis,
flags); |
| 161 | 172 |
| 162 // There is no need to call our notifyAllCntsAreZero function at this point
since we already | 173 // There is no need to call our notifyAllCntsAreZero function at this point
since we already |
| 163 // told the cache about the state of cnts. | 174 // told the cache about the state of cnts. |
| 164 return false; | 175 return false; |
| 165 } | 176 } |
| 166 | 177 |
| 167 void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) { | |
| 168 SkASSERT(!fScratchKey.isValid()); | |
| 169 SkASSERT(scratchKey.isValid()); | |
| 170 // Wrapped resources can never have a scratch key. | |
| 171 if (this->resourcePriv().isExternal()) { | |
| 172 return; | |
| 173 } | |
| 174 fScratchKey = scratchKey; | |
| 175 } | |
| 176 | |
| 177 void GrGpuResource::removeScratchKey() { | 178 void GrGpuResource::removeScratchKey() { |
| 178 if (!this->wasDestroyed() && fScratchKey.isValid()) { | 179 if (!this->wasDestroyed() && fScratchKey.isValid()) { |
| 179 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); | 180 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
| 180 fScratchKey.reset(); | 181 fScratchKey.reset(); |
| 181 } | 182 } |
| 182 } | 183 } |
| 183 | 184 |
| 184 void GrGpuResource::makeBudgeted() { | 185 void GrGpuResource::makeBudgeted() { |
| 185 if (!this->wasDestroyed() && GrGpuResource::kUncached_LifeCycle == fLifeCycl
e) { | 186 if (!this->wasDestroyed() && SkBudgeted::kNo == fBudgeted) { |
| 186 fLifeCycle = kCached_LifeCycle; | 187 // Currently resources referencing wrapped objects are not budgeted. |
| 188 SkASSERT(!fRefsWrappedObjects); |
| 189 fBudgeted = SkBudgeted::kYes; |
| 187 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); | 190 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
| 188 } | 191 } |
| 189 } | 192 } |
| 190 | 193 |
| 191 void GrGpuResource::makeUnbudgeted() { | 194 void GrGpuResource::makeUnbudgeted() { |
| 192 if (!this->wasDestroyed() && GrGpuResource::kCached_LifeCycle == fLifeCycle
&& | 195 if (!this->wasDestroyed() && SkBudgeted::kYes == fBudgeted && |
| 193 !fUniqueKey.isValid()) { | 196 !fUniqueKey.isValid()) { |
| 194 fLifeCycle = kUncached_LifeCycle; | 197 fBudgeted = SkBudgeted::kNo; |
| 195 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); | 198 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
| 196 } | 199 } |
| 197 } | 200 } |
| 198 | 201 |
| 199 uint32_t GrGpuResource::CreateUniqueID() { | 202 uint32_t GrGpuResource::CreateUniqueID() { |
| 200 static int32_t gUniqueID = SK_InvalidUniqueID; | 203 static int32_t gUniqueID = SK_InvalidUniqueID; |
| 201 uint32_t id; | 204 uint32_t id; |
| 202 do { | 205 do { |
| 203 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); | 206 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 204 } while (id == SK_InvalidUniqueID); | 207 } while (id == SK_InvalidUniqueID); |
| 205 return id; | 208 return id; |
| 206 } | 209 } |
| OLD | NEW |