2012-01-17 14:25:10 +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.
|
|
|
|
*/
|
2019-05-01 21:28:53 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2020-08-16 15:15:41 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2022-02-23 17:17:12 +00:00
|
|
|
#include "include/core/SkRRect.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkNoncopyable.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2012-01-17 14:25:10 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-01-17 17:55:02 +00:00
|
|
|
class SkDoOnce : SkNoncopyable {
|
2012-03-30 20:04:21 +00:00
|
|
|
public:
|
2014-01-17 17:55:02 +00:00
|
|
|
SkDoOnce() { fDidOnce = false; }
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-30 20:04:21 +00:00
|
|
|
bool needToDo() const { return !fDidOnce; }
|
|
|
|
bool alreadyDone() const { return fDidOnce; }
|
|
|
|
void accomplished() {
|
|
|
|
SkASSERT(!fDidOnce);
|
|
|
|
fDidOnce = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fDidOnce;
|
|
|
|
};
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
class ConvexPathsGM : public skiagm::GM {
|
2014-01-17 17:55:02 +00:00
|
|
|
SkDoOnce fOnce;
|
2012-01-17 14:25:10 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onOnceBeforeDraw() override { this->setBGColor(0xFF000000); }
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkString onShortName() override { return SkString("convexpaths"); }
|
2012-01-17 14:25:10 +00:00
|
|
|
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkISize onISize() override { return {1200, 1100}; }
|
2012-01-17 14:25:10 +00:00
|
|
|
|
|
|
|
void makePaths() {
|
2012-03-30 20:04:21 +00:00
|
|
|
if (fOnce.alreadyDone()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fOnce.accomplished();
|
2013-08-15 18:16:27 +00:00
|
|
|
|
2020-08-16 15:15:41 +00:00
|
|
|
SkPathBuilder b;
|
|
|
|
fPaths.push_back(b.moveTo(0, 0)
|
|
|
|
.quadTo(50, 100, 0, 100)
|
|
|
|
.lineTo(0, 0)
|
|
|
|
.detach());
|
|
|
|
|
|
|
|
fPaths.push_back(b.moveTo(0, 50)
|
|
|
|
.quadTo(50, 0, 100, 50)
|
|
|
|
.quadTo(50, 100, 0, 50)
|
|
|
|
.detach());
|
|
|
|
|
|
|
|
fPaths.push_back(SkPath::Rect({0, 0, 100, 100}, SkPathDirection::kCW));
|
|
|
|
fPaths.push_back(SkPath::Rect({0, 0, 100, 100}, SkPathDirection::kCCW));
|
|
|
|
fPaths.push_back(SkPath::Circle(50, 50, 50, SkPathDirection::kCW));
|
|
|
|
fPaths.push_back(SkPath::Oval(SkRect::MakeXYWH(0, 0, 50, 100), SkPathDirection::kCW));
|
|
|
|
fPaths.push_back(SkPath::Oval(SkRect::MakeXYWH(0, 0, 100, 5), SkPathDirection::kCCW));
|
|
|
|
fPaths.push_back(SkPath::Oval(SkRect::MakeXYWH(0, 0, 1, 100), SkPathDirection::kCCW));
|
|
|
|
fPaths.push_back(SkPath::RRect(SkRRect::MakeRectXY({0, 0, 100, 100}, 40, 20),
|
|
|
|
SkPathDirection::kCW));
|
2012-01-17 14:25:10 +00:00
|
|
|
|
2013-05-14 14:20:28 +00:00
|
|
|
// large number of points
|
|
|
|
enum {
|
|
|
|
kLength = 100,
|
|
|
|
kPtsPerSide = (1 << 12),
|
|
|
|
};
|
2020-08-16 15:15:41 +00:00
|
|
|
b.moveTo(0, 0);
|
2013-05-14 14:20:28 +00:00
|
|
|
for (int i = 1; i < kPtsPerSide; ++i) { // skip the first point due to moveTo.
|
2020-08-16 15:15:41 +00:00
|
|
|
b.lineTo(kLength * SkIntToScalar(i) / kPtsPerSide, 0);
|
2013-05-14 14:20:28 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < kPtsPerSide; ++i) {
|
2020-08-16 15:15:41 +00:00
|
|
|
b.lineTo(kLength, kLength * SkIntToScalar(i) / kPtsPerSide);
|
2013-05-14 14:20:28 +00:00
|
|
|
}
|
|
|
|
for (int i = kPtsPerSide; i > 0; --i) {
|
2020-08-16 15:15:41 +00:00
|
|
|
b.lineTo(kLength * SkIntToScalar(i) / kPtsPerSide, kLength);
|
2013-05-14 14:20:28 +00:00
|
|
|
}
|
|
|
|
for (int i = kPtsPerSide; i > 0; --i) {
|
2020-08-16 15:15:41 +00:00
|
|
|
b.lineTo(0, kLength * SkIntToScalar(i) / kPtsPerSide);
|
2013-05-14 14:20:28 +00:00
|
|
|
}
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.detach());
|
2013-05-14 14:20:28 +00:00
|
|
|
|
2012-01-26 20:33:19 +00:00
|
|
|
// shallow diagonals
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(SkPath::Polygon({{0,0}, {100,1}, {98,100}, {3,96}}, false));
|
2012-01-31 15:19:21 +00:00
|
|
|
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.arcTo(SkRect::MakeXYWH(0, 0, 50, 100), 25, 130, false)
|
|
|
|
.detach());
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-26 20:33:19 +00:00
|
|
|
// cubics
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.cubicTo( 1, 1, 10, 90, 0, 100).detach());
|
|
|
|
fPaths.push_back(b.cubicTo(100, 50, 20, 100, 0, 0).detach());
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-07-10 19:53:34 +00:00
|
|
|
// path that has a cubic with a repeated first control point and
|
|
|
|
// a repeated last control point.
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(10, 10)
|
|
|
|
.cubicTo(10, 10, 10, 0, 20, 0)
|
|
|
|
.lineTo(40, 0)
|
|
|
|
.cubicTo(40, 0, 50, 0, 50, 10)
|
|
|
|
.detach());
|
2012-07-10 19:53:34 +00:00
|
|
|
|
|
|
|
// path that has two cubics with repeated middle control points.
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(10, 10)
|
|
|
|
.cubicTo(10, 0, 10, 0, 20, 0)
|
|
|
|
.lineTo(40, 0)
|
|
|
|
.cubicTo(50, 0, 50, 0, 50, 10)
|
|
|
|
.detach());
|
2012-07-10 19:53:34 +00:00
|
|
|
|
2012-08-02 14:55:45 +00:00
|
|
|
// cubic where last three points are almost a line
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 228.0f/8)
|
|
|
|
.cubicTo( 628.0f/ 8, 82.0f/8,
|
|
|
|
1255.0f/ 8, 141.0f/8,
|
|
|
|
1883.0f/ 8, 202.0f/8)
|
|
|
|
.detach());
|
2012-08-02 14:55:45 +00:00
|
|
|
|
|
|
|
// flat cubic where the at end point tangents both point outward.
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(10, 0)
|
|
|
|
.cubicTo(0, 1, 30, 1, 20, 0)
|
|
|
|
.detach());
|
2012-08-02 14:55:45 +00:00
|
|
|
|
|
|
|
// flat cubic where initial tangent is in, end tangent out
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 0)
|
|
|
|
.cubicTo(10, 1, 30, 1, 20, 0)
|
|
|
|
.detach());
|
2012-08-02 14:55:45 +00:00
|
|
|
|
|
|
|
// flat cubic where initial tangent is out, end tangent in
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(10, 0)
|
|
|
|
.cubicTo(0, 1, 20, 1, 30, 0)
|
|
|
|
.detach());
|
2012-08-02 14:55:45 +00:00
|
|
|
|
2012-01-31 20:46:32 +00:00
|
|
|
// triangle where one edge is a degenerate quad
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(8.59375f, 45)
|
|
|
|
.quadTo(16.9921875f, 45,
|
|
|
|
31.25f, 45)
|
|
|
|
.lineTo(100, 100)
|
|
|
|
.lineTo(8.59375f, 45)
|
|
|
|
.detach());
|
2012-01-31 20:46:32 +00:00
|
|
|
|
2013-08-15 18:16:27 +00:00
|
|
|
// triangle where one edge is a quad with a repeated point
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 25)
|
|
|
|
.lineTo(50, 0)
|
|
|
|
.quadTo(50, 50, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// triangle where one edge is a cubic with a 2x repeated point
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 25)
|
|
|
|
.lineTo(50, 0)
|
|
|
|
.cubicTo(50, 0, 50, 50, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// triangle where one edge is a quad with a nearly repeated point
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 25)
|
|
|
|
.lineTo(50, 0)
|
|
|
|
.quadTo(50, 49.95f, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// triangle where one edge is a cubic with a 3x nearly repeated point
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 25)
|
|
|
|
.lineTo(50, 0)
|
|
|
|
.cubicTo(50, 49.95f, 50, 49.97f, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// triangle where there is a point degenerate cubic at one corner
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 25)
|
|
|
|
.lineTo(50, 0)
|
|
|
|
.lineTo(50, 50)
|
|
|
|
.cubicTo(50, 50, 50, 50, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// point line
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(SkPath::Line({50, 50}, {50, 50}));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-08-15 18:16:27 +00:00
|
|
|
// point quad
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(50, 50)
|
|
|
|
.quadTo(50, 50, 50, 50)
|
|
|
|
.detach());
|
2013-08-15 18:16:27 +00:00
|
|
|
|
|
|
|
// point cubic
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(50, 50)
|
|
|
|
.cubicTo(50, 50, 50, 50, 50, 50)
|
|
|
|
.detach());
|
2012-01-31 20:46:32 +00:00
|
|
|
|
2012-01-31 15:19:21 +00:00
|
|
|
// moveTo only paths
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 0)
|
|
|
|
.moveTo(0, 0)
|
|
|
|
.moveTo(1, 1)
|
|
|
|
.moveTo(1, 1)
|
|
|
|
.moveTo(10, 10)
|
|
|
|
.detach());
|
2012-01-31 15:19:21 +00:00
|
|
|
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(0, 0)
|
|
|
|
.moveTo(0, 0)
|
|
|
|
.detach());
|
2012-01-17 14:25:10 +00:00
|
|
|
|
|
|
|
// line degenerate
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.lineTo(100, 100).detach());
|
|
|
|
fPaths.push_back(b.quadTo(100, 100, 0, 0).detach());
|
|
|
|
fPaths.push_back(b.quadTo(100, 100, 50, 50).detach());
|
|
|
|
fPaths.push_back(b.quadTo(50, 50, 100, 100).detach());
|
|
|
|
fPaths.push_back(b.cubicTo(0, 0, 0, 0, 100, 100).detach());
|
2012-02-23 19:18:37 +00:00
|
|
|
|
2019-03-29 19:41:27 +00:00
|
|
|
// skbug.com/8928
|
2020-08-16 15:15:41 +00:00
|
|
|
fPaths.push_back(b.moveTo(16.875f, 192.594f)
|
|
|
|
.cubicTo(45.625f, 192.594f, 74.375f, 192.594f, 103.125f, 192.594f)
|
|
|
|
.cubicTo(88.75f, 167.708f, 74.375f, 142.823f, 60, 117.938f)
|
|
|
|
.cubicTo(45.625f, 142.823f, 31.25f, 167.708f, 16.875f, 192.594f)
|
|
|
|
.close()
|
|
|
|
.detach());
|
2019-03-29 19:41:27 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setAll(0.1f, 0, -1, 0, 0.115207f, -2.64977f, 0, 0, 1);
|
|
|
|
fPaths.back().transform(m);
|
|
|
|
|
2012-02-23 19:18:37 +00:00
|
|
|
// small circle. This is listed last so that it has device coords far
|
|
|
|
// from the origin (small area relative to x,y values).
|
2020-08-25 15:48:41 +00:00
|
|
|
fPaths.push_back(SkPath::Circle(0, 0, 1.2f));
|
2012-01-17 14:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-03-30 20:04:21 +00:00
|
|
|
this->makePaths();
|
2012-01-17 14:25:10 +00:00
|
|
|
|
2017-02-15 20:46:52 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
SkRandom rand;
|
2020-08-16 15:15:41 +00:00
|
|
|
canvas->translate(20, 20);
|
2017-02-15 20:46:52 +00:00
|
|
|
|
|
|
|
// As we've added more paths this has gotten pretty big. Scale the whole thing down.
|
2020-08-16 15:15:41 +00:00
|
|
|
canvas->scale(2.0f/3, 2.0f/3);
|
2017-02-15 20:46:52 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < fPaths.count(); ++i) {
|
|
|
|
canvas->save();
|
|
|
|
// position the path, and make it at off-integer coords.
|
2020-08-16 15:15:41 +00:00
|
|
|
canvas->translate(200.0f * (i % 5) + 1.0f/10,
|
|
|
|
200.0f * (i / 5) + 9.0f/10);
|
2017-02-15 20:46:52 +00:00
|
|
|
SkColor color = rand.nextU();
|
|
|
|
color |= 0xff000000;
|
|
|
|
paint.setColor(color);
|
|
|
|
#if 0 // This hitting on 32bit Linux builds for some paths. Temporarily disabling while it is
|
|
|
|
// debugged.
|
|
|
|
SkASSERT(fPaths[i].isConvex());
|
2013-08-15 20:34:45 +00:00
|
|
|
#endif
|
2017-02-15 20:46:52 +00:00
|
|
|
canvas->drawPath(fPaths[i], paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-01-17 14:25:10 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-17 14:25:10 +00:00
|
|
|
SkTArray<SkPath> fPaths;
|
|
|
|
};
|
2019-07-18 15:51:19 +00:00
|
|
|
} // namespace
|
2012-01-17 14:25:10 +00:00
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new ConvexPathsGM; )
|