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 | |
| 8 | // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. |
| 9 | #include "SkTypes.h" |
| 10 | |
Robert Phillips | fa8c080 | 2017-10-04 08:42:28 -0400 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
| 12 | #ifndef SK_DISABLE_DEFERRED_PROXIES |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 13 | #include "Test.h" |
| 14 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
| 16 | #include "GrGpu.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 17 | #include "GrProxyProvider.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 18 | #include "GrResourceAllocator.h" |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 19 | #include "GrResourceProvider.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 20 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 21 | #include "GrTest.h" |
| 22 | #include "GrTexture.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 23 | #include "GrTextureProxy.h" |
| 24 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 25 | struct ProxyParams { |
| 26 | int fSize; |
| 27 | bool fIsRT; |
| 28 | GrPixelConfig fConfig; |
| 29 | SkBackingFit fFit; |
| 30 | int fSampleCnt; |
| 31 | GrSurfaceOrigin fOrigin; |
| 32 | // TODO: do we care about mipmapping |
| 33 | }; |
| 34 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 35 | static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const ProxyParams& p) { |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 36 | GrSurfaceDesc desc; |
| 37 | desc.fFlags = p.fIsRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 38 | desc.fOrigin = p.fOrigin; |
| 39 | desc.fWidth = p.fSize; |
| 40 | desc.fHeight = p.fSize; |
| 41 | desc.fConfig = p.fConfig; |
| 42 | desc.fSampleCnt = p.fSampleCnt; |
| 43 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 44 | return proxyProvider->createProxy(desc, p.fFit, SkBudgeted::kNo); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p, |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 48 | GrBackendTexture* backendTex) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 49 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 50 | |
Hal Canary | 07320e4 | 2018-01-19 18:18:20 +0000 | [diff] [blame^] | 51 | *backendTex = context->getGpu()->createTestingOnlyBackendTexture(nullptr, p.fSize, p.fSize, |
| 52 | p.fConfig, false, |
| 53 | GrMipMapped::kNo); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 54 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 55 | return proxyProvider->createWrappedTextureProxy(*backendTex, p.fOrigin); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 58 | static void cleanup_backend(GrContext* context, GrBackendTexture* backendTex) { |
Hal Canary | 07320e4 | 2018-01-19 18:18:20 +0000 | [diff] [blame^] | 59 | context->getGpu()->deleteTestingOnlyBackendTexture(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, |
| 65 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
| 66 | bool expectedResult) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 67 | GrResourceAllocator alloc(resourceProvider); |
| 68 | |
| 69 | alloc.addInterval(p1.get(), 0, 4); |
| 70 | alloc.addInterval(p2.get(), 1, 2); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 71 | alloc.markEndOfOpList(0); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 72 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 73 | int startIndex, stopIndex; |
| 74 | alloc.assign(&startIndex, &stopIndex); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 75 | |
| 76 | REPORTER_ASSERT(reporter, p1->priv().peekSurface()); |
| 77 | REPORTER_ASSERT(reporter, p2->priv().peekSurface()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 78 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 79 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 82 | // Test various cases when two proxies do not have overlapping intervals. |
| 83 | // This mainly acts as a test of the ResourceAllocator's free pool. |
| 84 | static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider, |
| 85 | sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2, |
| 86 | bool expectedResult) { |
| 87 | GrResourceAllocator alloc(resourceProvider); |
| 88 | |
| 89 | alloc.addInterval(p1.get(), 0, 2); |
| 90 | alloc.addInterval(p2.get(), 3, 5); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 91 | alloc.markEndOfOpList(0); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 92 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 93 | int startIndex, stopIndex; |
| 94 | alloc.assign(&startIndex, &stopIndex); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 95 | |
| 96 | REPORTER_ASSERT(reporter, p1->priv().peekSurface()); |
| 97 | REPORTER_ASSERT(reporter, p2->priv().peekSurface()); |
| 98 | bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID(); |
| 99 | REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch); |
| 100 | } |
| 101 | |
| 102 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 103 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 104 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 105 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 106 | struct TestCase { |
| 107 | ProxyParams fP1; |
| 108 | ProxyParams fP2; |
| 109 | bool fExpectation; |
| 110 | }; |
| 111 | |
| 112 | constexpr bool kRT = true; |
| 113 | constexpr bool kNotRT = false; |
| 114 | |
| 115 | constexpr bool kShare = true; |
| 116 | constexpr bool kDontShare = false; |
| 117 | // Non-RT GrSurfaces are never recycled on some platforms. |
| 118 | bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures(); |
| 119 | |
| 120 | const GrPixelConfig kRGBA = kRGBA_8888_GrPixelConfig; |
| 121 | const GrPixelConfig kBGRA = kBGRA_8888_GrPixelConfig; |
| 122 | |
| 123 | const SkBackingFit kE = SkBackingFit::kExact; |
| 124 | const SkBackingFit kA = SkBackingFit::kApprox; |
| 125 | |
| 126 | const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin; |
| 127 | const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin; |
| 128 | |
| 129 | //-------------------------------------------------------------------------------------------- |
| 130 | TestCase gOverlappingTests[] = { |
| 131 | //---------------------------------------------------------------------------------------- |
| 132 | // Two proxies with overlapping intervals and compatible descriptors should never share |
| 133 | // RT version |
| 134 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kDontShare }, |
| 135 | // non-RT version |
| 136 | { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kDontShare }, |
| 137 | }; |
| 138 | |
| 139 | for (auto test : gOverlappingTests) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 140 | sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, test.fP1); |
| 141 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, test.fP2); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 142 | overlap_test(reporter, resourceProvider, |
| 143 | std::move(p1), std::move(p2), test.fExpectation); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int k2 = ctxInfo.grContext()->caps()->getSampleCount(2, kRGBA); |
| 147 | int k4 = ctxInfo.grContext()->caps()->getSampleCount(4, kRGBA); |
| 148 | |
| 149 | //-------------------------------------------------------------------------------------------- |
| 150 | TestCase gNonOverlappingTests[] = { |
| 151 | //---------------------------------------------------------------------------------------- |
| 152 | // Two non-overlapping intervals w/ compatible proxies should share |
| 153 | // both same size & approx |
| 154 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kShare }, |
| 155 | { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare }, |
| 156 | // diffs sizes but still approx |
| 157 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 50, kRT, kRGBA, kA, 0, kTL }, kShare }, |
| 158 | { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 50, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare }, |
| 159 | // sames sizes but exact |
| 160 | { { 64, kRT, kRGBA, kE, 0, kTL }, { 64, kRT, kRGBA, kE, 0, kTL }, kShare }, |
| 161 | { { 64, kNotRT, kRGBA, kE, 0, kTL }, { 64, kNotRT, kRGBA, kE, 0, kTL }, kConditionallyShare }, |
| 162 | //---------------------------------------------------------------------------------------- |
| 163 | // Two non-overlapping intervals w/ different exact sizes should not share |
| 164 | { { 56, kRT, kRGBA, kE, 0, kTL }, { 54, kRT, kRGBA, kE, 0, kTL }, kDontShare }, |
| 165 | // Two non-overlapping intervals w/ _very different_ approx sizes should not share |
| 166 | { { 255, kRT, kRGBA, kA, 0, kTL }, { 127, kRT, kRGBA, kA, 0, kTL }, kDontShare }, |
| 167 | // Two non-overlapping intervals w/ different MSAA sample counts should not share |
| 168 | { { 64, kRT, kRGBA, kA, k2, kTL },{ 64, kRT, kRGBA, kA, k4, kTL}, k2 == k4 }, |
| 169 | // Two non-overlapping intervals w/ different configs should not share |
| 170 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kBGRA, kA, 0, kTL }, kDontShare }, |
| 171 | // Two non-overlapping intervals w/ different RT classifications should never share |
| 172 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kDontShare }, |
| 173 | { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kDontShare }, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 174 | // Two non-overlapping intervals w/ different origins should share |
| 175 | { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kBL }, kShare }, |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | for (auto test : gNonOverlappingTests) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 179 | sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, test.fP1); |
| 180 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, test.fP2); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 181 | if (!p1 || !p2) { |
| 182 | continue; // creation can fail (i.e., for msaa4 on iOS) |
| 183 | } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 184 | non_overlap_test(reporter, resourceProvider, |
| 185 | std::move(p1), std::move(p2), test.fExpectation); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | { |
| 189 | // Wrapped backend textures should never be reused |
| 190 | TestCase t[1] = { |
| 191 | { { 64, kNotRT, kRGBA, kE, 0, kTL }, { 64, kNotRT, kRGBA, kE, 0, kTL }, kDontShare } |
| 192 | }; |
| 193 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 194 | GrBackendTexture backEndTex; |
| 195 | sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 196 | sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, t[0].fP2); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 197 | non_overlap_test(reporter, resourceProvider, |
| 198 | std::move(p1), std::move(p2), t[0].fExpectation); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 199 | cleanup_backend(ctxInfo.grContext(), &backEndTex); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 200 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | #endif |
Robert Phillips | fa8c080 | 2017-10-04 08:42:28 -0400 | [diff] [blame] | 204 | #endif |