blob: a9b46435407dc014d1f522bea4907b262d62f744 [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 Phillipsc7635fa2016-10-28 13:25:24 -040015#include "GrRenderTargetPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070016#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050017#include "GrResourceProvider.h"
18#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040019#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050020#include "GrTextureProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070021
robertphillips8abb3702016-08-31 14:04:06 -070022// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070023static void check_surface(skiatest::Reporter* reporter,
24 GrSurfaceProxy* proxy,
25 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000026 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070027 GrPixelConfig config,
Robert Phillips294870f2016-11-11 12:38:40 -050028 const GrGpuResource::UniqueID& uniqueID,
Robert Phillipsabacf092016-11-02 10:23:32 -040029 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070030 REPORTER_ASSERT(reporter, proxy->origin() == origin);
31 REPORTER_ASSERT(reporter, proxy->width() == width);
32 REPORTER_ASSERT(reporter, proxy->height() == height);
33 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips294870f2016-11-11 12:38:40 -050034 if (!uniqueID.isInvalid()) {
35 REPORTER_ASSERT(reporter, proxy->uniqueID().asUInt() == uniqueID.asUInt());
36 } else {
37 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
robertphillips8abb3702016-08-31 14:04:06 -070038 }
Robert Phillipsabacf092016-11-02 10:23:32 -040039 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
robertphillips76948d42016-05-04 12:47:41 -070040}
41
42static void check_rendertarget(skiatest::Reporter* reporter,
Robert Phillipsec2249f2016-11-09 08:54:35 -050043 const GrCaps& caps,
Brian Osman32342f02017-03-04 08:12:46 -050044 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070045 GrRenderTargetProxy* rtProxy,
Robert Phillipsabacf092016-11-02 10:23:32 -040046 int numSamples,
Robert Phillipsec2249f2016-11-09 08:54:35 -050047 SkBackingFit fit,
Robert Phillips294870f2016-11-11 12:38:40 -050048 int expectedMaxWindowRects,
49 bool wasWrapped) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050050 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040051 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
52
Robert Phillips294870f2016-11-11 12:38:40 -050053 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040054 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
55 GrRenderTarget* rt = rtProxy->priv().peekRenderTarget();
robertphillips76948d42016-05-04 12:47:41 -070056
Robert Phillips294870f2016-11-11 12:38:40 -050057 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
58 if (wasWrapped) {
59 // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
60 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
61 } else {
62 // Deferred resources should always have a different ID from their instantiated rendertarget
63 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
64 }
65
Robert Phillips7294b852017-08-01 13:51:44 +000066 REPORTER_ASSERT(reporter, rt->origin() == rtProxy->origin());
robertphillips76948d42016-05-04 12:47:41 -070067 if (SkBackingFit::kExact == fit) {
68 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
69 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
70 } else {
71 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050072 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070073 }
74 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
75
Brian Salomon7c8460e2017-05-12 11:36:10 -040076 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070077 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
78 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070079 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070080}
81
82static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050083 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070084 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050085 SkBackingFit fit,
86 bool wasWrapped) {
87 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040088
89 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
90 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070091
Robert Phillips294870f2016-11-11 12:38:40 -050092 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
93 if (wasWrapped) {
94 // Wrapped resources share their uniqueID with the wrapping TextureProxy
95 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
96 } else {
97 // Deferred resources should always have a different ID from their instantiated texture
98 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
99 }
100
Robert Phillips7294b852017-08-01 13:51:44 +0000101 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
robertphillips76948d42016-05-04 12:47:41 -0700102 if (SkBackingFit::kExact == fit) {
103 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
104 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
105 } else {
106 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
107 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
108 }
109 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
110}
111
112
robertphillips8abb3702016-08-31 14:04:06 -0700113DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500114 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400115 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700116
Robert Phillips294870f2016-11-11 12:38:40 -0500117 const GrGpuResource::UniqueID kInvalidResourceID = GrGpuResource::UniqueID::InvalidID();
118
Robert Phillips6520a692017-02-01 09:20:00 -0500119 int attempt = 0; // useful for debugging
120
robertphillips76948d42016-05-04 12:47:41 -0700121 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500122 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500123 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400124 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700125 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
126 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500127 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700128 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400129 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700130 desc.fOrigin = origin;
131 desc.fWidth = widthHeight;
132 desc.fHeight = widthHeight;
133 desc.fConfig = config;
134 desc.fSampleCnt = numSamples;
135
Robert Phillips6520a692017-02-01 09:20:00 -0500136 {
137 sk_sp<GrTexture> tex;
138 if (SkBackingFit::kApprox == fit) {
Robert Phillips67d52cf2017-06-05 13:38:13 -0400139 tex = provider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500140 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400141 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500142 }
143
Robert Phillips2f493142017-03-02 18:18:38 -0500144 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips26c90e02017-03-14 14:39:29 -0400145 provider, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400146 fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500147 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
148 if (proxy) {
149 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500150 // This forces the proxy to compute and cache its
151 // pre-instantiation size guess. Later, when it is actually
152 // instantiated, it checks that the instantiated size is <= to
153 // the pre-computation. If the proxy never computed its
154 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500155 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500156
Robert Phillips2f493142017-03-02 18:18:38 -0500157 check_surface(reporter, proxy.get(), origin,
Robert Phillips40d94952017-01-30 14:15:59 -0500158 widthHeight, widthHeight, config,
159 kInvalidResourceID, budgeted);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400160 int supportedSamples = caps.getSampleCount(numSamples, config);
Robert Phillips40d94952017-01-30 14:15:59 -0500161 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500162 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400163 supportedSamples,
Robert Phillips40d94952017-01-30 14:15:59 -0500164 fit, caps.maxWindowRectangles(), false);
165 }
robertphillips76948d42016-05-04 12:47:41 -0700166 }
167
Robert Phillips84a81202016-11-04 11:59:10 -0400168 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700169
Robert Phillips6520a692017-02-01 09:20:00 -0500170 {
171 sk_sp<GrTexture> tex;
172 if (SkBackingFit::kApprox == fit) {
Robert Phillips67d52cf2017-06-05 13:38:13 -0400173 tex = provider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500174 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400175 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500176 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500177
Robert Phillips2f493142017-03-02 18:18:38 -0500178 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
Robert Phillips26c90e02017-03-14 14:39:29 -0400179 desc,
180 fit,
181 budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500182 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
183 if (proxy) {
Robert Phillips6520a692017-02-01 09:20:00 -0500184 // This forces the proxy to compute and cache its pre-instantiation
185 // size guess. Later, when it is actually instantiated, it checks
186 // that the instantiated size is <= to the pre-computation.
187 // If the proxy never computed its pre-instantiation size then the
188 // check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500189 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500190
Robert Phillips2f493142017-03-02 18:18:38 -0500191 check_surface(reporter, proxy.get(), origin,
Robert Phillips6520a692017-02-01 09:20:00 -0500192 widthHeight, widthHeight, config,
193 kInvalidResourceID, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500194 check_texture(reporter, provider, proxy->asTextureProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500195 fit, false);
196 }
Robert Phillips40d94952017-01-30 14:15:59 -0500197 }
Robert Phillips6520a692017-02-01 09:20:00 -0500198
199 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700200 }
201 }
202 }
203 }
204 }
205 }
206}
207
egdanielab527a52016-06-28 08:07:26 -0700208DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500209 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700210 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700211
212 static const int kWidthHeight = 100;
213
214 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
215 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
216 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
217 for (auto numSamples: { 0, 4}) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400218 int supportedNumSamples = caps.getSampleCount(numSamples, config);
Robert Phillips78de2122017-04-26 07:44:26 -0400219
csmartdaltonf9635992016-08-10 11:09:07 -0700220 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700221
222 GrSurfaceDesc desc;
223 desc.fOrigin = origin;
224 desc.fWidth = kWidthHeight;
225 desc.fHeight = kWidthHeight;
226 desc.fConfig = config;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400227 desc.fSampleCnt = supportedNumSamples;
robertphillips76948d42016-05-04 12:47:41 -0700228
csmartdaltonf9635992016-08-10 11:09:07 -0700229 // External on-screen render target.
230 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000231 GrGLFramebufferInfo fboInfo;
232 fboInfo.fFBOID = 0;
233 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
234 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700235
csmartdaltonf9635992016-08-10 11:09:07 -0700236 sk_sp<GrRenderTarget> defaultFBO(
Robert Phillips7294b852017-08-01 13:51:44 +0000237 provider->wrapBackendRenderTarget(backendRT, origin));
csmartdaltonf9635992016-08-10 11:09:07 -0700238
Robert Phillips066f0202017-07-25 10:16:35 -0400239 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO,
240 origin));
Robert Phillips37430132016-11-09 06:50:43 -0500241 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400242 kWidthHeight, kWidthHeight, config,
243 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500244 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400245 supportedNumSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700246 }
247
robertphillips76948d42016-05-04 12:47:41 -0700248 sk_sp<GrTexture> tex;
249
csmartdaltonf9635992016-08-10 11:09:07 -0700250 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700251 if (renderable) {
252 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400253 tex = provider->createTexture(desc, budgeted);
Robert Phillipsd9971c02017-08-02 11:51:43 -0400254 if (!tex) {
255 continue; // This can fail on Mesa
256 }
robertphillips76948d42016-05-04 12:47:41 -0700257 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
258
Robert Phillips066f0202017-07-25 10:16:35 -0400259 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500260 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400261 kWidthHeight, kWidthHeight, config,
262 rt->uniqueID(), budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500263 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400264 supportedNumSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500265 caps.maxWindowRectangles(), true);
robertphillips76948d42016-05-04 12:47:41 -0700266 }
267
268 if (!tex) {
269 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
270 desc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -0400271 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700272 }
273
Robert Phillips066f0202017-07-25 10:16:35 -0400274 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500275 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400276 kWidthHeight, kWidthHeight, config, tex->uniqueID(), budgeted);
Robert Phillips37430132016-11-09 06:50:43 -0500277 check_texture(reporter, provider, sProxy->asTextureProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500278 SkBackingFit::kExact, true);
robertphillips76948d42016-05-04 12:47:41 -0700279 }
280 }
281 }
282 }
283}
284
Robert Phillips78de2122017-04-26 07:44:26 -0400285DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
286 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
287
288 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
289 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
290 for (int width : { 0, 100 }) {
291 for (int height : { 0, 100}) {
292 if (width && height) {
293 continue; // not zero-sized
294 }
295
296 GrSurfaceDesc desc;
297 desc.fFlags = flags;
298 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
299 desc.fWidth = width;
300 desc.fHeight = height;
301 desc.fConfig = kRGBA_8888_GrPixelConfig;
302 desc.fSampleCnt = 0;
303
304 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
305 desc,
306 fit,
307 SkBudgeted::kNo));
308 REPORTER_ASSERT(reporter, !proxy);
309 }
310 }
311 }
312 }
313}
314
robertphillips76948d42016-05-04 12:47:41 -0700315#endif