blob: 3f84482f4f0a57d56d48cf7e9b213a81d51b9dcc [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 */
reed@android.comed881c22009-09-15 14:10:42 +00007#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
9#include "SkBlurMaskFilter.h"
reed@android.comed881c22009-09-15 14:10:42 +000010#include "SkCanvas.h"
11#include "SkParsePath.h"
12#include "SkPath.h"
13#include "SkRandom.h"
14#include "SkView.h"
15
reed@android.com4913b772009-09-21 00:27:39 +000016
reed@android.com04d86c62010-01-25 22:02:44 +000017static void test_huge_stroke(SkCanvas* canvas) {
18 SkRect srcR = { 0, 0, 72000, 54000 };
19 SkRect dstR = { 0, 0, 640, 480 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000020
reed@android.com04d86c62010-01-25 22:02:44 +000021 SkPath path;
22 path.moveTo(17600, 8000);
23 path.lineTo(52800, 8000);
24 path.lineTo(52800, 41600);
25 path.lineTo(17600, 41600);
26 path.close();
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com04d86c62010-01-25 22:02:44 +000028 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setStrokeWidth(8000);
31 paint.setStrokeMiter(10);
32 paint.setStrokeCap(SkPaint::kButt_Cap);
33 paint.setStrokeJoin(SkPaint::kRound_Join);
34 paint.setStyle(SkPaint::kStroke_Style);
35
36 SkMatrix matrix;
37 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
38 canvas->concat(matrix);
39
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
reed@google.com81e3d7f2011-06-01 12:42:36 +000097class StrokePathView : public SampleView {
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
113 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
114 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
115#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
reed@android.comed881c22009-09-15 14:10:42 +0000124 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700125 bool onQuery(SkEvent* evt) override {
reed@android.comed881c22009-09-15 14:10:42 +0000126 if (SampleCode::TitleQ(*evt)) {
127 SampleCode::TitleR(evt, "StrokePath");
128 return true;
129 }
130 return this->INHERITED::onQuery(evt);
131 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000133 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@android.comed881c22009-09-15 14:10:42 +0000135 void drawSet(SkCanvas* canvas, SkPaint* paint) {
136 SkAutoCanvasRestore acr(canvas, true);
137
138 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
139 paint->setStyle(gRec[i].fStyle);
140 paint->setStrokeJoin(gRec[i].fJoin);
141 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
142 canvas->drawPath(fPath, *paint);
143 canvas->translate(fWidth * 5 / 4, 0);
144 }
145 }
146
mtklein36352bf2015-03-25 18:17:31 -0700147 void onDrawContent(SkCanvas* canvas) override {
reed@android.com04d86c62010-01-25 22:02:44 +0000148 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000149 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
150
151 SkPaint paint;
152 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@android.comda449a32009-09-18 20:57:05 +0000154 if (true) {
155 canvas->drawColor(SK_ColorBLACK);
156
157 paint.setTextSize(24);
158 paint.setColor(SK_ColorWHITE);
159 canvas->translate(10, 30);
160
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000161 static const SkBlurStyle gStyle[] = {
162 kNormal_SkBlurStyle,
163 kInner_SkBlurStyle,
164 kOuter_SkBlurStyle,
165 kSolid_SkBlurStyle,
reed@android.comda449a32009-09-18 20:57:05 +0000166 };
167 for (int x = 0; x < 5; x++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000168 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000169 for (int y = 0; y < 10; y++) {
170 if (x) {
reedefdfd512016-04-04 10:02:58 -0700171 paint.setMaskFilter(SkBlurMaskFilter::Make(gStyle[x - 1], sigma));
reed@android.comda449a32009-09-18 20:57:05 +0000172 }
Cary Clark2a475ea2017-04-28 15:35:12 -0400173 canvas->drawString("Title Bar", x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000174 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
reed@android.comda449a32009-09-18 20:57:05 +0000177 }
178 return;
179 }
180
reed@android.comed881c22009-09-15 14:10:42 +0000181 paint.setColor(SK_ColorBLUE);
182
183#if 1
184 SkPath p;
185 float r = rand.nextUScalar1() + 0.5f;
186 SkScalar x = 0, y = 0;
187 p.moveTo(x, y);
188#if 0
189 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
190 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
191#else
192 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
193 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
194#endif
195 p.close();
196 fPath = p;
197 fPath.offset(100, 0);
198#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.comed881c22009-09-15 14:10:42 +0000200 fPath.setFillType(SkPath::kWinding_FillType);
201 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000202
reed@android.comed881c22009-09-15 14:10:42 +0000203 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
204 fPath.setFillType(SkPath::kEvenOdd_FillType);
205 drawSet(canvas, &paint);
206 }
207
reed@google.com4d5c26d2013-01-08 16:17:50 +0000208 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700209 unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700210 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000211 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comed881c22009-09-15 14:10:42 +0000212 }
213private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000214 typedef SampleView INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000215};
216
217//////////////////////////////////////////////////////////////////////////////
218
219static SkView* MyFactory() { return new StrokePathView; }
220static SkViewRegister reg(MyFactory);