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.
|
|
|
|
*/
|
2019-01-08 19:00:08 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkMaskFilter.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/utils/SkParsePath.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
2009-09-15 14:10:42 +00:00
|
|
|
|
2009-09-21 00:27:39 +00:00
|
|
|
|
2010-01-25 22:02:44 +00:00
|
|
|
static void test_huge_stroke(SkCanvas* canvas) {
|
|
|
|
SkRect srcR = { 0, 0, 72000, 54000 };
|
|
|
|
SkRect dstR = { 0, 0, 640, 480 };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-01-25 22:02:44 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(17600, 8000);
|
|
|
|
path.lineTo(52800, 8000);
|
|
|
|
path.lineTo(52800, 41600);
|
|
|
|
path.lineTo(17600, 41600);
|
|
|
|
path.close();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-01-25 22:02:44 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(8000);
|
|
|
|
paint.setStrokeMiter(10);
|
|
|
|
paint.setStrokeCap(SkPaint::kButt_Cap);
|
|
|
|
paint.setStrokeJoin(SkPaint::kRound_Join);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
|
|
|
|
canvas->concat(matrix);
|
|
|
|
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:01:32 +00:00
|
|
|
#if 0
|
2009-09-21 00:27:39 +00:00
|
|
|
static void test_blur() {
|
|
|
|
uint8_t cell[9];
|
|
|
|
memset(cell, 0xFF, sizeof(cell));
|
|
|
|
SkMask src;
|
|
|
|
src.fImage = cell;
|
|
|
|
src.fFormat = SkMask::kA8_Format;
|
|
|
|
SkMask dst;
|
|
|
|
|
|
|
|
for (int y = 1; y <= 3; y++) {
|
|
|
|
for (int x = 1; x <= 3; x++) {
|
|
|
|
src.fBounds.set(0, 0, x, y);
|
|
|
|
src.fRowBytes = src.fBounds.width();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-21 00:27:39 +00:00
|
|
|
SkScalar radius = 1.f;
|
|
|
|
|
|
|
|
printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
|
|
|
|
src.fBounds.fRight, src.fBounds.fBottom, radius);
|
|
|
|
|
|
|
|
SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
|
|
|
|
uint8_t* dstPtr = dst.fImage;
|
|
|
|
|
|
|
|
for (int y = 0; y < dst.fBounds.height(); y++) {
|
|
|
|
for (int x = 0; x < dst.fBounds.width(); x++) {
|
|
|
|
printf(" %02X", dstPtr[x]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
dstPtr += dst.fRowBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 16:01:32 +00:00
|
|
|
#endif
|
2009-09-18 20:57:05 +00:00
|
|
|
|
2009-09-15 14:10:42 +00:00
|
|
|
static void scale_to_width(SkPath* path, SkScalar dstWidth) {
|
|
|
|
const SkRect& bounds = path->getBounds();
|
|
|
|
SkScalar scale = dstWidth / bounds.width();
|
|
|
|
SkMatrix matrix;
|
|
|
|
|
|
|
|
matrix.setScale(scale, scale);
|
|
|
|
path->transform(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkPaint::Style fStyle;
|
|
|
|
SkPaint::Join fJoin;
|
|
|
|
int fStrokeWidth;
|
|
|
|
} gRec[] = {
|
|
|
|
{ SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
|
|
|
|
{ SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
|
|
|
|
{ SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
|
|
|
|
{ SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class StrokePathView : public Sample {
|
2009-09-15 14:10:42 +00:00
|
|
|
SkScalar fWidth;
|
|
|
|
SkPath fPath;
|
2015-02-25 17:04:04 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2009-09-21 16:01:32 +00:00
|
|
|
// test_blur();
|
2009-09-15 14:10:42 +00:00
|
|
|
fWidth = SkIntToScalar(120);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
const char str[] =
|
|
|
|
"M 0, 3"
|
|
|
|
"C 10, -10, 30, -10, 0, 28"
|
|
|
|
"C -30, -10, -10, -10, 0, 3"
|
|
|
|
"Z";
|
|
|
|
SkParsePath::FromSVGString(str, &fPath);
|
|
|
|
#else
|
2019-11-22 18:34:02 +00:00
|
|
|
fPath.addCircle(0, 0, SkIntToScalar(50), SkPathDirection::kCW);
|
|
|
|
fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPathDirection::kCW);
|
2009-09-15 14:10:42 +00:00
|
|
|
#endif
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-15 14:10:42 +00:00
|
|
|
scale_to_width(&fPath, fWidth);
|
|
|
|
const SkRect& bounds = fPath.getBounds();
|
|
|
|
fPath.offset(-bounds.fLeft, -bounds.fTop);
|
2011-06-01 12:42:36 +00:00
|
|
|
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2009-09-15 14:10:42 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("StrokePath"); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-15 14:10:42 +00:00
|
|
|
void drawSet(SkCanvas* canvas, SkPaint* paint) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
|
|
|
|
paint->setStyle(gRec[i].fStyle);
|
|
|
|
paint->setStrokeJoin(gRec[i].fJoin);
|
|
|
|
paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
|
|
|
|
canvas->drawPath(fPath, *paint);
|
|
|
|
canvas->translate(fWidth * 5 / 4, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2010-01-25 22:02:44 +00:00
|
|
|
test_huge_stroke(canvas); return;
|
2009-09-15 14:10:42 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-18 20:57:05 +00:00
|
|
|
if (true) {
|
|
|
|
canvas->drawColor(SK_ColorBLACK);
|
|
|
|
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, 24);
|
2009-09-18 20:57:05 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->translate(10, 30);
|
|
|
|
|
2014-04-28 16:25:35 +00:00
|
|
|
static const SkBlurStyle gStyle[] = {
|
|
|
|
kNormal_SkBlurStyle,
|
|
|
|
kInner_SkBlurStyle,
|
|
|
|
kOuter_SkBlurStyle,
|
|
|
|
kSolid_SkBlurStyle,
|
2009-09-18 20:57:05 +00:00
|
|
|
};
|
|
|
|
for (int x = 0; x < 5; x++) {
|
2013-09-06 14:16:12 +00:00
|
|
|
SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
|
2009-09-18 20:57:05 +00:00
|
|
|
for (int y = 0; y < 10; y++) {
|
|
|
|
if (x) {
|
2018-03-14 17:01:17 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
|
2009-09-18 20:57:05 +00:00
|
|
|
}
|
2019-01-08 19:00:08 +00:00
|
|
|
canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
|
2013-09-06 14:16:12 +00:00
|
|
|
sigma *= 0.75f;
|
2009-09-18 20:57:05 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-18 20:57:05 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-15 14:10:42 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
SkPath p;
|
|
|
|
float r = rand.nextUScalar1() + 0.5f;
|
|
|
|
SkScalar x = 0, y = 0;
|
|
|
|
p.moveTo(x, y);
|
|
|
|
#if 0
|
|
|
|
p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
|
|
|
|
p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
|
|
|
|
#else
|
|
|
|
p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
|
|
|
|
p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
|
|
|
|
#endif
|
|
|
|
p.close();
|
|
|
|
fPath = p;
|
|
|
|
fPath.offset(100, 0);
|
|
|
|
#endif
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-11-26 17:17:17 +00:00
|
|
|
fPath.setFillType(SkPathFillType::kWinding);
|
2009-09-15 14:10:42 +00:00
|
|
|
drawSet(canvas, &paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-09-15 14:10:42 +00:00
|
|
|
canvas->translate(0, fPath.getBounds().height() * 5 / 4);
|
2019-11-26 17:17:17 +00:00
|
|
|
fPath.setFillType(SkPathFillType::kEvenOdd);
|
2009-09-15 14:10:42 +00:00
|
|
|
drawSet(canvas, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2009-09-15 14:10:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new StrokePathView(); )
|