2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-08-05 20:57:49 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
#include "gm.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2011-04-11 13:57:04 +00:00
|
|
|
|
|
|
|
typedef SkScalar (*MakePathProc)(SkPath*);
|
|
|
|
|
|
|
|
static SkScalar make_frame(SkPath* path) {
|
2011-05-20 14:05:03 +00:00
|
|
|
SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(630), SkIntToScalar(470) };
|
|
|
|
path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2011-05-20 14:05:03 +00:00
|
|
|
paint.setStrokeWidth(SkIntToScalar(5));
|
2011-04-11 13:57:04 +00:00
|
|
|
paint.getFillPath(*path, path);
|
2011-05-20 14:05:03 +00:00
|
|
|
return SkIntToScalar(15);
|
2011-04-11 13:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_triangle(SkPath* path) {
|
|
|
|
static const int gCoord[] = {
|
|
|
|
10, 20, 15, 5, 30, 30
|
|
|
|
};
|
|
|
|
path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
|
|
|
|
path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
|
|
|
|
path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
|
|
|
|
path->close();
|
2011-05-20 14:05:03 +00:00
|
|
|
path->offset(SkIntToScalar(10), SkIntToScalar(0));
|
2011-04-11 13:57:04 +00:00
|
|
|
return SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_rect(SkPath* path) {
|
2011-05-20 14:05:03 +00:00
|
|
|
SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(30), SkIntToScalar(30) };
|
2011-04-11 13:57:04 +00:00
|
|
|
path->addRect(r);
|
2011-05-20 14:05:03 +00:00
|
|
|
path->offset(SkIntToScalar(10), SkIntToScalar(0));
|
2011-04-11 13:57:04 +00:00
|
|
|
return SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_oval(SkPath* path) {
|
2011-05-20 14:05:03 +00:00
|
|
|
SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(30), SkIntToScalar(30) };
|
2011-04-11 13:57:04 +00:00
|
|
|
path->addOval(r);
|
2011-05-20 14:05:03 +00:00
|
|
|
path->offset(SkIntToScalar(10), SkIntToScalar(0));
|
2011-04-11 13:57:04 +00:00
|
|
|
return SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_sawtooth(SkPath* path) {
|
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(20);
|
|
|
|
const SkScalar x0 = x;
|
|
|
|
const SkScalar dx = SK_Scalar1 * 5;
|
|
|
|
const SkScalar dy = SK_Scalar1 * 10;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
path->moveTo(x, y);
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
x += dx;
|
|
|
|
path->lineTo(x, y - dy);
|
|
|
|
x += dx;
|
|
|
|
path->lineTo(x, y + dy);
|
|
|
|
}
|
2011-05-20 14:05:03 +00:00
|
|
|
path->lineTo(x, y + (2 * dy));
|
|
|
|
path->lineTo(x0, y + (2 * dy));
|
2011-04-11 13:57:04 +00:00
|
|
|
path->close();
|
|
|
|
return SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_star(SkPath* path, int n) {
|
|
|
|
const SkScalar c = SkIntToScalar(45);
|
|
|
|
const SkScalar r = SkIntToScalar(20);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
SkScalar rad = -SK_ScalarPI / 2;
|
|
|
|
const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
path->moveTo(c, c - r);
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
rad += drad;
|
|
|
|
SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
|
|
|
|
path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r));
|
|
|
|
}
|
|
|
|
path->close();
|
|
|
|
return r * 2 * 6 / 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
|
|
|
|
static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
|
|
|
|
|
2012-05-09 17:17:51 +00:00
|
|
|
// We don't expect any output from this path.
|
|
|
|
static SkScalar make_line(SkPath* path) {
|
|
|
|
path->moveTo(SkIntToScalar(30), SkIntToScalar(30));
|
|
|
|
path->lineTo(SkIntToScalar(120), SkIntToScalar(40));
|
|
|
|
path->close();
|
|
|
|
path->moveTo(SkIntToScalar(150), SkIntToScalar(30));
|
|
|
|
path->lineTo(SkIntToScalar(150), SkIntToScalar(30));
|
|
|
|
path->lineTo(SkIntToScalar(300), SkIntToScalar(40));
|
|
|
|
path->close();
|
|
|
|
return SkIntToScalar(40);
|
|
|
|
}
|
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
static const MakePathProc gProcs[] = {
|
|
|
|
make_frame,
|
|
|
|
make_triangle,
|
|
|
|
make_rect,
|
|
|
|
make_oval,
|
|
|
|
make_sawtooth,
|
|
|
|
make_star_5,
|
2012-05-09 17:17:51 +00:00
|
|
|
make_star_13,
|
|
|
|
make_line,
|
2011-04-11 13:57:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define N SK_ARRAY_COUNT(gProcs)
|
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
class PathFillGM : public skiagm::GM {
|
2011-04-11 13:57:04 +00:00
|
|
|
SkPath fPath[N];
|
|
|
|
SkScalar fDY[N];
|
2015-02-18 18:56:00 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2011-04-11 13:57:04 +00:00
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
fDY[i] = gProcs[i](&fPath[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2011-04-11 13:57:04 +00:00
|
|
|
return SkString("pathfill");
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-06-11 17:30:33 +00:00
|
|
|
return SkISize::Make(640, 480);
|
2011-04-11 13:57:04 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2011-04-11 13:57:04 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
canvas->drawPath(fPath[i], paint);
|
2011-05-20 14:05:03 +00:00
|
|
|
canvas->translate(SkIntToScalar(0), fDY[i]);
|
2011-04-11 13:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// test inverse-fill w/ a clip that completely excludes the geometry
|
|
|
|
class PathInverseFillGM : public skiagm::GM {
|
|
|
|
SkPath fPath[N];
|
|
|
|
SkScalar fDY[N];
|
2015-02-18 18:56:00 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2012-06-11 17:30:33 +00:00
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
fDY[i] = gProcs[i](&fPath[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-06-11 17:30:33 +00:00
|
|
|
return SkString("pathinvfill");
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-06-11 17:30:33 +00:00
|
|
|
return SkISize::Make(450, 220);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
static void show(SkCanvas* canvas, const SkPath& path, const SkPaint& paint,
|
|
|
|
const SkRect* clip, SkScalar top, const SkScalar bottom) {
|
|
|
|
canvas->save();
|
|
|
|
if (clip) {
|
|
|
|
SkRect r = *clip;
|
|
|
|
r.fTop = top;
|
|
|
|
r.fBottom = bottom;
|
|
|
|
canvas->clipRect(r);
|
|
|
|
}
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-06-11 17:30:33 +00:00
|
|
|
SkPath path;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
path.addCircle(SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(40));
|
|
|
|
path.toggleInverseFillType();
|
|
|
|
|
|
|
|
SkRect clipR = { 0, 0, SkIntToScalar(100), SkIntToScalar(200) };
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
|
|
|
|
for (int doclip = 0; doclip <= 1; ++doclip) {
|
|
|
|
for (int aa = 0; aa <= 1; ++aa) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipR);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
const SkRect* clipPtr = doclip ? &clipR : nullptr;
|
2011-04-11 13:57:04 +00:00
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
show(canvas, path, paint, clipPtr, clipR.fTop, clipR.centerY());
|
|
|
|
show(canvas, path, paint, clipPtr, clipR.centerY(), clipR.fBottom);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-11 17:30:33 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(SkIntToScalar(110), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
private:
|
2012-06-11 17:30:33 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2011-04-11 13:57:04 +00:00
|
|
|
};
|
|
|
|
|
2016-03-01 17:42:03 +00:00
|
|
|
DEF_SIMPLE_GM(rotatedcubicpath, canvas, 200, 200) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
|
|
|
canvas->translate(50, 50);
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(48,-23);
|
|
|
|
path.cubicTo(48,-29.5, 6,-30, 6,-30);
|
|
|
|
path.cubicTo(6,-30, 2,0, 2,0);
|
|
|
|
path.cubicTo(2,0, 44,-21.5, 48,-23);
|
|
|
|
path.close();
|
|
|
|
|
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
|
|
|
|
// Rotated path, which is not antialiased on GPU
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->rotate(90);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
2011-04-11 13:57:04 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-10 21:31:59 +00:00
|
|
|
DEF_GM( return new PathFillGM; )
|
|
|
|
DEF_GM( return new PathInverseFillGM; )
|