Flutter Engine
The Flutter Engine
TextureProxyTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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 */
7
8// This is a GPU-backend specific test.
9
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
23#include "include/gpu/GrTypes.h"
27#include "src/gpu/ResourceKey.h"
38#include "src/gpu/ganesh/SkGr.h"
40#include "tests/Test.h"
42
43#include <cstddef>
44#include <cstdint>
45#include <initializer_list>
46
48struct GrContextOptions;
49
51 return fUniquelyKeyedProxies.count();
52}
53
54static constexpr auto kColorType = GrColorType::kRGBA_8888;
55static constexpr auto kSize = SkISize::Make(64, 64);
56
57///////////////////////////////////////////////////////////////////////////////////////////////////
58// Basic test
59
61 GrRecordingContext* rContext,
62 GrProxyProvider* proxyProvider,
63 SkBackingFit fit) {
64 using namespace skgpu;
65
66 const GrCaps* caps = rContext->priv().caps();
67
68 Protected isProtected = Protected(caps->supportsProtectedContent());
69
71
72 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
73 kSize,
75 1,
77 fit,
78 Budgeted::kYes,
79 isProtected,
80 /*label=*/{});
81 // Only budgeted & wrapped external proxies get to carry uniqueKeys
83 return proxy;
84}
85
87 GrRecordingContext* rContext,
88 GrProxyProvider* proxyProvider,
89 SkBackingFit fit) {
90 using namespace skgpu;
91
92 const GrCaps* caps = rContext->priv().caps();
93
94 Protected isProtected = Protected(caps->supportsProtectedContent());
95
97
98 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
99 kSize,
101 1,
103 fit,
104 Budgeted::kYes,
105 isProtected,
106 /*label=*/{});
107 // Only budgeted & wrapped external proxies get to carry uniqueKeys
109 return proxy;
110}
111
113 GrProxyProvider* proxyProvider, SkBackingFit fit) {
114 using namespace skgpu;
115
116 Protected isProtected = Protected(rContext->priv().caps()->supportsProtectedContent());
117
118 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
119 kSize, kColorType, GrRenderable::kNo, 1, fit, Budgeted::kYes, isProtected);
120 // Only budgeted & wrapped external proxies get to carry uniqueKeys
122 return proxy;
123}
124
126 GrRecordingContext* rContext,
127 GrProxyProvider* proxyProvider,
128 SkBackingFit fit) {
129 using namespace skgpu;
130
131 Protected isProtected = Protected(rContext->priv().caps()->supportsProtectedContent());
132
133 static UniqueKey::Domain d = UniqueKey::GenerateDomain();
134 static int kUniqueKeyData = 0;
135
137
138 UniqueKey::Builder builder(&key, d, 1, nullptr);
139 builder[0] = kUniqueKeyData++;
140 builder.finish();
141
142 // Only budgeted & wrapped external proxies get to carry uniqueKeys
143 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
144 kSize, kColorType, GrRenderable::kNo, 1, fit, Budgeted::kYes, isProtected);
145 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
147 return proxy;
148}
149
151 using namespace skgpu;
152
153 Protected isProtected = Protected(dContext->priv().caps()->supportsProtectedContent());
154
155 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
156 dContext,
157 kSize.width(),
158 kSize.height(),
162 isProtected);
163 if (!mbet) {
164 return nullptr;
165 }
166 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
167 return proxyProvider->wrapBackendTexture(mbet->texture(),
171 mbet->refCountedCallback());
172}
173
174// This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning
175// and looking them up work, etc.
176static void basic_test(GrDirectContext* dContext,
179 int cacheEntriesPerProxy) {
180 static int id = 1;
181
182 GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
183 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
185
186 int startCacheCount = cache->getResourceCount();
187
189 if (proxy->getUniqueKey().isValid()) {
190 key = proxy->getUniqueKey();
191 } else {
193 ++id;
194
195 // Assigning the uniqueKey adds the proxy to the hash but doesn't force instantiation
197 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
198 }
199
201 REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount());
202
203 // setUniqueKey had better stick
205
206 // We just added it, surely we can find it
209
210 int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : cacheEntriesPerProxy);
211
212 // Once instantiated, the backing resource should have the same key
213 SkAssertResult(proxy->instantiate(resourceProvider));
214 const skgpu::UniqueKey texKey = proxy->peekSurface()->getUniqueKey();
216 REPORTER_ASSERT(reporter, key == texKey);
217
218 // An Unbudgeted-cacheable resource will not get purged when a proxy with the same key is
219 // deleted.
220 bool expectResourceToOutliveProxy = proxy->peekSurface()->resourcePriv().budgetedType() ==
222
223 // An Unbudgeted-uncacheable resource is never kept alive if it's ref cnt reaches zero even if
224 // it has a key.
225 bool expectDeletingProxyToDeleteResource =
226 proxy->peekSurface()->resourcePriv().budgetedType() ==
228
229 // deleting the proxy should delete it from the hash but not the cache
230 proxy = nullptr;
231 if (expectDeletingProxyToDeleteResource) {
232 expectedCacheCount -= cacheEntriesPerProxy;
233 }
235 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
236
237 // If the proxy was cached refinding it should bring it back to life
238 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
241 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
242
243 // Mega-purging it should remove it from both the hash and the cache
244 proxy = nullptr;
245 cache->purgeUnlockedResources(GrPurgeResourceOptions::kAllResources);
246 if (!expectResourceToOutliveProxy) {
247 expectedCacheCount -= cacheEntriesPerProxy;
248 }
249 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
250
251 // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should
252 // be.
253 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
254 REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy);
255 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
256
257 if (expectResourceToOutliveProxy) {
258 proxy.reset();
259 skgpu::UniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID());
261 cache->purgeAsNeeded();
262 expectedCacheCount -= cacheEntriesPerProxy;
263 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
264 REPORTER_ASSERT(reporter, !proxy);
265 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
266 }
267}
268
269///////////////////////////////////////////////////////////////////////////////////////////////////
270// Invalidation test
271
272// Test if invalidating unique ids operates as expected for texture proxies.
273static void invalidation_test(GrDirectContext* dContext,
275 int cacheEntriesPerProxy) {
276
277 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
279 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
280
281 sk_sp<SkImage> rasterImg;
282
283 {
285
286 SkBitmap bm;
287 bm.allocPixels(ii);
288
289 rasterImg = bm.asImage();
291 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
292 }
293
294 // Some of our backends use buffers to do uploads that will live in our resource cache. So we
295 // need to account for those extra resources here.
296 int bufferResources = 0;
297 if (dContext->backend() == GrBackendApi::kVulkan ||
298 dContext->backend() == GrBackendApi::kDirect3D ||
299 dContext->backend() == GrBackendApi::kMetal) {
300 bufferResources = 1;
301 }
302
303 sk_sp<SkImage> textureImg = SkImages::TextureFromImage(dContext, rasterImg);
305 REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount());
306
307 rasterImg = nullptr; // this invalidates the uniqueKey
308
309 // this forces the cache to respond to the inval msg
310 size_t maxBytes = dContext->getResourceCacheLimit();
311 dContext->setResourceCacheLimit(maxBytes-1);
312
314 REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount());
315
316 textureImg = nullptr;
317
318 // For backends that use buffers to upload lets make sure that work has been submit and done
319 // before we try to purge all resources.
320 dContext->submit(GrSyncCpu::kYes);
323
325 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
326}
327
328// Test if invalidating unique ids prior to instantiating operates as expected
331 int cacheEntriesPerProxy) {
332 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
333 GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
335 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
336
339 skgpu::UniqueKey::Builder builder(&key, d, 1, nullptr);
340 builder[0] = 0;
341 builder.finish();
342
343 // Create proxy, assign unique key
344 sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, dContext, proxyProvider,
346 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
347
348 // Send an invalidation message, which will be sitting in the cache's inbox
351
353 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
354
355 // Instantiate the proxy. This will trigger the message to be processed, so the resulting
356 // texture should *not* have the unique key on it!
357 SkAssertResult(proxy->instantiate(resourceProvider));
358
362 REPORTER_ASSERT(reporter, cacheEntriesPerProxy == cache->getResourceCount());
363
364 proxy = nullptr;
367
369 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
370}
371
373 reporter,
374 ctxInfo,
376 auto direct = ctxInfo.directContext();
377 GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
378 GrResourceCache* cache = direct->priv().getResourceCache();
379
381 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
382
383 // As we transition to using attachments instead of GrTextures and GrRenderTargets individual
384 // proxy instansiations may add multiple things to the cache. There would be an entry for the
385 // GrTexture/GrRenderTarget and entries for one or more attachments.
386 int cacheEntriesPerProxy = 1;
387 // We currently only have attachments on the vulkan and metal backends
388 if (direct->backend() == GrBackend::kVulkan || direct->backend() == GrBackend::kMetal) {
389 cacheEntriesPerProxy++;
390 // If we ever have a test with multisamples this would have an additional attachment as
391 // well.
392 }
393
394 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
396 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
397 basic_test(direct, reporter, create(reporter, direct, proxyProvider, fit),
398 cacheEntriesPerProxy);
399 }
400
401 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
402 cache->purgeUnlockedResources(GrPurgeResourceOptions::kAllResources);
403 }
404
405 basic_test(direct, reporter, create_wrapped_backend(direct), cacheEntriesPerProxy);
406
407 invalidation_test(direct, reporter, cacheEntriesPerProxy);
408 invalidation_and_instantiation_test(direct, reporter, cacheEntriesPerProxy);
409}
reporter
Definition: FontMgrTest.cpp:39
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
@ kRead_GrIOType
Definition: GrTypesPriv.h:403
@ kBorrow_GrWrapOwnership
Definition: GrTypesPriv.h:79
static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct)
Definition: GrTypesPriv.h:589
skgpu::Protected Protected
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
SkBackingFit
Definition: SkBackingFit.h:16
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
void GrMakeKeyFromImageID(skgpu::UniqueKey *key, uint32_t imageID, const SkIRect &imageBounds)
Definition: SkGr.cpp:60
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static sk_sp< GrTextureProxy > wrapped_with_key(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
static sk_sp< GrTextureProxy > create_wrapped_backend(GrDirectContext *dContext)
static void invalidation_and_instantiation_test(GrDirectContext *dContext, skiatest::Reporter *reporter, int cacheEntriesPerProxy)
static void basic_test(GrDirectContext *dContext, skiatest::Reporter *reporter, sk_sp< GrTextureProxy > proxy, int cacheEntriesPerProxy)
static constexpr auto kColorType
static constexpr auto kSize
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
static sk_sp< GrTextureProxy > deferred_tex(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
static sk_sp< GrTextureProxy > deferred_texRT(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
static void invalidation_test(GrDirectContext *dContext, skiatest::Reporter *reporter, int cacheEntriesPerProxy)
static sk_sp< GrTextureProxy > wrapped(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
uint32_t contextID() const
const GrCaps * caps() const
Definition: GrCaps.h:57
bool supportsProtectedContent() const
Definition: GrCaps.h:422
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition: GrCaps.cpp:400
SK_API GrBackendApi backend() const
GrResourceProvider * resourceProvider()
GrResourceCache * getResourceCache()
bool submit(GrSyncCpu sync=GrSyncCpu::kNo)
size_t getResourceCacheLimit() const
GrDirectContextPriv priv()
void setResourceCacheLimit(size_t maxResourceBytes)
GrBudgetedType budgetedType() const
const skgpu::UniqueKey & getUniqueKey() const
ResourcePriv resourcePriv()
sk_sp< GrTextureProxy > wrapBackendTexture(const GrBackendTexture &, GrWrapOwnership, GrWrapCacheable, GrIOType, sk_sp< skgpu::RefCntedCallback >=nullptr)
int numUniqueKeyProxies_TestOnly() const
sk_sp< GrTextureProxy > createProxy(const GrBackendFormat &, SkISize dimensions, GrRenderable, int renderTargetSampleCnt, skgpu::Mipmapped, SkBackingFit, skgpu::Budgeted, GrProtected, std::string_view label, GrInternalSurfaceFlags=GrInternalSurfaceFlags::kNone, UseAllocator useAllocator=UseAllocator::kYes)
bool assignUniqueKeyToProxy(const skgpu::UniqueKey &, GrTextureProxy *)
sk_sp< GrTextureProxy > findOrCreateProxyByUniqueKey(const skgpu::UniqueKey &, UseAllocator=UseAllocator::kYes)
GrProxyProvider * proxyProvider()
GrRecordingContextPriv priv()
void purgeUnlockedResources(GrPurgeResourceOptions opts)
GrTexture * peekTexture() const
GrSurface * peekSurface() const
bool isInstantiated() const
const skgpu::UniqueKey & getUniqueKey() const override
bool instantiate(GrResourceProvider *) override
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
static void Post(Message m)
Definition: SkMessageBus.h:130
int count() const
T * get() const
Definition: SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
bool isValid() const
Definition: ResourceKey.h:55
static Domain GenerateDomain()
Definition: ResourceKey.cpp:27
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
uint32_t uint32_t * format
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
Definition: create.py:1
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
Definition: GpuTools.h:21
Protected
Definition: GpuTypes.h:61
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
const uintptr_t id
int_closure create