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