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" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPoint.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/utils/SkRandom.h" |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 17 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include <stddef.h> |
| 19 | |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 20 | namespace skiagm { |
| 21 | |
| 22 | class PointsGM : public GM { |
| 23 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 24 | PointsGM() {} |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 25 | |
| 26 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 27 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | SkString onShortName() override { |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 29 | return SkString("points"); |
| 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 33 | return SkISize::Make(640, 490); |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 34 | } |
| 35 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 36 | static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 37 | for (size_t i = 0; i < n; i++) { |
| 38 | // Compute these independently and store in variables, rather |
| 39 | // than in the parameter-passing expression, to get consistent |
| 40 | // evaluation order across compilers. |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 41 | SkScalar y = rand->nextUScalar1() * 480; |
| 42 | SkScalar x = rand->nextUScalar1() * 640; |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 43 | pts[i].set(x, y); |
| 44 | } |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 48 | canvas->translate(SK_Scalar1, SK_Scalar1); |
| 49 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 50 | SkRandom rand; |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 51 | SkPaint p0, p1, p2, p3; |
| 52 | const size_t n = 99; |
| 53 | |
| 54 | p0.setColor(SK_ColorRED); |
| 55 | p1.setColor(SK_ColorGREEN); |
| 56 | p2.setColor(SK_ColorBLUE); |
| 57 | p3.setColor(SK_ColorWHITE); |
| 58 | |
| 59 | p0.setStrokeWidth(SkIntToScalar(4)); |
| 60 | p2.setStrokeCap(SkPaint::kRound_Cap); |
| 61 | p2.setStrokeWidth(SkIntToScalar(6)); |
| 62 | |
| 63 | SkPoint* pts = new SkPoint[n]; |
| 64 | fill_pts(pts, n, &rand); |
| 65 | |
| 66 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, n, pts, p0); |
| 67 | canvas->drawPoints(SkCanvas::kLines_PointMode, n, pts, p1); |
| 68 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p2); |
| 69 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p3); |
| 70 | |
| 71 | delete[] pts; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | typedef GM INHERITED; |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 80 | DEF_GM( return new PointsGM; ) |
reed@google.com | a965a15 | 2011-02-17 15:06:52 +0000 | [diff] [blame] | 81 | |
| 82 | } |