blob: 3a46438e791c202ac6af4787c455808ca343ebb3 [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"
Greg Daniel2a303902018-02-20 10:25:54 -050023#include "SkGr.h"
robertphillips76948d42016-05-04 12:47:41 -070024
robertphillips8abb3702016-08-31 14:04:06 -070025// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070026static void check_surface(skiatest::Reporter* reporter,
27 GrSurfaceProxy* proxy,
28 GrSurfaceOrigin origin,
Greg Danielbcf612b2017-05-01 13:50:58 +000029 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070030 GrPixelConfig config,
Robert Phillipsabacf092016-11-02 10:23:32 -040031 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070032 REPORTER_ASSERT(reporter, proxy->origin() == origin);
33 REPORTER_ASSERT(reporter, proxy->width() == width);
34 REPORTER_ASSERT(reporter, proxy->height() == height);
35 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips0bd24dc2018-01-16 08:06:32 -050036 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
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,
Greg Daniel2a303902018-02-20 10:25:54 -050046 int expectedMaxWindowRects) {
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);
Greg Daniel2a303902018-02-20 10:25:54 -050055 // Deferred resources should always have a different ID from their instantiated rendertarget
56 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
Robert Phillips294870f2016-11-11 12:38:40 -050057
robertphillips76948d42016-05-04 12:47:41 -070058 if (SkBackingFit::kExact == fit) {
59 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
60 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
61 } else {
62 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050063 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070064 }
65 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
66
Brian Salomon7c8460e2017-05-12 11:36:10 -040067 REPORTER_ASSERT(reporter, rt->fsaaType() == rtProxy->fsaaType());
robertphillips76948d42016-05-04 12:47:41 -070068 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
69 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070070 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070071}
72
73static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050074 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070075 GrTextureProxy* texProxy,
Greg Daniel2a303902018-02-20 10:25:54 -050076 SkBackingFit fit) {
Robert Phillips294870f2016-11-11 12:38:40 -050077 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
Robert Phillipseee4d6e2017-06-05 09:26:07 -040078
79 REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
80 GrTexture* tex = texProxy->priv().peekTexture();
robertphillips76948d42016-05-04 12:47:41 -070081
Robert Phillips294870f2016-11-11 12:38:40 -050082 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
Greg Daniel2a303902018-02-20 10:25:54 -050083 // Deferred resources should always have a different ID from their instantiated texture
84 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
Robert Phillips294870f2016-11-11 12:38:40 -050085
robertphillips76948d42016-05-04 12:47:41 -070086 if (SkBackingFit::kExact == fit) {
87 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
88 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
89 } else {
90 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
91 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
92 }
93 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
94}
95
96
robertphillips8abb3702016-08-31 14:04:06 -070097DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050098 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050099 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400100 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700101
Robert Phillips6520a692017-02-01 09:20:00 -0500102 int attempt = 0; // useful for debugging
103
robertphillips76948d42016-05-04 12:47:41 -0700104 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500105 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500106 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400107 kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700108 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
109 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500110 for (auto numSamples : {1, 4, 16, 128}) {
robertphillips76948d42016-05-04 12:47:41 -0700111 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400112 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700113 desc.fOrigin = origin;
114 desc.fWidth = widthHeight;
115 desc.fHeight = widthHeight;
116 desc.fConfig = config;
117 desc.fSampleCnt = numSamples;
118
Robert Phillips6520a692017-02-01 09:20:00 -0500119 {
120 sk_sp<GrTexture> tex;
121 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500122 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500123 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500124 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500125 }
126
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500127 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
128 desc, fit, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500129 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
130 if (proxy) {
131 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500132 // This forces the proxy to compute and cache its
133 // pre-instantiation size guess. Later, when it is actually
134 // instantiated, it checks that the instantiated size is <= to
135 // the pre-computation. If the proxy never computed its
136 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500137 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500138
Robert Phillips2f493142017-03-02 18:18:38 -0500139 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500140 widthHeight, widthHeight, config, budgeted);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 int supportedSamples =
142 caps.getRenderTargetSampleCount(numSamples, config);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500143 check_rendertarget(reporter, caps, resourceProvider,
Robert Phillips2f493142017-03-02 18:18:38 -0500144 proxy->asRenderTargetProxy(),
Greg Daniel81e7bf82017-07-19 14:47:42 -0400145 supportedSamples,
Greg Daniel2a303902018-02-20 10:25:54 -0500146 fit, caps.maxWindowRectangles());
Robert Phillips40d94952017-01-30 14:15:59 -0500147 }
robertphillips76948d42016-05-04 12:47:41 -0700148 }
149
Robert Phillips84a81202016-11-04 11:59:10 -0400150 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700151
Robert Phillips6520a692017-02-01 09:20:00 -0500152 {
153 sk_sp<GrTexture> tex;
154 if (SkBackingFit::kApprox == fit) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500155 tex = resourceProvider->createApproxTexture(desc, 0);
Robert Phillips6520a692017-02-01 09:20:00 -0500156 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157 tex = resourceProvider->createTexture(desc, budgeted);
Robert Phillips6520a692017-02-01 09:20:00 -0500158 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500159
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500160 sk_sp<GrTextureProxy> proxy(proxyProvider->createProxy(
161 desc, fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500162 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
163 if (proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164 // This forces the proxy to compute and cache its
165 // pre-instantiation size guess. Later, when it is actually
166 // instantiated, it checks that the instantiated size is <= to
167 // the pre-computation. If the proxy never computed its
168 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500169 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500170
Robert Phillips2f493142017-03-02 18:18:38 -0500171 check_surface(reporter, proxy.get(), origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500172 widthHeight, widthHeight, config, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500173 check_texture(reporter, resourceProvider,
Greg Daniel2a303902018-02-20 10:25:54 -0500174 proxy->asTextureProxy(), fit);
Robert Phillips6520a692017-02-01 09:20:00 -0500175 }
Robert Phillips40d94952017-01-30 14:15:59 -0500176 }
Robert Phillips6520a692017-02-01 09:20:00 -0500177
178 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700179 }
180 }
181 }
182 }
183 }
184 }
185}
186
egdanielab527a52016-06-28 08:07:26 -0700187DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500188 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
Greg Daniel2a303902018-02-20 10:25:54 -0500190 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
csmartdaltonf9635992016-08-10 11:09:07 -0700191 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700192
193 static const int kWidthHeight = 100;
194
Greg Daniel2a303902018-02-20 10:25:54 -0500195 if (kOpenGL_GrBackend != ctxInfo.backend()) {
196 return;
197 }
robertphillips76948d42016-05-04 12:47:41 -0700198 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Greg Daniel2a303902018-02-20 10:25:54 -0500199 for (auto colorType : { kAlpha_8_SkColorType, kRGBA_8888_SkColorType }) {
200 for (auto numSamples : {1, 4}) {
201 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, caps);
202 int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
robertphillips76948d42016-05-04 12:47:41 -0700203
Greg Daniel2a303902018-02-20 10:25:54 -0500204 if (!supportedNumSamples) {
205 continue;
206 }
robertphillips76948d42016-05-04 12:47:41 -0700207
Greg Daniel2a303902018-02-20 10:25:54 -0500208 // External on-screen render target.
209 {
210 GrGLFramebufferInfo fboInfo;
211 fboInfo.fFBOID = 0;
212 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
213 config, fboInfo);
csmartdaltonf9635992016-08-10 11:09:07 -0700214
Greg Daniel2a303902018-02-20 10:25:54 -0500215 sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
216 backendRT, origin));
217 check_surface(reporter, sProxy.get(), origin,
218 kWidthHeight, kWidthHeight,
219 backendRT.testingOnly_getPixelConfig(), SkBudgeted::kNo);
220 check_rendertarget(reporter, caps, resourceProvider,
221 sProxy->asRenderTargetProxy(),
222 supportedNumSamples, SkBackingFit::kExact, 0);
223 }
224
225 {
226 GrBackendTexture backendTex =
227 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
228 kWidthHeight, colorType, true,
229 GrMipMapped::kNo);
230
231 sk_sp<GrSurfaceProxy> sProxy =
232 proxyProvider->createWrappedTextureProxy(backendTex, origin,
233 supportedNumSamples);
234 if (!sProxy) {
235 continue; // This can fail on Mesa
csmartdaltonf9635992016-08-10 11:09:07 -0700236 }
237
Greg Daniel2a303902018-02-20 10:25:54 -0500238 check_surface(reporter, sProxy.get(), origin,
239 kWidthHeight, kWidthHeight,
240 backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
241 check_rendertarget(reporter, caps, resourceProvider,
242 sProxy->asRenderTargetProxy(),
243 supportedNumSamples, SkBackingFit::kExact,
244 caps.maxWindowRectangles());
robertphillips76948d42016-05-04 12:47:41 -0700245
Greg Daniel2a303902018-02-20 10:25:54 -0500246 gpu->deleteTestingOnlyBackendTexture(&backendTex);
247 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500248
Greg Daniel2a303902018-02-20 10:25:54 -0500249 {
250 // Internal offscreen texture
251 GrBackendTexture backendTex =
252 gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
253 kWidthHeight, colorType, false,
254 GrMipMapped::kNo);
robertphillips76948d42016-05-04 12:47:41 -0700255
Greg Daniel2a303902018-02-20 10:25:54 -0500256 sk_sp<GrSurfaceProxy> sProxy =
257 proxyProvider->createWrappedTextureProxy(backendTex, origin,
258 kBorrow_GrWrapOwnership,
259 nullptr, nullptr);
260 if (!sProxy) {
261 continue;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500262 }
Greg Daniel2a303902018-02-20 10:25:54 -0500263
264 check_surface(reporter, sProxy.get(), origin,
265 kWidthHeight, kWidthHeight,
266 backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
267 check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
268 SkBackingFit::kExact);
269
270 gpu->deleteTestingOnlyBackendTexture(&backendTex);
robertphillips76948d42016-05-04 12:47:41 -0700271 }
272 }
273 }
274 }
275}
276
Robert Phillips78de2122017-04-26 07:44:26 -0400277DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ZeroSizedProxyTest, reporter, ctxInfo) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500278 GrProxyProvider* provider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips78de2122017-04-26 07:44:26 -0400279
280 for (auto flags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags }) {
281 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
282 for (int width : { 0, 100 }) {
283 for (int height : { 0, 100}) {
284 if (width && height) {
285 continue; // not zero-sized
286 }
287
288 GrSurfaceDesc desc;
289 desc.fFlags = flags;
290 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
291 desc.fWidth = width;
292 desc.fHeight = height;
293 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500294 desc.fSampleCnt = 1;
Robert Phillips78de2122017-04-26 07:44:26 -0400295
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500296 sk_sp<GrTextureProxy> proxy = provider->createProxy(desc, fit, SkBudgeted::kNo);
Robert Phillips78de2122017-04-26 07:44:26 -0400297 REPORTER_ASSERT(reporter, !proxy);
298 }
299 }
300 }
301 }
302}
303
robertphillips76948d42016-05-04 12:47:41 -0700304#endif