Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "tests/Test.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 11 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 12 | #include "include/core/SkBitmap.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrResourceCache.h" |
| 20 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 21 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/SkGr.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 24 | #include "tools/gpu/ManagedBackendTexture.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 25 | |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 26 | #ifdef SK_DAWN |
| 27 | #include "src/gpu/dawn/GrDawnGpu.h" |
| 28 | #endif |
| 29 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 30 | int GrProxyProvider::numUniqueKeyProxies_TestOnly() const { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 31 | return fUniquelyKeyedProxies.count(); |
| 32 | } |
| 33 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 34 | static constexpr auto kColorType = GrColorType::kRGBA_8888; |
| 35 | static constexpr auto kSize = SkISize::Make(64, 64); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 38 | // Basic test |
| 39 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 40 | static 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 Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 45 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 46 | GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 47 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 48 | sk_sp<GrTextureProxy> proxy = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 49 | proxyProvider->createProxy(format, kSize, GrRenderable::kNo, 1, GrMipmapped::kNo, fit, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 50 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 51 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 53 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 56 | static 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 Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 61 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 62 | GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kYes); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 63 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 64 | sk_sp<GrTextureProxy> proxy = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 65 | proxyProvider->createProxy(format, kSize, GrRenderable::kYes, 1, GrMipmapped::kNo, fit, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 66 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 67 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 69 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 72 | static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter, GrRecordingContext*, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 73 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 74 | sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 75 | kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 76 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 78 | return proxy; |
| 79 | } |
| 80 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 81 | static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter, GrRecordingContext*, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 82 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 83 | 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 Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 92 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 93 | sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 94 | kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 95 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, proxy->getUniqueKey().isValid()); |
| 97 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 100 | static 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 Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 110 | return nullptr; |
| 111 | } |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 112 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 113 | return proxyProvider->wrapBackendTexture(mbet->texture(), |
| 114 | kBorrow_GrWrapOwnership, |
| 115 | GrWrapCacheable::kYes, |
| 116 | kRead_GrIOType, |
| 117 | mbet->refCountedCallback()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 120 | // This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning |
| 121 | // and looking them up work, etc. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 122 | static void basic_test(GrDirectContext* dContext, |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 123 | skiatest::Reporter* reporter, |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 124 | sk_sp<GrTextureProxy> proxy, |
| 125 | int cacheEntriesPerProxy) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 126 | static int id = 1; |
| 127 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 128 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 129 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 130 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 131 | |
| 132 | int startCacheCount = cache->getResourceCount(); |
| 133 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 134 | GrUniqueKey key; |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 135 | if (proxy->getUniqueKey().isValid()) { |
| 136 | key = proxy->getUniqueKey(); |
| 137 | } else { |
| 138 | GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64)); |
| 139 | ++id; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 140 | |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 141 | // Assigning the uniqueKey adds the proxy to the hash but doesn't force instantiation |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 142 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 143 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 146 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 147 | 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 Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(key)); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 155 | |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 156 | int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : cacheEntriesPerProxy); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 157 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 158 | // Once instantiated, the backing resource should have the same key |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 159 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 160 | const GrUniqueKey texKey = proxy->peekSurface()->getUniqueKey(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 161 | REPORTER_ASSERT(reporter, texKey.isValid()); |
| 162 | REPORTER_ASSERT(reporter, key == texKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 163 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 164 | // 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 Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 175 | // deleting the proxy should delete it from the hash but not the cache |
| 176 | proxy = nullptr; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 177 | if (expectDeletingProxyToDeleteResource) { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 178 | expectedCacheCount -= cacheEntriesPerProxy; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 179 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 180 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 181 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 182 | |
| 183 | // If the proxy was cached refinding it should bring it back to life |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 184 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, proxy); |
| 186 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
| 187 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 188 | |
| 189 | // Mega-purging it should remove it from both the hash and the cache |
| 190 | proxy = nullptr; |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 191 | cache->purgeUnlockedResources(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 192 | if (!expectResourceToOutliveProxy) { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 193 | expectedCacheCount -= cacheEntriesPerProxy; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 194 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 196 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 197 | // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should |
| 198 | // be. |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 199 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 200 | REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 201 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 202 | |
| 203 | if (expectResourceToOutliveProxy) { |
| 204 | proxy.reset(); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 205 | GrUniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID()); |
Robert Phillips | e7a959d | 2021-03-11 14:44:42 -0500 | [diff] [blame] | 206 | SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 207 | cache->purgeAsNeeded(); |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 208 | expectedCacheCount -= cacheEntriesPerProxy; |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 209 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, !proxy); |
| 211 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
| 212 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 216 | // Invalidation test |
| 217 | |
| 218 | // Test if invalidating unique ids operates as expected for texture proxies. |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 219 | static void invalidation_test(GrDirectContext* dContext, |
| 220 | skiatest::Reporter* reporter, |
| 221 | int cacheEntriesPerProxy) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 222 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 223 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 224 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 225 | 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 Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 235 | rasterImg = bm.asImage(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 236 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 237 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 238 | } |
| 239 | |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 240 | // 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 Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 243 | if (dContext->backend() == GrBackendApi::kDawn || |
| 244 | dContext->backend() == GrBackendApi::kVulkan || |
Jim Van Verth | 1e95010 | 2020-07-31 09:47:08 -0400 | [diff] [blame] | 245 | dContext->backend() == GrBackendApi::kDirect3D || |
| 246 | dContext->backend() == GrBackendApi::kMetal) { |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 247 | bufferResources = 1; |
| 248 | } |
| 249 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 250 | sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(dContext); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 251 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 252 | REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 253 | |
| 254 | rasterImg = nullptr; // this invalidates the uniqueKey |
| 255 | |
| 256 | // this forces the cache to respond to the inval msg |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 257 | size_t maxBytes = dContext->getResourceCacheLimit(); |
| 258 | dContext->setResourceCacheLimit(maxBytes-1); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 259 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 260 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 261 | REPORTER_ASSERT(reporter, cacheEntriesPerProxy + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 262 | |
| 263 | textureImg = nullptr; |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 264 | |
| 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 Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 267 | dContext->submit(true); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 268 | |
| 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 Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 273 | if (dContext->backend() == GrBackendApi::kDawn) { |
| 274 | GrDawnGpu* gpu = static_cast<GrDawnGpu*>(dContext->priv().getGpu()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 275 | gpu->waitOnAllBusyStagingBuffers(); |
| 276 | } |
| 277 | #endif |
| 278 | |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 279 | dContext->priv().getResourceCache()->purgeUnlockedResources(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 280 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 281 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 282 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 283 | } |
| 284 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 285 | // Test if invalidating unique ids prior to instantiating operates as expected |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 286 | static void invalidation_and_instantiation_test(GrDirectContext* dContext, |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 287 | skiatest::Reporter* reporter, |
| 288 | int cacheEntriesPerProxy) { |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 289 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 290 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 291 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 292 | 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 Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 301 | sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, dContext, proxyProvider, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 302 | SkBackingFit::kExact); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 303 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 304 | |
| 305 | // Send an invalidation message, which will be sitting in the cache's inbox |
Robert Phillips | e7a959d | 2021-03-11 14:44:42 -0500 | [diff] [blame] | 306 | SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post( |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 307 | GrUniqueKeyInvalidatedMessage(key, dContext->priv().contextID())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 308 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 309 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 310 | 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 Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 314 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 315 | |
| 316 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 319 | REPORTER_ASSERT(reporter, cacheEntriesPerProxy == cache->getResourceCount()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 320 | |
| 321 | proxy = nullptr; |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 322 | dContext->priv().getResourceCache()->purgeUnlockedResources(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 323 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 324 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 325 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 326 | } |
| 327 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 328 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) { |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 329 | auto direct = ctxInfo.directContext(); |
| 330 | GrProxyProvider* proxyProvider = direct->priv().proxyProvider(); |
| 331 | GrResourceCache* cache = direct->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 332 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 333 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 334 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 335 | |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 336 | // 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 Verth | 0541265 | 2021-07-13 10:37:58 -0400 | [diff] [blame] | 340 | // We currently only have attachments on the vulkan and metal backends |
| 341 | if (direct->backend() == GrBackend::kVulkan || direct->backend() == GrBackend::kMetal) { |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 342 | cacheEntriesPerProxy++; |
| 343 | // If we ever have a test with multisamples this would have an additional attachment as |
| 344 | // well. |
| 345 | } |
| 346 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 347 | for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 348 | for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 349 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 350 | basic_test(direct, reporter, create(reporter, direct, proxyProvider, fit), |
| 351 | cacheEntriesPerProxy); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 355 | cache->purgeUnlockedResources(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 358 | basic_test(direct, reporter, create_wrapped_backend(direct), cacheEntriesPerProxy); |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 359 | |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 360 | invalidation_test(direct, reporter, cacheEntriesPerProxy); |
| 361 | invalidation_and_instantiation_test(direct, reporter, cacheEntriesPerProxy); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 362 | } |