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