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" |
| 13 | #include "include/gpu/GrTexture.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 | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrTextureProxy.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 19 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkImage.h" |
| 21 | #include "src/gpu/SkGr.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 22 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 23 | int GrProxyProvider::numUniqueKeyProxies_TestOnly() const { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 24 | return fUniquelyKeyedProxies.count(); |
| 25 | } |
| 26 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 27 | static constexpr auto kColorType = GrColorType::kRGBA_8888; |
| 28 | static constexpr auto kSize = SkISize::Make(64, 64); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 29 | static GrSurfaceDesc make_desc() { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 30 | GrSurfaceDesc desc; |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 31 | desc.fWidth = kSize.width(); |
| 32 | desc.fHeight = kSize.height(); |
| 33 | desc.fConfig = GrColorTypeToPixelConfig(kColorType); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 34 | return desc; |
| 35 | } |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 38 | // Basic test |
| 39 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 40 | static sk_sp<GrTextureProxy> deferred_tex(skiatest::Reporter* reporter, GrContext* ctx, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 41 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 42 | const GrCaps* caps = ctx->priv().caps(); |
| 43 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 44 | const GrSurfaceDesc desc = make_desc(); |
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 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 47 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
| 48 | format, desc, GrRenderable::kNo, 1, kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo, fit, |
| 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 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 55 | static sk_sp<GrTextureProxy> deferred_texRT(skiatest::Reporter* reporter, GrContext* ctx, |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 56 | GrProxyProvider* proxyProvider, SkBackingFit fit) { |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 57 | const GrCaps* caps = ctx->priv().caps(); |
| 58 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 59 | const GrSurfaceDesc desc = make_desc(); |
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 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 63 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
| 64 | format, desc, GrRenderable::kYes, 1, kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo, fit, |
| 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 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 71 | static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter, GrContext* ctx, |
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( |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 74 | kSize, kColorType, GrRenderable::kNo, 1, kBottomLeft_GrSurfaceOrigin, fit, |
| 75 | 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 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 81 | static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter, GrContext* ctx, |
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( |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 94 | kSize, kColorType, GrRenderable::kNo, 1, kBottomLeft_GrSurfaceOrigin, fit, |
| 95 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 96 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, proxy->getUniqueKey().isValid()); |
| 98 | return proxy; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static sk_sp<GrTextureProxy> create_wrapped_backend(GrContext* context, SkBackingFit fit, |
| 102 | sk_sp<GrTexture>* backingSurface) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 103 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 104 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 105 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 106 | const GrSurfaceDesc desc = make_desc(); |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 107 | GrBackendFormat format = |
| 108 | proxyProvider->caps()->getDefaultBackendFormat(kColorType, GrRenderable::kYes); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 109 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 110 | *backingSurface = |
| 111 | resourceProvider->createTexture(desc, format, GrRenderable::kNo, 1, GrMipMapped::kNo, |
| 112 | SkBudgeted::kNo, GrProtected::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 113 | if (!(*backingSurface)) { |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 117 | GrBackendTexture backendTex = (*backingSurface)->getBackendTexture(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 118 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 119 | return proxyProvider->wrapBackendTexture(backendTex, GrColorType::kRGBA_8888, |
| 120 | kBottomLeft_GrSurfaceOrigin, kBorrow_GrWrapOwnership, |
| 121 | GrWrapCacheable::kYes, kRead_GrIOType); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | // This tests the basic capabilities of the uniquely keyed texture proxies. Does assigning |
| 126 | // and looking them up work, etc. |
| 127 | static void basic_test(GrContext* context, |
| 128 | skiatest::Reporter* reporter, |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 129 | sk_sp<GrTextureProxy> proxy) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 130 | static int id = 1; |
| 131 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 132 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 133 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 134 | GrResourceCache* cache = context->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 135 | |
| 136 | int startCacheCount = cache->getResourceCount(); |
| 137 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 138 | GrUniqueKey key; |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 139 | if (proxy->getUniqueKey().isValid()) { |
| 140 | key = proxy->getUniqueKey(); |
| 141 | } else { |
| 142 | GrMakeKeyFromImageID(&key, id, SkIRect::MakeWH(64, 64)); |
| 143 | ++id; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 144 | |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 145 | // 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] | 146 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 147 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount()); |
| 152 | |
| 153 | // setUniqueKey had better stick |
| 154 | REPORTER_ASSERT(reporter, key == proxy->getUniqueKey()); |
| 155 | |
| 156 | // We just added it, surely we can find it |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey( |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 158 | key, kColorType, kBottomLeft_GrSurfaceOrigin)); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 159 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 160 | |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 161 | int expectedCacheCount = startCacheCount + (proxy->isInstantiated() ? 0 : 1); |
| 162 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 163 | // Once instantiated, the backing resource should have the same key |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 164 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 165 | const GrUniqueKey texKey = proxy->peekSurface()->getUniqueKey(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, texKey.isValid()); |
| 167 | REPORTER_ASSERT(reporter, key == texKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 168 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 169 | // An Unbudgeted-cacheable resource will not get purged when a proxy with the same key is |
| 170 | // deleted. |
| 171 | bool expectResourceToOutliveProxy = proxy->peekSurface()->resourcePriv().budgetedType() == |
| 172 | GrBudgetedType::kUnbudgetedCacheable; |
| 173 | |
| 174 | // An Unbudgeted-uncacheable resource is never kept alive if it's ref cnt reaches zero even if |
| 175 | // it has a key. |
| 176 | bool expectDeletingProxyToDeleteResource = |
| 177 | proxy->peekSurface()->resourcePriv().budgetedType() == |
| 178 | GrBudgetedType::kUnbudgetedUncacheable; |
| 179 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 180 | // deleting the proxy should delete it from the hash but not the cache |
| 181 | proxy = nullptr; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 182 | if (expectDeletingProxyToDeleteResource) { |
| 183 | expectedCacheCount -= 1; |
| 184 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 186 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 187 | |
| 188 | // If the proxy was cached refinding it should bring it back to life |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 189 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kColorType, |
| 190 | kBottomLeft_GrSurfaceOrigin); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, proxy); |
| 192 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
| 193 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 194 | |
| 195 | // Mega-purging it should remove it from both the hash and the cache |
| 196 | proxy = nullptr; |
| 197 | cache->purgeAllUnlocked(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 198 | if (!expectResourceToOutliveProxy) { |
| 199 | expectedCacheCount--; |
| 200 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 201 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 202 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 203 | // If the texture was deleted then the proxy should no longer be findable. Otherwise, it should |
| 204 | // be. |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 205 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kColorType, |
| 206 | kBottomLeft_GrSurfaceOrigin); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, expectResourceToOutliveProxy ? (bool)proxy : !proxy); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 208 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 209 | |
| 210 | if (expectResourceToOutliveProxy) { |
| 211 | proxy.reset(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 212 | GrUniqueKeyInvalidatedMessage msg(texKey, context->priv().contextID()); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 213 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg); |
| 214 | cache->purgeAsNeeded(); |
| 215 | expectedCacheCount--; |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 216 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kColorType, |
| 217 | kBottomLeft_GrSurfaceOrigin); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, !proxy); |
| 219 | REPORTER_ASSERT(reporter, expectedCacheCount == cache->getResourceCount()); |
| 220 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 224 | // Invalidation test |
| 225 | |
| 226 | // Test if invalidating unique ids operates as expected for texture proxies. |
| 227 | static void invalidation_test(GrContext* context, skiatest::Reporter* reporter) { |
| 228 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 229 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 230 | GrResourceCache* cache = context->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 231 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 232 | |
| 233 | sk_sp<SkImage> rasterImg; |
| 234 | |
| 235 | { |
| 236 | SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 237 | |
| 238 | SkBitmap bm; |
| 239 | bm.allocPixels(ii); |
| 240 | |
| 241 | rasterImg = SkImage::MakeFromBitmap(bm); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 242 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 243 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 244 | } |
| 245 | |
Brian Osman | d566e2e | 2019-08-14 13:19:04 -0400 | [diff] [blame] | 246 | sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(context); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 247 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 249 | |
| 250 | rasterImg = nullptr; // this invalidates the uniqueKey |
| 251 | |
| 252 | // this forces the cache to respond to the inval msg |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 253 | size_t maxBytes = context->getResourceCacheLimit(); |
| 254 | context->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()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 258 | |
| 259 | textureImg = nullptr; |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 260 | context->priv().testingOnly_purgeAllUnlockedResources(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 261 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 264 | } |
| 265 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 266 | // Test if invalidating unique ids prior to instantiating operates as expected |
| 267 | static void invalidation_and_instantiation_test(GrContext* context, skiatest::Reporter* reporter) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 268 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 269 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 270 | GrResourceCache* cache = context->priv().getResourceCache(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 272 | |
| 273 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
| 274 | GrUniqueKey key; |
| 275 | GrUniqueKey::Builder builder(&key, d, 1, nullptr); |
| 276 | builder[0] = 0; |
| 277 | builder.finish(); |
| 278 | |
| 279 | // Create proxy, assign unique key |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 280 | sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, context, proxyProvider, |
| 281 | SkBackingFit::kExact); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 282 | SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 283 | |
| 284 | // 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] | 285 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 286 | GrUniqueKeyInvalidatedMessage(key, context->priv().contextID())); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 287 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 288 | REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 290 | |
| 291 | // Instantiate the proxy. This will trigger the message to be processed, so the resulting |
| 292 | // texture should *not* have the unique key on it! |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 293 | SkAssertResult(proxy->instantiate(resourceProvider)); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 294 | |
| 295 | REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 296 | REPORTER_ASSERT(reporter, !proxy->peekTexture()->getUniqueKey().isValid()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 298 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 299 | |
| 300 | proxy = nullptr; |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 301 | context->priv().testingOnly_purgeAllUnlockedResources(); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 302 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 303 | REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly()); |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 305 | } |
| 306 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 307 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) { |
| 308 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 309 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 310 | GrResourceCache* cache = context->priv().getResourceCache(); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 311 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 314 | |
| 315 | for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 316 | for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 318 | basic_test(context, reporter, create(reporter, context, proxyProvider, fit)); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 322 | sk_sp<GrTexture> backingTex; |
| 323 | sk_sp<GrTextureProxy> proxy = create_wrapped_backend(context, fit, &backingTex); |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 324 | basic_test(context, reporter, std::move(proxy)); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 325 | |
| 326 | backingTex = nullptr; |
| 327 | cache->purgeAllUnlocked(); |
| 328 | } |
| 329 | |
| 330 | invalidation_test(context, reporter); |
Robert Phillips | fa8c080 | 2017-10-04 08:42:28 -0400 | [diff] [blame] | 331 | invalidation_and_instantiation_test(context, reporter); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 332 | } |