blob: f48e2a090f923e20184eaa7fb231187631ebf1e3 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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#if SK_SUPPORT_GPU
Robert Phillips8bf1f9f2017-05-31 15:01:20 -040013
Greg Danielbcf612b2017-05-01 13:50:58 +000014#include "GrBackendSurface.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040017#include "GrRenderTargetPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070018#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050019#include "GrResourceProvider.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -040020#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040021#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050022#include "GrTextureProxy.h"
Greg Daniel2a303902018-02-20 10:25:54 -050023#include "SkGr.h"
robertphillips76948d42016-05-04 12:47:41 -070024
robertphillips8abb3702016-08-31 14:04:06 -070025// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070026static void check_surface(skiatest::Reporter* reporter,
27 GrSurfaceProxy* proxy,
28 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000029 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070030 GrPixelConfig config,
Robert Phillipsabacf092016-11-02 10:23:32 -040031 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070032 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);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050036 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
Robert Phillipsabacf092016-11-02 10:23:32 -040037 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
robertphillips76948d42016-05-04 12:47:41 -070038}
39
40static void check_rendertarget(skiatest::Reporter* reporter,
Robert Phillipsec2249f2016-11-09 08:54:35 -050041 const GrCaps& caps,
Brian Osman32342f02017-03-04 08:12:46 -050042 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070043 GrRenderTargetProxy* rtProxy,
Robert Phillipsabacf092016-11-02 10:23:32 -040044 int numSamples,
Robert Phillipsec2249f2016-11-09 08:54:35 -050045 SkBackingFit fit,
Greg Daniel2a303902018-02-20 10:25:54 -050046 int expectedMaxWindowRects) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050047 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040048 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
49
Robert Phillips294870f2016-11-11 12:38:40 -050050 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040051 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
52 GrRenderTarget* rt = rtProxy->priv().peekRenderTarget();
robertphillips76948d42016-05-04 12:47:41 -070053
Robert Phillips294870f2016-11-11 12:38:40 -050054 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
Greg Daniel2a303902018-02-20 10:25:54 -050055 // Deferred resources should always have a different ID from their instantiated rendertarget
56 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
Robert Phillips294870f2016-11-11 12:38:40 -050057
robertphillips76948d42016-05-04 12:47:41 -070058 if (SkBackingFit::kExact == fit) {
59 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
60 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
61 } else {
62 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050063 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070064 }
65 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
66
Brian Salomon7c8460e2017-05-12 11:36:10 -040067 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070068 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
69 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070070 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070071}
72
73static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050074 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070075 GrTextureProxy* texProxy,
Greg Daniel2a303902018-02-20 10:25:54 -050076 SkBackingFit fit) {
Robert Phillips294870f2016-11-11 12:38:40 -050077 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040078
79 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
80 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070081
Robert Phillips294870f2016-11-11 12:38:40 -050082 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
Greg Daniel2a303902018-02-20 10:25:54 -050083 // Deferred resources should always have a different ID from their instantiated texture
84 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
Robert Phillips294870f2016-11-11 12:38:40 -050085
robertphillips76948d42016-05-04 12:47:41 -070086 if (SkBackingFit::kExact == fit) {
87 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
88 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
89 } else {
90 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
91 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
92 }
93 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
94}
95
96
robertphillips8abb3702016-08-31 14:04:06 -070097DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050098 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050099 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400100 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700101
Robert Phillips6520a692017-02-01 09:20:00 -0500102 int attempt = 0; // useful for debugging
103
robertphillips76948d42016-05-04 12:47:41 -0700104 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500105 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500106 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400107 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700108 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
109 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500110 for (auto numSamples : {1, 4, 16, 128}) {
robertphillips76948d42016-05-04 12:47:41 -0700111 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400112 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700113 desc.fOrigin = origin;
114 desc.fWidth = widthHeight;
115 desc.fHeight = widthHeight;
116 desc.fConfig = config;
117 desc.fSampleCnt = numSamples;
118
Robert Phillips6520a692017-02-01 09:20:00 -0500119 {
120 sk_sp<GrTexture> tex;
121 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500122 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500123 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500124 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500125 }
126
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500127 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
128 desc, fit, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500129 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
130 if (proxy) {
131 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500132 // This forces the proxy to compute and cache its
133 // pre-instantiation size guess. Later, when it is actually
134 // instantiated, it checks that the instantiated size is <= to
135 // the pre-computation. If the proxy never computed its
136 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500137 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500138
Robert Phillips2f493142017-03-02 18:18:38 -0500139 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500140 widthHeight, widthHeight, config, budgeted);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 int supportedSamples =
142 caps.getRenderTargetSampleCount(numSamples, config);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500143 check_rendertarget(reporter, caps, resourceProvider,
Robert Phillips2f493142017-03-02 18:18:38 -0500144 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400145 supportedSamples,
Greg Daniel2a303902018-02-20 10:25:54 -0500146 fit, caps.maxWindowRectangles());
Robert Phillips40d94952017-01-30 14:15:59 -0500147 }
robertphillips76948d42016-05-04 12:47:41 -0700148 }
149
Robert Phillips84a81202016-11-04 11:59:10 -0400150 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700151
Robert Phillips6520a692017-02-01 09:20:00 -0500152 {
153 sk_sp<GrTexture> tex;
154 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500155 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500156 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500158 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500159
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500160 sk_sp<GrTextureProxy> proxy(proxyProvider->createProxy(
161 desc, fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500162 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
163 if (proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164 // This forces the proxy to compute and cache its
165 // pre-instantiation size guess. Later, when it is actually
166 // instantiated, it checks that the instantiated size is <= to
167 // the pre-computation. If the proxy never computed its
168 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500169 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500170
Robert Phillips2f493142017-03-02 18:18:38 -0500171 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500172 widthHeight, widthHeight, config, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500173 check_texture(reporter, resourceProvider,
Greg Daniel2a303902018-02-20 10:25:54 -0500174 proxy->asTextureProxy(), fit);
Robert Phillips6520a692017-02-01 09:20:00 -0500175 }
Robert Phillips40d94952017-01-30 14:15:59 -0500176 }
Robert Phillips6520a692017-02-01 09:20:00 -0500177
178 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700179 }
180 }
181 }
182 }
183 }
184 }
185}
186
egdanielab527a52016-06-28 08:07:26 -0700187DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500188 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
Greg Daniel2a303902018-02-20 10:25:54 -0500190 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
csmartdaltonf9635992016-08-10 11:09:07 -0700191 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700192
193 static const int kWidthHeight = 100;
194
Greg Daniel2a303902018-02-20 10:25:54 -0500195 if (kOpenGL_GrBackend != ctxInfo.backend()) {
196 return;
197 }
robertphillips76948d42016-05-04 12:47:41 -0700198 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Greg Daniel2a303902018-02-20 10:25:54 -0500199 for (auto colorType : { kAlpha_8_SkColorType, kRGBA_8888_SkColorType }) {
200 for (auto numSamples : {1, 4}) {
201 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, caps);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500202 SkASSERT(kUnknown_GrPixelConfig != config);
Greg Daniel2a303902018-02-20 10:25:54 -0500203 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
robertphillips76948d42016-05-04 12:47:41 -0700204
Greg Daniel2a303902018-02-20 10:25:54 -0500205 if (!supportedNumSamples) {
206 continue;
207 }
robertphillips76948d42016-05-04 12:47:41 -0700208
Greg Daniel2a303902018-02-20 10:25:54 -0500209 // External on-screen render target.
Greg Danielf87651e2018-02-21 11:36:53 -0500210 // Tests createWrappedRenderTargetProxy with a GrBackendRenderTarget
Greg Daniel2a303902018-02-20 10:25:54 -0500211 {
212 GrGLFramebufferInfo fboInfo;
213 fboInfo.fFBOID = 0;
214 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
215 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700216
Greg Daniel2a303902018-02-20 10:25:54 -0500217 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
218 backendRT, origin));
219 check_surface(reporter, sProxy.get(), origin,
220 kWidthHeight, kWidthHeight,
221 backendRT.testingOnly_getPixelConfig(), SkBudgeted::kNo);
222 check_rendertarget(reporter, caps, resourceProvider,
223 sProxy->asRenderTargetProxy(),
224 supportedNumSamples, SkBackingFit::kExact, 0);
225 }
226
Greg Danielf87651e2018-02-21 11:36:53 -0500227 // Tests createWrappedRenderTargetProxy with a GrBackendTexture
Greg Daniel2a303902018-02-20 10:25:54 -0500228 {
229 GrBackendTexture backendTex =
230 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
231 kWidthHeight, colorType, true,
232 GrMipMapped::kNo);
Greg Daniel2a303902018-02-20 10:25:54 -0500233 sk_sp<GrSurfaceProxy> sProxy =
Greg Danielf87651e2018-02-21 11:36:53 -0500234 proxyProvider->createWrappedRenderTargetProxy(backendTex, origin,
235 supportedNumSamples);
Greg Daniel2a303902018-02-20 10:25:54 -0500236 if (!sProxy) {
Greg Danielf87651e2018-02-21 11:36:53 -0500237 gpu->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel2a303902018-02-20 10:25:54 -0500238 continue; // This can fail on Mesa
csmartdaltonf9635992016-08-10 11:09:07 -0700239 }
240
Greg Daniel2a303902018-02-20 10:25:54 -0500241 check_surface(reporter, sProxy.get(), origin,
242 kWidthHeight, kWidthHeight,
243 backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
244 check_rendertarget(reporter, caps, resourceProvider,
245 sProxy->asRenderTargetProxy(),
246 supportedNumSamples, SkBackingFit::kExact,
247 caps.maxWindowRectangles());
robertphillips76948d42016-05-04 12:47:41 -0700248
Greg Daniel2a303902018-02-20 10:25:54 -0500249 gpu->deleteTestingOnlyBackendTexture(&backendTex);
250 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500251
Greg Danielf87651e2018-02-21 11:36:53 -0500252 // Tests createWrappedTextureProxy that is only renderable
253 {
254 GrBackendTexture backendTex =
255 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
256 kWidthHeight, colorType, true,
257 GrMipMapped::kNo);
258
259 sk_sp<GrSurfaceProxy> sProxy =
260 proxyProvider->createWrappedTextureProxy(backendTex, origin,
261 supportedNumSamples);
262 if (!sProxy) {
263 gpu->deleteTestingOnlyBackendTexture(&backendTex);
264 continue; // This can fail on Mesa
265 }
266
267 check_surface(reporter, sProxy.get(), origin,
268 kWidthHeight, kWidthHeight,
269 backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
270 check_rendertarget(reporter, caps, resourceProvider,
271 sProxy->asRenderTargetProxy(),
272 supportedNumSamples, SkBackingFit::kExact,
273 caps.maxWindowRectangles());
274
275 gpu->deleteTestingOnlyBackendTexture(&backendTex);
276 }
277
278 // Tests createWrappedTextureProxy that is only textureable
Greg Daniel2a303902018-02-20 10:25:54 -0500279 {
280 // Internal offscreen texture
281 GrBackendTexture backendTex =
282 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
283 kWidthHeight, colorType, false,
284 GrMipMapped::kNo);
robertphillips76948d42016-05-04 12:47:41 -0700285
Greg Daniel2a303902018-02-20 10:25:54 -0500286 sk_sp<GrSurfaceProxy> sProxy =
287 proxyProvider->createWrappedTextureProxy(backendTex, origin,
288 kBorrow_GrWrapOwnership,
289 nullptr, nullptr);
290 if (!sProxy) {
Greg Danielf87651e2018-02-21 11:36:53 -0500291 gpu->deleteTestingOnlyBackendTexture(&backendTex);
Greg Daniel2a303902018-02-20 10:25:54 -0500292 continue;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500293 }
Greg Daniel2a303902018-02-20 10:25:54 -0500294
295 check_surface(reporter, sProxy.get(), origin,
296 kWidthHeight, kWidthHeight,
297 backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
298 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
299 SkBackingFit::kExact);
300
301 gpu->deleteTestingOnlyBackendTexture(&backendTex);
robertphillips76948d42016-05-04 12:47:41 -0700302 }
303 }
304 }
305 }
306}
307
Robert Phillips78de2122017-04-26 07:44:26 -0400308DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500309 GrProxyProvider* provider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips78de2122017-04-26 07:44:26 -0400310
311 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
312 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
313 for (int width : { 0, 100 }) {
314 for (int height : { 0, 100}) {
315 if (width && height) {
316 continue; // not zero-sized
317 }
318
319 GrSurfaceDesc desc;
320 desc.fFlags = flags;
321 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
322 desc.fWidth = width;
323 desc.fHeight = height;
324 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500325 desc.fSampleCnt = 1;
Robert Phillips78de2122017-04-26 07:44:26 -0400326
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500327 sk_sp<GrTextureProxy> proxy = provider->createProxy(desc, fit, SkBudgeted::kNo);
Robert Phillips78de2122017-04-26 07:44:26 -0400328 REPORTER_ASSERT(reporter, !proxy);
329 }
330 }
331 }
332 }
333}
334
robertphillips76948d42016-05-04 12:47:41 -0700335#endif