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" |
| 9 | #include "include/core/SkSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/GrProxyProvider.h" |
| 13 | #include "src/gpu/GrResourceAllocator.h" |
| 14 | #include "src/gpu/GrResourceProvider.h" |
| 15 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 16 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrTextureProxy.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 18 | #include "tests/Test.h" |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 19 | #include "tests/TestUtils.h" |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 20 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 21 | struct ProxyParams { |
| 22 | int fSize; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 23 | GrRenderable fRenderable; |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 24 | GrColorType fColorType; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 25 | SkBackingFit fFit; |
| 26 | int fSampleCnt; |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 27 | SkBudgeted fBudgeted; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 28 | // TODO: do we care about mipmapping |
| 29 | }; |
| 30 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 31 | static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps, |
| 32 | const ProxyParams& p) { |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 33 | const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable); |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 34 | return proxyProvider->createProxy(format, {p.fSize, p.fSize}, p.fRenderable, p.fSampleCnt, |
| 35 | GrMipMapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 38 | static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p, |
| 39 | GrBackendTexture* backendTex) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 40 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 41 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 42 | SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType); |
| 43 | SkASSERT(SkColorType::kUnknown_SkColorType != skColorType); |
| 44 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 45 | CreateBackendTexture(context, backendTex, p.fSize, p.fSize, skColorType, |
| 46 | SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo); |
| 47 | |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 48 | if (!backendTex->isValid()) { |
| 49 | return nullptr; |
| 50 | } |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 51 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 52 | return proxyProvider->wrapBackendTexture(*backendTex, kBorrow_GrWrapOwnership, |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 53 | GrWrapCacheable::kNo, kRead_GrIOType); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 56 | static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 57 | context->deleteBackendTexture(backendTex); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 60 | // Basic test that two proxies with overlapping intervals and compatible descriptors are |
| 61 | // assigned different GrSurfaces. |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 62 | static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider, |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 63 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 64 | bool expectedResult) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 65 | GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 66 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 67 | alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 68 | alloc.incOps(); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 69 | alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 70 | alloc.incOps(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 71 | alloc.markEndOfOpsTask(0); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 72 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 73 | alloc.determineRecyclability(); |
| 74 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 75 | int startIndex, stopIndex; |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 76 | GrResourceAllocator::AssignError error; |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 77 | alloc.assign(&startIndex, &stopIndex, &error); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 79 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, p1->peekSurface()); |
| 81 | REPORTER_ASSERT(reporter, p2->peekSurface()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 82 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 83 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 86 | // Test various cases when two proxies do not have overlapping intervals. |
| 87 | // This mainly acts as a test of the ResourceAllocator's free pool. |
| 88 | static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider, |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 89 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 90 | bool expectedResult) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 91 | GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1)); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 92 | |
| 93 | alloc.incOps(); |
| 94 | alloc.incOps(); |
| 95 | alloc.incOps(); |
| 96 | alloc.incOps(); |
| 97 | alloc.incOps(); |
| 98 | alloc.incOps(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 99 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 100 | alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes); |
| 101 | alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 102 | alloc.markEndOfOpsTask(0); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 103 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 104 | alloc.determineRecyclability(); |
| 105 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 106 | int startIndex, stopIndex; |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 107 | GrResourceAllocator::AssignError error; |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 108 | alloc.assign(&startIndex, &stopIndex, &error); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 110 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, p1->peekSurface()); |
| 112 | REPORTER_ASSERT(reporter, p2->peekSurface()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 113 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 114 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
| 115 | } |
| 116 | |
| 117 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 118 | const GrCaps* caps = ctxInfo.grContext()->priv().caps(); |
| 119 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider(); |
| 120 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 121 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 122 | struct TestCase { |
| 123 | ProxyParams fP1; |
| 124 | ProxyParams fP2; |
| 125 | bool fExpectation; |
| 126 | }; |
| 127 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 128 | constexpr GrRenderable kRT = GrRenderable::kYes; |
| 129 | constexpr GrRenderable kNotRT = GrRenderable::kNo; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 130 | |
| 131 | constexpr bool kShare = true; |
| 132 | constexpr bool kDontShare = false; |
| 133 | // Non-RT GrSurfaces are never recycled on some platforms. |
| 134 | bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures(); |
| 135 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 136 | const GrColorType kRGBA = GrColorType::kRGBA_8888; |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 137 | const GrColorType kAlpha = GrColorType::kAlpha_8; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 138 | |
| 139 | const SkBackingFit kE = SkBackingFit::kExact; |
| 140 | const SkBackingFit kA = SkBackingFit::kApprox; |
| 141 | |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 142 | const SkBudgeted kNotB = SkBudgeted::kNo; |
| 143 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 144 | //-------------------------------------------------------------------------------------------- |
| 145 | TestCase gOverlappingTests[] = { |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 146 | //---------------------------------------------------------------------------------------- |
| 147 | // Two proxies with overlapping intervals and compatible descriptors should never share |
| 148 | // RT version |
| 149 | {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare}, |
| 150 | // non-RT version |
| 151 | {{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare}, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | for (auto test : gOverlappingTests) { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 155 | sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1); |
| 156 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2); |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 157 | overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 160 | auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes); |
| 161 | int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(2, beFormat); |
| 162 | int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(4, beFormat); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 163 | |
| 164 | //-------------------------------------------------------------------------------------------- |
| 165 | TestCase gNonOverlappingTests[] = { |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 166 | //---------------------------------------------------------------------------------------- |
| 167 | // Two non-overlapping intervals w/ compatible proxies should share |
| 168 | // both same size & approx |
| 169 | {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare}, |
| 170 | {{64, kNotRT, kRGBA, kA, 1, kNotB}, |
| 171 | {64, kNotRT, kRGBA, kA, 1, kNotB}, |
| 172 | kConditionallyShare}, |
| 173 | // diffs sizes but still approx |
| 174 | {{64, kRT, kRGBA, kA, 1, kNotB}, {50, kRT, kRGBA, kA, 1, kNotB}, kShare}, |
| 175 | {{64, kNotRT, kRGBA, kA, 1, kNotB}, |
| 176 | {50, kNotRT, kRGBA, kA, 1, kNotB}, |
| 177 | kConditionallyShare}, |
| 178 | // sames sizes but exact |
| 179 | {{64, kRT, kRGBA, kE, 1, kNotB}, {64, kRT, kRGBA, kE, 1, kNotB}, kShare}, |
| 180 | {{64, kNotRT, kRGBA, kE, 1, kNotB}, |
| 181 | {64, kNotRT, kRGBA, kE, 1, kNotB}, |
| 182 | kConditionallyShare}, |
| 183 | //---------------------------------------------------------------------------------------- |
| 184 | // Two non-overlapping intervals w/ different exact sizes should not share |
| 185 | {{56, kRT, kRGBA, kE, 1, kNotB}, {54, kRT, kRGBA, kE, 1, kNotB}, kDontShare}, |
| 186 | // Two non-overlapping intervals w/ _very different_ approx sizes should not share |
| 187 | {{255, kRT, kRGBA, kA, 1, kNotB}, {127, kRT, kRGBA, kA, 1, kNotB}, kDontShare}, |
| 188 | // Two non-overlapping intervals w/ different MSAA sample counts should not share |
| 189 | {{64, kRT, kRGBA, kA, k2, kNotB}, {64, kRT, kRGBA, kA, k4, kNotB}, k2 == k4}, |
| 190 | // Two non-overlapping intervals w/ different configs should not share |
| 191 | {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kAlpha, kA, 1, kNotB}, kDontShare}, |
| 192 | // Two non-overlapping intervals w/ different RT classifications should never share |
| 193 | {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare}, |
| 194 | {{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare}, |
| 195 | // Two non-overlapping intervals w/ different origins should share |
| 196 | {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare}, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | for (auto test : gNonOverlappingTests) { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 200 | sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1); |
| 201 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 202 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 203 | if (!p1 || !p2) { |
| 204 | continue; // creation can fail (i.e., for msaa4 on iOS) |
| 205 | } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 206 | |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 207 | non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), |
| 208 | test.fExpectation); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | { |
| 212 | // Wrapped backend textures should never be reused |
| 213 | TestCase t[1] = { |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 214 | {{64, kNotRT, kRGBA, kE, 1, kNotB}, {64, kNotRT, kRGBA, kE, 1, kNotB}, kDontShare}}; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 215 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 216 | GrBackendTexture backEndTex; |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 217 | sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex); |
| 218 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 219 | |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 220 | non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), |
| 221 | t[0].fExpectation); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 222 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 223 | cleanup_backend(ctxInfo.grContext(), backEndTex); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 224 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 225 | } |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 226 | |
| 227 | static void draw(GrContext* context) { |
| 228 | SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 229 | |
| 230 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, |
| 231 | ii, 1, kTopLeft_GrSurfaceOrigin, nullptr); |
| 232 | |
| 233 | SkCanvas* c = s->getCanvas(); |
| 234 | |
| 235 | c->clear(SK_ColorBLACK); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) { |
| 240 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 241 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 242 | size_t maxBytes = context->getResourceCacheLimit(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 243 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 244 | context->setResourceCacheLimit(0); // We'll always be overbudget |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 245 | |
| 246 | draw(context); |
| 247 | draw(context); |
| 248 | draw(context); |
| 249 | draw(context); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 250 | context->flushAndSubmit(); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 251 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 252 | context->setResourceCacheLimit(maxBytes); |
Robert Phillips | 1734dd3 | 2018-08-21 13:52:09 -0400 | [diff] [blame] | 253 | } |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 254 | |
| 255 | sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 256 | const ProxyParams& p) { |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 257 | const auto format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable); |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 258 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 259 | auto callback = [](GrResourceProvider* resourceProvider, |
| 260 | const GrSurfaceProxy::LazySurfaceDesc& desc) { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 261 | sk_sp<GrTexture> texture; |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 262 | if (desc.fFit == SkBackingFit::kApprox) { |
| 263 | texture = resourceProvider->createApproxTexture(desc.fDimensions, desc.fFormat, |
| 264 | desc.fRenderable, desc.fSampleCnt, |
| 265 | desc.fProtected); |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 266 | } else { |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 267 | texture = resourceProvider->createTexture( |
| 268 | desc.fDimensions, desc.fFormat, desc.fRenderable, desc.fSampleCnt, |
| 269 | desc.fMipMapped, desc.fBudgeted, desc.fProtected); |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 270 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 271 | return GrSurfaceProxy::LazyCallbackResult(std::move(texture)); |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 272 | }; |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 273 | GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone; |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 274 | SkISize dims = {p.fSize, p.fSize}; |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 275 | return proxyProvider->createLazyProxy(callback, format, dims, p.fRenderable, p.fSampleCnt, |
| 276 | GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, flags, |
| 277 | p.fFit, p.fBudgeted, GrProtected::kNo, |
| 278 | GrSurfaceProxy::UseAllocator::kYes); |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 279 | } |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 280 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 281 | // Set up so there are two opsTasks that need to be flushed but the resource allocator thinks |
| 282 | // it is over budget. The two opsTasks should be flushed separately and the opsTask indices |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 283 | // returned from assign should be correct. |
| 284 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) { |
| 285 | GrContext* context = ctxInfo.grContext(); |
| 286 | const GrCaps* caps = context->priv().caps(); |
| 287 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 288 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 289 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 290 | size_t origMaxBytes = context->getResourceCacheLimit(); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 291 | |
| 292 | // Force the resource allocator to always believe it is over budget |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 293 | context->setResourceCacheLimit(0); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 294 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 295 | const ProxyParams params = { |
| 296 | 64, GrRenderable::kNo, GrColorType::kRGBA_8888, SkBackingFit::kExact, |
| 297 | 1, SkBudgeted::kYes}; |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 298 | |
| 299 | { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 300 | sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params); |
| 301 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params); |
| 302 | sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params); |
| 303 | sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 304 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 305 | GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 2)); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 306 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 307 | alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 308 | alloc.incOps(); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 309 | alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 310 | alloc.incOps(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 311 | alloc.markEndOfOpsTask(0); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 312 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 313 | alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 314 | alloc.incOps(); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 315 | alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 316 | alloc.incOps(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 317 | alloc.markEndOfOpsTask(1); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 318 | |
| 319 | int startIndex, stopIndex; |
| 320 | GrResourceAllocator::AssignError error; |
| 321 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 322 | alloc.determineRecyclability(); |
| 323 | |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 324 | alloc.assign(&startIndex, &stopIndex, &error); |
| 325 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
| 326 | REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex); |
| 327 | |
| 328 | alloc.assign(&startIndex, &stopIndex, &error); |
| 329 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
| 330 | REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 333 | context->setResourceCacheLimit(origMaxBytes); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 334 | } |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 335 | |
| 336 | // This test is used to make sure we are tracking the current task index during the assign call in |
| 337 | // the GrResourceAllocator. Specifically we can fall behind if we have intervals that don't |
| 338 | // use the allocator. In this case we need to possibly increment the fCurOpsTaskIndex multiple times |
| 339 | // to get in back in sync. We had a bug where we'd only every increment the index by one, |
| 340 | // http://crbug.com/996610. |
| 341 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorCurOpsTaskIndexTest, |
| 342 | reporter, ctxInfo) { |
| 343 | GrContext* context = ctxInfo.grContext(); |
| 344 | const GrCaps* caps = context->priv().caps(); |
| 345 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 346 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 347 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 348 | size_t origMaxBytes = context->getResourceCacheLimit(); |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 349 | |
| 350 | // Force the resource allocator to always believe it is over budget |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 351 | context->setResourceCacheLimit(0); |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 352 | |
| 353 | ProxyParams params; |
| 354 | params.fFit = SkBackingFit::kExact; |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 355 | params.fColorType = GrColorType::kRGBA_8888; |
| 356 | params.fRenderable = GrRenderable::kYes; |
| 357 | params.fSampleCnt = 1; |
| 358 | params.fSize = 100; |
| 359 | params.fBudgeted = SkBudgeted::kYes; |
| 360 | |
| 361 | sk_sp<GrSurfaceProxy> proxy1 = make_deferred(proxyProvider, caps, params); |
| 362 | if (!proxy1) { |
| 363 | return; |
| 364 | } |
| 365 | sk_sp<GrSurfaceProxy> proxy2 = make_deferred(proxyProvider, caps, params); |
| 366 | if (!proxy2) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // Wrapped proxy that will be ignored by the resourceAllocator. We use this to try and get the |
| 371 | // resource allocator fCurOpsTaskIndex to fall behind what it really should be. |
| 372 | GrBackendTexture backEndTex; |
| 373 | sk_sp<GrSurfaceProxy> proxyWrapped = make_backend(ctxInfo.grContext(), params, |
| 374 | &backEndTex); |
| 375 | if (!proxyWrapped) { |
| 376 | return; |
| 377 | } |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 378 | |
| 379 | // Same as above, but we actually need to have at least two intervals that don't go through the |
| 380 | // resource allocator to expose the index bug. |
| 381 | GrBackendTexture backEndTex2; |
| 382 | sk_sp<GrSurfaceProxy> proxyWrapped2 = make_backend(ctxInfo.grContext(), params, |
| 383 | &backEndTex2); |
| 384 | if (!proxyWrapped2) { |
| 385 | cleanup_backend(ctxInfo.grContext(), backEndTex); |
| 386 | return; |
| 387 | } |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 388 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 389 | GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 4)); |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 390 | |
| 391 | alloc.addInterval(proxyWrapped.get(), 0, 0, GrResourceAllocator::ActualUse::kYes); |
| 392 | alloc.incOps(); |
| 393 | alloc.markEndOfOpsTask(0); |
| 394 | |
| 395 | alloc.addInterval(proxyWrapped2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes); |
| 396 | alloc.incOps(); |
| 397 | alloc.markEndOfOpsTask(1); |
| 398 | |
| 399 | alloc.addInterval(proxy1.get(), 2, 2, GrResourceAllocator::ActualUse::kYes); |
| 400 | alloc.incOps(); |
| 401 | alloc.markEndOfOpsTask(2); |
| 402 | |
| 403 | // We want to force the resource allocator to do a intermediateFlush for the previous interval. |
| 404 | // But if it is the resource allocator is at the of its list of intervals it skips the |
| 405 | // intermediate flush call, so we add another interval here so it is not skipped. |
| 406 | alloc.addInterval(proxy2.get(), 3, 3, GrResourceAllocator::ActualUse::kYes); |
| 407 | alloc.incOps(); |
| 408 | alloc.markEndOfOpsTask(3); |
| 409 | |
| 410 | int startIndex, stopIndex; |
| 411 | GrResourceAllocator::AssignError error; |
| 412 | |
| 413 | alloc.determineRecyclability(); |
| 414 | |
| 415 | alloc.assign(&startIndex, &stopIndex, &error); |
| 416 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
| 417 | // The original bug in the allocator here would return a stopIndex of 2 since it would have only |
| 418 | // incremented its fCurOpsTaskIndex once instead of the needed two times to skip the first two |
| 419 | // unused intervals. |
| 420 | REPORTER_ASSERT(reporter, 0 == startIndex && 3 == stopIndex); |
| 421 | |
| 422 | alloc.assign(&startIndex, &stopIndex, &error); |
| 423 | REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error); |
| 424 | REPORTER_ASSERT(reporter, 3 == startIndex && 4 == stopIndex); |
| 425 | |
| 426 | cleanup_backend(ctxInfo.grContext(), backEndTex); |
| 427 | cleanup_backend(ctxInfo.grContext(), backEndTex2); |
| 428 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 429 | context->setResourceCacheLimit(origMaxBytes); |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 430 | } |
| 431 | |