blob: b22ba8d3b5379ae28d9ff7fca24f8be3c76605f2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "samplecode/Sample.h"
Hal Canary4484b8f2019-01-08 14:00:08 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkMaskFilter.h"
13#include "include/core/SkPath.h"
14#include "include/utils/SkParsePath.h"
15#include "include/utils/SkRandom.h"
16#include "src/core/SkBlurMask.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
Mike Reed30bc5272019-11-22 18:34:02 +0000115 fPath.addCircle(0, 0, SkIntToScalar(50), SkPathDirection::kCW);
116 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPathDirection::kCW);
reed@android.comed881c22009-09-15 14:10:42 +0000117#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
Hal Canary8a027312019-07-03 10:55:44 -0400126 SkString name() override { return SkString("StrokePath"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000128 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.comed881c22009-09-15 14:10:42 +0000130 void drawSet(SkCanvas* canvas, SkPaint* paint) {
131 SkAutoCanvasRestore acr(canvas, true);
132
133 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
134 paint->setStyle(gRec[i].fStyle);
135 paint->setStrokeJoin(gRec[i].fJoin);
136 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
137 canvas->drawPath(fPath, *paint);
138 canvas->translate(fWidth * 5 / 4, 0);
139 }
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDrawContent(SkCanvas* canvas) override {
reed@android.com04d86c62010-01-25 22:02:44 +0000143 test_huge_stroke(canvas); return;
reed@android.comed881c22009-09-15 14:10:42 +0000144 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
145
146 SkPaint paint;
147 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.comda449a32009-09-18 20:57:05 +0000149 if (true) {
150 canvas->drawColor(SK_ColorBLACK);
151
Hal Canary4484b8f2019-01-08 14:00:08 -0500152 SkFont font(nullptr, 24);
reed@android.comda449a32009-09-18 20:57:05 +0000153 paint.setColor(SK_ColorWHITE);
154 canvas->translate(10, 30);
155
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000156 static const SkBlurStyle gStyle[] = {
157 kNormal_SkBlurStyle,
158 kInner_SkBlurStyle,
159 kOuter_SkBlurStyle,
160 kSolid_SkBlurStyle,
reed@android.comda449a32009-09-18 20:57:05 +0000161 };
162 for (int x = 0; x < 5; x++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000163 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
reed@android.comda449a32009-09-18 20:57:05 +0000164 for (int y = 0; y < 10; y++) {
165 if (x) {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400166 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
reed@android.comda449a32009-09-18 20:57:05 +0000167 }
Hal Canary4484b8f2019-01-08 14:00:08 -0500168 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000169 sigma *= 0.75f;
reed@android.comda449a32009-09-18 20:57:05 +0000170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@android.comda449a32009-09-18 20:57:05 +0000172 }
173 return;
174 }
175
reed@android.comed881c22009-09-15 14:10:42 +0000176 paint.setColor(SK_ColorBLUE);
177
178#if 1
179 SkPath p;
180 float r = rand.nextUScalar1() + 0.5f;
181 SkScalar x = 0, y = 0;
182 p.moveTo(x, y);
183#if 0
184 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
185 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
186#else
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#endif
190 p.close();
191 fPath = p;
192 fPath.offset(100, 0);
193#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
Mike Reed7d34dc72019-11-26 12:17:17 -0500195 fPath.setFillType(SkPathFillType::kWinding);
reed@android.comed881c22009-09-15 14:10:42 +0000196 drawSet(canvas, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@android.comed881c22009-09-15 14:10:42 +0000198 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
Mike Reed7d34dc72019-11-26 12:17:17 -0500199 fPath.setFillType(SkPathFillType::kEvenOdd);
reed@android.comed881c22009-09-15 14:10:42 +0000200 drawSet(canvas, &paint);
201 }
202
reed@android.comed881c22009-09-15 14:10:42 +0000203private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400204 typedef Sample INHERITED;
reed@android.comed881c22009-09-15 14:10:42 +0000205};
206
207//////////////////////////////////////////////////////////////////////////////
208
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400209DEF_SAMPLE( return new StrokePathView(); )