commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) { |
| 15 | SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w); |
| 16 | SkPath path; |
| 17 | path.addRect(rect); |
| 18 | return path; |
| 19 | } |
| 20 | |
| 21 | static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 22 | SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0); |
| 23 | SkPath path; |
| 24 | path.addRect(rect); |
| 25 | return path; |
| 26 | } |
| 27 | |
| 28 | static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) { |
| 29 | SkPath path; |
| 30 | path.addCircle(cx, cy, d/2, SkPath::kCW_Direction); |
| 31 | return path; |
| 32 | } |
| 33 | |
| 34 | static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 35 | SkPath path; |
| 36 | path.moveTo(cx - l / 2, cy); |
| 37 | path.lineTo(cx + l / 2, cy); |
| 38 | return path; |
| 39 | } |
| 40 | |
| 41 | SkPaint::Style styles[] = { |
| 42 | SkPaint::kStroke_Style, |
| 43 | SkPaint::kStrokeAndFill_Style, |
| 44 | SkPaint::kFill_Style |
| 45 | }; |
| 46 | SkScalar pathSizes[] = { |
| 47 | 40, |
| 48 | 10, |
| 49 | 0 |
| 50 | }; |
| 51 | SkScalar strokeWidths[] = { |
| 52 | 10, |
| 53 | 0 |
| 54 | }; |
| 55 | SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = { |
| 56 | generate_square, |
| 57 | generate_rect_line, |
| 58 | generate_circle, |
| 59 | generate_line |
| 60 | }; |
| 61 | |
| 62 | const SkScalar slideWidth = 90, slideHeight = 90; |
| 63 | const SkScalar slideBoundary = 5; |
| 64 | |
| 65 | |
| 66 | class InversePathsGM : public GM { |
| 67 | public: |
| 68 | InversePathsGM() { |
| 69 | |
| 70 | } |
| 71 | |
| 72 | protected: |
| 73 | virtual SkString onShortName() { |
| 74 | return SkString("inverse_paths"); |
| 75 | } |
| 76 | |
| 77 | virtual SkISize onISize() { |
| 78 | return make_isize(800, 900); |
| 79 | } |
| 80 | |
| 81 | virtual void onDraw(SkCanvas* canvas) { |
| 82 | SkScalar cx = slideWidth / 2 + slideBoundary; |
| 83 | SkScalar cy = slideHeight / 2 + slideBoundary; |
| 84 | SkScalar dx = slideWidth + 2 * slideBoundary; |
| 85 | SkScalar dy = slideHeight + 2 * slideBoundary; |
| 86 | |
| 87 | SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary, |
| 88 | slideBoundary + slideWidth, |
| 89 | slideBoundary + slideHeight); |
| 90 | SkPaint clipPaint; |
| 91 | clipPaint.setStyle(SkPaint::kStroke_Style); |
| 92 | clipPaint.setStrokeWidth(SkIntToScalar(2)); |
| 93 | |
| 94 | SkPaint outlinePaint; |
| 95 | outlinePaint.setColor(0x40000000); |
| 96 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 97 | outlinePaint.setStrokeWidth(SkIntToScalar(0)); |
| 98 | |
| 99 | for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles); |
| 100 | styleIndex++) { |
| 101 | for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes); |
| 102 | sizeIndex++) { |
| 103 | SkScalar size = pathSizes[sizeIndex]; |
| 104 | |
| 105 | canvas->save(); |
| 106 | |
| 107 | for (size_t widthIndex = 0; |
| 108 | widthIndex < SK_ARRAY_COUNT(strokeWidths); |
| 109 | widthIndex++) { |
| 110 | SkPaint paint; |
| 111 | paint.setColor(0xff007000); |
| 112 | paint.setStrokeWidth(strokeWidths[widthIndex]); |
| 113 | paint.setStyle(styles[styleIndex]); |
| 114 | |
| 115 | for (size_t pathIndex = 0; |
| 116 | pathIndex < SK_ARRAY_COUNT(paths); |
| 117 | pathIndex++) { |
| 118 | canvas->drawRect(clipRect, clipPaint); |
| 119 | |
| 120 | canvas->save(); |
| 121 | canvas->clipRect(clipRect); |
| 122 | |
| 123 | SkPath path = paths[pathIndex](cx, cy, size); |
| 124 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 125 | canvas->drawPath(path, paint); |
| 126 | |
| 127 | path.setFillType(SkPath::kWinding_FillType); |
| 128 | canvas->drawPath(path, outlinePaint); |
| 129 | |
| 130 | canvas->restore(); |
| 131 | canvas->translate(dx, 0); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | canvas->restore(); |
| 136 | canvas->translate(0, dy); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | typedef GM INHERITED; |
| 143 | }; |
| 144 | |
| 145 | DEF_GM( return new InversePathsGM; ) |
| 146 | } |