blob: a0d67bb2e3bb0821b2487e74b39280ee30d0bfe1 [file] [log] [blame]
Robert Phillips5af44de2017-07-18 14:49:38 -04001/*
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 Phillipsfa8c0802017-10-04 08:42:28 -040011#if SK_SUPPORT_GPU
Robert Phillips5af44de2017-07-18 14:49:38 -040012#include "Test.h"
13
Robert Phillips57aa3672017-07-21 11:38:13 -040014#include "GrContextPriv.h"
15#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040017#include "GrResourceAllocator.h"
Robert Phillips57aa3672017-07-21 11:38:13 -040018#include "GrResourceProvider.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040019#include "GrSurfaceProxyPriv.h"
Robert Phillips57aa3672017-07-21 11:38:13 -040020#include "GrTest.h"
21#include "GrTexture.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040022#include "GrTextureProxy.h"
Greg Daniel4684f822018-03-08 15:27:36 -050023#include "GrUninstantiateProxyTracker.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040024
Robert Phillips57aa3672017-07-21 11:38:13 -040025struct 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 Phillips1afd4cd2018-01-08 13:40:32 -050035static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const ProxyParams& p) {
Robert Phillips57aa3672017-07-21 11:38:13 -040036 GrSurfaceDesc desc;
37 desc.fFlags = p.fIsRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillips57aa3672017-07-21 11:38:13 -040038 desc.fWidth = p.fSize;
39 desc.fHeight = p.fSize;
40 desc.fConfig = p.fConfig;
41 desc.fSampleCnt = p.fSampleCnt;
42
Brian Salomon2a4f9832018-03-03 22:43:43 -050043 return proxyProvider->createProxy(desc, p.fOrigin, p.fFit, SkBudgeted::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040044}
45
46static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050047 GrBackendTexture* backendTex) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050048 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050049 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050050
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050051 *backendTex = gpu->createTestingOnlyBackendTexture(nullptr, p.fSize, p.fSize,
52 p.fConfig, false,
53 GrMipMapped::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040054
Brian Salomon7578f3e2018-03-07 14:39:54 -050055 return proxyProvider->wrapBackendTexture(*backendTex, p.fOrigin);
Robert Phillips57aa3672017-07-21 11:38:13 -040056}
57
Brian Salomon26102cb2018-03-09 09:33:19 -050058static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050059 context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040060}
61
Robert Phillips5af44de2017-07-18 14:49:38 -040062// Basic test that two proxies with overlapping intervals and compatible descriptors are
63// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040064static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
65 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
66 bool expectedResult) {
Robert Phillips5af44de2017-07-18 14:49:38 -040067 GrResourceAllocator alloc(resourceProvider);
68
69 alloc.addInterval(p1.get(), 0, 4);
70 alloc.addInterval(p2.get(), 1, 2);
Robert Phillipseafd48a2017-11-16 07:52:08 -050071 alloc.markEndOfOpList(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040072
Robert Phillipseafd48a2017-11-16 07:52:08 -050073 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050074 GrResourceAllocator::AssignError error;
Greg Daniel4684f822018-03-08 15:27:36 -050075 GrUninstantiateProxyTracker uninstantiateTracker;
76 alloc.assign(&startIndex, &stopIndex, &uninstantiateTracker, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050077 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040078
79 REPORTER_ASSERT(reporter, p1->priv().peekSurface());
80 REPORTER_ASSERT(reporter, p2->priv().peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040081 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
82 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040083}
84
Robert Phillips57aa3672017-07-21 11:38:13 -040085// Test various cases when two proxies do not have overlapping intervals.
86// This mainly acts as a test of the ResourceAllocator's free pool.
87static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
88 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
89 bool expectedResult) {
90 GrResourceAllocator alloc(resourceProvider);
91
92 alloc.addInterval(p1.get(), 0, 2);
93 alloc.addInterval(p2.get(), 3, 5);
Robert Phillipseafd48a2017-11-16 07:52:08 -050094 alloc.markEndOfOpList(0);
Robert Phillips57aa3672017-07-21 11:38:13 -040095
Robert Phillipseafd48a2017-11-16 07:52:08 -050096 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050097 GrResourceAllocator::AssignError error;
Greg Daniel4684f822018-03-08 15:27:36 -050098 GrUninstantiateProxyTracker uninstantiateTracker;
99 alloc.assign(&startIndex, &stopIndex, &uninstantiateTracker, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500100 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400101
102 REPORTER_ASSERT(reporter, p1->priv().peekSurface());
103 REPORTER_ASSERT(reporter, p2->priv().peekSurface());
104 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
105 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
106}
107
Robert Phillips4150eea2018-02-07 17:08:21 -0500108bool GrResourceProvider::testingOnly_setExplicitlyAllocateGPUResources(bool newValue) {
109 bool oldValue = fExplicitlyAllocateGPUResources;
110 fExplicitlyAllocateGPUResources = newValue;
111 return oldValue;
112}
113
Robert Phillips57aa3672017-07-21 11:38:13 -0400114DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500115 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500116 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400117
Robert Phillips4150eea2018-02-07 17:08:21 -0500118 bool orig = resourceProvider->testingOnly_setExplicitlyAllocateGPUResources(true);
119
Robert Phillips57aa3672017-07-21 11:38:13 -0400120 struct TestCase {
121 ProxyParams fP1;
122 ProxyParams fP2;
123 bool fExpectation;
124 };
125
126 constexpr bool kRT = true;
127 constexpr bool kNotRT = false;
128
129 constexpr bool kShare = true;
130 constexpr bool kDontShare = false;
131 // Non-RT GrSurfaces are never recycled on some platforms.
132 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
133
134 const GrPixelConfig kRGBA = kRGBA_8888_GrPixelConfig;
135 const GrPixelConfig kBGRA = kBGRA_8888_GrPixelConfig;
136
137 const SkBackingFit kE = SkBackingFit::kExact;
138 const SkBackingFit kA = SkBackingFit::kApprox;
139
140 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
141 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
142
143 //--------------------------------------------------------------------------------------------
144 TestCase gOverlappingTests[] = {
145 //----------------------------------------------------------------------------------------
146 // Two proxies with overlapping intervals and compatible descriptors should never share
147 // RT version
148 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kDontShare },
149 // non-RT version
150 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kDontShare },
151 };
152
153 for (auto test : gOverlappingTests) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500154 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, test.fP1);
155 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, test.fP2);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500156 overlap_test(reporter, resourceProvider,
157 std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400158 }
159
Brian Salomonbdecacf2018-02-02 20:32:49 -0500160 int k2 = ctxInfo.grContext()->caps()->getRenderTargetSampleCount(2, kRGBA);
161 int k4 = ctxInfo.grContext()->caps()->getRenderTargetSampleCount(4, kRGBA);
Robert Phillips57aa3672017-07-21 11:38:13 -0400162
163 //--------------------------------------------------------------------------------------------
164 TestCase gNonOverlappingTests[] = {
165 //----------------------------------------------------------------------------------------
166 // Two non-overlapping intervals w/ compatible proxies should share
167 // both same size & approx
168 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kShare },
169 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare },
170 // diffs sizes but still approx
171 { { 64, kRT, kRGBA, kA, 0, kTL }, { 50, kRT, kRGBA, kA, 0, kTL }, kShare },
172 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 50, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare },
173 // sames sizes but exact
174 { { 64, kRT, kRGBA, kE, 0, kTL }, { 64, kRT, kRGBA, kE, 0, kTL }, kShare },
175 { { 64, kNotRT, kRGBA, kE, 0, kTL }, { 64, kNotRT, kRGBA, kE, 0, kTL }, kConditionallyShare },
176 //----------------------------------------------------------------------------------------
177 // Two non-overlapping intervals w/ different exact sizes should not share
178 { { 56, kRT, kRGBA, kE, 0, kTL }, { 54, kRT, kRGBA, kE, 0, kTL }, kDontShare },
179 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
180 { { 255, kRT, kRGBA, kA, 0, kTL }, { 127, kRT, kRGBA, kA, 0, kTL }, kDontShare },
181 // Two non-overlapping intervals w/ different MSAA sample counts should not share
182 { { 64, kRT, kRGBA, kA, k2, kTL },{ 64, kRT, kRGBA, kA, k4, kTL}, k2 == k4 },
183 // Two non-overlapping intervals w/ different configs should not share
184 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kBGRA, kA, 0, kTL }, kDontShare },
185 // Two non-overlapping intervals w/ different RT classifications should never share
186 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kDontShare },
187 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400188 // Two non-overlapping intervals w/ different origins should share
189 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kBL }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400190 };
191
192 for (auto test : gNonOverlappingTests) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500193 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, test.fP1);
194 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, test.fP2);
Robert Phillips57aa3672017-07-21 11:38:13 -0400195 if (!p1 || !p2) {
196 continue; // creation can fail (i.e., for msaa4 on iOS)
197 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500198 non_overlap_test(reporter, resourceProvider,
199 std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400200 }
201
202 {
203 // Wrapped backend textures should never be reused
204 TestCase t[1] = {
205 { { 64, kNotRT, kRGBA, kE, 0, kTL }, { 64, kNotRT, kRGBA, kE, 0, kTL }, kDontShare }
206 };
207
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500208 GrBackendTexture backEndTex;
209 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, t[0].fP2);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500211 non_overlap_test(reporter, resourceProvider,
212 std::move(p1), std::move(p2), t[0].fExpectation);
Brian Salomon26102cb2018-03-09 09:33:19 -0500213 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400214 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500215
216 resourceProvider->testingOnly_setExplicitlyAllocateGPUResources(orig);
Robert Phillips5af44de2017-07-18 14:49:38 -0400217}
218
219#endif