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