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