bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 1 | |
| 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 | */ |
| 8 | #include "gm.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "SkTArray.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class ConvexPathsGM : public GM { |
| 15 | public: |
| 16 | ConvexPathsGM() { |
| 17 | this->setBGColor(0xFFDDDDDD); |
| 18 | this->makePaths(); |
| 19 | } |
| 20 | |
| 21 | protected: |
| 22 | virtual SkString onShortName() { |
| 23 | return SkString("convexpaths"); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | virtual SkISize onISize() { |
| 28 | return make_isize(1200, 900); |
| 29 | } |
| 30 | |
| 31 | void makePaths() { |
| 32 | fPaths.push_back().addRect(0, 0, |
| 33 | 100 * SK_Scalar1, 100 * SK_Scalar1, |
| 34 | SkPath::kCW_Direction); |
| 35 | |
| 36 | fPaths.push_back().addRect(0, 0, |
| 37 | 100 * SK_Scalar1, 100 * SK_Scalar1, |
| 38 | SkPath::kCCW_Direction); |
| 39 | |
| 40 | fPaths.push_back().addCircle(50 * SK_Scalar1, 50 * SK_Scalar1, |
| 41 | 50 * SK_Scalar1, SkPath::kCW_Direction); |
| 42 | |
| 43 | fPaths.push_back().addCircle(50 * SK_Scalar1, 50 * SK_Scalar1, |
| 44 | 40 * SK_Scalar1, SkPath::kCCW_Direction); |
| 45 | |
| 46 | fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0, |
| 47 | 50 * SK_Scalar1, |
| 48 | 100 * SK_Scalar1), |
| 49 | SkPath::kCW_Direction); |
| 50 | |
| 51 | fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0, |
| 52 | 100 * SK_Scalar1, |
| 53 | 50 * SK_Scalar1), |
| 54 | SkPath::kCCW_Direction); |
| 55 | |
| 56 | fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0, |
| 57 | 100 * SK_Scalar1, |
| 58 | 5 * SK_Scalar1), |
| 59 | SkPath::kCCW_Direction); |
| 60 | |
| 61 | fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0, |
| 62 | SK_Scalar1, |
| 63 | 100 * SK_Scalar1), |
| 64 | SkPath::kCCW_Direction); |
| 65 | |
| 66 | fPaths.push_back().addRect(0, 0, |
| 67 | 100 * SK_Scalar1, 100 * SK_Scalar1, |
| 68 | SkPath::kCCW_Direction); |
| 69 | |
| 70 | fPaths.push_back().addRoundRect(SkRect::MakeXYWH(0, 0, |
| 71 | SK_Scalar1 * 100, |
| 72 | SK_Scalar1 * 100), |
| 73 | 40 * SK_Scalar1, 20 * SK_Scalar1, |
| 74 | SkPath::kCW_Direction); |
| 75 | |
| 76 | fPaths.push_back().addRoundRect(SkRect::MakeXYWH(0, 0, |
| 77 | SK_Scalar1 * 100, |
| 78 | SK_Scalar1 * 100), |
| 79 | 20 * SK_Scalar1, 40 * SK_Scalar1, |
| 80 | SkPath::kCCW_Direction); |
| 81 | /* |
bsalomon@google.com | 5cc90d1 | 2012-01-17 16:28:34 +0000 | [diff] [blame] | 82 | It turns out arcTos are not automatically marked as convex and they |
| 83 | may in fact be ever so slightly concave. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 84 | fPaths.push_back().arcTo(SkRect::MakeXYWH(0, 0, |
| 85 | 50 * SK_Scalar1, |
| 86 | 100 * SK_Scalar1), |
| 87 | 25 * SK_Scalar1, 130 * SK_Scalar1, false); |
| 88 | */ |
| 89 | |
| 90 | // point degenerate |
bsalomon@google.com | 5cc90d1 | 2012-01-17 16:28:34 +0000 | [diff] [blame] | 91 | fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1); |
| 92 | fPaths.back().lineTo(50 * SK_Scalar1, 50 * SK_Scalar1); |
| 93 | |
| 94 | fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1); |
| 95 | fPaths.back().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1, |
| 96 | 50 * SK_Scalar1, 50 * SK_Scalar1); |
| 97 | fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1); |
| 98 | fPaths.back().cubicTo(50 * SK_Scalar1, 50 * SK_Scalar1, |
| 99 | 50 * SK_Scalar1, 50 * SK_Scalar1, |
| 100 | 50 * SK_Scalar1, 50 * SK_Scalar1); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 101 | |
| 102 | // line degenerate |
| 103 | fPaths.push_back().lineTo(100 * SK_Scalar1, 100 * SK_Scalar1); |
| 104 | fPaths.push_back().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1, 0, 0); |
| 105 | fPaths.push_back().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1, |
| 106 | 50 * SK_Scalar1, 50 * SK_Scalar1); |
| 107 | fPaths.push_back().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1, |
| 108 | 100 * SK_Scalar1, 100 * SK_Scalar1); |
| 109 | fPaths.push_back().cubicTo(0, 0, |
| 110 | 0, 0, |
| 111 | 100 * SK_Scalar1, 100 * SK_Scalar1); |
| 112 | } |
| 113 | |
| 114 | virtual void onDraw(SkCanvas* canvas) { |
| 115 | |
| 116 | SkPaint paint; |
| 117 | paint.setAntiAlias(true); |
| 118 | SkRandom rand; |
| 119 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
| 120 | for (int i = 0 ; i < fPaths.count(); ++i) { |
| 121 | canvas->save(); |
| 122 | // position the path, and make it at off-integer coords. |
| 123 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 124 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 125 | SkColor color = rand.nextU(); |
| 126 | color |= 0xff000000; |
| 127 | paint.setColor(color); |
| 128 | SkASSERT(fPaths[i].isConvex()); |
| 129 | canvas->drawPath(fPaths[i], paint); |
| 130 | canvas->restore(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | typedef GM INHERITED; |
| 136 | SkTArray<SkPath> fPaths; |
| 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static GM* MyFactory(void*) { return new ConvexPathsGM; } |
| 142 | static GMRegistry reg(MyFactory); |
| 143 | |
| 144 | } |
| 145 | |