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 | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 124 | sk_sp<GrTextureProxy> proxy) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 125 | static int id = 1; |
| 126 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 127 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 128 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 129 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 130 | |
| 131 | int startCacheCount = cache->getResourceCount(); |
| 132 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 133 | GrUniqueKey key; |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 134 | if (proxy->getUniqueKey().isValid()) { |
| 135 | key = proxy->getUniqueKey(); |
| 136 | } else { |
| 137 | GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64)); |
| 138 | ++id; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 139 | |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 140 | // 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] | 141 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 142 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 145 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 146 | REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount()); |
| 147 | |
| 148 | // setUniqueKey had better stick |
| 149 | REPORTER_ASSERT(reporter, key == proxy->getUniqueKey()); |
| 150 | |
| 151 | // We just added it, surely we can find it |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(key)); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 154 | |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 155 | int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : 1); |
| 156 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 157 | // Once instantiated, the backing resource should have the same key |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 158 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 159 | const GrUniqueKey texKey = proxy->peekSurface()->getUniqueKey(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, texKey.isValid()); |
| 161 | REPORTER_ASSERT(reporter, key == texKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 162 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 163 | // An Unbudgeted-cacheable resource will not get purged when a proxy with the same key is |
| 164 | // deleted. |
| 165 | bool expectResourceToOutliveProxy = proxy->peekSurface()->resourcePriv().budgetedType() == |
| 166 | GrBudgetedType::kUnbudgetedCacheable; |
| 167 | |
| 168 | // An Unbudgeted-uncacheable resource is never kept alive if it's ref cnt reaches zero even if |
| 169 | // it has a key. |
| 170 | bool expectDeletingProxyToDeleteResource = |
| 171 | proxy->peekSurface()->resourcePriv().budgetedType() == |
| 172 | GrBudgetedType::kUnbudgetedUncacheable; |
| 173 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 174 | // deleting the proxy should delete it from the hash but not the cache |
| 175 | proxy = nullptr; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 176 | if (expectDeletingProxyToDeleteResource) { |
| 177 | expectedCacheCount -= 1; |
| 178 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 179 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 180 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 181 | |
| 182 | // 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] | 183 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 184 | REPORTER_ASSERT(reporter, proxy); |
| 185 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
| 186 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 187 | |
| 188 | // Mega-purging it should remove it from both the hash and the cache |
| 189 | proxy = nullptr; |
| 190 | cache->purgeAllUnlocked(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 191 | if (!expectResourceToOutliveProxy) { |
| 192 | expectedCacheCount--; |
| 193 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 195 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 196 | // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should |
| 197 | // be. |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 198 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 199 | REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 200 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 201 | |
| 202 | if (expectResourceToOutliveProxy) { |
| 203 | proxy.reset(); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 204 | GrUniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 205 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg); |
| 206 | cache->purgeAsNeeded(); |
| 207 | expectedCacheCount--; |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 208 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 209 | REPORTER_ASSERT(reporter, !proxy); |
| 210 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
| 211 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 215 | // Invalidation test |
| 216 | |
| 217 | // Test if invalidating unique ids operates as expected for texture proxies. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 218 | static void invalidation_test(GrDirectContext* dContext, skiatest::Reporter* reporter) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 219 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 220 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 221 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 222 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 223 | |
| 224 | sk_sp<SkImage> rasterImg; |
| 225 | |
| 226 | { |
| 227 | SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 228 | |
| 229 | SkBitmap bm; |
| 230 | bm.allocPixels(ii); |
| 231 | |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 232 | rasterImg = bm.asImage(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 235 | } |
| 236 | |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 237 | // Some of our backends use buffers to do uploads that will live in our resource cache. So we |
| 238 | // need to account for those extra resources here. |
| 239 | int bufferResources = 0; |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 240 | if (dContext->backend() == GrBackendApi::kDawn || |
| 241 | dContext->backend() == GrBackendApi::kVulkan || |
Jim Van Verth | 1e95010 | 2020-07-31 09:47:08 -0400 | [diff] [blame] | 242 | dContext->backend() == GrBackendApi::kDirect3D || |
| 243 | dContext->backend() == GrBackendApi::kMetal) { |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 244 | bufferResources = 1; |
| 245 | } |
| 246 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 247 | sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(dContext); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 249 | REPORTER_ASSERT(reporter, 1 + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 250 | |
| 251 | rasterImg = nullptr; // this invalidates the uniqueKey |
| 252 | |
| 253 | // this forces the cache to respond to the inval msg |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 254 | size_t maxBytes = dContext->getResourceCacheLimit(); |
| 255 | dContext->setResourceCacheLimit(maxBytes-1); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 256 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 258 | REPORTER_ASSERT(reporter, 1 + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 259 | |
| 260 | textureImg = nullptr; |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 261 | |
| 262 | // For backends that use buffers to upload lets make sure that work has been submit and done |
| 263 | // before we try to purge all resources. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 264 | dContext->submit(true); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 265 | |
| 266 | #ifdef SK_DAWN |
| 267 | // The forced cpu sync in dawn doesn't actually mean the async map will finish thus we may |
| 268 | // still have a ref on the GrGpuBuffer and it will not get purged by the call below. We dig |
| 269 | // 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] | 270 | if (dContext->backend() == GrBackendApi::kDawn) { |
| 271 | GrDawnGpu* gpu = static_cast<GrDawnGpu*>(dContext->priv().getGpu()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 272 | gpu->waitOnAllBusyStagingBuffers(); |
| 273 | } |
| 274 | #endif |
| 275 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 276 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 277 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 280 | } |
| 281 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 282 | // Test if invalidating unique ids prior to instantiating operates as expected |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 283 | static void invalidation_and_instantiation_test(GrDirectContext* dContext, |
| 284 | skiatest::Reporter* reporter) { |
| 285 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 286 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 287 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 288 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 289 | |
| 290 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
| 291 | GrUniqueKey key; |
| 292 | GrUniqueKey::Builder builder(&key, d, 1, nullptr); |
| 293 | builder[0] = 0; |
| 294 | builder.finish(); |
| 295 | |
| 296 | // Create proxy, assign unique key |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 297 | sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, dContext, proxyProvider, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 298 | SkBackingFit::kExact); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 299 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 300 | |
| 301 | // Send an invalidation message, which will be sitting in the cache's inbox |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 302 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 303 | GrUniqueKeyInvalidatedMessage(key, dContext->priv().contextID())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 304 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 306 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 307 | |
| 308 | // Instantiate the proxy. This will trigger the message to be processed, so the resulting |
| 309 | // texture should *not* have the unique key on it! |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 310 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 311 | |
| 312 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 314 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 316 | |
| 317 | proxy = nullptr; |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 318 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 319 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 320 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 321 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 322 | } |
| 323 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 324 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) { |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 325 | auto direct = ctxInfo.directContext(); |
| 326 | GrProxyProvider* proxyProvider = direct->priv().proxyProvider(); |
| 327 | GrResourceCache* cache = direct->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 328 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 329 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 330 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 331 | |
| 332 | for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 333 | for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 334 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 335 | basic_test(direct, reporter, create(reporter, direct, proxyProvider, fit)); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 339 | cache->purgeAllUnlocked(); |
| 340 | } |
| 341 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 342 | basic_test(direct, reporter, create_wrapped_backend(direct)); |
| 343 | |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 344 | invalidation_test(direct, reporter); |
| 345 | invalidation_and_instantiation_test(direct, reporter); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 346 | } |