blob: 7945ec671498bd2a297cf1f1d569cfbd550f13ef [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 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
113 void onPrepare(GrOpFlushState*) override {}
114
115 LazyProxyTest* const fTest;
116 sk_sp<GrTextureProxy> fProxy;
117 };
118
119 class ClipFP : public GrFragmentProcessor {
120 public:
Robert Phillips777707b2018-01-17 11:40:14 -0500121 ClipFP(GrProxyProvider* proxyProvider, LazyProxyTest* test, GrTextureProxy* atlas)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700122 : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags)
Robert Phillips777707b2018-01-17 11:40:14 -0500123 , fProxyProvider(proxyProvider)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700124 , fTest(test)
125 , fAtlas(atlas) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600126 fLazyProxy = GrProxyProvider::MakeFullyLazyProxy(
Robert Phillipsce5209a2018-02-13 11:13:51 -0500127 [this](GrResourceProvider* rp) {
128 if (!rp) {
129 return sk_sp<GrTexture>();
130 }
131 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture);
132 fTest->fHasClipTexture = true;
133 fAtlas->instantiate(rp);
134 return sk_ref_sp(fAtlas->priv().peekTexture());
135 },
136 GrProxyProvider::Renderable::kYes,
137 kBottomLeft_GrSurfaceOrigin,
Chris Dalton4c458b12018-06-16 17:22:59 -0600138 kAlpha_half_GrPixelConfig, *proxyProvider->caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700139 fAccess.reset(fLazyProxy, GrSamplerState::Filter::kNearest,
140 GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag);
141 this->addTextureSampler(&fAccess);
142 }
143
144 private:
145 const char* name() const override { return "LazyProxyTest::ClipFP"; }
146 std::unique_ptr<GrFragmentProcessor> clone() const override {
Robert Phillips777707b2018-01-17 11:40:14 -0500147 return skstd::make_unique<ClipFP>(fProxyProvider, fTest, fAtlas);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700148 }
149 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return nullptr; }
150 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
151 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
152
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 },
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400245 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrInternalSurfaceFlags::kNone,
Greg Daniela8d92112018-03-09 12:05:04 -0500246 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 },
312 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
313 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 }
326 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
327 void onPrepare(GrOpFlushState*) override {}
328 void onExecute(GrOpFlushState* state) override {
329 *fTestExecuteValue = 2;
330 }
331
332 int* fTestExecuteValue;
333 sk_sp<GrSurfaceProxy> fLazyProxy;
334
335 typedef GrDrawOp INHERITED;
336};
337
338// Test that when a lazy proxy fails to instantiate during flush that we drop the Op that it was
339// associated with.
340DEF_GPUTEST(LazyProxyFailedInstantiationTest, reporter, /* options */) {
341 GrMockOptions mockOptions;
342 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips4150eea2018-02-07 17:08:21 -0500343 GrResourceProvider* resourceProvider = ctx->contextPriv().resourceProvider();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500344 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
345 for (bool failInstantiation : {false, true}) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500346 sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
347 SkBackingFit::kExact, 100, 100,
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500348 kRGBA_8888_GrPixelConfig, nullptr);
349 REPORTER_ASSERT(reporter, rtc);
350
351 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
352
353 int executeTestValue = 0;
Robert Phillips88a32ef2018-06-07 11:05:56 -0400354 rtc->priv().testingOnly_addDrawOp(LazyFailedInstantiationTestOp::Make(
355 ctx.get(), proxyProvider, &executeTestValue, failInstantiation));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500356 ctx->flush();
357
358 if (failInstantiation) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500359 if (resourceProvider->explicitlyAllocateGPUResources()) {
360 REPORTER_ASSERT(reporter, 1 == executeTestValue);
361 } else {
362 // When we disable explicit gpu resource allocation we don't throw away ops that
363 // have uninstantiated proxies.
364 REPORTER_ASSERT(reporter, 2 == executeTestValue);
365 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500366 } else {
367 REPORTER_ASSERT(reporter, 2 == executeTestValue);
368 }
369 }
Greg Daniel4684f822018-03-08 15:27:36 -0500370}
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500371
Greg Daniel4684f822018-03-08 15:27:36 -0500372class LazyUninstantiateTestOp : public GrDrawOp {
373public:
374 DEFINE_OP_CLASS_ID
375
Robert Phillips88a32ef2018-06-07 11:05:56 -0400376 static std::unique_ptr<GrDrawOp> Make(GrContext* context, sk_sp<GrTextureProxy> proxy) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400377 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
378
379 return pool->allocate<LazyUninstantiateTestOp>(std::move(proxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500380 }
381
382 void visitProxies(const VisitProxyFunc& func) const override {
383 func(fLazyProxy.get());
384 }
385
386private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400387 friend class GrOpMemoryPool; // for ctor
388
Robert Phillips88a32ef2018-06-07 11:05:56 -0400389 LazyUninstantiateTestOp(sk_sp<GrTextureProxy> proxy)
390 : INHERITED(ClassID())
391 , fLazyProxy(std::move(proxy)) {
392 this->setBounds(SkRect::MakeIWH(kSize, kSize), HasAABloat::kNo, IsZeroArea::kNo);
393 }
394
Greg Daniel4684f822018-03-08 15:27:36 -0500395 const char* name() const override { return "LazyUninstantiateTestOp"; }
396 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400397 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Greg Daniel4684f822018-03-08 15:27:36 -0500398 return RequiresDstTexture::kNo;
399 }
400 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
401 void onPrepare(GrOpFlushState*) override {}
402 void onExecute(GrOpFlushState* state) override {}
403
404 sk_sp<GrSurfaceProxy> fLazyProxy;
405
406 typedef GrDrawOp INHERITED;
407};
408
409static void UninstantiateReleaseProc(void* releaseValue) {
410 (*static_cast<int*>(releaseValue))++;
411}
412
413// Test that lazy proxies with the Uninstantiate LazyCallbackType are uninstantiated and released as
414// expected.
415DEF_GPUTEST(LazyProxyUninstantiateTest, reporter, /* options */) {
416 GrMockOptions mockOptions;
417 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
418 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
419 GrGpu* gpu = ctx->contextPriv().getGpu();
420
421 using LazyType = GrSurfaceProxy::LazyInstantiationType;
422 for (auto lazyType : {LazyType::kSingleUse, LazyType::kMultipleUse, LazyType::kUninstantiate}) {
423 sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
424 SkBackingFit::kExact, 100, 100,
425 kRGBA_8888_GrPixelConfig, nullptr);
426 REPORTER_ASSERT(reporter, rtc);
427
428 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
429
430 int instantiateTestValue = 0;
431 int releaseTestValue = 0;
432 int* instantiatePtr = &instantiateTestValue;
433 int* releasePtr = &releaseTestValue;
434 GrSurfaceDesc desc;
435 desc.fWidth = kSize;
436 desc.fHeight = kSize;
437 desc.fConfig = kRGBA_8888_GrPixelConfig;
438
439 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
440 nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
441
442 sk_sp<GrTextureProxy> lazyProxy = proxyProvider->createLazyProxy(
443 [instantiatePtr, releasePtr, backendTex](GrResourceProvider* rp) {
444 if (!rp) {
445 return sk_sp<GrTexture>();
446 }
447
448 sk_sp<GrTexture> texture = rp->wrapBackendTexture(backendTex);
449 if (!texture) {
450 return sk_sp<GrTexture>();
451 }
452 (*instantiatePtr)++;
453 texture->setRelease(UninstantiateReleaseProc, releasePtr);
454 return texture;
455 },
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400456 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrInternalSurfaceFlags::kNone,
Greg Daniela8d92112018-03-09 12:05:04 -0500457 SkBackingFit::kExact, SkBudgeted::kNo, lazyType);
Greg Daniel4684f822018-03-08 15:27:36 -0500458
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400459 REPORTER_ASSERT(reporter, lazyProxy.get());
460
Robert Phillips88a32ef2018-06-07 11:05:56 -0400461 rtc->priv().testingOnly_addDrawOp(LazyUninstantiateTestOp::Make(ctx.get(), lazyProxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500462
463 ctx->flush();
464
465 REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
466 if (LazyType::kUninstantiate == lazyType) {
467 REPORTER_ASSERT(reporter, 1 == releaseTestValue);
468 } else {
469 REPORTER_ASSERT(reporter, 0 == releaseTestValue);
470 }
471
472 // This should cause the uninstantiate proxies to be instantiated again but have no effect
473 // on the others
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 // Add a second op to make sure we only instantiate once.
Robert Phillips88a32ef2018-06-07 11:05:56 -0400476 rtc->priv().testingOnly_addDrawOp(LazyUninstantiateTestOp::Make(ctx.get(), lazyProxy));
Greg Daniel4684f822018-03-08 15:27:36 -0500477 ctx->flush();
478
479 if (LazyType::kUninstantiate == lazyType) {
480 REPORTER_ASSERT(reporter, 2 == instantiateTestValue);
481 REPORTER_ASSERT(reporter, 2 == releaseTestValue);
482 } else {
483 REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
484 REPORTER_ASSERT(reporter, 0 == releaseTestValue);
485 }
486
487 lazyProxy.reset();
488 if (LazyType::kUninstantiate == lazyType) {
489 REPORTER_ASSERT(reporter, 2 == releaseTestValue);
490 } else {
491 REPORTER_ASSERT(reporter, 1 == releaseTestValue);
492 }
493
Brian Salomon26102cb2018-03-09 09:33:19 -0500494 gpu->deleteTestingOnlyBackendTexture(backendTex);
Greg Daniel4684f822018-03-08 15:27:36 -0500495 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500496}