blob: 7b3600f8005d95642251ed65e042f74aed128ea6 [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
Brian Salomon7c8460e2017-05-12 11:36:10 -040074 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070075 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
76 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070077 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070078}
79
80static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050081 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070082 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050083 SkBackingFit fit,
84 bool wasWrapped) {
85 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070086 GrTexture* tex = texProxy->instantiate(provider);
87 REPORTER_ASSERT(reporter, tex);
88
Robert Phillips294870f2016-11-11 12:38:40 -050089 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
90 if (wasWrapped) {
91 // Wrapped resources share their uniqueID with the wrapping TextureProxy
92 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
93 } else {
94 // Deferred resources should always have a different ID from their instantiated texture
95 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
96 }
97
robertphillips76948d42016-05-04 12:47:41 -070098 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
99 if (SkBackingFit::kExact == fit) {
100 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
101 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
102 } else {
103 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
104 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
105 }
106 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
107}
108
109
robertphillips8abb3702016-08-31 14:04:06 -0700110DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500111 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400112 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700113
Robert Phillips294870f2016-11-11 12:38:40 -0500114 const GrGpuResource::UniqueID kInvalidResourceID = GrGpuResource::UniqueID::InvalidID();
115
Robert Phillips6520a692017-02-01 09:20:00 -0500116 int attempt = 0; // useful for debugging
117
robertphillips76948d42016-05-04 12:47:41 -0700118 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500119 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500120 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
121 kETC1_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700122 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
123 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500124 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700125 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400126 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700127 desc.fOrigin = origin;
128 desc.fWidth = widthHeight;
129 desc.fHeight = widthHeight;
130 desc.fConfig = config;
131 desc.fSampleCnt = numSamples;
132
Robert Phillips6520a692017-02-01 09:20:00 -0500133 {
134 sk_sp<GrTexture> tex;
135 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500136 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500137 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400138 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500139 }
140
Robert Phillips2f493142017-03-02 18:18:38 -0500141 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips26c90e02017-03-14 14:39:29 -0400142 provider, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400143 fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500144 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
145 if (proxy) {
146 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500147 // This forces the proxy to compute and cache its
148 // pre-instantiation size guess. Later, when it is actually
149 // instantiated, it checks that the instantiated size is <= to
150 // the pre-computation. If the proxy never computed its
151 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500152 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500153
Robert Phillips2f493142017-03-02 18:18:38 -0500154 check_surface(reporter, proxy.get(), origin,
Robert Phillips40d94952017-01-30 14:15:59 -0500155 widthHeight, widthHeight, config,
156 kInvalidResourceID, budgeted);
157 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500158 proxy->asRenderTargetProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500159 SkTMin(numSamples, caps.maxSampleCount()),
Robert Phillips40d94952017-01-30 14:15:59 -0500160 fit, caps.maxWindowRectangles(), false);
161 }
robertphillips76948d42016-05-04 12:47:41 -0700162 }
163
Robert Phillips84a81202016-11-04 11:59:10 -0400164 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700165
Robert Phillips6520a692017-02-01 09:20:00 -0500166 {
167 sk_sp<GrTexture> tex;
168 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500169 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500170 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400171 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500172 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500173
Robert Phillips2f493142017-03-02 18:18:38 -0500174 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
Robert Phillips26c90e02017-03-14 14:39:29 -0400175 desc,
176 fit,
177 budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500178 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
179 if (proxy) {
Robert Phillips6520a692017-02-01 09:20:00 -0500180 // This forces the proxy to compute and cache its pre-instantiation
181 // size guess. Later, when it is actually instantiated, it checks
182 // that the instantiated size is <= to the pre-computation.
183 // If the proxy never computed its pre-instantiation size then the
184 // check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500185 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500186
Robert Phillips2f493142017-03-02 18:18:38 -0500187 check_surface(reporter, proxy.get(), origin,
Robert Phillips6520a692017-02-01 09:20:00 -0500188 widthHeight, widthHeight, config,
189 kInvalidResourceID, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500190 check_texture(reporter, provider, proxy->asTextureProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500191 fit, false);
192 }
Robert Phillips40d94952017-01-30 14:15:59 -0500193 }
Robert Phillips6520a692017-02-01 09:20:00 -0500194
195 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700196 }
197 }
198 }
199 }
200 }
201 }
202}
203
egdanielab527a52016-06-28 08:07:26 -0700204DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500205 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700206 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700207
208 static const int kWidthHeight = 100;
209
210 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
211 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
212 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
213 for (auto numSamples: { 0, 4}) {
Robert Phillips78de2122017-04-26 07:44:26 -0400214 if (caps.maxSampleCount() < numSamples) {
215 continue;
216 }
217
csmartdaltonf9635992016-08-10 11:09:07 -0700218 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700219
220 GrSurfaceDesc desc;
221 desc.fOrigin = origin;
222 desc.fWidth = kWidthHeight;
223 desc.fHeight = kWidthHeight;
224 desc.fConfig = config;
225 desc.fSampleCnt = numSamples;
226
csmartdaltonf9635992016-08-10 11:09:07 -0700227 // External on-screen render target.
228 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000229 GrGLFramebufferInfo fboInfo;
230 fboInfo.fFBOID = 0;
231 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
232 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700233
csmartdaltonf9635992016-08-10 11:09:07 -0700234 sk_sp<GrRenderTarget> defaultFBO(
Greg Danielbcf612b2017-05-01 13:50:58 +0000235 provider->wrapBackendRenderTarget(backendRT, origin));
csmartdaltonf9635992016-08-10 11:09:07 -0700236
Robert Phillips37430132016-11-09 06:50:43 -0500237 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO));
238 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400239 kWidthHeight, kWidthHeight, config,
240 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500241 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500242 numSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700243 }
244
robertphillips76948d42016-05-04 12:47:41 -0700245 sk_sp<GrTexture> tex;
246
csmartdaltonf9635992016-08-10 11:09:07 -0700247 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700248 if (renderable) {
249 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400250 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700251 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
252
Robert Phillips37430132016-11-09 06:50:43 -0500253 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt));
254 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400255 kWidthHeight, kWidthHeight, config,
256 rt->uniqueID(), budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500257 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
258 numSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500259 caps.maxWindowRectangles(), true);
robertphillips76948d42016-05-04 12:47:41 -0700260 }
261
262 if (!tex) {
263 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
264 desc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -0400265 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700266 }
267
Robert Phillips37430132016-11-09 06:50:43 -0500268 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex));
269 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400270 kWidthHeight, kWidthHeight, config, tex->uniqueID(), budgeted);
Robert Phillips37430132016-11-09 06:50:43 -0500271 check_texture(reporter, provider, sProxy->asTextureProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500272 SkBackingFit::kExact, true);
robertphillips76948d42016-05-04 12:47:41 -0700273 }
274 }
275 }
276 }
277}
278
Robert Phillips78de2122017-04-26 07:44:26 -0400279DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
280 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
281
282 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
283 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
284 for (int width : { 0, 100 }) {
285 for (int height : { 0, 100}) {
286 if (width && height) {
287 continue; // not zero-sized
288 }
289
290 GrSurfaceDesc desc;
291 desc.fFlags = flags;
292 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
293 desc.fWidth = width;
294 desc.fHeight = height;
295 desc.fConfig = kRGBA_8888_GrPixelConfig;
296 desc.fSampleCnt = 0;
297
298 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
299 desc,
300 fit,
301 SkBudgeted::kNo));
302 REPORTER_ASSERT(reporter, !proxy);
303 }
304 }
305 }
306 }
307}
308
robertphillips76948d42016-05-04 12:47:41 -0700309#endif