blob: 1fc3734645a0e98373b3c1670b9bc3c829ffe7ed [file] [log] [blame]
Robert Phillipsae7d3f32017-09-21 08:26:08 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040011
Mike Reedac9f0c92020-12-23 10:11:33 -050012#include "include/core/SkBitmap.h"
Brian Salomon72050802020-10-12 20:45:06 -040013#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040015#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrProxyProvider.h"
Robert Phillipse94b4e12020-07-23 13:54:35 -040018#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrResourceCache.h"
20#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Brian Salomon72050802020-10-12 20:45:06 -040024#include "tools/gpu/ManagedBackendTexture.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040025
Greg Daniela58db7f2020-07-15 09:17:59 -040026#ifdef SK_DAWN
27#include "src/gpu/dawn/GrDawnGpu.h"
28#endif
29
Robert Phillips1afd4cd2018-01-08 13:40:32 -050030int GrProxyProvider::numUniqueKeyProxies_TestOnly() const {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040031 return fUniquelyKeyedProxies.count();
32}
33
Brian Salomon4eb38b72019-08-05 12:58:39 -040034static constexpr auto kColorType = GrColorType::kRGBA_8888;
35static constexpr auto kSize = SkISize::Make(64, 64);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040036
37///////////////////////////////////////////////////////////////////////////////////////////////////
38// Basic test
39
Robert Phillipse94b4e12020-07-23 13:54:35 -040040static sk_sp<GrTextureProxy> deferred_tex(skiatest::Reporter* reporter,
41 GrRecordingContext* rContext,
42 GrProxyProvider* proxyProvider,
43 SkBackingFit fit) {
44 const GrCaps* caps = rContext->priv().caps();
Robert Phillips0a15cc62019-07-30 12:49:10 -040045
Brian Salomon4eb38b72019-08-05 12:58:39 -040046 GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040047
Greg Daniel3a365112020-02-14 10:47:18 -050048 sk_sp<GrTextureProxy> proxy =
Brian Salomon7e67dca2020-07-21 09:27:25 -040049 proxyProvider->createProxy(format, kSize, GrRenderable::kNo, 1, GrMipmapped::kNo, fit,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040050 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040051 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040052 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
53 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040054}
55
Robert Phillipse94b4e12020-07-23 13:54:35 -040056static sk_sp<GrTextureProxy> deferred_texRT(skiatest::Reporter* reporter,
57 GrRecordingContext* rContext,
58 GrProxyProvider* proxyProvider,
59 SkBackingFit fit) {
60 const GrCaps* caps = rContext->priv().caps();
Robert Phillips0a15cc62019-07-30 12:49:10 -040061
Brian Salomon4eb38b72019-08-05 12:58:39 -040062 GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kYes);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040063
Greg Daniel3a365112020-02-14 10:47:18 -050064 sk_sp<GrTextureProxy> proxy =
Brian Salomon7e67dca2020-07-21 09:27:25 -040065 proxyProvider->createProxy(format, kSize, GrRenderable::kYes, 1, GrMipmapped::kNo, fit,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040066 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040067 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040068 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
69 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040070}
71
Robert Phillipse94b4e12020-07-23 13:54:35 -040072static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter, GrRecordingContext*,
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Chris Daltond004e0b2018-09-27 09:28:03 -060074 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Greg Daniel3a365112020-02-14 10:47:18 -050075 kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050076 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040077 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
78 return proxy;
79}
80
Robert Phillipse94b4e12020-07-23 13:54:35 -040081static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter, GrRecordingContext*,
Robert Phillipsadbe1322018-01-17 13:35:46 -050082 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Greg Danielcd871402017-09-26 12:49:26 -040083 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
84 static int kUniqueKeyData = 0;
85
86 GrUniqueKey key;
87
88 GrUniqueKey::Builder builder(&key, d, 1, nullptr);
89 builder[0] = kUniqueKeyData++;
90 builder.finish();
91
Robert Phillipsadbe1322018-01-17 13:35:46 -050092 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Chris Daltond004e0b2018-09-27 09:28:03 -060093 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Greg Daniel3a365112020-02-14 10:47:18 -050094 kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsadbe1322018-01-17 13:35:46 -050095 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Greg Danielcd871402017-09-26 12:49:26 -040096 REPORTER_ASSERT(reporter, proxy->getUniqueKey().isValid());
97 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040098}
99
Brian Salomon72050802020-10-12 20:45:06 -0400100static sk_sp<GrTextureProxy> create_wrapped_backend(GrDirectContext* dContext) {
101 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
102 dContext,
103 kSize.width(),
104 kSize.height(),
105 GrColorTypeToSkColorType(kColorType),
106 GrMipmapped::kNo,
107 GrRenderable::kNo,
108 GrProtected::kNo);
109 if (!mbet) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400110 return nullptr;
111 }
Brian Salomon72050802020-10-12 20:45:06 -0400112 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
113 return proxyProvider->wrapBackendTexture(mbet->texture(),
114 kBorrow_GrWrapOwnership,
115 GrWrapCacheable::kYes,
116 kRead_GrIOType,
117 mbet->refCountedCallback());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400118}
119
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400120// This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning
121// and looking them up work, etc.
Robert Phillipse94b4e12020-07-23 13:54:35 -0400122static void basic_test(GrDirectContext* dContext,
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400123 skiatest::Reporter* reporter,
Greg Daniele895ab22021-03-12 16:29:40 -0500124 sk_sp<GrTextureProxy> proxy,
125 int cacheEntriesPerProxy) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400126 static int id = 1;
127
Robert Phillipse94b4e12020-07-23 13:54:35 -0400128 GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
129 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
130 GrResourceCache* cache = dContext->priv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400131
132 int startCacheCount = cache->getResourceCount();
133
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400134 GrUniqueKey key;
Greg Danielcd871402017-09-26 12:49:26 -0400135 if (proxy->getUniqueKey().isValid()) {
136 key = proxy->getUniqueKey();
137 } else {
138 GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64));
139 ++id;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400140
Greg Danielcd871402017-09-26 12:49:26 -0400141 // Assigning the uniqueKey adds the proxy to the hash but doesn't force instantiation
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500142 REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsadbe1322018-01-17 13:35:46 -0500143 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Greg Danielcd871402017-09-26 12:49:26 -0400144 }
145
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400147 REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount());
148
149 // setUniqueKey had better stick
150 REPORTER_ASSERT(reporter, key == proxy->getUniqueKey());
151
152 // We just added it, surely we can find it
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400153 REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500154 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400155
Greg Daniel00d6cf42021-03-05 22:50:08 +0000156 int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : cacheEntriesPerProxy);
Greg Daniel303e83e2018-09-10 14:10:19 -0400157
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400158 // Once instantiated, the backing resource should have the same key
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500159 SkAssertResult(proxy->instantiate(resourceProvider));
Brian Salomon9bc76d92019-01-24 12:18:33 -0500160 const GrUniqueKey texKey = proxy->peekSurface()->getUniqueKey();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400161 REPORTER_ASSERT(reporter, texKey.isValid());
162 REPORTER_ASSERT(reporter, key == texKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400163
Brian Salomon9bc76d92019-01-24 12:18:33 -0500164 // An Unbudgeted-cacheable resource will not get purged when a proxy with the same key is
165 // deleted.
166 bool expectResourceToOutliveProxy = proxy->peekSurface()->resourcePriv().budgetedType() ==
167 GrBudgetedType::kUnbudgetedCacheable;
168
169 // An Unbudgeted-uncacheable resource is never kept alive if it's ref cnt reaches zero even if
170 // it has a key.
171 bool expectDeletingProxyToDeleteResource =
172 proxy->peekSurface()->resourcePriv().budgetedType() ==
173 GrBudgetedType::kUnbudgetedUncacheable;
174
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400175 // deleting the proxy should delete it from the hash but not the cache
176 proxy = nullptr;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500177 if (expectDeletingProxyToDeleteResource) {
Greg Daniel00d6cf42021-03-05 22:50:08 +0000178 expectedCacheCount -= cacheEntriesPerProxy;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500179 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500180 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Greg Daniel303e83e2018-09-10 14:10:19 -0400181 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400182
183 // If the proxy was cached refinding it should bring it back to life
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400184 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
Greg Daniel303e83e2018-09-10 14:10:19 -0400185 REPORTER_ASSERT(reporter, proxy);
186 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
187 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400188
189 // Mega-purging it should remove it from both the hash and the cache
190 proxy = nullptr;
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400191 cache->purgeUnlockedResources();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500192 if (!expectResourceToOutliveProxy) {
Greg Daniel00d6cf42021-03-05 22:50:08 +0000193 expectedCacheCount -= cacheEntriesPerProxy;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500194 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400195 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400196
Brian Salomon9bc76d92019-01-24 12:18:33 -0500197 // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should
198 // be.
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400199 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500200 REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy);
Greg Daniel303e83e2018-09-10 14:10:19 -0400201 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500202
203 if (expectResourceToOutliveProxy) {
204 proxy.reset();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400205 GrUniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID());
Robert Phillipse7a959d2021-03-11 14:44:42 -0500206 SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500207 cache->purgeAsNeeded();
Greg Daniel00d6cf42021-03-05 22:50:08 +0000208 expectedCacheCount -= cacheEntriesPerProxy;
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400209 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500210 REPORTER_ASSERT(reporter, !proxy);
211 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
212 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400213}
214
215///////////////////////////////////////////////////////////////////////////////////////////////////
216// Invalidation test
217
218// Test if invalidating unique ids operates as expected for texture proxies.
Greg Daniele895ab22021-03-12 16:29:40 -0500219static void invalidation_test(GrDirectContext* dContext,
220 skiatest::Reporter* reporter,
221 int cacheEntriesPerProxy) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400222
Robert Phillipse94b4e12020-07-23 13:54:35 -0400223 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
224 GrResourceCache* cache = dContext->priv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400225 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
226
227 sk_sp<SkImage> rasterImg;
228
229 {
230 SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
231
232 SkBitmap bm;
233 bm.allocPixels(ii);
234
Mike Reeddc607e32020-12-23 11:50:36 -0500235 rasterImg = bm.asImage();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500236 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400237 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
238 }
239
Greg Daniela58db7f2020-07-15 09:17:59 -0400240 // Some of our backends use buffers to do uploads that will live in our resource cache. So we
241 // need to account for those extra resources here.
242 int bufferResources = 0;
Robert Phillipse94b4e12020-07-23 13:54:35 -0400243 if (dContext->backend() == GrBackendApi::kDawn ||
244 dContext->backend() == GrBackendApi::kVulkan ||
Jim Van Verth1e950102020-07-31 09:47:08 -0400245 dContext->backend() == GrBackendApi::kDirect3D ||
246 dContext->backend() == GrBackendApi::kMetal) {
Greg Daniela58db7f2020-07-15 09:17:59 -0400247 bufferResources = 1;
248 }
249
Robert Phillipse94b4e12020-07-23 13:54:35 -0400250 sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(dContext);
Brian Salomonbc074a62020-03-18 10:06:13 -0400251 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Greg Daniele895ab22021-03-12 16:29:40 -0500252 REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400253
254 rasterImg = nullptr; // this invalidates the uniqueKey
255
256 // this forces the cache to respond to the inval msg
Robert Phillipse94b4e12020-07-23 13:54:35 -0400257 size_t maxBytes = dContext->getResourceCacheLimit();
258 dContext->setResourceCacheLimit(maxBytes-1);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400259
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500260 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Greg Daniele895ab22021-03-12 16:29:40 -0500261 REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400262
263 textureImg = nullptr;
Greg Daniela58db7f2020-07-15 09:17:59 -0400264
265 // For backends that use buffers to upload lets make sure that work has been submit and done
266 // before we try to purge all resources.
Robert Phillipse94b4e12020-07-23 13:54:35 -0400267 dContext->submit(true);
Greg Daniela58db7f2020-07-15 09:17:59 -0400268
269#ifdef SK_DAWN
270 // The forced cpu sync in dawn doesn't actually mean the async map will finish thus we may
271 // still have a ref on the GrGpuBuffer and it will not get purged by the call below. We dig
272 // deep into the dawn gpu to make sure we wait for the async map to finish.
Robert Phillipse94b4e12020-07-23 13:54:35 -0400273 if (dContext->backend() == GrBackendApi::kDawn) {
274 GrDawnGpu* gpu = static_cast<GrDawnGpu*>(dContext->priv().getGpu());
Greg Daniela58db7f2020-07-15 09:17:59 -0400275 gpu->waitOnAllBusyStagingBuffers();
276 }
277#endif
278
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400279 dContext->priv().getResourceCache()->purgeUnlockedResources();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400280
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500281 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400282 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
283}
284
Brian Osman28c434b2017-09-27 13:11:16 -0400285// Test if invalidating unique ids prior to instantiating operates as expected
Robert Phillipse94b4e12020-07-23 13:54:35 -0400286static void invalidation_and_instantiation_test(GrDirectContext* dContext,
Greg Daniele895ab22021-03-12 16:29:40 -0500287 skiatest::Reporter* reporter,
288 int cacheEntriesPerProxy) {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400289 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
290 GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
291 GrResourceCache* cache = dContext->priv().getResourceCache();
Brian Osman28c434b2017-09-27 13:11:16 -0400292 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
293
294 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
295 GrUniqueKey key;
296 GrUniqueKey::Builder builder(&key, d, 1, nullptr);
297 builder[0] = 0;
298 builder.finish();
299
300 // Create proxy, assign unique key
Robert Phillipse94b4e12020-07-23 13:54:35 -0400301 sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, dContext, proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -0500302 SkBackingFit::kExact);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500303 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Brian Osman28c434b2017-09-27 13:11:16 -0400304
305 // Send an invalidation message, which will be sitting in the cache's inbox
Robert Phillipse7a959d2021-03-11 14:44:42 -0500306 SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
Robert Phillipse94b4e12020-07-23 13:54:35 -0400307 GrUniqueKeyInvalidatedMessage(key, dContext->priv().contextID()));
Brian Osman28c434b2017-09-27 13:11:16 -0400308
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500309 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Brian Osman28c434b2017-09-27 13:11:16 -0400310 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
311
312 // Instantiate the proxy. This will trigger the message to be processed, so the resulting
313 // texture should *not* have the unique key on it!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500314 SkAssertResult(proxy->instantiate(resourceProvider));
Brian Osman28c434b2017-09-27 13:11:16 -0400315
316 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400317 REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500318 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Greg Daniele895ab22021-03-12 16:29:40 -0500319 REPORTER_ASSERT(reporter, cacheEntriesPerProxy == cache->getResourceCount());
Brian Osman28c434b2017-09-27 13:11:16 -0400320
321 proxy = nullptr;
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400322 dContext->priv().getResourceCache()->purgeUnlockedResources();
Brian Osman28c434b2017-09-27 13:11:16 -0400323
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500324 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Brian Osman28c434b2017-09-27 13:11:16 -0400325 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
326}
327
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400328DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) {
Adlai Holler4caa9352020-07-16 10:58:58 -0400329 auto direct = ctxInfo.directContext();
330 GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
331 GrResourceCache* cache = direct->priv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400332
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500333 REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400334 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
335
Greg Daniele895ab22021-03-12 16:29:40 -0500336 // As we transition to using attachments instead of GrTextures and GrRenderTargets individual
337 // proxy instansiations may add multiple things to the cache. There would be an entry for the
338 // GrTexture/GrRenderTarget and entries for one or more attachments.
339 int cacheEntriesPerProxy = 1;
Jim Van Verth05412652021-07-13 10:37:58 -0400340 // We currently only have attachments on the vulkan and metal backends
341 if (direct->backend() == GrBackend::kVulkan || direct->backend() == GrBackend::kMetal) {
Greg Daniele895ab22021-03-12 16:29:40 -0500342 cacheEntriesPerProxy++;
343 // If we ever have a test with multisamples this would have an additional attachment as
344 // well.
345 }
346
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400347 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
Greg Danielcd871402017-09-26 12:49:26 -0400348 for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400349 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
Greg Daniele895ab22021-03-12 16:29:40 -0500350 basic_test(direct, reporter, create(reporter, direct, proxyProvider, fit),
351 cacheEntriesPerProxy);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400352 }
353
354 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400355 cache->purgeUnlockedResources();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400356 }
357
Greg Daniele895ab22021-03-12 16:29:40 -0500358 basic_test(direct, reporter, create_wrapped_backend(direct), cacheEntriesPerProxy);
Brian Salomon72050802020-10-12 20:45:06 -0400359
Greg Daniele895ab22021-03-12 16:29:40 -0500360 invalidation_test(direct, reporter, cacheEntriesPerProxy);
361 invalidation_and_instantiation_test(direct, reporter, cacheEntriesPerProxy);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400362}