epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Kevin Lubick | 1ef156b | 2022-11-29 12:07:14 -0500 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPoint.h" |
| 14 | #include "include/core/SkScalar.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 17 | #include "src/base/SkRandom.h" |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 18 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 21 | namespace skiagm { |
| 22 | |
| 23 | class PointsGM : public GM { |
| 24 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | PointsGM() {} |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 26 | |
| 27 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 28 | SkString getName() const override { return SkString("points"); } |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 29 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 30 | SkISize getISize() override { return SkISize::Make(640, 490); } |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 31 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 32 | static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 33 | for (size_t i = 0; i < n; i++) { |
| 34 | // Compute these independently and store in variables, rather |
| 35 | // than in the parameter-passing expression, to get consistent |
| 36 | // evaluation order across compilers. |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 37 | SkScalar y = rand->nextUScalar1() * 480; |
| 38 | SkScalar x = rand->nextUScalar1() * 640; |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 39 | pts[i].set(x, y); |
| 40 | } |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 41 | } |
| 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 44 | canvas->translate(SK_Scalar1, SK_Scalar1); |
| 45 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 46 | SkRandom rand; |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 47 | SkPaint p0, p1, p2, p3; |
| 48 | const size_t n = 99; |
| 49 | |
| 50 | p0.setColor(SK_ColorRED); |
| 51 | p1.setColor(SK_ColorGREEN); |
| 52 | p2.setColor(SK_ColorBLUE); |
| 53 | p3.setColor(SK_ColorWHITE); |
| 54 | |
| 55 | p0.setStrokeWidth(SkIntToScalar(4)); |
| 56 | p2.setStrokeCap(SkPaint::kRound_Cap); |
| 57 | p2.setStrokeWidth(SkIntToScalar(6)); |
| 58 | |
| 59 | SkPoint* pts = new SkPoint[n]; |
| 60 | fill_pts(pts, n, &rand); |
| 61 | |
| 62 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, n, pts, p0); |
| 63 | canvas->drawPoints(SkCanvas::kLines_PointMode, n, pts, p1); |
| 64 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p2); |
| 65 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p3); |
| 66 | |
| 67 | delete[] pts; |
| 68 | } |
| 69 | |
| 70 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 71 | using INHERITED = GM; |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 72 | }; |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 73 | DEF_GM( return new PointsGM; ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 74 | } // namespace skiagm |
Mike Reed | b1559f2 | 2021-09-13 14:19:03 -0400 | [diff] [blame] | 75 | |
| 76 | #include "include/core/SkMaskFilter.h" |
| 77 | |
| 78 | DEF_SIMPLE_GM(points_maskfilter, canvas, 512, 256) { |
| 79 | constexpr int N = 30; |
| 80 | SkPoint pts[N]; |
| 81 | |
| 82 | SkRandom rand; |
| 83 | for (SkPoint& p : pts) { |
| 84 | p.fX = rand.nextF() * 220 + 18; |
| 85 | p.fY = rand.nextF() * 220 + 18; |
| 86 | } |
| 87 | |
| 88 | auto mf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 6); |
| 89 | const SkPaint::Cap caps[] = { SkPaint::kSquare_Cap, SkPaint::kRound_Cap }; |
| 90 | |
| 91 | SkPaint paint; |
| 92 | paint.setAntiAlias(true); |
| 93 | paint.setStroke(true); |
| 94 | paint.setStrokeWidth(10); |
| 95 | |
| 96 | for (auto cap : caps) { |
| 97 | paint.setStrokeCap(cap); |
| 98 | |
| 99 | paint.setMaskFilter(mf); |
| 100 | paint.setColor(SK_ColorBLACK); |
| 101 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint); |
| 102 | |
| 103 | paint.setMaskFilter(nullptr); |
| 104 | paint.setColor(SK_ColorRED); |
| 105 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint); |
| 106 | |
| 107 | canvas->translate(256, 0); |
| 108 | } |
| 109 | } |