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