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