2017-07-14 21:17:41 +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"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "GrContextPriv.h"
|
2018-08-14 21:51:23 +00:00
|
|
|
#include "GrMemoryPool.h"
|
2017-09-05 22:46:48 +00:00
|
|
|
#include "GrPathUtils.h"
|
2017-07-14 21:17:41 +00:00
|
|
|
#include "GrRenderTargetContext.h"
|
|
|
|
#include "GrRenderTargetContextPriv.h"
|
|
|
|
#include "GrResourceProvider.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2017-07-14 21:17:41 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
2018-01-08 20:05:02 +00:00
|
|
|
#include "SkRectPriv.h"
|
2018-01-08 22:21:41 +00:00
|
|
|
#include "ccpr/GrCCCoverageProcessor.h"
|
2018-08-20 20:00:30 +00:00
|
|
|
#include "ccpr/GrCCFillGeometry.h"
|
2018-08-31 11:53:15 +00:00
|
|
|
#include "ccpr/GrCCStroker.h"
|
2017-07-14 21:17:41 +00:00
|
|
|
#include "gl/GrGLGpu.cpp"
|
2018-06-08 14:49:00 +00:00
|
|
|
#include "glsl/GrGLSLFragmentProcessor.h"
|
2017-07-14 21:17:41 +00:00
|
|
|
#include "ops/GrDrawOp.h"
|
|
|
|
|
2018-02-14 02:46:17 +00:00
|
|
|
using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
|
|
|
|
using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
|
2018-03-27 01:15:22 +00:00
|
|
|
using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2017-09-12 05:04:03 +00:00
|
|
|
static constexpr float kDebugBloat = 40;
|
|
|
|
|
2017-07-14 21:17:41 +00:00
|
|
|
/**
|
|
|
|
* This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
|
|
|
|
* increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
|
|
|
|
* coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
|
|
|
|
* geometry processors.
|
|
|
|
*/
|
2018-08-08 15:36:17 +00:00
|
|
|
class CCPRGeometryView : public Sample {
|
2017-07-14 21:17:41 +00:00
|
|
|
public:
|
|
|
|
CCPRGeometryView() { this->updateGpuData(); }
|
|
|
|
void onDrawContent(SkCanvas*) override;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
|
|
|
|
bool onClick(Sample::Click*) override;
|
|
|
|
bool onQuery(Sample::Event* evt) override;
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
class Click;
|
2018-03-19 20:16:44 +00:00
|
|
|
class DrawCoverageCountOp;
|
|
|
|
class VisualizeCoverageCountFP;
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-01-08 22:21:41 +00:00
|
|
|
void updateAndInval() { this->updateGpuData(); }
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
void updateGpuData();
|
|
|
|
|
2018-03-27 01:15:22 +00:00
|
|
|
PrimitiveType fPrimitiveType = PrimitiveType::kTriangles;
|
2017-12-08 17:59:58 +00:00
|
|
|
SkCubicType fCubicType;
|
2017-09-05 22:46:48 +00:00
|
|
|
SkMatrix fCubicKLM;
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
SkPoint fPoints[4] = {
|
2018-01-08 22:21:41 +00:00
|
|
|
{100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}};
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-04-18 20:07:03 +00:00
|
|
|
float fConicWeight = .5;
|
2018-08-31 11:53:15 +00:00
|
|
|
float fStrokeWidth = 40;
|
|
|
|
bool fDoStroke = false;
|
2018-04-18 20:07:03 +00:00
|
|
|
|
2018-02-14 02:46:17 +00:00
|
|
|
SkTArray<TriPointInstance> fTriPointInstances;
|
|
|
|
SkTArray<QuadPointInstance> fQuadPointInstances;
|
2018-08-31 11:53:15 +00:00
|
|
|
SkPath fPath;
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2017-07-14 21:17:41 +00:00
|
|
|
};
|
|
|
|
|
2018-03-19 20:16:44 +00:00
|
|
|
class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp {
|
2017-07-14 21:17:41 +00:00
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
|
|
|
public:
|
2018-03-19 20:16:44 +00:00
|
|
|
DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) {
|
2018-08-20 20:00:30 +00:00
|
|
|
this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo,
|
|
|
|
GrOp::IsZeroArea::kNo);
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 20:16:44 +00:00
|
|
|
const char* name() const override {
|
|
|
|
return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp";
|
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
2018-07-11 14:02:07 +00:00
|
|
|
RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
|
2017-07-14 21:17:41 +00:00
|
|
|
return RequiresDstTexture::kNo;
|
|
|
|
}
|
|
|
|
void onPrepare(GrOpFlushState*) override {}
|
|
|
|
void onExecute(GrOpFlushState*) override;
|
|
|
|
|
|
|
|
CCPRGeometryView* fView;
|
|
|
|
|
|
|
|
typedef GrDrawOp INHERITED;
|
|
|
|
};
|
|
|
|
|
2018-03-19 20:16:44 +00:00
|
|
|
class CCPRGeometryView::VisualizeCoverageCountFP : public GrFragmentProcessor {
|
|
|
|
public:
|
|
|
|
VisualizeCoverageCountFP() : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* name() const override {
|
|
|
|
return "[Testing/Sample code] CCPRGeometryView::VisualizeCoverageCountFP";
|
|
|
|
}
|
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
return skstd::make_unique<VisualizeCoverageCountFP>();
|
|
|
|
}
|
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
|
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
|
|
|
|
|
|
|
|
class Impl : public GrGLSLFragmentProcessor {
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
|
|
|
|
f->codeAppendf("half count = %s.a;", args.fInputColor);
|
|
|
|
f->codeAppendf("%s = half4(clamp(-count, 0, 1), clamp(+count, 0, 1), 0, abs(count));",
|
|
|
|
args.fOutputColor);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; }
|
|
|
|
};
|
|
|
|
|
2017-09-05 22:46:48 +00:00
|
|
|
static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
|
|
|
|
SkPoint p1, p2;
|
|
|
|
if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
|
|
|
|
// Draw from vertical edge to vertical edge.
|
|
|
|
p1 = {0, -line[2] / line[1]};
|
2018-01-08 22:21:41 +00:00
|
|
|
p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]};
|
2017-09-05 22:46:48 +00:00
|
|
|
} else {
|
|
|
|
// Draw from horizontal edge to horizontal edge.
|
|
|
|
p1 = {-line[2] / line[0], 0};
|
2018-01-08 22:21:41 +00:00
|
|
|
p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h};
|
2017-09-05 22:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint linePaint;
|
|
|
|
linePaint.setColor(color);
|
|
|
|
linePaint.setAlpha(128);
|
|
|
|
linePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
linePaint.setStrokeWidth(0);
|
|
|
|
linePaint.setAntiAlias(true);
|
|
|
|
canvas->drawLine(p1, p2, linePaint);
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:17:41 +00:00
|
|
|
void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
|
2018-03-09 18:57:40 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-08-31 11:53:15 +00:00
|
|
|
if (!fDoStroke) {
|
|
|
|
SkPaint outlinePaint;
|
|
|
|
outlinePaint.setColor(0x80ffffff);
|
|
|
|
outlinePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
outlinePaint.setStrokeWidth(0);
|
|
|
|
outlinePaint.setAntiAlias(true);
|
|
|
|
canvas->drawPath(fPath, outlinePaint);
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 22:46:48 +00:00
|
|
|
#if 0
|
|
|
|
SkPaint gridPaint;
|
|
|
|
gridPaint.setColor(0x10000000);
|
|
|
|
gridPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
gridPaint.setStrokeWidth(0);
|
|
|
|
gridPaint.setAntiAlias(true);
|
2017-09-12 05:04:03 +00:00
|
|
|
for (int y = 0; y < this->height(); y += kDebugBloat) {
|
2017-09-05 22:46:48 +00:00
|
|
|
canvas->drawLine(0, y, this->width(), y, gridPaint);
|
|
|
|
}
|
2017-09-12 05:04:03 +00:00
|
|
|
for (int x = 0; x < this->width(); x += kDebugBloat) {
|
2017-09-05 22:46:48 +00:00
|
|
|
canvas->drawLine(x, 0, x, this->height(), outlinePaint);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-08 17:59:58 +00:00
|
|
|
SkString caption;
|
2018-01-08 22:21:41 +00:00
|
|
|
if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) {
|
2018-03-19 20:16:44 +00:00
|
|
|
// Render coverage count.
|
|
|
|
GrContext* ctx = canvas->getGrContext();
|
2018-08-20 20:00:30 +00:00
|
|
|
SkASSERT(ctx);
|
|
|
|
|
2018-08-14 21:51:23 +00:00
|
|
|
GrOpMemoryPool* pool = ctx->contextPriv().opMemoryPool();
|
|
|
|
|
2018-11-12 16:09:22 +00:00
|
|
|
const GrBackendFormat format =
|
|
|
|
ctx->contextPriv().caps()->getBackendFormatFromGrColorType(GrColorType::kAlpha_F16,
|
|
|
|
GrSRGBEncoded::kNo);
|
2018-03-19 20:16:44 +00:00
|
|
|
sk_sp<GrRenderTargetContext> ccbuff =
|
2018-11-12 16:09:22 +00:00
|
|
|
ctx->contextPriv().makeDeferredRenderTargetContext(format, SkBackingFit::kApprox,
|
2018-03-19 20:16:44 +00:00
|
|
|
this->width(), this->height(),
|
|
|
|
kAlpha_half_GrPixelConfig,
|
|
|
|
nullptr);
|
|
|
|
SkASSERT(ccbuff);
|
2018-11-05 20:06:26 +00:00
|
|
|
ccbuff->clear(nullptr, SK_PMColor4fTRANSPARENT,
|
|
|
|
GrRenderTargetContext::CanClearFullscreen::kYes);
|
2018-08-14 21:51:23 +00:00
|
|
|
ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this));
|
2018-03-19 20:16:44 +00:00
|
|
|
|
|
|
|
// Visualize coverage count in main canvas.
|
|
|
|
GrPaint paint;
|
|
|
|
paint.addColorFragmentProcessor(
|
|
|
|
GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I()));
|
|
|
|
paint.addColorFragmentProcessor(
|
|
|
|
skstd::make_unique<VisualizeCoverageCountFP>());
|
|
|
|
paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
|
|
|
|
rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
|
|
|
|
SkRect::MakeIWH(this->width(), this->height()));
|
|
|
|
|
|
|
|
// Add label.
|
2018-04-06 22:11:48 +00:00
|
|
|
caption.appendf("PrimitiveType_%s",
|
|
|
|
GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType));
|
2018-03-27 01:15:22 +00:00
|
|
|
if (PrimitiveType::kCubics == fPrimitiveType) {
|
2017-12-08 17:59:58 +00:00
|
|
|
caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
|
2018-04-18 20:07:03 +00:00
|
|
|
} else if (PrimitiveType::kConics == fPrimitiveType) {
|
|
|
|
caption.appendf(" (w=%f)", fConicWeight);
|
2017-12-08 17:59:58 +00:00
|
|
|
}
|
2018-08-31 11:53:15 +00:00
|
|
|
if (fDoStroke) {
|
|
|
|
caption.appendf(" (stroke_width=%f)", fStrokeWidth);
|
|
|
|
}
|
2017-12-08 17:59:58 +00:00
|
|
|
} else {
|
|
|
|
caption = "Use GPU backend to visualize geometry.";
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint pointsPaint;
|
|
|
|
pointsPaint.setColor(SK_ColorBLUE);
|
|
|
|
pointsPaint.setStrokeWidth(8);
|
|
|
|
pointsPaint.setAntiAlias(true);
|
|
|
|
|
2018-03-27 01:15:22 +00:00
|
|
|
if (PrimitiveType::kCubics == fPrimitiveType) {
|
2017-07-14 21:17:41 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
|
2018-08-31 11:53:15 +00:00
|
|
|
if (!fDoStroke) {
|
|
|
|
int w = this->width(), h = this->height();
|
|
|
|
draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
|
|
|
|
draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
|
|
|
|
draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
|
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
} else {
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint captionPaint;
|
|
|
|
captionPaint.setTextSize(20);
|
2018-03-09 18:57:40 +00:00
|
|
|
captionPaint.setColor(SK_ColorWHITE);
|
2017-07-14 21:17:41 +00:00
|
|
|
captionPaint.setAntiAlias(true);
|
2017-12-08 17:59:58 +00:00
|
|
|
canvas->drawText(caption.c_str(), caption.size(), 10, 30, captionPaint);
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCPRGeometryView::updateGpuData() {
|
2018-08-20 20:00:30 +00:00
|
|
|
using Verb = GrCCFillGeometry::Verb;
|
2018-02-14 02:46:17 +00:00
|
|
|
fTriPointInstances.reset();
|
|
|
|
fQuadPointInstances.reset();
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-08-31 11:53:15 +00:00
|
|
|
fPath.reset();
|
|
|
|
fPath.moveTo(fPoints[0]);
|
|
|
|
|
2018-03-27 01:15:22 +00:00
|
|
|
if (PrimitiveType::kCubics == fPrimitiveType) {
|
2017-07-14 21:17:41 +00:00
|
|
|
double t[2], s[2];
|
2017-12-08 17:59:58 +00:00
|
|
|
fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
|
2018-08-20 20:00:30 +00:00
|
|
|
GrCCFillGeometry geometry;
|
2017-09-05 22:46:48 +00:00
|
|
|
geometry.beginContour(fPoints[0]);
|
2018-04-10 06:21:19 +00:00
|
|
|
geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2);
|
2017-09-05 22:46:48 +00:00
|
|
|
geometry.endContour();
|
|
|
|
int ptsIdx = 0;
|
2018-08-20 20:00:30 +00:00
|
|
|
for (Verb verb : geometry.verbs()) {
|
2017-09-05 22:46:48 +00:00
|
|
|
switch (verb) {
|
2018-08-20 20:00:30 +00:00
|
|
|
case Verb::kLineTo:
|
2017-09-05 22:46:48 +00:00
|
|
|
++ptsIdx;
|
|
|
|
continue;
|
2018-08-20 20:00:30 +00:00
|
|
|
case Verb::kMonotonicQuadraticTo:
|
2017-09-05 22:46:48 +00:00
|
|
|
ptsIdx += 2;
|
|
|
|
continue;
|
2018-08-20 20:00:30 +00:00
|
|
|
case Verb::kMonotonicCubicTo:
|
2018-02-14 02:46:17 +00:00
|
|
|
fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
|
2017-09-05 22:46:48 +00:00
|
|
|
ptsIdx += 3;
|
|
|
|
continue;
|
2018-01-08 22:21:41 +00:00
|
|
|
default:
|
|
|
|
continue;
|
2017-09-05 22:46:48 +00:00
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
2018-08-31 11:53:15 +00:00
|
|
|
fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
|
2018-04-18 20:07:03 +00:00
|
|
|
} else if (PrimitiveType::kTriangles != fPrimitiveType) {
|
2018-04-10 06:21:19 +00:00
|
|
|
SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]};
|
2018-08-20 20:00:30 +00:00
|
|
|
GrCCFillGeometry geometry;
|
2018-04-10 06:21:19 +00:00
|
|
|
geometry.beginContour(P3[0]);
|
2018-04-18 20:07:03 +00:00
|
|
|
if (PrimitiveType::kQuadratics == fPrimitiveType) {
|
|
|
|
geometry.quadraticTo(P3);
|
2018-08-31 11:53:15 +00:00
|
|
|
fPath.quadTo(fPoints[1], fPoints[3]);
|
2018-04-18 20:07:03 +00:00
|
|
|
} else {
|
|
|
|
SkASSERT(PrimitiveType::kConics == fPrimitiveType);
|
|
|
|
geometry.conicTo(P3, fConicWeight);
|
2018-08-31 11:53:15 +00:00
|
|
|
fPath.conicTo(fPoints[1], fPoints[3], fConicWeight);
|
2018-04-18 20:07:03 +00:00
|
|
|
}
|
2017-09-05 06:30:07 +00:00
|
|
|
geometry.endContour();
|
2018-04-18 20:07:03 +00:00
|
|
|
int ptsIdx = 0, conicWeightIdx = 0;
|
2018-08-20 20:00:30 +00:00
|
|
|
for (Verb verb : geometry.verbs()) {
|
|
|
|
if (Verb::kBeginContour == verb ||
|
|
|
|
Verb::kEndOpenContour == verb ||
|
|
|
|
Verb::kEndClosedContour == verb) {
|
2017-09-05 06:30:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-20 20:00:30 +00:00
|
|
|
if (Verb::kLineTo == verb) {
|
2017-12-22 09:24:53 +00:00
|
|
|
++ptsIdx;
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-20 20:00:30 +00:00
|
|
|
SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb);
|
2018-04-18 20:07:03 +00:00
|
|
|
if (PrimitiveType::kQuadratics == fPrimitiveType &&
|
2018-08-20 20:00:30 +00:00
|
|
|
Verb::kMonotonicQuadraticTo == verb) {
|
2018-04-18 20:07:03 +00:00
|
|
|
fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0));
|
|
|
|
} else if (PrimitiveType::kConics == fPrimitiveType &&
|
2018-08-20 20:00:30 +00:00
|
|
|
Verb::kMonotonicConicTo == verb) {
|
2018-04-18 20:07:03 +00:00
|
|
|
fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0),
|
|
|
|
geometry.getConicWeight(conicWeightIdx++));
|
|
|
|
}
|
2017-12-04 18:45:51 +00:00
|
|
|
ptsIdx += 2;
|
2017-08-07 15:00:46 +00:00
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
} else {
|
2018-02-14 02:46:17 +00:00
|
|
|
fTriPointInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0));
|
2018-08-31 11:53:15 +00:00
|
|
|
fPath.lineTo(fPoints[1]);
|
|
|
|
fPath.lineTo(fPoints[3]);
|
|
|
|
fPath.close();
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 20:16:44 +00:00
|
|
|
void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state) {
|
2017-07-14 21:17:41 +00:00
|
|
|
GrResourceProvider* rp = state->resourceProvider();
|
|
|
|
GrContext* context = state->gpu()->getContext();
|
2018-10-12 13:31:11 +00:00
|
|
|
GrGLGpu* glGpu = GrBackendApi::kOpenGL == context->contextPriv().getBackend()
|
2018-01-08 22:21:41 +00:00
|
|
|
? static_cast<GrGLGpu*>(state->gpu())
|
|
|
|
: nullptr;
|
2017-07-14 21:17:41 +00:00
|
|
|
if (glGpu) {
|
|
|
|
glGpu->handleDirtyContext();
|
2018-03-09 18:57:40 +00:00
|
|
|
// GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
|
2017-07-14 21:17:41 +00:00
|
|
|
GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
|
|
|
|
}
|
|
|
|
|
2018-08-31 11:53:15 +00:00
|
|
|
GrPipeline pipeline(state->drawOpArgs().fProxy, GrScissorTest::kDisabled, SkBlendMode::kPlus);
|
|
|
|
|
|
|
|
if (!fView->fDoStroke) {
|
|
|
|
GrCCCoverageProcessor proc(rp, fView->fPrimitiveType);
|
|
|
|
SkDEBUGCODE(proc.enableDebugBloat(kDebugBloat));
|
|
|
|
|
|
|
|
SkSTArray<1, GrMesh> mesh;
|
|
|
|
if (PrimitiveType::kCubics == fView->fPrimitiveType ||
|
|
|
|
PrimitiveType::kConics == fView->fPrimitiveType) {
|
|
|
|
sk_sp<GrBuffer> instBuff(rp->createBuffer(
|
|
|
|
fView->fQuadPointInstances.count() * sizeof(QuadPointInstance),
|
|
|
|
kVertex_GrBufferType, kDynamic_GrAccessPattern,
|
2018-09-27 15:28:03 +00:00
|
|
|
GrResourceProvider::Flags::kNoPendingIO |
|
|
|
|
GrResourceProvider::Flags::kRequireGpuMemory,
|
2018-08-31 11:53:15 +00:00
|
|
|
fView->fQuadPointInstances.begin()));
|
|
|
|
if (!fView->fQuadPointInstances.empty() && instBuff) {
|
|
|
|
proc.appendMesh(instBuff.get(), fView->fQuadPointInstances.count(), 0, &mesh);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sk_sp<GrBuffer> instBuff(rp->createBuffer(
|
|
|
|
fView->fTriPointInstances.count() * sizeof(TriPointInstance),
|
|
|
|
kVertex_GrBufferType, kDynamic_GrAccessPattern,
|
2018-09-27 15:28:03 +00:00
|
|
|
GrResourceProvider::Flags::kNoPendingIO |
|
|
|
|
GrResourceProvider::Flags::kRequireGpuMemory, fView->fTriPointInstances.begin()));
|
2018-08-31 11:53:15 +00:00
|
|
|
if (!fView->fTriPointInstances.empty() && instBuff) {
|
|
|
|
proc.appendMesh(instBuff.get(), fView->fTriPointInstances.count(), 0, &mesh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mesh.empty()) {
|
|
|
|
SkASSERT(1 == mesh.count());
|
|
|
|
proc.draw(state, pipeline, nullptr, mesh.begin(), 1, this->bounds());
|
|
|
|
}
|
|
|
|
} else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet.
|
|
|
|
GrCCStroker stroker(0,0,0);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(fView->fStrokeWidth);
|
|
|
|
p.setStrokeJoin(SkPaint::kMiter_Join);
|
|
|
|
p.setStrokeMiter(4);
|
|
|
|
// p.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath),
|
|
|
|
SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled,
|
|
|
|
SkIRect::MakeWH(fView->width(), fView->height()), {0, 0});
|
|
|
|
GrCCStroker::BatchID batchID = stroker.closeCurrentBatch();
|
|
|
|
|
|
|
|
GrOnFlushResourceProvider onFlushRP(context->contextPriv().drawingManager());
|
|
|
|
stroker.prepareToDraw(&onFlushRP);
|
|
|
|
|
|
|
|
SkIRect ibounds;
|
|
|
|
this->bounds().roundOut(&ibounds);
|
|
|
|
stroker.drawStrokes(state, batchID, ibounds);
|
2017-12-10 23:41:45 +00:00
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
if (glGpu) {
|
|
|
|
context->resetContext(kMisc_GrGLBackendState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CCPRGeometryView::Click : public Sample::Click {
|
2017-07-14 21:17:41 +00:00
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
Click(Sample* target, int ptIdx) : Sample::Click(target), fPtIdx(ptIdx) {}
|
2017-07-14 21:17:41 +00:00
|
|
|
|
|
|
|
void doClick(SkPoint points[]) {
|
|
|
|
if (fPtIdx >= 0) {
|
|
|
|
this->dragPoint(points, fPtIdx);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
this->dragPoint(points, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-08 22:21:41 +00:00
|
|
|
void dragPoint(SkPoint points[], int idx) {
|
2017-07-14 21:17:41 +00:00
|
|
|
SkIPoint delta = fICurr - fIPrev;
|
|
|
|
points[idx] += SkPoint::Make(delta.x(), delta.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
int fPtIdx;
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
|
2017-07-14 21:17:41 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2018-03-27 01:15:22 +00:00
|
|
|
if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) {
|
2017-07-14 21:17:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
|
|
|
|
return new Click(this, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Click(this, -1);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
bool CCPRGeometryView::onClick(Sample::Click* click) {
|
2018-01-08 22:21:41 +00:00
|
|
|
Click* myClick = (Click*)click;
|
2017-07-14 21:17:41 +00:00
|
|
|
myClick->doClick(fPoints);
|
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
bool CCPRGeometryView::onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "CCPRGeometry");
|
2017-07-14 21:17:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar unichar;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &unichar)) {
|
2018-04-18 20:07:03 +00:00
|
|
|
if (unichar >= '1' && unichar <= '4') {
|
2018-03-27 01:15:22 +00:00
|
|
|
fPrimitiveType = PrimitiveType(unichar - '1');
|
2018-04-06 22:11:48 +00:00
|
|
|
if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) {
|
|
|
|
fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1);
|
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-31 11:53:15 +00:00
|
|
|
float* valueToScale = nullptr;
|
|
|
|
if (fDoStroke) {
|
|
|
|
valueToScale = &fStrokeWidth;
|
|
|
|
} else if (PrimitiveType::kConics == fPrimitiveType) {
|
|
|
|
valueToScale = &fConicWeight;
|
|
|
|
}
|
|
|
|
if (valueToScale) {
|
2018-04-18 20:07:03 +00:00
|
|
|
if (unichar == '+') {
|
2018-08-31 11:53:15 +00:00
|
|
|
*valueToScale *= 2;
|
2018-04-18 20:07:03 +00:00
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (unichar == '+' || unichar == '=') {
|
2018-08-31 11:53:15 +00:00
|
|
|
*valueToScale *= 5/4.f;
|
2018-04-18 20:07:03 +00:00
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (unichar == '-') {
|
2018-08-31 11:53:15 +00:00
|
|
|
*valueToScale *= 4/5.f;
|
2018-04-18 20:07:03 +00:00
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (unichar == '_') {
|
2018-08-31 11:53:15 +00:00
|
|
|
*valueToScale *= .5f;
|
2018-04-18 20:07:03 +00:00
|
|
|
this->updateAndInval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
if (unichar == 'D') {
|
|
|
|
SkDebugf(" SkPoint fPoints[4] = {\n");
|
2017-09-05 22:46:48 +00:00
|
|
|
SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
|
|
|
|
SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
|
|
|
|
SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
|
|
|
|
SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
|
2017-07-14 21:17:41 +00:00
|
|
|
SkDebugf(" };\n");
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-31 11:53:15 +00:00
|
|
|
if (unichar == 'S') {
|
|
|
|
fDoStroke = !fDoStroke;
|
|
|
|
this->updateAndInval();
|
|
|
|
}
|
2017-07-14 21:17:41 +00:00
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2018-01-08 22:21:41 +00:00
|
|
|
DEF_SAMPLE(return new CCPRGeometryView;)
|
2017-07-14 21:17:41 +00:00
|
|
|
|
2018-01-08 22:21:41 +00:00
|
|
|
#endif // SK_SUPPORT_GPU
|