bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 8 | #include "GrGpuResource.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 9 | #include "GrContext.h" |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 11 | #include "GrResourceCache.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 14 | #include "SkTraceMemoryDump.h" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 15 | #include <atomic> |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 16 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 17 | static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 18 | SkASSERT(gpu); |
| 19 | SkASSERT(gpu->getContext()); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 20 | SkASSERT(gpu->getContext()->priv().getResourceCache()); |
| 21 | return gpu->getContext()->priv().getResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 24 | GrGpuResource::GrGpuResource(GrGpu* gpu) : fGpu(gpu), fUniqueID(CreateUniqueID()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 25 | SkDEBUGCODE(fCacheArrayIndex = -1); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 26 | } |
| 27 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 28 | void GrGpuResource::registerWithCache(SkBudgeted budgeted) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 29 | SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable); |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 30 | fBudgetedType = budgeted == SkBudgeted::kYes ? GrBudgetedType::kBudgeted |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 31 | : GrBudgetedType::kUnbudgetedUncacheable; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 32 | this->computeScratchKey(&fScratchKey); |
| 33 | get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
| 34 | } |
| 35 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 36 | void GrGpuResource::registerWithCacheWrapped(GrWrapCacheable wrapType) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 37 | SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable); |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 38 | // Resources referencing wrapped objects are never budgeted. They may be cached or uncached. |
| 39 | fBudgetedType = wrapType == GrWrapCacheable::kNo ? GrBudgetedType::kUnbudgetedUncacheable |
| 40 | : GrBudgetedType::kUnbudgetedCacheable; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 41 | fRefsWrappedObjects = true; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 42 | get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 43 | } |
| 44 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 45 | GrGpuResource::~GrGpuResource() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 46 | // The cache should have released or destroyed this resource. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 47 | SkASSERT(this->wasDestroyed()); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 48 | } |
| 49 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 50 | void GrGpuResource::release() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 51 | SkASSERT(fGpu); |
| 52 | this->onRelease(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 53 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | fGpu = nullptr; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 55 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 56 | } |
| 57 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 58 | void GrGpuResource::abandon() { |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 59 | if (this->wasDestroyed()) { |
| 60 | return; |
| 61 | } |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 62 | SkASSERT(fGpu); |
| 63 | this->onAbandon(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 64 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | fGpu = nullptr; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 66 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 67 | } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 68 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 69 | void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 70 | if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 71 | return; |
| 72 | } |
| 73 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 74 | this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(), |
| 75 | this->getResourceType(), this->gpuMemorySize()); |
| 76 | } |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 77 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 78 | void GrGpuResource::dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump, |
| 79 | const SkString& resourceName, |
| 80 | const char* type, size_t size) const { |
| 81 | const char* tag = "Scratch"; |
| 82 | if (fUniqueKey.isValid()) { |
| 83 | tag = (fUniqueKey.tag() != nullptr) ? fUniqueKey.tag() : "Other"; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 86 | traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size); |
| 87 | traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", type); |
| 88 | traceMemoryDump->dumpStringValue(resourceName.c_str(), "category", tag); |
| 89 | if (this->isPurgeable()) { |
| 90 | traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size); |
| 91 | } |
| 92 | |
| 93 | this->setMemoryBacking(traceMemoryDump, resourceName); |
| 94 | } |
| 95 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 96 | bool GrGpuResource::isPurgeable() const { |
| 97 | // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique |
| 98 | // key. The key must be removed/invalidated to make them purgeable. |
| 99 | return !this->hasRefOrPendingIO() && |
| 100 | !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid()); |
| 101 | } |
| 102 | |
| 103 | bool GrGpuResource::hasRefOrPendingIO() const { |
| 104 | return this->internalHasRef() || this->internalHasPendingIO(); |
| 105 | } |
| 106 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 107 | SkString GrGpuResource::getResourceName() const { |
| 108 | // Dump resource as "skia/gpu_resources/resource_#". |
| 109 | SkString resourceName("skia/gpu_resources/resource_"); |
| 110 | resourceName.appendU32(this->uniqueID().asUInt()); |
| 111 | return resourceName; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 112 | } |
| 113 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 114 | const GrContext* GrGpuResource::getContext() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 115 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 116 | return fGpu->getContext(); |
| 117 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | return nullptr; |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 122 | GrContext* GrGpuResource::getContext() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 123 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 124 | return fGpu->getContext(); |
| 125 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | return nullptr; |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 129 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 130 | void GrGpuResource::removeUniqueKey() { |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 131 | if (this->wasDestroyed()) { |
| 132 | return; |
| 133 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 134 | SkASSERT(fUniqueKey.isValid()); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 135 | get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 136 | } |
| 137 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 138 | void GrGpuResource::setUniqueKey(const GrUniqueKey& key) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 139 | SkASSERT(this->internalHasRef()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 140 | SkASSERT(key.isValid()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 141 | |
Brian Osman | 0562eb9 | 2017-05-08 11:16:39 -0400 | [diff] [blame] | 142 | // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped |
| 143 | // resources are a special case: the unique keys give us a weak ref so that we can reuse the |
| 144 | // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced, |
| 145 | // it will always be released - it is never converted to a scratch resource. |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 146 | if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted && |
| 147 | !this->fRefsWrappedObjects) { |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 148 | return; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 149 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 150 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 151 | if (this->wasDestroyed()) { |
| 152 | return; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 153 | } |
| 154 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 155 | get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 158 | void GrGpuResource::notifyAllCntsWillBeZero() const { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 159 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 160 | mutableThis->willRemoveLastRefOrPendingIO(); |
| 161 | } |
| 162 | |
| 163 | void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 164 | if (this->wasDestroyed()) { |
| 165 | // We've already been removed from the cache. Goodbye cruel world! |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 166 | delete this; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 167 | return; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 168 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 169 | |
| 170 | // We should have already handled this fully in notifyRefCntIsZero(). |
| 171 | SkASSERT(kRef_CntType != lastCntTypeToReachZero); |
| 172 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 173 | static const uint32_t kFlag = |
| 174 | GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 175 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 176 | get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag); |
| 177 | } |
| 178 | |
| 179 | bool GrGpuResource::notifyRefCountIsZero() const { |
| 180 | if (this->wasDestroyed()) { |
| 181 | // handle this in notifyAllCntsAreZero(). |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 186 | uint32_t flags = GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 187 | if (!this->internalHasPendingIO()) { |
| 188 | flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag; |
| 189 | } |
| 190 | get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags); |
| 191 | |
| 192 | // There is no need to call our notifyAllCntsAreZero function at this point since we already |
| 193 | // told the cache about the state of cnts. |
| 194 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 195 | } |
| 196 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 197 | void GrGpuResource::removeScratchKey() { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 198 | if (!this->wasDestroyed() && fScratchKey.isValid()) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 199 | get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 200 | fScratchKey.reset(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 204 | void GrGpuResource::makeBudgeted() { |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 205 | // We should never make a wrapped resource budgeted. |
| 206 | SkASSERT(!fRefsWrappedObjects); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 207 | // Only wrapped resources can be in the kUnbudgetedCacheable state. |
| 208 | SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable); |
| 209 | if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 210 | // Currently resources referencing wrapped objects are not budgeted. |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 211 | fBudgetedType = GrBudgetedType::kBudgeted; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 212 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 216 | void GrGpuResource::makeUnbudgeted() { |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 217 | if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted && |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 218 | !fUniqueKey.isValid()) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 219 | fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 220 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 224 | uint32_t GrGpuResource::CreateUniqueID() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 225 | static std::atomic<uint32_t> nextID{1}; |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 226 | uint32_t id; |
| 227 | do { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 228 | id = nextID++; |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 229 | } while (id == SK_InvalidUniqueID); |
| 230 | return id; |
| 231 | } |