Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GrGpuResource.cpp
Go to the documentation of this file.
1/*
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.
6 */
8
17
18#include <atomic>
19
21 SkASSERT(gpu);
22 SkASSERT(gpu->getContext());
24 return gpu->getContext()->priv().getResourceCache();
25}
26
27GrGpuResource::GrGpuResource(GrGpu* gpu, std::string_view label)
28 : fGpu(gpu), fUniqueID(CreateUniqueID()), fLabel(label) {
29 SkDEBUGCODE(fCacheArrayIndex = -1);
30}
31
34 fBudgetedType = budgeted == skgpu::Budgeted::kYes ? GrBudgetedType::kBudgeted
36 this->computeScratchKey(&fScratchKey);
37 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
38}
39
42 // Resources referencing wrapped objects are never budgeted. They may be cached or uncached.
45 fRefsWrappedObjects = true;
46 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
47}
48
50 // The cache should have released or destroyed this resource.
51 SkASSERT(this->wasDestroyed());
52}
53
54void GrGpuResource::release() {
55 SkASSERT(fGpu);
56 this->onRelease();
57 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
58 fGpu = nullptr;
59 fGpuMemorySize = 0;
60}
61
62void GrGpuResource::abandon() {
63 if (this->wasDestroyed()) {
64 return;
65 }
66 SkASSERT(fGpu);
67 this->onAbandon();
68 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
69 fGpu = nullptr;
70 fGpuMemorySize = 0;
71}
72
74 if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
75 return;
76 }
77
78 this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(),
79 this->getResourceType(), this->gpuMemorySize());
80}
81
83 const SkString& resourceName,
84 const char* type, size_t size) const {
85 const char* tag = "Scratch";
86 if (fUniqueKey.isValid()) {
87 tag = (fUniqueKey.tag() != nullptr) ? fUniqueKey.tag() : "Other";
88 }
89
90 traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size);
91 traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", type);
92 traceMemoryDump->dumpStringValue(resourceName.c_str(), "label", this->getLabel().c_str());
93 traceMemoryDump->dumpStringValue(resourceName.c_str(), "category", tag);
94 if (this->isPurgeable()) {
95 traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size);
96 }
97 if (traceMemoryDump->shouldDumpWrappedObjects()) {
98 traceMemoryDump->dumpWrappedState(resourceName.c_str(), fRefsWrappedObjects);
99 }
100
101 this->setMemoryBacking(traceMemoryDump, resourceName);
102}
103
104bool GrGpuResource::isPurgeable() const {
105 // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique
106 // key. The key must be removed/invalidated to make them purgeable.
107 return !this->hasRef() &&
108 this->hasNoCommandBufferUsages() &&
109 !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
110}
111
112bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
113
114bool GrGpuResource::hasNoCommandBufferUsages() const {
116}
117
119 // Dump resource as "skia/gpu_resources/resource_#".
120 SkString resourceName("skia/gpu_resources/resource_");
121 resourceName.appendU32(this->uniqueID().asUInt());
122 return resourceName;
123}
124
126 if (fGpu) {
127 return fGpu->getContext();
128 } else {
129 return nullptr;
130 }
131}
132
134 if (fGpu) {
135 return fGpu->getContext();
136 } else {
137 return nullptr;
138 }
139}
140
141void GrGpuResource::removeUniqueKey() {
142 if (this->wasDestroyed()) {
143 return;
144 }
145 SkASSERT(fUniqueKey.isValid());
146 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
147}
148
149void GrGpuResource::setUniqueKey(const skgpu::UniqueKey& key) {
150 SkASSERT(this->internalHasRef());
151 SkASSERT(key.isValid());
152
153 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
154 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
155 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
156 // it will always be released - it is never converted to a scratch resource.
157 if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
158 !this->fRefsWrappedObjects) {
159 return;
160 }
161
162 if (this->wasDestroyed()) {
163 return;
164 }
165
166 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
167}
168
169void GrGpuResource::notifyARefCntIsZero(LastRemovedRef removedRef) const {
170 if (this->wasDestroyed()) {
171 if (this->hasNoCommandBufferUsages() && !this->hasRef()) {
172 // We've already been removed from the cache. Goodbye cruel world!
173 delete this;
174 }
175 return;
176 }
177
178 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
179
180 get_resource_cache(fGpu)->resourceAccess().notifyARefCntReachedZero(mutableThis, removedRef);
181}
182
183void GrGpuResource::removeScratchKey() {
184 if (!this->wasDestroyed() && fScratchKey.isValid()) {
185 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
186 fScratchKey.reset();
187 }
188}
189
190void GrGpuResource::makeBudgeted() {
191 // We should never make a wrapped resource budgeted.
192 SkASSERT(!fRefsWrappedObjects);
193 // Only wrapped resources can be in the kUnbudgetedCacheable state.
195 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
196 // Currently resources referencing wrapped objects are not budgeted.
197 fBudgetedType = GrBudgetedType::kBudgeted;
198 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
199 }
200}
201
202void GrGpuResource::makeUnbudgeted() {
203 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
204 !fUniqueKey.isValid()) {
206 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
207 }
208}
209
211 static std::atomic<uint32_t> nextID{1};
212 uint32_t id;
213 do {
214 id = nextID.fetch_add(1, std::memory_order_relaxed);
215 } while (id == SK_InvalidUniqueID);
216 return id;
217}
218
219//////////////////////////////////////////////////////////////////////////////
220
221void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
222 SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
223 cache->resourceAccess().refResource(fResource);
224}
static GrResourceCache * get_resource_cache(GrGpu *gpu)
GrWrapCacheable
Definition: GrTypesPriv.h:85
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
static constexpr uint32_t SK_InvalidUniqueID
Definition: SkTypes.h:196
GLenum type
GrResourceCache * getResourceCache()
GrDirectContextPriv priv()
virtual void computeScratchKey(skgpu::ScratchKey *) const
virtual void onAbandon()
std::string getLabel() const
size_t gpuMemorySize() const
virtual ~GrGpuResource()
void dumpMemoryStatisticsPriv(SkTraceMemoryDump *traceMemoryDump, const SkString &resourceName, const char *type, size_t size) const
const GrDirectContext * getContext() const
UniqueID uniqueID() const
virtual void dumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump) const
bool wasDestroyed() const
SkString getResourceName() const
GrGpuResource(GrGpu *, std::string_view label)
virtual const char * getResourceType() const =0
void registerWithCacheWrapped(GrWrapCacheable)
virtual void setMemoryBacking(SkTraceMemoryDump *, const SkString &) const
static uint32_t CreateUniqueID()
void registerWithCache(skgpu::Budgeted)
virtual void onRelease()
ResourcePriv resourcePriv()
Definition: GrGpu.h:62
GrDirectContext * getContext()
Definition: GrGpu.h:67
bool internalHasNoCommandBufferUsages() const
Definition: GrGpuResource.h:89
bool internalHasRef() const
Definition: GrGpuResource.h:88
ResourceAccess resourceAccess()
void appendU32(uint32_t value)
Definition: SkString.h:210
const char * c_str() const
Definition: SkString.h:133
virtual void dumpWrappedState(const char *, bool)
virtual void dumpStringValue(const char *, const char *, const char *)
virtual bool shouldDumpWrappedObjects() const
virtual void dumpNumericValue(const char *dumpName, const char *valueName, const char *units, uint64_t value)=0
bool isValid() const
Definition: ResourceKey.h:55
const char * tag() const
Definition: ResourceKey.h:266
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Budgeted
Definition: GpuTypes.h:35
const uintptr_t id