blob: 077045f5db474cad38278f7806bc887b86ce353b [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 Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.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"
Robert Phillipsf95b1752017-08-31 08:56:07 -040020#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040021#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050022#include "GrTextureProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070023
robertphillips8abb3702016-08-31 14:04:06 -070024// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070025static void check_surface(skiatest::Reporter* reporter,
26 GrSurfaceProxy* proxy,
27 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000028 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070029 GrPixelConfig config,
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 Phillips0bd24dc2018-01-16 08:06:32 -050035 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
Robert Phillipsabacf092016-11-02 10:23:32 -040036 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
robertphillips76948d42016-05-04 12:47:41 -070037}
38
39static void check_rendertarget(skiatest::Reporter* reporter,
Robert Phillipsec2249f2016-11-09 08:54:35 -050040 const GrCaps& caps,
Brian Osman32342f02017-03-04 08:12:46 -050041 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070042 GrRenderTargetProxy* rtProxy,
Robert Phillipsabacf092016-11-02 10:23:32 -040043 int numSamples,
Robert Phillipsec2249f2016-11-09 08:54:35 -050044 SkBackingFit fit,
Robert Phillips294870f2016-11-11 12:38:40 -050045 int expectedMaxWindowRects,
46 bool wasWrapped) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050047 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040048 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
49
Robert Phillips294870f2016-11-11 12:38:40 -050050 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040051 REPORTER_ASSERT(reporter, rtProxy->instantiate(provider));
52 GrRenderTarget* rt = rtProxy->priv().peekRenderTarget();
robertphillips76948d42016-05-04 12:47:41 -070053
Robert Phillips294870f2016-11-11 12:38:40 -050054 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
55 if (wasWrapped) {
56 // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
57 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
58 } else {
59 // Deferred resources should always have a different ID from their instantiated rendertarget
60 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
61 }
62
robertphillips76948d42016-05-04 12:47:41 -070063 if (SkBackingFit::kExact == fit) {
64 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
65 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
66 } else {
67 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050068 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070069 }
70 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
71
Brian Salomon7c8460e2017-05-12 11:36:10 -040072 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070073 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
74 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070075 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070076}
77
78static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050079 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070080 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050081 SkBackingFit fit,
82 bool wasWrapped) {
83 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040084
85 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
86 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070087
Robert Phillips294870f2016-11-11 12:38:40 -050088 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
89 if (wasWrapped) {
90 // Wrapped resources share their uniqueID with the wrapping TextureProxy
91 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
92 } else {
93 // Deferred resources should always have a different ID from their instantiated texture
94 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
95 }
96
robertphillips76948d42016-05-04 12:47:41 -070097 if (SkBackingFit::kExact == fit) {
98 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
99 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
100 } else {
101 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
102 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
103 }
104 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
105}
106
107
robertphillips8abb3702016-08-31 14:04:06 -0700108DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
110 GrResourceProvider* resourceProvider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400111 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700112
Robert Phillips6520a692017-02-01 09:20:00 -0500113 int attempt = 0; // useful for debugging
114
robertphillips76948d42016-05-04 12:47:41 -0700115 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500116 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500117 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400118 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700119 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
120 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500121 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700122 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400123 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700124 desc.fOrigin = origin;
125 desc.fWidth = widthHeight;
126 desc.fHeight = widthHeight;
127 desc.fConfig = config;
128 desc.fSampleCnt = numSamples;
129
Robert Phillips6520a692017-02-01 09:20:00 -0500130 {
131 sk_sp<GrTexture> tex;
132 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500133 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500134 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500135 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500136 }
137
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500138 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
139 desc, fit, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500140 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
141 if (proxy) {
142 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500143 // This forces the proxy to compute and cache its
144 // pre-instantiation size guess. Later, when it is actually
145 // instantiated, it checks that the instantiated size is <= to
146 // the pre-computation. If the proxy never computed its
147 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500148 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500149
Robert Phillips2f493142017-03-02 18:18:38 -0500150 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500151 widthHeight, widthHeight, config, budgeted);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400152 int supportedSamples = caps.getSampleCount(numSamples, config);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500153 check_rendertarget(reporter, caps, resourceProvider,
Robert Phillips2f493142017-03-02 18:18:38 -0500154 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400155 supportedSamples,
Robert Phillips40d94952017-01-30 14:15:59 -0500156 fit, caps.maxWindowRectangles(), false);
157 }
robertphillips76948d42016-05-04 12:47:41 -0700158 }
159
Robert Phillips84a81202016-11-04 11:59:10 -0400160 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700161
Robert Phillips6520a692017-02-01 09:20:00 -0500162 {
163 sk_sp<GrTexture> tex;
164 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500166 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500167 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500168 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500169
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500170 sk_sp<GrTextureProxy> proxy(proxyProvider->createProxy(
171 desc, fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500172 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
173 if (proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500174 // This forces the proxy to compute and cache its
175 // pre-instantiation size guess. Later, when it is actually
176 // instantiated, it checks that the instantiated size is <= to
177 // the pre-computation. If the proxy never computed its
178 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500179 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500180
Robert Phillips2f493142017-03-02 18:18:38 -0500181 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182 widthHeight, widthHeight, config, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500183 check_texture(reporter, resourceProvider,
184 proxy->asTextureProxy(), fit, false);
Robert Phillips6520a692017-02-01 09:20:00 -0500185 }
Robert Phillips40d94952017-01-30 14:15:59 -0500186 }
Robert Phillips6520a692017-02-01 09:20:00 -0500187
188 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700189 }
190 }
191 }
192 }
193 }
194 }
195}
196
egdanielab527a52016-06-28 08:07:26 -0700197DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500198 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Brian Osman32342f02017-03-04 08:12:46 -0500199 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700200 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700201
202 static const int kWidthHeight = 100;
203
204 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
205 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
206 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
207 for (auto numSamples: { 0, 4}) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400208 int supportedNumSamples = caps.getSampleCount(numSamples, config);
Robert Phillips78de2122017-04-26 07:44:26 -0400209
csmartdaltonf9635992016-08-10 11:09:07 -0700210 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700211
212 GrSurfaceDesc desc;
213 desc.fOrigin = origin;
214 desc.fWidth = kWidthHeight;
215 desc.fHeight = kWidthHeight;
216 desc.fConfig = config;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400217 desc.fSampleCnt = supportedNumSamples;
robertphillips76948d42016-05-04 12:47:41 -0700218
csmartdaltonf9635992016-08-10 11:09:07 -0700219 // External on-screen render target.
220 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000221 GrGLFramebufferInfo fboInfo;
222 fboInfo.fFBOID = 0;
223 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
224 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700225
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500226 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
227 backendRT, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500228 check_surface(reporter, sProxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500229 kWidthHeight, kWidthHeight, config, SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500230 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400231 supportedNumSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700232 }
233
robertphillips76948d42016-05-04 12:47:41 -0700234 if (renderable) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500235 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700236 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700237
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500238 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->createInstantiatedProxy(
239 desc, SkBackingFit::kExact, budgeted);
240 if (!sProxy) {
241 continue; // This can fail on Mesa
242 }
243
Robert Phillips37430132016-11-09 06:50:43 -0500244 check_surface(reporter, sProxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500245 kWidthHeight, kWidthHeight, config, budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500246 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400247 supportedNumSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500248 caps.maxWindowRectangles(), true);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500249 } else {
250 // Internal offscreen texture
robertphillips76948d42016-05-04 12:47:41 -0700251 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
252 desc.fSampleCnt = 0;
robertphillips76948d42016-05-04 12:47:41 -0700253
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500254 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->createInstantiatedProxy(
255 desc, SkBackingFit::kExact, budgeted);
256 if (!sProxy) {
257 continue;
258 }
259
260 check_surface(reporter, sProxy.get(), origin,
261 kWidthHeight, kWidthHeight, config, budgeted);
262 check_texture(reporter, provider, sProxy->asTextureProxy(),
263 SkBackingFit::kExact, true);
264 }
robertphillips76948d42016-05-04 12:47:41 -0700265 }
266 }
267 }
268 }
269}
270
Robert Phillips78de2122017-04-26 07:44:26 -0400271DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500272 GrProxyProvider* provider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips78de2122017-04-26 07:44:26 -0400273
274 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
275 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
276 for (int width : { 0, 100 }) {
277 for (int height : { 0, 100}) {
278 if (width && height) {
279 continue; // not zero-sized
280 }
281
282 GrSurfaceDesc desc;
283 desc.fFlags = flags;
284 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
285 desc.fWidth = width;
286 desc.fHeight = height;
287 desc.fConfig = kRGBA_8888_GrPixelConfig;
288 desc.fSampleCnt = 0;
289
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500290 sk_sp<GrTextureProxy> proxy = provider->createProxy(desc, fit, SkBudgeted::kNo);
Robert Phillips78de2122017-04-26 07:44:26 -0400291 REPORTER_ASSERT(reporter, !proxy);
292 }
293 }
294 }
295 }
296}
297
robertphillips76948d42016-05-04 12:47:41 -0700298#endif