blob: 71f92932fa5bd82174f3ac7b9ced45007bf6cbcf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTraceMemoryDump.h"
9#include "include/gpu/GrContext.h"
10#include "include/gpu/GrGpuResource.h"
11#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrGpu.h"
13#include "src/gpu/GrGpuResourcePriv.h"
14#include "src/gpu/GrResourceCache.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.
Robert Phillipsbf8bf832019-08-30 13:13:44 -040099 return !this->hasRef() &&
Brian Salomon9bc76d92019-01-24 12:18:33 -0500100 !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
101}
102
Brian Salomon2c791fc2019-04-02 11:52:03 -0400103bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
104
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400105SkString GrGpuResource::getResourceName() const {
106 // Dump resource as "skia/gpu_resources/resource_#".
107 SkString resourceName("skia/gpu_resources/resource_");
108 resourceName.appendU32(this->uniqueID().asUInt());
109 return resourceName;
ericrk0a5fa482015-09-15 14:16:10 -0700110}
111
bsalomon6d3fe022014-07-25 08:35:45 -0700112const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -0700113 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000114 return fGpu->getContext();
115 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700116 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000117 }
118}
119
bsalomon6d3fe022014-07-25 08:35:45 -0700120GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -0700121 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000122 return fGpu->getContext();
123 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700124 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000125 }
126}
bsalomonc44be0e2014-07-25 07:32:33 -0700127
bsalomon8718aaf2015-02-19 07:24:21 -0800128void GrGpuResource::removeUniqueKey() {
bsalomonc6363ef2015-09-24 07:07:40 -0700129 if (this->wasDestroyed()) {
130 return;
131 }
bsalomon8718aaf2015-02-19 07:24:21 -0800132 SkASSERT(fUniqueKey.isValid());
bsalomonf99e9612015-02-19 08:24:16 -0800133 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
bsalomon23e619c2015-02-06 11:54:28 -0800134}
135
bsalomonf99e9612015-02-19 08:24:16 -0800136void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -0800137 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -0800138 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -0800139
Brian Osman0562eb92017-05-08 11:16:39 -0400140 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
141 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
142 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
143 // it will always be released - it is never converted to a scratch resource.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500144 if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
145 !this->fRefsWrappedObjects) {
bsalomonf99e9612015-02-19 08:24:16 -0800146 return;
bsalomondace19e2014-11-17 07:34:06 -0800147 }
bsalomon84c8e622014-11-17 09:33:27 -0800148
bsalomonf99e9612015-02-19 08:24:16 -0800149 if (this->wasDestroyed()) {
150 return;
bsalomon8b79d232014-11-10 10:19:06 -0800151 }
152
bsalomonf99e9612015-02-19 08:24:16 -0800153 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
bsalomon8b79d232014-11-10 10:19:06 -0800154}
155
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400156void GrGpuResource::notifyRefCntWillBeZero() const {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500157 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400158 mutableThis->willRemoveLastRef();
Brian Salomon8cabb322019-02-22 10:44:19 -0500159}
160
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400161void GrGpuResource::notifyRefCntIsZero() const {
bsalomon12299ab2014-11-14 13:33:09 -0800162 if (this->wasDestroyed()) {
163 // We've already been removed from the cache. Goodbye cruel world!
halcanary385fe4d2015-08-26 13:07:48 -0700164 delete this;
bsalomon3f324322015-04-08 11:01:54 -0700165 return;
bsalomonbcf0a522014-10-08 08:40:09 -0700166 }
bsalomon3f324322015-04-08 11:01:54 -0700167
Brian Salomon8cabb322019-02-22 10:44:19 -0500168 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
bsalomon3f324322015-04-08 11:01:54 -0700169
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400170 get_resource_cache(fGpu)->resourceAccess().notifyRefCntReachedZero(mutableThis);
bsalomonbcf0a522014-10-08 08:40:09 -0700171}
172
bsalomon10e23ca2014-11-25 05:52:06 -0800173void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800174 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800175 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800176 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800177 }
178}
179
bsalomonafe30052015-01-16 07:32:33 -0800180void GrGpuResource::makeBudgeted() {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500181 // We should never make a wrapped resource budgeted.
182 SkASSERT(!fRefsWrappedObjects);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500183 // Only wrapped resources can be in the kUnbudgetedCacheable state.
184 SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
185 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700186 // Currently resources referencing wrapped objects are not budgeted.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500187 fBudgetedType = GrBudgetedType::kBudgeted;
bsalomon0ea80f42015-02-11 10:49:59 -0800188 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800189 }
190}
191
bsalomonc2f35b72015-01-23 07:19:22 -0800192void GrGpuResource::makeUnbudgeted() {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500193 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
bsalomonc6363ef2015-09-24 07:07:40 -0700194 !fUniqueKey.isValid()) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500195 fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
bsalomon0ea80f42015-02-11 10:49:59 -0800196 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800197 }
198}
199
bsalomon6d3fe022014-07-25 08:35:45 -0700200uint32_t GrGpuResource::CreateUniqueID() {
Mike Klein0ec1c572018-12-04 11:52:51 -0500201 static std::atomic<uint32_t> nextID{1};
bsalomonc44be0e2014-07-25 07:32:33 -0700202 uint32_t id;
203 do {
Mike Klein0ec1c572018-12-04 11:52:51 -0500204 id = nextID++;
bsalomonc44be0e2014-07-25 07:32:33 -0700205 } while (id == SK_InvalidUniqueID);
206 return id;
207}
Brian Salomon2c791fc2019-04-02 11:52:03 -0400208
209//////////////////////////////////////////////////////////////////////////////
210
211void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
212 SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
213 cache->resourceAccess().refResource(fResource);
214}