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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrClip.h"
|
2017-11-04 21:22:22 +00:00
|
|
|
#include "GrDrawingManager.h"
|
|
|
|
#include "GrPathRenderer.h"
|
|
|
|
#include "GrPaint.h"
|
2017-10-06 22:27:32 +00:00
|
|
|
#include "GrRenderTargetContext.h"
|
|
|
|
#include "GrRenderTargetContextPriv.h"
|
|
|
|
#include "GrShape.h"
|
|
|
|
#include "SkMatrix.h"
|
2017-11-04 21:22:22 +00:00
|
|
|
#include "SkPathPriv.h"
|
2017-10-06 22:27:32 +00:00
|
|
|
#include "SkRect.h"
|
|
|
|
#include "ccpr/GrCoverageCountingPathRenderer.h"
|
2017-11-04 21:22:22 +00:00
|
|
|
#include "mock/GrMockTypes.h"
|
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:
|
|
|
|
CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {}
|
|
|
|
|
|
|
|
private:
|
2018-01-17 16:40:14 +00:00
|
|
|
bool apply(GrContext* context, GrRenderTargetContext* rtc, bool, bool, GrAppliedClip* out,
|
2017-12-05 17:05:21 +00:00
|
|
|
SkRect* bounds) const override {
|
2018-01-17 16:40:14 +00:00
|
|
|
GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
|
|
|
|
out->addCoverageFP(fCCPR->makeClipProcessor(proxyProvider,
|
|
|
|
rtc->priv().testingOnly_getOpListID(), fPath,
|
2017-12-05 17:05:21 +00:00
|
|
|
SkIRect::MakeWH(rtc->width(), rtc->height()),
|
|
|
|
rtc->width(), rtc->height()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool quickContains(const SkRect&) const final { return false; }
|
|
|
|
bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
|
|
|
|
void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
|
|
|
|
rect->set(0, 0, width, height);
|
|
|
|
if (iior) {
|
|
|
|
*iior = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GrCoverageCountingPathRenderer* const fCCPR;
|
|
|
|
const SkPath fPath;
|
|
|
|
};
|
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
class CCPRPathDrawer {
|
|
|
|
public:
|
2017-11-04 21:22:22 +00:00
|
|
|
CCPRPathDrawer(GrContext* ctx, skiatest::Reporter* reporter)
|
2017-10-06 22:27:32 +00:00
|
|
|
: fCtx(ctx)
|
2017-11-04 21:22:22 +00:00
|
|
|
, fCCPR(fCtx->contextPriv().drawingManager()->getCoverageCountingPathRenderer())
|
2017-10-06 22:27:32 +00:00
|
|
|
, fRTC(fCtx->makeDeferredRenderTargetContext(SkBackingFit::kExact, kCanvasSize,
|
|
|
|
kCanvasSize, kRGBA_8888_GrPixelConfig,
|
|
|
|
nullptr)) {
|
2017-11-04 21:22:22 +00:00
|
|
|
if (!fCCPR) {
|
|
|
|
ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests");
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
2017-11-04 21:22:22 +00:00
|
|
|
if (!fRTC) {
|
|
|
|
ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests");
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
bool valid() const { return fCCPR && fRTC; }
|
2017-12-11 22:42:09 +00:00
|
|
|
void clear() const { fRTC->clear(nullptr, 0, GrRenderTargetContext::CanClearFullscreen::kYes); }
|
2017-11-04 21:22:22 +00:00
|
|
|
void abandonGrContext() { fCtx = nullptr; fCCPR = nullptr; fRTC = nullptr; }
|
2017-10-06 22:27:32 +00:00
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
void drawPath(SkPath path, GrColor4f color = GrColor4f(0, 1, 0, 1)) const {
|
|
|
|
SkASSERT(this->valid());
|
2017-10-06 22:27:32 +00:00
|
|
|
|
|
|
|
GrPaint paint;
|
|
|
|
paint.setColor4f(color);
|
2017-11-04 21:22:22 +00:00
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
GrNoClip noClip;
|
|
|
|
SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize);
|
2017-11-04 21:22:22 +00:00
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
SkMatrix matrix = SkMatrix::I();
|
2017-11-04 21:22:22 +00:00
|
|
|
|
|
|
|
path.setIsVolatile(true);
|
2017-10-06 22:27:32 +00:00
|
|
|
GrShape shape(path);
|
2017-11-04 21:22:22 +00:00
|
|
|
|
2017-10-06 22:27:32 +00:00
|
|
|
fCCPR->drawPath({fCtx, std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(),
|
|
|
|
&noClip, &clipBounds, &matrix, &shape, GrAAType::kCoverage, false});
|
|
|
|
}
|
|
|
|
|
2017-12-05 17:05:21 +00:00
|
|
|
void clipFullscreenRect(SkPath clipPath, GrColor4f color = GrColor4f(0, 1, 0, 1)) {
|
|
|
|
SkASSERT(this->valid());
|
|
|
|
|
|
|
|
GrPaint paint;
|
|
|
|
paint.setColor4f(color);
|
|
|
|
|
|
|
|
fRTC->drawRect(CCPRClip(fCCPR, clipPath), std::move(paint), GrAA::kYes, SkMatrix::I(),
|
|
|
|
SkRect::MakeIWH(kCanvasSize, kCanvasSize));
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
void flush() const {
|
|
|
|
SkASSERT(this->valid());
|
2017-10-06 22:27:32 +00:00
|
|
|
fCtx->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-11-04 21:22:22 +00:00
|
|
|
GrContext* fCtx;
|
|
|
|
GrCoverageCountingPathRenderer* fCCPR;
|
|
|
|
sk_sp<GrRenderTargetContext> fRTC;
|
2017-10-06 22:27:32 +00:00
|
|
|
};
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
class CCPRTest {
|
|
|
|
public:
|
|
|
|
void run(skiatest::Reporter* reporter) {
|
|
|
|
GrMockOptions mockOptions;
|
|
|
|
mockOptions.fInstanceAttribSupport = true;
|
|
|
|
mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag;
|
2018-02-03 01:32:49 +00:00
|
|
|
mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fRenderability =
|
|
|
|
GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
|
2017-11-04 21:22:22 +00:00
|
|
|
mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fTexturable = true;
|
|
|
|
mockOptions.fGeometryShaderSupport = true;
|
|
|
|
mockOptions.fIntegerSupport = true;
|
|
|
|
mockOptions.fFlatInterpolationSupport = true;
|
|
|
|
|
|
|
|
GrContextOptions ctxOptions;
|
|
|
|
ctxOptions.fAllowPathMaskCaching = false;
|
|
|
|
ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting;
|
|
|
|
|
|
|
|
fMockContext = GrContext::MakeMock(&mockOptions, ctxOptions);
|
|
|
|
if (!fMockContext) {
|
|
|
|
ERRORF(reporter, "could not create mock context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!fMockContext->unique()) {
|
|
|
|
ERRORF(reporter, "mock context is not unique");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPRPathDrawer ccpr(fMockContext.get(), reporter);
|
|
|
|
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:
|
|
|
|
virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0;
|
|
|
|
|
|
|
|
sk_sp<GrContext> fMockContext;
|
|
|
|
SkPath fPath;
|
|
|
|
};
|
|
|
|
|
2017-11-15 20:48:03 +00:00
|
|
|
#define DEF_CCPR_TEST(name) \
|
|
|
|
DEF_GPUTEST(name, reporter, /* options */) { \
|
|
|
|
name test; \
|
|
|
|
test.run(reporter); \
|
2017-10-06 22:27:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
class GrCCPRTest_cleanup : public CCPRTest {
|
|
|
|
void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
|
|
|
|
// Ensure paths get unreffed.
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
ccpr.drawPath(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));
|
|
|
|
ccpr.flush();
|
|
|
|
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) {
|
|
|
|
ccpr.drawPath(fPath);
|
2017-12-05 17:05:21 +00:00
|
|
|
ccpr.clipFullscreenRect(fPath);
|
2017-11-04 21:22:22 +00:00
|
|
|
}
|
|
|
|
ccpr.abandonGrContext();
|
|
|
|
REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
fMockContext.reset();
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_CCPR_TEST(GrCCPRTest_cleanup)
|
|
|
|
|
2017-11-06 21:19:19 +00:00
|
|
|
class GrCCPRTest_unregisterCulledOps : public CCPRTest {
|
|
|
|
void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
|
|
|
|
// Ensure Ops get unregistered from CCPR when culled early.
|
|
|
|
ccpr.drawPath(fPath);
|
|
|
|
REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
ccpr.clear(); // Clear should delete the CCPR Op.
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself).
|
|
|
|
|
|
|
|
// Ensure Op unregisters work when we delete the context without flushing.
|
|
|
|
ccpr.drawPath(fPath);
|
|
|
|
REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
ccpr.clear(); // Clear should delete the CCPR DrawPathsOp.
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
|
|
|
|
ccpr.abandonGrContext();
|
|
|
|
fMockContext.reset(); // Should not crash (DrawPathsOp should have unregistered itself).
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_CCPR_TEST(GrCCPRTest_unregisterCulledOps)
|
|
|
|
|
2017-11-27 22:34:26 +00:00
|
|
|
class GrCCPRTest_parseEmptyPath : public CCPRTest {
|
|
|
|
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.
|
|
|
|
SkPath largeOutsidePath;
|
|
|
|
largeOutsidePath.moveTo(-1e30f, -1e30f);
|
|
|
|
largeOutsidePath.lineTo(-1e30f, +1e30f);
|
|
|
|
largeOutsidePath.lineTo(-1e10f, +1e30f);
|
|
|
|
ccpr.drawPath(largeOutsidePath);
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
ccpr.drawPath(emptyPath);
|
|
|
|
|
|
|
|
// 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.drawPath(largeOutsidePath);
|
|
|
|
ccpr.clipFullscreenRect(largeOutsidePath);
|
|
|
|
ccpr.drawPath(emptyPath);
|
|
|
|
ccpr.clipFullscreenRect(emptyPath);
|
|
|
|
ccpr.flush();
|
2017-11-27 22:34:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_CCPR_TEST(GrCCPRTest_parseEmptyPath)
|
|
|
|
|
2017-11-04 21:22:22 +00:00
|
|
|
class CCPRRenderingTest {
|
|
|
|
public:
|
|
|
|
void run(skiatest::Reporter* reporter, GrContext* ctx) const {
|
|
|
|
if (!ctx->contextPriv().drawingManager()->getCoverageCountingPathRenderer()) {
|
|
|
|
return; // CCPR is not enabled on this GPU.
|
|
|
|
}
|
|
|
|
CCPRPathDrawer ccpr(ctx, reporter);
|
|
|
|
if (!ccpr.valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->onRun(reporter, ccpr);
|
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; \
|
|
|
|
test.run(reporter, ctxInfo.grContext()); \
|
|
|
|
}
|
|
|
|
|
|
|
|
class GrCCPRTest_busyPath : public CCPRRenderingTest {
|
|
|
|
void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
|
|
|
|
static constexpr int kNumBusyVerbs = 1 << 17;
|
|
|
|
ccpr.clear();
|
|
|
|
SkPath busyPath;
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ccpr.drawPath(busyPath);
|
|
|
|
|
|
|
|
ccpr.flush(); // If this doesn't crash, the test passed.
|
|
|
|
// If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in
|
|
|
|
// your platform's GrGLCaps.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_CCPR_RENDERING_TEST(GrCCPRTest_busyPath)
|
2017-10-06 22:27:32 +00:00
|
|
|
|
|
|
|
#endif
|