blob: 50af7f70ec5cb570b37be77c8938cc75609a5f2e [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 Phillipsc7635fa2016-10-28 13:25:24 -040013#include "GrRenderTargetPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070014#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
16#include "GrSurfaceProxy.h"
17#include "GrTextureProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070018
robertphillips8abb3702016-08-31 14:04:06 -070019// Check that the surface proxy's member vars are set as expected
robertphillips76948d42016-05-04 12:47:41 -070020static void check_surface(skiatest::Reporter* reporter,
21 GrSurfaceProxy* proxy,
22 GrSurfaceOrigin origin,
23 int width, int height,
robertphillips8abb3702016-08-31 14:04:06 -070024 GrPixelConfig config,
Robert Phillips294870f2016-11-11 12:38:40 -050025 const GrGpuResource::UniqueID& uniqueID,
Robert Phillipsabacf092016-11-02 10:23:32 -040026 SkBudgeted budgeted) {
robertphillips76948d42016-05-04 12:47:41 -070027 REPORTER_ASSERT(reporter, proxy->origin() == origin);
28 REPORTER_ASSERT(reporter, proxy->width() == width);
29 REPORTER_ASSERT(reporter, proxy->height() == height);
30 REPORTER_ASSERT(reporter, proxy->config() == config);
Robert Phillips294870f2016-11-11 12:38:40 -050031 if (!uniqueID.isInvalid()) {
32 REPORTER_ASSERT(reporter, proxy->uniqueID().asUInt() == uniqueID.asUInt());
33 } else {
34 REPORTER_ASSERT(reporter, !proxy->uniqueID().isInvalid());
robertphillips8abb3702016-08-31 14:04:06 -070035 }
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();
robertphillips76948d42016-05-04 12:47:41 -070051 GrRenderTarget* rt = rtProxy->instantiate(provider);
52 REPORTER_ASSERT(reporter, rt);
53
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 REPORTER_ASSERT(reporter, rt->origin() == rtProxy->origin());
64 if (SkBackingFit::kExact == fit) {
65 REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
66 REPORTER_ASSERT(reporter, rt->height() == rtProxy->height());
67 } else {
68 REPORTER_ASSERT(reporter, rt->width() >= rtProxy->width());
Robert Phillips294870f2016-11-11 12:38:40 -050069 REPORTER_ASSERT(reporter, rt->height() >= rtProxy->height());
robertphillips76948d42016-05-04 12:47:41 -070070 }
71 REPORTER_ASSERT(reporter, rt->config() == rtProxy->config());
72
73 REPORTER_ASSERT(reporter, rt->isUnifiedMultisampled() == rtProxy->isUnifiedMultisampled());
74 REPORTER_ASSERT(reporter, rt->isStencilBufferMultisampled() ==
75 rtProxy->isStencilBufferMultisampled());
76 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->isMixedSampled() == rtProxy->isMixedSampled());
79 REPORTER_ASSERT(reporter, rt->renderTargetPriv().flags() == rtProxy->testingOnly_getFlags());
robertphillips76948d42016-05-04 12:47:41 -070080}
81
82static void check_texture(skiatest::Reporter* reporter,
Brian Osman32342f02017-03-04 08:12:46 -050083 GrResourceProvider* provider,
robertphillips76948d42016-05-04 12:47:41 -070084 GrTextureProxy* texProxy,
Robert Phillips294870f2016-11-11 12:38:40 -050085 SkBackingFit fit,
86 bool wasWrapped) {
87 GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
robertphillips76948d42016-05-04 12:47:41 -070088 GrTexture* tex = texProxy->instantiate(provider);
89 REPORTER_ASSERT(reporter, tex);
90
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 REPORTER_ASSERT(reporter, tex->origin() == texProxy->origin());
101 if (SkBackingFit::kExact == fit) {
102 REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
103 REPORTER_ASSERT(reporter, tex->height() == texProxy->height());
104 } else {
105 REPORTER_ASSERT(reporter, tex->width() >= texProxy->width());
106 REPORTER_ASSERT(reporter, tex->height() >= texProxy->height());
107 }
108 REPORTER_ASSERT(reporter, tex->config() == texProxy->config());
109}
110
111
robertphillips8abb3702016-08-31 14:04:06 -0700112DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500113 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
Robert Phillipsabacf092016-11-02 10:23:32 -0400114 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700115
Robert Phillips294870f2016-11-11 12:38:40 -0500116 const GrGpuResource::UniqueID kInvalidResourceID = GrGpuResource::UniqueID::InvalidID();
117
Robert Phillips6520a692017-02-01 09:20:00 -0500118 int attempt = 0; // useful for debugging
119
robertphillips76948d42016-05-04 12:47:41 -0700120 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Robert Phillips40d94952017-01-30 14:15:59 -0500121 for (auto widthHeight : { 100, 128, 1048576 }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500122 for (auto config : { kAlpha_8_GrPixelConfig, kRGB_565_GrPixelConfig,
123 kETC1_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
robertphillips76948d42016-05-04 12:47:41 -0700124 for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
125 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
Robert Phillips6520a692017-02-01 09:20:00 -0500126 for (auto numSamples : { 0, 4, 16, 128 }) {
robertphillips76948d42016-05-04 12:47:41 -0700127 GrSurfaceDesc desc;
Robert Phillips84a81202016-11-04 11:59:10 -0400128 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -0700129 desc.fOrigin = origin;
130 desc.fWidth = widthHeight;
131 desc.fHeight = widthHeight;
132 desc.fConfig = config;
133 desc.fSampleCnt = numSamples;
134
Robert Phillips6520a692017-02-01 09:20:00 -0500135 {
136 sk_sp<GrTexture> tex;
137 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500138 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500139 } else {
Robert Phillips5c446572017-02-01 10:53:20 -0500140 tex.reset(provider->createTexture(desc, budgeted));
Robert Phillips6520a692017-02-01 09:20:00 -0500141 }
142
Robert Phillips2f493142017-03-02 18:18:38 -0500143 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(
Robert Phillips7928e762017-02-28 16:30:28 -0500144 provider,
Robert Phillips40d94952017-01-30 14:15:59 -0500145 caps, desc,
Robert Phillipsabacf092016-11-02 10:23:32 -0400146 fit, budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500147 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
148 if (proxy) {
149 REPORTER_ASSERT(reporter, proxy->asRenderTargetProxy());
Robert Phillips40d94952017-01-30 14:15:59 -0500150 // This forces the proxy to compute and cache its
151 // pre-instantiation size guess. Later, when it is actually
152 // instantiated, it checks that the instantiated size is <= to
153 // the pre-computation. If the proxy never computed its
154 // pre-instantiation size then the check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500155 proxy->gpuMemorySize();
Robert Phillipsb4460882016-11-17 14:43:51 -0500156
Robert Phillips2f493142017-03-02 18:18:38 -0500157 check_surface(reporter, proxy.get(), origin,
Robert Phillips40d94952017-01-30 14:15:59 -0500158 widthHeight, widthHeight, config,
159 kInvalidResourceID, budgeted);
160 check_rendertarget(reporter, caps, provider,
Robert Phillips2f493142017-03-02 18:18:38 -0500161 proxy->asRenderTargetProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500162 SkTMin(numSamples, caps.maxSampleCount()),
Robert Phillips40d94952017-01-30 14:15:59 -0500163 fit, caps.maxWindowRectangles(), false);
164 }
robertphillips76948d42016-05-04 12:47:41 -0700165 }
166
Robert Phillips84a81202016-11-04 11:59:10 -0400167 desc.fFlags = kNone_GrSurfaceFlags;
robertphillips76948d42016-05-04 12:47:41 -0700168
Robert Phillips6520a692017-02-01 09:20:00 -0500169 {
170 sk_sp<GrTexture> tex;
171 if (SkBackingFit::kApprox == fit) {
Brian Osman32342f02017-03-04 08:12:46 -0500172 tex.reset(provider->createApproxTexture(desc, 0));
Robert Phillips6520a692017-02-01 09:20:00 -0500173 } else {
Robert Phillips5c446572017-02-01 10:53:20 -0500174 tex.reset(provider->createTexture(desc, budgeted));
Robert Phillips6520a692017-02-01 09:20:00 -0500175 }
Robert Phillipsb4460882016-11-17 14:43:51 -0500176
Robert Phillips2f493142017-03-02 18:18:38 -0500177 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(provider,
Robert Phillips7928e762017-02-28 16:30:28 -0500178 caps,
Robert Phillips6520a692017-02-01 09:20:00 -0500179 desc,
180 fit,
181 budgeted));
Robert Phillips2f493142017-03-02 18:18:38 -0500182 REPORTER_ASSERT(reporter, SkToBool(tex) == SkToBool(proxy));
183 if (proxy) {
Robert Phillips6520a692017-02-01 09:20:00 -0500184 // This forces the proxy to compute and cache its pre-instantiation
185 // size guess. Later, when it is actually instantiated, it checks
186 // that the instantiated size is <= to the pre-computation.
187 // If the proxy never computed its pre-instantiation size then the
188 // check is skipped.
Robert Phillips2f493142017-03-02 18:18:38 -0500189 proxy->gpuMemorySize();
Robert Phillips6520a692017-02-01 09:20:00 -0500190
Robert Phillips2f493142017-03-02 18:18:38 -0500191 check_surface(reporter, proxy.get(), origin,
Robert Phillips6520a692017-02-01 09:20:00 -0500192 widthHeight, widthHeight, config,
193 kInvalidResourceID, budgeted);
Robert Phillips2f493142017-03-02 18:18:38 -0500194 check_texture(reporter, provider, proxy->asTextureProxy(),
Robert Phillips6520a692017-02-01 09:20:00 -0500195 fit, false);
196 }
Robert Phillips40d94952017-01-30 14:15:59 -0500197 }
Robert Phillips6520a692017-02-01 09:20:00 -0500198
199 attempt++;
robertphillips76948d42016-05-04 12:47:41 -0700200 }
201 }
202 }
203 }
204 }
205 }
206}
207
egdanielab527a52016-06-28 08:07:26 -0700208DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -0500209 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
csmartdaltonf9635992016-08-10 11:09:07 -0700210 const GrCaps& caps = *ctxInfo.grContext()->caps();
robertphillips76948d42016-05-04 12:47:41 -0700211
212 static const int kWidthHeight = 100;
213
214 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
215 for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
216 for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
217 for (auto numSamples: { 0, 4}) {
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;
225 desc.fSampleCnt = numSamples;
226
csmartdaltonf9635992016-08-10 11:09:07 -0700227 // External on-screen render target.
228 if (renderable && kOpenGL_GrBackend == ctxInfo.backend()) {
229 GrBackendRenderTargetDesc backendDesc;
230 backendDesc.fWidth = kWidthHeight;
231 backendDesc.fHeight = kWidthHeight;
232 backendDesc.fConfig = config;
233 backendDesc.fOrigin = origin;
234 backendDesc.fSampleCnt = numSamples;
235 backendDesc.fStencilBits = 8;
236 backendDesc.fRenderTargetHandle = 0;
237
csmartdaltonf9635992016-08-10 11:09:07 -0700238 sk_sp<GrRenderTarget> defaultFBO(
Robert Phillipsabacf092016-11-02 10:23:32 -0400239 provider->wrapBackendRenderTarget(backendDesc));
csmartdaltonf9635992016-08-10 11:09:07 -0700240
Robert Phillips37430132016-11-09 06:50:43 -0500241 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(defaultFBO));
242 check_surface(reporter, sProxy.get(), origin,
Robert Phillipsabacf092016-11-02 10:23:32 -0400243 kWidthHeight, kWidthHeight, config,
244 defaultFBO->uniqueID(), SkBudgeted::kNo);
Robert Phillipsec2249f2016-11-09 08:54:35 -0500245 check_rendertarget(reporter, caps, provider, sProxy->asRenderTargetProxy(),
Robert Phillips294870f2016-11-11 12:38:40 -0500246 numSamples, SkBackingFit::kExact, 0, true);
csmartdaltonf9635992016-08-10 11:09:07 -0700247 }
248
robertphillips76948d42016-05-04 12:47:41 -0700249 sk_sp<GrTexture> tex;
250
csmartdaltonf9635992016-08-10 11:09:07 -0700251 // Internal offscreen render target.
robertphillips76948d42016-05-04 12:47:41 -0700252 if (renderable) {
253 desc.fFlags = kRenderTarget_GrSurfaceFlag;
254 tex.reset(provider->createTexture(desc, budgeted));
255 sk_sp<GrRenderTarget> rt(sk_ref_sp(tex->asRenderTarget()));
256
Robert Phillips37430132016-11-09 06:50:43 -0500257 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(rt));
258 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(),
262 numSamples, 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;
269 tex.reset(provider->createTexture(desc, budgeted));
270 }
271
Robert Phillips37430132016-11-09 06:50:43 -0500272 sk_sp<GrSurfaceProxy> sProxy(GrSurfaceProxy::MakeWrapped(tex));
273 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
283#endif