blob: a77a9155e51db3d29d06b2c86aed80ecbbc60a7a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Hal Canary4484b8f2019-01-08 14:00:08 -05007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "samplecode/Sample.h"
Hal Canary4484b8f2019-01-08 14:00:08 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkMaskFilter.h"
13#include "include/core/SkPath.h"
14#include "include/utils/SkParsePath.h"
15#include "include/utils/SkRandom.h"
16#include "src/core/SkBlurMask.h"
reed@android.comed881c22009-09-15 14:10:42 +000017
reed@android.com4913b772009-09-21 00:27:39 +000018
reed@android.com04d86c62010-01-25 22:02:44 +000019static void test_huge_stroke(SkCanvas* canvas) {
20 SkRect srcR = { 0, 0, 72000, 54000 };
21 SkRect dstR = { 0, 0, 640, 480 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
reed@android.com04d86c62010-01-25 22:02:44 +000023 SkPath path;
24 path.moveTo(17600, 8000);
25 path.lineTo(52800, 8000);
26 path.lineTo(52800, 41600);
27 path.lineTo(17600, 41600);
28 path.close();
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com04d86c62010-01-25 22:02:44 +000030 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setStrokeWidth(8000);
33 paint.setStrokeMiter(10);
34 paint.setStrokeCap(SkPaint::kButt_Cap);
35 paint.setStrokeJoin(SkPaint::kRound_Join);
36 paint.setStyle(SkPaint::kStroke_Style);
37
Mike Reed2ac6ce82021-01-15 12:26:22 -050038 canvas->concat(SkMatrix::RectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit));
reed@android.com04d86c62010-01-25 22:02:44 +000039
40 canvas->drawPath(path, paint);
41}
42
reed@android.com7ab2cf92009-09-21 16:01:32 +000043#if 0
reed@android.com4913b772009-09-21 00:27:39 +000044static void test_blur() {
45 uint8_t cell[9];
46 memset(cell, 0xFF, sizeof(cell));
47 SkMask src;
48 src.fImage = cell;
49 src.fFormat = SkMask::kA8_Format;
50 SkMask dst;
51
52 for (int y = 1; y <= 3; y++) {
53 for (int x = 1; x <= 3; x++) {
54 src.fBounds.set(0, 0, x, y);
55 src.fRowBytes = src.fBounds.width();
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.com4913b772009-09-21 00:27:39 +000057 SkScalar radius = 1.f;
58
59 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
60 src.fBounds.fRight, src.fBounds.fBottom, radius);
61
62 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
63 uint8_t* dstPtr = dst.fImage;
64
65 for (int y = 0; y < dst.fBounds.height(); y++) {
66 for (int x = 0; x < dst.fBounds.width(); x++) {
67 printf(" %02X", dstPtr[x]);
68 }
69 printf("\n");
70 dstPtr += dst.fRowBytes;
71 }
72 }
73 }
74}
reed@android.com7ab2cf92009-09-21 16:01:32 +000075#endif
reed@android.comda449a32009-09-18 20:57:05 +000076
reed@android.comed881c22009-09-15 14:10:42 +000077static void scale_to_width(SkPath* path, SkScalar dstWidth) {
78 const SkRect& bounds = path->getBounds();
79 SkScalar scale = dstWidth / bounds.width();
80 SkMatrix matrix;
81
82 matrix.setScale(scale, scale);
83 path->transform(matrix);
84}
85
86static const struct {
87 SkPaint::Style fStyle;
88 SkPaint::Join fJoin;
89 int fStrokeWidth;
90} gRec[] = {
91 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
92 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
93 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
94 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
95};
96
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040097class StrokePathView : public Sample {
reed@android.comed881c22009-09-15 14:10:42 +000098 SkScalar fWidth;
99 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -0800100protected:
mtklein36352bf2015-03-25 18:17:31 -0700101 void onOnceBeforeDraw() override {
reed@android.com7ab2cf92009-09-21 16:01:32 +0000102// test_blur();
reed@android.comed881c22009-09-15 14:10:42 +0000103 fWidth = SkIntToScalar(120);
104
105#if 0
106 const char str[] =
107 "M 0, 3"
108 "C 10, -10, 30, -10, 0, 28"
109 "C -30, -10, -10, -10, 0, 3"
110 "Z";
111 SkParsePath::FromSVGString(str, &fPath);
112#else
Mike Reed30bc5272019-11-22 18:34:02 +0000113 fPath.addCircle(0, 0, SkIntToScalar(50), SkPathDirection::kCW);
114 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPathDirection::kCW);
reed@android.comed881c22009-09-15 14:10:42 +0000115#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.comed881c22009-09-15 14:10:42 +0000117 scale_to_width(&fPath, fWidth);
118 const SkRect& bounds = fPath.getBounds();
119 fPath.offset(-bounds.fLeft, -bounds.fTop);
reed@google.com81e3d7f2011-06-01 12:42:36 +0000120
121 this->setBGColor(0xFFDDDDDD);
reed@android.comed881c22009-09-15 14:10:42 +0000122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
Hal Canary8a027312019-07-03 10:55:44 -0400124 SkString name() override { return SkString("StrokePath"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000126 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.comed881c22009-09-15 14:10:42 +0000128 void drawSet(SkCanvas* canvas, SkPaint* paint) {
129 SkAutoCanvasRestore acr(canvas, true);
130
131 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
132 paint->setStyle(gRec[i].fStyle);
133 paint->setStrokeJoin(gRec[i].fJoin);
134 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
135 canvas->drawPath(fPath, *paint);
136 canvas->translate(fWidth * 5 / 4, 0);
137 }
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 void onDrawContent(SkCanvas* canvas) override {
reed@android.com04d86c62010-01-25 22:02:44 +0000141 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000142 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
143
144 SkPaint paint;
145 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.comda449a32009-09-18 20:57:05 +0000147 if (true) {
148 canvas->drawColor(SK_ColorBLACK);
149
Hal Canary4484b8f2019-01-08 14:00:08 -0500150 SkFont font(nullptr, 24);
reed@android.comda449a32009-09-18 20:57:05 +0000151 paint.setColor(SK_ColorWHITE);
152 canvas->translate(10, 30);
153
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000154 static const SkBlurStyle gStyle[] = {
155 kNormal_SkBlurStyle,
156 kInner_SkBlurStyle,
157 kOuter_SkBlurStyle,
158 kSolid_SkBlurStyle,
reed@android.comda449a32009-09-18 20:57:05 +0000159 };
160 for (int x = 0; x < 5; x++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000161 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000162 for (int y = 0; y < 10; y++) {
163 if (x) {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400164 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
reed@android.comda449a32009-09-18 20:57:05 +0000165 }
Hal Canary4484b8f2019-01-08 14:00:08 -0500166 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000167 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000168 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@android.comda449a32009-09-18 20:57:05 +0000170 }
171 return;
172 }
173
reed@android.comed881c22009-09-15 14:10:42 +0000174 paint.setColor(SK_ColorBLUE);
175
176#if 1
177 SkPath p;
178 float r = rand.nextUScalar1() + 0.5f;
179 SkScalar x = 0, y = 0;
180 p.moveTo(x, y);
181#if 0
182 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
183 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
184#else
185 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
186 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
187#endif
188 p.close();
189 fPath = p;
190 fPath.offset(100, 0);
191#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000192
Mike Reed7d34dc72019-11-26 12:17:17 -0500193 fPath.setFillType(SkPathFillType::kWinding);
reed@android.comed881c22009-09-15 14:10:42 +0000194 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed@android.comed881c22009-09-15 14:10:42 +0000196 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
Mike Reed7d34dc72019-11-26 12:17:17 -0500197 fPath.setFillType(SkPathFillType::kEvenOdd);
reed@android.comed881c22009-09-15 14:10:42 +0000198 drawSet(canvas, &paint);
199 }
200
reed@android.comed881c22009-09-15 14:10:42 +0000201private:
John Stiles7571f9e2020-09-02 22:42:33 -0400202 using INHERITED = Sample;
reed@android.comed881c22009-09-15 14:10:42 +0000203};
204
205//////////////////////////////////////////////////////////////////////////////
206
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400207DEF_SAMPLE( return new StrokePathView(); )