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