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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "tests/Test.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkMatrix.h" |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 12 | #include "include/core/SkPathBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkRect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrDirectContext.h" |
| 15 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/gpu/mock/GrMockTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkPathPriv.h" |
| 18 | #include "src/gpu/GrClip.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrDrawingManager.h" |
| 21 | #include "src/gpu/GrPaint.h" |
| 22 | #include "src/gpu/GrPathRenderer.h" |
| 23 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrSurfaceDrawContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 25 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/ccpr/GrCCPathCache.h" |
| 27 | #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 28 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "tools/ToolUtils.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 30 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 31 | #include <cmath> |
| 32 | |
| 33 | static constexpr int kCanvasSize = 100; |
| 34 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 35 | enum class DoCoverageCount { kNo = false, kYes }; |
| 36 | enum class DoStroke { kNo = false, kYes }; |
| 37 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 38 | class CCPRClip : public GrClip { |
| 39 | public: |
| 40 | CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {} |
| 41 | |
| 42 | private: |
Michael Ludwig | e06a897 | 2020-06-11 10:29:00 -0400 | [diff] [blame] | 43 | SkIRect getConservativeBounds() const final { return fPath.getBounds().roundOut(); } |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 44 | Effect apply(GrRecordingContext* context, GrSurfaceDrawContext* rtc, GrAAType, |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 45 | bool hasUserStencilSettings, GrAppliedClip* out, |
| 46 | SkRect* bounds) const override { |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 47 | out->addCoverageFP(fCCPR->makeClipProcessor( |
| 48 | /*inputFP=*/nullptr, rtc->getOpsTask()->uniqueID(), fPath, |
| 49 | SkIRect::MakeWH(rtc->width(), rtc->height()), *context->priv().caps())); |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 50 | return Effect::kClipped; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 51 | } |
Michael Ludwig | c002d56 | 2020-05-13 14:17:57 -0400 | [diff] [blame] | 52 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 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: |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 59 | CCPRPathDrawer(sk_sp<GrDirectContext> dContext, skiatest::Reporter* reporter, DoStroke doStroke) |
| 60 | : fDContext(dContext) |
| 61 | , fCCPR(fDContext->priv().drawingManager()->getCoverageCountingPathRenderer()) |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 62 | , fRTC(GrSurfaceDrawContext::Make( |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 63 | fDContext.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 64 | {kCanvasSize, kCanvasSize})) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 65 | , fDoStroke(DoStroke::kYes == doStroke) { |
Chris Dalton | 5501b59 | 2021-01-20 15:00:54 -0700 | [diff] [blame] | 66 | #if 0 |
John Stiles | 8f7689c | 2021-01-15 21:38:27 +0000 | [diff] [blame] | 67 | if (!fCCPR) { |
| 68 | ERRORF(reporter, "ccpr not enabled in GrDirectContext for ccpr tests"); |
| 69 | } |
Chris Dalton | 5501b59 | 2021-01-20 15:00:54 -0700 | [diff] [blame] | 70 | #endif |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 71 | if (!fRTC) { |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 72 | ERRORF(reporter, "failed to create GrSurfaceDrawContext for ccpr tests"); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 76 | GrDirectContext* dContext() const { return fDContext.get(); } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 77 | GrCoverageCountingPathRenderer* ccpr() const { return fCCPR; } |
| 78 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 79 | bool valid() const { return fCCPR && fRTC; } |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 80 | void clear() const { fRTC->clear(SK_PMColor4fTRANSPARENT); } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 81 | void destroyGrContext() { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 82 | SkASSERT(fDContext->unique()); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 83 | fRTC.reset(); |
| 84 | fCCPR = nullptr; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 85 | fDContext.reset(); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 86 | } |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 87 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 88 | void drawPath(const SkPath& path, const SkMatrix& matrix = SkMatrix::I()) const { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 89 | SkASSERT(this->valid()); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 90 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 91 | GrPaint paint; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 92 | paint.setColor4f({ 0, 1, 0, 1 }); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 93 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 94 | SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 95 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 96 | GrStyledShape shape; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 97 | if (!fDoStroke) { |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 98 | shape = GrStyledShape(path); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 99 | } else { |
| 100 | // Use hairlines for now, since they are the only stroke type that doesn't require a |
| 101 | // rigid-body transform. The CCPR stroke code makes no distinction between hairlines |
| 102 | // and regular strokes other than how it decides the device-space stroke width. |
| 103 | SkStrokeRec stroke(SkStrokeRec::kHairline_InitStyle); |
| 104 | stroke.setStrokeParams(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 4); |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 105 | shape = GrStyledShape(path, GrStyle(stroke, nullptr)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 106 | } |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 107 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 108 | fCCPR->testingOnly_drawPathDirectly({ |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 109 | fDContext.get(), std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), |
| 110 | nullptr, &clipBounds, &matrix, &shape, GrAAType::kCoverage, false}); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 113 | void clipFullscreenRect(SkPath clipPath, SkPMColor4f color = { 0, 1, 0, 1 }) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 114 | SkASSERT(this->valid()); |
| 115 | |
| 116 | GrPaint paint; |
| 117 | paint.setColor4f(color); |
| 118 | |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 119 | CCPRClip clip(fCCPR, clipPath); |
| 120 | fRTC->drawRect(&clip, std::move(paint), GrAA::kYes, SkMatrix::I(), |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 121 | SkRect::MakeIWH(kCanvasSize, kCanvasSize)); |
| 122 | } |
| 123 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 124 | void flush() const { |
| 125 | SkASSERT(this->valid()); |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 126 | fDContext->flushAndSubmit(); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 130 | sk_sp<GrDirectContext> fDContext; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 131 | GrCoverageCountingPathRenderer* fCCPR; |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 132 | std::unique_ptr<GrSurfaceDrawContext> fRTC; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 133 | const bool fDoStroke; |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 134 | }; |
| 135 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 136 | class CCPRTest { |
| 137 | public: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 138 | void run(skiatest::Reporter* reporter, DoCoverageCount doCoverageCount, DoStroke doStroke) { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 139 | GrMockOptions mockOptions; |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 140 | mockOptions.fDrawInstancedSupport = true; |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 141 | mockOptions.fHalfFloatVertexAttributeSupport = true; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 142 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
Robert Phillips | a5e78be | 2019-07-09 12:34:38 -0400 | [diff] [blame] | 143 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability = |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 144 | GrMockOptions::ConfigOptions::Renderability::kNonMSAA; |
Robert Phillips | a5e78be | 2019-07-09 12:34:38 -0400 | [diff] [blame] | 145 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true; |
| 146 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 147 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
Robert Phillips | a5e78be | 2019-07-09 12:34:38 -0400 | [diff] [blame] | 148 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 149 | mockOptions.fGeometryShaderSupport = true; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 150 | mockOptions.fIntegerSupport = true; |
| 151 | mockOptions.fFlatInterpolationSupport = true; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 152 | |
| 153 | GrContextOptions ctxOptions; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 154 | ctxOptions.fDisableCoverageCountingPaths = (DoCoverageCount::kNo == doCoverageCount); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 155 | ctxOptions.fAllowPathMaskCaching = false; |
| 156 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting; |
| 157 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 158 | this->customizeOptions(&mockOptions, &ctxOptions); |
| 159 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 160 | sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&mockOptions, ctxOptions); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 161 | if (!mockContext) { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 162 | ERRORF(reporter, "could not create mock context"); |
| 163 | return; |
| 164 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 165 | if (!mockContext->unique()) { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 166 | ERRORF(reporter, "mock context is not unique"); |
| 167 | return; |
| 168 | } |
| 169 | |
Adlai Holler | 5ba50af | 2020-04-29 21:11:14 -0400 | [diff] [blame] | 170 | CCPRPathDrawer ccpr(std::exchange(mockContext, nullptr), reporter, doStroke); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 171 | if (!ccpr.valid()) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | fPath.moveTo(0, 0); |
| 176 | fPath.cubicTo(50, 50, 0, 50, 50, 0); |
| 177 | this->onRun(reporter, ccpr); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 180 | virtual ~CCPRTest() {} |
| 181 | |
| 182 | protected: |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 183 | virtual void customizeOptions(GrMockOptions*, GrContextOptions*) {} |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 184 | virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0; |
| 185 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 186 | SkPath fPath; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 187 | }; |
| 188 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 189 | #define DEF_CCPR_TEST(name) \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 190 | DEF_GPUTEST(name, reporter, /* options */) { \ |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 191 | name test; \ |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 192 | test.run(reporter, DoCoverageCount::kYes, DoStroke::kNo); \ |
| 193 | test.run(reporter, DoCoverageCount::kYes, DoStroke::kYes); \ |
| 194 | test.run(reporter, DoCoverageCount::kNo, DoStroke::kNo); \ |
| 195 | /* FIXME: test.run(reporter, (DoCoverageCount::kNo, DoStroke::kYes) once supported. */ \ |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 198 | class CCPR_cleanup : public CCPRTest { |
Chris Dalton | 5a5fe79 | 2020-02-15 11:41:30 -0700 | [diff] [blame] | 199 | protected: |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 200 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 201 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 202 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 203 | // Ensure paths get unreffed. |
| 204 | for (int i = 0; i < 10; ++i) { |
| 205 | ccpr.drawPath(fPath); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 206 | } |
| 207 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 208 | ccpr.flush(); |
| 209 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 210 | |
| 211 | // Ensure clip paths get unreffed. |
| 212 | for (int i = 0; i < 10; ++i) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 213 | ccpr.clipFullscreenRect(fPath); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 214 | } |
| 215 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 216 | ccpr.flush(); |
| 217 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 218 | |
| 219 | // Ensure paths get unreffed when we delete the context without flushing. |
| 220 | for (int i = 0; i < 10; ++i) { |
| 221 | ccpr.drawPath(fPath); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 222 | ccpr.clipFullscreenRect(fPath); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 223 | } |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 224 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 225 | |
| 226 | ccpr.destroyGrContext(); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 227 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 228 | } |
| 229 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 230 | DEF_CCPR_TEST(CCPR_cleanup) |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 231 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 232 | class CCPR_cleanupWithTexAllocFail : public CCPR_cleanup { |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 233 | void customizeOptions(GrMockOptions* mockOptions, GrContextOptions*) override { |
| 234 | mockOptions->fFailTextureAllocations = true; |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 235 | } |
Chris Dalton | 5a5fe79 | 2020-02-15 11:41:30 -0700 | [diff] [blame] | 236 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 237 | ((GrRecordingContext*)ccpr.dContext())->priv().incrSuppressWarningMessages(); |
Chris Dalton | 5a5fe79 | 2020-02-15 11:41:30 -0700 | [diff] [blame] | 238 | this->CCPR_cleanup::onRun(reporter, ccpr); |
| 239 | } |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 240 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 241 | DEF_CCPR_TEST(CCPR_cleanupWithTexAllocFail) |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 242 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 243 | class CCPR_unregisterCulledOps : public CCPRTest { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 244 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 245 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 246 | |
| 247 | // Ensure Ops get unregistered from CCPR when culled early. |
| 248 | ccpr.drawPath(fPath); |
| 249 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 250 | ccpr.clear(); // Clear should delete the CCPR Op. |
| 251 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 252 | ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself). |
| 253 | |
| 254 | // Ensure Op unregisters work when we delete the context without flushing. |
| 255 | ccpr.drawPath(fPath); |
| 256 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 257 | ccpr.clear(); // Clear should delete the CCPR DrawPathsOp. |
| 258 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 259 | ccpr.destroyGrContext(); // Should not crash (DrawPathsOp should have unregistered itself). |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 260 | } |
| 261 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 262 | DEF_CCPR_TEST(CCPR_unregisterCulledOps) |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 263 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 264 | class CCPR_parseEmptyPath : public CCPRTest { |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 265 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 266 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 267 | |
| 268 | // Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with |
| 269 | // an empty path. |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 270 | SkPath largeOutsidePath = SkPath::Polygon({ |
| 271 | {-1e30f, -1e30f}, |
| 272 | {-1e30f, +1e30f}, |
| 273 | {-1e10f, +1e30f}, |
| 274 | }, false); |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 275 | ccpr.drawPath(largeOutsidePath); |
| 276 | |
| 277 | // Normally an empty path is culled before reaching ccpr, however we use a back door for |
| 278 | // testing so this path will make it. |
| 279 | SkPath emptyPath; |
| 280 | SkASSERT(emptyPath.isEmpty()); |
| 281 | ccpr.drawPath(emptyPath); |
| 282 | |
| 283 | // This is the test. It will exercise various internal asserts and verify we do not crash. |
| 284 | ccpr.flush(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 285 | |
| 286 | // Now try again with clips. |
| 287 | ccpr.clipFullscreenRect(largeOutsidePath); |
| 288 | ccpr.clipFullscreenRect(emptyPath); |
| 289 | ccpr.flush(); |
| 290 | |
| 291 | // ... and both. |
| 292 | ccpr.drawPath(largeOutsidePath); |
| 293 | ccpr.clipFullscreenRect(largeOutsidePath); |
| 294 | ccpr.drawPath(emptyPath); |
| 295 | ccpr.clipFullscreenRect(emptyPath); |
| 296 | ccpr.flush(); |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 297 | } |
| 298 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 299 | DEF_CCPR_TEST(CCPR_parseEmptyPath) |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 300 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 301 | static int get_mock_texture_id(const GrTexture* texture) { |
| 302 | const GrBackendTexture& backingTexture = texture->getBackendTexture(); |
| 303 | SkASSERT(GrBackendApi::kMock == backingTexture.backend()); |
| 304 | |
| 305 | if (!backingTexture.isValid()) { |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | GrMockTextureInfo info; |
| 310 | backingTexture.getMockTextureInfo(&info); |
Robert Phillips | a27d625 | 2019-12-10 14:48:36 -0500 | [diff] [blame] | 311 | return info.id(); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // Base class for cache path unit tests. |
| 315 | class CCPRCacheTest : public CCPRTest { |
| 316 | protected: |
| 317 | // Registers as an onFlush callback in order to snag the CCPR per-flush resources and note the |
| 318 | // texture IDs. |
| 319 | class RecordLastMockAtlasIDs : public GrOnFlushCallbackObject { |
| 320 | public: |
| 321 | RecordLastMockAtlasIDs(sk_sp<GrCoverageCountingPathRenderer> ccpr) : fCCPR(ccpr) {} |
| 322 | |
| 323 | int lastCopyAtlasID() const { return fLastCopyAtlasID; } |
| 324 | int lastRenderedAtlasID() const { return fLastRenderedAtlasID; } |
| 325 | |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 326 | void preFlush(GrOnFlushResourceProvider*, SkSpan<const uint32_t>) override { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 327 | fLastRenderedAtlasID = fLastCopyAtlasID = 0; |
| 328 | |
| 329 | const GrCCPerFlushResources* resources = fCCPR->testingOnly_getCurrentFlushResources(); |
| 330 | if (!resources) { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | if (const GrTexture* tex = resources->testingOnly_frontCopyAtlasTexture()) { |
| 335 | fLastCopyAtlasID = get_mock_texture_id(tex); |
| 336 | } |
| 337 | if (const GrTexture* tex = resources->testingOnly_frontRenderedAtlasTexture()) { |
| 338 | fLastRenderedAtlasID = get_mock_texture_id(tex); |
| 339 | } |
| 340 | } |
| 341 | |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 342 | void postFlush(GrDeferredUploadToken, SkSpan<const uint32_t>) override {} |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 343 | |
| 344 | private: |
| 345 | sk_sp<GrCoverageCountingPathRenderer> fCCPR; |
| 346 | int fLastCopyAtlasID = 0; |
| 347 | int fLastRenderedAtlasID = 0; |
| 348 | }; |
| 349 | |
| 350 | CCPRCacheTest() { |
| 351 | static constexpr int primes[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; |
| 352 | |
| 353 | SkRandom rand; |
| 354 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) { |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 355 | int numPts = rand.nextRangeU(GrStyledShape::kMaxKeyFromDataVerbCnt + 1, |
| 356 | GrStyledShape::kMaxKeyFromDataVerbCnt * 2); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 357 | int step; |
| 358 | do { |
| 359 | step = primes[rand.nextU() % SK_ARRAY_COUNT(primes)]; |
| 360 | } while (step == numPts); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 361 | fPaths[i] = ToolUtils::make_star(SkRect::MakeLTRB(0, 0, 1, 1), numPts, step); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix& m) { |
| 366 | this->drawPathsAndFlush(ccpr, &m, 1); |
| 367 | } |
| 368 | void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix* matrices, int numMatrices) { |
| 369 | // Draw all the paths. |
| 370 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) { |
| 371 | ccpr.drawPath(fPaths[i], matrices[i % numMatrices]); |
| 372 | } |
| 373 | // Re-draw a few paths, to test the case where a cache entry is hit more than once in a |
| 374 | // single flush. |
| 375 | SkRandom rand; |
| 376 | int duplicateIndices[10]; |
| 377 | for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) { |
| 378 | duplicateIndices[i] = rand.nextULessThan(SK_ARRAY_COUNT(fPaths)); |
| 379 | } |
| 380 | for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) { |
| 381 | for (size_t j = 0; j <= i; ++j) { |
| 382 | int idx = duplicateIndices[j]; |
| 383 | ccpr.drawPath(fPaths[idx], matrices[idx % numMatrices]); |
| 384 | } |
| 385 | } |
| 386 | ccpr.flush(); |
| 387 | } |
| 388 | |
| 389 | private: |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 390 | void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override { |
| 391 | ctxOptions->fAllowPathMaskCaching = true; |
| 392 | } |
| 393 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 394 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) final { |
| 395 | RecordLastMockAtlasIDs atlasIDRecorder(sk_ref_sp(ccpr.ccpr())); |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 396 | ccpr.dContext()->priv().addOnFlushCallbackObject(&atlasIDRecorder); |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 397 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 398 | this->onRun(reporter, ccpr, atlasIDRecorder); |
| 399 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 400 | ccpr.dContext()->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&atlasIDRecorder); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 404 | const RecordLastMockAtlasIDs&) = 0; |
| 405 | |
| 406 | protected: |
| 407 | SkPath fPaths[350]; |
| 408 | }; |
| 409 | |
| 410 | // Ensures ccpr always reuses the same atlas texture in the animation use case. |
| 411 | class CCPR_cache_animationAtlasReuse : public CCPRCacheTest { |
| 412 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 413 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 414 | SkMatrix m = SkMatrix::Translate(kCanvasSize/2, kCanvasSize/2); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 415 | m.preScale(80, 80); |
| 416 | m.preTranslate(-.5,-.5); |
| 417 | this->drawPathsAndFlush(ccpr, m); |
| 418 | |
| 419 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 420 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 421 | const int atlasID = atlasIDRecorder.lastRenderedAtlasID(); |
| 422 | |
| 423 | // Ensures we always reuse the same atlas texture in the animation use case. |
| 424 | for (int i = 0; i < 12; ++i) { |
| 425 | // 59 is prime, so we will hit every integer modulo 360 before repeating. |
| 426 | m.preRotate(59, .5, .5); |
| 427 | |
| 428 | // Go twice. Paths have to get drawn twice with the same matrix before we cache their |
| 429 | // atlas. This makes sure that on the subsequent draw, after an atlas has been cached |
| 430 | // and is then invalidated since the matrix will change, that the same underlying |
| 431 | // texture object is still reused for the next atlas. |
| 432 | for (int j = 0; j < 2; ++j) { |
| 433 | this->drawPathsAndFlush(ccpr, m); |
| 434 | // Nothing should be copied to an 8-bit atlas after just two draws. |
| 435 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 436 | REPORTER_ASSERT(reporter, atlasIDRecorder.lastRenderedAtlasID() == atlasID); |
| 437 | } |
Chris Dalton | 2e825a3 | 2019-01-04 22:14:27 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 440 | // Do the last draw again. (On draw 3 they should get copied to an 8-bit atlas.) |
| 441 | this->drawPathsAndFlush(ccpr, m); |
| 442 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 443 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 444 | |
| 445 | // Now double-check that everything continues to hit the cache as expected when the matrix |
| 446 | // doesn't change. |
| 447 | for (int i = 0; i < 10; ++i) { |
| 448 | this->drawPathsAndFlush(ccpr, m); |
| 449 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 450 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 451 | } |
| 452 | } |
| 453 | }; |
| 454 | DEF_CCPR_TEST(CCPR_cache_animationAtlasReuse) |
| 455 | |
| 456 | class CCPR_cache_recycleEntries : public CCPRCacheTest { |
| 457 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 458 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 459 | SkMatrix m = SkMatrix::Translate(kCanvasSize/2, kCanvasSize/2); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 460 | m.preScale(80, 80); |
| 461 | m.preTranslate(-.5,-.5); |
| 462 | |
| 463 | auto cache = ccpr.ccpr()->testingOnly_getPathCache(); |
| 464 | REPORTER_ASSERT(reporter, cache); |
| 465 | |
| 466 | const auto& lru = cache->testingOnly_getLRU(); |
| 467 | |
| 468 | SkTArray<const void*> expectedPtrs; |
| 469 | |
| 470 | // Ensures we always reuse the same atlas texture in the animation use case. |
| 471 | for (int i = 0; i < 5; ++i) { |
| 472 | // 59 is prime, so we will hit every integer modulo 360 before repeating. |
| 473 | m.preRotate(59, .5, .5); |
| 474 | |
| 475 | // Go twice. Paths have to get drawn twice with the same matrix before we cache their |
| 476 | // atlas. |
| 477 | for (int j = 0; j < 2; ++j) { |
| 478 | this->drawPathsAndFlush(ccpr, m); |
| 479 | // Nothing should be copied to an 8-bit atlas after just two draws. |
| 480 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 481 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 482 | } |
| 483 | |
| 484 | int idx = 0; |
| 485 | for (const GrCCPathCacheEntry* entry : lru) { |
| 486 | if (0 == i) { |
| 487 | expectedPtrs.push_back(entry); |
| 488 | } else { |
| 489 | // The same pointer should have been recycled for the new matrix. |
| 490 | REPORTER_ASSERT(reporter, entry == expectedPtrs[idx]); |
| 491 | } |
| 492 | ++idx; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | }; |
| 497 | DEF_CCPR_TEST(CCPR_cache_recycleEntries) |
| 498 | |
| 499 | // Ensures mostly-visible paths get their full mask cached. |
| 500 | class CCPR_cache_mostlyVisible : public CCPRCacheTest { |
| 501 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 502 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
| 503 | SkMatrix matrices[3] = { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 504 | SkMatrix::Scale(kCanvasSize/2, kCanvasSize/2), // Fully visible. |
| 505 | SkMatrix::Scale(kCanvasSize * 1.25, kCanvasSize * 1.25), // Mostly visible. |
| 506 | SkMatrix::Scale(kCanvasSize * 1.5, kCanvasSize * 1.5), // Mostly NOT visible. |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | for (int i = 0; i < 10; ++i) { |
| 510 | this->drawPathsAndFlush(ccpr, matrices, 3); |
| 511 | if (2 == i) { |
| 512 | // The mostly-visible paths should still get cached. |
| 513 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 514 | } else { |
| 515 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 516 | } |
| 517 | // Ensure mostly NOT-visible paths never get cached. |
| 518 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 519 | } |
| 520 | |
| 521 | // Clear the path cache. |
| 522 | this->drawPathsAndFlush(ccpr, SkMatrix::I()); |
| 523 | |
| 524 | // Now only draw the fully/mostly visible ones. |
| 525 | for (int i = 0; i < 2; ++i) { |
| 526 | this->drawPathsAndFlush(ccpr, matrices, 2); |
| 527 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 528 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 529 | } |
| 530 | |
| 531 | // On draw 3 they should get copied to an 8-bit atlas. |
| 532 | this->drawPathsAndFlush(ccpr, matrices, 2); |
| 533 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 534 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 535 | |
| 536 | for (int i = 0; i < 10; ++i) { |
| 537 | this->drawPathsAndFlush(ccpr, matrices, 2); |
| 538 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 539 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 540 | } |
| 541 | |
| 542 | // Draw a different part of the path to ensure the full mask was cached. |
| 543 | matrices[1].postTranslate(SkScalarFloorToInt(kCanvasSize * -.25f), |
| 544 | SkScalarFloorToInt(kCanvasSize * -.25f)); |
| 545 | for (int i = 0; i < 10; ++i) { |
| 546 | this->drawPathsAndFlush(ccpr, matrices, 2); |
| 547 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 548 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 549 | } |
| 550 | } |
| 551 | }; |
| 552 | DEF_CCPR_TEST(CCPR_cache_mostlyVisible) |
| 553 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 554 | // Ensures GrDirectContext::performDeferredCleanup works. |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 555 | class CCPR_cache_deferredCleanup : public CCPRCacheTest { |
| 556 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 557 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 558 | SkMatrix m = SkMatrix::Scale(20, 20); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 559 | int lastRenderedAtlasID = 0; |
| 560 | |
| 561 | for (int i = 0; i < 5; ++i) { |
| 562 | this->drawPathsAndFlush(ccpr, m); |
| 563 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 564 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 565 | int renderedAtlasID = atlasIDRecorder.lastRenderedAtlasID(); |
| 566 | REPORTER_ASSERT(reporter, renderedAtlasID != lastRenderedAtlasID); |
| 567 | lastRenderedAtlasID = renderedAtlasID; |
| 568 | |
| 569 | this->drawPathsAndFlush(ccpr, m); |
| 570 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 571 | REPORTER_ASSERT(reporter, lastRenderedAtlasID == atlasIDRecorder.lastRenderedAtlasID()); |
| 572 | |
| 573 | // On draw 3 they should get copied to an 8-bit atlas. |
| 574 | this->drawPathsAndFlush(ccpr, m); |
| 575 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 576 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 577 | |
| 578 | for (int i = 0; i < 10; ++i) { |
| 579 | this->drawPathsAndFlush(ccpr, m); |
| 580 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 581 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 582 | } |
| 583 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 584 | ccpr.dContext()->performDeferredCleanup(std::chrono::milliseconds(0)); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | }; |
| 588 | DEF_CCPR_TEST(CCPR_cache_deferredCleanup) |
| 589 | |
| 590 | // Verifies the cache/hash table internals. |
| 591 | class CCPR_cache_hashTable : public CCPRCacheTest { |
| 592 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 593 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
| 594 | using CoverageType = GrCCAtlas::CoverageType; |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 595 | SkMatrix m = SkMatrix::Scale(20, 20); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 596 | |
| 597 | for (int i = 0; i < 5; ++i) { |
| 598 | this->drawPathsAndFlush(ccpr, m); |
| 599 | if (2 == i) { |
| 600 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 601 | } else { |
| 602 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 603 | } |
| 604 | if (i < 2) { |
| 605 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 606 | } else { |
| 607 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 608 | } |
| 609 | |
| 610 | auto cache = ccpr.ccpr()->testingOnly_getPathCache(); |
| 611 | REPORTER_ASSERT(reporter, cache); |
| 612 | |
| 613 | const auto& hash = cache->testingOnly_getHashTable(); |
| 614 | const auto& lru = cache->testingOnly_getLRU(); |
| 615 | int count = 0; |
| 616 | for (GrCCPathCacheEntry* entry : lru) { |
| 617 | auto* node = hash.find(entry->cacheKey()); |
| 618 | REPORTER_ASSERT(reporter, node); |
| 619 | REPORTER_ASSERT(reporter, node->entry() == entry); |
| 620 | REPORTER_ASSERT(reporter, 0 == entry->testingOnly_peekOnFlushRefCnt()); |
| 621 | REPORTER_ASSERT(reporter, entry->unique()); |
| 622 | if (0 == i) { |
| 623 | REPORTER_ASSERT(reporter, !entry->cachedAtlas()); |
| 624 | } else { |
| 625 | const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas(); |
| 626 | REPORTER_ASSERT(reporter, cachedAtlas); |
| 627 | if (1 == i) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 628 | REPORTER_ASSERT(reporter, ccpr.ccpr()->coverageType() |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 629 | == cachedAtlas->coverageType()); |
| 630 | } else { |
| 631 | REPORTER_ASSERT(reporter, CoverageType::kA8_LiteralCoverage |
| 632 | == cachedAtlas->coverageType()); |
| 633 | } |
| 634 | REPORTER_ASSERT(reporter, cachedAtlas->textureKey().isValid()); |
| 635 | // The actual proxy should not be held past the end of a flush. |
| 636 | REPORTER_ASSERT(reporter, !cachedAtlas->getOnFlushProxy()); |
| 637 | REPORTER_ASSERT(reporter, 0 == cachedAtlas->testingOnly_peekOnFlushRefCnt()); |
| 638 | } |
| 639 | ++count; |
| 640 | } |
| 641 | REPORTER_ASSERT(reporter, hash.count() == count); |
| 642 | } |
| 643 | } |
| 644 | }; |
| 645 | DEF_CCPR_TEST(CCPR_cache_hashTable) |
| 646 | |
| 647 | // Ensures paths get cached even when using a sporadic flushing pattern and drawing out of order |
| 648 | // (a la Chrome tiles). |
| 649 | class CCPR_cache_multiFlush : public CCPRCacheTest { |
| 650 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 651 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
| 652 | static constexpr int kNumPaths = SK_ARRAY_COUNT(fPaths); |
| 653 | static constexpr int kBigPrimes[] = { |
| 654 | 9323, 11059, 22993, 38749, 45127, 53147, 64853, 77969, 83269, 99989}; |
| 655 | |
| 656 | SkRandom rand; |
| 657 | SkMatrix m = SkMatrix::I(); |
| 658 | |
| 659 | for (size_t i = 0; i < SK_ARRAY_COUNT(kBigPrimes); ++i) { |
| 660 | int prime = kBigPrimes[i]; |
| 661 | int endPathIdx = (int)rand.nextULessThan(kNumPaths); |
| 662 | int pathIdx = endPathIdx; |
| 663 | int nextFlush = rand.nextRangeU(1, 47); |
| 664 | for (int j = 0; j < kNumPaths; ++j) { |
| 665 | pathIdx = (pathIdx + prime) % kNumPaths; |
| 666 | int repeat = rand.nextRangeU(1, 3); |
| 667 | for (int k = 0; k < repeat; ++k) { |
| 668 | ccpr.drawPath(fPaths[pathIdx], m); |
| 669 | } |
| 670 | if (nextFlush == j) { |
| 671 | ccpr.flush(); |
| 672 | // The paths are small enough that we should never copy to an A8 atlas. |
| 673 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 674 | if (i < 2) { |
| 675 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 676 | } else { |
| 677 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 678 | } |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 679 | nextFlush = std::min(j + (int)rand.nextRangeU(1, 29), kNumPaths - 1); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | SkASSERT(endPathIdx == pathIdx % kNumPaths); |
| 683 | } |
| 684 | } |
| 685 | }; |
| 686 | DEF_CCPR_TEST(CCPR_cache_multiFlush) |
| 687 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 688 | // Ensures a path drawn over mutiple tiles gets cached. |
| 689 | class CCPR_cache_multiTileCache : public CCPRCacheTest { |
| 690 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 691 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
| 692 | // Make sure a path drawn over 9 tiles gets cached (1 tile out of 9 is >10% visibility). |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 693 | const SkMatrix m0 = SkMatrix::Scale(kCanvasSize*3, kCanvasSize*3); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 694 | const SkPath p0 = fPaths[0]; |
| 695 | for (int i = 0; i < 9; ++i) { |
| 696 | static constexpr int kRowOrder[9] = {0,1,1,0,2,2,2,1,0}; |
| 697 | static constexpr int kColumnOrder[9] = {0,0,1,1,0,1,2,2,2}; |
| 698 | |
| 699 | SkMatrix tileM = m0; |
| 700 | tileM.postTranslate(-kCanvasSize * kColumnOrder[i], -kCanvasSize * kRowOrder[i]); |
| 701 | ccpr.drawPath(p0, tileM); |
| 702 | ccpr.flush(); |
| 703 | if (i < 5) { |
| 704 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 705 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 706 | } else if (5 == i) { |
| 707 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 708 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 709 | } else { |
| 710 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 711 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // Now make sure paths don't get cached when visibility is <10% for every draw (12 tiles). |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 716 | const SkMatrix m1 = SkMatrix::Scale(kCanvasSize*4, kCanvasSize*3); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 717 | const SkPath p1 = fPaths[1]; |
| 718 | for (int row = 0; row < 3; ++row) { |
| 719 | for (int col = 0; col < 4; ++col) { |
| 720 | SkMatrix tileM = m1; |
| 721 | tileM.postTranslate(-kCanvasSize * col, -kCanvasSize * row); |
| 722 | ccpr.drawPath(p1, tileM); |
| 723 | ccpr.flush(); |
| 724 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 725 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // Double-check the cache is still intact. |
| 730 | ccpr.drawPath(p0, m0); |
| 731 | ccpr.flush(); |
| 732 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 733 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 734 | |
| 735 | ccpr.drawPath(p1, m1); |
| 736 | ccpr.flush(); |
| 737 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 738 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID()); |
| 739 | } |
| 740 | }; |
| 741 | DEF_CCPR_TEST(CCPR_cache_multiTileCache) |
| 742 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 743 | // This test exercises CCPR's cache capabilities by drawing many paths with two different |
| 744 | // transformation matrices. We then vary the matrices independently by whole and partial pixels, |
| 745 | // and verify the caching behaved as expected. |
| 746 | class CCPR_cache_partialInvalidate : public CCPRCacheTest { |
| 747 | void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override { |
| 748 | ctxOptions->fAllowPathMaskCaching = true; |
| 749 | } |
| 750 | |
| 751 | static constexpr int kPathSize = 4; |
| 752 | |
| 753 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr, |
| 754 | const RecordLastMockAtlasIDs& atlasIDRecorder) override { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 755 | SkMatrix matrices[2] = { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 756 | SkMatrix::Translate(5, 5), |
| 757 | SkMatrix::Translate(kCanvasSize - kPathSize - 5, kCanvasSize - kPathSize - 5) |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 758 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 759 | matrices[0].preScale(kPathSize, kPathSize); |
| 760 | matrices[1].preScale(kPathSize, kPathSize); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 761 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 762 | int firstAtlasID = 0; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 763 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 764 | for (int iterIdx = 0; iterIdx < 4*3*2; ++iterIdx) { |
| 765 | this->drawPathsAndFlush(ccpr, matrices, 2); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 766 | |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 767 | if (0 == iterIdx) { |
| 768 | // First iteration: just note the ID of the stashed atlas and continue. |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 769 | firstAtlasID = atlasIDRecorder.lastRenderedAtlasID(); |
| 770 | REPORTER_ASSERT(reporter, 0 != firstAtlasID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 771 | continue; |
| 772 | } |
| 773 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 774 | int testIdx = (iterIdx/2) % 3; |
| 775 | int repetitionIdx = iterIdx % 2; |
| 776 | switch (testIdx) { |
| 777 | case 0: |
| 778 | if (0 == repetitionIdx) { |
| 779 | // This is the big test. New paths were drawn twice last round. On hit 2 |
| 780 | // (last time), 'firstAtlasID' was cached as a 16-bit atlas. Now, on hit 3, |
| 781 | // these paths should be copied out of 'firstAtlasID', and into an A8 atlas. |
| 782 | // THEN: we should recycle 'firstAtlasID' and reuse that same texture to |
| 783 | // render the new masks. |
| 784 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 785 | REPORTER_ASSERT(reporter, |
| 786 | atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID); |
| 787 | } else { |
| 788 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 789 | // This is hit 2 for the new masks. Next time they will be copied to an A8 |
| 790 | // atlas. |
| 791 | REPORTER_ASSERT(reporter, |
| 792 | atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID); |
| 793 | } |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 794 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 795 | if (1 == repetitionIdx) { |
| 796 | // Integer translates: all path masks stay valid. |
| 797 | matrices[0].preTranslate(-1, -1); |
| 798 | matrices[1].preTranslate(1, 1); |
| 799 | } |
| 800 | break; |
| 801 | |
| 802 | case 1: |
| 803 | if (0 == repetitionIdx) { |
| 804 | // New paths were drawn twice last round. The third hit (now) they should be |
| 805 | // copied to an A8 atlas. |
| 806 | REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID()); |
| 807 | } else { |
| 808 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
| 809 | } |
| 810 | |
| 811 | // This draw should have gotten 100% cache hits; we only did integer translates |
| 812 | // last time (or none if it was the first flush). Therefore, everything should |
| 813 | // have been cached. |
| 814 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID()); |
| 815 | |
| 816 | if (1 == repetitionIdx) { |
| 817 | // Invalidate even path masks. |
| 818 | matrices[0].preTranslate(1.6f, 1.4f); |
| 819 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 820 | break; |
| 821 | |
| 822 | case 2: |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 823 | // No new masks to copy from last time; it had 100% cache hits. |
| 824 | REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID()); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 825 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 826 | // Even path masks were invalidated last iteration by a subpixel translate. |
| 827 | // They should have been re-rendered this time in the original 'firstAtlasID' |
| 828 | // texture. |
| 829 | REPORTER_ASSERT(reporter, |
| 830 | atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 831 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 832 | if (1 == repetitionIdx) { |
| 833 | // Invalidate odd path masks. |
| 834 | matrices[1].preTranslate(-1.4f, -1.6f); |
| 835 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 841 | DEF_CCPR_TEST(CCPR_cache_partialInvalidate) |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 842 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 843 | class CCPR_unrefPerOpsTaskPathsBeforeOps : public CCPRTest { |
Chris Dalton | dedf8f2 | 2018-09-24 20:23:47 -0600 | [diff] [blame] | 844 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 845 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 846 | for (int i = 0; i < 10000; ++i) { |
| 847 | // Draw enough paths to make the arena allocator hit the heap. |
| 848 | ccpr.drawPath(fPath); |
| 849 | } |
| 850 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 851 | // Unref the GrCCPerOpsTaskPaths object. |
| 852 | auto perOpsTaskPathsMap = ccpr.ccpr()->detachPendingPaths(); |
| 853 | perOpsTaskPathsMap.clear(); |
Chris Dalton | dedf8f2 | 2018-09-24 20:23:47 -0600 | [diff] [blame] | 854 | |
| 855 | // Now delete the Op and all its draws. |
| 856 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 857 | ccpr.flush(); |
| 858 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 859 | } |
| 860 | }; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 861 | DEF_CCPR_TEST(CCPR_unrefPerOpsTaskPathsBeforeOps) |
Chris Dalton | dedf8f2 | 2018-09-24 20:23:47 -0600 | [diff] [blame] | 862 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 863 | class CCPRRenderingTest { |
| 864 | public: |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 865 | void run(skiatest::Reporter* reporter, GrDirectContext* dContext, DoStroke doStroke) const { |
| 866 | if (auto ccpr = dContext->priv().drawingManager()->getCoverageCountingPathRenderer()) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 867 | if (DoStroke::kYes == doStroke && |
| 868 | GrCCAtlas::CoverageType::kA8_Multisample == ccpr->coverageType()) { |
| 869 | return; // Stroking is not yet supported for multisample. |
| 870 | } |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 871 | CCPRPathDrawer drawer(sk_ref_sp(dContext), reporter, doStroke); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 872 | if (!drawer.valid()) { |
| 873 | return; |
| 874 | } |
| 875 | this->onRun(reporter, drawer); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 876 | } |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | virtual ~CCPRRenderingTest() {} |
| 880 | |
| 881 | protected: |
| 882 | virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0; |
| 883 | }; |
| 884 | |
| 885 | #define DEF_CCPR_RENDERING_TEST(name) \ |
| 886 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \ |
| 887 | name test; \ |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 888 | test.run(reporter, ctxInfo.directContext(), DoStroke::kNo); \ |
| 889 | test.run(reporter, ctxInfo.directContext(), DoStroke::kYes); \ |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 890 | } |
| 891 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 892 | class CCPR_busyPath : public CCPRRenderingTest { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 893 | void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override { |
| 894 | static constexpr int kNumBusyVerbs = 1 << 17; |
| 895 | ccpr.clear(); |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 896 | SkPathBuilder busyPath; |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 897 | busyPath.moveTo(0, 0); // top left |
| 898 | busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right |
| 899 | for (int i = 2; i < kNumBusyVerbs; ++i) { |
| 900 | float offset = i * ((float)kCanvasSize / kNumBusyVerbs); |
| 901 | busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen |
| 902 | } |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 903 | ccpr.drawPath(busyPath.detach()); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 904 | |
| 905 | ccpr.flush(); // If this doesn't crash, the test passed. |
| 906 | // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in |
| 907 | // your platform's GrGLCaps. |
| 908 | } |
| 909 | }; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 910 | DEF_CCPR_RENDERING_TEST(CCPR_busyPath) |
Jiulong Wang | df5739c | 2020-10-13 15:09:09 -0700 | [diff] [blame] | 911 | |
| 912 | // https://bugs.chromium.org/p/chromium/issues/detail?id=1102117 |
| 913 | class CCPR_evictCacheEntryForPendingDrawOp : public CCPRRenderingTest { |
| 914 | void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override { |
| 915 | static constexpr SkRect kRect = SkRect::MakeWH(50, 50); |
| 916 | ccpr.clear(); |
| 917 | |
| 918 | // make sure path is cached. |
| 919 | for (int i = 0; i < 2; i++) { |
| 920 | SkPath path; |
| 921 | path.addRect(kRect); |
| 922 | |
| 923 | ccpr.drawPath(path); |
| 924 | ccpr.flush(); |
| 925 | } |
| 926 | |
| 927 | // make enough cached draws to make DoCopies happen. |
| 928 | for (int i = 0; i <= GrCoverageCountingPathRenderer::kDoCopiesThreshold; i++) { |
| 929 | SkPath path; |
| 930 | path.addRect(kRect); |
| 931 | ccpr.drawPath(path); |
| 932 | } |
| 933 | |
| 934 | // now draw the path in an incompatible matrix. Previous draw's cached atlas should |
| 935 | // not be invalidated. otherwise, this flush would render more paths than allocated for. |
| 936 | auto m = SkMatrix::Translate(0.1f, 0.1f); |
| 937 | SkPath path; |
| 938 | path.addRect(kRect); |
| 939 | ccpr.drawPath(path, m); |
| 940 | ccpr.flush(); |
| 941 | |
| 942 | // if this test does not crash, it is passed. |
| 943 | } |
| 944 | }; |
| 945 | DEF_CCPR_RENDERING_TEST(CCPR_evictCacheEntryForPendingDrawOp) |