blob: aac72c861be05149027eb522eb7739028e8166c2 [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/GrTexture.h"
11#include "include/gpu/mock/GrMockTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkExchange.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkRectPriv.h"
14#include "src/gpu/GrClip.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrMemoryPool.h"
17#include "src/gpu/GrOnFlushResourceProvider.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrRecordingContextPriv.h"
20#include "src/gpu/GrRenderTargetContext.h"
21#include "src/gpu/GrRenderTargetContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrTextureProxyPriv.h"
26#include "src/gpu/mock/GrMockGpu.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070027
28// This test verifies that lazy proxy callbacks get invoked during flush, after onFlush callbacks,
29// but before Ops are executed. It also ensures that lazy proxy callbacks are invoked both for
30// regular Ops and for clips.
31class LazyProxyTest final : public GrOnFlushCallbackObject {
32public:
33 LazyProxyTest(skiatest::Reporter* reporter)
34 : fReporter(reporter)
35 , fHasOpTexture(false)
36 , fHasClipTexture(false) {
37 }
38
39 ~LazyProxyTest() override {
40 REPORTER_ASSERT(fReporter, fHasOpTexture);
41 REPORTER_ASSERT(fReporter, fHasClipTexture);
42 }
43
Chris Daltonc4b47352019-08-23 10:10:36 -060044 void preFlush(GrOnFlushResourceProvider*, const uint32_t*, int) override {
Chris Dalton706a6ff2017-11-29 22:01:06 -070045 REPORTER_ASSERT(fReporter, !fHasOpTexture);
46 REPORTER_ASSERT(fReporter, !fHasClipTexture);
47 }
48
Greg Danielf41b2bd2019-08-22 16:19:24 -040049 void postFlush(GrDeferredUploadToken, const uint32_t* opsTaskIDs, int numOpsTaskIDs) override {
Chris Dalton706a6ff2017-11-29 22:01:06 -070050 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 Phillips6f0e02f2019-02-13 11:02:28 -050058 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips88a32ef2018-06-07 11:05:56 -040059 GrProxyProvider* proxyProvider,
60 LazyProxyTest* test,
61 bool nullTexture) {
Robert Phillips9da87e02019-02-04 13:26:26 -050062 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040063
Greg Daniel4065d452018-11-16 15:43:41 -050064 return pool->allocate<Op>(context, proxyProvider, test, nullTexture);
Robert Phillips88a32ef2018-06-07 11:05:56 -040065 }
66
Chris Dalton1706cbf2019-05-21 19:35:29 -060067 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060068 func(fProxy.get(), GrMipMapped::kNo);
Robert Phillips88a32ef2018-06-07 11:05:56 -040069 }
70
Brian Salomon588cec72018-11-14 13:56:37 -050071 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override {
Robert Phillips88a32ef2018-06-07 11:05:56 -040072 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 Phillips6f0e02f2019-02-13 11:02:28 -050079 Op(GrRecordingContext* ctx, GrProxyProvider* proxyProvider,
80 LazyProxyTest* test, bool nullTexture)
Robert Phillips777707b2018-01-17 11:40:14 -050081 : GrDrawOp(ClassID()), fTest(test) {
Greg Daniel4065d452018-11-16 15:43:41 -050082 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -040083 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kBGR_565,
84 GrRenderable::kNo);
Greg Danielce3ddaa2020-01-22 16:58:15 -050085 GrSwizzle readSwizzle = ctx->priv().caps()->getReadSwizzle(
86 format, GrColorType::kBGR_565);
Chris Dalton4c458b12018-06-16 17:22:59 -060087 fProxy = GrProxyProvider::MakeFullyLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -040088 [this, format,
89 nullTexture](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
Chris Dalton4c458b12018-06-16 17:22:59 -060090 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasOpTexture);
91 fTest->fHasOpTexture = true;
92 if (nullTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040093 return {};
Chris Dalton4c458b12018-06-16 17:22:59 -060094 } else {
95 GrSurfaceDesc desc;
96 desc.fWidth = 1234;
97 desc.fHeight = 567;
Brian Salomone8a766b2019-07-19 14:24:36 -040098 sk_sp<GrTexture> texture = rp->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -040099 desc, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
100 SkBudgeted::kYes, GrProtected::kNo);
Chris Dalton4c458b12018-06-16 17:22:59 -0600101 REPORTER_ASSERT(fTest->fReporter, texture);
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400102 return texture;
Chris Dalton4c458b12018-06-16 17:22:59 -0600103 }
104 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500105 format, readSwizzle, GrRenderable::kNo, 1, GrProtected::kNo,
Greg Danielb58a3c72020-01-23 10:05:14 -0500106 kTopLeft_GrSurfaceOrigin, *proxyProvider->caps(),
Brian Salomonbeb7f522019-08-30 16:19:42 -0400107 GrSurfaceProxy::UseAllocator::kYes);
Chris Dalton4c458b12018-06-16 17:22:59 -0600108
109 this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo,
Greg Daniel5faf4742019-10-01 15:14:44 -0400110 GrOp::IsHairline::kNo);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700111 }
112
Chris Dalton706a6ff2017-11-29 22:01:06 -0700113 const char* name() const override { return "LazyProxyTest::Op"; }
114 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600115 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip* clip,
116 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700117 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700118 }
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 Salomonbeb7f522019-08-30 16:19:42 -0400140 [this](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400141 REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture);
142 fTest->fHasClipTexture = true;
143 fAtlas->instantiate(rp);
144 return sk_ref_sp(fAtlas->peekTexture());
145 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500146 format, readSwizzle, GrRenderable::kYes, 1, GrProtected::kNo, kOrigin,
Greg Danielb58a3c72020-01-23 10:05:14 -0500147 *proxyProvider->caps(), GrSurfaceProxy::UseAllocator::kYes);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500148 fAccess.set(GrSurfaceProxyView(fLazyProxy, kOrigin, readSwizzle),
Brian Salomonccb61422020-01-09 10:46:36 -0500149 GrSamplerState::Filter::kNearest);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400150 this->setTextureSamplerCnt(1);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700151 }
152
153 private:
154 const char* name() const override { return "LazyProxyTest::ClipFP"; }
155 std::unique_ptr<GrFragmentProcessor> clone() const override {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500156 return std::make_unique<ClipFP>(fContext, fProxyProvider, fTest, fAtlas);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700157 }
158 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return nullptr; }
159 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
160 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400161 const TextureSampler& onTextureSampler(int) const override { return fAccess; }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700162
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;
168 TextureSampler fAccess;
169 };
170
171
172 class Clip : public GrClip {
173 public:
174 Clip(LazyProxyTest* test, GrTextureProxy* atlas)
175 : fTest(test)
176 , fAtlas(atlas) {}
177
178 private:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500179 bool apply(GrRecordingContext* context, GrRenderTargetContext*, bool useHWAA,
180 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
Robert Phillips9da87e02019-02-04 13:26:26 -0500181 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500182 out->addCoverageFP(std::make_unique<ClipFP>(context, proxyProvider, fTest, fAtlas));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700183 return true;
184 }
185 bool quickContains(const SkRect&) const final { return false; }
186 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
187 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
Mike Reed92b33352019-08-24 19:39:13 -0400188 rect->setLTRB(0, 0, width, height);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700189 if (iior) {
190 *iior = false;
191 }
192 }
193
194 LazyProxyTest* const fTest;
195 GrTextureProxy* fAtlas;
196 };
197
198private:
199 skiatest::Reporter* fReporter;
200 bool fHasOpTexture;
201 bool fHasClipTexture;
202};
203
204DEF_GPUTEST(LazyProxyTest, reporter, /* options */) {
205 GrMockOptions mockOptions;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400206 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
Brian Salomonbdecacf2018-02-02 20:32:49 -0500207 GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400208 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700209 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500210 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700211 for (bool nullTexture : {false, true}) {
212 LazyProxyTest test(reporter);
Robert Phillips9da87e02019-02-04 13:26:26 -0500213 ctx->priv().addOnFlushCallbackObject(&test);
Greg Daniele20fcad2020-01-08 11:52:34 -0500214 auto rtc = GrRenderTargetContext::Make(
215 ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {100, 100});
Chris Dalton706a6ff2017-11-29 22:01:06 -0700216 REPORTER_ASSERT(reporter, rtc);
Greg Daniele20fcad2020-01-08 11:52:34 -0500217 auto mockAtlas = GrRenderTargetContext::Make(
218 ctx.get(), GrColorType::kAlpha_F16, nullptr, SkBackingFit::kExact, {10, 10});
Chris Dalton706a6ff2017-11-29 22:01:06 -0700219 REPORTER_ASSERT(reporter, mockAtlas);
220 rtc->priv().testingOnly_addDrawOp(LazyProxyTest::Clip(&test, mockAtlas->asTextureProxy()),
Robert Phillips88a32ef2018-06-07 11:05:56 -0400221 LazyProxyTest::Op::Make(ctx.get(), proxyProvider, &test, nullTexture));
Robert Phillips9da87e02019-02-04 13:26:26 -0500222 ctx->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&test);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700223 }
224}
225
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500226static const int kSize = 16;
227
Greg Daniel94a6ce82018-01-16 16:14:41 -0500228DEF_GPUTEST(LazyProxyReleaseTest, reporter, /* options */) {
229 GrMockOptions mockOptions;
230 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500231 auto proxyProvider = ctx->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400232 const GrCaps* caps = ctx->priv().caps();
Greg Daniel94a6ce82018-01-16 16:14:41 -0500233
234 GrSurfaceDesc desc;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500235 desc.fWidth = kSize;
236 desc.fHeight = kSize;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500237
Robert Phillips0a15cc62019-07-30 12:49:10 -0400238 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
239 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500240
Brian Salomonbeb7f522019-08-30 16:19:42 -0400241 auto tex = ctx->priv().resourceProvider()->createTexture(desc, format, GrRenderable::kNo, 1,
Brian Salomona90382f2019-09-17 09:01:56 -0400242 GrMipMapped::kNo, SkBudgeted::kNo,
243 GrProtected::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400244 using LazyInstantiationResult = GrSurfaceProxy::LazyCallbackResult;
Greg Daniel94a6ce82018-01-16 16:14:41 -0500245 for (bool doInstantiate : {true, false}) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400246 for (bool releaseCallback : {false, true}) {
Greg Daniel457469c2018-02-08 15:05:44 -0500247 int testCount = 0;
Brian Salomon67f01952019-02-14 13:05:25 -0500248 // Sets an integer to 1 when the callback is called and -1 when it is deleted.
249 class TestCallback {
250 public:
Brian Salomonbeb7f522019-08-30 16:19:42 -0400251 TestCallback(int* value, bool releaseCallback, sk_sp<GrTexture> tex)
252 : fValue(value)
253 , fReleaseCallback(releaseCallback)
254 , fTexture(std::move(tex)) {}
Brian Salomon67f01952019-02-14 13:05:25 -0500255 TestCallback(const TestCallback& that) { SkASSERT(0); }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400256 TestCallback(TestCallback&& that)
257 : fValue(that.fValue)
258 , fReleaseCallback(that.fReleaseCallback)
259 , fTexture(std::move(that.fTexture)) {
260 that.fValue = nullptr;
261 }
Brian Salomon67f01952019-02-14 13:05:25 -0500262
263 ~TestCallback() { fValue ? (void)(*fValue = -1) : void(); }
264
265 TestCallback& operator=(TestCallback&& that) {
266 fValue = skstd::exchange(that.fValue, nullptr);
267 return *this;
268 }
269 TestCallback& operator=(const TestCallback& that) = delete;
270
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400271 LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) const {
Brian Salomon67f01952019-02-14 13:05:25 -0500272 *fValue = 1;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400273 return {fTexture, fReleaseCallback};
Brian Salomon67f01952019-02-14 13:05:25 -0500274 }
275
276 private:
277 int* fValue = nullptr;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400278 bool fReleaseCallback;
279 sk_sp<GrTexture> fTexture;
Brian Salomon67f01952019-02-14 13:05:25 -0500280 };
Greg Danielce3ddaa2020-01-22 16:58:15 -0500281 GrSwizzle readSwizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888);
Greg Daniel457469c2018-02-08 15:05:44 -0500282 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500283 TestCallback(&testCount, releaseCallback, tex), format, desc, readSwizzle,
284 GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
285 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone,
286 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
287 GrSurfaceProxy::UseAllocator::kYes);
Greg Daniel94a6ce82018-01-16 16:14:41 -0500288
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400289 REPORTER_ASSERT(reporter, proxy.get());
Greg Daniel457469c2018-02-08 15:05:44 -0500290 REPORTER_ASSERT(reporter, 0 == testCount);
291
292 if (doInstantiate) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500293 proxy->priv().doLazyInstantiation(ctx->priv().resourceProvider());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400294 if (releaseCallback) {
295 // We will call the cleanup and delete the callback in the
Greg Daniel457469c2018-02-08 15:05:44 -0500296 // doLazyInstantiationCall.
297 REPORTER_ASSERT(reporter, -1 == testCount);
298 } else {
299 REPORTER_ASSERT(reporter, 1 == testCount);
300 }
301 proxy.reset();
302 REPORTER_ASSERT(reporter, -1 == testCount);
303 } else {
304 proxy.reset();
305 REPORTER_ASSERT(reporter, -1 == testCount);
306 }
Greg Daniel94a6ce82018-01-16 16:14:41 -0500307 }
308 }
309}
310
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500311class LazyFailedInstantiationTestOp : public GrDrawOp {
312public:
313 DEFINE_OP_CLASS_ID
314
Robert Phillips88a32ef2018-06-07 11:05:56 -0400315 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
316 GrProxyProvider* proxyProvider,
317 int* testExecuteValue,
318 bool shouldFailInstantiation) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500319 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400320
Greg Daniel4065d452018-11-16 15:43:41 -0500321 return pool->allocate<LazyFailedInstantiationTestOp>(context, proxyProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400322 testExecuteValue,
323 shouldFailInstantiation);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400324 }
325
Chris Dalton1706cbf2019-05-21 19:35:29 -0600326 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600327 func(fLazyProxy.get(), GrMipMapped::kNo);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400328 }
329
330private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400331 friend class GrOpMemoryPool; // for ctor
332
Greg Daniel4065d452018-11-16 15:43:41 -0500333 LazyFailedInstantiationTestOp(GrContext* ctx, GrProxyProvider* proxyProvider,
334 int* testExecuteValue, bool shouldFailInstantiation)
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500335 : INHERITED(ClassID())
336 , fTestExecuteValue(testExecuteValue) {
337 GrSurfaceDesc desc;
338 desc.fWidth = kSize;
339 desc.fHeight = kSize;
Greg Daniel4065d452018-11-16 15:43:41 -0500340 GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400341 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
342 GrRenderable::kNo);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500343
Greg Danielce3ddaa2020-01-22 16:58:15 -0500344 GrSwizzle readSwizzle = ctx->priv().caps()->getReadSwizzle(
345 format, GrColorType::kRGBA_8888);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500346 fLazyProxy = proxyProvider->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400347 [testExecuteValue, shouldFailInstantiation, desc,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400348 format](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel0a375db2018-02-01 12:21:39 -0500349 if (shouldFailInstantiation) {
350 *testExecuteValue = 1;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400351 return {};
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500352 }
Brian Salomona90382f2019-09-17 09:01:56 -0400353 return {rp->createTexture(desc, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
354 SkBudgeted::kNo, GrProtected::kNo),
Brian Salomonbeb7f522019-08-30 16:19:42 -0400355 true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
Brian Salomon2a4f9832018-03-03 22:43:43 -0500356 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500357 format, desc, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
358 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone,
359 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
360 GrSurfaceProxy::UseAllocator::kYes);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500361
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400362 SkASSERT(fLazyProxy.get());
363
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500364 this->setBounds(SkRect::MakeIWH(kSize, kSize),
Greg Daniel5faf4742019-10-01 15:14:44 -0400365 HasAABloat::kNo, IsHairline::kNo);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500366 }
367
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500368 const char* name() const override { return "LazyFailedInstantiationTestOp"; }
369 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600370 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
371 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700372 return GrProcessorSet::EmptySetAnalysis();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500373 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500374 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500375 void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500376 *fTestExecuteValue = 2;
377 }
378
379 int* fTestExecuteValue;
Greg Danieldcf9ca12019-08-27 14:30:21 -0400380 sk_sp<GrTextureProxy> fLazyProxy;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500381
382 typedef GrDrawOp INHERITED;
383};
384
385// Test that when a lazy proxy fails to instantiate during flush that we drop the Op that it was
386// associated with.
387DEF_GPUTEST(LazyProxyFailedInstantiationTest, reporter, /* options */) {
388 GrMockOptions mockOptions;
389 sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
Robert Phillips9da87e02019-02-04 13:26:26 -0500390 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500391 for (bool failInstantiation : {false, true}) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500392 auto rtc = GrRenderTargetContext::Make(
393 ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {100, 100});
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500394 REPORTER_ASSERT(reporter, rtc);
395
Brian Osman9a9baae2018-11-05 15:06:26 -0500396 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
397 GrRenderTargetContext::CanClearFullscreen::kYes);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500398
399 int executeTestValue = 0;
Robert Phillips88a32ef2018-06-07 11:05:56 -0400400 rtc->priv().testingOnly_addDrawOp(LazyFailedInstantiationTestOp::Make(
401 ctx.get(), proxyProvider, &executeTestValue, failInstantiation));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500402 ctx->flush();
403
404 if (failInstantiation) {
Robert Phillips12c46292019-04-23 07:36:17 -0400405 REPORTER_ASSERT(reporter, 1 == executeTestValue);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500406 } else {
407 REPORTER_ASSERT(reporter, 2 == executeTestValue);
408 }
409 }
Greg Daniel4684f822018-03-08 15:27:36 -0500410}