blob: dc34c76b66593a6873cfcc7bf3d40a1cbe1001ec [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
14#ifndef SK_DISABLE_DEFERRED_PROXIES
15
Greg Danielbcf612b2017-05-01 13:50:58 +000016#include "GrBackendSurface.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"
20#include "GrSurfaceProxy.h"
21#include "GrTextureProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070022
robertphillips8abb3702016-08-31 14:04:06 -070023// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070024static void check_surface(skiatest::Reporter* reporter,
25 GrSurfaceProxy* proxy,
26 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000027 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070028 GrPixelConfig config,
Robert Phillips294870f2016-11-11 12:38:40 -050029 const GrGpuResource::UniqueID& uniqueID,
Robert Phillipsabacf092016-11-02 10:23:32 -040030 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070031 REPORTER_ASSERT(reporter, proxy->origin() == origin);
32 REPORTER_ASSERT(reporter, proxy->width() == width);
33 REPORTER_ASSERT(reporter, proxy->height() == height);
34 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips294870f2016-11-11 12:38:40 -050035 if (!uniqueID.isInvalid()) {
36 REPORTER_ASSERT(reporter, proxy->uniqueID().asUInt() == uniqueID.asUInt());
37 } else {
38 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
robertphillips8abb3702016-08-31 14:04:06 -070039 }
Robert Phillipsabacf092016-11-02 10:23:32 -040040 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
robertphillips76948d42016-05-04 12:47:41 -070041}
42
43static void check_rendertarget(skiatest::Reporter* reporter,
Robert Phillipsec2249f2016-11-09 08:54:35 -050044 const GrCaps& caps,
Brian Osman32342f02017-03-04 08:12:46 -050045 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070046 GrRenderTargetProxy* rtProxy,
Robert Phillipsabacf092016-11-02 10:23:32 -040047 int numSamples,
Robert Phillipsec2249f2016-11-09 08:54:35 -050048 SkBackingFit fit,
Robert Phillips294870f2016-11-11 12:38:40 -050049 int expectedMaxWindowRects,
50 bool wasWrapped) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050051 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040052 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
53
Robert Phillips294870f2016-11-11 12:38:40 -050054 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040055 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
56 GrRenderTarget* rt = rtProxy->priv().peekRenderTarget();
robertphillips76948d42016-05-04 12:47:41 -070057
Robert Phillips294870f2016-11-11 12:38:40 -050058 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
59 if (wasWrapped) {
60 // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
61 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
62 } else {
63 // Deferred resources should always have a different ID from their instantiated rendertarget
64 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
65 }
66
robertphillips76948d42016-05-04 12:47:41 -070067 REPORTER_ASSERT(reporter, rt->origin() == rtProxy->origin());
68 if (SkBackingFit::kExact == fit) {
69 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
70 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
71 } else {
72 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050073 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070074 }
75 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
76
Brian Salomon7c8460e2017-05-12 11:36:10 -040077 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070078 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
79 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070080 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();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040089
90 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
91 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070092
Robert Phillips294870f2016-11-11 12:38:40 -050093 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
94 if (wasWrapped) {
95 // Wrapped resources share their uniqueID with the wrapping TextureProxy
96 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
97 } else {
98 // Deferred resources should always have a different ID from their instantiated texture
99 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
100 }
101
robertphillips76948d42016-05-04 12:47:41 -0700102 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
103 if (SkBackingFit::kExact == fit) {
104 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
105 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
106 } else {
107 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
108 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
109 }
110 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
111}
112
113
robertphillips8abb3702016-08-31 14:04:06 -0700114DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500115 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400116 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700117
Robert Phillips294870f2016-11-11 12:38:40 -0500118 const GrGpuResource::UniqueID kInvalidResourceID = GrGpuResource::UniqueID::InvalidID();
119
Robert Phillips6520a692017-02-01 09:20:00 -0500120 int attempt = 0; // useful for debugging
121
robertphillips76948d42016-05-04 12:47:41 -0700122 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500123 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500124 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400125 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700126 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
127 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500128 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700129 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400130 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700131 desc.fOrigin = origin;
132 desc.fWidth = widthHeight;
133 desc.fHeight = widthHeight;
134 desc.fConfig = config;
135 desc.fSampleCnt = numSamples;
136
Robert Phillips6520a692017-02-01 09:20:00 -0500137 {
138 sk_sp<GrTexture> tex;
139 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500140 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500141 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400142 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500143 }
144
Robert Phillips2f493142017-03-02 18:18:38 -0500145 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips26c90e02017-03-14 14:39:29 -0400146 provider, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400147 fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500148 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
149 if (proxy) {
150 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500151 // This forces the proxy to compute and cache its
152 // pre-instantiation size guess. Later, when it is actually
153 // instantiated, it checks that the instantiated size is <= to
154 // the pre-computation. If the proxy never computed its
155 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500156 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500157
Robert Phillips2f493142017-03-02 18:18:38 -0500158 check_surface(reporter, proxy.get(), origin,
Robert Phillips40d94952017-01-30 14:15:59 -0500159 widthHeight, widthHeight, config,
160 kInvalidResourceID, budgeted);
161 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500162 proxy->asRenderTargetProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500163 SkTMin(numSamples, caps.maxSampleCount()),
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) {
Brian Osman32342f02017-03-04 08:12:46 -0500173 tex.reset(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}) {
Robert Phillips78de2122017-04-26 07:44:26 -0400218 if (caps.maxSampleCount() < numSamples) {
219 continue;
220 }
221
csmartdaltonf9635992016-08-10 11:09:07 -0700222 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700223
224 GrSurfaceDesc desc;
225 desc.fOrigin = origin;
226 desc.fWidth = kWidthHeight;
227 desc.fHeight = kWidthHeight;
228 desc.fConfig = config;
229 desc.fSampleCnt = numSamples;
230
csmartdaltonf9635992016-08-10 11:09:07 -0700231 // External on-screen render target.
232 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000233 GrGLFramebufferInfo fboInfo;
234 fboInfo.fFBOID = 0;
235 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
236 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700237
csmartdaltonf9635992016-08-10 11:09:07 -0700238 sk_sp<GrRenderTarget> defaultFBO(
Greg Danielbcf612b2017-05-01 13:50:58 +0000239 provider->wrapBackendRenderTarget(backendRT, origin));
csmartdaltonf9635992016-08-10 11:09:07 -0700240
Robert Phillips37430132016-11-09 06:50:43 -0500241 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO));
242 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400243 kWidthHeight, kWidthHeight, config,
244 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500245 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500246 numSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700247 }
248
robertphillips76948d42016-05-04 12:47:41 -0700249 sk_sp<GrTexture> tex;
250
csmartdaltonf9635992016-08-10 11:09:07 -0700251 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700252 if (renderable) {
253 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400254 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700255 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
256
Robert Phillips37430132016-11-09 06:50:43 -0500257 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt));
258 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400259 kWidthHeight, kWidthHeight, config,
260 rt->uniqueID(), budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500261 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
262 numSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500263 caps.maxWindowRectangles(), true);
robertphillips76948d42016-05-04 12:47:41 -0700264 }
265
266 if (!tex) {
267 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
268 desc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -0400269 tex = provider->createTexture(desc, budgeted);
robertphillips76948d42016-05-04 12:47:41 -0700270 }
271
Robert Phillips37430132016-11-09 06:50:43 -0500272 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex));
273 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400274 kWidthHeight, kWidthHeight, config, tex->uniqueID(), budgeted);
Robert Phillips37430132016-11-09 06:50:43 -0500275 check_texture(reporter, provider, sProxy->asTextureProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500276 SkBackingFit::kExact, true);
robertphillips76948d42016-05-04 12:47:41 -0700277 }
278 }
279 }
280 }
281}
282
Robert Phillips78de2122017-04-26 07:44:26 -0400283DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
284 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
285
286 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
287 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
288 for (int width : { 0, 100 }) {
289 for (int height : { 0, 100}) {
290 if (width && height) {
291 continue; // not zero-sized
292 }
293
294 GrSurfaceDesc desc;
295 desc.fFlags = flags;
296 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
297 desc.fWidth = width;
298 desc.fHeight = height;
299 desc.fConfig = kRGBA_8888_GrPixelConfig;
300 desc.fSampleCnt = 0;
301
302 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
303 desc,
304 fit,
305 SkBudgeted::kNo));
306 REPORTER_ASSERT(reporter, !proxy);
307 }
308 }
309 }
310 }
311}
312
robertphillips76948d42016-05-04 12:47:41 -0700313#endif
Robert Phillips8bf1f9f2017-05-31 15:01:20 -0400314
315#endif