blob: 6553b4e94fd02027ccf0016c1c161a637e2752f3 [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
Greg Danielbcf612b2017-05-01 13:50:58 +000013#include "GrBackendSurface.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040014#include "GrRenderTargetPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070015#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
17#include "GrSurfaceProxy.h"
18#include "GrTextureProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070019
robertphillips8abb3702016-08-31 14:04:06 -070020// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070021static void check_surface(skiatest::Reporter* reporter,
22 GrSurfaceProxy* proxy,
23 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000024 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070025 GrPixelConfig config,
Robert Phillips294870f2016-11-11 12:38:40 -050026 const GrGpuResource::UniqueID& uniqueID,
Robert Phillipsabacf092016-11-02 10:23:32 -040027 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070028 REPORTER_ASSERT(reporter, proxy->origin() == origin);
29 REPORTER_ASSERT(reporter, proxy->width() == width);
30 REPORTER_ASSERT(reporter, proxy->height() == height);
31 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips294870f2016-11-11 12:38:40 -050032 if (!uniqueID.isInvalid()) {
33 REPORTER_ASSERT(reporter, proxy->uniqueID().asUInt() == uniqueID.asUInt());
34 } else {
35 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
robertphillips8abb3702016-08-31 14:04:06 -070036 }
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,
Robert Phillips294870f2016-11-11 12:38:40 -050046 int expectedMaxWindowRects,
47 bool wasWrapped) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050048 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040049 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
50
Robert Phillips294870f2016-11-11 12:38:40 -050051 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070052 GrRenderTarget* rt = rtProxy->instantiate(provider);
53 REPORTER_ASSERT(reporter, rt);
54
Robert Phillips294870f2016-11-11 12:38:40 -050055 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
56 if (wasWrapped) {
57 // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
58 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
59 } else {
60 // Deferred resources should always have a different ID from their instantiated rendertarget
61 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
62 }
63
robertphillips76948d42016-05-04 12:47:41 -070064 REPORTER_ASSERT(reporter, rt->origin() == rtProxy->origin());
65 if (SkBackingFit::kExact == fit) {
66 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
67 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
68 } else {
69 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050070 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070071 }
72 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
73
74 REPORTER_ASSERT(reporter, rt->isUnifiedMultisampled() == rtProxy->isUnifiedMultisampled());
75 REPORTER_ASSERT(reporter, rt->isStencilBufferMultisampled() ==
76 rtProxy->isStencilBufferMultisampled());
77 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->isMixedSampled() == rtProxy->isMixedSampled());
80 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070081}
82
83static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050084 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070085 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050086 SkBackingFit fit,
87 bool wasWrapped) {
88 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070089 GrTexture* tex = texProxy->instantiate(provider);
90 REPORTER_ASSERT(reporter, tex);
91
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
robertphillips76948d42016-05-04 12:47:41 -0700101 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
102 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,
124 kETC1_GrPixelConfig, 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) {
Brian Osman32342f02017-03-04 08:12:46 -0500139 tex.reset(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);
160 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500161 proxy->asRenderTargetProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500162 SkTMin(numSamples, caps.maxSampleCount()),
Robert Phillips40d94952017-01-30 14:15:59 -0500163 fit, caps.maxWindowRectangles(), false);
164 }
robertphillips76948d42016-05-04 12:47:41 -0700165 }
166
Robert Phillips84a81202016-11-04 11:59:10 -0400167 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700168
Robert Phillips6520a692017-02-01 09:20:00 -0500169 {
170 sk_sp<GrTexture> tex;
171 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500172 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500173 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400174 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500175 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500176
Robert Phillips2f493142017-03-02 18:18:38 -0500177 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
Robert Phillips26c90e02017-03-14 14:39:29 -0400178 desc,
179 fit,
180 budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500181 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
182 if (proxy) {
Robert Phillips6520a692017-02-01 09:20:00 -0500183 // This forces the proxy to compute and cache its pre-instantiation
184 // size guess. Later, when it is actually instantiated, it checks
185 // that the instantiated size is <= to the pre-computation.
186 // If the proxy never computed its pre-instantiation size then the
187 // check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500188 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500189
Robert Phillips2f493142017-03-02 18:18:38 -0500190 check_surface(reporter, proxy.get(), origin,
Robert Phillips6520a692017-02-01 09:20:00 -0500191 widthHeight, widthHeight, config,
192 kInvalidResourceID, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500193 check_texture(reporter, provider, proxy->asTextureProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500194 fit, false);
195 }
Robert Phillips40d94952017-01-30 14:15:59 -0500196 }
Robert Phillips6520a692017-02-01 09:20:00 -0500197
198 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700199 }
200 }
201 }
202 }
203 }
204 }
205}
206
egdanielab527a52016-06-28 08:07:26 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500208 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700209 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700210
211 static const int kWidthHeight = 100;
212
213 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
214 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
215 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
216 for (auto numSamples: { 0, 4}) {
Robert Phillips78de2122017-04-26 07:44:26 -0400217 if (caps.maxSampleCount() < numSamples) {
218 continue;
219 }
220
csmartdaltonf9635992016-08-10 11:09:07 -0700221 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700222
223 GrSurfaceDesc desc;
224 desc.fOrigin = origin;
225 desc.fWidth = kWidthHeight;
226 desc.fHeight = kWidthHeight;
227 desc.fConfig = config;
228 desc.fSampleCnt = numSamples;
229
csmartdaltonf9635992016-08-10 11:09:07 -0700230 // External on-screen render target.
231 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000232 GrGLFramebufferInfo fboInfo;
233 fboInfo.fFBOID = 0;
234 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
235 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700236
csmartdaltonf9635992016-08-10 11:09:07 -0700237 sk_sp<GrRenderTarget> defaultFBO(
Greg Danielbcf612b2017-05-01 13:50:58 +0000238 provider->wrapBackendRenderTarget(backendRT, origin));
csmartdaltonf9635992016-08-10 11:09:07 -0700239
Robert Phillips37430132016-11-09 06:50:43 -0500240 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO));
241 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(),
Robert Phillips294870f2016-11-11 12:38:40 -0500245 numSamples, 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);
robertphillips76948d42016-05-04 12:47:41 -0700254 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
255
Robert Phillips37430132016-11-09 06:50:43 -0500256 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt));
257 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400258 kWidthHeight, kWidthHeight, config,
259 rt->uniqueID(), budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500260 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
261 numSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500262 caps.maxWindowRectangles(), true);
robertphillips76948d42016-05-04 12:47:41 -0700263 }
264
265 if (!tex) {
266 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
267 desc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -0400268 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700269 }
270
Robert Phillips37430132016-11-09 06:50:43 -0500271 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex));
272 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400273 kWidthHeight, kWidthHeight, config, tex->uniqueID(), budgeted);
Robert Phillips37430132016-11-09 06:50:43 -0500274 check_texture(reporter, provider, sProxy->asTextureProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500275 SkBackingFit::kExact, true);
robertphillips76948d42016-05-04 12:47:41 -0700276 }
277 }
278 }
279 }
280}
281
Robert Phillips78de2122017-04-26 07:44:26 -0400282DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
283 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
284
285 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
286 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
287 for (int width : { 0, 100 }) {
288 for (int height : { 0, 100}) {
289 if (width && height) {
290 continue; // not zero-sized
291 }
292
293 GrSurfaceDesc desc;
294 desc.fFlags = flags;
295 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
296 desc.fWidth = width;
297 desc.fHeight = height;
298 desc.fConfig = kRGBA_8888_GrPixelConfig;
299 desc.fSampleCnt = 0;
300
301 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
302 desc,
303 fit,
304 SkBudgeted::kNo));
305 REPORTER_ASSERT(reporter, !proxy);
306 }
307 }
308 }
309 }
310}
311
robertphillips76948d42016-05-04 12:47:41 -0700312#endif