blob: edf54f6113ac0ff1e50aace909bd91b50bc1e8ad [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"
Robert Phillipsf95b1752017-08-31 08:56:07 -040018#include "GrSurfaceProxyPriv.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
robertphillips76948d42016-05-04 12:47:41 -070066 if (SkBackingFit::kExact == fit) {
67 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
68 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
69 } else {
70 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050071 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070072 }
73 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
74
Brian Salomon7c8460e2017-05-12 11:36:10 -040075 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070076 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
77 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070078 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070079}
80
81static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050082 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070083 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050084 SkBackingFit fit,
85 bool wasWrapped) {
86 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040087
88 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
89 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070090
Robert Phillips294870f2016-11-11 12:38:40 -050091 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
92 if (wasWrapped) {
93 // Wrapped resources share their uniqueID with the wrapping TextureProxy
94 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
95 } else {
96 // Deferred resources should always have a different ID from their instantiated texture
97 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
98 }
99
robertphillips76948d42016-05-04 12:47:41 -0700100 if (SkBackingFit::kExact == fit) {
101 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
102 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
103 } else {
104 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
105 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
106 }
107 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
108}
109
110
robertphillips8abb3702016-08-31 14:04:06 -0700111DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500112 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400113 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700114
Robert Phillips294870f2016-11-11 12:38:40 -0500115 const GrGpuResource::UniqueID kInvalidResourceID = GrGpuResource::UniqueID::InvalidID();
116
Robert Phillips6520a692017-02-01 09:20:00 -0500117 int attempt = 0; // useful for debugging
118
robertphillips76948d42016-05-04 12:47:41 -0700119 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500120 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500121 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400122 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700123 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
124 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500125 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700126 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400127 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700128 desc.fOrigin = origin;
129 desc.fWidth = widthHeight;
130 desc.fHeight = widthHeight;
131 desc.fConfig = config;
132 desc.fSampleCnt = numSamples;
133
Robert Phillips6520a692017-02-01 09:20:00 -0500134 {
135 sk_sp<GrTexture> tex;
136 if (SkBackingFit::kApprox == fit) {
Robert Phillips67d52cf2017-06-05 13:38:13 -0400137 tex = provider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500138 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400139 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500140 }
141
Robert Phillips2f493142017-03-02 18:18:38 -0500142 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips26c90e02017-03-14 14:39:29 -0400143 provider, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400144 fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500145 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
146 if (proxy) {
147 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500148 // This forces the proxy to compute and cache its
149 // pre-instantiation size guess. Later, when it is actually
150 // instantiated, it checks that the instantiated size is <= to
151 // the pre-computation. If the proxy never computed its
152 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500153 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500154
Robert Phillips2f493142017-03-02 18:18:38 -0500155 check_surface(reporter, proxy.get(), origin,
Robert Phillips40d94952017-01-30 14:15:59 -0500156 widthHeight, widthHeight, config,
157 kInvalidResourceID, budgeted);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400158 int supportedSamples = caps.getSampleCount(numSamples, config);
Robert Phillips40d94952017-01-30 14:15:59 -0500159 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500160 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400161 supportedSamples,
Robert Phillips40d94952017-01-30 14:15:59 -0500162 fit, caps.maxWindowRectangles(), false);
163 }
robertphillips76948d42016-05-04 12:47:41 -0700164 }
165
Robert Phillips84a81202016-11-04 11:59:10 -0400166 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700167
Robert Phillips6520a692017-02-01 09:20:00 -0500168 {
169 sk_sp<GrTexture> tex;
170 if (SkBackingFit::kApprox == fit) {
Robert Phillips67d52cf2017-06-05 13:38:13 -0400171 tex = provider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500172 } else {
Robert Phillipse78b7252017-04-06 07:59:41 -0400173 tex = provider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500174 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500175
Robert Phillips2f493142017-03-02 18:18:38 -0500176 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
Robert Phillips26c90e02017-03-14 14:39:29 -0400177 desc,
178 fit,
179 budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500180 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
181 if (proxy) {
Robert Phillips6520a692017-02-01 09:20:00 -0500182 // This forces the proxy to compute and cache its pre-instantiation
183 // size guess. Later, when it is actually instantiated, it checks
184 // that the instantiated size is <= to the pre-computation.
185 // If the proxy never computed its pre-instantiation size then the
186 // check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500187 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500188
Robert Phillips2f493142017-03-02 18:18:38 -0500189 check_surface(reporter, proxy.get(), origin,
Robert Phillips6520a692017-02-01 09:20:00 -0500190 widthHeight, widthHeight, config,
191 kInvalidResourceID, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500192 check_texture(reporter, provider, proxy->asTextureProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500193 fit, false);
194 }
Robert Phillips40d94952017-01-30 14:15:59 -0500195 }
Robert Phillips6520a692017-02-01 09:20:00 -0500196
197 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700198 }
199 }
200 }
201 }
202 }
203 }
204}
205
egdanielab527a52016-06-28 08:07:26 -0700206DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500207 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700208 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700209
210 static const int kWidthHeight = 100;
211
212 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
213 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
214 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
215 for (auto numSamples: { 0, 4}) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400216 int supportedNumSamples = caps.getSampleCount(numSamples, config);
Robert Phillips78de2122017-04-26 07:44:26 -0400217
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;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400225 desc.fSampleCnt = supportedNumSamples;
robertphillips76948d42016-05-04 12:47:41 -0700226
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(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400235 provider->wrapBackendRenderTarget(backendRT));
csmartdaltonf9635992016-08-10 11:09:07 -0700236
Robert Phillips066f0202017-07-25 10:16:35 -0400237 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO,
238 origin));
Robert Phillips37430132016-11-09 06:50:43 -0500239 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400240 kWidthHeight, kWidthHeight, config,
241 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500242 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400243 supportedNumSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700244 }
245
robertphillips76948d42016-05-04 12:47:41 -0700246 sk_sp<GrTexture> tex;
247
csmartdaltonf9635992016-08-10 11:09:07 -0700248 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700249 if (renderable) {
250 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse78b7252017-04-06 07:59:41 -0400251 tex = provider->createTexture(desc, budgeted);
Robert Phillipsd9971c02017-08-02 11:51:43 -0400252 if (!tex) {
253 continue; // This can fail on Mesa
254 }
robertphillips76948d42016-05-04 12:47:41 -0700255 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
256
Robert Phillips066f0202017-07-25 10:16:35 -0400257 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500258 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(),
Greg Danield62f5542017-07-20 09:11:41 -0400262 supportedNumSamples, 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 Phillips066f0202017-07-25 10:16:35 -0400272 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500273 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