blob: 6dc0dfa360e25d5a304b38f0af5925f2355cee0c [file] [log] [blame]
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com8fe72472011-03-30 21:26:44 +00006 */
7
bsalomon6d3fe022014-07-25 08:35:45 -07008#include "GrGpuResource.h"
kkinnunencabe20c2015-06-01 01:37:26 -07009#include "GrContext.h"
Robert Phillips6be756b2018-01-16 15:07:54 -050010#include "GrContextPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080011#include "GrResourceCache.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000012#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070014#include "SkTraceMemoryDump.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050015#include <atomic>
bsalomon@google.com8fe72472011-03-30 21:26:44 +000016
bsalomon0ea80f42015-02-11 10:49:59 -080017static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070018 SkASSERT(gpu);
19 SkASSERT(gpu->getContext());
Robert Phillips9da87e02019-02-04 13:26:26 -050020 SkASSERT(gpu->getContext()->priv().getResourceCache());
21 return gpu->getContext()->priv().getResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070022}
23
Brian Salomonfa2ebea2019-01-24 15:58:58 -050024GrGpuResource::GrGpuResource(GrGpu* gpu) : fGpu(gpu), fUniqueID(CreateUniqueID()) {
bsalomon9f2d1572015-02-17 11:47:40 -080025 SkDEBUGCODE(fCacheArrayIndex = -1);
bsalomon16961262014-08-26 14:01:07 -070026}
27
kkinnunen2e6055b2016-04-22 01:48:29 -070028void GrGpuResource::registerWithCache(SkBudgeted budgeted) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050029 SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
Brian Salomonfa2ebea2019-01-24 15:58:58 -050030 fBudgetedType = budgeted == SkBudgeted::kYes ? GrBudgetedType::kBudgeted
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050031 : GrBudgetedType::kUnbudgetedUncacheable;
kkinnunen2e6055b2016-04-22 01:48:29 -070032 this->computeScratchKey(&fScratchKey);
33 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
34}
35
Brian Salomonfa2ebea2019-01-24 15:58:58 -050036void GrGpuResource::registerWithCacheWrapped(GrWrapCacheable wrapType) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050037 SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
Brian Salomonfa2ebea2019-01-24 15:58:58 -050038 // Resources referencing wrapped objects are never budgeted. They may be cached or uncached.
39 fBudgetedType = wrapType == GrWrapCacheable::kNo ? GrBudgetedType::kUnbudgetedUncacheable
40 : GrBudgetedType::kUnbudgetedCacheable;
kkinnunen2e6055b2016-04-22 01:48:29 -070041 fRefsWrappedObjects = true;
bsalomon0ea80f42015-02-11 10:49:59 -080042 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043}
44
bsalomon6d3fe022014-07-25 08:35:45 -070045GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080046 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000047 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000048}
49
halcanary9d524f22016-03-29 09:03:52 -070050void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080051 SkASSERT(fGpu);
52 this->onRelease();
bsalomon0ea80f42015-02-11 10:49:59 -080053 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
halcanary96fcdcc2015-08-27 07:41:13 -070054 fGpu = nullptr;
bsalomon12299ab2014-11-14 13:33:09 -080055 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000056}
57
bsalomon6d3fe022014-07-25 08:35:45 -070058void GrGpuResource::abandon() {
bsalomonc6363ef2015-09-24 07:07:40 -070059 if (this->wasDestroyed()) {
60 return;
61 }
bsalomon12299ab2014-11-14 13:33:09 -080062 SkASSERT(fGpu);
63 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080064 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
halcanary96fcdcc2015-08-27 07:41:13 -070065 fGpu = nullptr;
bsalomon12299ab2014-11-14 13:33:09 -080066 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000067}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000068
ericrk0a5fa482015-09-15 14:16:10 -070069void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -070070 if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
71 return;
72 }
73
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040074 this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(),
75 this->getResourceType(), this->gpuMemorySize());
76}
ericrk0a5fa482015-09-15 14:16:10 -070077
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040078void 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";
ericrk0a5fa482015-09-15 14:16:10 -070084 }
85
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040086 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 Salomon9bc76d92019-01-24 12:18:33 -050096bool 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.
99 return !this->hasRefOrPendingIO() &&
100 !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
101}
102
103bool GrGpuResource::hasRefOrPendingIO() const {
104 return this->internalHasRef() || this->internalHasPendingIO();
105}
106
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400107SkString GrGpuResource::getResourceName() const {
108 // Dump resource as "skia/gpu_resources/resource_#".
109 SkString resourceName("skia/gpu_resources/resource_");
110 resourceName.appendU32(this->uniqueID().asUInt());
111 return resourceName;
ericrk0a5fa482015-09-15 14:16:10 -0700112}
113
bsalomon6d3fe022014-07-25 08:35:45 -0700114const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -0700115 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000116 return fGpu->getContext();
117 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000119 }
120}
121
bsalomon6d3fe022014-07-25 08:35:45 -0700122GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -0700123 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000124 return fGpu->getContext();
125 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000127 }
128}
bsalomonc44be0e2014-07-25 07:32:33 -0700129
bsalomon8718aaf2015-02-19 07:24:21 -0800130void GrGpuResource::removeUniqueKey() {
bsalomonc6363ef2015-09-24 07:07:40 -0700131 if (this->wasDestroyed()) {
132 return;
133 }
bsalomon8718aaf2015-02-19 07:24:21 -0800134 SkASSERT(fUniqueKey.isValid());
bsalomonf99e9612015-02-19 08:24:16 -0800135 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
bsalomon23e619c2015-02-06 11:54:28 -0800136}
137
bsalomonf99e9612015-02-19 08:24:16 -0800138void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -0800139 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -0800140 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -0800141
Brian Osman0562eb92017-05-08 11:16:39 -0400142 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
143 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
144 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
145 // it will always be released - it is never converted to a scratch resource.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500146 if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
147 !this->fRefsWrappedObjects) {
bsalomonf99e9612015-02-19 08:24:16 -0800148 return;
bsalomondace19e2014-11-17 07:34:06 -0800149 }
bsalomon84c8e622014-11-17 09:33:27 -0800150
bsalomonf99e9612015-02-19 08:24:16 -0800151 if (this->wasDestroyed()) {
152 return;
bsalomon8b79d232014-11-10 10:19:06 -0800153 }
154
bsalomonf99e9612015-02-19 08:24:16 -0800155 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
bsalomon8b79d232014-11-10 10:19:06 -0800156}
157
Brian Salomon8cabb322019-02-22 10:44:19 -0500158void GrGpuResource::notifyAllCntsWillBeZero() const {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500159 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
Brian Salomon8cabb322019-02-22 10:44:19 -0500160 mutableThis->willRemoveLastRefOrPendingIO();
161}
162
163void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
bsalomon12299ab2014-11-14 13:33:09 -0800164 if (this->wasDestroyed()) {
165 // We've already been removed from the cache. Goodbye cruel world!
halcanary385fe4d2015-08-26 13:07:48 -0700166 delete this;
bsalomon3f324322015-04-08 11:01:54 -0700167 return;
bsalomonbcf0a522014-10-08 08:40:09 -0700168 }
bsalomon3f324322015-04-08 11:01:54 -0700169
170 // We should have already handled this fully in notifyRefCntIsZero().
171 SkASSERT(kRef_CntType != lastCntTypeToReachZero);
172
bsalomon3f324322015-04-08 11:01:54 -0700173 static const uint32_t kFlag =
174 GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
Brian Salomon8cabb322019-02-22 10:44:19 -0500175 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
bsalomon3f324322015-04-08 11:01:54 -0700176 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
177}
178
179bool GrGpuResource::notifyRefCountIsZero() const {
180 if (this->wasDestroyed()) {
181 // handle this in notifyAllCntsAreZero().
182 return true;
183 }
184
185 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
Robert Phillipsb6deea82017-05-11 14:14:30 -0400186 uint32_t flags = GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
bsalomon3f324322015-04-08 11:01:54 -0700187 if (!this->internalHasPendingIO()) {
188 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
189 }
190 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
191
192 // There is no need to call our notifyAllCntsAreZero function at this point since we already
193 // told the cache about the state of cnts.
194 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700195}
196
bsalomon10e23ca2014-11-25 05:52:06 -0800197void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800198 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800199 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800200 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800201 }
202}
203
bsalomonafe30052015-01-16 07:32:33 -0800204void GrGpuResource::makeBudgeted() {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500205 // We should never make a wrapped resource budgeted.
206 SkASSERT(!fRefsWrappedObjects);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500207 // Only wrapped resources can be in the kUnbudgetedCacheable state.
208 SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
209 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700210 // Currently resources referencing wrapped objects are not budgeted.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500211 fBudgetedType = GrBudgetedType::kBudgeted;
bsalomon0ea80f42015-02-11 10:49:59 -0800212 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800213 }
214}
215
bsalomonc2f35b72015-01-23 07:19:22 -0800216void GrGpuResource::makeUnbudgeted() {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500217 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
bsalomonc6363ef2015-09-24 07:07:40 -0700218 !fUniqueKey.isValid()) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500219 fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
bsalomon0ea80f42015-02-11 10:49:59 -0800220 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800221 }
222}
223
bsalomon6d3fe022014-07-25 08:35:45 -0700224uint32_t GrGpuResource::CreateUniqueID() {
Mike Klein0ec1c572018-12-04 11:52:51 -0500225 static std::atomic<uint32_t> nextID{1};
bsalomonc44be0e2014-07-25 07:32:33 -0700226 uint32_t id;
227 do {
Mike Klein0ec1c572018-12-04 11:52:51 -0500228 id = nextID++;
bsalomonc44be0e2014-07-25 07:32:33 -0700229 } while (id == SK_InvalidUniqueID);
230 return id;
231}