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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTraceMemoryDump.h" |
| 9 | #include "include/gpu/GrContext.h" |
| 10 | #include "include/gpu/GrGpuResource.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
| 12 | #include "src/gpu/GrGpu.h" |
| 13 | #include "src/gpu/GrGpuResourcePriv.h" |
| 14 | #include "src/gpu/GrResourceCache.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. |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 99 | return !this->hasRef() && |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 100 | !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid()); |
| 101 | } |
| 102 | |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 103 | bool GrGpuResource::hasRef() const { return this->internalHasRef(); } |
| 104 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 105 | SkString GrGpuResource::getResourceName() const { |
| 106 | // Dump resource as "skia/gpu_resources/resource_#". |
| 107 | SkString resourceName("skia/gpu_resources/resource_"); |
| 108 | resourceName.appendU32(this->uniqueID().asUInt()); |
| 109 | return resourceName; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 112 | const GrContext* GrGpuResource::getContext() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 113 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 114 | return fGpu->getContext(); |
| 115 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | return nullptr; |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 120 | GrContext* GrGpuResource::getContext() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 121 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 122 | return fGpu->getContext(); |
| 123 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | return nullptr; |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 127 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 128 | void GrGpuResource::removeUniqueKey() { |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 129 | if (this->wasDestroyed()) { |
| 130 | return; |
| 131 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 132 | SkASSERT(fUniqueKey.isValid()); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 133 | get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 136 | void GrGpuResource::setUniqueKey(const GrUniqueKey& key) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 137 | SkASSERT(this->internalHasRef()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 138 | SkASSERT(key.isValid()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 139 | |
Brian Osman | 0562eb9 | 2017-05-08 11:16:39 -0400 | [diff] [blame] | 140 | // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped |
| 141 | // resources are a special case: the unique keys give us a weak ref so that we can reuse the |
| 142 | // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced, |
| 143 | // 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] | 144 | if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted && |
| 145 | !this->fRefsWrappedObjects) { |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 146 | return; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 147 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 148 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 149 | if (this->wasDestroyed()) { |
| 150 | return; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 151 | } |
| 152 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 153 | get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 156 | void GrGpuResource::notifyRefCntWillBeZero() const { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 157 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 158 | mutableThis->willRemoveLastRef(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 161 | void GrGpuResource::notifyRefCntIsZero() const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 162 | if (this->wasDestroyed()) { |
| 163 | // We've already been removed from the cache. Goodbye cruel world! |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 164 | delete this; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 165 | return; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 166 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 167 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 168 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 169 | |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 170 | get_resource_cache(fGpu)->resourceAccess().notifyRefCntReachedZero(mutableThis); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 173 | void GrGpuResource::removeScratchKey() { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 174 | if (!this->wasDestroyed() && fScratchKey.isValid()) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 175 | get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 176 | fScratchKey.reset(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 180 | void GrGpuResource::makeBudgeted() { |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 181 | // We should never make a wrapped resource budgeted. |
| 182 | SkASSERT(!fRefsWrappedObjects); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 183 | // Only wrapped resources can be in the kUnbudgetedCacheable state. |
| 184 | SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable); |
| 185 | if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 186 | // Currently resources referencing wrapped objects are not budgeted. |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 187 | fBudgetedType = GrBudgetedType::kBudgeted; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 188 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 192 | void GrGpuResource::makeUnbudgeted() { |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 193 | if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted && |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 194 | !fUniqueKey.isValid()) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 195 | fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 196 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 200 | uint32_t GrGpuResource::CreateUniqueID() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 201 | static std::atomic<uint32_t> nextID{1}; |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 202 | uint32_t id; |
| 203 | do { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 204 | id = nextID++; |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 205 | } while (id == SK_InvalidUniqueID); |
| 206 | return id; |
| 207 | } |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 208 | |
| 209 | ////////////////////////////////////////////////////////////////////////////// |
| 210 | |
| 211 | void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) { |
| 212 | SkASSERT(cache == fResource->getContext()->priv().getResourceCache()); |
| 213 | cache->resourceAccess().refResource(fResource); |
| 214 | } |