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()->priv().proxyProvider();
109 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
110 const GrCaps& caps = *ctxInfo.grContext()->priv().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()->priv().proxyProvider();
214 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
215 GrGpu* gpu = ctxInfo.grContext()->priv().getGpu();
216 const GrCaps& caps = *ctxInfo.grContext()->priv().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, nullptr,
232 nullptr));
233 check_surface(reporter, sProxy.get(), origin, kWidthHeight, kWidthHeight,
234 backendRT.pixelConfig(), SkBudgeted::kNo);
235 static constexpr int kExpectedNumSamples = 1;
236 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
237 kExpectedNumSamples, SkBackingFit::kExact,
238 caps.maxWindowRectangles());
239 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
240 }
241
242 for (auto numSamples : {1, 4}) {
243 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
244 SkASSERT(kUnknown_GrPixelConfig != config);
245 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
246
247 if (!supportedNumSamples) {
248 continue;
249 }
250
251 // Test wrapping FBO 0 (with made up properties). This tests sample count and the
252 // special case where FBO 0 doesn't support window rectangles.
253 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
254 GrGLFramebufferInfo fboInfo;
255 fboInfo.fFBOID = 0;
256 fboInfo.fFormat = GR_GL_RGBA8;
257 static constexpr int kStencilBits = 8;
258 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples,
259 kStencilBits, fboInfo);
260 backendRT.setPixelConfig(config);
261 sk_sp<GrSurfaceProxy> sProxy(
262 proxyProvider->wrapBackendRenderTarget(backendRT, origin, nullptr,
263 nullptr));
264 check_surface(reporter, sProxy.get(), origin,
265 kWidthHeight, kWidthHeight,
266 backendRT.pixelConfig(), SkBudgeted::kNo);
267 check_rendertarget(reporter, caps, resourceProvider,
268 sProxy->asRenderTargetProxy(),
269 supportedNumSamples, SkBackingFit::kExact, 0);
270 }
271
272 // Tests wrapBackendRenderTarget with a GrBackendTexture
273 {
274 GrBackendTexture backendTex =
275 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
276 kWidthHeight, colorType,
277 true, GrMipMapped::kNo);
278 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTextureAsRenderTarget(
279 backendTex, origin, supportedNumSamples);
280 if (!sProxy) {
281 gpu->deleteTestingOnlyBackendTexture(backendTex);
282 continue; // This can fail on Mesa
283 }
284
285 check_surface(reporter, sProxy.get(), origin,
286 kWidthHeight, kWidthHeight,
287 backendTex.pixelConfig(), SkBudgeted::kNo);
288 check_rendertarget(reporter, caps, resourceProvider,
289 sProxy->asRenderTargetProxy(),
290 supportedNumSamples, SkBackingFit::kExact,
291 caps.maxWindowRectangles());
292
293 gpu->deleteTestingOnlyBackendTexture(backendTex);
294 }
295
296 // Tests wrapBackendTexture that is only renderable
297 {
298 GrBackendTexture backendTex =
299 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
300 kWidthHeight, colorType,
301 true, GrMipMapped::kNo);
302
303 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapRenderableBackendTexture(
304 backendTex, origin, supportedNumSamples, kBorrow_GrWrapOwnership,
305 GrWrapCacheable::kNo, nullptr, nullptr);
306 if (!sProxy) {
307 gpu->deleteTestingOnlyBackendTexture(backendTex);
308 continue; // This can fail on Mesa
309 }
310
311 check_surface(reporter, sProxy.get(), origin,
312 kWidthHeight, kWidthHeight,
313 backendTex.pixelConfig(), SkBudgeted::kNo);
314 check_rendertarget(reporter, caps, resourceProvider,
315 sProxy->asRenderTargetProxy(),
316 supportedNumSamples, SkBackingFit::kExact,
317 caps.maxWindowRectangles());
318
319 gpu->deleteTestingOnlyBackendTexture(backendTex);
320 }
321
322 // Tests wrapBackendTexture that is only textureable
323 {
324 // Internal offscreen texture
325 GrBackendTexture backendTex =
326 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
327 kWidthHeight, colorType,
328 false, GrMipMapped::kNo);
329
330 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTexture(
331 backendTex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
332 kRead_GrIOType);
333 if (!sProxy) {
334 gpu->deleteTestingOnlyBackendTexture(backendTex);
335 continue;
336 }
337
338 check_surface(reporter, sProxy.get(), origin,
339 kWidthHeight, kWidthHeight,
340 backendTex.pixelConfig(), SkBudgeted::kNo);
341 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
342 SkBackingFit::kExact);
343
344 gpu->deleteTestingOnlyBackendTexture(backendTex);
345 }
346 }
347 }
348 }
349 }
350
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest,reporter,ctxInfo)351 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
352 GrProxyProvider* provider = ctxInfo.grContext()->priv().proxyProvider();
353
354 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
355 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
356 for (int width : { 0, 100 }) {
357 for (int height : { 0, 100}) {
358 if (width && height) {
359 continue; // not zero-sized
360 }
361
362 GrSurfaceDesc desc;
363 desc.fFlags = flags;
364 desc.fWidth = width;
365 desc.fHeight = height;
366 desc.fConfig = kRGBA_8888_GrPixelConfig;
367 desc.fSampleCnt = 1;
368
369 const GrBackendFormat format =
370 ctxInfo.grContext()->priv().caps()->getBackendFormatFromColorType(
371 kRGBA_8888_SkColorType);
372
373 sk_sp<GrTextureProxy> proxy = provider->createProxy(
374 format, desc, kBottomLeft_GrSurfaceOrigin, fit, SkBudgeted::kNo);
375 REPORTER_ASSERT(reporter, !proxy);
376 }
377 }
378 }
379 }
380 }
381