blob: 63809cea759a24a2bbb399fd4ee0900f315a1ae9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed881c22009-09-15 14:10:42 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
reed@android.comed881c22009-09-15 14:10:42 +000011#include "SkCanvas.h"
12#include "SkParsePath.h"
13#include "SkPath.h"
14#include "SkRandom.h"
15#include "SkView.h"
16
reed@android.com4913b772009-09-21 00:27:39 +000017
reed@android.com04d86c62010-01-25 22:02:44 +000018static void test_huge_stroke(SkCanvas* canvas) {
19 SkRect srcR = { 0, 0, 72000, 54000 };
20 SkRect dstR = { 0, 0, 640, 480 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
reed@android.com04d86c62010-01-25 22:02:44 +000022 SkPath path;
23 path.moveTo(17600, 8000);
24 path.lineTo(52800, 8000);
25 path.lineTo(52800, 41600);
26 path.lineTo(17600, 41600);
27 path.close();
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com04d86c62010-01-25 22:02:44 +000029 SkPaint paint;
30 paint.setAntiAlias(true);
31 paint.setStrokeWidth(8000);
32 paint.setStrokeMiter(10);
33 paint.setStrokeCap(SkPaint::kButt_Cap);
34 paint.setStrokeJoin(SkPaint::kRound_Join);
35 paint.setStyle(SkPaint::kStroke_Style);
36
37 SkMatrix matrix;
38 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
39 canvas->concat(matrix);
40
41 canvas->drawPath(path, paint);
42}
43
reed@android.com7ab2cf92009-09-21 16:01:32 +000044#if 0
reed@android.com4913b772009-09-21 00:27:39 +000045static void test_blur() {
46 uint8_t cell[9];
47 memset(cell, 0xFF, sizeof(cell));
48 SkMask src;
49 src.fImage = cell;
50 src.fFormat = SkMask::kA8_Format;
51 SkMask dst;
52
53 for (int y = 1; y <= 3; y++) {
54 for (int x = 1; x <= 3; x++) {
55 src.fBounds.set(0, 0, x, y);
56 src.fRowBytes = src.fBounds.width();
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reed@android.com4913b772009-09-21 00:27:39 +000058 SkScalar radius = 1.f;
59
60 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
61 src.fBounds.fRight, src.fBounds.fBottom, radius);
62
63 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
64 uint8_t* dstPtr = dst.fImage;
65
66 for (int y = 0; y < dst.fBounds.height(); y++) {
67 for (int x = 0; x < dst.fBounds.width(); x++) {
68 printf(" %02X", dstPtr[x]);
69 }
70 printf("\n");
71 dstPtr += dst.fRowBytes;
72 }
73 }
74 }
75}
reed@android.com7ab2cf92009-09-21 16:01:32 +000076#endif
reed@android.comda449a32009-09-18 20:57:05 +000077
reed@android.comed881c22009-09-15 14:10:42 +000078static void scale_to_width(SkPath* path, SkScalar dstWidth) {
79 const SkRect& bounds = path->getBounds();
80 SkScalar scale = dstWidth / bounds.width();
81 SkMatrix matrix;
82
83 matrix.setScale(scale, scale);
84 path->transform(matrix);
85}
86
87static const struct {
88 SkPaint::Style fStyle;
89 SkPaint::Join fJoin;
90 int fStrokeWidth;
91} gRec[] = {
92 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
93 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
94 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
95 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
96};
97
reed@google.com81e3d7f2011-06-01 12:42:36 +000098class StrokePathView : public SampleView {
reed@android.comed881c22009-09-15 14:10:42 +000099 SkScalar fWidth;
100 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -0800101protected:
mtklein36352bf2015-03-25 18:17:31 -0700102 void onOnceBeforeDraw() override {
reed@android.com7ab2cf92009-09-21 16:01:32 +0000103// test_blur();
reed@android.comed881c22009-09-15 14:10:42 +0000104 fWidth = SkIntToScalar(120);
105
106#if 0
107 const char str[] =
108 "M 0, 3"
109 "C 10, -10, 30, -10, 0, 28"
110 "C -30, -10, -10, -10, 0, 3"
111 "Z";
112 SkParsePath::FromSVGString(str, &fPath);
113#else
114 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
115 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
116#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed@android.comed881c22009-09-15 14:10:42 +0000118 scale_to_width(&fPath, fWidth);
119 const SkRect& bounds = fPath.getBounds();
120 fPath.offset(-bounds.fLeft, -bounds.fTop);
reed@google.com81e3d7f2011-06-01 12:42:36 +0000121
122 this->setBGColor(0xFFDDDDDD);
reed@android.comed881c22009-09-15 14:10:42 +0000123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.comed881c22009-09-15 14:10:42 +0000125 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700126 bool onQuery(SkEvent* evt) override {
reed@android.comed881c22009-09-15 14:10:42 +0000127 if (SampleCode::TitleQ(*evt)) {
128 SampleCode::TitleR(evt, "StrokePath");
129 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
158 paint.setTextSize(24);
159 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++) {
169 SkMaskFilter* mf;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000170 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000171 for (int y = 0; y < 10; y++) {
172 if (x) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000173 mf = SkBlurMaskFilter::Create(gStyle[x - 1], sigma);
reed@android.comda449a32009-09-18 20:57:05 +0000174 paint.setMaskFilter(mf)->unref();
175 }
reed@google.com261b8e22011-04-14 17:53:24 +0000176 canvas->drawText("Title Bar", 9, x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000177 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000178 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@android.comda449a32009-09-18 20:57:05 +0000180 }
181 return;
182 }
183
reed@android.comed881c22009-09-15 14:10:42 +0000184 paint.setColor(SK_ColorBLUE);
185
186#if 1
187 SkPath p;
188 float r = rand.nextUScalar1() + 0.5f;
189 SkScalar x = 0, y = 0;
190 p.moveTo(x, y);
191#if 0
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#else
195 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
196 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
197#endif
198 p.close();
199 fPath = p;
200 fPath.offset(100, 0);
201#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000202
reed@android.comed881c22009-09-15 14:10:42 +0000203 fPath.setFillType(SkPath::kWinding_FillType);
204 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000205
reed@android.comed881c22009-09-15 14:10:42 +0000206 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
207 fPath.setFillType(SkPath::kEvenOdd_FillType);
208 drawSet(canvas, &paint);
209 }
210
reed@google.com4d5c26d2013-01-08 16:17:50 +0000211 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700212 unsigned modi) override {
reed@android.comed881c22009-09-15 14:10:42 +0000213 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000214 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comed881c22009-09-15 14:10:42 +0000215 }
216private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000217 typedef SampleView INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000218};
219
220//////////////////////////////////////////////////////////////////////////////
221
222static SkView* MyFactory() { return new StrokePathView; }
223static SkViewRegister reg(MyFactory);