blob: 6af9ba1a16136d58efdd0fc23d7cd68bd0918f14 [file] [log] [blame]
Chris Daltoncc604e52017-10-06 16:27:32 -06001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
9#include "tests/Test.h"
Chris Daltoncc604e52017-10-06 16:27:32 -060010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
12#include "include/core/SkRect.h"
13#include "include/gpu/GrTexture.h"
14#include "include/gpu/mock/GrMockTypes.h"
15#include "include/private/GrRecordingContext.h"
16#include "src/core/SkExchange.h"
17#include "src/core/SkPathPriv.h"
18#include "src/gpu/GrClip.h"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrDrawingManager.h"
21#include "src/gpu/GrPaint.h"
22#include "src/gpu/GrPathRenderer.h"
23#include "src/gpu/GrRecordingContextPriv.h"
24#include "src/gpu/GrRenderTargetContext.h"
25#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/ccpr/GrCCPathCache.h"
27#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040028#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "tools/ToolUtils.h"
Hal Canary8a001442018-09-19 11:31:27 -040030
Chris Daltoncc604e52017-10-06 16:27:32 -060031#include <cmath>
32
33static constexpr int kCanvasSize = 100;
34
Chris Daltonc3318f02019-07-19 14:20:53 -060035enum class DoCoverageCount { kNo = false, kYes };
36enum class DoStroke { kNo = false, kYes };
37
Chris Daltona32a3c32017-12-05 10:05:21 -070038class CCPRClip : public GrClip {
39public:
40 CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {}
41
42private:
Robert Phillips6f0e02f2019-02-13 11:02:28 -050043 bool apply(GrRecordingContext* context, GrRenderTargetContext* rtc, bool useHWAA,
44 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
Greg Danielf41b2bd2019-08-22 16:19:24 -040045 out->addCoverageFP(fCCPR->makeClipProcessor(rtc->priv().testingOnly_getOpsTaskID(), fPath,
Chris Daltona32a3c32017-12-05 10:05:21 -070046 SkIRect::MakeWH(rtc->width(), rtc->height()),
Robert Phillips9da87e02019-02-04 13:26:26 -050047 *context->priv().caps()));
Chris Daltona32a3c32017-12-05 10:05:21 -070048 return true;
49 }
50 bool quickContains(const SkRect&) const final { return false; }
51 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
52 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
Mike Reed92b33352019-08-24 19:39:13 -040053 rect->setWH(width, height);
Chris Daltona32a3c32017-12-05 10:05:21 -070054 if (iior) {
55 *iior = false;
56 }
57 }
58 GrCoverageCountingPathRenderer* const fCCPR;
59 const SkPath fPath;
60};
61
Chris Daltoncc604e52017-10-06 16:27:32 -060062class CCPRPathDrawer {
63public:
Chris Daltonc3318f02019-07-19 14:20:53 -060064 CCPRPathDrawer(sk_sp<GrContext> ctx, skiatest::Reporter* reporter, DoStroke doStroke)
Chris Daltoncc604e52017-10-06 16:27:32 -060065 : fCtx(ctx)
Robert Phillips9da87e02019-02-04 13:26:26 -050066 , fCCPR(fCtx->priv().drawingManager()->getCoverageCountingPathRenderer())
Greg Daniele20fcad2020-01-08 11:52:34 -050067 , fRTC(GrRenderTargetContext::Make(
68 fCtx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
69 {kCanvasSize, kCanvasSize}))
Chris Daltonc3318f02019-07-19 14:20:53 -060070 , fDoStroke(DoStroke::kYes == doStroke) {
Chris Daltonfddb6c02017-11-04 15:22:22 -060071 if (!fCCPR) {
72 ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests");
73 }
74 if (!fRTC) {
75 ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests");
Chris Daltoncc604e52017-10-06 16:27:32 -060076 }
77 }
78
Chris Dalton351e80c2019-01-06 22:51:00 -070079 GrContext* ctx() const { return fCtx.get(); }
Chris Dalton4da70192018-06-18 09:51:36 -060080 GrCoverageCountingPathRenderer* ccpr() const { return fCCPR; }
81
Chris Daltonfddb6c02017-11-04 15:22:22 -060082 bool valid() const { return fCCPR && fRTC; }
Brian Osman9a9baae2018-11-05 15:06:26 -050083 void clear() const { fRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
84 GrRenderTargetContext::CanClearFullscreen::kYes); }
Chris Dalton351e80c2019-01-06 22:51:00 -070085 void destroyGrContext() {
Chris Dalton351e80c2019-01-06 22:51:00 -070086 SkASSERT(fCtx->unique());
87 fRTC.reset();
88 fCCPR = nullptr;
89 fCtx.reset();
90 }
Chris Daltoncc604e52017-10-06 16:27:32 -060091
Chris Daltona2b5b642018-06-24 13:08:57 -060092 void drawPath(const SkPath& path, const SkMatrix& matrix = SkMatrix::I()) const {
Chris Daltonfddb6c02017-11-04 15:22:22 -060093 SkASSERT(this->valid());
Chris Daltoncc604e52017-10-06 16:27:32 -060094
Chris Daltoncc604e52017-10-06 16:27:32 -060095 GrPaint paint;
Brian Osmancb3d0872018-10-16 15:19:28 -040096 paint.setColor4f({ 0, 1, 0, 1 });
Chris Daltonfddb6c02017-11-04 15:22:22 -060097
Chris Daltoncc604e52017-10-06 16:27:32 -060098 GrNoClip noClip;
99 SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600100
Chris Dalton09a7bb22018-08-31 19:53:15 +0800101 GrShape shape;
102 if (!fDoStroke) {
103 shape = GrShape(path);
104 } else {
105 // Use hairlines for now, since they are the only stroke type that doesn't require a
106 // rigid-body transform. The CCPR stroke code makes no distinction between hairlines
107 // and regular strokes other than how it decides the device-space stroke width.
108 SkStrokeRec stroke(SkStrokeRec::kHairline_InitStyle);
109 stroke.setStrokeParams(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 4);
110 shape = GrShape(path, GrStyle(stroke, nullptr));
111 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600112
Chris Daltona2b5b642018-06-24 13:08:57 -0600113 fCCPR->testingOnly_drawPathDirectly({
Chris Dalton351e80c2019-01-06 22:51:00 -0700114 fCtx.get(), std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), &noClip,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600115 &clipBounds, &matrix, &shape, GrAAType::kCoverage, false});
Chris Daltoncc604e52017-10-06 16:27:32 -0600116 }
117
Brian Osmancb3d0872018-10-16 15:19:28 -0400118 void clipFullscreenRect(SkPath clipPath, SkPMColor4f color = { 0, 1, 0, 1 }) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700119 SkASSERT(this->valid());
120
121 GrPaint paint;
122 paint.setColor4f(color);
123
124 fRTC->drawRect(CCPRClip(fCCPR, clipPath), std::move(paint), GrAA::kYes, SkMatrix::I(),
125 SkRect::MakeIWH(kCanvasSize, kCanvasSize));
126 }
127
Chris Daltonfddb6c02017-11-04 15:22:22 -0600128 void flush() const {
129 SkASSERT(this->valid());
Chris Daltoncc604e52017-10-06 16:27:32 -0600130 fCtx->flush();
131 }
132
133private:
Chris Dalton351e80c2019-01-06 22:51:00 -0700134 sk_sp<GrContext> fCtx;
Chris Dalton4da70192018-06-18 09:51:36 -0600135 GrCoverageCountingPathRenderer* fCCPR;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400136 std::unique_ptr<GrRenderTargetContext> fRTC;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800137 const bool fDoStroke;
Chris Daltoncc604e52017-10-06 16:27:32 -0600138};
139
Chris Daltonfddb6c02017-11-04 15:22:22 -0600140class CCPRTest {
141public:
Chris Daltonc3318f02019-07-19 14:20:53 -0600142 void run(skiatest::Reporter* reporter, DoCoverageCount doCoverageCount, DoStroke doStroke) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600143 GrMockOptions mockOptions;
144 mockOptions.fInstanceAttribSupport = true;
Brian Osmanc6444d22019-01-09 16:30:12 -0500145 mockOptions.fHalfFloatVertexAttributeSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600146 mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400147 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
Brian Salomonbdecacf2018-02-02 20:32:49 -0500148 GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400149 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
150 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability =
Chris Daltonc3318f02019-07-19 14:20:53 -0600151 GrMockOptions::ConfigOptions::Renderability::kMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400152 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600153 mockOptions.fGeometryShaderSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600154 mockOptions.fIntegerSupport = true;
155 mockOptions.fFlatInterpolationSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600156
157 GrContextOptions ctxOptions;
Chris Daltonc3318f02019-07-19 14:20:53 -0600158 ctxOptions.fDisableCoverageCountingPaths = (DoCoverageCount::kNo == doCoverageCount);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600159 ctxOptions.fAllowPathMaskCaching = false;
160 ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting;
161
Chris Daltona2b5b642018-06-24 13:08:57 -0600162 this->customizeOptions(&mockOptions, &ctxOptions);
163
Chris Dalton351e80c2019-01-06 22:51:00 -0700164 sk_sp<GrContext> mockContext = GrContext::MakeMock(&mockOptions, ctxOptions);
165 if (!mockContext) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600166 ERRORF(reporter, "could not create mock context");
167 return;
168 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700169 if (!mockContext->unique()) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600170 ERRORF(reporter, "mock context is not unique");
171 return;
172 }
173
Chris Dalton351e80c2019-01-06 22:51:00 -0700174 CCPRPathDrawer ccpr(skstd::exchange(mockContext, nullptr), reporter, doStroke);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600175 if (!ccpr.valid()) {
176 return;
177 }
178
179 fPath.moveTo(0, 0);
180 fPath.cubicTo(50, 50, 0, 50, 50, 0);
181 this->onRun(reporter, ccpr);
Chris Daltoncc604e52017-10-06 16:27:32 -0600182 }
183
Chris Daltonfddb6c02017-11-04 15:22:22 -0600184 virtual ~CCPRTest() {}
185
186protected:
Chris Daltona2b5b642018-06-24 13:08:57 -0600187 virtual void customizeOptions(GrMockOptions*, GrContextOptions*) {}
Chris Daltonfddb6c02017-11-04 15:22:22 -0600188 virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0;
189
Chris Dalton4da70192018-06-18 09:51:36 -0600190 SkPath fPath;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600191};
192
Chris Dalton09a7bb22018-08-31 19:53:15 +0800193#define DEF_CCPR_TEST(name) \
Brian Salomondcfca432017-11-15 15:48:03 -0500194 DEF_GPUTEST(name, reporter, /* options */) { \
Chris Dalton09a7bb22018-08-31 19:53:15 +0800195 name test; \
Chris Daltonc3318f02019-07-19 14:20:53 -0600196 test.run(reporter, DoCoverageCount::kYes, DoStroke::kNo); \
197 test.run(reporter, DoCoverageCount::kYes, DoStroke::kYes); \
198 test.run(reporter, DoCoverageCount::kNo, DoStroke::kNo); \
199 /* FIXME: test.run(reporter, (DoCoverageCount::kNo, DoStroke::kYes) once supported. */ \
Chris Daltoncc604e52017-10-06 16:27:32 -0600200 }
201
Chris Dalton351e80c2019-01-06 22:51:00 -0700202class CCPR_cleanup : public CCPRTest {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600203 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
204 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
Chris Daltoncc604e52017-10-06 16:27:32 -0600205
Chris Daltonfddb6c02017-11-04 15:22:22 -0600206 // Ensure paths get unreffed.
207 for (int i = 0; i < 10; ++i) {
208 ccpr.drawPath(fPath);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600209 }
210 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
211 ccpr.flush();
212 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
213
214 // Ensure clip paths get unreffed.
215 for (int i = 0; i < 10; ++i) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700216 ccpr.clipFullscreenRect(fPath);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600217 }
218 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
219 ccpr.flush();
220 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
221
222 // Ensure paths get unreffed when we delete the context without flushing.
223 for (int i = 0; i < 10; ++i) {
224 ccpr.drawPath(fPath);
Chris Daltona32a3c32017-12-05 10:05:21 -0700225 ccpr.clipFullscreenRect(fPath);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600226 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600227 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
Chris Dalton351e80c2019-01-06 22:51:00 -0700228
229 ccpr.destroyGrContext();
Chris Daltonfddb6c02017-11-04 15:22:22 -0600230 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
231 }
232};
Chris Dalton351e80c2019-01-06 22:51:00 -0700233DEF_CCPR_TEST(CCPR_cleanup)
Chris Daltonfddb6c02017-11-04 15:22:22 -0600234
Chris Dalton351e80c2019-01-06 22:51:00 -0700235class CCPR_cleanupWithTexAllocFail : public CCPR_cleanup {
Chris Daltona2b5b642018-06-24 13:08:57 -0600236 void customizeOptions(GrMockOptions* mockOptions, GrContextOptions*) override {
237 mockOptions->fFailTextureAllocations = true;
Chris Dalton91ab1552018-04-18 13:24:25 -0600238 }
239};
Chris Dalton351e80c2019-01-06 22:51:00 -0700240DEF_CCPR_TEST(CCPR_cleanupWithTexAllocFail)
Chris Dalton91ab1552018-04-18 13:24:25 -0600241
Chris Dalton351e80c2019-01-06 22:51:00 -0700242class CCPR_unregisterCulledOps : public CCPRTest {
Chris Dalton080baa42017-11-06 14:19:19 -0700243 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
244 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
245
246 // Ensure Ops get unregistered from CCPR when culled early.
247 ccpr.drawPath(fPath);
248 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
249 ccpr.clear(); // Clear should delete the CCPR Op.
250 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
251 ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself).
252
253 // Ensure Op unregisters work when we delete the context without flushing.
254 ccpr.drawPath(fPath);
255 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
256 ccpr.clear(); // Clear should delete the CCPR DrawPathsOp.
257 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
Chris Dalton351e80c2019-01-06 22:51:00 -0700258 ccpr.destroyGrContext(); // Should not crash (DrawPathsOp should have unregistered itself).
Chris Dalton080baa42017-11-06 14:19:19 -0700259 }
260};
Chris Dalton351e80c2019-01-06 22:51:00 -0700261DEF_CCPR_TEST(CCPR_unregisterCulledOps)
Chris Dalton080baa42017-11-06 14:19:19 -0700262
Chris Dalton351e80c2019-01-06 22:51:00 -0700263class CCPR_parseEmptyPath : public CCPRTest {
Chris Daltonc9c97b72017-11-27 15:34:26 -0700264 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
265 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
266
267 // Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with
268 // an empty path.
269 SkPath largeOutsidePath;
270 largeOutsidePath.moveTo(-1e30f, -1e30f);
271 largeOutsidePath.lineTo(-1e30f, +1e30f);
272 largeOutsidePath.lineTo(-1e10f, +1e30f);
273 ccpr.drawPath(largeOutsidePath);
274
275 // Normally an empty path is culled before reaching ccpr, however we use a back door for
276 // testing so this path will make it.
277 SkPath emptyPath;
278 SkASSERT(emptyPath.isEmpty());
279 ccpr.drawPath(emptyPath);
280
281 // This is the test. It will exercise various internal asserts and verify we do not crash.
282 ccpr.flush();
Chris Daltona32a3c32017-12-05 10:05:21 -0700283
284 // Now try again with clips.
285 ccpr.clipFullscreenRect(largeOutsidePath);
286 ccpr.clipFullscreenRect(emptyPath);
287 ccpr.flush();
288
289 // ... and both.
290 ccpr.drawPath(largeOutsidePath);
291 ccpr.clipFullscreenRect(largeOutsidePath);
292 ccpr.drawPath(emptyPath);
293 ccpr.clipFullscreenRect(emptyPath);
294 ccpr.flush();
Chris Daltonc9c97b72017-11-27 15:34:26 -0700295 }
296};
Chris Dalton351e80c2019-01-06 22:51:00 -0700297DEF_CCPR_TEST(CCPR_parseEmptyPath)
Chris Daltond6fa4542019-01-04 13:23:51 -0700298
Chris Dalton351e80c2019-01-06 22:51:00 -0700299static int get_mock_texture_id(const GrTexture* texture) {
300 const GrBackendTexture& backingTexture = texture->getBackendTexture();
301 SkASSERT(GrBackendApi::kMock == backingTexture.backend());
302
303 if (!backingTexture.isValid()) {
304 return 0;
305 }
306
307 GrMockTextureInfo info;
308 backingTexture.getMockTextureInfo(&info);
Robert Phillipsa27d6252019-12-10 14:48:36 -0500309 return info.id();
Chris Dalton351e80c2019-01-06 22:51:00 -0700310}
311
312// Base class for cache path unit tests.
313class CCPRCacheTest : public CCPRTest {
314protected:
315 // Registers as an onFlush callback in order to snag the CCPR per-flush resources and note the
316 // texture IDs.
317 class RecordLastMockAtlasIDs : public GrOnFlushCallbackObject {
318 public:
319 RecordLastMockAtlasIDs(sk_sp<GrCoverageCountingPathRenderer> ccpr) : fCCPR(ccpr) {}
320
321 int lastCopyAtlasID() const { return fLastCopyAtlasID; }
322 int lastRenderedAtlasID() const { return fLastRenderedAtlasID; }
323
Chris Daltonc4b47352019-08-23 10:10:36 -0600324 void preFlush(GrOnFlushResourceProvider*, const uint32_t* opsTaskIDs,
325 int numOpsTaskIDs) override {
Chris Dalton351e80c2019-01-06 22:51:00 -0700326 fLastRenderedAtlasID = fLastCopyAtlasID = 0;
327
328 const GrCCPerFlushResources* resources = fCCPR->testingOnly_getCurrentFlushResources();
329 if (!resources) {
330 return;
331 }
332
333 if (const GrTexture* tex = resources->testingOnly_frontCopyAtlasTexture()) {
334 fLastCopyAtlasID = get_mock_texture_id(tex);
335 }
336 if (const GrTexture* tex = resources->testingOnly_frontRenderedAtlasTexture()) {
337 fLastRenderedAtlasID = get_mock_texture_id(tex);
338 }
339 }
340
341 void postFlush(GrDeferredUploadToken, const uint32_t*, int) override {}
342
343 private:
344 sk_sp<GrCoverageCountingPathRenderer> fCCPR;
345 int fLastCopyAtlasID = 0;
346 int fLastRenderedAtlasID = 0;
347 };
348
349 CCPRCacheTest() {
350 static constexpr int primes[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
351
352 SkRandom rand;
353 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
354 int numPts = rand.nextRangeU(GrShape::kMaxKeyFromDataVerbCnt + 1,
355 GrShape::kMaxKeyFromDataVerbCnt * 2);
356 int step;
357 do {
358 step = primes[rand.nextU() % SK_ARRAY_COUNT(primes)];
359 } while (step == numPts);
Mike Kleinea3f0142019-03-20 11:12:10 -0500360 fPaths[i] = ToolUtils::make_star(SkRect::MakeLTRB(0, 0, 1, 1), numPts, step);
Chris Dalton351e80c2019-01-06 22:51:00 -0700361 }
362 }
363
364 void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix& m) {
365 this->drawPathsAndFlush(ccpr, &m, 1);
366 }
367 void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix* matrices, int numMatrices) {
368 // Draw all the paths.
369 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
370 ccpr.drawPath(fPaths[i], matrices[i % numMatrices]);
371 }
372 // Re-draw a few paths, to test the case where a cache entry is hit more than once in a
373 // single flush.
374 SkRandom rand;
375 int duplicateIndices[10];
376 for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
377 duplicateIndices[i] = rand.nextULessThan(SK_ARRAY_COUNT(fPaths));
378 }
379 for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
380 for (size_t j = 0; j <= i; ++j) {
381 int idx = duplicateIndices[j];
382 ccpr.drawPath(fPaths[idx], matrices[idx % numMatrices]);
383 }
384 }
385 ccpr.flush();
386 }
387
388private:
Chris Daltond6fa4542019-01-04 13:23:51 -0700389 void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
390 ctxOptions->fAllowPathMaskCaching = true;
391 }
392
Chris Dalton351e80c2019-01-06 22:51:00 -0700393 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) final {
394 RecordLastMockAtlasIDs atlasIDRecorder(sk_ref_sp(ccpr.ccpr()));
Robert Phillips9da87e02019-02-04 13:26:26 -0500395 ccpr.ctx()->priv().addOnFlushCallbackObject(&atlasIDRecorder);
Chris Daltond6fa4542019-01-04 13:23:51 -0700396
Chris Dalton351e80c2019-01-06 22:51:00 -0700397 this->onRun(reporter, ccpr, atlasIDRecorder);
398
Robert Phillips9da87e02019-02-04 13:26:26 -0500399 ccpr.ctx()->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&atlasIDRecorder);
Chris Dalton351e80c2019-01-06 22:51:00 -0700400 }
401
402 virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
403 const RecordLastMockAtlasIDs&) = 0;
404
405protected:
406 SkPath fPaths[350];
407};
408
409// Ensures ccpr always reuses the same atlas texture in the animation use case.
410class CCPR_cache_animationAtlasReuse : public CCPRCacheTest {
411 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
412 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
413 SkMatrix m = SkMatrix::MakeTrans(kCanvasSize/2, kCanvasSize/2);
414 m.preScale(80, 80);
415 m.preTranslate(-.5,-.5);
416 this->drawPathsAndFlush(ccpr, m);
417
418 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
419 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
420 const int atlasID = atlasIDRecorder.lastRenderedAtlasID();
421
422 // Ensures we always reuse the same atlas texture in the animation use case.
423 for (int i = 0; i < 12; ++i) {
424 // 59 is prime, so we will hit every integer modulo 360 before repeating.
425 m.preRotate(59, .5, .5);
426
427 // Go twice. Paths have to get drawn twice with the same matrix before we cache their
428 // atlas. This makes sure that on the subsequent draw, after an atlas has been cached
429 // and is then invalidated since the matrix will change, that the same underlying
430 // texture object is still reused for the next atlas.
431 for (int j = 0; j < 2; ++j) {
432 this->drawPathsAndFlush(ccpr, m);
433 // Nothing should be copied to an 8-bit atlas after just two draws.
434 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
435 REPORTER_ASSERT(reporter, atlasIDRecorder.lastRenderedAtlasID() == atlasID);
436 }
Chris Dalton2e825a32019-01-04 22:14:27 +0000437 }
438
Chris Dalton351e80c2019-01-06 22:51:00 -0700439 // Do the last draw again. (On draw 3 they should get copied to an 8-bit atlas.)
440 this->drawPathsAndFlush(ccpr, m);
441 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
442 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
443
444 // Now double-check that everything continues to hit the cache as expected when the matrix
445 // doesn't change.
446 for (int i = 0; i < 10; ++i) {
447 this->drawPathsAndFlush(ccpr, m);
448 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
449 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
450 }
451 }
452};
453DEF_CCPR_TEST(CCPR_cache_animationAtlasReuse)
454
455class CCPR_cache_recycleEntries : public CCPRCacheTest {
456 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
457 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
458 SkMatrix m = SkMatrix::MakeTrans(kCanvasSize/2, kCanvasSize/2);
459 m.preScale(80, 80);
460 m.preTranslate(-.5,-.5);
461
462 auto cache = ccpr.ccpr()->testingOnly_getPathCache();
463 REPORTER_ASSERT(reporter, cache);
464
465 const auto& lru = cache->testingOnly_getLRU();
466
467 SkTArray<const void*> expectedPtrs;
468
469 // Ensures we always reuse the same atlas texture in the animation use case.
470 for (int i = 0; i < 5; ++i) {
471 // 59 is prime, so we will hit every integer modulo 360 before repeating.
472 m.preRotate(59, .5, .5);
473
474 // Go twice. Paths have to get drawn twice with the same matrix before we cache their
475 // atlas.
476 for (int j = 0; j < 2; ++j) {
477 this->drawPathsAndFlush(ccpr, m);
478 // Nothing should be copied to an 8-bit atlas after just two draws.
479 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
480 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
481 }
482
483 int idx = 0;
484 for (const GrCCPathCacheEntry* entry : lru) {
485 if (0 == i) {
486 expectedPtrs.push_back(entry);
487 } else {
488 // The same pointer should have been recycled for the new matrix.
489 REPORTER_ASSERT(reporter, entry == expectedPtrs[idx]);
490 }
491 ++idx;
492 }
493 }
494 }
495};
496DEF_CCPR_TEST(CCPR_cache_recycleEntries)
497
498// Ensures mostly-visible paths get their full mask cached.
499class CCPR_cache_mostlyVisible : public CCPRCacheTest {
500 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
501 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
502 SkMatrix matrices[3] = {
503 SkMatrix::MakeScale(kCanvasSize/2, kCanvasSize/2), // Fully visible.
504 SkMatrix::MakeScale(kCanvasSize * 1.25, kCanvasSize * 1.25), // Mostly visible.
505 SkMatrix::MakeScale(kCanvasSize * 1.5, kCanvasSize * 1.5), // Mostly NOT visible.
506 };
507
508 for (int i = 0; i < 10; ++i) {
509 this->drawPathsAndFlush(ccpr, matrices, 3);
510 if (2 == i) {
511 // The mostly-visible paths should still get cached.
512 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
513 } else {
514 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
515 }
516 // Ensure mostly NOT-visible paths never get cached.
517 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
518 }
519
520 // Clear the path cache.
521 this->drawPathsAndFlush(ccpr, SkMatrix::I());
522
523 // Now only draw the fully/mostly visible ones.
524 for (int i = 0; i < 2; ++i) {
525 this->drawPathsAndFlush(ccpr, matrices, 2);
526 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
527 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
528 }
529
530 // On draw 3 they should get copied to an 8-bit atlas.
531 this->drawPathsAndFlush(ccpr, matrices, 2);
532 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
533 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
534
535 for (int i = 0; i < 10; ++i) {
536 this->drawPathsAndFlush(ccpr, matrices, 2);
537 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
538 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
539 }
540
541 // Draw a different part of the path to ensure the full mask was cached.
542 matrices[1].postTranslate(SkScalarFloorToInt(kCanvasSize * -.25f),
543 SkScalarFloorToInt(kCanvasSize * -.25f));
544 for (int i = 0; i < 10; ++i) {
545 this->drawPathsAndFlush(ccpr, matrices, 2);
546 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
547 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
548 }
549 }
550};
551DEF_CCPR_TEST(CCPR_cache_mostlyVisible)
552
553// Ensures GrContext::performDeferredCleanup works.
554class CCPR_cache_deferredCleanup : public CCPRCacheTest {
555 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
556 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
557 SkMatrix m = SkMatrix::MakeScale(20, 20);
558 int lastRenderedAtlasID = 0;
559
560 for (int i = 0; i < 5; ++i) {
561 this->drawPathsAndFlush(ccpr, m);
562 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
563 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
564 int renderedAtlasID = atlasIDRecorder.lastRenderedAtlasID();
565 REPORTER_ASSERT(reporter, renderedAtlasID != lastRenderedAtlasID);
566 lastRenderedAtlasID = renderedAtlasID;
567
568 this->drawPathsAndFlush(ccpr, m);
569 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
570 REPORTER_ASSERT(reporter, lastRenderedAtlasID == atlasIDRecorder.lastRenderedAtlasID());
571
572 // On draw 3 they should get copied to an 8-bit atlas.
573 this->drawPathsAndFlush(ccpr, m);
574 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
575 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
576
577 for (int i = 0; i < 10; ++i) {
578 this->drawPathsAndFlush(ccpr, m);
579 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
580 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
581 }
582
583 ccpr.ctx()->performDeferredCleanup(std::chrono::milliseconds(0));
584 }
585 }
586};
587DEF_CCPR_TEST(CCPR_cache_deferredCleanup)
588
589// Verifies the cache/hash table internals.
590class CCPR_cache_hashTable : public CCPRCacheTest {
591 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
592 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
593 using CoverageType = GrCCAtlas::CoverageType;
594 SkMatrix m = SkMatrix::MakeScale(20, 20);
595
596 for (int i = 0; i < 5; ++i) {
597 this->drawPathsAndFlush(ccpr, m);
598 if (2 == i) {
599 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
600 } else {
601 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
602 }
603 if (i < 2) {
604 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
605 } else {
606 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
607 }
608
609 auto cache = ccpr.ccpr()->testingOnly_getPathCache();
610 REPORTER_ASSERT(reporter, cache);
611
612 const auto& hash = cache->testingOnly_getHashTable();
613 const auto& lru = cache->testingOnly_getLRU();
614 int count = 0;
615 for (GrCCPathCacheEntry* entry : lru) {
616 auto* node = hash.find(entry->cacheKey());
617 REPORTER_ASSERT(reporter, node);
618 REPORTER_ASSERT(reporter, node->entry() == entry);
619 REPORTER_ASSERT(reporter, 0 == entry->testingOnly_peekOnFlushRefCnt());
620 REPORTER_ASSERT(reporter, entry->unique());
621 if (0 == i) {
622 REPORTER_ASSERT(reporter, !entry->cachedAtlas());
623 } else {
624 const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas();
625 REPORTER_ASSERT(reporter, cachedAtlas);
626 if (1 == i) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600627 REPORTER_ASSERT(reporter, ccpr.ccpr()->coverageType()
Chris Dalton351e80c2019-01-06 22:51:00 -0700628 == cachedAtlas->coverageType());
629 } else {
630 REPORTER_ASSERT(reporter, CoverageType::kA8_LiteralCoverage
631 == cachedAtlas->coverageType());
632 }
633 REPORTER_ASSERT(reporter, cachedAtlas->textureKey().isValid());
634 // The actual proxy should not be held past the end of a flush.
635 REPORTER_ASSERT(reporter, !cachedAtlas->getOnFlushProxy());
636 REPORTER_ASSERT(reporter, 0 == cachedAtlas->testingOnly_peekOnFlushRefCnt());
637 }
638 ++count;
639 }
640 REPORTER_ASSERT(reporter, hash.count() == count);
641 }
642 }
643};
644DEF_CCPR_TEST(CCPR_cache_hashTable)
645
646// Ensures paths get cached even when using a sporadic flushing pattern and drawing out of order
647// (a la Chrome tiles).
648class CCPR_cache_multiFlush : public CCPRCacheTest {
649 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
650 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
651 static constexpr int kNumPaths = SK_ARRAY_COUNT(fPaths);
652 static constexpr int kBigPrimes[] = {
653 9323, 11059, 22993, 38749, 45127, 53147, 64853, 77969, 83269, 99989};
654
655 SkRandom rand;
656 SkMatrix m = SkMatrix::I();
657
658 for (size_t i = 0; i < SK_ARRAY_COUNT(kBigPrimes); ++i) {
659 int prime = kBigPrimes[i];
660 int endPathIdx = (int)rand.nextULessThan(kNumPaths);
661 int pathIdx = endPathIdx;
662 int nextFlush = rand.nextRangeU(1, 47);
663 for (int j = 0; j < kNumPaths; ++j) {
664 pathIdx = (pathIdx + prime) % kNumPaths;
665 int repeat = rand.nextRangeU(1, 3);
666 for (int k = 0; k < repeat; ++k) {
667 ccpr.drawPath(fPaths[pathIdx], m);
668 }
669 if (nextFlush == j) {
670 ccpr.flush();
671 // The paths are small enough that we should never copy to an A8 atlas.
672 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
673 if (i < 2) {
674 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
675 } else {
676 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
677 }
678 nextFlush = SkTMin(j + (int)rand.nextRangeU(1, 29), kNumPaths - 1);
679 }
680 }
681 SkASSERT(endPathIdx == pathIdx % kNumPaths);
682 }
683 }
684};
685DEF_CCPR_TEST(CCPR_cache_multiFlush)
686
Chris Daltonaaa77c12019-01-07 17:45:36 -0700687// Ensures a path drawn over mutiple tiles gets cached.
688class CCPR_cache_multiTileCache : public CCPRCacheTest {
689 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
690 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
691 // Make sure a path drawn over 9 tiles gets cached (1 tile out of 9 is >10% visibility).
692 const SkMatrix m0 = SkMatrix::MakeScale(kCanvasSize*3, kCanvasSize*3);
693 const SkPath p0 = fPaths[0];
694 for (int i = 0; i < 9; ++i) {
695 static constexpr int kRowOrder[9] = {0,1,1,0,2,2,2,1,0};
696 static constexpr int kColumnOrder[9] = {0,0,1,1,0,1,2,2,2};
697
698 SkMatrix tileM = m0;
699 tileM.postTranslate(-kCanvasSize * kColumnOrder[i], -kCanvasSize * kRowOrder[i]);
700 ccpr.drawPath(p0, tileM);
701 ccpr.flush();
702 if (i < 5) {
703 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
704 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
705 } else if (5 == i) {
706 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
707 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
708 } else {
709 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
710 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
711 }
712 }
713
714 // Now make sure paths don't get cached when visibility is <10% for every draw (12 tiles).
715 const SkMatrix m1 = SkMatrix::MakeScale(kCanvasSize*4, kCanvasSize*3);
716 const SkPath p1 = fPaths[1];
717 for (int row = 0; row < 3; ++row) {
718 for (int col = 0; col < 4; ++col) {
719 SkMatrix tileM = m1;
720 tileM.postTranslate(-kCanvasSize * col, -kCanvasSize * row);
721 ccpr.drawPath(p1, tileM);
722 ccpr.flush();
723 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
724 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
725 }
726 }
727
728 // Double-check the cache is still intact.
729 ccpr.drawPath(p0, m0);
730 ccpr.flush();
731 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
732 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
733
734 ccpr.drawPath(p1, m1);
735 ccpr.flush();
736 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
737 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
738 }
739};
740DEF_CCPR_TEST(CCPR_cache_multiTileCache)
741
Chris Dalton351e80c2019-01-06 22:51:00 -0700742// This test exercises CCPR's cache capabilities by drawing many paths with two different
743// transformation matrices. We then vary the matrices independently by whole and partial pixels,
744// and verify the caching behaved as expected.
745class CCPR_cache_partialInvalidate : public CCPRCacheTest {
746 void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
747 ctxOptions->fAllowPathMaskCaching = true;
748 }
749
750 static constexpr int kPathSize = 4;
751
752 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
753 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
Chris Dalton4da70192018-06-18 09:51:36 -0600754 SkMatrix matrices[2] = {
755 SkMatrix::MakeTrans(5, 5),
756 SkMatrix::MakeTrans(kCanvasSize - kPathSize - 5, kCanvasSize - kPathSize - 5)
757 };
Chris Dalton351e80c2019-01-06 22:51:00 -0700758 matrices[0].preScale(kPathSize, kPathSize);
759 matrices[1].preScale(kPathSize, kPathSize);
Chris Dalton4da70192018-06-18 09:51:36 -0600760
Chris Dalton351e80c2019-01-06 22:51:00 -0700761 int firstAtlasID = 0;
Chris Dalton4da70192018-06-18 09:51:36 -0600762
Chris Dalton351e80c2019-01-06 22:51:00 -0700763 for (int iterIdx = 0; iterIdx < 4*3*2; ++iterIdx) {
764 this->drawPathsAndFlush(ccpr, matrices, 2);
Chris Dalton4da70192018-06-18 09:51:36 -0600765
Chris Daltona8429cf2018-06-22 11:43:31 -0600766 if (0 == iterIdx) {
767 // First iteration: just note the ID of the stashed atlas and continue.
Chris Dalton351e80c2019-01-06 22:51:00 -0700768 firstAtlasID = atlasIDRecorder.lastRenderedAtlasID();
769 REPORTER_ASSERT(reporter, 0 != firstAtlasID);
Chris Dalton4da70192018-06-18 09:51:36 -0600770 continue;
771 }
772
Chris Dalton351e80c2019-01-06 22:51:00 -0700773 int testIdx = (iterIdx/2) % 3;
774 int repetitionIdx = iterIdx % 2;
775 switch (testIdx) {
776 case 0:
777 if (0 == repetitionIdx) {
778 // This is the big test. New paths were drawn twice last round. On hit 2
779 // (last time), 'firstAtlasID' was cached as a 16-bit atlas. Now, on hit 3,
780 // these paths should be copied out of 'firstAtlasID', and into an A8 atlas.
781 // THEN: we should recycle 'firstAtlasID' and reuse that same texture to
782 // render the new masks.
783 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
784 REPORTER_ASSERT(reporter,
785 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
786 } else {
787 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
788 // This is hit 2 for the new masks. Next time they will be copied to an A8
789 // atlas.
790 REPORTER_ASSERT(reporter,
791 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
792 }
Chris Daltond6fa4542019-01-04 13:23:51 -0700793
Chris Dalton351e80c2019-01-06 22:51:00 -0700794 if (1 == repetitionIdx) {
795 // Integer translates: all path masks stay valid.
796 matrices[0].preTranslate(-1, -1);
797 matrices[1].preTranslate(1, 1);
798 }
799 break;
800
801 case 1:
802 if (0 == repetitionIdx) {
803 // New paths were drawn twice last round. The third hit (now) they should be
804 // copied to an A8 atlas.
805 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
806 } else {
807 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
808 }
809
810 // This draw should have gotten 100% cache hits; we only did integer translates
811 // last time (or none if it was the first flush). Therefore, everything should
812 // have been cached.
813 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
814
815 if (1 == repetitionIdx) {
816 // Invalidate even path masks.
817 matrices[0].preTranslate(1.6f, 1.4f);
818 }
Chris Dalton4da70192018-06-18 09:51:36 -0600819 break;
820
821 case 2:
Chris Dalton351e80c2019-01-06 22:51:00 -0700822 // No new masks to copy from last time; it had 100% cache hits.
823 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
Chris Dalton4da70192018-06-18 09:51:36 -0600824
Chris Dalton351e80c2019-01-06 22:51:00 -0700825 // Even path masks were invalidated last iteration by a subpixel translate.
826 // They should have been re-rendered this time in the original 'firstAtlasID'
827 // texture.
828 REPORTER_ASSERT(reporter,
829 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
Chris Dalton4da70192018-06-18 09:51:36 -0600830
Chris Dalton351e80c2019-01-06 22:51:00 -0700831 if (1 == repetitionIdx) {
832 // Invalidate odd path masks.
833 matrices[1].preTranslate(-1.4f, -1.6f);
834 }
Chris Dalton4da70192018-06-18 09:51:36 -0600835 break;
836 }
837 }
838 }
839};
Chris Dalton351e80c2019-01-06 22:51:00 -0700840DEF_CCPR_TEST(CCPR_cache_partialInvalidate)
Chris Dalton4da70192018-06-18 09:51:36 -0600841
Greg Danielf41b2bd2019-08-22 16:19:24 -0400842class CCPR_unrefPerOpsTaskPathsBeforeOps : public CCPRTest {
Chris Daltondedf8f22018-09-24 20:23:47 -0600843 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
844 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
845 for (int i = 0; i < 10000; ++i) {
846 // Draw enough paths to make the arena allocator hit the heap.
847 ccpr.drawPath(fPath);
848 }
849
Greg Danielf41b2bd2019-08-22 16:19:24 -0400850 // Unref the GrCCPerOpsTaskPaths object.
851 auto perOpsTaskPathsMap = ccpr.ccpr()->detachPendingPaths();
852 perOpsTaskPathsMap.clear();
Chris Daltondedf8f22018-09-24 20:23:47 -0600853
854 // Now delete the Op and all its draws.
855 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
856 ccpr.flush();
857 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
858 }
859};
Greg Danielf41b2bd2019-08-22 16:19:24 -0400860DEF_CCPR_TEST(CCPR_unrefPerOpsTaskPathsBeforeOps)
Chris Daltondedf8f22018-09-24 20:23:47 -0600861
Chris Daltonfddb6c02017-11-04 15:22:22 -0600862class CCPRRenderingTest {
863public:
Chris Daltonc3318f02019-07-19 14:20:53 -0600864 void run(skiatest::Reporter* reporter, GrContext* ctx, DoStroke doStroke) const {
865 if (auto ccpr = ctx->priv().drawingManager()->getCoverageCountingPathRenderer()) {
866 if (DoStroke::kYes == doStroke &&
867 GrCCAtlas::CoverageType::kA8_Multisample == ccpr->coverageType()) {
868 return; // Stroking is not yet supported for multisample.
869 }
870 CCPRPathDrawer drawer(sk_ref_sp(ctx), reporter, doStroke);
871 if (!drawer.valid()) {
872 return;
873 }
874 this->onRun(reporter, drawer);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600875 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600876 }
877
878 virtual ~CCPRRenderingTest() {}
879
880protected:
881 virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0;
882};
883
884#define DEF_CCPR_RENDERING_TEST(name) \
885 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \
886 name test; \
Chris Daltonc3318f02019-07-19 14:20:53 -0600887 test.run(reporter, ctxInfo.grContext(), DoStroke::kNo); \
888 test.run(reporter, ctxInfo.grContext(), DoStroke::kYes); \
Chris Daltonfddb6c02017-11-04 15:22:22 -0600889 }
890
Chris Dalton351e80c2019-01-06 22:51:00 -0700891class CCPR_busyPath : public CCPRRenderingTest {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600892 void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
893 static constexpr int kNumBusyVerbs = 1 << 17;
894 ccpr.clear();
895 SkPath busyPath;
896 busyPath.moveTo(0, 0); // top left
897 busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right
898 for (int i = 2; i < kNumBusyVerbs; ++i) {
899 float offset = i * ((float)kCanvasSize / kNumBusyVerbs);
900 busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen
901 }
902 ccpr.drawPath(busyPath);
903
904 ccpr.flush(); // If this doesn't crash, the test passed.
905 // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in
906 // your platform's GrGLCaps.
907 }
908};
Chris Dalton351e80c2019-01-06 22:51:00 -0700909DEF_CCPR_RENDERING_TEST(CCPR_busyPath)