1 /*
2 * Copyright 2016 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 // This is a GPU-backend specific test.
9
10 #include "Test.h"
11
12 #include "GrBackendSurface.h"
13 #include "GrContextPriv.h"
14 #include "GrProxyProvider.h"
15 #include "GrRenderTargetPriv.h"
16 #include "GrRenderTargetProxy.h"
17 #include "GrResourceProvider.h"
18 #include "GrSurfacePriv.h"
19 #include "GrSurfaceProxyPriv.h"
20 #include "GrTexture.h"
21 #include "GrTextureProxy.h"
22 #include "SkGr.h"
23 #include "gl/GrGLDefines.h"
24
25 // Check that the surface proxy's member vars are set as expected
check_surface(skiatest::Reporter * reporter,GrSurfaceProxy * proxy,GrSurfaceOrigin origin,int width,int height,GrPixelConfig config,SkBudgeted budgeted)26 static void check_surface(skiatest::Reporter* reporter,
27 GrSurfaceProxy* proxy,
28 GrSurfaceOrigin origin,
29 int width, int height,
30 GrPixelConfig config,
31 SkBudgeted budgeted) {
32 REPORTER_ASSERT(reporter, proxy->origin() == origin);
33 REPORTER_ASSERT(reporter, proxy->width() == width);
34 REPORTER_ASSERT(reporter, proxy->height() == height);
35 REPORTER_ASSERT(reporter, proxy->config() == config);
36 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
37 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
38 }
39
check_rendertarget(skiatest::Reporter * reporter,const GrCaps & caps,GrResourceProvider * provider,GrRenderTargetProxy * rtProxy,int numSamples,SkBackingFit fit,int expectedMaxWindowRects)40 static void check_rendertarget(skiatest::Reporter* reporter,
41 const GrCaps& caps,
42 GrResourceProvider* provider,
43 GrRenderTargetProxy* rtProxy,
44 int numSamples,
45 SkBackingFit fit,
46 int expectedMaxWindowRects) {
47 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
48 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
49
50 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
51 bool preinstantiated = rtProxy->isInstantiated();
52 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
53 GrRenderTarget* rt = rtProxy->peekRenderTarget();
54
55 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
56 // Deferred resources should always have a different ID from their instantiated rendertarget
57 if (preinstantiated) {
58 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
59 } else {
60 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
61 }
62
63 if (SkBackingFit::kExact == fit) {
64 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
65 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
66 } else {
67 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
68 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
69 }
70 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
71
72 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
73 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
74 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
75 REPORTER_ASSERT(reporter, rt->surfacePriv().flags() == rtProxy->testingOnly_getFlags());
76 }
77
check_texture(skiatest::Reporter * reporter,GrResourceProvider * provider,GrTextureProxy * texProxy,SkBackingFit fit)78 static void check_texture(skiatest::Reporter* reporter,
79 GrResourceProvider* provider,
80 GrTextureProxy* texProxy,
81 SkBackingFit fit) {
82 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
83
84 bool preinstantiated = texProxy->isInstantiated();
85 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
86 GrTexture* tex = texProxy->peekTexture();
87
88 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
89 // Deferred resources should always have a different ID from their instantiated texture
90 if (preinstantiated) {
91 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
92 } else {
93 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
94 }
95
96 if (SkBackingFit::kExact == fit) {
97 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
98 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
99 } else {
100 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
101 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
102 }
103 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
104 }
105
106
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest,reporter,ctxInfo)107 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
108 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
109 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
110 const GrCaps& caps = *ctxInfo.grContext()->contextPriv().caps();
111
112 int attempt = 0; // useful for debugging
113
114 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
115 for (auto widthHeight : { 100, 128, 1048576 }) {
116 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
117 kRGBA_8888_GrPixelConfig, kRGBA_1010102_GrPixelConfig,
118 kRGB_ETC1_GrPixelConfig }) {
119 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
120 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
121 for (auto numSamples : {1, 4, 16, 128}) {
122 // We don't have recycling support for compressed textures
123 if (GrPixelConfigIsCompressed(config) && SkBackingFit::kApprox == fit) {
124 continue;
125 }
126
127 GrSurfaceDesc desc;
128 desc.fFlags = kRenderTarget_GrSurfaceFlag;
129 desc.fWidth = widthHeight;
130 desc.fHeight = widthHeight;
131 desc.fConfig = config;
132 desc.fSampleCnt = numSamples;
133
134 GrSRGBEncoded srgbEncoded;
135 GrColorType colorType =
136 GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded);
137 const GrBackendFormat format =
138 caps.getBackendFormatFromGrColorType(colorType, srgbEncoded);
139
140 {
141 sk_sp<GrTexture> tex;
142 if (SkBackingFit::kApprox == fit) {
143 tex = resourceProvider->createApproxTexture(
144 desc, GrResourceProvider::Flags::kNone);
145 } else {
146 tex = resourceProvider->createTexture(desc, budgeted);
147 }
148
149 sk_sp<GrTextureProxy> proxy =
150 proxyProvider->createProxy(format, desc, origin, fit,
151 budgeted);
152 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
153 if (proxy) {
154 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
155 // This forces the proxy to compute and cache its
156 // pre-instantiation size guess. Later, when it is actually
157 // instantiated, it checks that the instantiated size is <= to
158 // the pre-computation. If the proxy never computed its
159 // pre-instantiation size then the check is skipped.
160 proxy->gpuMemorySize();
161
162 check_surface(reporter, proxy.get(), origin,
163 widthHeight, widthHeight, config, budgeted);
164 int supportedSamples =
165 caps.getRenderTargetSampleCount(numSamples, config);
166 check_rendertarget(reporter, caps, resourceProvider,
167 proxy->asRenderTargetProxy(),
168 supportedSamples,
169 fit, caps.maxWindowRectangles());
170 }
171 }
172
173 desc.fFlags = kNone_GrSurfaceFlags;
174
175 {
176 sk_sp<GrTexture> tex;
177 if (SkBackingFit::kApprox == fit) {
178 tex = resourceProvider->createApproxTexture(
179 desc, GrResourceProvider::Flags::kNone);
180 } else {
181 tex = resourceProvider->createTexture(desc, budgeted);
182 }
183
184 sk_sp<GrTextureProxy> proxy(
185 proxyProvider->createProxy(format, desc, origin, fit,
186 budgeted));
187 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
188 if (proxy) {
189 // This forces the proxy to compute and cache its
190 // pre-instantiation size guess. Later, when it is actually
191 // instantiated, it checks that the instantiated size is <= to
192 // the pre-computation. If the proxy never computed its
193 // pre-instantiation size then the check is skipped.
194 proxy->gpuMemorySize();
195
196 check_surface(reporter, proxy.get(), origin,
197 widthHeight, widthHeight, config, budgeted);
198 check_texture(reporter, resourceProvider,
199 proxy->asTextureProxy(), fit);
200 }
201 }
202
203 attempt++;
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest,reporter,ctxInfo)212 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
213 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
214 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
215 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
216 const GrCaps& caps = *ctxInfo.grContext()->contextPriv().caps();
217
218 static const int kWidthHeight = 100;
219
220 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
221 for (auto colorType : { kAlpha_8_SkColorType, kRGBA_8888_SkColorType,
222 kRGBA_1010102_SkColorType }) {
223 // External on-screen render target.
224 // Tests wrapBackendRenderTarget with a GrBackendRenderTarget
225 // Our test-only function that creates a backend render target doesn't currently support
226 // sample counts :(.
227 if (ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType)) {
228 GrBackendRenderTarget backendRT = gpu->createTestingOnlyBackendRenderTarget(
229 kWidthHeight, kWidthHeight, SkColorTypeToGrColorType(colorType));
230 sk_sp<GrSurfaceProxy> sProxy(
231 proxyProvider->wrapBackendRenderTarget(backendRT, origin));
232 check_surface(reporter, sProxy.get(), origin, kWidthHeight, kWidthHeight,
233 backendRT.pixelConfig(), SkBudgeted::kNo);
234 static constexpr int kExpectedNumSamples = 1;
235 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
236 kExpectedNumSamples, SkBackingFit::kExact,
237 caps.maxWindowRectangles());
238 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
239 }
240
241 for (auto numSamples : {1, 4}) {
242 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
243 SkASSERT(kUnknown_GrPixelConfig != config);
244 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
245
246 if (!supportedNumSamples) {
247 continue;
248 }
249
250 // Test wrapping FBO 0 (with made up properties). This tests sample count and the
251 // special case where FBO 0 doesn't support window rectangles.
252 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
253 GrGLFramebufferInfo fboInfo;
254 fboInfo.fFBOID = 0;
255 fboInfo.fFormat = GR_GL_RGBA8;
256 static constexpr int kStencilBits = 8;
257 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples,
258 kStencilBits, fboInfo);
259 backendRT.setPixelConfig(config);
260 sk_sp<GrSurfaceProxy> sProxy(
261 proxyProvider->wrapBackendRenderTarget(backendRT, origin));
262 check_surface(reporter, sProxy.get(), origin,
263 kWidthHeight, kWidthHeight,
264 backendRT.pixelConfig(), SkBudgeted::kNo);
265 check_rendertarget(reporter, caps, resourceProvider,
266 sProxy->asRenderTargetProxy(),
267 supportedNumSamples, SkBackingFit::kExact, 0);
268 }
269
270 // Tests wrapBackendRenderTarget with a GrBackendTexture
271 {
272 GrBackendTexture backendTex =
273 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
274 kWidthHeight, colorType,
275 true, GrMipMapped::kNo);
276 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTextureAsRenderTarget(
277 backendTex, origin, supportedNumSamples);
278 if (!sProxy) {
279 gpu->deleteTestingOnlyBackendTexture(backendTex);
280 continue; // This can fail on Mesa
281 }
282
283 check_surface(reporter, sProxy.get(), origin,
284 kWidthHeight, kWidthHeight,
285 backendTex.pixelConfig(), SkBudgeted::kNo);
286 check_rendertarget(reporter, caps, resourceProvider,
287 sProxy->asRenderTargetProxy(),
288 supportedNumSamples, SkBackingFit::kExact,
289 caps.maxWindowRectangles());
290
291 gpu->deleteTestingOnlyBackendTexture(backendTex);
292 }
293
294 // Tests wrapBackendTexture that is only renderable
295 {
296 GrBackendTexture backendTex =
297 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
298 kWidthHeight, colorType,
299 true, GrMipMapped::kNo);
300
301 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapRenderableBackendTexture(
302 backendTex, origin, supportedNumSamples, kBorrow_GrWrapOwnership,
303 GrWrapCacheable::kNo);
304 if (!sProxy) {
305 gpu->deleteTestingOnlyBackendTexture(backendTex);
306 continue; // This can fail on Mesa
307 }
308
309 check_surface(reporter, sProxy.get(), origin,
310 kWidthHeight, kWidthHeight,
311 backendTex.pixelConfig(), SkBudgeted::kNo);
312 check_rendertarget(reporter, caps, resourceProvider,
313 sProxy->asRenderTargetProxy(),
314 supportedNumSamples, SkBackingFit::kExact,
315 caps.maxWindowRectangles());
316
317 gpu->deleteTestingOnlyBackendTexture(backendTex);
318 }
319
320 // Tests wrapBackendTexture that is only textureable
321 {
322 // Internal offscreen texture
323 GrBackendTexture backendTex =
324 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
325 kWidthHeight, colorType,
326 false, GrMipMapped::kNo);
327
328 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTexture(
329 backendTex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
330 kRead_GrIOType);
331 if (!sProxy) {
332 gpu->deleteTestingOnlyBackendTexture(backendTex);
333 continue;
334 }
335
336 check_surface(reporter, sProxy.get(), origin,
337 kWidthHeight, kWidthHeight,
338 backendTex.pixelConfig(), SkBudgeted::kNo);
339 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
340 SkBackingFit::kExact);
341
342 gpu->deleteTestingOnlyBackendTexture(backendTex);
343 }
344 }
345 }
346 }
347 }
348
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest,reporter,ctxInfo)349 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
350 GrProxyProvider* provider = ctxInfo.grContext()->contextPriv().proxyProvider();
351
352 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
353 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
354 for (int width : { 0, 100 }) {
355 for (int height : { 0, 100}) {
356 if (width && height) {
357 continue; // not zero-sized
358 }
359
360 GrSurfaceDesc desc;
361 desc.fFlags = flags;
362 desc.fWidth = width;
363 desc.fHeight = height;
364 desc.fConfig = kRGBA_8888_GrPixelConfig;
365 desc.fSampleCnt = 1;
366
367 const GrBackendFormat format =
368 ctxInfo.grContext()->contextPriv().caps()->getBackendFormatFromColorType(
369 kRGBA_8888_SkColorType);
370
371 sk_sp<GrTextureProxy> proxy = provider->createProxy(
372 format, desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kNo);
373 REPORTER_ASSERT(reporter, !proxy);
374 }
375 }
376 }
377 }
378 }
379