blob: adf5a449d7f68c2c63456ca51a4131de64ee8dfa [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
reed@android.comed881c22009-09-15 14:10:42 +00009#include "SkCanvas.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040010#include "SkMaskFilter.h"
reed@android.comed881c22009-09-15 14:10:42 +000011#include "SkParsePath.h"
12#include "SkPath.h"
13#include "SkRandom.h"
reed@android.comed881c22009-09-15 14:10:42 +000014
reed@android.com4913b772009-09-21 00:27:39 +000015
reed@android.com04d86c62010-01-25 22:02:44 +000016static void test_huge_stroke(SkCanvas* canvas) {
17 SkRect srcR = { 0, 0, 72000, 54000 };
18 SkRect dstR = { 0, 0, 640, 480 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
reed@android.com04d86c62010-01-25 22:02:44 +000020 SkPath path;
21 path.moveTo(17600, 8000);
22 path.lineTo(52800, 8000);
23 path.lineTo(52800, 41600);
24 path.lineTo(17600, 41600);
25 path.close();
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com04d86c62010-01-25 22:02:44 +000027 SkPaint paint;
28 paint.setAntiAlias(true);
29 paint.setStrokeWidth(8000);
30 paint.setStrokeMiter(10);
31 paint.setStrokeCap(SkPaint::kButt_Cap);
32 paint.setStrokeJoin(SkPaint::kRound_Join);
33 paint.setStyle(SkPaint::kStroke_Style);
34
35 SkMatrix matrix;
36 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
37 canvas->concat(matrix);
38
39 canvas->drawPath(path, paint);
40}
41
reed@android.com7ab2cf92009-09-21 16:01:32 +000042#if 0
reed@android.com4913b772009-09-21 00:27:39 +000043static void test_blur() {
44 uint8_t cell[9];
45 memset(cell, 0xFF, sizeof(cell));
46 SkMask src;
47 src.fImage = cell;
48 src.fFormat = SkMask::kA8_Format;
49 SkMask dst;
50
51 for (int y = 1; y <= 3; y++) {
52 for (int x = 1; x <= 3; x++) {
53 src.fBounds.set(0, 0, x, y);
54 src.fRowBytes = src.fBounds.width();
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.com4913b772009-09-21 00:27:39 +000056 SkScalar radius = 1.f;
57
58 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
59 src.fBounds.fRight, src.fBounds.fBottom, radius);
60
61 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
62 uint8_t* dstPtr = dst.fImage;
63
64 for (int y = 0; y < dst.fBounds.height(); y++) {
65 for (int x = 0; x < dst.fBounds.width(); x++) {
66 printf(" %02X", dstPtr[x]);
67 }
68 printf("\n");
69 dstPtr += dst.fRowBytes;
70 }
71 }
72 }
73}
reed@android.com7ab2cf92009-09-21 16:01:32 +000074#endif
reed@android.comda449a32009-09-18 20:57:05 +000075
reed@android.comed881c22009-09-15 14:10:42 +000076static void scale_to_width(SkPath* path, SkScalar dstWidth) {
77 const SkRect& bounds = path->getBounds();
78 SkScalar scale = dstWidth / bounds.width();
79 SkMatrix matrix;
80
81 matrix.setScale(scale, scale);
82 path->transform(matrix);
83}
84
85static const struct {
86 SkPaint::Style fStyle;
87 SkPaint::Join fJoin;
88 int fStrokeWidth;
89} gRec[] = {
90 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
91 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
92 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
93 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
94};
95
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040096class StrokePathView : public Sample {
reed@android.comed881c22009-09-15 14:10:42 +000097 SkScalar fWidth;
98 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -080099protected:
mtklein36352bf2015-03-25 18:17:31 -0700100 void onOnceBeforeDraw() override {
reed@android.com7ab2cf92009-09-21 16:01:32 +0000101// test_blur();
reed@android.comed881c22009-09-15 14:10:42 +0000102 fWidth = SkIntToScalar(120);
103
104#if 0
105 const char str[] =
106 "M 0, 3"
107 "C 10, -10, 30, -10, 0, 28"
108 "C -30, -10, -10, -10, 0, 3"
109 "Z";
110 SkParsePath::FromSVGString(str, &fPath);
111#else
112 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
113 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
114#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.comed881c22009-09-15 14:10:42 +0000116 scale_to_width(&fPath, fWidth);
117 const SkRect& bounds = fPath.getBounds();
118 fPath.offset(-bounds.fLeft, -bounds.fTop);
reed@google.com81e3d7f2011-06-01 12:42:36 +0000119
120 this->setBGColor(0xFFDDDDDD);
reed@android.comed881c22009-09-15 14:10:42 +0000121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400123 bool onQuery(Sample::Event* evt) override {
124 if (Sample::TitleQ(*evt)) {
125 Sample::TitleR(evt, "StrokePath");
reed@android.comed881c22009-09-15 14:10:42 +0000126 return true;
127 }
128 return this->INHERITED::onQuery(evt);
129 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000131 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.comed881c22009-09-15 14:10:42 +0000133 void drawSet(SkCanvas* canvas, SkPaint* paint) {
134 SkAutoCanvasRestore acr(canvas, true);
135
136 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
137 paint->setStyle(gRec[i].fStyle);
138 paint->setStrokeJoin(gRec[i].fJoin);
139 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
140 canvas->drawPath(fPath, *paint);
141 canvas->translate(fWidth * 5 / 4, 0);
142 }
143 }
144
mtklein36352bf2015-03-25 18:17:31 -0700145 void onDrawContent(SkCanvas* canvas) override {
reed@android.com04d86c62010-01-25 22:02:44 +0000146 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000147 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
148
149 SkPaint paint;
150 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.comda449a32009-09-18 20:57:05 +0000152 if (true) {
153 canvas->drawColor(SK_ColorBLACK);
154
155 paint.setTextSize(24);
156 paint.setColor(SK_ColorWHITE);
157 canvas->translate(10, 30);
158
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000159 static const SkBlurStyle gStyle[] = {
160 kNormal_SkBlurStyle,
161 kInner_SkBlurStyle,
162 kOuter_SkBlurStyle,
163 kSolid_SkBlurStyle,
reed@android.comda449a32009-09-18 20:57:05 +0000164 };
165 for (int x = 0; x < 5; x++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000166 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000167 for (int y = 0; y < 10; y++) {
168 if (x) {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400169 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
reed@android.comda449a32009-09-18 20:57:05 +0000170 }
Cary Clark2a475ea2017-04-28 15:35:12 -0400171 canvas->drawString("Title Bar", x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000172 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000173 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.comda449a32009-09-18 20:57:05 +0000175 }
176 return;
177 }
178
reed@android.comed881c22009-09-15 14:10:42 +0000179 paint.setColor(SK_ColorBLUE);
180
181#if 1
182 SkPath p;
183 float r = rand.nextUScalar1() + 0.5f;
184 SkScalar x = 0, y = 0;
185 p.moveTo(x, y);
186#if 0
187 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
188 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
189#else
190 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
191 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
192#endif
193 p.close();
194 fPath = p;
195 fPath.offset(100, 0);
196#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@android.comed881c22009-09-15 14:10:42 +0000198 fPath.setFillType(SkPath::kWinding_FillType);
199 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
reed@android.comed881c22009-09-15 14:10:42 +0000201 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
202 fPath.setFillType(SkPath::kEvenOdd_FillType);
203 drawSet(canvas, &paint);
204 }
205
reed@android.comed881c22009-09-15 14:10:42 +0000206private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400207 typedef Sample INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000208};
209
210//////////////////////////////////////////////////////////////////////////////
211
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400212DEF_SAMPLE( return new StrokePathView(); )