blob: bde67391319db71610662a3cd53773096d960e24 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/mock/GrMockTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkRectPriv.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrMemoryPool.h"
15#include "src/gpu/GrOnFlushResourceProvider.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRecordingContextPriv.h"
18#include "src/gpu/GrRenderTargetContext.h"
19#include "src/gpu/GrRenderTargetContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000022#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrTextureProxyPriv.h"
25#include "src/gpu/mock/GrMockGpu.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070026
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
Chris Daltonc4b47352019-08-23 10:10:36 -060043 void preFlush(GrOnFlushResourceProvider*, const uint32_t*, int) override {
Chris Dalton706a6ff2017-11-29 22:01:06 -070044 REPORTER_ASSERT(fReporter, !fHasOpTexture);
45 REPORTER_ASSERT(fReporter, !fHasClipTexture);
46 }
47
Greg Danielf41b2bd2019-08-22 16:19:24 -040048 void postFlush(GrDeferredUploadToken, const uint32_t* opsTaskIDs, int numOpsTaskIDs) override {
Chris Dalton706a6ff2017-11-29 22:01:06 -070049 REPORTER_ASSERT(fReporter, fHasOpTexture);
50 REPORTER_ASSERT(fReporter, fHasClipTexture);
51 }
52
53 class Op final : public GrDrawOp {
54 public:
55 DEFINE_OP_CLASS_ID
56
Robert Phillips6f0e02f2019-02-13 11:02:28 -050057 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips88a32ef2018-06-07 11:05:56 -040058 GrProxyProvider* proxyProvider,
59 LazyProxyTest* test,
60 bool nullTexture) {
Robert Phillips9da87e02019-02-04 13:26:26 -050061 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040062
Greg Daniel4065d452018-11-16 15:43:41 -050063 return pool->allocate<Op>(context, proxyProvider, test, nullTexture);
Robert Phillips88a32ef2018-06-07 11:05:56 -040064 }
65
Chris Dalton1706cbf2019-05-21 19:35:29 -060066 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060067 func(fProxy.get(), GrMipMapped::kNo);
Robert Phillips88a32ef2018-06-07 11:05:56 -040068 }
69
Brian Salomon588cec72018-11-14 13:56:37 -050070 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override {
Robert Phillips88a32ef2018-06-07 11:05:56 -040071 REPORTER_ASSERT(fTest->fReporter, fTest->fHasOpTexture);
72 REPORTER_ASSERT(fTest->fReporter, fTest->fHasClipTexture);
73 }
74
75 private:
Robert Phillips7c525e62018-06-12 10:11:12 -040076 friend class GrOpMemoryPool; // for ctor
77
Robert Phillips6f0e02f2019-02-13 11:02:28 -050078 Op(GrRecordingContext* ctx, GrProxyProvider* proxyProvider,
79 LazyProxyTest* test, bool nullTexture)
Robert Phillips777707b2018-01-17 11:40:14 -050080 : GrDrawOp(ClassID()), fTest(test) {
Greg Daniel4065d452018-11-16 15:43:41 -050081 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -040082 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kBGR_565,
83 GrRenderable::kNo);
Chris Dalton4c458b12018-06-16 17:22:59 -060084 fProxy = GrProxyProvider::MakeFullyLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -040085 [this, nullTexture](GrResourceProvider* rp,
86 const GrSurfaceProxy::LazySurfaceDesc& desc)
87 -> GrSurfaceProxy::LazyCallbackResult {
Chris Dalton4c458b12018-06-16 17:22:59 -060088 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasOpTexture);
89 fTest->fHasOpTexture = true;
90 if (nullTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040091 return {};
Chris Dalton4c458b12018-06-16 17:22:59 -060092 } else {
Brian Salomona56a7462020-02-07 14:17:25 -050093 static constexpr SkISize kDimensions = {1234, 567};
Brian Salomone8a766b2019-07-19 14:24:36 -040094 sk_sp<GrTexture> texture = rp->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -040095 kDimensions, desc.fFormat, desc.fRenderable, desc.fSampleCnt,
96 desc.fMipMapped, desc.fBudgeted, desc.fProtected);
Chris Dalton4c458b12018-06-16 17:22:59 -060097 REPORTER_ASSERT(fTest->fReporter, texture);
Brian Salomon9c73e3d2019-08-15 10:55:49 -040098 return texture;
Chris Dalton4c458b12018-06-16 17:22:59 -060099 }
100 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400101 format, GrRenderable::kNo, 1, GrProtected::kNo, *proxyProvider->caps(),
102 GrSurfaceProxy::UseAllocator::kYes);
Chris Dalton4c458b12018-06-16 17:22:59 -0600103
104 this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo,
Greg Daniel5faf4742019-10-01 15:14:44 -0400105 GrOp::IsHairline::kNo);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700106 }
107
Chris Dalton706a6ff2017-11-29 22:01:06 -0700108 const char* name() const override { return "LazyProxyTest::Op"; }
109 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600110 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip* clip,
111 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700112 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700113 }
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400114 void onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400115 const GrSurfaceProxyView* writeView,
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400116 GrAppliedClip*,
117 const GrXferProcessor::DstProxyView&) override {}
118
Chris Dalton706a6ff2017-11-29 22:01:06 -0700119 void onPrepare(GrOpFlushState*) override {}
120
121 LazyProxyTest* const fTest;
122 sk_sp<GrTextureProxy> fProxy;
123 };
124
125 class ClipFP : public GrFragmentProcessor {
126 public:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500127 ClipFP(GrRecordingContext* ctx, GrProxyProvider* proxyProvider, LazyProxyTest* test,
Greg Daniel4065d452018-11-16 15:43:41 -0500128 GrTextureProxy* atlas)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700129 : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500130 , fContext(ctx)
Robert Phillips777707b2018-01-17 11:40:14 -0500131 , fProxyProvider(proxyProvider)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700132 , fTest(test)
133 , fAtlas(atlas) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000134 static const GrColorType kColorType = GrColorType::kAlpha_F16;
135 static const GrSurfaceOrigin kOrigin = kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500136 const GrBackendFormat format =
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000137 ctx->priv().caps()->getDefaultBackendFormat(kColorType, GrRenderable::kYes);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500138 GrSwizzle readSwizzle = ctx->priv().caps()->getReadSwizzle(format, kColorType);
Chris Dalton4c458b12018-06-16 17:22:59 -0600139 fLazyProxy = GrProxyProvider::MakeFullyLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400140 [this](GrResourceProvider* rp, const GrSurfaceProxy::LazySurfaceDesc&)
141 -> GrSurfaceProxy::LazyCallbackResult {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400142 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture);
143 fTest->fHasClipTexture = true;
144 fAtlas->instantiate(rp);
145 return sk_ref_sp(fAtlas->peekTexture());
146 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400147 format, GrRenderable::kYes, 1, GrProtected::kNo, *proxyProvider->caps(),
148 GrSurfaceProxy::UseAllocator::kYes);
Brian Salomonf629a902020-06-17 13:05:00 -0400149 auto atlasEffect = GrTextureEffect::Make({fLazyProxy, kOrigin, readSwizzle},
150 kPremul_SkAlphaType);
Michael Ludwig9aba6252020-06-22 14:46:36 -0400151 this->registerChild(std::move(atlasEffect));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700152 }
153
154 private:
155 const char* name() const override { return "LazyProxyTest::ClipFP"; }
156 std::unique_ptr<GrFragmentProcessor> clone() const override {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500157 return std::make_unique<ClipFP>(fContext, fProxyProvider, fTest, fAtlas);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700158 }
159 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return nullptr; }
160 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
161 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
162
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500163 GrRecordingContext* const fContext;
Robert Phillips777707b2018-01-17 11:40:14 -0500164 GrProxyProvider* const fProxyProvider;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700165 LazyProxyTest* const fTest;
166 GrTextureProxy* const fAtlas;
167 sk_sp<GrTextureProxy> fLazyProxy;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700168 };
169
170
171 class Clip : public GrClip {
172 public:
173 Clip(LazyProxyTest* test, GrTextureProxy* atlas)
174 : fTest(test)
175 , fAtlas(atlas) {}
176
177 private:
Michael Ludwige06a8972020-06-11 10:29:00 -0400178 SkIRect getConservativeBounds() const final {
179 return SkIRect::MakeSize(fAtlas->dimensions());
180 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400181 Effect apply(GrRecordingContext* context, GrRenderTargetContext*, bool useHWAA,
182 bool hasUserStencilSettings, GrAppliedClip* out,
183 SkRect* bounds) const override {
Robert Phillips9da87e02019-02-04 13:26:26 -0500184 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500185 out->addCoverageFP(std::make_unique<ClipFP>(context, proxyProvider, fTest, fAtlas));
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400186 return Effect::kClipped;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700187 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700188
189 LazyProxyTest* const fTest;
190 GrTextureProxy* fAtlas;
191 };
192
193private:
194 skiatest::Reporter* fReporter;
195 bool fHasOpTexture;
196 bool fHasClipTexture;
197};
198
199DEF_GPUTEST(LazyProxyTest, reporter, /* options */) {
200 GrMockOptions mockOptions;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400201 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
Brian Salomonbdecacf2018-02-02 20:32:49 -0500202 GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400203 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
Robert Phillipsf4f80112020-07-13 16:13:31 -0400204 sk_sp<GrDirectContext> ctx = GrDirectContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500205 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700206 for (bool nullTexture : {false, true}) {
207 LazyProxyTest test(reporter);
Robert Phillips9da87e02019-02-04 13:26:26 -0500208 ctx->priv().addOnFlushCallbackObject(&test);
Greg Daniele20fcad2020-01-08 11:52:34 -0500209 auto rtc = GrRenderTargetContext::Make(
210 ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {100, 100});
Chris Dalton706a6ff2017-11-29 22:01:06 -0700211 REPORTER_ASSERT(reporter, rtc);
Greg Daniele20fcad2020-01-08 11:52:34 -0500212 auto mockAtlas = GrRenderTargetContext::Make(
213 ctx.get(), GrColorType::kAlpha_F16, nullptr, SkBackingFit::kExact, {10, 10});
Chris Dalton706a6ff2017-11-29 22:01:06 -0700214 REPORTER_ASSERT(reporter, mockAtlas);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400215 LazyProxyTest::Clip clip(&test, mockAtlas->asTextureProxy());
216 rtc->priv().testingOnly_addDrawOp(
217 &clip, LazyProxyTest::Op::Make(ctx.get(), proxyProvider, &test, nullTexture));
Robert Phillips9da87e02019-02-04 13:26:26 -0500218 ctx->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&test);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700219 }
220}
221
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500222static const int kSize = 16;
223
Greg Daniel94a6ce82018-01-16 16:14:41 -0500224DEF_GPUTEST(LazyProxyReleaseTest, reporter, /* options */) {
225 GrMockOptions mockOptions;
Robert Phillipsf4f80112020-07-13 16:13:31 -0400226 sk_sp<GrDirectContext> ctx = GrDirectContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500227 auto proxyProvider = ctx->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400228 const GrCaps* caps = ctx->priv().caps();
Greg Daniel94a6ce82018-01-16 16:14:41 -0500229
Robert Phillips0a15cc62019-07-30 12:49:10 -0400230 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
231 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500232
Brian Salomona56a7462020-02-07 14:17:25 -0500233 auto tex = ctx->priv().resourceProvider()->createTexture({kSize, kSize}, format,
234 GrRenderable::kNo, 1, GrMipMapped::kNo,
235 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400236 using LazyInstantiationResult = GrSurfaceProxy::LazyCallbackResult;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500237 for (bool doInstantiate : {true, false}) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400238 for (bool releaseCallback : {false, true}) {
Greg Daniel457469c2018-02-08 15:05:44 -0500239 int testCount = 0;
Brian Salomon67f01952019-02-14 13:05:25 -0500240 // Sets an integer to 1 when the callback is called and -1 when it is deleted.
241 class TestCallback {
242 public:
Brian Salomonbeb7f522019-08-30 16:19:42 -0400243 TestCallback(int* value, bool releaseCallback, sk_sp<GrTexture> tex)
244 : fValue(value)
245 , fReleaseCallback(releaseCallback)
246 , fTexture(std::move(tex)) {}
Brian Salomon67f01952019-02-14 13:05:25 -0500247 TestCallback(const TestCallback& that) { SkASSERT(0); }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400248 TestCallback(TestCallback&& that)
249 : fValue(that.fValue)
250 , fReleaseCallback(that.fReleaseCallback)
251 , fTexture(std::move(that.fTexture)) {
252 that.fValue = nullptr;
253 }
Brian Salomon67f01952019-02-14 13:05:25 -0500254
255 ~TestCallback() { fValue ? (void)(*fValue = -1) : void(); }
256
257 TestCallback& operator=(TestCallback&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -0400258 fValue = std::exchange(that.fValue, nullptr);
Brian Salomon67f01952019-02-14 13:05:25 -0500259 return *this;
260 }
261 TestCallback& operator=(const TestCallback& that) = delete;
262
Brian Salomon63410e92020-03-23 18:32:50 -0400263 LazyInstantiationResult operator()(GrResourceProvider*,
264 const GrSurfaceProxy::LazySurfaceDesc&) const {
Brian Salomon67f01952019-02-14 13:05:25 -0500265 *fValue = 1;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400266 return {fTexture, fReleaseCallback};
Brian Salomon67f01952019-02-14 13:05:25 -0500267 }
268
269 private:
270 int* fValue = nullptr;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400271 bool fReleaseCallback;
272 sk_sp<GrTexture> fTexture;
Brian Salomon67f01952019-02-14 13:05:25 -0500273 };
Greg Daniel457469c2018-02-08 15:05:44 -0500274 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500275 TestCallback(&testCount, releaseCallback, tex), format, {kSize, kSize},
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400276 GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
277 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, SkBudgeted::kNo,
278 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Greg Daniel94a6ce82018-01-16 16:14:41 -0500279
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400280 REPORTER_ASSERT(reporter, proxy.get());
Greg Daniel457469c2018-02-08 15:05:44 -0500281 REPORTER_ASSERT(reporter, 0 == testCount);
282
283 if (doInstantiate) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500284 proxy->priv().doLazyInstantiation(ctx->priv().resourceProvider());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400285 if (releaseCallback) {
286 // We will call the cleanup and delete the callback in the
Greg Daniel457469c2018-02-08 15:05:44 -0500287 // doLazyInstantiationCall.
288 REPORTER_ASSERT(reporter, -1 == testCount);
289 } else {
290 REPORTER_ASSERT(reporter, 1 == testCount);
291 }
292 proxy.reset();
293 REPORTER_ASSERT(reporter, -1 == testCount);
294 } else {
295 proxy.reset();
296 REPORTER_ASSERT(reporter, -1 == testCount);
297 }
Greg Daniel94a6ce82018-01-16 16:14:41 -0500298 }
299 }
300}
301
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500302class LazyFailedInstantiationTestOp : public GrDrawOp {
303public:
304 DEFINE_OP_CLASS_ID
305
Robert Phillips88a32ef2018-06-07 11:05:56 -0400306 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
307 GrProxyProvider* proxyProvider,
308 int* testExecuteValue,
309 bool shouldFailInstantiation) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500310 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400311
Greg Daniel4065d452018-11-16 15:43:41 -0500312 return pool->allocate<LazyFailedInstantiationTestOp>(context, proxyProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400313 testExecuteValue,
314 shouldFailInstantiation);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400315 }
316
Chris Dalton1706cbf2019-05-21 19:35:29 -0600317 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600318 func(fLazyProxy.get(), GrMipMapped::kNo);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400319 }
320
321private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400322 friend class GrOpMemoryPool; // for ctor
323
Greg Daniel4065d452018-11-16 15:43:41 -0500324 LazyFailedInstantiationTestOp(GrContext* ctx, GrProxyProvider* proxyProvider,
325 int* testExecuteValue, bool shouldFailInstantiation)
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500326 : INHERITED(ClassID())
327 , fTestExecuteValue(testExecuteValue) {
Brian Salomon63410e92020-03-23 18:32:50 -0400328 SkISize dims = {kSize, kSize};
Greg Daniel4065d452018-11-16 15:43:41 -0500329 GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400330 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
331 GrRenderable::kNo);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500332
333 fLazyProxy = proxyProvider->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400334 [testExecuteValue, shouldFailInstantiation](
335 GrResourceProvider* rp, const GrSurfaceProxy::LazySurfaceDesc& desc)
336 -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel0a375db2018-02-01 12:21:39 -0500337 if (shouldFailInstantiation) {
338 *testExecuteValue = 1;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400339 return {};
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500340 }
Brian Salomon63410e92020-03-23 18:32:50 -0400341 return {rp->createTexture(desc.fDimensions, desc.fFormat, desc.fRenderable,
342 desc.fSampleCnt, desc.fMipMapped, desc.fBudgeted,
343 desc.fProtected),
Brian Salomonbeb7f522019-08-30 16:19:42 -0400344 true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
Brian Salomon2a4f9832018-03-03 22:43:43 -0500345 },
Brian Salomon63410e92020-03-23 18:32:50 -0400346 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500347 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
348 SkBudgeted::kNo, GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500349
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400350 SkASSERT(fLazyProxy.get());
351
Brian Salomon63410e92020-03-23 18:32:50 -0400352 this->setBounds(SkRect::Make(dims), HasAABloat::kNo, IsHairline::kNo);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500353 }
354
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500355 const char* name() const override { return "LazyFailedInstantiationTestOp"; }
356 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600357 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
358 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700359 return GrProcessorSet::EmptySetAnalysis();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500360 }
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400361 void onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400362 const GrSurfaceProxyView* writeView,
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400363 GrAppliedClip*,
364 const GrXferProcessor::DstProxyView&) override {}
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500365 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500366 void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500367 *fTestExecuteValue = 2;
368 }
369
370 int* fTestExecuteValue;
Greg Danieldcf9ca12019-08-27 14:30:21 -0400371 sk_sp<GrTextureProxy> fLazyProxy;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500372
373 typedef GrDrawOp INHERITED;
374};
375
376// Test that when a lazy proxy fails to instantiate during flush that we drop the Op that it was
377// associated with.
378DEF_GPUTEST(LazyProxyFailedInstantiationTest, reporter, /* options */) {
379 GrMockOptions mockOptions;
Robert Phillipsf4f80112020-07-13 16:13:31 -0400380 sk_sp<GrDirectContext> ctx = GrDirectContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500381 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500382 for (bool failInstantiation : {false, true}) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500383 auto rtc = GrRenderTargetContext::Make(
384 ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {100, 100});
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500385 REPORTER_ASSERT(reporter, rtc);
386
Michael Ludwig81d41722020-05-26 16:57:38 -0400387 rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500388
389 int executeTestValue = 0;
Robert Phillips88a32ef2018-06-07 11:05:56 -0400390 rtc->priv().testingOnly_addDrawOp(LazyFailedInstantiationTestOp::Make(
391 ctx.get(), proxyProvider, &executeTestValue, failInstantiation));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400392 ctx->flushAndSubmit();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500393
394 if (failInstantiation) {
Robert Phillips12c46292019-04-23 07:36:17 -0400395 REPORTER_ASSERT(reporter, 1 == executeTestValue);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500396 } else {
397 REPORTER_ASSERT(reporter, 2 == executeTestValue);
398 }
399 }
Greg Daniel4684f822018-03-08 15:27:36 -0500400}