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 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrResourceCache.h" |
| 19 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 20 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/SkGr.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 23 | #include "tools/gpu/ManagedBackendTexture.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 24 | |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 25 | #ifdef SK_DAWN |
| 26 | #include "src/gpu/dawn/GrDawnGpu.h" |
| 27 | #endif |
| 28 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 29 | int GrProxyProvider::numUniqueKeyProxies_TestOnly() const { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 30 | return fUniquelyKeyedProxies.count(); |
| 31 | } |
| 32 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 33 | static constexpr auto kColorType = GrColorType::kRGBA_8888; |
| 34 | static constexpr auto kSize = SkISize::Make(64, 64); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 37 | // Basic test |
| 38 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 39 | static sk_sp<GrTextureProxy> deferred_tex(skiatest::Reporter* reporter, |
| 40 | GrRecordingContext* rContext, |
| 41 | GrProxyProvider* proxyProvider, |
| 42 | SkBackingFit fit) { |
| 43 | const GrCaps* caps = rContext->priv().caps(); |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 44 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 45 | GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 46 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 47 | sk_sp<GrTextureProxy> proxy = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 48 | proxyProvider->createProxy(format, kSize, GrRenderable::kNo, 1, GrMipmapped::kNo, fit, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 49 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 50 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 52 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 55 | static sk_sp<GrTextureProxy> deferred_texRT(skiatest::Reporter* reporter, |
| 56 | GrRecordingContext* rContext, |
| 57 | GrProxyProvider* proxyProvider, |
| 58 | SkBackingFit fit) { |
| 59 | const GrCaps* caps = rContext->priv().caps(); |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 60 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 61 | GrBackendFormat format = caps->getDefaultBackendFormat(kColorType, GrRenderable::kYes); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 62 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 63 | sk_sp<GrTextureProxy> proxy = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 64 | proxyProvider->createProxy(format, kSize, GrRenderable::kYes, 1, GrMipmapped::kNo, fit, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 65 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 66 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 68 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 71 | static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter, GrRecordingContext*, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 72 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 73 | sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 74 | kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 75 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
| 77 | return proxy; |
| 78 | } |
| 79 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 80 | static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter, GrRecordingContext*, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 81 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 82 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
| 83 | static int kUniqueKeyData = 0; |
| 84 | |
| 85 | GrUniqueKey key; |
| 86 | |
| 87 | GrUniqueKey::Builder builder(&key, d, 1, nullptr); |
| 88 | builder[0] = kUniqueKeyData++; |
| 89 | builder.finish(); |
| 90 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 91 | // Only budgeted & wrapped external proxies get to carry uniqueKeys |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 92 | sk_sp<GrTextureProxy> proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 93 | kSize, kColorType, GrRenderable::kNo, 1, fit, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 94 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, proxy->getUniqueKey().isValid()); |
| 96 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 99 | static sk_sp<GrTextureProxy> create_wrapped_backend(GrDirectContext* dContext) { |
| 100 | auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData( |
| 101 | dContext, |
| 102 | kSize.width(), |
| 103 | kSize.height(), |
| 104 | GrColorTypeToSkColorType(kColorType), |
| 105 | GrMipmapped::kNo, |
| 106 | GrRenderable::kNo, |
| 107 | GrProtected::kNo); |
| 108 | if (!mbet) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 111 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 112 | return proxyProvider->wrapBackendTexture(mbet->texture(), |
| 113 | kBorrow_GrWrapOwnership, |
| 114 | GrWrapCacheable::kYes, |
| 115 | kRead_GrIOType, |
| 116 | mbet->refCountedCallback()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 119 | // This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning |
| 120 | // and looking them up work, etc. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 121 | static void basic_test(GrDirectContext* dContext, |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 122 | skiatest::Reporter* reporter, |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 123 | sk_sp<GrTextureProxy> proxy) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 124 | static int id = 1; |
| 125 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 126 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 127 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 128 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 129 | |
| 130 | int startCacheCount = cache->getResourceCount(); |
| 131 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 132 | GrUniqueKey key; |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 133 | if (proxy->getUniqueKey().isValid()) { |
| 134 | key = proxy->getUniqueKey(); |
| 135 | } else { |
| 136 | GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64)); |
| 137 | ++id; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 138 | |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 139 | // 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] | 140 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 141 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 145 | REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount()); |
| 146 | |
| 147 | // setUniqueKey had better stick |
| 148 | REPORTER_ASSERT(reporter, key == proxy->getUniqueKey()); |
| 149 | |
| 150 | // We just added it, surely we can find it |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(key)); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 153 | |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 154 | int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : 1); |
| 155 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 156 | // Once instantiated, the backing resource should have the same key |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 157 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 158 | const GrUniqueKey texKey = proxy->peekSurface()->getUniqueKey(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 159 | REPORTER_ASSERT(reporter, texKey.isValid()); |
| 160 | REPORTER_ASSERT(reporter, key == texKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 161 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 162 | // An Unbudgeted-cacheable resource will not get purged when a proxy with the same key is |
| 163 | // deleted. |
| 164 | bool expectResourceToOutliveProxy = proxy->peekSurface()->resourcePriv().budgetedType() == |
| 165 | GrBudgetedType::kUnbudgetedCacheable; |
| 166 | |
| 167 | // An Unbudgeted-uncacheable resource is never kept alive if it's ref cnt reaches zero even if |
| 168 | // it has a key. |
| 169 | bool expectDeletingProxyToDeleteResource = |
| 170 | proxy->peekSurface()->resourcePriv().budgetedType() == |
| 171 | GrBudgetedType::kUnbudgetedUncacheable; |
| 172 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 173 | // deleting the proxy should delete it from the hash but not the cache |
| 174 | proxy = nullptr; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 175 | if (expectDeletingProxyToDeleteResource) { |
| 176 | expectedCacheCount -= 1; |
| 177 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 179 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 180 | |
| 181 | // 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] | 182 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 183 | REPORTER_ASSERT(reporter, proxy); |
| 184 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
| 185 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 186 | |
| 187 | // Mega-purging it should remove it from both the hash and the cache |
| 188 | proxy = nullptr; |
| 189 | cache->purgeAllUnlocked(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 190 | if (!expectResourceToOutliveProxy) { |
| 191 | expectedCacheCount--; |
| 192 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 194 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 195 | // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should |
| 196 | // be. |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 197 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 199 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 200 | |
| 201 | if (expectResourceToOutliveProxy) { |
| 202 | proxy.reset(); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 203 | GrUniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 204 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg); |
| 205 | cache->purgeAsNeeded(); |
| 206 | expectedCacheCount--; |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 207 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 208 | REPORTER_ASSERT(reporter, !proxy); |
| 209 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
| 210 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 214 | // Invalidation test |
| 215 | |
| 216 | // Test if invalidating unique ids operates as expected for texture proxies. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 217 | static void invalidation_test(GrDirectContext* dContext, skiatest::Reporter* reporter) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 218 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 219 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 220 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 221 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 222 | |
| 223 | sk_sp<SkImage> rasterImg; |
| 224 | |
| 225 | { |
| 226 | SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 227 | |
| 228 | SkBitmap bm; |
| 229 | bm.allocPixels(ii); |
| 230 | |
| 231 | rasterImg = SkImage::MakeFromBitmap(bm); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 232 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 234 | } |
| 235 | |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 236 | // Some of our backends use buffers to do uploads that will live in our resource cache. So we |
| 237 | // need to account for those extra resources here. |
| 238 | int bufferResources = 0; |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 239 | if (dContext->backend() == GrBackendApi::kDawn || |
| 240 | dContext->backend() == GrBackendApi::kVulkan || |
Jim Van Verth | 1e95010 | 2020-07-31 09:47:08 -0400 | [diff] [blame] | 241 | dContext->backend() == GrBackendApi::kDirect3D || |
| 242 | dContext->backend() == GrBackendApi::kMetal) { |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 243 | bufferResources = 1; |
| 244 | } |
| 245 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 246 | sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(dContext); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 247 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, 1 + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 249 | |
| 250 | rasterImg = nullptr; // this invalidates the uniqueKey |
| 251 | |
| 252 | // this forces the cache to respond to the inval msg |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 253 | size_t maxBytes = dContext->getResourceCacheLimit(); |
| 254 | dContext->setResourceCacheLimit(maxBytes-1); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 255 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 256 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, 1 + bufferResources == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 258 | |
| 259 | textureImg = nullptr; |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 260 | |
| 261 | // For backends that use buffers to upload lets make sure that work has been submit and done |
| 262 | // before we try to purge all resources. |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 263 | dContext->submit(true); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 264 | |
| 265 | #ifdef SK_DAWN |
| 266 | // The forced cpu sync in dawn doesn't actually mean the async map will finish thus we may |
| 267 | // still have a ref on the GrGpuBuffer and it will not get purged by the call below. We dig |
| 268 | // 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] | 269 | if (dContext->backend() == GrBackendApi::kDawn) { |
| 270 | GrDawnGpu* gpu = static_cast<GrDawnGpu*>(dContext->priv().getGpu()); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 271 | gpu->waitOnAllBusyStagingBuffers(); |
| 272 | } |
| 273 | #endif |
| 274 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 275 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 276 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 277 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 279 | } |
| 280 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 281 | // Test if invalidating unique ids prior to instantiating operates as expected |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 282 | static void invalidation_and_instantiation_test(GrDirectContext* dContext, |
| 283 | skiatest::Reporter* reporter) { |
| 284 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 285 | GrResourceProvider* resourceProvider = dContext->priv().resourceProvider(); |
| 286 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 287 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 288 | |
| 289 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
| 290 | GrUniqueKey key; |
| 291 | GrUniqueKey::Builder builder(&key, d, 1, nullptr); |
| 292 | builder[0] = 0; |
| 293 | builder.finish(); |
| 294 | |
| 295 | // Create proxy, assign unique key |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 296 | sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, dContext, proxyProvider, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 297 | SkBackingFit::kExact); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 298 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 299 | |
| 300 | // 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] | 301 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 302 | GrUniqueKeyInvalidatedMessage(key, dContext->priv().contextID())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 303 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 306 | |
| 307 | // Instantiate the proxy. This will trigger the message to be processed, so the resulting |
| 308 | // texture should *not* have the unique key on it! |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 309 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 310 | |
| 311 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 314 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 315 | |
| 316 | proxy = nullptr; |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 317 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 318 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 319 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 320 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 321 | } |
| 322 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 323 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) { |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 324 | auto direct = ctxInfo.directContext(); |
| 325 | GrProxyProvider* proxyProvider = direct->priv().proxyProvider(); |
| 326 | GrResourceCache* cache = direct->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 327 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 328 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 329 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 330 | |
| 331 | for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 332 | for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 333 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 334 | basic_test(direct, reporter, create(reporter, direct, proxyProvider, fit)); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 338 | cache->purgeAllUnlocked(); |
| 339 | } |
| 340 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 341 | basic_test(direct, reporter, create_wrapped_backend(direct)); |
| 342 | |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 343 | invalidation_test(direct, reporter); |
| 344 | invalidation_and_instantiation_test(direct, reporter); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 345 | } |