blob: 098c81abda6f654e99e900e3cc64f2a30d1f8374 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/mock/GrMockTypes.h"
14#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkPathPriv.h"
16#include "src/gpu/GrClip.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrDrawingManager.h"
19#include "src/gpu/GrPaint.h"
20#include "src/gpu/GrPathRenderer.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContext.h"
23#include "src/gpu/GrRenderTargetContextPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000024#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/ccpr/GrCCPathCache.h"
26#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000027#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "tools/ToolUtils.h"
Hal Canary8a001442018-09-19 11:31:27 -040029
Chris Daltoncc604e52017-10-06 16:27:32 -060030#include <cmath>
31
32static constexpr int kCanvasSize = 100;
33
Chris Daltonc3318f02019-07-19 14:20:53 -060034enum class DoCoverageCount { kNo = false, kYes };
35enum class DoStroke { kNo = false, kYes };
36
Chris Daltona32a3c32017-12-05 10:05:21 -070037class CCPRClip : public GrClip {
38public:
39 CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {}
40
41private:
Robert Phillips6f0e02f2019-02-13 11:02:28 -050042 bool apply(GrRecordingContext* context, GrRenderTargetContext* rtc, bool useHWAA,
43 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
Greg Danielf41b2bd2019-08-22 16:19:24 -040044 out->addCoverageFP(fCCPR->makeClipProcessor(rtc->priv().testingOnly_getOpsTaskID(), fPath,
Chris Daltona32a3c32017-12-05 10:05:21 -070045 SkIRect::MakeWH(rtc->width(), rtc->height()),
Robert Phillips9da87e02019-02-04 13:26:26 -050046 *context->priv().caps()));
Chris Daltona32a3c32017-12-05 10:05:21 -070047 return true;
48 }
49 bool quickContains(const SkRect&) const final { return false; }
50 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
Michael Ludwigc002d562020-05-13 14:17:57 -040051
Chris Daltona32a3c32017-12-05 10:05:21 -070052 GrCoverageCountingPathRenderer* const fCCPR;
53 const SkPath fPath;
54};
55
Chris Daltoncc604e52017-10-06 16:27:32 -060056class CCPRPathDrawer {
57public:
Chris Daltonc3318f02019-07-19 14:20:53 -060058 CCPRPathDrawer(sk_sp<GrContext> ctx, skiatest::Reporter* reporter, DoStroke doStroke)
Chris Daltoncc604e52017-10-06 16:27:32 -060059 : fCtx(ctx)
Robert Phillips9da87e02019-02-04 13:26:26 -050060 , fCCPR(fCtx->priv().drawingManager()->getCoverageCountingPathRenderer())
Greg Daniele20fcad2020-01-08 11:52:34 -050061 , fRTC(GrRenderTargetContext::Make(
62 fCtx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
63 {kCanvasSize, kCanvasSize}))
Chris Daltonc3318f02019-07-19 14:20:53 -060064 , fDoStroke(DoStroke::kYes == doStroke) {
Chris Daltonfddb6c02017-11-04 15:22:22 -060065 if (!fCCPR) {
66 ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests");
67 }
68 if (!fRTC) {
69 ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests");
Chris Daltoncc604e52017-10-06 16:27:32 -060070 }
71 }
72
Chris Dalton351e80c2019-01-06 22:51:00 -070073 GrContext* ctx() const { return fCtx.get(); }
Chris Dalton4da70192018-06-18 09:51:36 -060074 GrCoverageCountingPathRenderer* ccpr() const { return fCCPR; }
75
Chris Daltonfddb6c02017-11-04 15:22:22 -060076 bool valid() const { return fCCPR && fRTC; }
Michael Ludwig81d41722020-05-26 16:57:38 -040077 void clear() const { fRTC->clear(SK_PMColor4fTRANSPARENT); }
Chris Dalton351e80c2019-01-06 22:51:00 -070078 void destroyGrContext() {
Chris Dalton351e80c2019-01-06 22:51:00 -070079 SkASSERT(fCtx->unique());
80 fRTC.reset();
81 fCCPR = nullptr;
82 fCtx.reset();
83 }
Chris Daltoncc604e52017-10-06 16:27:32 -060084
Chris Daltona2b5b642018-06-24 13:08:57 -060085 void drawPath(const SkPath& path, const SkMatrix& matrix = SkMatrix::I()) const {
Chris Daltonfddb6c02017-11-04 15:22:22 -060086 SkASSERT(this->valid());
Chris Daltoncc604e52017-10-06 16:27:32 -060087
Chris Daltoncc604e52017-10-06 16:27:32 -060088 GrPaint paint;
Brian Osmancb3d0872018-10-16 15:19:28 -040089 paint.setColor4f({ 0, 1, 0, 1 });
Chris Daltonfddb6c02017-11-04 15:22:22 -060090
Chris Daltoncc604e52017-10-06 16:27:32 -060091 GrNoClip noClip;
92 SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize);
Chris Daltonfddb6c02017-11-04 15:22:22 -060093
Michael Ludwig2686d692020-04-17 20:21:37 +000094 GrStyledShape shape;
Chris Dalton09a7bb22018-08-31 19:53:15 +080095 if (!fDoStroke) {
Michael Ludwig2686d692020-04-17 20:21:37 +000096 shape = GrStyledShape(path);
Chris Dalton09a7bb22018-08-31 19:53:15 +080097 } else {
98 // Use hairlines for now, since they are the only stroke type that doesn't require a
99 // rigid-body transform. The CCPR stroke code makes no distinction between hairlines
100 // and regular strokes other than how it decides the device-space stroke width.
101 SkStrokeRec stroke(SkStrokeRec::kHairline_InitStyle);
102 stroke.setStrokeParams(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 4);
Michael Ludwig2686d692020-04-17 20:21:37 +0000103 shape = GrStyledShape(path, GrStyle(stroke, nullptr));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800104 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600105
Chris Daltona2b5b642018-06-24 13:08:57 -0600106 fCCPR->testingOnly_drawPathDirectly({
Chris Dalton351e80c2019-01-06 22:51:00 -0700107 fCtx.get(), std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), &noClip,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600108 &clipBounds, &matrix, &shape, GrAAType::kCoverage, false});
Chris Daltoncc604e52017-10-06 16:27:32 -0600109 }
110
Brian Osmancb3d0872018-10-16 15:19:28 -0400111 void clipFullscreenRect(SkPath clipPath, SkPMColor4f color = { 0, 1, 0, 1 }) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700112 SkASSERT(this->valid());
113
114 GrPaint paint;
115 paint.setColor4f(color);
116
117 fRTC->drawRect(CCPRClip(fCCPR, clipPath), std::move(paint), GrAA::kYes, SkMatrix::I(),
118 SkRect::MakeIWH(kCanvasSize, kCanvasSize));
119 }
120
Chris Daltonfddb6c02017-11-04 15:22:22 -0600121 void flush() const {
122 SkASSERT(this->valid());
Greg Daniel0a2464f2020-05-14 15:45:44 -0400123 fCtx->flushAndSubmit();
Chris Daltoncc604e52017-10-06 16:27:32 -0600124 }
125
126private:
Chris Dalton351e80c2019-01-06 22:51:00 -0700127 sk_sp<GrContext> fCtx;
Chris Dalton4da70192018-06-18 09:51:36 -0600128 GrCoverageCountingPathRenderer* fCCPR;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400129 std::unique_ptr<GrRenderTargetContext> fRTC;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800130 const bool fDoStroke;
Chris Daltoncc604e52017-10-06 16:27:32 -0600131};
132
Chris Daltonfddb6c02017-11-04 15:22:22 -0600133class CCPRTest {
134public:
Chris Daltonc3318f02019-07-19 14:20:53 -0600135 void run(skiatest::Reporter* reporter, DoCoverageCount doCoverageCount, DoStroke doStroke) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600136 GrMockOptions mockOptions;
Chris Daltona77cdee2020-04-03 14:50:43 -0600137 mockOptions.fDrawInstancedSupport = true;
Brian Osmanc6444d22019-01-09 16:30:12 -0500138 mockOptions.fHalfFloatVertexAttributeSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600139 mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400140 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400142 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
143 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability =
Chris Daltonc3318f02019-07-19 14:20:53 -0600144 GrMockOptions::ConfigOptions::Renderability::kMSAA;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400145 mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600146 mockOptions.fGeometryShaderSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600147 mockOptions.fIntegerSupport = true;
148 mockOptions.fFlatInterpolationSupport = true;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600149
150 GrContextOptions ctxOptions;
Chris Daltonc3318f02019-07-19 14:20:53 -0600151 ctxOptions.fDisableCoverageCountingPaths = (DoCoverageCount::kNo == doCoverageCount);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600152 ctxOptions.fAllowPathMaskCaching = false;
153 ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting;
154
Chris Daltona2b5b642018-06-24 13:08:57 -0600155 this->customizeOptions(&mockOptions, &ctxOptions);
156
Chris Dalton351e80c2019-01-06 22:51:00 -0700157 sk_sp<GrContext> mockContext = GrContext::MakeMock(&mockOptions, ctxOptions);
158 if (!mockContext) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600159 ERRORF(reporter, "could not create mock context");
160 return;
161 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700162 if (!mockContext->unique()) {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600163 ERRORF(reporter, "mock context is not unique");
164 return;
165 }
166
Adlai Holler5ba50af2020-04-29 21:11:14 -0400167 CCPRPathDrawer ccpr(std::exchange(mockContext, nullptr), reporter, doStroke);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600168 if (!ccpr.valid()) {
169 return;
170 }
171
172 fPath.moveTo(0, 0);
173 fPath.cubicTo(50, 50, 0, 50, 50, 0);
174 this->onRun(reporter, ccpr);
Chris Daltoncc604e52017-10-06 16:27:32 -0600175 }
176
Chris Daltonfddb6c02017-11-04 15:22:22 -0600177 virtual ~CCPRTest() {}
178
179protected:
Chris Daltona2b5b642018-06-24 13:08:57 -0600180 virtual void customizeOptions(GrMockOptions*, GrContextOptions*) {}
Chris Daltonfddb6c02017-11-04 15:22:22 -0600181 virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0;
182
Chris Dalton4da70192018-06-18 09:51:36 -0600183 SkPath fPath;
Chris Daltonfddb6c02017-11-04 15:22:22 -0600184};
185
Chris Dalton09a7bb22018-08-31 19:53:15 +0800186#define DEF_CCPR_TEST(name) \
Brian Salomondcfca432017-11-15 15:48:03 -0500187 DEF_GPUTEST(name, reporter, /* options */) { \
Chris Dalton09a7bb22018-08-31 19:53:15 +0800188 name test; \
Chris Daltonc3318f02019-07-19 14:20:53 -0600189 test.run(reporter, DoCoverageCount::kYes, DoStroke::kNo); \
190 test.run(reporter, DoCoverageCount::kYes, DoStroke::kYes); \
191 test.run(reporter, DoCoverageCount::kNo, DoStroke::kNo); \
192 /* FIXME: test.run(reporter, (DoCoverageCount::kNo, DoStroke::kYes) once supported. */ \
Chris Daltoncc604e52017-10-06 16:27:32 -0600193 }
194
Chris Dalton351e80c2019-01-06 22:51:00 -0700195class CCPR_cleanup : public CCPRTest {
Chris Dalton5a5fe792020-02-15 11:41:30 -0700196protected:
Chris Daltonfddb6c02017-11-04 15:22:22 -0600197 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
198 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
Chris Daltoncc604e52017-10-06 16:27:32 -0600199
Chris Daltonfddb6c02017-11-04 15:22:22 -0600200 // Ensure paths get unreffed.
201 for (int i = 0; i < 10; ++i) {
202 ccpr.drawPath(fPath);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600203 }
204 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
205 ccpr.flush();
206 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
207
208 // Ensure clip paths get unreffed.
209 for (int i = 0; i < 10; ++i) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700210 ccpr.clipFullscreenRect(fPath);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600211 }
212 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
213 ccpr.flush();
214 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
215
216 // Ensure paths get unreffed when we delete the context without flushing.
217 for (int i = 0; i < 10; ++i) {
218 ccpr.drawPath(fPath);
Chris Daltona32a3c32017-12-05 10:05:21 -0700219 ccpr.clipFullscreenRect(fPath);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600220 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600221 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
Chris Dalton351e80c2019-01-06 22:51:00 -0700222
223 ccpr.destroyGrContext();
Chris Daltonfddb6c02017-11-04 15:22:22 -0600224 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
225 }
226};
Chris Dalton351e80c2019-01-06 22:51:00 -0700227DEF_CCPR_TEST(CCPR_cleanup)
Chris Daltonfddb6c02017-11-04 15:22:22 -0600228
Chris Dalton351e80c2019-01-06 22:51:00 -0700229class CCPR_cleanupWithTexAllocFail : public CCPR_cleanup {
Chris Daltona2b5b642018-06-24 13:08:57 -0600230 void customizeOptions(GrMockOptions* mockOptions, GrContextOptions*) override {
231 mockOptions->fFailTextureAllocations = true;
Chris Dalton91ab1552018-04-18 13:24:25 -0600232 }
Chris Dalton5a5fe792020-02-15 11:41:30 -0700233 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
234 ((GrRecordingContext*)ccpr.ctx())->priv().incrSuppressWarningMessages();
235 this->CCPR_cleanup::onRun(reporter, ccpr);
236 }
Chris Dalton91ab1552018-04-18 13:24:25 -0600237};
Chris Dalton351e80c2019-01-06 22:51:00 -0700238DEF_CCPR_TEST(CCPR_cleanupWithTexAllocFail)
Chris Dalton91ab1552018-04-18 13:24:25 -0600239
Chris Dalton351e80c2019-01-06 22:51:00 -0700240class CCPR_unregisterCulledOps : public CCPRTest {
Chris Dalton080baa42017-11-06 14:19:19 -0700241 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
242 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
243
244 // Ensure Ops get unregistered from CCPR when culled early.
245 ccpr.drawPath(fPath);
246 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
247 ccpr.clear(); // Clear should delete the CCPR Op.
248 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
249 ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself).
250
251 // Ensure Op unregisters work when we delete the context without flushing.
252 ccpr.drawPath(fPath);
253 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
254 ccpr.clear(); // Clear should delete the CCPR DrawPathsOp.
255 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
Chris Dalton351e80c2019-01-06 22:51:00 -0700256 ccpr.destroyGrContext(); // Should not crash (DrawPathsOp should have unregistered itself).
Chris Dalton080baa42017-11-06 14:19:19 -0700257 }
258};
Chris Dalton351e80c2019-01-06 22:51:00 -0700259DEF_CCPR_TEST(CCPR_unregisterCulledOps)
Chris Dalton080baa42017-11-06 14:19:19 -0700260
Chris Dalton351e80c2019-01-06 22:51:00 -0700261class CCPR_parseEmptyPath : public CCPRTest {
Chris Daltonc9c97b72017-11-27 15:34:26 -0700262 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
263 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
264
265 // Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with
266 // an empty path.
267 SkPath largeOutsidePath;
268 largeOutsidePath.moveTo(-1e30f, -1e30f);
269 largeOutsidePath.lineTo(-1e30f, +1e30f);
270 largeOutsidePath.lineTo(-1e10f, +1e30f);
271 ccpr.drawPath(largeOutsidePath);
272
273 // Normally an empty path is culled before reaching ccpr, however we use a back door for
274 // testing so this path will make it.
275 SkPath emptyPath;
276 SkASSERT(emptyPath.isEmpty());
277 ccpr.drawPath(emptyPath);
278
279 // This is the test. It will exercise various internal asserts and verify we do not crash.
280 ccpr.flush();
Chris Daltona32a3c32017-12-05 10:05:21 -0700281
282 // Now try again with clips.
283 ccpr.clipFullscreenRect(largeOutsidePath);
284 ccpr.clipFullscreenRect(emptyPath);
285 ccpr.flush();
286
287 // ... and both.
288 ccpr.drawPath(largeOutsidePath);
289 ccpr.clipFullscreenRect(largeOutsidePath);
290 ccpr.drawPath(emptyPath);
291 ccpr.clipFullscreenRect(emptyPath);
292 ccpr.flush();
Chris Daltonc9c97b72017-11-27 15:34:26 -0700293 }
294};
Chris Dalton351e80c2019-01-06 22:51:00 -0700295DEF_CCPR_TEST(CCPR_parseEmptyPath)
Chris Daltond6fa4542019-01-04 13:23:51 -0700296
Chris Dalton351e80c2019-01-06 22:51:00 -0700297static int get_mock_texture_id(const GrTexture* texture) {
298 const GrBackendTexture& backingTexture = texture->getBackendTexture();
299 SkASSERT(GrBackendApi::kMock == backingTexture.backend());
300
301 if (!backingTexture.isValid()) {
302 return 0;
303 }
304
305 GrMockTextureInfo info;
306 backingTexture.getMockTextureInfo(&info);
Robert Phillipsa27d6252019-12-10 14:48:36 -0500307 return info.id();
Chris Dalton351e80c2019-01-06 22:51:00 -0700308}
309
310// Base class for cache path unit tests.
311class CCPRCacheTest : public CCPRTest {
312protected:
313 // Registers as an onFlush callback in order to snag the CCPR per-flush resources and note the
314 // texture IDs.
315 class RecordLastMockAtlasIDs : public GrOnFlushCallbackObject {
316 public:
317 RecordLastMockAtlasIDs(sk_sp<GrCoverageCountingPathRenderer> ccpr) : fCCPR(ccpr) {}
318
319 int lastCopyAtlasID() const { return fLastCopyAtlasID; }
320 int lastRenderedAtlasID() const { return fLastRenderedAtlasID; }
321
Chris Daltonc4b47352019-08-23 10:10:36 -0600322 void preFlush(GrOnFlushResourceProvider*, const uint32_t* opsTaskIDs,
323 int numOpsTaskIDs) override {
Chris Dalton351e80c2019-01-06 22:51:00 -0700324 fLastRenderedAtlasID = fLastCopyAtlasID = 0;
325
326 const GrCCPerFlushResources* resources = fCCPR->testingOnly_getCurrentFlushResources();
327 if (!resources) {
328 return;
329 }
330
331 if (const GrTexture* tex = resources->testingOnly_frontCopyAtlasTexture()) {
332 fLastCopyAtlasID = get_mock_texture_id(tex);
333 }
334 if (const GrTexture* tex = resources->testingOnly_frontRenderedAtlasTexture()) {
335 fLastRenderedAtlasID = get_mock_texture_id(tex);
336 }
337 }
338
339 void postFlush(GrDeferredUploadToken, const uint32_t*, int) override {}
340
341 private:
342 sk_sp<GrCoverageCountingPathRenderer> fCCPR;
343 int fLastCopyAtlasID = 0;
344 int fLastRenderedAtlasID = 0;
345 };
346
347 CCPRCacheTest() {
348 static constexpr int primes[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
349
350 SkRandom rand;
351 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
Michael Ludwig2686d692020-04-17 20:21:37 +0000352 int numPts = rand.nextRangeU(GrStyledShape::kMaxKeyFromDataVerbCnt + 1,
353 GrStyledShape::kMaxKeyFromDataVerbCnt * 2);
Chris Dalton351e80c2019-01-06 22:51:00 -0700354 int step;
355 do {
356 step = primes[rand.nextU() % SK_ARRAY_COUNT(primes)];
357 } while (step == numPts);
Mike Kleinea3f0142019-03-20 11:12:10 -0500358 fPaths[i] = ToolUtils::make_star(SkRect::MakeLTRB(0, 0, 1, 1), numPts, step);
Chris Dalton351e80c2019-01-06 22:51:00 -0700359 }
360 }
361
362 void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix& m) {
363 this->drawPathsAndFlush(ccpr, &m, 1);
364 }
365 void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix* matrices, int numMatrices) {
366 // Draw all the paths.
367 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
368 ccpr.drawPath(fPaths[i], matrices[i % numMatrices]);
369 }
370 // Re-draw a few paths, to test the case where a cache entry is hit more than once in a
371 // single flush.
372 SkRandom rand;
373 int duplicateIndices[10];
374 for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
375 duplicateIndices[i] = rand.nextULessThan(SK_ARRAY_COUNT(fPaths));
376 }
377 for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
378 for (size_t j = 0; j <= i; ++j) {
379 int idx = duplicateIndices[j];
380 ccpr.drawPath(fPaths[idx], matrices[idx % numMatrices]);
381 }
382 }
383 ccpr.flush();
384 }
385
386private:
Chris Daltond6fa4542019-01-04 13:23:51 -0700387 void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
388 ctxOptions->fAllowPathMaskCaching = true;
389 }
390
Chris Dalton351e80c2019-01-06 22:51:00 -0700391 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) final {
392 RecordLastMockAtlasIDs atlasIDRecorder(sk_ref_sp(ccpr.ccpr()));
Robert Phillips9da87e02019-02-04 13:26:26 -0500393 ccpr.ctx()->priv().addOnFlushCallbackObject(&atlasIDRecorder);
Chris Daltond6fa4542019-01-04 13:23:51 -0700394
Chris Dalton351e80c2019-01-06 22:51:00 -0700395 this->onRun(reporter, ccpr, atlasIDRecorder);
396
Robert Phillips9da87e02019-02-04 13:26:26 -0500397 ccpr.ctx()->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&atlasIDRecorder);
Chris Dalton351e80c2019-01-06 22:51:00 -0700398 }
399
400 virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
401 const RecordLastMockAtlasIDs&) = 0;
402
403protected:
404 SkPath fPaths[350];
405};
406
407// Ensures ccpr always reuses the same atlas texture in the animation use case.
408class CCPR_cache_animationAtlasReuse : public CCPRCacheTest {
409 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
410 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
Mike Reed1f607332020-05-21 12:11:27 -0400411 SkMatrix m = SkMatrix::Translate(kCanvasSize/2, kCanvasSize/2);
Chris Dalton351e80c2019-01-06 22:51:00 -0700412 m.preScale(80, 80);
413 m.preTranslate(-.5,-.5);
414 this->drawPathsAndFlush(ccpr, m);
415
416 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
417 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
418 const int atlasID = atlasIDRecorder.lastRenderedAtlasID();
419
420 // Ensures we always reuse the same atlas texture in the animation use case.
421 for (int i = 0; i < 12; ++i) {
422 // 59 is prime, so we will hit every integer modulo 360 before repeating.
423 m.preRotate(59, .5, .5);
424
425 // Go twice. Paths have to get drawn twice with the same matrix before we cache their
426 // atlas. This makes sure that on the subsequent draw, after an atlas has been cached
427 // and is then invalidated since the matrix will change, that the same underlying
428 // texture object is still reused for the next atlas.
429 for (int j = 0; j < 2; ++j) {
430 this->drawPathsAndFlush(ccpr, m);
431 // Nothing should be copied to an 8-bit atlas after just two draws.
432 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
433 REPORTER_ASSERT(reporter, atlasIDRecorder.lastRenderedAtlasID() == atlasID);
434 }
Chris Dalton2e825a32019-01-04 22:14:27 +0000435 }
436
Chris Dalton351e80c2019-01-06 22:51:00 -0700437 // Do the last draw again. (On draw 3 they should get copied to an 8-bit atlas.)
438 this->drawPathsAndFlush(ccpr, m);
439 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
440 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
441
442 // Now double-check that everything continues to hit the cache as expected when the matrix
443 // doesn't change.
444 for (int i = 0; i < 10; ++i) {
445 this->drawPathsAndFlush(ccpr, m);
446 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
447 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
448 }
449 }
450};
451DEF_CCPR_TEST(CCPR_cache_animationAtlasReuse)
452
453class CCPR_cache_recycleEntries : public CCPRCacheTest {
454 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
455 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
Mike Reed1f607332020-05-21 12:11:27 -0400456 SkMatrix m = SkMatrix::Translate(kCanvasSize/2, kCanvasSize/2);
Chris Dalton351e80c2019-01-06 22:51:00 -0700457 m.preScale(80, 80);
458 m.preTranslate(-.5,-.5);
459
460 auto cache = ccpr.ccpr()->testingOnly_getPathCache();
461 REPORTER_ASSERT(reporter, cache);
462
463 const auto& lru = cache->testingOnly_getLRU();
464
465 SkTArray<const void*> expectedPtrs;
466
467 // Ensures we always reuse the same atlas texture in the animation use case.
468 for (int i = 0; i < 5; ++i) {
469 // 59 is prime, so we will hit every integer modulo 360 before repeating.
470 m.preRotate(59, .5, .5);
471
472 // Go twice. Paths have to get drawn twice with the same matrix before we cache their
473 // atlas.
474 for (int j = 0; j < 2; ++j) {
475 this->drawPathsAndFlush(ccpr, m);
476 // Nothing should be copied to an 8-bit atlas after just two draws.
477 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
478 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
479 }
480
481 int idx = 0;
482 for (const GrCCPathCacheEntry* entry : lru) {
483 if (0 == i) {
484 expectedPtrs.push_back(entry);
485 } else {
486 // The same pointer should have been recycled for the new matrix.
487 REPORTER_ASSERT(reporter, entry == expectedPtrs[idx]);
488 }
489 ++idx;
490 }
491 }
492 }
493};
494DEF_CCPR_TEST(CCPR_cache_recycleEntries)
495
496// Ensures mostly-visible paths get their full mask cached.
497class CCPR_cache_mostlyVisible : public CCPRCacheTest {
498 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
499 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
500 SkMatrix matrices[3] = {
Mike Reed1f607332020-05-21 12:11:27 -0400501 SkMatrix::Scale(kCanvasSize/2, kCanvasSize/2), // Fully visible.
502 SkMatrix::Scale(kCanvasSize * 1.25, kCanvasSize * 1.25), // Mostly visible.
503 SkMatrix::Scale(kCanvasSize * 1.5, kCanvasSize * 1.5), // Mostly NOT visible.
Chris Dalton351e80c2019-01-06 22:51:00 -0700504 };
505
506 for (int i = 0; i < 10; ++i) {
507 this->drawPathsAndFlush(ccpr, matrices, 3);
508 if (2 == i) {
509 // The mostly-visible paths should still get cached.
510 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
511 } else {
512 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
513 }
514 // Ensure mostly NOT-visible paths never get cached.
515 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
516 }
517
518 // Clear the path cache.
519 this->drawPathsAndFlush(ccpr, SkMatrix::I());
520
521 // Now only draw the fully/mostly visible ones.
522 for (int i = 0; i < 2; ++i) {
523 this->drawPathsAndFlush(ccpr, matrices, 2);
524 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
525 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
526 }
527
528 // On draw 3 they should get copied to an 8-bit atlas.
529 this->drawPathsAndFlush(ccpr, matrices, 2);
530 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
531 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
532
533 for (int i = 0; i < 10; ++i) {
534 this->drawPathsAndFlush(ccpr, matrices, 2);
535 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
536 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
537 }
538
539 // Draw a different part of the path to ensure the full mask was cached.
540 matrices[1].postTranslate(SkScalarFloorToInt(kCanvasSize * -.25f),
541 SkScalarFloorToInt(kCanvasSize * -.25f));
542 for (int i = 0; i < 10; ++i) {
543 this->drawPathsAndFlush(ccpr, matrices, 2);
544 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
545 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
546 }
547 }
548};
549DEF_CCPR_TEST(CCPR_cache_mostlyVisible)
550
551// Ensures GrContext::performDeferredCleanup works.
552class CCPR_cache_deferredCleanup : public CCPRCacheTest {
553 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
554 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
Mike Reed1f607332020-05-21 12:11:27 -0400555 SkMatrix m = SkMatrix::Scale(20, 20);
Chris Dalton351e80c2019-01-06 22:51:00 -0700556 int lastRenderedAtlasID = 0;
557
558 for (int i = 0; i < 5; ++i) {
559 this->drawPathsAndFlush(ccpr, m);
560 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
561 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
562 int renderedAtlasID = atlasIDRecorder.lastRenderedAtlasID();
563 REPORTER_ASSERT(reporter, renderedAtlasID != lastRenderedAtlasID);
564 lastRenderedAtlasID = renderedAtlasID;
565
566 this->drawPathsAndFlush(ccpr, m);
567 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
568 REPORTER_ASSERT(reporter, lastRenderedAtlasID == atlasIDRecorder.lastRenderedAtlasID());
569
570 // On draw 3 they should get copied to an 8-bit atlas.
571 this->drawPathsAndFlush(ccpr, m);
572 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
573 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
574
575 for (int i = 0; i < 10; ++i) {
576 this->drawPathsAndFlush(ccpr, m);
577 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
578 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
579 }
580
581 ccpr.ctx()->performDeferredCleanup(std::chrono::milliseconds(0));
582 }
583 }
584};
585DEF_CCPR_TEST(CCPR_cache_deferredCleanup)
586
587// Verifies the cache/hash table internals.
588class CCPR_cache_hashTable : public CCPRCacheTest {
589 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
590 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
591 using CoverageType = GrCCAtlas::CoverageType;
Mike Reed1f607332020-05-21 12:11:27 -0400592 SkMatrix m = SkMatrix::Scale(20, 20);
Chris Dalton351e80c2019-01-06 22:51:00 -0700593
594 for (int i = 0; i < 5; ++i) {
595 this->drawPathsAndFlush(ccpr, m);
596 if (2 == i) {
597 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
598 } else {
599 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
600 }
601 if (i < 2) {
602 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
603 } else {
604 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
605 }
606
607 auto cache = ccpr.ccpr()->testingOnly_getPathCache();
608 REPORTER_ASSERT(reporter, cache);
609
610 const auto& hash = cache->testingOnly_getHashTable();
611 const auto& lru = cache->testingOnly_getLRU();
612 int count = 0;
613 for (GrCCPathCacheEntry* entry : lru) {
614 auto* node = hash.find(entry->cacheKey());
615 REPORTER_ASSERT(reporter, node);
616 REPORTER_ASSERT(reporter, node->entry() == entry);
617 REPORTER_ASSERT(reporter, 0 == entry->testingOnly_peekOnFlushRefCnt());
618 REPORTER_ASSERT(reporter, entry->unique());
619 if (0 == i) {
620 REPORTER_ASSERT(reporter, !entry->cachedAtlas());
621 } else {
622 const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas();
623 REPORTER_ASSERT(reporter, cachedAtlas);
624 if (1 == i) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600625 REPORTER_ASSERT(reporter, ccpr.ccpr()->coverageType()
Chris Dalton351e80c2019-01-06 22:51:00 -0700626 == cachedAtlas->coverageType());
627 } else {
628 REPORTER_ASSERT(reporter, CoverageType::kA8_LiteralCoverage
629 == cachedAtlas->coverageType());
630 }
631 REPORTER_ASSERT(reporter, cachedAtlas->textureKey().isValid());
632 // The actual proxy should not be held past the end of a flush.
633 REPORTER_ASSERT(reporter, !cachedAtlas->getOnFlushProxy());
634 REPORTER_ASSERT(reporter, 0 == cachedAtlas->testingOnly_peekOnFlushRefCnt());
635 }
636 ++count;
637 }
638 REPORTER_ASSERT(reporter, hash.count() == count);
639 }
640 }
641};
642DEF_CCPR_TEST(CCPR_cache_hashTable)
643
644// Ensures paths get cached even when using a sporadic flushing pattern and drawing out of order
645// (a la Chrome tiles).
646class CCPR_cache_multiFlush : public CCPRCacheTest {
647 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
648 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
649 static constexpr int kNumPaths = SK_ARRAY_COUNT(fPaths);
650 static constexpr int kBigPrimes[] = {
651 9323, 11059, 22993, 38749, 45127, 53147, 64853, 77969, 83269, 99989};
652
653 SkRandom rand;
654 SkMatrix m = SkMatrix::I();
655
656 for (size_t i = 0; i < SK_ARRAY_COUNT(kBigPrimes); ++i) {
657 int prime = kBigPrimes[i];
658 int endPathIdx = (int)rand.nextULessThan(kNumPaths);
659 int pathIdx = endPathIdx;
660 int nextFlush = rand.nextRangeU(1, 47);
661 for (int j = 0; j < kNumPaths; ++j) {
662 pathIdx = (pathIdx + prime) % kNumPaths;
663 int repeat = rand.nextRangeU(1, 3);
664 for (int k = 0; k < repeat; ++k) {
665 ccpr.drawPath(fPaths[pathIdx], m);
666 }
667 if (nextFlush == j) {
668 ccpr.flush();
669 // The paths are small enough that we should never copy to an A8 atlas.
670 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
671 if (i < 2) {
672 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
673 } else {
674 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
675 }
Brian Osman788b9162020-02-07 10:36:46 -0500676 nextFlush = std::min(j + (int)rand.nextRangeU(1, 29), kNumPaths - 1);
Chris Dalton351e80c2019-01-06 22:51:00 -0700677 }
678 }
679 SkASSERT(endPathIdx == pathIdx % kNumPaths);
680 }
681 }
682};
683DEF_CCPR_TEST(CCPR_cache_multiFlush)
684
Chris Daltonaaa77c12019-01-07 17:45:36 -0700685// Ensures a path drawn over mutiple tiles gets cached.
686class CCPR_cache_multiTileCache : public CCPRCacheTest {
687 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
688 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
689 // Make sure a path drawn over 9 tiles gets cached (1 tile out of 9 is >10% visibility).
Mike Reed1f607332020-05-21 12:11:27 -0400690 const SkMatrix m0 = SkMatrix::Scale(kCanvasSize*3, kCanvasSize*3);
Chris Daltonaaa77c12019-01-07 17:45:36 -0700691 const SkPath p0 = fPaths[0];
692 for (int i = 0; i < 9; ++i) {
693 static constexpr int kRowOrder[9] = {0,1,1,0,2,2,2,1,0};
694 static constexpr int kColumnOrder[9] = {0,0,1,1,0,1,2,2,2};
695
696 SkMatrix tileM = m0;
697 tileM.postTranslate(-kCanvasSize * kColumnOrder[i], -kCanvasSize * kRowOrder[i]);
698 ccpr.drawPath(p0, tileM);
699 ccpr.flush();
700 if (i < 5) {
701 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
702 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
703 } else if (5 == i) {
704 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
705 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
706 } else {
707 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
708 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
709 }
710 }
711
712 // Now make sure paths don't get cached when visibility is <10% for every draw (12 tiles).
Mike Reed1f607332020-05-21 12:11:27 -0400713 const SkMatrix m1 = SkMatrix::Scale(kCanvasSize*4, kCanvasSize*3);
Chris Daltonaaa77c12019-01-07 17:45:36 -0700714 const SkPath p1 = fPaths[1];
715 for (int row = 0; row < 3; ++row) {
716 for (int col = 0; col < 4; ++col) {
717 SkMatrix tileM = m1;
718 tileM.postTranslate(-kCanvasSize * col, -kCanvasSize * row);
719 ccpr.drawPath(p1, tileM);
720 ccpr.flush();
721 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
722 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
723 }
724 }
725
726 // Double-check the cache is still intact.
727 ccpr.drawPath(p0, m0);
728 ccpr.flush();
729 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
730 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
731
732 ccpr.drawPath(p1, m1);
733 ccpr.flush();
734 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
735 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
736 }
737};
738DEF_CCPR_TEST(CCPR_cache_multiTileCache)
739
Chris Dalton351e80c2019-01-06 22:51:00 -0700740// This test exercises CCPR's cache capabilities by drawing many paths with two different
741// transformation matrices. We then vary the matrices independently by whole and partial pixels,
742// and verify the caching behaved as expected.
743class CCPR_cache_partialInvalidate : public CCPRCacheTest {
744 void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
745 ctxOptions->fAllowPathMaskCaching = true;
746 }
747
748 static constexpr int kPathSize = 4;
749
750 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
751 const RecordLastMockAtlasIDs& atlasIDRecorder) override {
Chris Dalton4da70192018-06-18 09:51:36 -0600752 SkMatrix matrices[2] = {
Mike Reed1f607332020-05-21 12:11:27 -0400753 SkMatrix::Translate(5, 5),
754 SkMatrix::Translate(kCanvasSize - kPathSize - 5, kCanvasSize - kPathSize - 5)
Chris Dalton4da70192018-06-18 09:51:36 -0600755 };
Chris Dalton351e80c2019-01-06 22:51:00 -0700756 matrices[0].preScale(kPathSize, kPathSize);
757 matrices[1].preScale(kPathSize, kPathSize);
Chris Dalton4da70192018-06-18 09:51:36 -0600758
Chris Dalton351e80c2019-01-06 22:51:00 -0700759 int firstAtlasID = 0;
Chris Dalton4da70192018-06-18 09:51:36 -0600760
Chris Dalton351e80c2019-01-06 22:51:00 -0700761 for (int iterIdx = 0; iterIdx < 4*3*2; ++iterIdx) {
762 this->drawPathsAndFlush(ccpr, matrices, 2);
Chris Dalton4da70192018-06-18 09:51:36 -0600763
Chris Daltona8429cf2018-06-22 11:43:31 -0600764 if (0 == iterIdx) {
765 // First iteration: just note the ID of the stashed atlas and continue.
Chris Dalton351e80c2019-01-06 22:51:00 -0700766 firstAtlasID = atlasIDRecorder.lastRenderedAtlasID();
767 REPORTER_ASSERT(reporter, 0 != firstAtlasID);
Chris Dalton4da70192018-06-18 09:51:36 -0600768 continue;
769 }
770
Chris Dalton351e80c2019-01-06 22:51:00 -0700771 int testIdx = (iterIdx/2) % 3;
772 int repetitionIdx = iterIdx % 2;
773 switch (testIdx) {
774 case 0:
775 if (0 == repetitionIdx) {
776 // This is the big test. New paths were drawn twice last round. On hit 2
777 // (last time), 'firstAtlasID' was cached as a 16-bit atlas. Now, on hit 3,
778 // these paths should be copied out of 'firstAtlasID', and into an A8 atlas.
779 // THEN: we should recycle 'firstAtlasID' and reuse that same texture to
780 // render the new masks.
781 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
782 REPORTER_ASSERT(reporter,
783 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
784 } else {
785 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
786 // This is hit 2 for the new masks. Next time they will be copied to an A8
787 // atlas.
788 REPORTER_ASSERT(reporter,
789 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
790 }
Chris Daltond6fa4542019-01-04 13:23:51 -0700791
Chris Dalton351e80c2019-01-06 22:51:00 -0700792 if (1 == repetitionIdx) {
793 // Integer translates: all path masks stay valid.
794 matrices[0].preTranslate(-1, -1);
795 matrices[1].preTranslate(1, 1);
796 }
797 break;
798
799 case 1:
800 if (0 == repetitionIdx) {
801 // New paths were drawn twice last round. The third hit (now) they should be
802 // copied to an A8 atlas.
803 REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
804 } else {
805 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
806 }
807
808 // This draw should have gotten 100% cache hits; we only did integer translates
809 // last time (or none if it was the first flush). Therefore, everything should
810 // have been cached.
811 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
812
813 if (1 == repetitionIdx) {
814 // Invalidate even path masks.
815 matrices[0].preTranslate(1.6f, 1.4f);
816 }
Chris Dalton4da70192018-06-18 09:51:36 -0600817 break;
818
819 case 2:
Chris Dalton351e80c2019-01-06 22:51:00 -0700820 // No new masks to copy from last time; it had 100% cache hits.
821 REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
Chris Dalton4da70192018-06-18 09:51:36 -0600822
Chris Dalton351e80c2019-01-06 22:51:00 -0700823 // Even path masks were invalidated last iteration by a subpixel translate.
824 // They should have been re-rendered this time in the original 'firstAtlasID'
825 // texture.
826 REPORTER_ASSERT(reporter,
827 atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
Chris Dalton4da70192018-06-18 09:51:36 -0600828
Chris Dalton351e80c2019-01-06 22:51:00 -0700829 if (1 == repetitionIdx) {
830 // Invalidate odd path masks.
831 matrices[1].preTranslate(-1.4f, -1.6f);
832 }
Chris Dalton4da70192018-06-18 09:51:36 -0600833 break;
834 }
835 }
836 }
837};
Chris Dalton351e80c2019-01-06 22:51:00 -0700838DEF_CCPR_TEST(CCPR_cache_partialInvalidate)
Chris Dalton4da70192018-06-18 09:51:36 -0600839
Greg Danielf41b2bd2019-08-22 16:19:24 -0400840class CCPR_unrefPerOpsTaskPathsBeforeOps : public CCPRTest {
Chris Daltondedf8f22018-09-24 20:23:47 -0600841 void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
842 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
843 for (int i = 0; i < 10000; ++i) {
844 // Draw enough paths to make the arena allocator hit the heap.
845 ccpr.drawPath(fPath);
846 }
847
Greg Danielf41b2bd2019-08-22 16:19:24 -0400848 // Unref the GrCCPerOpsTaskPaths object.
849 auto perOpsTaskPathsMap = ccpr.ccpr()->detachPendingPaths();
850 perOpsTaskPathsMap.clear();
Chris Daltondedf8f22018-09-24 20:23:47 -0600851
852 // Now delete the Op and all its draws.
853 REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
854 ccpr.flush();
855 REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
856 }
857};
Greg Danielf41b2bd2019-08-22 16:19:24 -0400858DEF_CCPR_TEST(CCPR_unrefPerOpsTaskPathsBeforeOps)
Chris Daltondedf8f22018-09-24 20:23:47 -0600859
Chris Daltonfddb6c02017-11-04 15:22:22 -0600860class CCPRRenderingTest {
861public:
Chris Daltonc3318f02019-07-19 14:20:53 -0600862 void run(skiatest::Reporter* reporter, GrContext* ctx, DoStroke doStroke) const {
863 if (auto ccpr = ctx->priv().drawingManager()->getCoverageCountingPathRenderer()) {
864 if (DoStroke::kYes == doStroke &&
865 GrCCAtlas::CoverageType::kA8_Multisample == ccpr->coverageType()) {
866 return; // Stroking is not yet supported for multisample.
867 }
868 CCPRPathDrawer drawer(sk_ref_sp(ctx), reporter, doStroke);
869 if (!drawer.valid()) {
870 return;
871 }
872 this->onRun(reporter, drawer);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600873 }
Chris Daltonfddb6c02017-11-04 15:22:22 -0600874 }
875
876 virtual ~CCPRRenderingTest() {}
877
878protected:
879 virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0;
880};
881
882#define DEF_CCPR_RENDERING_TEST(name) \
883 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \
884 name test; \
Chris Daltonc3318f02019-07-19 14:20:53 -0600885 test.run(reporter, ctxInfo.grContext(), DoStroke::kNo); \
886 test.run(reporter, ctxInfo.grContext(), DoStroke::kYes); \
Chris Daltonfddb6c02017-11-04 15:22:22 -0600887 }
888
Chris Dalton351e80c2019-01-06 22:51:00 -0700889class CCPR_busyPath : public CCPRRenderingTest {
Chris Daltonfddb6c02017-11-04 15:22:22 -0600890 void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
891 static constexpr int kNumBusyVerbs = 1 << 17;
892 ccpr.clear();
893 SkPath busyPath;
894 busyPath.moveTo(0, 0); // top left
895 busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right
896 for (int i = 2; i < kNumBusyVerbs; ++i) {
897 float offset = i * ((float)kCanvasSize / kNumBusyVerbs);
898 busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen
899 }
900 ccpr.drawPath(busyPath);
901
902 ccpr.flush(); // If this doesn't crash, the test passed.
903 // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in
904 // your platform's GrGLCaps.
905 }
906};
Chris Dalton351e80c2019-01-06 22:51:00 -0700907DEF_CCPR_RENDERING_TEST(CCPR_busyPath)