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