Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrClip.h" |
| 13 | #include "GrContextPriv.h" |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 15 | #include "GrOnFlushResourceProvider.h" |
| 16 | #include "GrRenderTargetContext.h" |
| 17 | #include "GrRenderTargetContextPriv.h" |
| 18 | #include "GrSurfaceProxy.h" |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 19 | #include "GrSurfaceProxyPriv.h" |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 20 | #include "GrTexture.h" |
| 21 | #include "GrTextureProxy.h" |
| 22 | #include "GrTextureProxyPriv.h" |
| 23 | #include "SkMakeUnique.h" |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 24 | #include "SkRectPriv.h" |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 25 | #include "mock/GrMockGpu.h" |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 26 | #include "mock/GrMockTypes.h" |
| 27 | |
| 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. |
| 31 | class LazyProxyTest final : public GrOnFlushCallbackObject { |
| 32 | public: |
| 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 | |
| 44 | void preFlush(GrOnFlushResourceProvider*, const uint32_t*, int, |
| 45 | SkTArray<sk_sp<GrRenderTargetContext>>*) override { |
| 46 | REPORTER_ASSERT(fReporter, !fHasOpTexture); |
| 47 | REPORTER_ASSERT(fReporter, !fHasClipTexture); |
| 48 | } |
| 49 | |
| 50 | void postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, int numOpListIDs) override { |
| 51 | REPORTER_ASSERT(fReporter, fHasOpTexture); |
| 52 | REPORTER_ASSERT(fReporter, fHasClipTexture); |
| 53 | } |
| 54 | |
| 55 | class Op final : public GrDrawOp { |
| 56 | public: |
| 57 | DEFINE_OP_CLASS_ID |
| 58 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 59 | Op(GrProxyProvider* proxyProvider, LazyProxyTest* test, bool nullTexture) |
| 60 | : GrDrawOp(ClassID()), fTest(test) { |
| 61 | fProxy = proxyProvider->createFullyLazyProxy([this, nullTexture]( |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 62 | GrResourceProvider* rp) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 63 | if (!rp) { |
| 64 | return sk_sp<GrTexture>(); |
| 65 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 66 | REPORTER_ASSERT(fTest->fReporter, !fTest->fHasOpTexture); |
| 67 | fTest->fHasOpTexture = true; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 68 | if (nullTexture) { |
| 69 | return sk_sp<GrTexture>(); |
| 70 | } else { |
| 71 | GrSurfaceDesc desc; |
| 72 | desc.fWidth = 1234; |
| 73 | desc.fHeight = 567; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 74 | desc.fConfig = kRGB_565_GrPixelConfig; |
| 75 | sk_sp<GrTexture> texture = rp->createTexture(desc, SkBudgeted::kYes); |
| 76 | REPORTER_ASSERT(fTest->fReporter, texture); |
| 77 | return texture; |
| 78 | } |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 79 | }, GrProxyProvider::Renderable::kNo, kTopLeft_GrSurfaceOrigin, kRGB_565_GrPixelConfig); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 80 | this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void visitProxies(const VisitProxyFunc& func) const override { |
| 84 | func(fProxy.get()); |
| 85 | } |
| 86 | |
| 87 | void onExecute(GrOpFlushState*) override { |
| 88 | REPORTER_ASSERT(fTest->fReporter, fTest->fHasOpTexture); |
| 89 | REPORTER_ASSERT(fTest->fReporter, fTest->fHasClipTexture); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | const char* name() const override { return "LazyProxyTest::Op"; } |
| 94 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 95 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 96 | GrPixelConfigIsClamped) override { |
| 97 | return RequiresDstTexture::kNo; |
| 98 | } |
| 99 | void wasRecorded(GrRenderTargetOpList*) override {} |
| 100 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 101 | void onPrepare(GrOpFlushState*) override {} |
| 102 | |
| 103 | LazyProxyTest* const fTest; |
| 104 | sk_sp<GrTextureProxy> fProxy; |
| 105 | }; |
| 106 | |
| 107 | class ClipFP : public GrFragmentProcessor { |
| 108 | public: |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 109 | ClipFP(GrProxyProvider* proxyProvider, LazyProxyTest* test, GrTextureProxy* atlas) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 110 | : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 111 | , fProxyProvider(proxyProvider) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 112 | , fTest(test) |
| 113 | , fAtlas(atlas) { |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 114 | fLazyProxy = proxyProvider->createFullyLazyProxy( |
| 115 | [this](GrResourceProvider* rp) { |
| 116 | if (!rp) { |
| 117 | return sk_sp<GrTexture>(); |
| 118 | } |
| 119 | REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture); |
| 120 | fTest->fHasClipTexture = true; |
| 121 | fAtlas->instantiate(rp); |
| 122 | return sk_ref_sp(fAtlas->priv().peekTexture()); |
| 123 | }, |
| 124 | GrProxyProvider::Renderable::kYes, |
| 125 | kBottomLeft_GrSurfaceOrigin, |
| 126 | kAlpha_half_GrPixelConfig); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 127 | fAccess.reset(fLazyProxy, GrSamplerState::Filter::kNearest, |
| 128 | GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag); |
| 129 | this->addTextureSampler(&fAccess); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | const char* name() const override { return "LazyProxyTest::ClipFP"; } |
| 134 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 135 | return skstd::make_unique<ClipFP>(fProxyProvider, fTest, fAtlas); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 136 | } |
| 137 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return nullptr; } |
| 138 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 139 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 140 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 141 | GrProxyProvider* const fProxyProvider; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 142 | LazyProxyTest* const fTest; |
| 143 | GrTextureProxy* const fAtlas; |
| 144 | sk_sp<GrTextureProxy> fLazyProxy; |
| 145 | TextureSampler fAccess; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | class Clip : public GrClip { |
| 150 | public: |
| 151 | Clip(LazyProxyTest* test, GrTextureProxy* atlas) |
| 152 | : fTest(test) |
| 153 | , fAtlas(atlas) {} |
| 154 | |
| 155 | private: |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 156 | bool apply(GrContext* context, GrRenderTargetContext*, bool, bool, GrAppliedClip* out, |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 157 | SkRect* bounds) const override { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 158 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 159 | out->addCoverageFP(skstd::make_unique<ClipFP>(proxyProvider, fTest, fAtlas)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | bool quickContains(const SkRect&) const final { return false; } |
| 163 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; } |
| 164 | void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final { |
| 165 | rect->set(0, 0, width, height); |
| 166 | if (iior) { |
| 167 | *iior = false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | LazyProxyTest* const fTest; |
| 172 | GrTextureProxy* fAtlas; |
| 173 | }; |
| 174 | |
| 175 | private: |
| 176 | skiatest::Reporter* fReporter; |
| 177 | bool fHasOpTexture; |
| 178 | bool fHasClipTexture; |
| 179 | }; |
| 180 | |
| 181 | DEF_GPUTEST(LazyProxyTest, reporter, /* options */) { |
| 182 | GrMockOptions mockOptions; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 183 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fRenderability = |
| 184 | GrMockOptions::ConfigOptions::Renderability::kNonMSAA; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 185 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fTexturable = true; |
| 186 | sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions()); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 187 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 188 | for (bool nullTexture : {false, true}) { |
| 189 | LazyProxyTest test(reporter); |
| 190 | ctx->contextPriv().addOnFlushCallbackObject(&test); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 191 | sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext( |
| 192 | SkBackingFit::kExact, 100, 100, |
| 193 | kRGBA_8888_GrPixelConfig, nullptr); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, rtc); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 195 | sk_sp<GrRenderTargetContext> mockAtlas = ctx->contextPriv().makeDeferredRenderTargetContext( |
| 196 | SkBackingFit::kExact, 10, 10, |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 197 | kAlpha_half_GrPixelConfig, nullptr); |
| 198 | REPORTER_ASSERT(reporter, mockAtlas); |
| 199 | rtc->priv().testingOnly_addDrawOp(LazyProxyTest::Clip(&test, mockAtlas->asTextureProxy()), |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 200 | skstd::make_unique<LazyProxyTest::Op>(proxyProvider, &test, nullTexture)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 201 | ctx->contextPriv().testingOnly_flushAndRemoveOnFlushCallbackObject(&test); |
| 202 | } |
| 203 | } |
| 204 | |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 205 | static const int kSize = 16; |
| 206 | |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 207 | DEF_GPUTEST(LazyProxyReleaseTest, reporter, /* options */) { |
| 208 | GrMockOptions mockOptions; |
| 209 | sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions()); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 210 | auto proxyProvider = ctx->contextPriv().proxyProvider(); |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 211 | |
| 212 | GrSurfaceDesc desc; |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 213 | desc.fWidth = kSize; |
| 214 | desc.fHeight = kSize; |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 215 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 216 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 217 | using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType; |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 218 | for (bool doInstantiate : {true, false}) { |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 219 | for (auto lazyType : {LazyInstantiationType::kSingleUse, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 220 | LazyInstantiationType::kMultipleUse, |
| 221 | LazyInstantiationType::kUninstantiate}) { |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 222 | int testCount = 0; |
| 223 | int* testCountPtr = &testCount; |
| 224 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 225 | [testCountPtr](GrResourceProvider* resourceProvider) { |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 226 | if (!resourceProvider) { |
| 227 | *testCountPtr = -1; |
| 228 | return sk_sp<GrTexture>(); |
| 229 | } |
| 230 | *testCountPtr = 1; |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 231 | return sk_sp<GrTexture>(); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 232 | }, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 233 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrRenderTargetFlags::kNone, |
| 234 | SkBackingFit::kExact, SkBudgeted::kNo, lazyType); |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 235 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 236 | REPORTER_ASSERT(reporter, 0 == testCount); |
| 237 | |
| 238 | if (doInstantiate) { |
| 239 | proxy->priv().doLazyInstantiation(ctx->contextPriv().resourceProvider()); |
| 240 | if (LazyInstantiationType::kSingleUse == proxy->priv().lazyInstantiationType()) { |
| 241 | // In SingleUse we will call the cleanup and delete the callback in the |
| 242 | // doLazyInstantiationCall. |
| 243 | REPORTER_ASSERT(reporter, -1 == testCount); |
| 244 | } else { |
| 245 | REPORTER_ASSERT(reporter, 1 == testCount); |
| 246 | } |
| 247 | proxy.reset(); |
| 248 | REPORTER_ASSERT(reporter, -1 == testCount); |
| 249 | } else { |
| 250 | proxy.reset(); |
| 251 | REPORTER_ASSERT(reporter, -1 == testCount); |
| 252 | } |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 257 | class LazyFailedInstantiationTestOp : public GrDrawOp { |
| 258 | public: |
| 259 | DEFINE_OP_CLASS_ID |
| 260 | |
| 261 | LazyFailedInstantiationTestOp(GrProxyProvider* proxyProvider, int* testExecuteValue, |
| 262 | bool shouldFailInstantiation) |
| 263 | : INHERITED(ClassID()) |
| 264 | , fTestExecuteValue(testExecuteValue) { |
| 265 | GrSurfaceDesc desc; |
| 266 | desc.fWidth = kSize; |
| 267 | desc.fHeight = kSize; |
| 268 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 269 | |
| 270 | fLazyProxy = proxyProvider->createLazyProxy( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 271 | [testExecuteValue, shouldFailInstantiation, desc](GrResourceProvider* rp) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 272 | if (!rp) { |
| 273 | return sk_sp<GrTexture>(); |
| 274 | } |
| 275 | if (shouldFailInstantiation) { |
| 276 | *testExecuteValue = 1; |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 277 | return sk_sp<GrTexture>(); |
| 278 | } |
| 279 | return rp->createTexture(desc, SkBudgeted::kNo); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 280 | }, |
| 281 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact, |
| 282 | SkBudgeted::kNo); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 283 | |
| 284 | this->setBounds(SkRect::MakeIWH(kSize, kSize), |
| 285 | HasAABloat::kNo, IsZeroArea::kNo); |
| 286 | } |
| 287 | |
| 288 | void visitProxies(const VisitProxyFunc& func) const override { |
| 289 | func(fLazyProxy.get()); |
| 290 | } |
| 291 | |
| 292 | private: |
| 293 | const char* name() const override { return "LazyFailedInstantiationTestOp"; } |
| 294 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 295 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 296 | GrPixelConfigIsClamped) override { |
| 297 | return RequiresDstTexture::kNo; |
| 298 | } |
| 299 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 300 | void onPrepare(GrOpFlushState*) override {} |
| 301 | void onExecute(GrOpFlushState* state) override { |
| 302 | *fTestExecuteValue = 2; |
| 303 | } |
| 304 | |
| 305 | int* fTestExecuteValue; |
| 306 | sk_sp<GrSurfaceProxy> fLazyProxy; |
| 307 | |
| 308 | typedef GrDrawOp INHERITED; |
| 309 | }; |
| 310 | |
| 311 | // Test that when a lazy proxy fails to instantiate during flush that we drop the Op that it was |
| 312 | // associated with. |
| 313 | DEF_GPUTEST(LazyProxyFailedInstantiationTest, reporter, /* options */) { |
| 314 | GrMockOptions mockOptions; |
| 315 | sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions()); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 316 | GrResourceProvider* resourceProvider = ctx->contextPriv().resourceProvider(); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 317 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 318 | for (bool failInstantiation : {false, true}) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 319 | sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext( |
| 320 | SkBackingFit::kExact, 100, 100, |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 321 | kRGBA_8888_GrPixelConfig, nullptr); |
| 322 | REPORTER_ASSERT(reporter, rtc); |
| 323 | |
| 324 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
| 325 | |
| 326 | int executeTestValue = 0; |
| 327 | rtc->priv().testingOnly_addDrawOp( |
| 328 | skstd::make_unique<LazyFailedInstantiationTestOp>(proxyProvider, &executeTestValue, |
| 329 | failInstantiation)); |
| 330 | ctx->flush(); |
| 331 | |
| 332 | if (failInstantiation) { |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 333 | if (resourceProvider->explicitlyAllocateGPUResources()) { |
| 334 | REPORTER_ASSERT(reporter, 1 == executeTestValue); |
| 335 | } else { |
| 336 | // When we disable explicit gpu resource allocation we don't throw away ops that |
| 337 | // have uninstantiated proxies. |
| 338 | REPORTER_ASSERT(reporter, 2 == executeTestValue); |
| 339 | } |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 340 | } else { |
| 341 | REPORTER_ASSERT(reporter, 2 == executeTestValue); |
| 342 | } |
| 343 | } |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 344 | } |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 345 | |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 346 | class LazyUninstantiateTestOp : public GrDrawOp { |
| 347 | public: |
| 348 | DEFINE_OP_CLASS_ID |
| 349 | |
| 350 | LazyUninstantiateTestOp(sk_sp<GrTextureProxy> proxy) |
| 351 | : INHERITED(ClassID()) |
| 352 | , fLazyProxy(std::move(proxy)) { |
| 353 | |
| 354 | this->setBounds(SkRect::MakeIWH(kSize, kSize), |
| 355 | HasAABloat::kNo, IsZeroArea::kNo); |
| 356 | } |
| 357 | |
| 358 | void visitProxies(const VisitProxyFunc& func) const override { |
| 359 | func(fLazyProxy.get()); |
| 360 | } |
| 361 | |
| 362 | private: |
| 363 | const char* name() const override { return "LazyUninstantiateTestOp"; } |
| 364 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 365 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 366 | GrPixelConfigIsClamped) override { |
| 367 | return RequiresDstTexture::kNo; |
| 368 | } |
| 369 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 370 | void onPrepare(GrOpFlushState*) override {} |
| 371 | void onExecute(GrOpFlushState* state) override {} |
| 372 | |
| 373 | sk_sp<GrSurfaceProxy> fLazyProxy; |
| 374 | |
| 375 | typedef GrDrawOp INHERITED; |
| 376 | }; |
| 377 | |
| 378 | static void UninstantiateReleaseProc(void* releaseValue) { |
| 379 | (*static_cast<int*>(releaseValue))++; |
| 380 | } |
| 381 | |
| 382 | // Test that lazy proxies with the Uninstantiate LazyCallbackType are uninstantiated and released as |
| 383 | // expected. |
| 384 | DEF_GPUTEST(LazyProxyUninstantiateTest, reporter, /* options */) { |
| 385 | GrMockOptions mockOptions; |
| 386 | sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions()); |
| 387 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 388 | GrGpu* gpu = ctx->contextPriv().getGpu(); |
| 389 | |
| 390 | using LazyType = GrSurfaceProxy::LazyInstantiationType; |
| 391 | for (auto lazyType : {LazyType::kSingleUse, LazyType::kMultipleUse, LazyType::kUninstantiate}) { |
| 392 | sk_sp<GrRenderTargetContext> rtc = ctx->contextPriv().makeDeferredRenderTargetContext( |
| 393 | SkBackingFit::kExact, 100, 100, |
| 394 | kRGBA_8888_GrPixelConfig, nullptr); |
| 395 | REPORTER_ASSERT(reporter, rtc); |
| 396 | |
| 397 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
| 398 | |
| 399 | int instantiateTestValue = 0; |
| 400 | int releaseTestValue = 0; |
| 401 | int* instantiatePtr = &instantiateTestValue; |
| 402 | int* releasePtr = &releaseTestValue; |
| 403 | GrSurfaceDesc desc; |
| 404 | desc.fWidth = kSize; |
| 405 | desc.fHeight = kSize; |
| 406 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 407 | |
| 408 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
| 409 | nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo); |
| 410 | |
| 411 | sk_sp<GrTextureProxy> lazyProxy = proxyProvider->createLazyProxy( |
| 412 | [instantiatePtr, releasePtr, backendTex](GrResourceProvider* rp) { |
| 413 | if (!rp) { |
| 414 | return sk_sp<GrTexture>(); |
| 415 | } |
| 416 | |
| 417 | sk_sp<GrTexture> texture = rp->wrapBackendTexture(backendTex); |
| 418 | if (!texture) { |
| 419 | return sk_sp<GrTexture>(); |
| 420 | } |
| 421 | (*instantiatePtr)++; |
| 422 | texture->setRelease(UninstantiateReleaseProc, releasePtr); |
| 423 | return texture; |
| 424 | }, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 425 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrRenderTargetFlags::kNone, |
| 426 | SkBackingFit::kExact, SkBudgeted::kNo, lazyType); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 427 | |
| 428 | rtc->priv().testingOnly_addDrawOp(skstd::make_unique<LazyUninstantiateTestOp>(lazyProxy)); |
| 429 | |
| 430 | ctx->flush(); |
| 431 | |
| 432 | REPORTER_ASSERT(reporter, 1 == instantiateTestValue); |
| 433 | if (LazyType::kUninstantiate == lazyType) { |
| 434 | REPORTER_ASSERT(reporter, 1 == releaseTestValue); |
| 435 | } else { |
| 436 | REPORTER_ASSERT(reporter, 0 == releaseTestValue); |
| 437 | } |
| 438 | |
| 439 | // This should cause the uninstantiate proxies to be instantiated again but have no effect |
| 440 | // on the others |
| 441 | rtc->priv().testingOnly_addDrawOp(skstd::make_unique<LazyUninstantiateTestOp>(lazyProxy)); |
| 442 | // Add a second op to make sure we only instantiate once. |
| 443 | rtc->priv().testingOnly_addDrawOp(skstd::make_unique<LazyUninstantiateTestOp>(lazyProxy)); |
| 444 | ctx->flush(); |
| 445 | |
| 446 | if (LazyType::kUninstantiate == lazyType) { |
| 447 | REPORTER_ASSERT(reporter, 2 == instantiateTestValue); |
| 448 | REPORTER_ASSERT(reporter, 2 == releaseTestValue); |
| 449 | } else { |
| 450 | REPORTER_ASSERT(reporter, 1 == instantiateTestValue); |
| 451 | REPORTER_ASSERT(reporter, 0 == releaseTestValue); |
| 452 | } |
| 453 | |
| 454 | lazyProxy.reset(); |
| 455 | if (LazyType::kUninstantiate == lazyType) { |
| 456 | REPORTER_ASSERT(reporter, 2 == releaseTestValue); |
| 457 | } else { |
| 458 | REPORTER_ASSERT(reporter, 1 == releaseTestValue); |
| 459 | } |
| 460 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 461 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 462 | } |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 463 | } |
| 464 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 465 | #endif |