| 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 #include "GrGpuResource.h" | 10 #include "GrGpuResource.h" |
| 11 #include "GrResourceCache2.h" | 11 #include "GrResourceCache2.h" |
| 12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
| 13 | 13 |
| 14 static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { | 14 static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { |
| 15 SkASSERT(gpu); | 15 SkASSERT(gpu); |
| 16 SkASSERT(gpu->getContext()); | 16 SkASSERT(gpu->getContext()); |
| 17 SkASSERT(gpu->getContext()->getResourceCache2()); | 17 SkASSERT(gpu->getContext()->getResourceCache2()); |
| 18 return gpu->getContext()->getResourceCache2(); | 18 return gpu->getContext()->getResourceCache2(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) | 21 GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) |
| 22 : fGpu(gpu) | 22 : fScratchKey(GrResourceKey::NullScratchKey()) |
| 23 , fGpu(gpu) |
| 23 , fGpuMemorySize(kInvalidGpuMemorySize) | 24 , fGpuMemorySize(kInvalidGpuMemorySize) |
| 24 , fUniqueID(CreateUniqueID()) | 25 , fUniqueID(CreateUniqueID()) { |
| 25 , fScratchKey(GrResourceKey::NullScratchKey()) | |
| 26 , fContentKeySet(false) { | |
| 27 if (isWrapped) { | 26 if (isWrapped) { |
| 28 fFlags = kWrapped_FlagBit; | 27 fFlags = kWrapped_Flag; |
| 29 } else { | 28 } else { |
| 30 fFlags = 0; | 29 // By default all non-wrapped resources are budgeted. |
| 30 fFlags = kBudgeted_Flag; |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 void GrGpuResource::registerWithCache() { | 34 void GrGpuResource::registerWithCache() { |
| 35 get_resource_cache2(fGpu)->resourceAccess().insertResource(this); | 35 get_resource_cache2(fGpu)->resourceAccess().insertResource(this); |
| 36 } | 36 } |
| 37 | 37 |
| 38 GrGpuResource::~GrGpuResource() { | 38 GrGpuResource::~GrGpuResource() { |
| 39 // The cache should have released or destroyed this resource. | 39 // The cache should have released or destroyed this resource. |
| 40 SkASSERT(this->wasDestroyed()); | 40 SkASSERT(this->wasDestroyed()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 85 |
| 86 bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) { | 86 bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) { |
| 87 // Currently this can only be called once and can't be called when the resou
rce is scratch. | 87 // Currently this can only be called once and can't be called when the resou
rce is scratch. |
| 88 SkASSERT(!contentKey.isScratch()); | 88 SkASSERT(!contentKey.isScratch()); |
| 89 SkASSERT(this->internalHasRef()); | 89 SkASSERT(this->internalHasRef()); |
| 90 | 90 |
| 91 // Wrapped resources can never have a key. | 91 // Wrapped resources can never have a key. |
| 92 if (this->isWrapped()) { | 92 if (this->isWrapped()) { |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 | 95 |
| 96 if (fContentKeySet || this->wasDestroyed()) { | 96 if ((fFlags & kContentKeySet_Flag) || this->wasDestroyed()) { |
| 97 return false; | 97 return false; |
| 98 } | 98 } |
| 99 | 99 |
| 100 fContentKey = contentKey; | 100 fContentKey = contentKey; |
| 101 fContentKeySet = true; | 101 fFlags |= kContentKeySet_Flag; |
| 102 | 102 |
| 103 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) { | 103 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) { |
| 104 fContentKeySet = false; | 104 fFlags &= ~kContentKeySet_Flag; |
| 105 return false; | 105 return false; |
| 106 } | 106 } |
| 107 return true; | 107 return true; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void GrGpuResource::notifyIsPurgable() const { | 110 void GrGpuResource::notifyIsPurgable() const { |
| 111 if (this->wasDestroyed()) { | 111 if (this->wasDestroyed()) { |
| 112 // We've already been removed from the cache. Goodbye cruel world! | 112 // We've already been removed from the cache. Goodbye cruel world! |
| 113 SkDELETE(this); | 113 SkDELETE(this); |
| 114 } else { | 114 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 129 } | 129 } |
| 130 | 130 |
| 131 uint32_t GrGpuResource::CreateUniqueID() { | 131 uint32_t GrGpuResource::CreateUniqueID() { |
| 132 static int32_t gUniqueID = SK_InvalidUniqueID; | 132 static int32_t gUniqueID = SK_InvalidUniqueID; |
| 133 uint32_t id; | 133 uint32_t id; |
| 134 do { | 134 do { |
| 135 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); | 135 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 136 } while (id == SK_InvalidUniqueID); | 136 } while (id == SK_InvalidUniqueID); |
| 137 return id; | 137 return id; |
| 138 } | 138 } |
| 139 |
| 140 void GrGpuResource::setBudgeted(bool countsAgainstBudget) { |
| 141 // Wrapped resources never count against the budget, nothing to do. No point
in changing the |
| 142 // budgeting of destroyed resources. |
| 143 if (this->isWrapped() || this->wasDestroyed()) { |
| 144 return; |
| 145 } |
| 146 |
| 147 uint32_t oldFlags = fFlags; |
| 148 if (countsAgainstBudget) { |
| 149 fFlags |= kBudgeted_Flag; |
| 150 } else { |
| 151 fFlags &= ~kBudgeted_Flag; |
| 152 } |
| 153 if (fFlags != oldFlags) { |
| 154 get_resource_cache2(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
| 155 } |
| 156 } |
| OLD | NEW |