2021-05-18 18:37:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkPathPriv.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2021-08-11 19:43:50 +00:00
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2021-05-18 18:37:08 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2021-08-11 19:43:50 +00:00
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
|
|
|
#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
|
2021-08-19 18:57:05 +00:00
|
|
|
#include "src/gpu/ops/TessellationPathRenderer.h"
|
2021-06-02 01:22:05 +00:00
|
|
|
#include "src/gpu/tessellate/GrPathCurveTessellator.h"
|
|
|
|
#include "src/gpu/tessellate/GrPathWedgeTessellator.h"
|
2021-06-01 19:40:03 +00:00
|
|
|
#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
|
2021-07-28 19:13:20 +00:00
|
|
|
#include "src/gpu/v1/SurfaceDrawContext_v1.h"
|
2021-05-18 18:37:08 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class Mode {
|
2021-06-09 06:12:59 +00:00
|
|
|
kWedgeMiddleOut,
|
2021-05-18 18:37:08 +00:00
|
|
|
kCurveMiddleOut,
|
|
|
|
kWedgeTessellate,
|
|
|
|
kCurveTessellate
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* ModeName(Mode mode) {
|
|
|
|
switch (mode) {
|
2021-06-09 06:12:59 +00:00
|
|
|
case Mode::kWedgeMiddleOut:
|
|
|
|
return "MiddleOutShader (kWedges)";
|
2021-05-18 18:37:08 +00:00
|
|
|
case Mode::kCurveMiddleOut:
|
2021-06-09 06:12:59 +00:00
|
|
|
return "MiddleOutShader (kCurves)";
|
2021-05-18 18:37:08 +00:00
|
|
|
case Mode::kWedgeTessellate:
|
2021-06-09 06:12:59 +00:00
|
|
|
return "HardwareWedgeShader";
|
2021-05-18 18:37:08 +00:00
|
|
|
case Mode::kCurveTessellate:
|
2021-06-09 06:12:59 +00:00
|
|
|
return "HardwareCurveShader";
|
2021-05-18 18:37:08 +00:00
|
|
|
}
|
|
|
|
SkUNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws a path directly to the screen using a specific tessellator.
|
|
|
|
class SamplePathTessellatorOp : public GrDrawOp {
|
|
|
|
private:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
|
|
|
SamplePathTessellatorOp(const SkRect& drawBounds, const SkPath& path, const SkMatrix& m,
|
|
|
|
GrPipeline::InputFlags pipelineFlags, Mode mode)
|
|
|
|
: GrDrawOp(ClassID())
|
|
|
|
, fPath(path)
|
|
|
|
, fMatrix(m)
|
|
|
|
, fPipelineFlags(pipelineFlags)
|
|
|
|
, fMode(mode) {
|
|
|
|
this->setBounds(drawBounds, HasAABloat::kNo, IsHairline::kNo);
|
|
|
|
}
|
|
|
|
const char* name() const override { return "SamplePathTessellatorOp"; }
|
2021-06-17 13:23:58 +00:00
|
|
|
void visitProxies(const GrVisitProxyFunc&) const override {}
|
2021-05-18 18:37:08 +00:00
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override {
|
|
|
|
return FixedFunctionFlags::kUsesHWAA;
|
|
|
|
}
|
|
|
|
GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
|
|
|
|
GrClampType clampType) override {
|
|
|
|
SkPMColor4f color;
|
|
|
|
return fProcessors.finalize(SK_PMColor4fWHITE, GrProcessorAnalysisCoverage::kNone, clip,
|
|
|
|
nullptr, caps, clampType, &color);
|
|
|
|
}
|
|
|
|
void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*,
|
2021-06-02 15:58:05 +00:00
|
|
|
const GrDstProxyView&, GrXferBarrierFlags, GrLoadOp colorLoadOp) override {}
|
2021-05-18 18:37:08 +00:00
|
|
|
void onPrepare(GrOpFlushState* flushState) override {
|
2021-06-01 18:11:57 +00:00
|
|
|
constexpr static SkPMColor4f kCyan = {0,1,1,1};
|
2021-05-18 18:37:08 +00:00
|
|
|
auto alloc = flushState->allocator();
|
2021-07-27 16:00:12 +00:00
|
|
|
const SkMatrix& shaderMatrix = SkMatrix::I();
|
|
|
|
const SkMatrix& pathMatrix = fMatrix;
|
2021-06-09 06:12:59 +00:00
|
|
|
const GrCaps& caps = flushState->caps();
|
|
|
|
int numVerbsToGetMiddleOut = 0;
|
|
|
|
int numVerbsToGetTessellation = caps.minPathVerbsForHwTessellation();
|
2021-06-09 19:49:43 +00:00
|
|
|
auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, std::move(fProcessors),
|
|
|
|
fPipelineFlags);
|
2021-05-18 18:37:08 +00:00
|
|
|
switch (fMode) {
|
2021-06-09 06:12:59 +00:00
|
|
|
using DrawInnerFan = GrPathCurveTessellator::DrawInnerFan;
|
|
|
|
case Mode::kWedgeMiddleOut:
|
2021-07-27 16:00:12 +00:00
|
|
|
fTessellator = GrPathWedgeTessellator::Make(alloc, shaderMatrix, kCyan,
|
2021-06-09 19:49:43 +00:00
|
|
|
numVerbsToGetMiddleOut, *pipeline,
|
|
|
|
caps);
|
2021-06-09 06:12:59 +00:00
|
|
|
break;
|
2021-05-18 18:37:08 +00:00
|
|
|
case Mode::kCurveMiddleOut:
|
2021-07-27 16:00:12 +00:00
|
|
|
fTessellator = GrPathCurveTessellator::Make(alloc, shaderMatrix, kCyan,
|
2021-06-08 22:25:46 +00:00
|
|
|
DrawInnerFan::kYes,
|
2021-06-09 19:49:43 +00:00
|
|
|
numVerbsToGetMiddleOut, *pipeline,
|
|
|
|
caps);
|
2021-05-18 18:37:08 +00:00
|
|
|
break;
|
|
|
|
case Mode::kWedgeTessellate:
|
2021-07-27 16:00:12 +00:00
|
|
|
fTessellator = GrPathWedgeTessellator::Make(alloc, shaderMatrix, kCyan,
|
2021-06-09 19:49:43 +00:00
|
|
|
numVerbsToGetTessellation, *pipeline,
|
|
|
|
caps);
|
2021-05-18 18:37:08 +00:00
|
|
|
break;
|
|
|
|
case Mode::kCurveTessellate:
|
2021-07-27 16:00:12 +00:00
|
|
|
fTessellator = GrPathCurveTessellator::Make(alloc, shaderMatrix, kCyan,
|
2021-06-08 22:25:46 +00:00
|
|
|
DrawInnerFan::kYes,
|
2021-06-09 19:49:43 +00:00
|
|
|
numVerbsToGetTessellation, *pipeline,
|
|
|
|
caps);
|
2021-05-18 18:37:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-28 15:43:52 +00:00
|
|
|
fTessellator->prepare(flushState, this->bounds(), {pathMatrix, fPath}, fPath.countVerbs());
|
2021-06-01 18:11:57 +00:00
|
|
|
fProgram = GrTessellationShader::MakeProgram({alloc, flushState->writeView(),
|
2021-08-26 16:05:11 +00:00
|
|
|
flushState->usesMSAASurface(),
|
2021-06-01 18:11:57 +00:00
|
|
|
&flushState->dstProxyView(),
|
|
|
|
flushState->renderPassBarriers(),
|
|
|
|
GrLoadOp::kClear, &flushState->caps()},
|
|
|
|
fTessellator->shader(), pipeline,
|
|
|
|
&GrUserStencilSettings::kUnused);
|
2021-05-18 18:37:08 +00:00
|
|
|
}
|
|
|
|
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
|
|
|
|
flushState->bindPipeline(*fProgram, chainBounds);
|
|
|
|
fTessellator->draw(flushState);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkPath fPath;
|
|
|
|
const SkMatrix fMatrix;
|
|
|
|
const GrPipeline::InputFlags fPipelineFlags;
|
|
|
|
const Mode fMode;
|
|
|
|
GrPathTessellator* fTessellator = nullptr;
|
|
|
|
GrProgramInfo* fProgram;
|
|
|
|
GrProcessorSet fProcessors{SkBlendMode::kSrcOver};
|
|
|
|
|
|
|
|
friend class GrOp; // For ctor.
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// This sample enables wireframe and visualizes the triangles generated by path tessellators.
|
|
|
|
class SamplePathTessellators : public Sample {
|
|
|
|
public:
|
|
|
|
SamplePathTessellators() {
|
|
|
|
#if 0
|
|
|
|
// For viewing middle-out triangulations of the inner fan.
|
|
|
|
fPath.moveTo(1, 0);
|
|
|
|
int numSides = 32 * 3;
|
|
|
|
for (int i = 1; i < numSides; ++i) {
|
|
|
|
float theta = 2*3.1415926535897932384626433832785 * i / numSides;
|
|
|
|
fPath.lineTo(std::cos(theta), std::sin(theta));
|
|
|
|
}
|
|
|
|
fPath.transform(SkMatrix::Scale(200, 200));
|
|
|
|
fPath.transform(SkMatrix::Translate(300, 300));
|
|
|
|
#else
|
|
|
|
fPath.moveTo(100, 500);
|
|
|
|
fPath.cubicTo(300, 400, -100, 300, 100, 200);
|
|
|
|
fPath.quadTo(250, 0, 400, 200);
|
|
|
|
fPath.conicTo(600, 350, 400, 500, fConicWeight);
|
|
|
|
fPath.close();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onDrawContent(SkCanvas*) override;
|
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
|
|
|
|
bool onClick(Sample::Click*) override;
|
|
|
|
bool onChar(SkUnichar) override;
|
|
|
|
|
|
|
|
SkString name() override { return SkString("PathTessellators"); }
|
|
|
|
|
|
|
|
SkPath fPath;
|
2021-08-19 03:39:02 +00:00
|
|
|
GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kWireframe;
|
2021-06-09 06:12:59 +00:00
|
|
|
Mode fMode = Mode::kWedgeMiddleOut;
|
2021-05-18 18:37:08 +00:00
|
|
|
|
|
|
|
float fConicWeight = .5;
|
|
|
|
|
|
|
|
class Click;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SamplePathTessellators::onDrawContent(SkCanvas* canvas) {
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
|
|
|
|
auto ctx = canvas->recordingContext();
|
2021-07-28 19:13:20 +00:00
|
|
|
auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
|
2021-05-18 18:37:08 +00:00
|
|
|
|
|
|
|
SkString error;
|
|
|
|
if (!sdc || !ctx) {
|
|
|
|
error = "GPU Only.";
|
2021-08-19 21:04:05 +00:00
|
|
|
} else if (!skgpu::v1::TessellationPathRenderer::IsSupported(*ctx->priv().caps())) {
|
|
|
|
error = "TessellationPathRenderer not supported.";
|
2021-05-18 18:37:08 +00:00
|
|
|
} else if (fMode >= Mode::kWedgeTessellate &&
|
|
|
|
!ctx->priv().caps()->shaderCaps()->tessellationSupport()) {
|
|
|
|
error.printf("%s requires hardware tessellation support.", ModeName(fMode));
|
|
|
|
}
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
SkFont font(nullptr, 20);
|
|
|
|
SkPaint captionPaint;
|
|
|
|
captionPaint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->drawString(error.c_str(), 10, 30, font, captionPaint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx,
|
|
|
|
sdc->asRenderTargetProxy()->getBoundsRect(),
|
|
|
|
fPath, canvas->getTotalMatrix(),
|
|
|
|
fPipelineFlags, fMode));
|
|
|
|
|
|
|
|
// Draw the path points.
|
|
|
|
SkPaint pointsPaint;
|
|
|
|
pointsPaint.setColor(SK_ColorBLUE);
|
|
|
|
pointsPaint.setStrokeWidth(8);
|
|
|
|
SkPath devPath = fPath;
|
|
|
|
devPath.transform(canvas->getTotalMatrix());
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->setMatrix(SkMatrix::I());
|
|
|
|
SkString caption(ModeName(fMode));
|
|
|
|
caption.appendf(" (w=%g)", fConicWeight);
|
|
|
|
SkFont font(nullptr, 20);
|
|
|
|
SkPaint captionPaint;
|
|
|
|
captionPaint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->drawString(caption, 10, 30, font, captionPaint);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(),
|
|
|
|
SkPathPriv::PointData(devPath), pointsPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SamplePathTessellators::Click : public Sample::Click {
|
|
|
|
public:
|
|
|
|
Click(int ptIdx) : fPtIdx(ptIdx) {}
|
|
|
|
|
|
|
|
void doClick(SkPath* path) {
|
|
|
|
SkPoint pt = path->getPoint(fPtIdx);
|
|
|
|
SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fPtIdx;
|
|
|
|
};
|
|
|
|
|
|
|
|
Sample::Click* SamplePathTessellators::onFindClickHandler(SkScalar x, SkScalar y,
|
|
|
|
skui::ModifierKey) {
|
|
|
|
const SkPoint* pts = SkPathPriv::PointData(fPath);
|
|
|
|
float fuzz = 30;
|
|
|
|
for (int i = 0; i < fPath.countPoints(); ++i) {
|
|
|
|
if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) {
|
|
|
|
return new Click(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SamplePathTessellators::onClick(Sample::Click* click) {
|
|
|
|
Click* myClick = (Click*)click;
|
|
|
|
myClick->doClick(&fPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPath update_weight(const SkPath& path, float w) {
|
|
|
|
SkPath path_;
|
|
|
|
for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) {
|
|
|
|
switch (verb) {
|
|
|
|
case SkPathVerb::kMove:
|
|
|
|
path_.moveTo(pts[0]);
|
|
|
|
break;
|
|
|
|
case SkPathVerb::kLine:
|
|
|
|
path_.lineTo(pts[1]);
|
|
|
|
break;
|
|
|
|
case SkPathVerb::kQuad:
|
|
|
|
path_.quadTo(pts[1], pts[2]);
|
|
|
|
break;
|
|
|
|
case SkPathVerb::kCubic:
|
|
|
|
path_.cubicTo(pts[1], pts[2], pts[3]);
|
|
|
|
break;
|
|
|
|
case SkPathVerb::kConic:
|
|
|
|
path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f);
|
|
|
|
break;
|
|
|
|
case SkPathVerb::kClose:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SamplePathTessellators::onChar(SkUnichar unichar) {
|
|
|
|
switch (unichar) {
|
|
|
|
case 'w':
|
|
|
|
fPipelineFlags = (GrPipeline::InputFlags)(
|
|
|
|
(int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe);
|
|
|
|
return true;
|
|
|
|
case 'D': {
|
|
|
|
fPath.dump();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case '+':
|
|
|
|
fConicWeight *= 2;
|
|
|
|
fPath = update_weight(fPath, fConicWeight);
|
|
|
|
return true;
|
|
|
|
case '=':
|
|
|
|
fConicWeight *= 5/4.f;
|
|
|
|
fPath = update_weight(fPath, fConicWeight);
|
|
|
|
return true;
|
|
|
|
case '_':
|
|
|
|
fConicWeight *= .5f;
|
|
|
|
fPath = update_weight(fPath, fConicWeight);
|
|
|
|
return true;
|
|
|
|
case '-':
|
|
|
|
fConicWeight *= 4/5.f;
|
|
|
|
fPath = update_weight(fPath, fConicWeight);
|
|
|
|
return true;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
2021-06-09 06:12:59 +00:00
|
|
|
case '4':
|
2021-05-18 18:37:08 +00:00
|
|
|
fMode = (Mode)(unichar - '1');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sample* MakeTessellatedPathSample() { return new SamplePathTessellators; }
|
|
|
|
static SampleRegistry gTessellatedPathSample(MakeTessellatedPathSample);
|
|
|
|
|
|
|
|
#endif // SK_SUPPORT_GPU
|