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