blob: ce6a010347658b024f1b7ec691cca96011b8d1c1 [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;
101public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102 StrokePathView() {
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 +0000125protected:
126 // overrides from SkEventSink
127 virtual bool onQuery(SkEvent* evt) {
128 if (SampleCode::TitleQ(*evt)) {
129 SampleCode::TitleR(evt, "StrokePath");
130 return true;
131 }
132 return this->INHERITED::onQuery(evt);
133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000135 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.comed881c22009-09-15 14:10:42 +0000137 void drawSet(SkCanvas* canvas, SkPaint* paint) {
138 SkAutoCanvasRestore acr(canvas, true);
139
140 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
141 paint->setStyle(gRec[i].fStyle);
142 paint->setStrokeJoin(gRec[i].fJoin);
143 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
144 canvas->drawPath(fPath, *paint);
145 canvas->translate(fWidth * 5 / 4, 0);
146 }
147 }
148
reed@google.com81e3d7f2011-06-01 12:42:36 +0000149 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com04d86c62010-01-25 22:02:44 +0000150 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000151 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
152
153 SkPaint paint;
154 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.comda449a32009-09-18 20:57:05 +0000156 if (true) {
157 canvas->drawColor(SK_ColorBLACK);
158
159 paint.setTextSize(24);
160 paint.setColor(SK_ColorWHITE);
161 canvas->translate(10, 30);
162
163 static const SkBlurMaskFilter::BlurStyle gStyle[] = {
164 SkBlurMaskFilter::kNormal_BlurStyle,
165 SkBlurMaskFilter::kInner_BlurStyle,
166 SkBlurMaskFilter::kOuter_BlurStyle,
167 SkBlurMaskFilter::kSolid_BlurStyle,
168 };
169 for (int x = 0; x < 5; x++) {
170 SkMaskFilter* mf;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000171 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000172 for (int y = 0; y < 10; y++) {
173 if (x) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000174 mf = SkBlurMaskFilter::Create(gStyle[x - 1], sigma);
reed@android.comda449a32009-09-18 20:57:05 +0000175 paint.setMaskFilter(mf)->unref();
176 }
reed@google.com261b8e22011-04-14 17:53:24 +0000177 canvas->drawText("Title Bar", 9, x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000178 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000179 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed@android.comda449a32009-09-18 20:57:05 +0000181 }
182 return;
183 }
184
reed@android.comed881c22009-09-15 14:10:42 +0000185 paint.setColor(SK_ColorBLUE);
186
187#if 1
188 SkPath p;
189 float r = rand.nextUScalar1() + 0.5f;
190 SkScalar x = 0, y = 0;
191 p.moveTo(x, y);
192#if 0
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#else
196 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
197 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
198#endif
199 p.close();
200 fPath = p;
201 fPath.offset(100, 0);
202#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@android.comed881c22009-09-15 14:10:42 +0000204 fPath.setFillType(SkPath::kWinding_FillType);
205 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@android.comed881c22009-09-15 14:10:42 +0000207 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
208 fPath.setFillType(SkPath::kEvenOdd_FillType);
209 drawSet(canvas, &paint);
210 }
211
reed@google.com4d5c26d2013-01-08 16:17:50 +0000212 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
213 unsigned modi) SK_OVERRIDE {
reed@android.comed881c22009-09-15 14:10:42 +0000214 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000215 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comed881c22009-09-15 14:10:42 +0000216 }
217private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000218 typedef SampleView INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000219};
220
221//////////////////////////////////////////////////////////////////////////////
222
223static SkView* MyFactory() { return new StrokePathView; }
224static SkViewRegister reg(MyFactory);