2014-01-30 18:15:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2014 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"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
#include "GrBatchTarget.h"
|
2014-01-30 18:15:51 +00:00
|
|
|
#include "GrContext.h"
|
2014-11-15 00:00:38 +00:00
|
|
|
#include "GrDefaultGeoProcFactory.h"
|
2014-01-30 18:15:51 +00:00
|
|
|
#include "GrPathUtils.h"
|
|
|
|
#include "GrTest.h"
|
2015-02-11 21:45:50 +00:00
|
|
|
#include "GrTestBatch.h"
|
2014-01-30 18:15:51 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkGeometry.h"
|
|
|
|
#include "SkTLList.h"
|
|
|
|
|
|
|
|
#include "effects/GrConvexPolyEffect.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
2015-02-11 21:45:50 +00:00
|
|
|
|
|
|
|
class ConvexPolyTestBatch : public GrTestBatch {
|
|
|
|
public:
|
|
|
|
struct Geometry : public GrTestBatch::Geometry {
|
|
|
|
SkRect fBounds;
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* name() const override { return "ConvexPolyTestBatch"; }
|
2015-02-11 21:45:50 +00:00
|
|
|
|
|
|
|
static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo) {
|
|
|
|
return SkNEW_ARGS(ConvexPolyTestBatch, (gp, geo));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConvexPolyTestBatch(const GrGeometryProcessor* gp, const Geometry& geo)
|
2015-05-01 20:43:30 +00:00
|
|
|
: INHERITED(gp, geo.fBounds)
|
2015-02-11 21:45:50 +00:00
|
|
|
, fGeometry(geo) {
|
|
|
|
}
|
|
|
|
|
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-03-26 01:17:31 +00:00
|
|
|
void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
|
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(SkPoint));
|
|
|
|
QuadHelper helper;
|
|
|
|
SkPoint* verts = reinterpret_cast<SkPoint*>(helper.init(batchTarget, vertexStride, 1));
|
|
|
|
if (!verts) {
|
2015-03-05 22:33:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
// Make sure any artifacts around the exterior of path are visible by using overly
|
|
|
|
// conservative bounding geometry.
|
|
|
|
fGeometry.fBounds.outset(5.f, 5.f);
|
|
|
|
fGeometry.fBounds.toQuad(verts);
|
|
|
|
|
2015-05-07 18:35:55 +00:00
|
|
|
helper.issueDraw(batchTarget);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Geometry fGeometry;
|
|
|
|
|
|
|
|
typedef GrTestBatch INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* This GM directly exercises a GrProcessor that draws convex polygons.
|
2014-01-30 18:15:51 +00:00
|
|
|
*/
|
|
|
|
class ConvexPolyEffect : public GM {
|
|
|
|
public:
|
|
|
|
ConvexPolyEffect() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-01-30 18:15:51 +00:00
|
|
|
return SkString("convex_poly_effect");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(720, 800);
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath tri;
|
|
|
|
tri.moveTo(5.f, 5.f);
|
|
|
|
tri.lineTo(100.f, 20.f);
|
|
|
|
tri.lineTo(15.f, 100.f);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
fPaths.addToTail(tri);
|
|
|
|
fPaths.addToTail(SkPath())->reverseAddPath(tri);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
tri.close();
|
|
|
|
fPaths.addToTail(tri);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath ngon;
|
|
|
|
static const SkScalar kRadius = 50.f;
|
|
|
|
const SkPoint center = { kRadius, kRadius };
|
|
|
|
for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
|
|
|
|
SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
|
|
|
|
SkPoint point;
|
|
|
|
point.fY = SkScalarSinCos(angle, &point.fX);
|
|
|
|
point.scale(kRadius);
|
|
|
|
point = center + point;
|
|
|
|
if (0 == i) {
|
|
|
|
ngon.moveTo(point);
|
|
|
|
} else {
|
|
|
|
ngon.lineTo(point);
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
fPaths.addToTail(ngon);
|
|
|
|
SkMatrix scaleM;
|
|
|
|
scaleM.setScale(1.1f, 0.4f);
|
|
|
|
ngon.transform(scaleM);
|
|
|
|
fPaths.addToTail(ngon);
|
2014-02-09 03:02:01 +00:00
|
|
|
|
2014-02-08 19:31:05 +00:00
|
|
|
// integer edges
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
|
|
|
|
// half-integer edges
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
|
|
|
|
// vertically/horizontally thin rects that cover pixel centers
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
|
|
|
|
// vertically/horizontally thin rects that don't cover pixel centers
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
|
|
|
|
// small in x and y
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
|
|
|
|
// inverted in x and y
|
|
|
|
fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
|
2014-01-30 18:15:51 +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();
|
2014-01-30 18:15:51 +00:00
|
|
|
if (NULL == rt) {
|
2015-01-31 15:51:14 +00:00
|
|
|
this->drawGpuOnlyMessage(canvas);
|
2014-01-30 18:15:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:18:07 +00:00
|
|
|
static const GrColor color = 0xff000000;
|
2015-02-11 21:45:50 +00:00
|
|
|
SkAutoTUnref<const GrGeometryProcessor> gp(
|
2015-05-13 21:18:07 +00:00
|
|
|
GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, color));
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
SkScalar y = 0;
|
|
|
|
for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_IterStart);
|
2014-09-05 20:34:00 +00:00
|
|
|
iter.get();
|
2014-01-30 18:15:51 +00:00
|
|
|
iter.next()) {
|
2014-02-08 19:31:05 +00:00
|
|
|
const SkPath* path = iter.get();
|
2014-01-30 18:15:51 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
|
2014-01-30 18:15:51 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
if (NULL == tt.target()) {
|
|
|
|
SkDEBUGFAIL("Couldn't get Gr test target.");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-27 02:57:08 +00:00
|
|
|
const SkMatrix m = SkMatrix::MakeTrans(x, y);
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath p;
|
|
|
|
path->transform(m, &p);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
|
|
|
|
SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, p));
|
|
|
|
if (!fp) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
|
|
|
pipelineBuilder.addCoverageProcessor(fp);
|
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2014-11-17 22:22:48 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
ConvexPolyTestBatch::Geometry geometry;
|
2015-05-13 21:18:07 +00:00
|
|
|
geometry.fColor = color;
|
2015-02-11 21:45:50 +00:00
|
|
|
geometry.fBounds = p.getBounds();
|
2014-11-03 20:31:14 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
|
2014-01-30 18:15:51 +00:00
|
|
|
|
2015-05-01 20:43:30 +00:00
|
|
|
tt.target()->drawBatch(&pipelineBuilder, batch);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-02-08 19:31:05 +00:00
|
|
|
x += SkScalarCeilToScalar(path->getBounds().width() + 10.f);
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
// Draw AA and non AA paths using normal API for reference.
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawPath(*path, paint);
|
|
|
|
canvas->translate(path->getBounds().width() + 10.f, 0);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawPath(*path, paint);
|
|
|
|
canvas->restore();
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-02-08 19:31:05 +00:00
|
|
|
y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SkTLList<SkRect>::Iter iter(fRects, SkTLList<SkRect>::Iter::kHead_IterStart);
|
2014-09-05 20:34:00 +00:00
|
|
|
iter.get();
|
2014-02-08 19:31:05 +00:00
|
|
|
iter.next()) {
|
|
|
|
|
|
|
|
SkScalar x = 0;
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
|
2014-02-28 14:43:26 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
if (NULL == tt.target()) {
|
|
|
|
SkDEBUGFAIL("Couldn't get Gr test target.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkRect rect = *iter.get();
|
|
|
|
rect.offset(x, y);
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
|
|
|
|
SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, rect));
|
|
|
|
if (!fp) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
2014-02-28 14:43:26 +00:00
|
|
|
}
|
2014-02-08 19:31:05 +00:00
|
|
|
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
|
|
|
pipelineBuilder.addCoverageProcessor(fp);
|
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2014-02-08 19:31:05 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
ConvexPolyTestBatch::Geometry geometry;
|
2015-05-13 21:18:07 +00:00
|
|
|
geometry.fColor = color;
|
2015-02-11 21:45:50 +00:00
|
|
|
geometry.fBounds = rect;
|
2014-11-03 20:31:14 +00:00
|
|
|
|
2015-02-11 21:45:50 +00:00
|
|
|
SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
|
2014-02-08 19:31:05 +00:00
|
|
|
|
2015-05-01 20:43:30 +00:00
|
|
|
tt.target()->drawBatch(&pipelineBuilder, batch);
|
2014-02-08 19:31:05 +00:00
|
|
|
|
2014-02-28 14:43:26 +00:00
|
|
|
x += SkScalarCeilToScalar(rect.width() + 10.f);
|
|
|
|
}
|
2014-02-08 19:31:05 +00:00
|
|
|
|
2014-02-28 14:43:26 +00:00
|
|
|
// Draw rect without and with AA using normal API for reference
|
2014-02-08 19:31:05 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
SkPaint paint;
|
2014-02-28 14:43:26 +00:00
|
|
|
canvas->drawRect(*iter.get(), paint);
|
|
|
|
x += SkScalarCeilToScalar(iter.get()->width() + 10.f);
|
2014-02-08 19:31:05 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawRect(*iter.get(), paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
2014-02-28 14:43:26 +00:00
|
|
|
y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTLList<SkPath> fPaths;
|
2014-02-08 19:31:05 +00:00
|
|
|
SkTLList<SkRect> fRects;
|
2014-02-09 03:02:01 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return SkNEW(ConvexPolyEffect); )
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|