2013-08-21 19:27:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This test only works with the GPU backend.
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
#include "GrBatchFlushState.h"
|
2013-08-21 19:27:48 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrPathUtils.h"
|
|
|
|
#include "GrTest.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkDevice.h"
|
2013-08-23 18:05:01 +00:00
|
|
|
#include "SkGeometry.h"
|
|
|
|
|
2015-08-07 19:46:26 +00:00
|
|
|
#include "batches/GrTestBatch.h"
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
#include "effects/GrBezierEffect.h"
|
2013-08-21 19:27:48 +00:00
|
|
|
|
|
|
|
static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkScalar sign) {
|
|
|
|
return sign * (lineEq[0] * p.fX + lineEq[1] * p.fY + lineEq[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
2015-02-11 21:45:50 +00:00
|
|
|
|
|
|
|
class BezierCubicOrConicTestBatch : public GrTestBatch {
|
|
|
|
public:
|
2015-09-18 03:16:13 +00:00
|
|
|
DEFINE_BATCH_CLASS_ID
|
2015-02-11 21:45:50 +00:00
|
|
|
struct Geometry : public GrTestBatch::Geometry {
|
|
|
|
SkRect fBounds;
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* name() const override { return "BezierCubicOrConicTestBatch"; }
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2015-08-13 20:34:48 +00:00
|
|
|
static GrDrawBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
|
|
|
|
const SkScalar klmEqs[9], SkScalar sign) {
|
2015-08-26 20:07:48 +00:00
|
|
|
return new BezierCubicOrConicTestBatch(gp, geo, klmEqs, sign);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BezierCubicOrConicTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
|
|
|
|
const SkScalar klmEqs[9], SkScalar sign)
|
2015-09-18 03:16:13 +00:00
|
|
|
: INHERITED(ClassID(), gp, geo.fBounds) {
|
2015-02-11 21:45:50 +00:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
fKlmEqs[i] = klmEqs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
fGeometry = geo;
|
|
|
|
fSign = sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Geometry* geoData(int index) override {
|
2015-02-11 21:45:50 +00:00
|
|
|
SkASSERT(0 == index);
|
|
|
|
return &fGeometry;
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:18:07 +00:00
|
|
|
const Geometry* geoData(int index) const override {
|
|
|
|
SkASSERT(0 == index);
|
|
|
|
return &fGeometry;
|
|
|
|
}
|
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
void generateGeometry(Target* target) override {
|
2015-05-05 14:49:49 +00:00
|
|
|
QuadHelper helper;
|
2015-05-04 17:36:42 +00:00
|
|
|
size_t vertexStride = this->geometryProcessor()->getVertexStride();
|
2015-05-05 14:49:49 +00:00
|
|
|
SkASSERT(vertexStride == sizeof(Vertex));
|
2015-08-17 19:55:38 +00:00
|
|
|
Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
|
2015-05-05 14:49:49 +00:00
|
|
|
if (!verts) {
|
2015-03-05 22:33:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
|
|
|
|
fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
|
|
|
|
sizeof(Vertex));
|
|
|
|
for (int v = 0; v < 4; ++v) {
|
|
|
|
verts[v].fKLM[0] = eval_line(verts[v].fPosition, fKlmEqs + 0, fSign);
|
|
|
|
verts[v].fKLM[1] = eval_line(verts[v].fPosition, fKlmEqs + 3, fSign);
|
|
|
|
verts[v].fKLM[2] = eval_line(verts[v].fPosition, fKlmEqs + 6, 1.f);
|
|
|
|
}
|
2015-08-17 19:55:38 +00:00
|
|
|
helper.recordDraw(target);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Geometry fGeometry;
|
|
|
|
SkScalar fKlmEqs[9];
|
|
|
|
SkScalar fSign;
|
|
|
|
|
|
|
|
static const int kVertsPerCubic = 4;
|
|
|
|
static const int kIndicesPerCubic = 6;
|
|
|
|
|
|
|
|
typedef GrTestBatch INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-08-21 19:27:48 +00:00
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier curves in the GPU backend.
|
|
|
|
*/
|
2013-08-23 18:05:01 +00:00
|
|
|
class BezierCubicEffects : public GM {
|
2013-08-21 19:27:48 +00:00
|
|
|
public:
|
2013-08-23 18:05:01 +00:00
|
|
|
BezierCubicEffects() {
|
2013-08-21 19:27:48 +00:00
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-23 18:05:01 +00:00
|
|
|
return SkString("bezier_cubic_effects");
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(800, 800);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-03-12 18:28:35 +00:00
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == rt) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2013-08-21 19:27:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2013-08-21 19:27:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
static const int kNumCubics = 15;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
// Mult by 3 for each edge effect type
|
|
|
|
int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
|
|
|
|
int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
|
2013-08-21 19:27:48 +00:00
|
|
|
SkScalar w = SkIntToScalar(rt->width()) / numCols;
|
|
|
|
SkScalar h = SkIntToScalar(rt->height()) / numRows;
|
|
|
|
int row = 0;
|
|
|
|
int col = 0;
|
2015-05-13 21:18:07 +00:00
|
|
|
static const GrColor color = 0xff000000;
|
2013-08-21 19:27:48 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumCubics; ++i) {
|
2013-08-23 18:05:01 +00:00
|
|
|
SkPoint baseControlPts[] = {
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
|
2013-08-21 19:27:48 +00:00
|
|
|
};
|
2014-09-23 16:50:21 +00:00
|
|
|
for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
|
|
|
|
SkAutoTUnref<GrGeometryProcessor> gp;
|
2014-03-05 18:27:43 +00:00
|
|
|
{ // scope to contain GrTestTarget
|
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tt.target()) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
|
2015-05-13 21:18:07 +00:00
|
|
|
gp.reset(GrCubicEffect::Create(color, SkMatrix::I(), et,
|
2014-12-29 23:10:07 +00:00
|
|
|
*tt.target()->caps()));
|
2014-09-23 16:50:21 +00:00
|
|
|
if (!gp) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
SkScalar x = SkScalarMul(col, w);
|
|
|
|
SkScalar y = SkScalarMul(row, h);
|
|
|
|
SkPoint controlPts[] = {
|
|
|
|
{x + baseControlPts[0].fX, y + baseControlPts[0].fY},
|
|
|
|
{x + baseControlPts[1].fX, y + baseControlPts[1].fY},
|
|
|
|
{x + baseControlPts[2].fX, y + baseControlPts[2].fY},
|
|
|
|
{x + baseControlPts[3].fX, y + baseControlPts[3].fY}
|
|
|
|
};
|
|
|
|
SkPoint chopped[10];
|
|
|
|
SkScalar klmEqs[9];
|
|
|
|
SkScalar klmSigns[3];
|
|
|
|
int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
|
|
|
|
chopped,
|
|
|
|
klmEqs,
|
|
|
|
klmSigns);
|
|
|
|
|
|
|
|
SkPaint ctrlPtPaint;
|
|
|
|
ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint polyPaint;
|
|
|
|
polyPaint.setColor(0xffA0A0A0);
|
|
|
|
polyPaint.setStrokeWidth(0);
|
|
|
|
polyPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
|
|
|
|
|
|
|
|
SkPaint choppedPtPaint;
|
|
|
|
choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
|
|
|
|
|
|
|
|
for (int c = 0; c < cnt; ++c) {
|
|
|
|
SkPoint* pts = chopped + 3 * c;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
bounds.set(pts, 4);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setColor(0xff808080);
|
|
|
|
boundsPaint.setStrokeWidth(0);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
|
|
|
|
2014-11-03 20:31:14 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
SkASSERT(tt.target());
|
|
|
|
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
2015-02-11 21:45:50 +00:00
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2014-11-03 20:31:14 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
BezierCubicOrConicTestBatch::Geometry geometry;
|
2015-05-13 21:18:07 +00:00
|
|
|
geometry.fColor = color;
|
2015-02-11 21:45:50 +00:00
|
|
|
geometry.fBounds = bounds;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-08-13 20:34:48 +00:00
|
|
|
SkAutoTUnref<GrDrawBatch> batch(
|
2015-02-23 22:44:57 +00:00
|
|
|
BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, klmSigns[c]));
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-07-13 15:08:25 +00:00
|
|
|
tt.target()->drawBatch(pipelineBuilder, batch);
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
++col;
|
|
|
|
if (numCols == col) {
|
|
|
|
col = 0;
|
|
|
|
++row;
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier curves in the GPU backend.
|
|
|
|
*/
|
|
|
|
class BezierConicEffects : public GM {
|
|
|
|
public:
|
|
|
|
BezierConicEffects() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-23 18:05:01 +00:00
|
|
|
return SkString("bezier_conic_effects");
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(800, 800);
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-03-12 18:28:35 +00:00
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == rt) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2013-08-23 18:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2013-08-23 18:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
static const int kNumConics = 10;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
|
|
|
// Mult by 3 for each edge effect type
|
|
|
|
int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
|
|
|
|
int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
|
|
|
|
SkScalar w = SkIntToScalar(rt->width()) / numCols;
|
|
|
|
SkScalar h = SkIntToScalar(rt->height()) / numRows;
|
|
|
|
int row = 0;
|
|
|
|
int col = 0;
|
2015-05-13 21:18:07 +00:00
|
|
|
static const GrColor color = 0xff000000;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumConics; ++i) {
|
|
|
|
SkPoint baseControlPts[] = {
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
|
|
|
|
};
|
|
|
|
SkScalar weight = rand.nextRangeF(0.f, 2.f);
|
2014-09-23 16:50:21 +00:00
|
|
|
for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
|
|
|
|
SkAutoTUnref<GrGeometryProcessor> gp;
|
2014-03-05 18:27:43 +00:00
|
|
|
{ // scope to contain GrTestTarget
|
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tt.target()) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
|
2015-05-13 21:18:07 +00:00
|
|
|
gp.reset(GrConicEffect::Create(color, SkMatrix::I(), et,
|
2015-05-19 15:23:30 +00:00
|
|
|
*tt.target()->caps(), SkMatrix::I(), false));
|
2014-09-23 16:50:21 +00:00
|
|
|
if (!gp) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
SkScalar x = SkScalarMul(col, w);
|
|
|
|
SkScalar y = SkScalarMul(row, h);
|
|
|
|
SkPoint controlPts[] = {
|
|
|
|
{x + baseControlPts[0].fX, y + baseControlPts[0].fY},
|
|
|
|
{x + baseControlPts[1].fX, y + baseControlPts[1].fY},
|
|
|
|
{x + baseControlPts[2].fX, y + baseControlPts[2].fY}
|
|
|
|
};
|
|
|
|
SkConic dst[4];
|
|
|
|
SkScalar klmEqs[9];
|
|
|
|
int cnt = chop_conic(controlPts, dst, weight);
|
|
|
|
GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
|
|
|
|
|
|
|
|
SkPaint ctrlPtPaint;
|
|
|
|
ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
SkPaint polyPaint;
|
|
|
|
polyPaint.setColor(0xffA0A0A0);
|
|
|
|
polyPaint.setStrokeWidth(0);
|
|
|
|
polyPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
|
|
|
|
|
|
|
|
SkPaint choppedPtPaint;
|
|
|
|
choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
|
|
|
|
|
|
|
|
for (int c = 0; c < cnt; ++c) {
|
|
|
|
SkPoint* pts = dst[c].fPts;
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
//SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
|
|
|
|
//bounds.set(bPts, 2);
|
|
|
|
bounds.set(pts, 3);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setColor(0xff808080);
|
|
|
|
boundsPaint.setStrokeWidth(0);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
|
|
|
|
2014-11-03 20:31:14 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
SkASSERT(tt.target());
|
|
|
|
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
2015-02-11 21:45:50 +00:00
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2014-11-03 20:31:14 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
BezierCubicOrConicTestBatch::Geometry geometry;
|
2015-05-13 21:18:07 +00:00
|
|
|
geometry.fColor = color;
|
2015-02-11 21:45:50 +00:00
|
|
|
geometry.fBounds = bounds;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-08-13 20:34:48 +00:00
|
|
|
SkAutoTUnref<GrDrawBatch> batch(
|
2015-02-23 22:44:57 +00:00
|
|
|
BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, 1.f));
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-07-13 15:08:25 +00:00
|
|
|
tt.target()->drawBatch(pipelineBuilder, batch);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
++col;
|
|
|
|
if (numCols == col) {
|
|
|
|
col = 0;
|
|
|
|
++row;
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
private:
|
|
|
|
// Uses the max curvature function for quads to estimate
|
|
|
|
// where to chop the conic. If the max curvature is not
|
|
|
|
// found along the curve segment it will return 1 and
|
|
|
|
// dst[0] is the original conic. If it returns 2 the dst[0]
|
|
|
|
// and dst[1] are the two new conics.
|
|
|
|
int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
|
|
|
|
SkScalar t = SkFindQuadMaxCurvature(src);
|
|
|
|
if (t == 0) {
|
|
|
|
if (dst) {
|
|
|
|
dst[0].set(src, weight);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (dst) {
|
|
|
|
SkConic conic;
|
|
|
|
conic.set(src, weight);
|
|
|
|
conic.chopAt(t, dst);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
return 2;
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
// Calls split_conic on the entire conic and then once more on each subsection.
|
|
|
|
// Most cases will result in either 1 conic (chop point is not within t range)
|
|
|
|
// or 3 points (split once and then one subsection is split again).
|
|
|
|
int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
|
|
|
|
SkConic dstTemp[2];
|
|
|
|
int conicCnt = split_conic(src, dstTemp, weight);
|
|
|
|
if (2 == conicCnt) {
|
|
|
|
int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
|
|
|
|
conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
|
|
|
|
} else {
|
|
|
|
dst[0] = dstTemp[0];
|
|
|
|
}
|
|
|
|
return conicCnt;
|
|
|
|
}
|
|
|
|
|
2013-08-21 19:27:48 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-02-11 21:45:50 +00:00
|
|
|
|
|
|
|
class BezierQuadTestBatch : public GrTestBatch {
|
|
|
|
public:
|
2015-09-18 03:16:13 +00:00
|
|
|
DEFINE_BATCH_CLASS_ID
|
2015-02-11 21:45:50 +00:00
|
|
|
struct Geometry : public GrTestBatch::Geometry {
|
|
|
|
SkRect fBounds;
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* name() const override { return "BezierQuadTestBatch"; }
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2015-08-13 20:34:48 +00:00
|
|
|
static GrDrawBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
|
|
|
|
const GrPathUtils::QuadUVMatrix& devToUV) {
|
2015-08-26 20:07:48 +00:00
|
|
|
return new BezierQuadTestBatch(gp, geo, devToUV);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BezierQuadTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
|
|
|
|
const GrPathUtils::QuadUVMatrix& devToUV)
|
2015-09-18 03:16:13 +00:00
|
|
|
: INHERITED(ClassID(), gp, geo.fBounds)
|
2015-02-11 21:45:50 +00:00
|
|
|
, fGeometry(geo)
|
|
|
|
, fDevToUV(devToUV) {
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Geometry* geoData(int index) override {
|
2015-02-11 21:45:50 +00:00
|
|
|
SkASSERT(0 == index);
|
|
|
|
return &fGeometry;
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:18:07 +00:00
|
|
|
const Geometry* geoData(int index) const override {
|
|
|
|
SkASSERT(0 == index);
|
|
|
|
return &fGeometry;
|
|
|
|
}
|
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
void generateGeometry(Target* target) override {
|
2015-05-05 14:49:49 +00:00
|
|
|
QuadHelper helper;
|
2015-05-04 17:36:42 +00:00
|
|
|
size_t vertexStride = this->geometryProcessor()->getVertexStride();
|
2015-05-05 14:49:49 +00:00
|
|
|
SkASSERT(vertexStride == sizeof(Vertex));
|
2015-08-17 19:55:38 +00:00
|
|
|
Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
|
2015-05-05 14:49:49 +00:00
|
|
|
if (!verts) {
|
2015-03-05 22:33:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-11 21:45:50 +00:00
|
|
|
verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
|
|
|
|
fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
|
|
|
|
sizeof(Vertex));
|
|
|
|
fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
|
2015-08-17 19:55:38 +00:00
|
|
|
helper.recordDraw(target);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Geometry fGeometry;
|
|
|
|
GrPathUtils::QuadUVMatrix fDevToUV;
|
|
|
|
|
|
|
|
static const int kVertsPerCubic = 4;
|
|
|
|
static const int kIndicesPerCubic = 6;
|
|
|
|
|
|
|
|
typedef GrTestBatch INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
|
|
|
|
*/
|
|
|
|
class BezierQuadEffects : public GM {
|
|
|
|
public:
|
|
|
|
BezierQuadEffects() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-23 18:05:01 +00:00
|
|
|
return SkString("bezier_quad_effects");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(800, 800);
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-03-12 18:28:35 +00:00
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == rt) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2013-08-23 18:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2013-08-23 18:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int kNumQuads = 5;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
|
|
|
int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
|
|
|
|
int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
|
|
|
|
SkScalar w = SkIntToScalar(rt->width()) / numCols;
|
|
|
|
SkScalar h = SkIntToScalar(rt->height()) / numRows;
|
|
|
|
int row = 0;
|
|
|
|
int col = 0;
|
2015-05-13 21:18:07 +00:00
|
|
|
static const GrColor color = 0xff000000;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumQuads; ++i) {
|
|
|
|
SkPoint baseControlPts[] = {
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
|
|
|
|
{rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
|
|
|
|
};
|
2014-09-23 16:50:21 +00:00
|
|
|
for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
|
|
|
|
SkAutoTUnref<GrGeometryProcessor> gp;
|
2014-03-05 18:27:43 +00:00
|
|
|
{ // scope to contain GrTestTarget
|
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tt.target()) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
|
2015-05-13 21:18:07 +00:00
|
|
|
gp.reset(GrQuadEffect::Create(color, SkMatrix::I(), et,
|
2015-05-19 15:23:30 +00:00
|
|
|
*tt.target()->caps(), SkMatrix::I(), false));
|
2014-09-23 16:50:21 +00:00
|
|
|
if (!gp) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
SkScalar x = SkScalarMul(col, w);
|
|
|
|
SkScalar y = SkScalarMul(row, h);
|
|
|
|
SkPoint controlPts[] = {
|
|
|
|
{x + baseControlPts[0].fX, y + baseControlPts[0].fY},
|
|
|
|
{x + baseControlPts[1].fX, y + baseControlPts[1].fY},
|
|
|
|
{x + baseControlPts[2].fX, y + baseControlPts[2].fY}
|
|
|
|
};
|
|
|
|
SkPoint chopped[5];
|
|
|
|
int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
|
|
|
|
|
|
|
|
SkPaint ctrlPtPaint;
|
|
|
|
ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint polyPaint;
|
|
|
|
polyPaint.setColor(0xffA0A0A0);
|
|
|
|
polyPaint.setStrokeWidth(0);
|
|
|
|
polyPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
|
|
|
|
|
|
|
|
SkPaint choppedPtPaint;
|
|
|
|
choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
|
|
|
|
|
|
|
|
for (int c = 0; c < cnt; ++c) {
|
|
|
|
SkPoint* pts = chopped + 2 * c;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
bounds.set(pts, 3);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setColor(0xff808080);
|
|
|
|
boundsPaint.setStrokeWidth(0);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
|
|
|
|
2014-11-03 20:31:14 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
SkASSERT(tt.target());
|
|
|
|
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
2015-02-11 21:45:50 +00:00
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2015-02-11 19:34:58 +00:00
|
|
|
|
2015-02-11 21:03:15 +00:00
|
|
|
GrPathUtils::QuadUVMatrix DevToUV(pts);
|
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
BezierQuadTestBatch::Geometry geometry;
|
2015-05-13 21:18:07 +00:00
|
|
|
geometry.fColor = color;
|
2015-02-11 21:45:50 +00:00
|
|
|
geometry.fBounds = bounds;
|
|
|
|
|
2015-08-13 20:34:48 +00:00
|
|
|
SkAutoTUnref<GrDrawBatch> batch(BezierQuadTestBatch::Create(gp, geometry,
|
|
|
|
DevToUV));
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2015-07-13 15:08:25 +00:00
|
|
|
tt.target()->drawBatch(pipelineBuilder, batch);
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
++col;
|
|
|
|
if (numCols == col) {
|
|
|
|
col = 0;
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new BezierCubicEffects;)
|
|
|
|
DEF_GM(return new BezierConicEffects;)
|
|
|
|
DEF_GM(return new BezierQuadEffects;)
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|