blob: c74a8bd0922bcb9e8971201aca6c7be659317c63 [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
12#ifndef SK_DISABLE_DEFERRED_PROXIES
Robert Phillips5af44de2017-07-18 14:49:38 -040013#include "Test.h"
14
Robert Phillips57aa3672017-07-21 11:38:13 -040015#include "GrContextPriv.h"
16#include "GrGpu.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"
23
Robert Phillips57aa3672017-07-21 11:38:13 -040024struct 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
34static 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
47static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050048 GrBackendTexture* backendTex) {
49 *backendTex = context->getGpu()->createTestingOnlyBackendTexture(nullptr, p.fSize, p.fSize,
50 p.fConfig, false,
51 GrMipMapped::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040052
Robert Phillipsd21b2a52017-12-12 13:01:25 -050053 sk_sp<GrSurface> tex = context->resourceProvider()->wrapBackendTexture(*backendTex,
Robert Phillips16d8ec62017-07-27 16:16:25 -040054 kBorrow_GrWrapOwnership);
Robert Phillips066f0202017-07-25 10:16:35 -040055 return GrSurfaceProxy::MakeWrapped(std::move(tex), p.fOrigin);
Robert Phillips57aa3672017-07-21 11:38:13 -040056}
57
Robert Phillipsd21b2a52017-12-12 13:01:25 -050058static void cleanup_backend(GrContext* context, GrBackendTexture* backendTex) {
59 context->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;
74 alloc.assign(&startIndex, &stopIndex);
Robert Phillips5af44de2017-07-18 14:49:38 -040075
76 REPORTER_ASSERT(reporter, p1->priv().peekSurface());
77 REPORTER_ASSERT(reporter, p2->priv().peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040078 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
79 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040080}
81
Robert Phillips57aa3672017-07-21 11:38:13 -040082// Test various cases when two proxies do not have overlapping intervals.
83// This mainly acts as a test of the ResourceAllocator's free pool.
84static 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 Phillipseafd48a2017-11-16 07:52:08 -050091 alloc.markEndOfOpList(0);
Robert Phillips57aa3672017-07-21 11:38:13 -040092
Robert Phillipseafd48a2017-11-16 07:52:08 -050093 int startIndex, stopIndex;
94 alloc.assign(&startIndex, &stopIndex);
Robert Phillips57aa3672017-07-21 11:38:13 -040095
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
102DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400103 GrResourceProvider* resourceProvider = ctxInfo.grContext()->resourceProvider();
104
Robert Phillips57aa3672017-07-21 11:38:13 -0400105 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);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500141 overlap_test(reporter, resourceProvider,
142 std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400143 }
144
145 int k2 = ctxInfo.grContext()->caps()->getSampleCount(2, kRGBA);
146 int k4 = ctxInfo.grContext()->caps()->getSampleCount(4, kRGBA);
147
148 //--------------------------------------------------------------------------------------------
149 TestCase gNonOverlappingTests[] = {
150 //----------------------------------------------------------------------------------------
151 // Two non-overlapping intervals w/ compatible proxies should share
152 // both same size & approx
153 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kShare },
154 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare },
155 // diffs sizes but still approx
156 { { 64, kRT, kRGBA, kA, 0, kTL }, { 50, kRT, kRGBA, kA, 0, kTL }, kShare },
157 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 50, kNotRT, kRGBA, kA, 0, kTL }, kConditionallyShare },
158 // sames sizes but exact
159 { { 64, kRT, kRGBA, kE, 0, kTL }, { 64, kRT, kRGBA, kE, 0, kTL }, kShare },
160 { { 64, kNotRT, kRGBA, kE, 0, kTL }, { 64, kNotRT, kRGBA, kE, 0, kTL }, kConditionallyShare },
161 //----------------------------------------------------------------------------------------
162 // Two non-overlapping intervals w/ different exact sizes should not share
163 { { 56, kRT, kRGBA, kE, 0, kTL }, { 54, kRT, kRGBA, kE, 0, kTL }, kDontShare },
164 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
165 { { 255, kRT, kRGBA, kA, 0, kTL }, { 127, kRT, kRGBA, kA, 0, kTL }, kDontShare },
166 // Two non-overlapping intervals w/ different MSAA sample counts should not share
167 { { 64, kRT, kRGBA, kA, k2, kTL },{ 64, kRT, kRGBA, kA, k4, kTL}, k2 == k4 },
168 // Two non-overlapping intervals w/ different configs should not share
169 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kBGRA, kA, 0, kTL }, kDontShare },
170 // Two non-overlapping intervals w/ different RT classifications should never share
171 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kNotRT, kRGBA, kA, 0, kTL }, kDontShare },
172 { { 64, kNotRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kTL }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400173 // Two non-overlapping intervals w/ different origins should share
174 { { 64, kRT, kRGBA, kA, 0, kTL }, { 64, kRT, kRGBA, kA, 0, kBL }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400175 };
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 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500183 non_overlap_test(reporter, resourceProvider,
184 std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400185 }
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
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500193 GrBackendTexture backEndTex;
194 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400195 sk_sp<GrSurfaceProxy> p2 = make_deferred(resourceProvider, t[0].fP2);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500196 non_overlap_test(reporter, resourceProvider,
197 std::move(p1), std::move(p2), t[0].fExpectation);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500198 cleanup_backend(ctxInfo.grContext(), &backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400199 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400200}
201
202#endif
Robert Phillipsfa8c0802017-10-04 08:42:28 -0400203#endif