blob: ef22dfa87627aba5c6301e28f11be59b6a2834f5 [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
13#include "GrSurfaceProxy.h"
14#include "GrTextureProxy.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040015#include "GrRenderTargetPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070016#include "GrRenderTargetProxy.h"
17
robertphillips8abb3702016-08-31 14:04:06 -070018// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070019static void check_surface(skiatest::Reporter* reporter,
20 GrSurfaceProxy* proxy,
21 GrSurfaceOrigin origin,
22 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070023 GrPixelConfig config,
Robert Phillips294870f2016-11-11 12:38:40 -050024 const GrGpuResource::UniqueID& uniqueID,
Robert Phillipsabacf092016-11-02 10:23:32 -040025 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070026 REPORTER_ASSERT(reporter, proxy->origin() == origin);
27 REPORTER_ASSERT(reporter, proxy->width() == width);
28 REPORTER_ASSERT(reporter, proxy->height() == height);
29 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips294870f2016-11-11 12:38:40 -050030 if (!uniqueID.isInvalid()) {
31 REPORTER_ASSERT(reporter, proxy->uniqueID().asUInt() == uniqueID.asUInt());
32 } else {
33 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
robertphillips8abb3702016-08-31 14:04:06 -070034 }
Robert Phillipsabacf092016-11-02 10:23:32 -040035 REPORTER_ASSERT(reporter, proxy->isBudgeted() == budgeted);
robertphillips76948d42016-05-04 12:47:41 -070036}
37
38static void check_rendertarget(skiatest::Reporter* reporter,
Robert Phillipsec2249f2016-11-09 08:54:35 -050039 const GrCaps& caps,
robertphillips76948d42016-05-04 12:47:41 -070040 GrTextureProvider* provider,
41 GrRenderTargetProxy* rtProxy,
Robert Phillipsabacf092016-11-02 10:23:32 -040042 int numSamples,
Robert Phillipsec2249f2016-11-09 08:54:35 -050043 SkBackingFit fit,
Robert Phillips294870f2016-11-11 12:38:40 -050044 int expectedMaxWindowRects,
45 bool wasWrapped) {
Robert Phillipsec2249f2016-11-09 08:54:35 -050046 REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
Robert Phillipsabacf092016-11-02 10:23:32 -040047 REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
48
Robert Phillips294870f2016-11-11 12:38:40 -050049 GrSurfaceProxy::UniqueID idBefore = rtProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070050 GrRenderTarget* rt = rtProxy->instantiate(provider);
51 REPORTER_ASSERT(reporter, rt);
52
Robert Phillips294870f2016-11-11 12:38:40 -050053 REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
54 if (wasWrapped) {
55 // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
56 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
57 } else {
58 // Deferred resources should always have a different ID from their instantiated rendertarget
59 REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
60 }
61
robertphillips76948d42016-05-04 12:47:41 -070062 REPORTER_ASSERT(reporter, rt->origin() == rtProxy->origin());
63 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
72 REPORTER_ASSERT(reporter, rt->isUnifiedMultisampled() == rtProxy->isUnifiedMultisampled());
73 REPORTER_ASSERT(reporter, rt->isStencilBufferMultisampled() ==
74 rtProxy->isStencilBufferMultisampled());
75 REPORTER_ASSERT(reporter, rt->numColorSamples() == rtProxy->numColorSamples());
76 REPORTER_ASSERT(reporter, rt->numStencilSamples() == rtProxy->numStencilSamples());
csmartdaltonf9635992016-08-10 11:09:07 -070077 REPORTER_ASSERT(reporter, rt->isMixedSampled() == rtProxy->isMixedSampled());
78 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070079}
80
81static void check_texture(skiatest::Reporter* reporter,
82 GrTextureProvider* provider,
83 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050084 SkBackingFit fit,
85 bool wasWrapped) {
86 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070087 GrTexture* tex = texProxy->instantiate(provider);
88 REPORTER_ASSERT(reporter, tex);
89
Robert Phillips294870f2016-11-11 12:38:40 -050090 REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
91 if (wasWrapped) {
92 // Wrapped resources share their uniqueID with the wrapping TextureProxy
93 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
94 } else {
95 // Deferred resources should always have a different ID from their instantiated texture
96 REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
97 }
98
robertphillips76948d42016-05-04 12:47:41 -070099 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
100 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) {
bsalomon8b7451a2016-05-11 06:33:06 -0700112 GrTextureProvider* provider = ctxInfo.grContext()->textureProvider();
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,
122 kETC1_GrPixelConfig, 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 Phillips5c446572017-02-01 10:53:20 -0500137 tex.reset(provider->createApproxTexture(desc));
Robert Phillips6520a692017-02-01 09:20:00 -0500138 } else {
Robert Phillips5c446572017-02-01 10:53:20 -0500139 tex.reset(provider->createTexture(desc, budgeted));
Robert Phillips6520a692017-02-01 09:20:00 -0500140 }
141
Robert Phillips37430132016-11-09 06:50:43 -0500142 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips7928e762017-02-28 16:30:28 -0500143 provider,
Robert Phillips40d94952017-01-30 14:15:59 -0500144 caps, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400145 fit, budgeted));
Robert Phillips6520a692017-02-01 09:20:00 -0500146 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(sProxy));
Robert Phillips40d94952017-01-30 14:15:59 -0500147 if (sProxy) {
148 REPORTER_ASSERT(reporter, sProxy->asRenderTargetProxy());
149 // This forces the proxy to compute and cache its
150 // pre-instantiation size guess. Later, when it is actually
151 // instantiated, it checks that the instantiated size is <= to
152 // the pre-computation. If the proxy never computed its
153 // pre-instantiation size then the check is skipped.
154 sProxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500155
Robert Phillips40d94952017-01-30 14:15:59 -0500156 check_surface(reporter, sProxy.get(), origin,
157 widthHeight, widthHeight, config,
158 kInvalidResourceID, budgeted);
159 check_rendertarget(reporter, caps, provider,
Robert Phillips6520a692017-02-01 09:20:00 -0500160 sProxy->asRenderTargetProxy(),
161 SkTMin(numSamples, caps.maxSampleCount()),
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 Phillips5c446572017-02-01 10:53:20 -0500171 tex.reset(provider->createApproxTexture(desc));
Robert Phillips6520a692017-02-01 09:20:00 -0500172 } else {
Robert Phillips5c446572017-02-01 10:53:20 -0500173 tex.reset(provider->createTexture(desc, budgeted));
Robert Phillips6520a692017-02-01 09:20:00 -0500174 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500175
Robert Phillips7928e762017-02-28 16:30:28 -0500176 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeDeferred(provider,
177 caps,
Robert Phillips6520a692017-02-01 09:20:00 -0500178 desc,
179 fit,
180 budgeted));
181 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(sProxy));
182 if (sProxy) {
183 // This forces the proxy to compute and cache its pre-instantiation
184 // size guess. Later, when it is actually instantiated, it checks
185 // that the instantiated size is <= to the pre-computation.
186 // If the proxy never computed its pre-instantiation size then the
187 // check is skipped.
188 sProxy->gpuMemorySize();
189
190 check_surface(reporter, sProxy.get(), origin,
191 widthHeight, widthHeight, config,
192 kInvalidResourceID, budgeted);
193 check_texture(reporter, provider, sProxy->asTextureProxy(),
194 fit, false);
195 }
Robert Phillips40d94952017-01-30 14:15:59 -0500196 }
Robert Phillips6520a692017-02-01 09:20:00 -0500197
198 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700199 }
200 }
201 }
202 }
203 }
204 }
205}
206
egdanielab527a52016-06-28 08:07:26 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700208 GrTextureProvider* provider = ctxInfo.grContext()->textureProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700209 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700210
211 static const int kWidthHeight = 100;
212
213 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
214 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
215 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
216 for (auto numSamples: { 0, 4}) {
csmartdaltonf9635992016-08-10 11:09:07 -0700217 bool renderable = caps.isConfigRenderable(config, numSamples > 0);
robertphillips76948d42016-05-04 12:47:41 -0700218
219 GrSurfaceDesc desc;
220 desc.fOrigin = origin;
221 desc.fWidth = kWidthHeight;
222 desc.fHeight = kWidthHeight;
223 desc.fConfig = config;
224 desc.fSampleCnt = numSamples;
225
csmartdaltonf9635992016-08-10 11:09:07 -0700226 // External on-screen render target.
227 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
228 GrBackendRenderTargetDesc backendDesc;
229 backendDesc.fWidth = kWidthHeight;
230 backendDesc.fHeight = kWidthHeight;
231 backendDesc.fConfig = config;
232 backendDesc.fOrigin = origin;
233 backendDesc.fSampleCnt = numSamples;
234 backendDesc.fStencilBits = 8;
235 backendDesc.fRenderTargetHandle = 0;
236
csmartdaltonf9635992016-08-10 11:09:07 -0700237 sk_sp<GrRenderTarget> defaultFBO(
Robert Phillipsabacf092016-11-02 10:23:32 -0400238 provider->wrapBackendRenderTarget(backendDesc));
csmartdaltonf9635992016-08-10 11:09:07 -0700239
Robert Phillips37430132016-11-09 06:50:43 -0500240 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO));
241 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400242 kWidthHeight, kWidthHeight, config,
243 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500244 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500245 numSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700246 }
247
robertphillips76948d42016-05-04 12:47:41 -0700248 sk_sp<GrTexture> tex;
249
csmartdaltonf9635992016-08-10 11:09:07 -0700250 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700251 if (renderable) {
252 desc.fFlags = kRenderTarget_GrSurfaceFlag;
253 tex.reset(provider->createTexture(desc, budgeted));
254 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
255
Robert Phillips37430132016-11-09 06:50:43 -0500256 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt));
257 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400258 kWidthHeight, kWidthHeight, config,
259 rt->uniqueID(), budgeted);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500260 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
261 numSamples, SkBackingFit::kExact,
Robert Phillips294870f2016-11-11 12:38:40 -0500262 caps.maxWindowRectangles(), true);
robertphillips76948d42016-05-04 12:47:41 -0700263 }
264
265 if (!tex) {
266 SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
267 desc.fSampleCnt = 0;
268 tex.reset(provider->createTexture(desc, budgeted));
269 }
270
Robert Phillips37430132016-11-09 06:50:43 -0500271 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex));
272 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400273 kWidthHeight, kWidthHeight, config, tex->uniqueID(), budgeted);
Robert Phillips37430132016-11-09 06:50:43 -0500274 check_texture(reporter, provider, sProxy->asTextureProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500275 SkBackingFit::kExact, true);
robertphillips76948d42016-05-04 12:47:41 -0700276 }
277 }
278 }
279 }
280}
281
282#endif