blob: 3eb6af1cea1f445e2a1bbf9ac232376a299d6af5 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
Hal Canary4484b8f2019-01-08 14:00:08 -05009
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
reed@android.comed881c22009-09-15 14:10:42 +000011#include "SkCanvas.h"
Hal Canary4484b8f2019-01-08 14:00:08 -050012#include "SkFont.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040013#include "SkMaskFilter.h"
reed@android.comed881c22009-09-15 14:10:42 +000014#include "SkParsePath.h"
15#include "SkPath.h"
16#include "SkRandom.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
38 SkMatrix matrix;
39 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
40 canvas->concat(matrix);
41
42 canvas->drawPath(path, paint);
43}
44
reed@android.com7ab2cf92009-09-21 16:01:32 +000045#if 0
reed@android.com4913b772009-09-21 00:27:39 +000046static void test_blur() {
47 uint8_t cell[9];
48 memset(cell, 0xFF, sizeof(cell));
49 SkMask src;
50 src.fImage = cell;
51 src.fFormat = SkMask::kA8_Format;
52 SkMask dst;
53
54 for (int y = 1; y <= 3; y++) {
55 for (int x = 1; x <= 3; x++) {
56 src.fBounds.set(0, 0, x, y);
57 src.fRowBytes = src.fBounds.width();
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.com4913b772009-09-21 00:27:39 +000059 SkScalar radius = 1.f;
60
61 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
62 src.fBounds.fRight, src.fBounds.fBottom, radius);
63
64 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
65 uint8_t* dstPtr = dst.fImage;
66
67 for (int y = 0; y < dst.fBounds.height(); y++) {
68 for (int x = 0; x < dst.fBounds.width(); x++) {
69 printf(" %02X", dstPtr[x]);
70 }
71 printf("\n");
72 dstPtr += dst.fRowBytes;
73 }
74 }
75 }
76}
reed@android.com7ab2cf92009-09-21 16:01:32 +000077#endif
reed@android.comda449a32009-09-18 20:57:05 +000078
reed@android.comed881c22009-09-15 14:10:42 +000079static void scale_to_width(SkPath* path, SkScalar dstWidth) {
80 const SkRect& bounds = path->getBounds();
81 SkScalar scale = dstWidth / bounds.width();
82 SkMatrix matrix;
83
84 matrix.setScale(scale, scale);
85 path->transform(matrix);
86}
87
88static const struct {
89 SkPaint::Style fStyle;
90 SkPaint::Join fJoin;
91 int fStrokeWidth;
92} gRec[] = {
93 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
94 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
95 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
96 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
97};
98
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040099class StrokePathView : public Sample {
reed@android.comed881c22009-09-15 14:10:42 +0000100 SkScalar fWidth;
101 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -0800102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 void onOnceBeforeDraw() override {
reed@android.com7ab2cf92009-09-21 16:01:32 +0000104// test_blur();
reed@android.comed881c22009-09-15 14:10:42 +0000105 fWidth = SkIntToScalar(120);
106
107#if 0
108 const char str[] =
109 "M 0, 3"
110 "C 10, -10, 30, -10, 0, 28"
111 "C -30, -10, -10, -10, 0, 3"
112 "Z";
113 SkParsePath::FromSVGString(str, &fPath);
114#else
115 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
116 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
117#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
reed@android.comed881c22009-09-15 14:10:42 +0000119 scale_to_width(&fPath, fWidth);
120 const SkRect& bounds = fPath.getBounds();
121 fPath.offset(-bounds.fLeft, -bounds.fTop);
reed@google.com81e3d7f2011-06-01 12:42:36 +0000122
123 this->setBGColor(0xFFDDDDDD);
reed@android.comed881c22009-09-15 14:10:42 +0000124 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400126 bool onQuery(Sample::Event* evt) override {
127 if (Sample::TitleQ(*evt)) {
128 Sample::TitleR(evt, "StrokePath");
reed@android.comed881c22009-09-15 14:10:42 +0000129 return true;
130 }
131 return this->INHERITED::onQuery(evt);
132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000134 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@android.comed881c22009-09-15 14:10:42 +0000136 void drawSet(SkCanvas* canvas, SkPaint* paint) {
137 SkAutoCanvasRestore acr(canvas, true);
138
139 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
140 paint->setStyle(gRec[i].fStyle);
141 paint->setStrokeJoin(gRec[i].fJoin);
142 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
143 canvas->drawPath(fPath, *paint);
144 canvas->translate(fWidth * 5 / 4, 0);
145 }
146 }
147
mtklein36352bf2015-03-25 18:17:31 -0700148 void onDrawContent(SkCanvas* canvas) override {
reed@android.com04d86c62010-01-25 22:02:44 +0000149 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000150 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
151
152 SkPaint paint;
153 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@android.comda449a32009-09-18 20:57:05 +0000155 if (true) {
156 canvas->drawColor(SK_ColorBLACK);
157
Hal Canary4484b8f2019-01-08 14:00:08 -0500158 SkFont font(nullptr, 24);
reed@android.comda449a32009-09-18 20:57:05 +0000159 paint.setColor(SK_ColorWHITE);
160 canvas->translate(10, 30);
161
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000162 static const SkBlurStyle gStyle[] = {
163 kNormal_SkBlurStyle,
164 kInner_SkBlurStyle,
165 kOuter_SkBlurStyle,
166 kSolid_SkBlurStyle,
reed@android.comda449a32009-09-18 20:57:05 +0000167 };
168 for (int x = 0; x < 5; x++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000169 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000170 for (int y = 0; y < 10; y++) {
171 if (x) {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400172 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
reed@android.comda449a32009-09-18 20:57:05 +0000173 }
Hal Canary4484b8f2019-01-08 14:00:08 -0500174 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000175 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@android.comda449a32009-09-18 20:57:05 +0000178 }
179 return;
180 }
181
reed@android.comed881c22009-09-15 14:10:42 +0000182 paint.setColor(SK_ColorBLUE);
183
184#if 1
185 SkPath p;
186 float r = rand.nextUScalar1() + 0.5f;
187 SkScalar x = 0, y = 0;
188 p.moveTo(x, y);
189#if 0
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#else
193 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
194 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
195#endif
196 p.close();
197 fPath = p;
198 fPath.offset(100, 0);
199#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
reed@android.comed881c22009-09-15 14:10:42 +0000201 fPath.setFillType(SkPath::kWinding_FillType);
202 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@android.comed881c22009-09-15 14:10:42 +0000204 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
205 fPath.setFillType(SkPath::kEvenOdd_FillType);
206 drawSet(canvas, &paint);
207 }
208
reed@android.comed881c22009-09-15 14:10:42 +0000209private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400210 typedef Sample INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000211};
212
213//////////////////////////////////////////////////////////////////////////////
214
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400215DEF_SAMPLE( return new StrokePathView(); )