Robert Phillips | 5af44de | 2017-07-18 14:49:38 -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 | |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Brian Osman | a5842bc | 2021-05-11 13:41:46 -0400 | [diff] [blame] | 9 | #include "include/core/SkSpan.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 10 | #include "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrGpu.h" |
| 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrResourceAllocator.h" |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrResourceProviderPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrSurfaceProxyPriv.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 | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 20 | #include "tests/Test.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 21 | #include "tools/gpu/ManagedBackendTexture.h" |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 22 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 23 | namespace { |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 24 | struct ProxyParams { |
| 25 | int fSize; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 26 | GrRenderable fRenderable; |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 27 | GrColorType fColorType; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 28 | SkBackingFit fFit; |
| 29 | int fSampleCnt; |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 30 | SkBudgeted fBudgeted; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 31 | enum Kind { |
| 32 | kDeferred, |
| 33 | kBackend, |
| 34 | kFullyLazy, |
| 35 | kLazy, |
| 36 | kInstantiated |
| 37 | }; |
| 38 | Kind fKind; |
| 39 | GrUniqueKey fUniqueKey = GrUniqueKey(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 40 | // TODO: do we care about mipmapping |
| 41 | }; |
| 42 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 43 | constexpr GrRenderable kRT = GrRenderable::kYes; |
| 44 | constexpr GrRenderable kNotRT = GrRenderable::kNo; |
| 45 | |
| 46 | constexpr GrColorType kRGBA = GrColorType::kRGBA_8888; |
| 47 | constexpr GrColorType kAlpha = GrColorType::kAlpha_8; |
| 48 | |
| 49 | constexpr SkBackingFit kE = SkBackingFit::kExact; |
| 50 | constexpr SkBackingFit kA = SkBackingFit::kApprox; |
| 51 | |
| 52 | constexpr SkBudgeted kNotB = SkBudgeted::kNo; |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 53 | constexpr SkBudgeted kB = SkBudgeted::kYes; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 54 | |
| 55 | constexpr ProxyParams::Kind kDeferred = ProxyParams::Kind::kDeferred; |
| 56 | constexpr ProxyParams::Kind kBackend = ProxyParams::Kind::kBackend; |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 57 | constexpr ProxyParams::Kind kInstantiated = ProxyParams::Kind::kInstantiated; |
| 58 | constexpr ProxyParams::Kind kLazy = ProxyParams::Kind::kLazy; |
| 59 | constexpr ProxyParams::Kind kFullyLazy = ProxyParams::Kind::kFullyLazy; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 62 | static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps, |
| 63 | const ProxyParams& p) { |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 64 | const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable); |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 65 | return proxyProvider->createProxy(format, {p.fSize, p.fSize}, p.fRenderable, p.fSampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 66 | GrMipmapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 69 | static sk_sp<GrSurfaceProxy> make_backend(GrDirectContext* dContext, const ProxyParams& p) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 70 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 71 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 72 | SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType); |
| 73 | SkASSERT(SkColorType::kUnknown_SkColorType != skColorType); |
| 74 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 75 | auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData( |
| 76 | dContext, p.fSize, p.fSize, skColorType, GrMipmapped::kNo, GrRenderable::kNo); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 77 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 78 | if (!mbet) { |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 79 | return nullptr; |
| 80 | } |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 81 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 82 | return proxyProvider->wrapBackendTexture(mbet->texture(), |
| 83 | kBorrow_GrWrapOwnership, |
| 84 | GrWrapCacheable::kNo, |
| 85 | kRead_GrIOType, |
| 86 | mbet->refCountedCallback()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 89 | static sk_sp<GrSurfaceProxy> make_fully_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps, |
| 90 | const ProxyParams& p) { |
| 91 | const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable); |
| 92 | auto cb = [p](GrResourceProvider* provider, const GrSurfaceProxy::LazySurfaceDesc& desc) { |
| 93 | auto tex = provider->createTexture({p.fSize, p.fSize}, desc.fFormat, |
Greg Daniel | e56d31f | 2021-08-16 09:29:56 -0400 | [diff] [blame] | 94 | desc.fTextureType, |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 95 | desc.fRenderable, desc.fSampleCnt, |
| 96 | desc.fMipmapped, desc.fBudgeted, |
| 97 | desc.fProtected); |
| 98 | return GrSurfaceProxy::LazyCallbackResult(std::move(tex)); |
| 99 | }; |
| 100 | return GrProxyProvider::MakeFullyLazyProxy(std::move(cb), format, p.fRenderable, p.fSampleCnt, |
| 101 | GrProtected::kNo, *caps, |
| 102 | GrSurfaceProxy::UseAllocator::kYes); |
| 103 | } |
| 104 | |
| 105 | static sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps, |
| 106 | const ProxyParams& p) { |
| 107 | const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable); |
| 108 | auto cb = [](GrResourceProvider* provider, const GrSurfaceProxy::LazySurfaceDesc& desc) { |
| 109 | auto tex = provider->createTexture(desc.fDimensions, desc.fFormat, |
Greg Daniel | e56d31f | 2021-08-16 09:29:56 -0400 | [diff] [blame] | 110 | desc.fTextureType, |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 111 | desc.fRenderable, desc.fSampleCnt, |
| 112 | desc.fMipmapped, desc.fBudgeted, |
| 113 | desc.fProtected); |
| 114 | return GrSurfaceProxy::LazyCallbackResult(std::move(tex)); |
| 115 | }; |
| 116 | return proxyProvider->createLazyProxy(std::move(cb), format, {p.fSize, p.fSize}, |
| 117 | GrMipmapped::kNo, GrMipmapStatus::kNotAllocated, |
| 118 | GrInternalSurfaceFlags::kNone, |
| 119 | p.fFit, p.fBudgeted, GrProtected::kNo, |
| 120 | GrSurfaceProxy::UseAllocator::kYes); |
| 121 | } |
| 122 | |
| 123 | static sk_sp<GrSurfaceProxy> make_proxy(GrDirectContext* dContext, const ProxyParams& p) { |
| 124 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 125 | const GrCaps* caps = dContext->priv().caps(); |
| 126 | sk_sp<GrSurfaceProxy> proxy; |
| 127 | switch (p.fKind) { |
| 128 | case ProxyParams::kDeferred: |
| 129 | proxy = make_deferred(proxyProvider, caps, p); |
| 130 | break; |
| 131 | case ProxyParams::kBackend: |
| 132 | proxy = make_backend(dContext, p); |
| 133 | break; |
| 134 | case ProxyParams::kFullyLazy: |
| 135 | proxy = make_fully_lazy(proxyProvider, caps, p); |
| 136 | break; |
| 137 | case ProxyParams::kLazy: |
| 138 | proxy = make_lazy(proxyProvider, caps, p); |
| 139 | break; |
| 140 | case ProxyParams::kInstantiated: |
| 141 | proxy = make_deferred(proxyProvider, caps, p); |
| 142 | if (proxy) { |
| 143 | auto surf = proxy->priv().createSurface(dContext->priv().resourceProvider()); |
| 144 | proxy->priv().assign(std::move(surf)); |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | if (proxy && p.fUniqueKey.isValid()) { |
| 149 | SkASSERT(proxy->asTextureProxy()); |
| 150 | proxyProvider->assignUniqueKeyToProxy(p.fUniqueKey, proxy->asTextureProxy()); |
| 151 | } |
| 152 | return proxy; |
| 153 | } |
| 154 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 155 | // Basic test that two proxies with overlapping intervals and compatible descriptors are |
| 156 | // assigned different GrSurfaces. |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 157 | static void overlap_test(skiatest::Reporter* reporter, GrDirectContext* dContext, |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 158 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 159 | bool expectedResult) { |
Adlai Holler | b51dd57 | 2021-04-15 11:01:46 -0400 | [diff] [blame] | 160 | GrResourceAllocator alloc(dContext); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 161 | |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 162 | alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 163 | alloc.incOps(); |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 164 | alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 165 | alloc.incOps(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 166 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, alloc.planAssignment()); |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, alloc.makeBudgetHeadroom()); |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, alloc.assign()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 170 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, p1->peekSurface()); |
| 172 | REPORTER_ASSERT(reporter, p2->peekSurface()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 173 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 174 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 177 | // Test various cases when two proxies do not have overlapping intervals. |
| 178 | // This mainly acts as a test of the ResourceAllocator's free pool. |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 179 | static void non_overlap_test(skiatest::Reporter* reporter, GrDirectContext* dContext, |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 180 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 181 | bool expectedResult) { |
Adlai Holler | b51dd57 | 2021-04-15 11:01:46 -0400 | [diff] [blame] | 182 | GrResourceAllocator alloc(dContext); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 183 | |
| 184 | alloc.incOps(); |
| 185 | alloc.incOps(); |
| 186 | alloc.incOps(); |
| 187 | alloc.incOps(); |
| 188 | alloc.incOps(); |
| 189 | alloc.incOps(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 190 | |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 191 | alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes); |
| 192 | alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 193 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, alloc.planAssignment()); |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, alloc.makeBudgetHeadroom()); |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, alloc.assign()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 197 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, p1->peekSurface()); |
| 199 | REPORTER_ASSERT(reporter, p2->peekSurface()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 200 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 201 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
| 202 | } |
| 203 | |
| 204 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) { |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 205 | auto dContext = ctxInfo.directContext(); |
| 206 | const GrCaps* caps = dContext->priv().caps(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 207 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 208 | struct TestCase { |
| 209 | ProxyParams fP1; |
| 210 | ProxyParams fP2; |
| 211 | bool fExpectation; |
| 212 | }; |
| 213 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 214 | constexpr bool kShare = true; |
| 215 | constexpr bool kDontShare = false; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 216 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 217 | // Non-RT GrSurfaces are never recycled on some platforms. |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 218 | bool kConditionallyShare = caps->reuseScratchTextures(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 219 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 220 | static const TestCase overlappingTests[] = { |
| 221 | // Two proxies with overlapping intervals and compatible descriptors should never share |
| 222 | // RT version |
| 223 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 224 | {64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 225 | kDontShare}, |
| 226 | // non-RT version |
| 227 | {{64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 228 | {64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 229 | kDontShare}, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 230 | }; |
| 231 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 232 | for (size_t i = 0; i < SK_ARRAY_COUNT(overlappingTests); i++) { |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 233 | const TestCase& test = overlappingTests[i]; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 234 | sk_sp<GrSurfaceProxy> p1 = make_proxy(dContext, test.fP1); |
| 235 | sk_sp<GrSurfaceProxy> p2 = make_proxy(dContext, test.fP2); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 236 | reporter->push(SkStringPrintf("case %d", SkToInt(i))); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 237 | overlap_test(reporter, dContext, std::move(p1), std::move(p2), test.fExpectation); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 238 | reporter->pop(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 241 | auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 242 | int k2 = caps->getRenderTargetSampleCount(2, beFormat); |
| 243 | int k4 = caps->getRenderTargetSampleCount(4, beFormat); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 244 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 245 | static const TestCase nonOverlappingTests[] = { |
| 246 | // Two non-overlapping intervals w/ compatible proxies should share |
| 247 | // both same size & approx |
| 248 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 249 | {64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 250 | kShare}, |
| 251 | {{64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 252 | {64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 253 | kConditionallyShare}, |
| 254 | // diffs sizes but still approx |
| 255 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 256 | {50, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 257 | kShare}, |
| 258 | {{64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 259 | {50, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 260 | kConditionallyShare}, |
| 261 | // sames sizes but exact |
| 262 | {{64, kRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 263 | {64, kRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 264 | kShare}, |
| 265 | {{64, kNotRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 266 | {64, kNotRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 267 | kConditionallyShare}, |
| 268 | // Two non-overlapping intervals w/ different exact sizes should not share |
| 269 | {{56, kRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 270 | {54, kRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 271 | kDontShare}, |
| 272 | // Two non-overlapping intervals w/ _very different_ approx sizes should not share |
| 273 | {{255, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 274 | {127, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 275 | kDontShare}, |
| 276 | // Two non-overlapping intervals w/ different MSAA sample counts should not share |
| 277 | {{64, kRT, kRGBA, kA, k2, kNotB, kDeferred}, |
| 278 | {64, kRT, kRGBA, kA, k4, kNotB, kDeferred}, |
| 279 | k2 == k4}, |
| 280 | // Two non-overlapping intervals w/ different configs should not share |
| 281 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 282 | {64, kRT, kAlpha, kA, 1, kNotB, kDeferred}, |
| 283 | kDontShare}, |
| 284 | // Two non-overlapping intervals w/ different RT classifications should never share |
| 285 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 286 | {64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 287 | kDontShare}, |
| 288 | {{64, kNotRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 289 | {64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 290 | kDontShare}, |
| 291 | // Two non-overlapping intervals w/ different origins should share |
| 292 | {{64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 293 | {64, kRT, kRGBA, kA, 1, kNotB, kDeferred}, |
| 294 | kShare}, |
| 295 | // Wrapped backend textures should never be reused |
| 296 | {{64, kNotRT, kRGBA, kE, 1, kNotB, kBackend}, |
| 297 | {64, kNotRT, kRGBA, kE, 1, kNotB, kDeferred}, |
| 298 | kDontShare} |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 299 | }; |
| 300 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 301 | for (size_t i = 0; i < SK_ARRAY_COUNT(nonOverlappingTests); i++) { |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 302 | const TestCase& test = nonOverlappingTests[i]; |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 303 | sk_sp<GrSurfaceProxy> p1 = make_proxy(dContext, test.fP1); |
| 304 | sk_sp<GrSurfaceProxy> p2 = make_proxy(dContext, test.fP2); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 305 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 306 | if (!p1 || !p2) { |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 307 | continue; // creation can fail (e.g., for msaa4 on iOS) |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 308 | } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 309 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 310 | reporter->push(SkStringPrintf("case %d", SkToInt(i))); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 311 | non_overlap_test(reporter, dContext, std::move(p1), std::move(p2), |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 312 | test.fExpectation); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 313 | reporter->pop(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 314 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 315 | } |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 316 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 317 | static void draw(GrRecordingContext* rContext) { |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 318 | SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 319 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 320 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 321 | ii, 1, kTopLeft_GrSurfaceOrigin, nullptr); |
| 322 | |
| 323 | SkCanvas* c = s->getCanvas(); |
| 324 | |
| 325 | c->clear(SK_ColorBLACK); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 330 | auto context = ctxInfo.directContext(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 331 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 332 | size_t maxBytes = context->getResourceCacheLimit(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 333 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 334 | context->setResourceCacheLimit(0); // We'll always be overbudget |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 335 | |
| 336 | draw(context); |
| 337 | draw(context); |
| 338 | draw(context); |
| 339 | draw(context); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 340 | context->flushAndSubmit(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 341 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 342 | context->setResourceCacheLimit(maxBytes); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 343 | } |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 344 | |
| 345 | struct Interval { |
| 346 | ProxyParams fParams; |
| 347 | int fStart; |
| 348 | int fEnd; |
| 349 | sk_sp<GrSurfaceProxy> fProxy = nullptr; |
| 350 | }; |
| 351 | |
| 352 | struct TestCase { |
| 353 | const char * fName; |
| 354 | bool fShouldFit; |
| 355 | size_t fBudget; |
| 356 | SkTArray<ProxyParams> fPurgeableResourcesInCache = {}; |
| 357 | SkTArray<ProxyParams> fUnpurgeableResourcesInCache = {}; |
| 358 | SkTArray<Interval> fIntervals; |
| 359 | }; |
| 360 | |
| 361 | static void memory_budget_test(skiatest::Reporter* reporter, |
| 362 | GrDirectContext* dContext, |
| 363 | const TestCase& test) { |
| 364 | // Reset cache. |
| 365 | auto cache = dContext->priv().getResourceCache(); |
| 366 | cache->releaseAll(); |
| 367 | cache->setLimit(test.fBudget); |
| 368 | |
| 369 | // Add purgeable entries. |
| 370 | size_t expectedPurgeableBytes = 0; |
| 371 | SkTArray<sk_sp<GrSurface>> purgeableSurfaces; |
| 372 | for (auto& params : test.fPurgeableResourcesInCache) { |
| 373 | SkASSERT(params.fKind == kInstantiated); |
| 374 | sk_sp<GrSurfaceProxy> proxy = make_proxy(dContext, params); |
| 375 | REPORTER_ASSERT(reporter, proxy->peekSurface()); |
| 376 | expectedPurgeableBytes += proxy->gpuMemorySize(); |
| 377 | purgeableSurfaces.push_back(sk_ref_sp(proxy->peekSurface())); |
| 378 | } |
| 379 | purgeableSurfaces.reset(); |
| 380 | REPORTER_ASSERT(reporter, expectedPurgeableBytes == cache->getPurgeableBytes(), |
| 381 | "%zu", cache->getPurgeableBytes()); |
| 382 | |
| 383 | // Add unpurgeable entries. |
| 384 | size_t expectedUnpurgeableBytes = 0; |
| 385 | SkTArray<sk_sp<GrSurface>> unpurgeableSurfaces; |
| 386 | for (auto& params : test.fUnpurgeableResourcesInCache) { |
| 387 | SkASSERT(params.fKind == kInstantiated); |
| 388 | sk_sp<GrSurfaceProxy> proxy = make_proxy(dContext, params); |
| 389 | REPORTER_ASSERT(reporter, proxy->peekSurface()); |
| 390 | expectedUnpurgeableBytes += proxy->gpuMemorySize(); |
| 391 | unpurgeableSurfaces.push_back(sk_ref_sp(proxy->peekSurface())); |
| 392 | } |
| 393 | |
| 394 | auto unpurgeableBytes = cache->getBudgetedResourceBytes() - cache->getPurgeableBytes(); |
| 395 | REPORTER_ASSERT(reporter, expectedUnpurgeableBytes == unpurgeableBytes, |
| 396 | "%zu", unpurgeableBytes); |
| 397 | |
| 398 | // Add intervals and test. |
Adlai Holler | b51dd57 | 2021-04-15 11:01:46 -0400 | [diff] [blame] | 399 | GrResourceAllocator alloc(dContext); |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame] | 400 | for (auto& interval : test.fIntervals) { |
| 401 | for (int i = interval.fStart; i <= interval.fEnd; i++) { |
| 402 | alloc.incOps(); |
| 403 | } |
| 404 | alloc.addInterval(interval.fProxy.get(), interval.fStart, interval.fEnd, |
| 405 | GrResourceAllocator::ActualUse::kYes); |
| 406 | } |
| 407 | REPORTER_ASSERT(reporter, alloc.planAssignment()); |
| 408 | REPORTER_ASSERT(reporter, alloc.makeBudgetHeadroom() == test.fShouldFit); |
| 409 | } |
| 410 | |
| 411 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorMemoryBudgetTest, reporter, ctxInfo) { |
| 412 | auto dContext = ctxInfo.directContext(); |
| 413 | |
| 414 | constexpr bool kUnder = true; |
| 415 | constexpr bool kOver = false; |
| 416 | constexpr size_t kRGBA64Bytes = 4 * 64 * 64; |
| 417 | const ProxyParams kProxy64 = {64, kRT, kRGBA, kE, 1, kB, kDeferred}; |
| 418 | const ProxyParams kProxy64NotBudgeted = {64, kRT, kRGBA, kE, 1, kNotB, kDeferred}; |
| 419 | const ProxyParams kProxy64Lazy = {64, kRT, kRGBA, kE, 1, kB, kLazy}; |
| 420 | const ProxyParams kProxy64FullyLazy = {64, kRT, kRGBA, kE, 1, kB, kFullyLazy}; |
| 421 | const ProxyParams kProxy32Instantiated = {32, kRT, kRGBA, kE, 1, kB, kInstantiated}; |
| 422 | const ProxyParams kProxy64Instantiated = {64, kRT, kRGBA, kE, 1, kB, kInstantiated}; |
| 423 | |
| 424 | TestCase tests[] = { |
| 425 | {"empty DAG", kUnder, 0, {}, {}, {}}, |
| 426 | {"unbudgeted", kUnder, 0, {}, {}, {{kProxy64NotBudgeted, 0, 2}}}, |
| 427 | {"basic", kUnder, kRGBA64Bytes, {}, {}, {{kProxy64, 0, 2}}}, |
| 428 | {"basic, over", kOver, kRGBA64Bytes - 1, {}, {}, {{kProxy64, 0, 2}}}, |
| 429 | {"shared", kUnder, kRGBA64Bytes, {}, {}, |
| 430 | { |
| 431 | {kProxy64, 0, 2}, |
| 432 | {kProxy64, 3, 5}, |
| 433 | }}, |
| 434 | {"retrieved from cache", kUnder, kRGBA64Bytes, |
| 435 | /* purgeable */{kProxy64Instantiated}, |
| 436 | /* unpurgeable */{}, |
| 437 | { |
| 438 | {kProxy64, 0, 2} |
| 439 | }}, |
| 440 | {"purge 4", kUnder, kRGBA64Bytes, |
| 441 | /* purgeable */{ |
| 442 | kProxy32Instantiated, |
| 443 | kProxy32Instantiated, |
| 444 | kProxy32Instantiated, |
| 445 | kProxy32Instantiated |
| 446 | }, |
| 447 | /* unpurgeable */{}, |
| 448 | { |
| 449 | {kProxy64, 0, 2} |
| 450 | }}, |
| 451 | {"dont purge what we've reserved", kOver, kRGBA64Bytes, |
| 452 | /* purgeable */{kProxy64Instantiated}, |
| 453 | /* unpurgeable */{}, |
| 454 | { |
| 455 | {kProxy64, 0, 2}, |
| 456 | {kProxy64, 1, 3} |
| 457 | }}, |
| 458 | {"unpurgeable", kOver, kRGBA64Bytes, |
| 459 | /* purgeable */{}, |
| 460 | /* unpurgeable */{kProxy64Instantiated}, |
| 461 | { |
| 462 | {kProxy64, 0, 2} |
| 463 | }}, |
| 464 | {"lazy", kUnder, kRGBA64Bytes, |
| 465 | /* purgeable */{}, |
| 466 | /* unpurgeable */{}, |
| 467 | { |
| 468 | {kProxy64Lazy, 0, 2} |
| 469 | }}, |
| 470 | {"lazy, over", kOver, kRGBA64Bytes - 1, |
| 471 | /* purgeable */{}, |
| 472 | /* unpurgeable */{}, |
| 473 | { |
| 474 | {kProxy64Lazy, 0, 2} |
| 475 | }}, |
| 476 | {"fully-lazy", kUnder, kRGBA64Bytes, |
| 477 | /* purgeable */{}, |
| 478 | /* unpurgeable */{}, |
| 479 | { |
| 480 | {kProxy64FullyLazy, 0, 2} |
| 481 | }}, |
| 482 | {"fully-lazy, over", kOver, kRGBA64Bytes - 1, |
| 483 | /* purgeable */{}, |
| 484 | /* unpurgeable */{}, |
| 485 | { |
| 486 | {kProxy64FullyLazy, 0, 2} |
| 487 | }}, |
| 488 | }; |
| 489 | SkString match(""); |
| 490 | for (size_t i = 0; i < SK_ARRAY_COUNT(tests); i++) { |
| 491 | TestCase& test = tests[i]; |
| 492 | if (match.isEmpty() || match == SkString(test.fName)) { |
| 493 | // Create proxies |
| 494 | for (Interval& interval : test.fIntervals) { |
| 495 | interval.fProxy = make_proxy(dContext, interval.fParams); |
| 496 | } |
| 497 | reporter->push(SkString(test.fName)); |
| 498 | memory_budget_test(reporter, dContext, test); |
| 499 | reporter->pop(); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |