Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [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 "SkTypes.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #if SK_SUPPORT_GPU |
| 12 | |
| 13 | #include "GrContext.h" |
| 14 | #include "GrContextPriv.h" |
| 15 | #include "GrClip.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 16 | #include "GrDrawingManager.h" |
| 17 | #include "GrPathRenderer.h" |
| 18 | #include "GrPaint.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 19 | #include "GrRenderTargetContext.h" |
| 20 | #include "GrRenderTargetContextPriv.h" |
| 21 | #include "GrShape.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 22 | #include "SkMatrix.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 23 | #include "SkPathPriv.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 24 | #include "SkRect.h" |
| 25 | #include "ccpr/GrCoverageCountingPathRenderer.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 26 | #include "mock/GrMockTypes.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 27 | #include <cmath> |
| 28 | |
| 29 | static constexpr int kCanvasSize = 100; |
| 30 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 31 | class CCPRClip : public GrClip { |
| 32 | public: |
| 33 | CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {} |
| 34 | |
| 35 | private: |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 36 | bool apply(GrContext* context, GrRenderTargetContext* rtc, bool, bool, GrAppliedClip* out, |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 37 | SkRect* bounds) const override { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 38 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 39 | out->addCoverageFP(fCCPR->makeClipProcessor(proxyProvider, |
| 40 | rtc->priv().testingOnly_getOpListID(), fPath, |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 41 | SkIRect::MakeWH(rtc->width(), rtc->height()), |
| 42 | rtc->width(), rtc->height())); |
| 43 | return true; |
| 44 | } |
| 45 | bool quickContains(const SkRect&) const final { return false; } |
| 46 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; } |
| 47 | void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final { |
| 48 | rect->set(0, 0, width, height); |
| 49 | if (iior) { |
| 50 | *iior = false; |
| 51 | } |
| 52 | } |
| 53 | GrCoverageCountingPathRenderer* const fCCPR; |
| 54 | const SkPath fPath; |
| 55 | }; |
| 56 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 57 | class CCPRPathDrawer { |
| 58 | public: |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 59 | CCPRPathDrawer(GrContext* ctx, skiatest::Reporter* reporter) |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 60 | : fCtx(ctx) |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 61 | , fCCPR(fCtx->contextPriv().drawingManager()->getCoverageCountingPathRenderer()) |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 62 | , fRTC(fCtx->contextPriv().makeDeferredRenderTargetContext( |
| 63 | SkBackingFit::kExact, kCanvasSize, |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 64 | kCanvasSize, kRGBA_8888_GrPixelConfig, |
| 65 | nullptr)) { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 66 | if (!fCCPR) { |
| 67 | ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests"); |
| 68 | } |
| 69 | if (!fRTC) { |
| 70 | ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests"); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 74 | bool valid() const { return fCCPR && fRTC; } |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 75 | void clear() const { fRTC->clear(nullptr, 0, GrRenderTargetContext::CanClearFullscreen::kYes); } |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 76 | void abandonGrContext() { fCtx = nullptr; fCCPR = nullptr; fRTC = nullptr; } |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 77 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 78 | void drawPath(SkPath path, GrColor4f color = GrColor4f(0, 1, 0, 1)) const { |
| 79 | SkASSERT(this->valid()); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 80 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 81 | GrPaint paint; |
| 82 | paint.setColor4f(color); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 83 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 84 | GrNoClip noClip; |
| 85 | SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 86 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 87 | SkMatrix matrix = SkMatrix::I(); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 88 | |
| 89 | path.setIsVolatile(true); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 90 | GrShape shape(path); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 91 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 92 | fCCPR->drawPath({fCtx, std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), |
| 93 | &noClip, &clipBounds, &matrix, &shape, GrAAType::kCoverage, false}); |
| 94 | } |
| 95 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 96 | void clipFullscreenRect(SkPath clipPath, GrColor4f color = GrColor4f(0, 1, 0, 1)) { |
| 97 | SkASSERT(this->valid()); |
| 98 | |
| 99 | GrPaint paint; |
| 100 | paint.setColor4f(color); |
| 101 | |
| 102 | fRTC->drawRect(CCPRClip(fCCPR, clipPath), std::move(paint), GrAA::kYes, SkMatrix::I(), |
| 103 | SkRect::MakeIWH(kCanvasSize, kCanvasSize)); |
| 104 | } |
| 105 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 106 | void flush() const { |
| 107 | SkASSERT(this->valid()); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 108 | fCtx->flush(); |
| 109 | } |
| 110 | |
| 111 | private: |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 112 | GrContext* fCtx; |
| 113 | GrCoverageCountingPathRenderer* fCCPR; |
| 114 | sk_sp<GrRenderTargetContext> fRTC; |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 117 | class CCPRTest { |
| 118 | public: |
| 119 | void run(skiatest::Reporter* reporter) { |
| 120 | GrMockOptions mockOptions; |
| 121 | mockOptions.fInstanceAttribSupport = true; |
| 122 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 123 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fRenderability = |
| 124 | GrMockOptions::ConfigOptions::Renderability::kNonMSAA; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 125 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fTexturable = true; |
| 126 | mockOptions.fGeometryShaderSupport = true; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 127 | mockOptions.fIntegerSupport = true; |
| 128 | mockOptions.fFlatInterpolationSupport = true; |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 129 | this->customizeMockOptions(&mockOptions); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 130 | |
| 131 | GrContextOptions ctxOptions; |
| 132 | ctxOptions.fAllowPathMaskCaching = false; |
| 133 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting; |
| 134 | |
| 135 | fMockContext = GrContext::MakeMock(&mockOptions, ctxOptions); |
| 136 | if (!fMockContext) { |
| 137 | ERRORF(reporter, "could not create mock context"); |
| 138 | return; |
| 139 | } |
| 140 | if (!fMockContext->unique()) { |
| 141 | ERRORF(reporter, "mock context is not unique"); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | CCPRPathDrawer ccpr(fMockContext.get(), reporter); |
| 146 | if (!ccpr.valid()) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | fPath.moveTo(0, 0); |
| 151 | fPath.cubicTo(50, 50, 0, 50, 50, 0); |
| 152 | this->onRun(reporter, ccpr); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 155 | virtual ~CCPRTest() {} |
| 156 | |
| 157 | protected: |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 158 | virtual void customizeMockOptions(GrMockOptions*) {} |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 159 | virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0; |
| 160 | |
| 161 | sk_sp<GrContext> fMockContext; |
| 162 | SkPath fPath; |
| 163 | }; |
| 164 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 165 | #define DEF_CCPR_TEST(name) \ |
| 166 | DEF_GPUTEST(name, reporter, /* options */) { \ |
| 167 | name test; \ |
| 168 | test.run(reporter); \ |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 169 | } |
| 170 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 171 | class GrCCPRTest_cleanup : public CCPRTest { |
| 172 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 173 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 174 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 175 | // Ensure paths get unreffed. |
| 176 | for (int i = 0; i < 10; ++i) { |
| 177 | ccpr.drawPath(fPath); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 178 | } |
| 179 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 180 | ccpr.flush(); |
| 181 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 182 | |
| 183 | // Ensure clip paths get unreffed. |
| 184 | for (int i = 0; i < 10; ++i) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 185 | ccpr.clipFullscreenRect(fPath); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 186 | } |
| 187 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 188 | ccpr.flush(); |
| 189 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 190 | |
| 191 | // Ensure paths get unreffed when we delete the context without flushing. |
| 192 | for (int i = 0; i < 10; ++i) { |
| 193 | ccpr.drawPath(fPath); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 194 | ccpr.clipFullscreenRect(fPath); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 195 | } |
| 196 | ccpr.abandonGrContext(); |
| 197 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 198 | fMockContext.reset(); |
| 199 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 200 | } |
| 201 | }; |
| 202 | DEF_CCPR_TEST(GrCCPRTest_cleanup) |
| 203 | |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 204 | class GrCCPRTest_cleanupWithTexAllocFail : public GrCCPRTest_cleanup { |
| 205 | void customizeMockOptions(GrMockOptions* options) override { |
| 206 | options->fFailTextureAllocations = true; |
| 207 | } |
| 208 | }; |
| 209 | DEF_CCPR_TEST(GrCCPRTest_cleanupWithTexAllocFail) |
| 210 | |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 211 | class GrCCPRTest_unregisterCulledOps : public CCPRTest { |
| 212 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 213 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 214 | |
| 215 | // Ensure Ops get unregistered from CCPR when culled early. |
| 216 | ccpr.drawPath(fPath); |
| 217 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 218 | ccpr.clear(); // Clear should delete the CCPR Op. |
| 219 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 220 | ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself). |
| 221 | |
| 222 | // Ensure Op unregisters work when we delete the context without flushing. |
| 223 | ccpr.drawPath(fPath); |
| 224 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 225 | ccpr.clear(); // Clear should delete the CCPR DrawPathsOp. |
| 226 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 227 | ccpr.abandonGrContext(); |
| 228 | fMockContext.reset(); // Should not crash (DrawPathsOp should have unregistered itself). |
| 229 | } |
| 230 | }; |
| 231 | DEF_CCPR_TEST(GrCCPRTest_unregisterCulledOps) |
| 232 | |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 233 | class GrCCPRTest_parseEmptyPath : public CCPRTest { |
| 234 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 235 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 236 | |
| 237 | // Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with |
| 238 | // an empty path. |
| 239 | SkPath largeOutsidePath; |
| 240 | largeOutsidePath.moveTo(-1e30f, -1e30f); |
| 241 | largeOutsidePath.lineTo(-1e30f, +1e30f); |
| 242 | largeOutsidePath.lineTo(-1e10f, +1e30f); |
| 243 | ccpr.drawPath(largeOutsidePath); |
| 244 | |
| 245 | // Normally an empty path is culled before reaching ccpr, however we use a back door for |
| 246 | // testing so this path will make it. |
| 247 | SkPath emptyPath; |
| 248 | SkASSERT(emptyPath.isEmpty()); |
| 249 | ccpr.drawPath(emptyPath); |
| 250 | |
| 251 | // This is the test. It will exercise various internal asserts and verify we do not crash. |
| 252 | ccpr.flush(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 253 | |
| 254 | // Now try again with clips. |
| 255 | ccpr.clipFullscreenRect(largeOutsidePath); |
| 256 | ccpr.clipFullscreenRect(emptyPath); |
| 257 | ccpr.flush(); |
| 258 | |
| 259 | // ... and both. |
| 260 | ccpr.drawPath(largeOutsidePath); |
| 261 | ccpr.clipFullscreenRect(largeOutsidePath); |
| 262 | ccpr.drawPath(emptyPath); |
| 263 | ccpr.clipFullscreenRect(emptyPath); |
| 264 | ccpr.flush(); |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | DEF_CCPR_TEST(GrCCPRTest_parseEmptyPath) |
| 268 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 269 | class CCPRRenderingTest { |
| 270 | public: |
| 271 | void run(skiatest::Reporter* reporter, GrContext* ctx) const { |
| 272 | if (!ctx->contextPriv().drawingManager()->getCoverageCountingPathRenderer()) { |
| 273 | return; // CCPR is not enabled on this GPU. |
| 274 | } |
| 275 | CCPRPathDrawer ccpr(ctx, reporter); |
| 276 | if (!ccpr.valid()) { |
| 277 | return; |
| 278 | } |
| 279 | this->onRun(reporter, ccpr); |
| 280 | } |
| 281 | |
| 282 | virtual ~CCPRRenderingTest() {} |
| 283 | |
| 284 | protected: |
| 285 | virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0; |
| 286 | }; |
| 287 | |
| 288 | #define DEF_CCPR_RENDERING_TEST(name) \ |
| 289 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \ |
| 290 | name test; \ |
| 291 | test.run(reporter, ctxInfo.grContext()); \ |
| 292 | } |
| 293 | |
| 294 | class GrCCPRTest_busyPath : public CCPRRenderingTest { |
| 295 | void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override { |
| 296 | static constexpr int kNumBusyVerbs = 1 << 17; |
| 297 | ccpr.clear(); |
| 298 | SkPath busyPath; |
| 299 | busyPath.moveTo(0, 0); // top left |
| 300 | busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right |
| 301 | for (int i = 2; i < kNumBusyVerbs; ++i) { |
| 302 | float offset = i * ((float)kCanvasSize / kNumBusyVerbs); |
| 303 | busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen |
| 304 | } |
| 305 | ccpr.drawPath(busyPath); |
| 306 | |
| 307 | ccpr.flush(); // If this doesn't crash, the test passed. |
| 308 | // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in |
| 309 | // your platform's GrGLCaps. |
| 310 | } |
| 311 | }; |
| 312 | DEF_CCPR_RENDERING_TEST(GrCCPRTest_busyPath) |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 313 | |
| 314 | #endif |