blob: 1b65585c7ab0a2364c684d21340f0d8948826f5f [file] [log] [blame]
Chris Dalton706a6ff2017-11-29 22:01:06 -07001/*
2 * Copyright 2017 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#include "Test.h"
9
Chris Dalton706a6ff2017-11-29 22:01:06 -070010#include "GrClip.h"
11#include "GrContextPriv.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040012#include "GrMemoryPool.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070013#include "GrOnFlushResourceProvider.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040014#include "GrProxyProvider.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070015#include "GrRenderTargetContext.h"
16#include "GrRenderTargetContextPriv.h"
17#include "GrSurfaceProxy.h"
Greg Daniel94a6ce82018-01-16 16:14:41 -050018#include "GrSurfaceProxyPriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070019#include "GrTexture.h"
20#include "GrTextureProxy.h"
21#include "GrTextureProxyPriv.h"
22#include "SkMakeUnique.h"
Mike Reed274218e2018-01-08 15:05:02 -050023#include "SkRectPriv.h"
Greg Daniel4684f822018-03-08 15:27:36 -050024#include "mock/GrMockGpu.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070025#include "mock/GrMockTypes.h"
26
27// This test verifies that lazy proxy callbacks get invoked during flush, after onFlush callbacks,
28// but before Ops are executed. It also ensures that lazy proxy callbacks are invoked both for
29// regular Ops and for clips.
30class LazyProxyTest final : public GrOnFlushCallbackObject {
31public:
32 LazyProxyTest(skiatest::Reporter* reporter)
33 : fReporter(reporter)
34 , fHasOpTexture(false)
35 , fHasClipTexture(false) {
36 }
37
38 ~LazyProxyTest() override {
39 REPORTER_ASSERT(fReporter, fHasOpTexture);
40 REPORTER_ASSERT(fReporter, fHasClipTexture);
41 }
42
43 void preFlush(GrOnFlushResourceProvider*, const uint32_t*, int,
44 SkTArray<sk_sp<GrRenderTargetContext>>*) override {
45 REPORTER_ASSERT(fReporter, !fHasOpTexture);
46 REPORTER_ASSERT(fReporter, !fHasClipTexture);
47 }
48
49 void postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, int numOpListIDs) override {
50 REPORTER_ASSERT(fReporter, fHasOpTexture);
51 REPORTER_ASSERT(fReporter, fHasClipTexture);
52 }
53
54 class Op final : public GrDrawOp {
55 public:
56 DEFINE_OP_CLASS_ID
57
Robert Phillips88a32ef2018-06-07 11:05:56 -040058 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
59 GrProxyProvider* proxyProvider,
60 LazyProxyTest* test,
61 bool nullTexture) {
Robert Phillipsc994a932018-06-19 13:09:54 -040062 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
63
64 return pool->allocate<Op>(proxyProvider, test, nullTexture);
Robert Phillips88a32ef2018-06-07 11:05:56 -040065 }
66
67 void visitProxies(const VisitProxyFunc& func) const override {
68 func(fProxy.get());
69 }
70
71 void onExecute(GrOpFlushState*) override {
72 REPORTER_ASSERT(fTest->fReporter, fTest->fHasOpTexture);
73 REPORTER_ASSERT(fTest->fReporter, fTest->fHasClipTexture);
74 }
75
76 private:
Robert Phillips7c525e62018-06-12 10:11:12 -040077 friend class GrOpMemoryPool; // for ctor
78
Robert Phillips777707b2018-01-17 11:40:14 -050079 Op(GrProxyProvider* proxyProvider, LazyProxyTest* test, bool nullTexture)
80 : GrDrawOp(ClassID()), fTest(test) {
Chris Dalton4c458b12018-06-16 17:22:59 -060081 fProxy = GrProxyProvider::MakeFullyLazyProxy(
82 [this, nullTexture](GrResourceProvider* rp) {
83 if (!rp) {
84 return sk_sp<GrTexture>();
85 }
86 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasOpTexture);
87 fTest->fHasOpTexture = true;
88 if (nullTexture) {
89 return sk_sp<GrTexture>();
90 } else {
91 GrSurfaceDesc desc;
92 desc.fWidth = 1234;
93 desc.fHeight = 567;
94 desc.fConfig = kRGB_565_GrPixelConfig;
95 sk_sp<GrTexture> texture = rp->createTexture(desc, SkBudgeted::kYes);
96 REPORTER_ASSERT(fTest->fReporter, texture);
97 return texture;
98 }
99 },
100 GrProxyProvider::Renderable::kNo, kTopLeft_GrSurfaceOrigin,
101 kRGB_565_GrPixelConfig, *proxyProvider->caps());
102
103 this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo,
104 GrOp::IsZeroArea::kNo);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700105 }
106
Chris Dalton706a6ff2017-11-29 22:01:06 -0700107 const char* name() const override { return "LazyProxyTest::Op"; }
108 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400109 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700110 return RequiresDstTexture::kNo;
111 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700112 void onPrepare(GrOpFlushState*) override {}
113
114 LazyProxyTest* const fTest;
115 sk_sp<GrTextureProxy> fProxy;
116 };
117
118 class ClipFP : public GrFragmentProcessor {
119 public:
Robert Phillips777707b2018-01-17 11:40:14 -0500120 ClipFP(GrProxyProvider* proxyProvider, LazyProxyTest* test, GrTextureProxy* atlas)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700121 : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags)
Robert Phillips777707b2018-01-17 11:40:14 -0500122 , fProxyProvider(proxyProvider)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700123 , fTest(test)
124 , fAtlas(atlas) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600125 fLazyProxy = GrProxyProvider::MakeFullyLazyProxy(
Robert Phillipsce5209a2018-02-13 11:13:51 -0500126 [this](GrResourceProvider* rp) {
127 if (!rp) {
128 return sk_sp<GrTexture>();
129 }
130 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture);
131 fTest->fHasClipTexture = true;
132 fAtlas->instantiate(rp);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400133 return sk_ref_sp(fAtlas->peekTexture());
Robert Phillipsce5209a2018-02-13 11:13:51 -0500134 },
135 GrProxyProvider::Renderable::kYes,
136 kBottomLeft_GrSurfaceOrigin,
Chris Dalton4c458b12018-06-16 17:22:59 -0600137 kAlpha_half_GrPixelConfig, *proxyProvider->caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700138 fAccess.reset(fLazyProxy, GrSamplerState::Filter::kNearest,
Brian Salomone782f842018-07-31 13:53:11 -0400139 GrSamplerState::WrapMode::kClamp);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400140 this->setTextureSamplerCnt(1);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700141 }
142
143 private:
144 const char* name() const override { return "LazyProxyTest::ClipFP"; }
145 std::unique_ptr<GrFragmentProcessor> clone() const override {
Robert Phillips777707b2018-01-17 11:40:14 -0500146 return skstd::make_unique<ClipFP>(fProxyProvider, fTest, fAtlas);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700147 }
148 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return nullptr; }
149 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
150 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400151 const TextureSampler& onTextureSampler(int) const override { return fAccess; }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700152
Robert Phillips777707b2018-01-17 11:40:14 -0500153 GrProxyProvider* const fProxyProvider;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700154 LazyProxyTest* const fTest;
155 GrTextureProxy* const fAtlas;
156 sk_sp<GrTextureProxy> fLazyProxy;
157 TextureSampler fAccess;
158 };
159
160
161 class Clip : public GrClip {
162 public:
163 Clip(LazyProxyTest* test, GrTextureProxy* atlas)
164 : fTest(test)
165 , fAtlas(atlas) {}
166
167 private:
Robert Phillips777707b2018-01-17 11:40:14 -0500168 bool apply(GrContext* context, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
Chris Dalton706a6ff2017-11-29 22:01:06 -0700169 SkRect* bounds) const override {
Robert Phillips777707b2018-01-17 11:40:14 -0500170 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
171 out->addCoverageFP(skstd::make_unique<ClipFP>(proxyProvider, fTest, fAtlas));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700172 return true;
173 }
174 bool quickContains(const SkRect&) const final { return false; }
175 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
176 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
177 rect->set(0, 0, width, height);
178 if (iior) {
179 *iior = false;
180 }
181 }
182
183 LazyProxyTest* const fTest;
184 GrTextureProxy* fAtlas;
185 };
186
187private:
188 skiatest::Reporter* fReporter;
189 bool fHasOpTexture;
190 bool fHasClipTexture;
191};
192
193DEF_GPUTEST(LazyProxyTest, reporter, /* options */) {
194 GrMockOptions mockOptions;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500195 mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fRenderability =
196 GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700197 mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fTexturable = true;
198 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips777707b2018-01-17 11:40:14 -0500199 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700200 for (bool nullTexture : {false, true}) {
201 LazyProxyTest test(reporter);
202 ctx->contextPriv().addOnFlushCallbackObject(&test);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500203 sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
204 SkBackingFit::kExact, 100, 100,
205 kRGBA_8888_GrPixelConfig, nullptr);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700206 REPORTER_ASSERT(reporter, rtc);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500207 sk_sp<GrRenderTargetContext> mockAtlas = ctx->contextPriv().makeDeferredRenderTargetContext(
208 SkBackingFit::kExact, 10, 10,
Chris Dalton706a6ff2017-11-29 22:01:06 -0700209 kAlpha_half_GrPixelConfig, nullptr);
210 REPORTER_ASSERT(reporter, mockAtlas);
211 rtc->priv().testingOnly_addDrawOp(LazyProxyTest::Clip(&test, mockAtlas->asTextureProxy()),
Robert Phillips88a32ef2018-06-07 11:05:56 -0400212 LazyProxyTest::Op::Make(ctx.get(), proxyProvider, &test, nullTexture));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700213 ctx->contextPriv().testingOnly_flushAndRemoveOnFlushCallbackObject(&test);
214 }
215}
216
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500217static const int kSize = 16;
218
Greg Daniel94a6ce82018-01-16 16:14:41 -0500219DEF_GPUTEST(LazyProxyReleaseTest, reporter, /* options */) {
220 GrMockOptions mockOptions;
221 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips777707b2018-01-17 11:40:14 -0500222 auto proxyProvider = ctx->contextPriv().proxyProvider();
Greg Daniel94a6ce82018-01-16 16:14:41 -0500223
224 GrSurfaceDesc desc;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500225 desc.fWidth = kSize;
226 desc.fHeight = kSize;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500227 desc.fConfig = kRGBA_8888_GrPixelConfig;
228
Greg Daniel457469c2018-02-08 15:05:44 -0500229 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500230 for (bool doInstantiate : {true, false}) {
Greg Daniel457469c2018-02-08 15:05:44 -0500231 for (auto lazyType : {LazyInstantiationType::kSingleUse,
Greg Daniela8d92112018-03-09 12:05:04 -0500232 LazyInstantiationType::kMultipleUse,
233 LazyInstantiationType::kUninstantiate}) {
Greg Daniel457469c2018-02-08 15:05:44 -0500234 int testCount = 0;
235 int* testCountPtr = &testCount;
236 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Robert Phillipsce5209a2018-02-13 11:13:51 -0500237 [testCountPtr](GrResourceProvider* resourceProvider) {
Greg Daniel457469c2018-02-08 15:05:44 -0500238 if (!resourceProvider) {
239 *testCountPtr = -1;
240 return sk_sp<GrTexture>();
241 }
242 *testCountPtr = 1;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500243 return sk_sp<GrTexture>();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500244 },
Brian Salomon7226c232018-07-30 13:13:17 -0400245 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrTextureType::k2D,
246 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, SkBudgeted::kNo, lazyType);
Greg Daniel94a6ce82018-01-16 16:14:41 -0500247
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400248 REPORTER_ASSERT(reporter, proxy.get());
Greg Daniel457469c2018-02-08 15:05:44 -0500249 REPORTER_ASSERT(reporter, 0 == testCount);
250
251 if (doInstantiate) {
252 proxy->priv().doLazyInstantiation(ctx->contextPriv().resourceProvider());
253 if (LazyInstantiationType::kSingleUse == proxy->priv().lazyInstantiationType()) {
254 // In SingleUse we will call the cleanup and delete the callback in the
255 // doLazyInstantiationCall.
256 REPORTER_ASSERT(reporter, -1 == testCount);
257 } else {
258 REPORTER_ASSERT(reporter, 1 == testCount);
259 }
260 proxy.reset();
261 REPORTER_ASSERT(reporter, -1 == testCount);
262 } else {
263 proxy.reset();
264 REPORTER_ASSERT(reporter, -1 == testCount);
265 }
Greg Daniel94a6ce82018-01-16 16:14:41 -0500266 }
267 }
268}
269
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500270class LazyFailedInstantiationTestOp : public GrDrawOp {
271public:
272 DEFINE_OP_CLASS_ID
273
Robert Phillips88a32ef2018-06-07 11:05:56 -0400274 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
275 GrProxyProvider* proxyProvider,
276 int* testExecuteValue,
277 bool shouldFailInstantiation) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400278 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
279
280 return pool->allocate<LazyFailedInstantiationTestOp>(proxyProvider,
281 testExecuteValue,
282 shouldFailInstantiation);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400283 }
284
285 void visitProxies(const VisitProxyFunc& func) const override {
286 func(fLazyProxy.get());
287 }
288
289private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400290 friend class GrOpMemoryPool; // for ctor
291
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500292 LazyFailedInstantiationTestOp(GrProxyProvider* proxyProvider, int* testExecuteValue,
293 bool shouldFailInstantiation)
294 : INHERITED(ClassID())
295 , fTestExecuteValue(testExecuteValue) {
296 GrSurfaceDesc desc;
297 desc.fWidth = kSize;
298 desc.fHeight = kSize;
299 desc.fConfig = kRGBA_8888_GrPixelConfig;
300
301 fLazyProxy = proxyProvider->createLazyProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500302 [testExecuteValue, shouldFailInstantiation, desc](GrResourceProvider* rp) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500303 if (!rp) {
304 return sk_sp<GrTexture>();
305 }
306 if (shouldFailInstantiation) {
307 *testExecuteValue = 1;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500308 return sk_sp<GrTexture>();
309 }
310 return rp->createTexture(desc, SkBudgeted::kNo);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500311 },
Brian Salomon7226c232018-07-30 13:13:17 -0400312 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrTextureType::k2D,
313 SkBackingFit::kExact, SkBudgeted::kNo);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500314
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400315 SkASSERT(fLazyProxy.get());
316
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500317 this->setBounds(SkRect::MakeIWH(kSize, kSize),
318 HasAABloat::kNo, IsZeroArea::kNo);
319 }
320
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500321 const char* name() const override { return "LazyFailedInstantiationTestOp"; }
322 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400323 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500324 return RequiresDstTexture::kNo;
325 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500326 void onPrepare(GrOpFlushState*) override {}
327 void onExecute(GrOpFlushState* state) override {
328 *fTestExecuteValue = 2;
329 }
330
331 int* fTestExecuteValue;
332 sk_sp<GrSurfaceProxy> fLazyProxy;
333
334 typedef GrDrawOp INHERITED;
335};
336
337// Test that when a lazy proxy fails to instantiate during flush that we drop the Op that it was
338// associated with.
339DEF_GPUTEST(LazyProxyFailedInstantiationTest, reporter, /* options */) {
340 GrMockOptions mockOptions;
341 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips4150eea2018-02-07 17:08:21 -0500342 GrResourceProvider* resourceProvider = ctx->contextPriv().resourceProvider();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500343 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
344 for (bool failInstantiation : {false, true}) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500345 sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
346 SkBackingFit::kExact, 100, 100,
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500347 kRGBA_8888_GrPixelConfig, nullptr);
348 REPORTER_ASSERT(reporter, rtc);
349
350 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
351
352 int executeTestValue = 0;
Robert Phillips88a32ef2018-06-07 11:05:56 -0400353 rtc->priv().testingOnly_addDrawOp(LazyFailedInstantiationTestOp::Make(
354 ctx.get(), proxyProvider, &executeTestValue, failInstantiation));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500355 ctx->flush();
356
357 if (failInstantiation) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500358 if (resourceProvider->explicitlyAllocateGPUResources()) {
359 REPORTER_ASSERT(reporter, 1 == executeTestValue);
360 } else {
361 // When we disable explicit gpu resource allocation we don't throw away ops that
362 // have uninstantiated proxies.
363 REPORTER_ASSERT(reporter, 2 == executeTestValue);
364 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500365 } else {
366 REPORTER_ASSERT(reporter, 2 == executeTestValue);
367 }
368 }
Greg Daniel4684f822018-03-08 15:27:36 -0500369}
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500370
Greg Daniel4684f822018-03-08 15:27:36 -0500371class LazyUninstantiateTestOp : public GrDrawOp {
372public:
373 DEFINE_OP_CLASS_ID
374
Robert Phillips88a32ef2018-06-07 11:05:56 -0400375 static std::unique_ptr<GrDrawOp> Make(GrContext* context, sk_sp<GrTextureProxy> proxy) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400376 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
377
378 return pool->allocate<LazyUninstantiateTestOp>(std::move(proxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500379 }
380
381 void visitProxies(const VisitProxyFunc& func) const override {
382 func(fLazyProxy.get());
383 }
384
385private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400386 friend class GrOpMemoryPool; // for ctor
387
Robert Phillips88a32ef2018-06-07 11:05:56 -0400388 LazyUninstantiateTestOp(sk_sp<GrTextureProxy> proxy)
389 : INHERITED(ClassID())
390 , fLazyProxy(std::move(proxy)) {
391 this->setBounds(SkRect::MakeIWH(kSize, kSize), HasAABloat::kNo, IsZeroArea::kNo);
392 }
393
Greg Daniel4684f822018-03-08 15:27:36 -0500394 const char* name() const override { return "LazyUninstantiateTestOp"; }
395 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400396 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Greg Daniel4684f822018-03-08 15:27:36 -0500397 return RequiresDstTexture::kNo;
398 }
Greg Daniel4684f822018-03-08 15:27:36 -0500399 void onPrepare(GrOpFlushState*) override {}
400 void onExecute(GrOpFlushState* state) override {}
401
402 sk_sp<GrSurfaceProxy> fLazyProxy;
403
404 typedef GrDrawOp INHERITED;
405};
406
407static void UninstantiateReleaseProc(void* releaseValue) {
408 (*static_cast<int*>(releaseValue))++;
409}
410
411// Test that lazy proxies with the Uninstantiate LazyCallbackType are uninstantiated and released as
412// expected.
413DEF_GPUTEST(LazyProxyUninstantiateTest, reporter, /* options */) {
414 GrMockOptions mockOptions;
415 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
416 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
417 GrGpu* gpu = ctx->contextPriv().getGpu();
418
419 using LazyType = GrSurfaceProxy::LazyInstantiationType;
420 for (auto lazyType : {LazyType::kSingleUse, LazyType::kMultipleUse, LazyType::kUninstantiate}) {
421 sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
422 SkBackingFit::kExact, 100, 100,
423 kRGBA_8888_GrPixelConfig, nullptr);
424 REPORTER_ASSERT(reporter, rtc);
425
426 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
427
428 int instantiateTestValue = 0;
429 int releaseTestValue = 0;
430 int* instantiatePtr = &instantiateTestValue;
431 int* releasePtr = &releaseTestValue;
432 GrSurfaceDesc desc;
433 desc.fWidth = kSize;
434 desc.fHeight = kSize;
435 desc.fConfig = kRGBA_8888_GrPixelConfig;
436
437 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
438 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
439
440 sk_sp<GrTextureProxy> lazyProxy = proxyProvider->createLazyProxy(
441 [instantiatePtr, releasePtr, backendTex](GrResourceProvider* rp) {
442 if (!rp) {
443 return sk_sp<GrTexture>();
444 }
445
446 sk_sp<GrTexture> texture = rp->wrapBackendTexture(backendTex);
447 if (!texture) {
448 return sk_sp<GrTexture>();
449 }
450 (*instantiatePtr)++;
451 texture->setRelease(UninstantiateReleaseProc, releasePtr);
452 return texture;
453 },
Brian Salomon7226c232018-07-30 13:13:17 -0400454 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrTextureType::k2D,
455 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, SkBudgeted::kNo, lazyType);
Greg Daniel4684f822018-03-08 15:27:36 -0500456
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400457 REPORTER_ASSERT(reporter, lazyProxy.get());
458
Robert Phillips88a32ef2018-06-07 11:05:56 -0400459 rtc->priv().testingOnly_addDrawOp(LazyUninstantiateTestOp::Make(ctx.get(), lazyProxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500460
461 ctx->flush();
462
463 REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
464 if (LazyType::kUninstantiate == lazyType) {
465 REPORTER_ASSERT(reporter, 1 == releaseTestValue);
466 } else {
467 REPORTER_ASSERT(reporter, 0 == releaseTestValue);
468 }
469
470 // This should cause the uninstantiate proxies to be instantiated again but have no effect
471 // on the others
Robert Phillips88a32ef2018-06-07 11:05:56 -0400472 rtc->priv().testingOnly_addDrawOp(LazyUninstantiateTestOp::Make(ctx.get(), lazyProxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500473 // Add a second op to make sure we only instantiate once.
Robert Phillips88a32ef2018-06-07 11:05:56 -0400474 rtc->priv().testingOnly_addDrawOp(LazyUninstantiateTestOp::Make(ctx.get(), lazyProxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500475 ctx->flush();
476
477 if (LazyType::kUninstantiate == lazyType) {
478 REPORTER_ASSERT(reporter, 2 == instantiateTestValue);
479 REPORTER_ASSERT(reporter, 2 == releaseTestValue);
480 } else {
481 REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
482 REPORTER_ASSERT(reporter, 0 == releaseTestValue);
483 }
484
485 lazyProxy.reset();
486 if (LazyType::kUninstantiate == lazyType) {
487 REPORTER_ASSERT(reporter, 2 == releaseTestValue);
488 } else {
489 REPORTER_ASSERT(reporter, 1 == releaseTestValue);
490 }
491
Brian Salomon26102cb2018-03-09 09:33:19 -0500492 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel4684f822018-03-08 15:27:36 -0500493 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500494}