2017-10-06 22:27:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tests/Test.h"
|
2017-10-06 22:27:32 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
2020-08-26 16:56:51 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRect.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/mock/GrMockTypes.h"
|
|
|
|
#include "src/core/SkPathPriv.h"
|
|
|
|
#include "src/gpu/GrClip.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrDrawingManager.h"
|
|
|
|
#include "src/gpu/GrPaint.h"
|
|
|
|
#include "src/gpu/GrPathRenderer.h"
|
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2020-12-09 21:37:04 +00:00
|
|
|
#include "src/gpu/GrSurfaceDrawContext.h"
|
2020-03-05 19:14:18 +00:00
|
|
|
#include "src/gpu/GrTexture.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
|
2020-04-17 20:21:37 +00:00
|
|
|
#include "src/gpu/geometry/GrStyledShape.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2018-09-19 15:31:27 +00:00
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
static constexpr int kCanvasSize = 100;
|
|
|
|
|
2017-12-05 17:05:21 +00:00
|
|
|
class CCPRClip : public GrClip {
|
|
|
|
public:
|
2021-04-02 15:44:36 +00:00
|
|
|
CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {}
|
2017-12-05 17:05:21 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-11 14:29:00 +00:00
|
|
|
SkIRect getConservativeBounds() const final { return fPath.getBounds().roundOut(); }
|
2020-12-09 21:37:04 +00:00
|
|
|
Effect apply(GrRecordingContext* context, GrSurfaceDrawContext* rtc, GrAAType,
|
2021-05-27 15:40:43 +00:00
|
|
|
GrAppliedClip* out, SkRect* bounds) const override {
|
2021-04-02 15:44:36 +00:00
|
|
|
auto [success, fp] = fCCPR->makeClipProcessor(/*inputFP=*/nullptr,
|
|
|
|
rtc->getOpsTask()->uniqueID(), fPath,
|
|
|
|
SkIRect::MakeWH(rtc->width(), rtc->height()),
|
|
|
|
*context->priv().caps());
|
|
|
|
if (success) {
|
|
|
|
out->addCoverageFP(std::move(fp));
|
|
|
|
return Effect::kClipped;
|
|
|
|
} else {
|
2021-03-19 21:44:18 +00:00
|
|
|
return Effect::kClippedOut;
|
|
|
|
}
|
2017-12-05 17:05:21 +00:00
|
|
|
}
|
2020-05-13 18:17:57 +00:00
|
|
|
|
2021-04-02 15:44:36 +00:00
|
|
|
GrCoverageCountingPathRenderer* const fCCPR;
|
2017-12-05 17:05:21 +00:00
|
|
|
const SkPath fPath;
|
|
|
|
};
|
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
class CCPRPathDrawer {
|
|
|
|
public:
|
2021-03-12 02:41:40 +00:00
|
|
|
CCPRPathDrawer(sk_sp<GrDirectContext> dContext, skiatest::Reporter* reporter)
|
2020-07-17 19:40:13 +00:00
|
|
|
: fDContext(dContext)
|
|
|
|
, fCCPR(fDContext->priv().drawingManager()->getCoverageCountingPathRenderer())
|
2020-12-09 21:37:04 +00:00
|
|
|
, fRTC(GrSurfaceDrawContext::Make(
|
2020-07-17 19:40:13 +00:00
|
|
|
fDContext.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
|
2021-04-19 23:27:09 +00:00
|
|
|
{kCanvasSize, kCanvasSize}, SkSurfaceProps())) {
|
2021-01-15 21:38:27 +00:00
|
|
|
if (!fCCPR) {
|
|
|
|
ERRORF(reporter, "ccpr not enabled in GrDirectContext for ccpr tests");
|
|
|
|
}
|
2017-11-04 21:22:22 +00:00
|
|
|
if (!fRTC) {
|
2020-12-09 21:37:04 +00:00
|
|
|
ERRORF(reporter, "failed to create GrSurfaceDrawContext for ccpr tests");
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
GrDirectContext* dContext() const { return fDContext.get(); }
|
2018-06-18 15:51:36 +00:00
|
|
|
GrCoverageCountingPathRenderer* ccpr() const { return fCCPR; }
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
bool valid() const { return fCCPR && fRTC; }
|
2020-05-26 20:57:38 +00:00
|
|
|
void clear() const { fRTC->clear(SK_PMColor4fTRANSPARENT); }
|
2019-01-07 05:51:00 +00:00
|
|
|
void destroyGrContext() {
|
2020-07-17 19:40:13 +00:00
|
|
|
SkASSERT(fDContext->unique());
|
2019-01-07 05:51:00 +00:00
|
|
|
fRTC.reset();
|
|
|
|
fCCPR = nullptr;
|
2020-07-17 19:40:13 +00:00
|
|
|
fDContext.reset();
|
2019-01-07 05:51:00 +00:00
|
|
|
}
|
2017-10-06 22:27:32 +00:00
|
|
|
|
2021-03-12 02:41:40 +00:00
|
|
|
void clipFullscreenRect(SkPath clipPath, const SkMatrix& matrix = SkMatrix::I()) const {
|
2017-12-05 17:05:21 +00:00
|
|
|
SkASSERT(this->valid());
|
|
|
|
|
|
|
|
GrPaint paint;
|
2021-03-12 02:41:40 +00:00
|
|
|
paint.setColor4f({0, 1, 0, 1});
|
2017-12-05 17:05:21 +00:00
|
|
|
|
2021-04-02 15:44:36 +00:00
|
|
|
CCPRClip clip(fCCPR, clipPath);
|
2020-05-29 13:54:07 +00:00
|
|
|
fRTC->drawRect(&clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
|
2017-12-05 17:05:21 +00:00
|
|
|
SkRect::MakeIWH(kCanvasSize, kCanvasSize));
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
void flush() const {
|
|
|
|
SkASSERT(this->valid());
|
2020-07-17 19:40:13 +00:00
|
|
|
fDContext->flushAndSubmit();
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-17 19:40:13 +00:00
|
|
|
sk_sp<GrDirectContext> fDContext;
|
2018-06-18 15:51:36 +00:00
|
|
|
GrCoverageCountingPathRenderer* fCCPR;
|
2020-12-09 21:37:04 +00:00
|
|
|
std::unique_ptr<GrSurfaceDrawContext> fRTC;
|
2017-10-06 22:27:32 +00:00
|
|
|
};
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
class CCPRTest {
|
|
|
|
public:
|
2021-03-12 02:41:40 +00:00
|
|
|
void run(skiatest::Reporter* reporter) {
|
2017-11-04 21:22:22 +00:00
|
|
|
GrMockOptions mockOptions;
|
2020-04-03 20:50:43 +00:00
|
|
|
mockOptions.fDrawInstancedSupport = true;
|
2019-01-09 21:30:12 +00:00
|
|
|
mockOptions.fHalfFloatVertexAttributeSupport = true;
|
2017-11-04 21:22:22 +00:00
|
|
|
mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag;
|
2019-07-09 16:34:38 +00:00
|
|
|
mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
|
2018-02-03 01:32:49 +00:00
|
|
|
GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
|
2019-07-09 16:34:38 +00:00
|
|
|
mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
|
|
|
|
mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability =
|
2019-07-19 20:20:53 +00:00
|
|
|
GrMockOptions::ConfigOptions::Renderability::kMSAA;
|
2019-07-09 16:34:38 +00:00
|
|
|
mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true;
|
2017-11-04 21:22:22 +00:00
|
|
|
mockOptions.fGeometryShaderSupport = true;
|
|
|
|
mockOptions.fIntegerSupport = true;
|
|
|
|
mockOptions.fFlatInterpolationSupport = true;
|
|
|
|
|
|
|
|
GrContextOptions ctxOptions;
|
|
|
|
ctxOptions.fAllowPathMaskCaching = false;
|
|
|
|
ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting;
|
|
|
|
|
2018-06-24 19:08:57 +00:00
|
|
|
this->customizeOptions(&mockOptions, &ctxOptions);
|
|
|
|
|
2020-07-13 20:13:31 +00:00
|
|
|
sk_sp<GrDirectContext> mockContext = GrDirectContext::MakeMock(&mockOptions, ctxOptions);
|
2019-01-07 05:51:00 +00:00
|
|
|
if (!mockContext) {
|
2017-11-04 21:22:22 +00:00
|
|
|
ERRORF(reporter, "could not create mock context");
|
|
|
|
return;
|
|
|
|
}
|
2019-01-07 05:51:00 +00:00
|
|
|
if (!mockContext->unique()) {
|
2017-11-04 21:22:22 +00:00
|
|
|
ERRORF(reporter, "mock context is not unique");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:41:40 +00:00
|
|
|
CCPRPathDrawer ccpr(std::exchange(mockContext, nullptr), reporter);
|
2017-11-04 21:22:22 +00:00
|
|
|
if (!ccpr.valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fPath.moveTo(0, 0);
|
|
|
|
fPath.cubicTo(50, 50, 0, 50, 50, 0);
|
|
|
|
this->onRun(reporter, ccpr);
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
virtual ~CCPRTest() {}
|
|
|
|
|
|
|
|
protected:
|
2018-06-24 19:08:57 +00:00
|
|
|
virtual void customizeOptions(GrMockOptions*, GrContextOptions*) {}
|
2017-11-04 21:22:22 +00:00
|
|
|
virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0;
|
|
|
|
|
2018-06-18 15:51:36 +00:00
|
|
|
SkPath fPath;
|
2017-11-04 21:22:22 +00:00
|
|
|
};
|
|
|
|
|
2018-08-31 11:53:15 +00:00
|
|
|
#define DEF_CCPR_TEST(name) \
|
2017-11-15 20:48:03 +00:00
|
|
|
DEF_GPUTEST(name, reporter, /* options */) { \
|
2018-08-31 11:53:15 +00:00
|
|
|
name test; \
|
2021-03-12 02:41:40 +00:00
|
|
|
test.run(reporter); \
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 05:51:00 +00:00
|
|
|
class CCPR_cleanup : public CCPRTest {
|
2020-02-15 18:41:30 +00:00
|
|
|
protected:
|
2017-11-04 21:22:22 +00:00
|
|
|
void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
|
|
|
|
// Ensure paths get unreffed when we delete the context without flushing.
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(fPath);
|
2017-12-05 17:05:21 +00:00
|
|
|
ccpr.clipFullscreenRect(fPath);
|
2017-11-04 21:22:22 +00:00
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
|
2019-01-07 05:51:00 +00:00
|
|
|
|
|
|
|
ccpr.destroyGrContext();
|
2017-11-04 21:22:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
}
|
|
|
|
};
|
2019-01-07 05:51:00 +00:00
|
|
|
DEF_CCPR_TEST(CCPR_cleanup)
|
2017-11-04 21:22:22 +00:00
|
|
|
|
2019-01-07 05:51:00 +00:00
|
|
|
class CCPR_cleanupWithTexAllocFail : public CCPR_cleanup {
|
2018-06-24 19:08:57 +00:00
|
|
|
void customizeOptions(GrMockOptions* mockOptions, GrContextOptions*) override {
|
|
|
|
mockOptions->fFailTextureAllocations = true;
|
2018-04-18 19:24:25 +00:00
|
|
|
}
|
2020-02-15 18:41:30 +00:00
|
|
|
void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
|
2020-07-17 19:40:13 +00:00
|
|
|
((GrRecordingContext*)ccpr.dContext())->priv().incrSuppressWarningMessages();
|
2020-02-15 18:41:30 +00:00
|
|
|
this->CCPR_cleanup::onRun(reporter, ccpr);
|
|
|
|
}
|
2018-04-18 19:24:25 +00:00
|
|
|
};
|
2019-01-07 05:51:00 +00:00
|
|
|
DEF_CCPR_TEST(CCPR_cleanupWithTexAllocFail)
|
2018-04-18 19:24:25 +00:00
|
|
|
|
2019-01-07 05:51:00 +00:00
|
|
|
class CCPR_parseEmptyPath : public CCPRTest {
|
2017-11-27 22:34:26 +00:00
|
|
|
void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
|
|
|
|
// Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with
|
|
|
|
// an empty path.
|
2020-08-25 15:48:41 +00:00
|
|
|
SkPath largeOutsidePath = SkPath::Polygon({
|
|
|
|
{-1e30f, -1e30f},
|
|
|
|
{-1e30f, +1e30f},
|
|
|
|
{-1e10f, +1e30f},
|
|
|
|
}, false);
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(largeOutsidePath);
|
2017-11-27 22:34:26 +00:00
|
|
|
|
|
|
|
// Normally an empty path is culled before reaching ccpr, however we use a back door for
|
|
|
|
// testing so this path will make it.
|
|
|
|
SkPath emptyPath;
|
|
|
|
SkASSERT(emptyPath.isEmpty());
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(emptyPath);
|
2017-11-27 22:34:26 +00:00
|
|
|
|
|
|
|
// This is the test. It will exercise various internal asserts and verify we do not crash.
|
|
|
|
ccpr.flush();
|
2017-12-05 17:05:21 +00:00
|
|
|
|
|
|
|
// Now try again with clips.
|
|
|
|
ccpr.clipFullscreenRect(largeOutsidePath);
|
|
|
|
ccpr.clipFullscreenRect(emptyPath);
|
|
|
|
ccpr.flush();
|
|
|
|
|
|
|
|
// ... and both.
|
|
|
|
ccpr.clipFullscreenRect(largeOutsidePath);
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(largeOutsidePath);
|
|
|
|
ccpr.clipFullscreenRect(emptyPath);
|
2017-12-05 17:05:21 +00:00
|
|
|
ccpr.clipFullscreenRect(emptyPath);
|
|
|
|
ccpr.flush();
|
2017-11-27 22:34:26 +00:00
|
|
|
}
|
|
|
|
};
|
2019-01-07 05:51:00 +00:00
|
|
|
DEF_CCPR_TEST(CCPR_parseEmptyPath)
|
2018-06-18 15:51:36 +00:00
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
class CCPRRenderingTest {
|
|
|
|
public:
|
2021-03-12 02:41:40 +00:00
|
|
|
void run(skiatest::Reporter* reporter, GrDirectContext* dContext) const {
|
|
|
|
if (dContext->priv().drawingManager()->getCoverageCountingPathRenderer()) {
|
|
|
|
CCPRPathDrawer drawer(sk_ref_sp(dContext), reporter);
|
2019-07-19 20:20:53 +00:00
|
|
|
if (!drawer.valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->onRun(reporter, drawer);
|
2017-11-04 21:22:22 +00:00
|
|
|
}
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
virtual ~CCPRRenderingTest() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEF_CCPR_RENDERING_TEST(name) \
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \
|
|
|
|
name test; \
|
2021-03-12 02:41:40 +00:00
|
|
|
test.run(reporter, ctxInfo.directContext()); \
|
2017-11-04 21:22:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 05:51:00 +00:00
|
|
|
class CCPR_busyPath : public CCPRRenderingTest {
|
2017-11-04 21:22:22 +00:00
|
|
|
void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
|
|
|
|
static constexpr int kNumBusyVerbs = 1 << 17;
|
|
|
|
ccpr.clear();
|
2020-08-26 16:56:51 +00:00
|
|
|
SkPathBuilder busyPath;
|
2017-11-04 21:22:22 +00:00
|
|
|
busyPath.moveTo(0, 0); // top left
|
|
|
|
busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right
|
|
|
|
for (int i = 2; i < kNumBusyVerbs; ++i) {
|
|
|
|
float offset = i * ((float)kCanvasSize / kNumBusyVerbs);
|
|
|
|
busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen
|
|
|
|
}
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(busyPath.detach());
|
2017-11-04 21:22:22 +00:00
|
|
|
|
|
|
|
ccpr.flush(); // If this doesn't crash, the test passed.
|
|
|
|
// If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in
|
|
|
|
// your platform's GrGLCaps.
|
|
|
|
}
|
|
|
|
};
|
2019-01-07 05:51:00 +00:00
|
|
|
DEF_CCPR_RENDERING_TEST(CCPR_busyPath)
|
2020-10-13 22:09:09 +00:00
|
|
|
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=1102117
|
|
|
|
class CCPR_evictCacheEntryForPendingDrawOp : public CCPRRenderingTest {
|
|
|
|
void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
|
|
|
|
static constexpr SkRect kRect = SkRect::MakeWH(50, 50);
|
|
|
|
ccpr.clear();
|
|
|
|
|
|
|
|
// make sure path is cached.
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
SkPath path;
|
|
|
|
path.addRect(kRect);
|
|
|
|
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(path);
|
2020-10-13 22:09:09 +00:00
|
|
|
ccpr.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// make enough cached draws to make DoCopies happen.
|
|
|
|
for (int i = 0; i <= GrCoverageCountingPathRenderer::kDoCopiesThreshold; i++) {
|
|
|
|
SkPath path;
|
|
|
|
path.addRect(kRect);
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(path);
|
2020-10-13 22:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now draw the path in an incompatible matrix. Previous draw's cached atlas should
|
|
|
|
// not be invalidated. otherwise, this flush would render more paths than allocated for.
|
|
|
|
auto m = SkMatrix::Translate(0.1f, 0.1f);
|
|
|
|
SkPath path;
|
|
|
|
path.addRect(kRect);
|
2021-03-12 02:41:40 +00:00
|
|
|
ccpr.clipFullscreenRect(path, m);
|
2020-10-13 22:09:09 +00:00
|
|
|
ccpr.flush();
|
|
|
|
|
|
|
|
// if this test does not crash, it is passed.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_CCPR_RENDERING_TEST(CCPR_evictCacheEntryForPendingDrawOp)
|