blob: 334863118ddd190b4f96418b5291d1d00ada0fc3 [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
10#include "Test.h"
11
Robert Phillipsae7d3f32017-09-21 08:26:08 -040012#include "GrBackendSurface.h"
13#include "GrContextPriv.h"
14#include "GrResourceCache.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrProxyProvider.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040016#include "GrResourceProvider.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040017#include "GrTexture.h"
18#include "GrTextureProxy.h"
19
20#include "SkGr.h"
21#include "SkImage.h"
22
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023int GrProxyProvider::numUniqueKeyProxies_TestOnly() const {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040024 return fUniquelyKeyedProxies.count();
25}
26
Robert Phillipsfe0253f2018-03-16 16:47:25 -040027static GrSurfaceDesc make_desc(GrSurfaceDescFlags descFlags) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040028 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -040029 desc.fFlags = descFlags;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040030 desc.fWidth = 64;
31 desc.fHeight = 64;
32 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -050033 desc.fSampleCnt = 1;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040034
35 return desc;
36}
37
38///////////////////////////////////////////////////////////////////////////////////////////////////
39// Basic test
40
Greg Danielcd871402017-09-26 12:49:26 -040041static sk_sp<GrTextureProxy> deferred_tex(skiatest::Reporter* reporter,
Robert Phillipsadbe1322018-01-17 13:35:46 -050042 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050043 const GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040044
Brian Salomon2a4f9832018-03-03 22:43:43 -050045 sk_sp<GrTextureProxy> proxy =
46 proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040047 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040048 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
49 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040050}
51
Greg Danielcd871402017-09-26 12:49:26 -040052static sk_sp<GrTextureProxy> deferred_texRT(skiatest::Reporter* reporter,
Robert Phillipsadbe1322018-01-17 13:35:46 -050053 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050054 const GrSurfaceDesc desc = make_desc(kRenderTarget_GrSurfaceFlag);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040055
Brian Salomon2a4f9832018-03-03 22:43:43 -050056 sk_sp<GrTextureProxy> proxy =
57 proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040058 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040059 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
60 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040061}
62
Greg Danielcd871402017-09-26 12:49:26 -040063static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter,
Robert Phillipsadbe1322018-01-17 13:35:46 -050064 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050065 const GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050066
Chris Daltond004e0b2018-09-27 09:28:03 -060067 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -050068 desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050069 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Greg Danielcd871402017-09-26 12:49:26 -040070 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
71 return proxy;
72}
73
74static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter,
Robert Phillipsadbe1322018-01-17 13:35:46 -050075 GrProxyProvider* proxyProvider, SkBackingFit fit) {
Greg Danielcd871402017-09-26 12:49:26 -040076 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
77 static int kUniqueKeyData = 0;
78
79 GrUniqueKey key;
80
81 GrUniqueKey::Builder builder(&key, d, 1, nullptr);
82 builder[0] = kUniqueKeyData++;
83 builder.finish();
84
Robert Phillips0bd24dc2018-01-16 08:06:32 -050085 const GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
Greg Danielcd871402017-09-26 12:49:26 -040086
Robert Phillipsadbe1322018-01-17 13:35:46 -050087 // Only budgeted & wrapped external proxies get to carry uniqueKeys
Chris Daltond004e0b2018-09-27 09:28:03 -060088 sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -040089 desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes);
Robert Phillipsadbe1322018-01-17 13:35:46 -050090 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Greg Danielcd871402017-09-26 12:49:26 -040091 REPORTER_ASSERT(reporter, proxy->getUniqueKey().isValid());
92 return proxy;
Robert Phillipsae7d3f32017-09-21 08:26:08 -040093}
94
95static sk_sp<GrTextureProxy> create_wrapped_backend(GrContext* context, SkBackingFit fit,
96 sk_sp<GrTexture>* backingSurface) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050097 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050098 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsae7d3f32017-09-21 08:26:08 -040099
Robert Phillipsb67821d2017-12-13 15:00:45 -0500100 const GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400101
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500102 *backingSurface = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400103 if (!(*backingSurface)) {
104 return nullptr;
105 }
106
Robert Phillipsb67821d2017-12-13 15:00:45 -0500107 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
Greg Daniel108bb232018-07-03 16:18:29 -0400108 backendTex.setPixelConfig(desc.fConfig);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400109
Brian Salomon7578f3e2018-03-07 14:39:54 -0500110 return proxyProvider->wrapBackendTexture(backendTex, kBottomLeft_GrSurfaceOrigin);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400111}
112
113
114// This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning
115// and looking them up work, etc.
116static void basic_test(GrContext* context,
117 skiatest::Reporter* reporter,
Greg Daniel303e83e2018-09-10 14:10:19 -0400118 sk_sp<GrTextureProxy> proxy) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400119 static int id = 1;
120
Robert Phillips6be756b2018-01-16 15:07:54 -0500121 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500122 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500123 GrResourceCache* cache = context->contextPriv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400124
125 int startCacheCount = cache->getResourceCount();
126
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400127 GrUniqueKey key;
Greg Danielcd871402017-09-26 12:49:26 -0400128 if (proxy->getUniqueKey().isValid()) {
129 key = proxy->getUniqueKey();
130 } else {
131 GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64));
132 ++id;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400133
Greg Danielcd871402017-09-26 12:49:26 -0400134 // Assigning the uniqueKey adds the proxy to the hash but doesn't force instantiation
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500135 REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsadbe1322018-01-17 13:35:46 -0500136 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Greg Danielcd871402017-09-26 12:49:26 -0400137 }
138
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500139 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400140 REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount());
141
142 // setUniqueKey had better stick
143 REPORTER_ASSERT(reporter, key == proxy->getUniqueKey());
144
145 // We just added it, surely we can find it
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146 REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(
147 key, kBottomLeft_GrSurfaceOrigin));
148 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400149
Greg Daniel303e83e2018-09-10 14:10:19 -0400150 int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : 1);
151
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400152 // Once instantiated, the backing resource should have the same key
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500153 SkAssertResult(proxy->instantiate(resourceProvider));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400154 const GrUniqueKey& texKey = proxy->peekSurface()->getUniqueKey();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400155 REPORTER_ASSERT(reporter, texKey.isValid());
156 REPORTER_ASSERT(reporter, key == texKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400157
158 // deleting the proxy should delete it from the hash but not the cache
159 proxy = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500160 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Greg Daniel303e83e2018-09-10 14:10:19 -0400161 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400162
163 // If the proxy was cached refinding it should bring it back to life
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
Greg Daniel303e83e2018-09-10 14:10:19 -0400165 REPORTER_ASSERT(reporter, proxy);
166 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
167 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400168
169 // Mega-purging it should remove it from both the hash and the cache
170 proxy = nullptr;
171 cache->purgeAllUnlocked();
Greg Daniel303e83e2018-09-10 14:10:19 -0400172 expectedCacheCount--;
173 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400174
175 // We can bring neither the texture nor proxy back from perma-death
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500176 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400177 REPORTER_ASSERT(reporter, !proxy);
Greg Daniel303e83e2018-09-10 14:10:19 -0400178 REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400179}
180
181///////////////////////////////////////////////////////////////////////////////////////////////////
182// Invalidation test
183
184// Test if invalidating unique ids operates as expected for texture proxies.
185static void invalidation_test(GrContext* context, skiatest::Reporter* reporter) {
186
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500187 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500188 GrResourceCache* cache = context->contextPriv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400189 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
190
191 sk_sp<SkImage> rasterImg;
192
193 {
194 SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
195
196 SkBitmap bm;
197 bm.allocPixels(ii);
198
199 rasterImg = SkImage::MakeFromBitmap(bm);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400201 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
202 }
203
204 sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(context, nullptr);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500205 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400206 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
207
208 rasterImg = nullptr; // this invalidates the uniqueKey
209
210 // this forces the cache to respond to the inval msg
211 int maxNum;
212 size_t maxBytes;
213 context->getResourceCacheLimits(&maxNum, &maxBytes);
214 context->setResourceCacheLimits(maxNum-1, maxBytes);
215
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500216 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400217 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
218
219 textureImg = nullptr;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500220 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400221
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500222 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
224}
225
Brian Osman28c434b2017-09-27 13:11:16 -0400226// Test if invalidating unique ids prior to instantiating operates as expected
227static void invalidation_and_instantiation_test(GrContext* context, skiatest::Reporter* reporter) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500228 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500229 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
230 GrResourceCache* cache = context->contextPriv().getResourceCache();
Brian Osman28c434b2017-09-27 13:11:16 -0400231 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
232
233 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
234 GrUniqueKey key;
235 GrUniqueKey::Builder builder(&key, d, 1, nullptr);
236 builder[0] = 0;
237 builder.finish();
238
239 // Create proxy, assign unique key
Robert Phillipsadbe1322018-01-17 13:35:46 -0500240 sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, proxyProvider, SkBackingFit::kExact);
241 SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
Brian Osman28c434b2017-09-27 13:11:16 -0400242
243 // Send an invalidation message, which will be sitting in the cache's inbox
Brian Salomon238069b2018-07-11 15:58:57 -0400244 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(
245 GrUniqueKeyInvalidatedMessage(key, context->uniqueID()));
Brian Osman28c434b2017-09-27 13:11:16 -0400246
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
Brian Osman28c434b2017-09-27 13:11:16 -0400248 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
249
250 // Instantiate the proxy. This will trigger the message to be processed, so the resulting
251 // texture should *not* have the unique key on it!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500252 SkAssertResult(proxy->instantiate(resourceProvider));
Brian Osman28c434b2017-09-27 13:11:16 -0400253
254 REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400255 REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500256 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Brian Osman28c434b2017-09-27 13:11:16 -0400257 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
258
259 proxy = nullptr;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500260 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Brian Osman28c434b2017-09-27 13:11:16 -0400261
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500262 REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
Brian Osman28c434b2017-09-27 13:11:16 -0400263 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
264}
265
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400266DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) {
267 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500268 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500269 GrResourceCache* cache = context->contextPriv().getResourceCache();
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400270
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500271 REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400272 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
273
274 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
Greg Danielcd871402017-09-26 12:49:26 -0400275 for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400276 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
Greg Daniel303e83e2018-09-10 14:10:19 -0400277 basic_test(context, reporter, create(reporter, proxyProvider, fit));
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400278 }
279
280 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
281 sk_sp<GrTexture> backingTex;
282 sk_sp<GrTextureProxy> proxy = create_wrapped_backend(context, fit, &backingTex);
Greg Daniel303e83e2018-09-10 14:10:19 -0400283 basic_test(context, reporter, std::move(proxy));
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400284
285 backingTex = nullptr;
286 cache->purgeAllUnlocked();
287 }
288
289 invalidation_test(context, reporter);
Robert Phillipsfa8c0802017-10-04 08:42:28 -0400290 invalidation_and_instantiation_test(context, reporter);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400291}