2013-05-24 18:51:55 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2020-08-17 18:14:13 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
2013-05-24 18:51:55 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class HairlinesGM : public GM {
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-05-24 18:51:55 +00:00
|
|
|
return SkString("hairlines");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(1250, 1250); }
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2013-05-24 18:51:55 +00:00
|
|
|
{
|
2020-08-17 18:14:13 +00:00
|
|
|
SkPathBuilder lineAngles;
|
2013-05-24 18:51:55 +00:00
|
|
|
enum {
|
|
|
|
kNumAngles = 15,
|
|
|
|
kRadius = 40,
|
|
|
|
};
|
|
|
|
for (int i = 0; i < kNumAngles; ++i) {
|
|
|
|
SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles;
|
|
|
|
SkScalar x = kRadius * SkScalarCos(angle);
|
|
|
|
SkScalar y = kRadius * SkScalarSin(angle);
|
2020-08-17 18:14:13 +00:00
|
|
|
lineAngles.moveTo(x, y).lineTo(-x, -y);
|
2013-05-24 18:51:55 +00:00
|
|
|
}
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(lineAngles.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -10)
|
|
|
|
.quadTo(100, 100, -10, 0)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -5)
|
|
|
|
.quadTo(100, 100, -5, 0)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -2)
|
|
|
|
.quadTo(100, 100, -2, 0)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -1)
|
|
|
|
.quadTo(100, 100, -2 + 306.0f / 4, 75)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -1)
|
|
|
|
.quadTo(100, 100, -1, 0)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -0)
|
|
|
|
.quadTo(100, 100, 0, 0)
|
|
|
|
.detach());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(0, -0)
|
|
|
|
.quadTo(100, 100, 75, 75)
|
|
|
|
.detach());
|
2013-09-03 14:56:17 +00:00
|
|
|
|
|
|
|
// Two problem cases for gpu hairline renderer found by shapeops testing. These used
|
|
|
|
// to assert that the computed bounding box didn't contain all the vertices.
|
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(4, 6)
|
|
|
|
.cubicTo(5, 6, 5, 4, 4, 0)
|
|
|
|
.close()
|
|
|
|
.detach());
|
|
|
|
|
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(5, 1)
|
|
|
|
.lineTo( 4.32787323f, 1.67212653f)
|
|
|
|
.cubicTo(2.75223875f, 3.24776125f,
|
|
|
|
3.00581908f, 4.51236057f,
|
|
|
|
3.7580452f, 4.37367964f)
|
|
|
|
.cubicTo(4.66472578f, 3.888381f,
|
|
|
|
5.f, 2.875f,
|
|
|
|
5.f, 1.f)
|
|
|
|
.close()
|
|
|
|
.detach());
|
2013-09-18 22:14:49 +00:00
|
|
|
|
|
|
|
// Three paths that show the same bug (missing end caps)
|
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(6.5f,5.5f)
|
|
|
|
.lineTo(3.5f,0.5f)
|
|
|
|
.moveTo(0.5f,5.5f)
|
|
|
|
.lineTo(3.5f,0.5f)
|
|
|
|
.detach());
|
|
|
|
|
|
|
|
// An X (crbug.com/137317)
|
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(1, 1)
|
|
|
|
.lineTo(6, 6)
|
|
|
|
.moveTo(1, 6)
|
|
|
|
.lineTo(6, 1)
|
|
|
|
.detach());
|
|
|
|
|
|
|
|
// A right angle (crbug.com/137465 and crbug.com/256776)
|
|
|
|
fPaths.push_back(SkPathBuilder().moveTo(5.5f, 5.5f)
|
|
|
|
.lineTo(5.5f, 0.5f)
|
|
|
|
.lineTo(0.5f, 0.5f)
|
|
|
|
.detach());
|
2013-09-26 12:05:32 +00:00
|
|
|
|
|
|
|
{
|
2013-09-27 07:01:29 +00:00
|
|
|
// Arc example to test imperfect truncation bug (crbug.com/295626)
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kRad = SkIntToScalar(2000);
|
|
|
|
constexpr SkScalar kStartAngle = 262.59717f;
|
|
|
|
constexpr SkScalar kSweepAngle = SkScalarHalf(17.188717f);
|
2013-09-26 12:05:32 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
SkPathBuilder bug;
|
2013-09-26 12:05:32 +00:00
|
|
|
|
|
|
|
// Add a circular arc
|
|
|
|
SkRect circle = SkRect::MakeLTRB(-kRad, -kRad, kRad, kRad);
|
2020-08-17 18:14:13 +00:00
|
|
|
bug.addArc(circle, kStartAngle, kSweepAngle);
|
2013-09-26 12:05:32 +00:00
|
|
|
|
|
|
|
// Now add the chord that should cap the circular arc
|
2019-04-02 14:59:28 +00:00
|
|
|
SkPoint p0 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle)),
|
|
|
|
kRad * SkScalarSin(SkDegreesToRadians(kStartAngle)) };
|
2013-09-26 12:05:32 +00:00
|
|
|
|
2019-04-02 14:59:28 +00:00
|
|
|
SkPoint p1 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle + kSweepAngle)),
|
|
|
|
kRad * SkScalarSin(SkDegreesToRadians(kStartAngle + kSweepAngle)) };
|
2013-09-26 12:05:32 +00:00
|
|
|
|
2020-08-17 18:14:13 +00:00
|
|
|
bug.moveTo(p0);
|
|
|
|
bug.lineTo(p1);
|
|
|
|
fPaths.push_back(bug.detach());
|
2013-09-26 12:05:32 +00:00
|
|
|
}
|
2013-05-24 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
|
|
|
|
constexpr SkScalar kWidths[] = { 0, 0.5f, 1.5f };
|
2013-05-24 18:51:55 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
kMargin = 5,
|
|
|
|
};
|
2015-04-12 02:29:50 +00:00
|
|
|
int wrapX = 1250 - kMargin;
|
2013-05-24 18:51:55 +00:00
|
|
|
|
|
|
|
SkScalar maxH = 0;
|
|
|
|
canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin));
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
SkScalar x = SkIntToScalar(kMargin);
|
|
|
|
for (int p = 0; p < fPaths.count(); ++p) {
|
|
|
|
for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
|
|
|
|
for (int aa = 0; aa < 2; ++aa) {
|
2014-12-10 22:23:40 +00:00
|
|
|
for (size_t w = 0; w < SK_ARRAY_COUNT(kWidths); w++) {
|
|
|
|
const SkRect& bounds = fPaths[p].getBounds();
|
|
|
|
|
|
|
|
if (x + bounds.width() > wrapX) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, maxH + SkIntToScalar(kMargin));
|
|
|
|
canvas->save();
|
|
|
|
maxH = 0;
|
|
|
|
x = SkIntToScalar(kMargin);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setARGB(kAlphaValue[a], 0, 0, 0);
|
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(kWidths[w]);
|
2013-05-24 18:51:55 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2014-12-10 22:23:40 +00:00
|
|
|
canvas->translate(-bounds.fLeft, -bounds.fTop);
|
|
|
|
canvas->drawPath(fPaths[p], paint);
|
|
|
|
canvas->restore();
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-02-05 18:34:09 +00:00
|
|
|
maxH = std::max(maxH, bounds.height());
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2014-12-10 22:23:40 +00:00
|
|
|
SkScalar dx = bounds.width() + SkIntToScalar(kMargin);
|
|
|
|
x += dx;
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
}
|
2013-05-24 18:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTArray<SkPath> fPaths;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-05-24 18:51:55 +00:00
|
|
|
};
|
|
|
|
|
2015-12-04 19:08:42 +00:00
|
|
|
static void draw_squarehair_tests(SkCanvas* canvas, SkScalar width, SkPaint::Cap cap, bool aa) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStrokeCap(cap);
|
|
|
|
paint.setStrokeWidth(width);
|
|
|
|
paint.setAntiAlias(aa);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawLine(10, 10, 20, 10, paint);
|
|
|
|
canvas->drawLine(30, 10, 30, 20, paint);
|
|
|
|
canvas->drawLine(40, 10, 50, 20, paint);
|
2020-08-17 18:14:13 +00:00
|
|
|
SkPathBuilder path;
|
2015-12-04 19:08:42 +00:00
|
|
|
path.moveTo(60, 10);
|
|
|
|
path.quadTo(60, 20, 70, 20);
|
|
|
|
path.conicTo(70, 10, 80, 10, 0.707f);
|
2020-08-17 18:14:13 +00:00
|
|
|
canvas->drawPath(path.detach(), paint);
|
|
|
|
|
2015-12-04 19:08:42 +00:00
|
|
|
path.moveTo(90, 10);
|
|
|
|
path.cubicTo(90, 20, 100, 20, 100, 10);
|
|
|
|
path.lineTo(110, 10);
|
2020-08-17 18:14:13 +00:00
|
|
|
canvas->drawPath(path.detach(), paint);
|
2015-12-04 19:08:42 +00:00
|
|
|
canvas->translate(0, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(squarehair, canvas, 240, 360) {
|
|
|
|
const bool aliases[] = { false, true };
|
|
|
|
const SkScalar widths[] = { 0, 0.999f, 1, 1.001f };
|
|
|
|
const SkPaint::Cap caps[] = { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap };
|
|
|
|
for (auto alias : aliases) {
|
|
|
|
canvas->save();
|
|
|
|
for (auto width : widths) {
|
|
|
|
for (auto cap : caps) {
|
|
|
|
draw_squarehair_tests(canvas, width, cap, alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(120, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 17:26:43 +00:00
|
|
|
// GM to test subdivision of hairlines
|
|
|
|
static void draw_subdivided_quad(SkCanvas* canvas, int x0, int y0, int x1, int y1, SkColor color) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStrokeWidth(1);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(color);
|
|
|
|
|
2020-08-22 19:08:27 +00:00
|
|
|
canvas->drawPath(SkPathBuilder().moveTo(0,0)
|
|
|
|
.quadTo(SkIntToScalar(x0), SkIntToScalar(y0),
|
|
|
|
SkIntToScalar(x1), SkIntToScalar(y1))
|
|
|
|
.detach(),
|
|
|
|
paint);
|
2020-07-15 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(hairline_subdiv, canvas, 512, 256) {
|
|
|
|
// no subdivisions
|
|
|
|
canvas->translate(45, -25);
|
|
|
|
draw_subdivided_quad(canvas, 334, 334, 467, 267, SK_ColorBLACK);
|
|
|
|
|
|
|
|
// one subdivision
|
|
|
|
canvas->translate(-185, -150);
|
|
|
|
draw_subdivided_quad(canvas, 472, 472, 660, 378, SK_ColorRED);
|
|
|
|
|
|
|
|
// two subdivisions
|
|
|
|
canvas->translate(-275, -200);
|
|
|
|
draw_subdivided_quad(canvas, 668, 668, 934, 535, SK_ColorGREEN);
|
|
|
|
|
|
|
|
// three subdivisions
|
|
|
|
canvas->translate(-385, -260);
|
|
|
|
draw_subdivided_quad(canvas, 944, 944, 1320, 756, SK_ColorBLUE);
|
|
|
|
}
|
|
|
|
|
2013-05-24 18:51:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new HairlinesGM; )
|
2013-05-24 18:51:55 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|