blob: dff230c9bb602489976a6aebf553af6232b1f07a [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();
Robert Phillips6be756b2018-01-16 15:07:54 -0500110 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().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 }) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500121 for (auto numSamples : {1, 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);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500152 int supportedSamples =
153 caps.getRenderTargetSampleCount(numSamples, config);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500154 check_rendertarget(reporter, caps, resourceProvider,
Robert Phillips2f493142017-03-02 18:18:38 -0500155 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400156 supportedSamples,
Robert Phillips40d94952017-01-30 14:15:59 -0500157 fit, caps.maxWindowRectangles(), false);
158 }
robertphillips76948d42016-05-04 12:47:41 -0700159 }
160
Robert Phillips84a81202016-11-04 11:59:10 -0400161 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700162
Robert Phillips6520a692017-02-01 09:20:00 -0500163 {
164 sk_sp<GrTexture> tex;
165 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500166 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500167 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500168 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500169 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500170
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500171 sk_sp<GrTextureProxy> proxy(proxyProvider->createProxy(
172 desc, fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500173 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
174 if (proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500175 // This forces the proxy to compute and cache its
176 // pre-instantiation size guess. Later, when it is actually
177 // instantiated, it checks that the instantiated size is <= to
178 // the pre-computation. If the proxy never computed its
179 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500180 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500181
Robert Phillips2f493142017-03-02 18:18:38 -0500182 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500183 widthHeight, widthHeight, config, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500184 check_texture(reporter, resourceProvider,
185 proxy->asTextureProxy(), fit, false);
Robert Phillips6520a692017-02-01 09:20:00 -0500186 }
Robert Phillips40d94952017-01-30 14:15:59 -0500187 }
Robert Phillips6520a692017-02-01 09:20:00 -0500188
189 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700190 }
191 }
192 }
193 }
194 }
195 }
196}
197
egdanielab527a52016-06-28 08:07:26 -0700198DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500199 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500200 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700201 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700202
203 static const int kWidthHeight = 100;
204
205 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
206 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
207 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500208 for (auto numSamples : {1, 4}) {
209 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
robertphillips76948d42016-05-04 12:47:41 -0700210
211 GrSurfaceDesc desc;
212 desc.fOrigin = origin;
213 desc.fWidth = kWidthHeight;
214 desc.fHeight = kWidthHeight;
215 desc.fConfig = config;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400216 desc.fSampleCnt = supportedNumSamples;
robertphillips76948d42016-05-04 12:47:41 -0700217
csmartdaltonf9635992016-08-10 11:09:07 -0700218 // External on-screen render target.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500219 if (supportedNumSamples && kOpenGL_GrBackend == ctxInfo.backend()) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000220 GrGLFramebufferInfo fboInfo;
221 fboInfo.fFBOID = 0;
222 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
223 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700224
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500225 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
226 backendRT, origin));
Robert Phillips37430132016-11-09 06:50:43 -0500227 check_surface(reporter, sProxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500228 kWidthHeight, kWidthHeight, config, SkBudgeted::kNo);
Robert Phillips6be756b2018-01-16 15:07:54 -0500229 check_rendertarget(reporter, caps, resourceProvider,
230 sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400231 supportedNumSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700232 }
233
Brian Salomonbdecacf2018-02-02 20:32:49 -0500234 if (supportedNumSamples) {
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 Phillips6be756b2018-01-16 15:07:54 -0500246 check_rendertarget(reporter, caps, resourceProvider,
247 sProxy->asRenderTargetProxy(),
Greg Danield62f5542017-07-20 09:11:41 -0400248 supportedNumSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500249 caps.maxWindowRectangles(), true);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500250 } else {
251 // Internal offscreen texture
robertphillips76948d42016-05-04 12:47:41 -0700252 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
Brian Salomonbdecacf2018-02-02 20:32:49 -0500253 desc.fSampleCnt = 1;
robertphillips76948d42016-05-04 12:47:41 -0700254
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500255 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->createInstantiatedProxy(
256 desc, SkBackingFit::kExact, budgeted);
257 if (!sProxy) {
258 continue;
259 }
260
261 check_surface(reporter, sProxy.get(), origin,
262 kWidthHeight, kWidthHeight, config, budgeted);
Robert Phillips6be756b2018-01-16 15:07:54 -0500263 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500264 SkBackingFit::kExact, true);
265 }
robertphillips76948d42016-05-04 12:47:41 -0700266 }
267 }
268 }
269 }
270}
271
Robert Phillips78de2122017-04-26 07:44:26 -0400272DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500273 GrProxyProvider* provider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips78de2122017-04-26 07:44:26 -0400274
275 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
276 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
277 for (int width : { 0, 100 }) {
278 for (int height : { 0, 100}) {
279 if (width && height) {
280 continue; // not zero-sized
281 }
282
283 GrSurfaceDesc desc;
284 desc.fFlags = flags;
285 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
286 desc.fWidth = width;
287 desc.fHeight = height;
288 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500289 desc.fSampleCnt = 1;
Robert Phillips78de2122017-04-26 07:44:26 -0400290
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500291 sk_sp<GrTextureProxy> proxy = provider->createProxy(desc, fit, SkBudgeted::kNo);
Robert Phillips78de2122017-04-26 07:44:26 -0400292 REPORTER_ASSERT(reporter, !proxy);
293 }
294 }
295 }
296 }
297}
298
robertphillips76948d42016-05-04 12:47:41 -0700299#endif