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 "tests/Test.h"
11
12 #include "include/gpu/GrBackendSurface.h"
13 #include "include/gpu/GrTexture.h"
14 #include "src/gpu/GrContextPriv.h"
15 #include "src/gpu/GrProxyProvider.h"
16 #include "src/gpu/GrRenderTargetPriv.h"
17 #include "src/gpu/GrRenderTargetProxy.h"
18 #include "src/gpu/GrResourceProvider.h"
19 #include "src/gpu/GrSurfacePriv.h"
20 #include "src/gpu/GrSurfaceProxyPriv.h"
21 #include "src/gpu/GrTextureProxy.h"
22 #include "src/gpu/SkGr.h"
23 #ifdef SK_GL
24 #include "src/gpu/gl/GrGLDefines.h"
25 #include "src/gpu/gl/GrGLUtil.h"
26 #endif
27
28 // Check that the surface proxy's member vars are set as expected
check_surface(skiatest::Reporter * reporter,GrSurfaceProxy * proxy,int width,int height,SkBudgeted budgeted)29 static void check_surface(skiatest::Reporter* reporter,
30 GrSurfaceProxy* proxy,
31 int width, int height,
32 SkBudgeted budgeted) {
33 REPORTER_ASSERT(reporter, proxy->width() == width);
34 REPORTER_ASSERT(reporter, proxy->height() == height);
35 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
36 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
37 }
38
check_rendertarget(skiatest::Reporter * reporter,const GrCaps & caps,GrResourceProvider * provider,GrRenderTargetProxy * rtProxy,int numSamples,SkBackingFit fit,int expectedMaxWindowRects)39 static void check_rendertarget(skiatest::Reporter* reporter,
40 const GrCaps& caps,
41 GrResourceProvider* provider,
42 GrRenderTargetProxy* rtProxy,
43 int numSamples,
44 SkBackingFit fit,
45 int expectedMaxWindowRects) {
46 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
47 REPORTER_ASSERT(reporter, rtProxy->numSamples() == numSamples);
48
49 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
50 bool preinstantiated = rtProxy->isInstantiated();
51 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
52 GrRenderTarget* rt = rtProxy->peekRenderTarget();
53
54 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
55 // Deferred resources should always have a different ID from their instantiated rendertarget
56 if (preinstantiated) {
57 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
58 } else {
59 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
60 }
61
62 if (SkBackingFit::kExact == fit) {
63 REPORTER_ASSERT(reporter, rt->dimensions() == rtProxy->dimensions());
64 } else {
65 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
66 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
67 }
68 REPORTER_ASSERT(reporter, rt->backendFormat() == rtProxy->backendFormat());
69
70 REPORTER_ASSERT(reporter, rt->numSamples() == rtProxy->numSamples());
71 REPORTER_ASSERT(reporter, rt->surfacePriv().flags() == rtProxy->testingOnly_getFlags());
72 }
73
check_texture(skiatest::Reporter * reporter,GrResourceProvider * provider,GrTextureProxy * texProxy,SkBackingFit fit)74 static void check_texture(skiatest::Reporter* reporter,
75 GrResourceProvider* provider,
76 GrTextureProxy* texProxy,
77 SkBackingFit fit) {
78 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
79
80 bool preinstantiated = texProxy->isInstantiated();
81 // The instantiated texture should have these dimensions. If the fit is kExact, then
82 // 'backingStoreDimensions' reports the original WxH. If it is kApprox, make sure that
83 // the texture is that size and didn't reuse one of the kExact surfaces in the provider.
84 // This is important because upstream usage (e.g. SkImage) reports size based on the
85 // backingStoreDimensions and client code may rely on that if they are creating backend
86 // resources.
87 // NOTE: we store these before instantiating, since after instantiation backingStoreDimensions
88 // just returns the target's dimensions. In this instance, we want to ensure the target's
89 // dimensions are no different from the original approximate (or exact) dimensions.
90 SkISize expectedSize = texProxy->backingStoreDimensions();
91
92 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
93 GrTexture* tex = texProxy->peekTexture();
94
95 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
96 // Deferred resources should always have a different ID from their instantiated texture
97 if (preinstantiated) {
98 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
99 } else {
100 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
101 }
102
103 REPORTER_ASSERT(reporter, tex->dimensions() == expectedSize);
104
105 REPORTER_ASSERT(reporter, tex->backendFormat() == texProxy->backendFormat());
106 }
107
108
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest,reporter,ctxInfo)109 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
110 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
111 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
112 const GrCaps& caps = *ctxInfo.grContext()->priv().caps();
113
114 int attempt = 0; // useful for debugging
115
116 for (auto widthHeight : {100, 128, 1048576}) {
117 for (auto ct : {GrColorType::kAlpha_8, GrColorType::kBGR_565, GrColorType::kRGBA_8888,
118 GrColorType::kRGBA_1010102}) {
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 SkISize dims = {widthHeight, widthHeight};
123
124 auto format = caps.getDefaultBackendFormat(ct, GrRenderable::kYes);
125 if (!format.isValid()) {
126 continue;
127 }
128 GrSwizzle swizzle = caps.getReadSwizzle(format, ct);
129
130 // Renderable
131 {
132 sk_sp<GrTexture> tex;
133 if (SkBackingFit::kApprox == fit) {
134 tex = resourceProvider->createApproxTexture(
135 dims, format, GrRenderable::kYes, numSamples,
136 GrProtected::kNo);
137 } else {
138 tex = resourceProvider->createTexture(
139 dims, format, GrRenderable::kYes, numSamples,
140 GrMipMapped::kNo, budgeted, GrProtected::kNo);
141 }
142
143 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
144 format, dims, swizzle, GrRenderable::kYes, numSamples,
145 GrMipMapped::kNo, fit, budgeted, GrProtected::kNo);
146 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
147 if (proxy) {
148 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
149 // This forces the proxy to compute and cache its
150 // pre-instantiation size guess. Later, when it is actually
151 // instantiated, it checks that the instantiated size is <= to
152 // the pre-computation. If the proxy never computed its
153 // pre-instantiation size then the check is skipped.
154 proxy->gpuMemorySize(caps);
155
156 check_surface(reporter, proxy.get(), widthHeight, widthHeight,
157 budgeted);
158 int supportedSamples =
159 caps.getRenderTargetSampleCount(numSamples, format);
160 check_rendertarget(reporter, caps, resourceProvider,
161 proxy->asRenderTargetProxy(), supportedSamples,
162 fit, caps.maxWindowRectangles());
163 }
164 }
165
166 // Not renderable
167 {
168 sk_sp<GrTexture> tex;
169 if (SkBackingFit::kApprox == fit) {
170 tex = resourceProvider->createApproxTexture(
171 dims, format, GrRenderable::kNo, numSamples,
172 GrProtected::kNo);
173 } else {
174 tex = resourceProvider->createTexture(
175 dims, format, GrRenderable::kNo, numSamples,
176 GrMipMapped::kNo, budgeted, GrProtected::kNo);
177 }
178
179 sk_sp<GrTextureProxy> proxy(proxyProvider->createProxy(
180 format, dims, swizzle, GrRenderable::kNo, numSamples,
181 GrMipMapped::kNo, fit, budgeted, GrProtected::kNo));
182 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
183 if (proxy) {
184 // This forces the proxy to compute and cache its
185 // pre-instantiation size guess. Later, when it is actually
186 // instantiated, it checks that the instantiated size is <= to
187 // the pre-computation. If the proxy never computed its
188 // pre-instantiation size then the check is skipped.
189 proxy->gpuMemorySize(caps);
190
191 check_surface(reporter, proxy.get(), widthHeight, widthHeight,
192 budgeted);
193 check_texture(reporter, resourceProvider, proxy->asTextureProxy(),
194 fit);
195 }
196 }
197
198 attempt++;
199 }
200 }
201 }
202 }
203 }
204 }
205
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest,reporter,ctxInfo)206 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
207 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
208 GrContext* context = ctxInfo.grContext();
209 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
210 GrGpu* gpu = context->priv().getGpu();
211 const GrCaps& caps = *context->priv().caps();
212
213 static const int kWidthHeight = 100;
214
215 for (auto colorType :
216 {kAlpha_8_SkColorType, kRGBA_8888_SkColorType, kRGBA_1010102_SkColorType}) {
217 GrColorType grColorType = SkColorTypeToGrColorType(colorType);
218
219 // External on-screen render target.
220 // Tests wrapBackendRenderTarget with a GrBackendRenderTarget
221 // Our test-only function that creates a backend render target doesn't currently support
222 // sample counts :(.
223 if (ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType)) {
224 GrBackendRenderTarget backendRT = gpu->createTestingOnlyBackendRenderTarget(
225 kWidthHeight, kWidthHeight, grColorType);
226 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->wrapBackendRenderTarget(
227 backendRT, grColorType, nullptr, nullptr));
228 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
229 static constexpr int kExpectedNumSamples = 1;
230 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
231 kExpectedNumSamples, SkBackingFit::kExact,
232 caps.maxWindowRectangles());
233 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
234 }
235
236 for (auto numSamples : {1, 4}) {
237 auto beFormat = caps.getDefaultBackendFormat(grColorType, GrRenderable::kYes);
238 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, beFormat);
239 if (!supportedNumSamples) {
240 continue;
241 }
242
243 #ifdef SK_GL
244 // Test wrapping FBO 0 (with made up properties). This tests sample count and the
245 // special case where FBO 0 doesn't support window rectangles.
246 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
247 GrGLFramebufferInfo fboInfo;
248 fboInfo.fFBOID = 0;
249 fboInfo.fFormat = GrGLFormatToEnum(beFormat.asGLFormat());
250 SkASSERT(fboInfo.fFormat);
251 static constexpr int kStencilBits = 8;
252 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples,
253 kStencilBits, fboInfo);
254 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->wrapBackendRenderTarget(
255 backendRT, grColorType, nullptr, nullptr));
256 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
257 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
258 supportedNumSamples, SkBackingFit::kExact, 0);
259 }
260 #endif
261
262 // Tests wrapBackendRenderTarget with a GrBackendTexture
263 {
264 GrBackendTexture backendTex = context->createBackendTexture(
265 kWidthHeight, kWidthHeight, colorType, SkColors::kTransparent,
266 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
267 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTextureAsRenderTarget(
268 backendTex, grColorType, supportedNumSamples);
269 if (!sProxy) {
270 context->deleteBackendTexture(backendTex);
271 continue; // This can fail on Mesa
272 }
273
274 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
275 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
276 supportedNumSamples, SkBackingFit::kExact,
277 caps.maxWindowRectangles());
278
279 context->deleteBackendTexture(backendTex);
280 }
281
282 // Tests wrapBackendTexture that is only renderable
283 {
284 GrBackendTexture backendTex = context->createBackendTexture(
285 kWidthHeight, kWidthHeight, colorType, SkColors::kTransparent,
286 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
287
288 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapRenderableBackendTexture(
289 backendTex, supportedNumSamples, grColorType, kBorrow_GrWrapOwnership,
290 GrWrapCacheable::kNo, nullptr, nullptr);
291 if (!sProxy) {
292 context->deleteBackendTexture(backendTex);
293 continue; // This can fail on Mesa
294 }
295
296 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
297 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
298 supportedNumSamples, SkBackingFit::kExact,
299 caps.maxWindowRectangles());
300
301 context->deleteBackendTexture(backendTex);
302 }
303
304 // Tests wrapBackendTexture that is only textureable
305 {
306 // Internal offscreen texture
307 GrBackendTexture backendTex = context->createBackendTexture(
308 kWidthHeight, kWidthHeight, colorType, SkColors::kTransparent,
309 GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
310
311 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapBackendTexture(
312 backendTex, grColorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
313 kRead_GrIOType);
314 if (!sProxy) {
315 context->deleteBackendTexture(backendTex);
316 continue;
317 }
318
319 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
320 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
321 SkBackingFit::kExact);
322
323 context->deleteBackendTexture(backendTex);
324 }
325 }
326 }
327 }
328
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest,reporter,ctxInfo)329 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
330 GrContext* context = ctxInfo.grContext();
331 GrProxyProvider* provider = context->priv().proxyProvider();
332
333 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
334 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
335 for (int width : { 0, 100 }) {
336 for (int height : { 0, 100}) {
337 if (width && height) {
338 continue; // not zero-sized
339 }
340
341 const GrBackendFormat format =
342 context->priv().caps()->getDefaultBackendFormat(
343 GrColorType::kRGBA_8888,
344 renderable);
345 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
346 format, GrColorType::kRGBA_8888);
347
348 sk_sp<GrTextureProxy> proxy = provider->createProxy(
349 format, {width, height}, swizzle, renderable, 1, GrMipMapped::kNo, fit,
350 SkBudgeted::kNo, GrProtected::kNo);
351 REPORTER_ASSERT(reporter, !proxy);
352 }
353 }
354 }
355 }
356 }
357