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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame^] | 10 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame^] | 12 | #include "include/core/SkPathEffect.h" |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkRefCnt.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/effects/SkDashPathEffect.h" |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 18 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame^] | 19 | #include <utility> |
| 20 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 21 | static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) { |
| 22 | SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w); |
| 23 | SkPath path; |
| 24 | path.addRect(rect); |
| 25 | return path; |
| 26 | } |
| 27 | |
| 28 | static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 29 | SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0); |
| 30 | SkPath path; |
| 31 | path.addRect(rect); |
| 32 | return path; |
| 33 | } |
| 34 | |
| 35 | static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) { |
| 36 | SkPath path; |
| 37 | path.addCircle(cx, cy, d/2, SkPath::kCW_Direction); |
| 38 | return path; |
| 39 | } |
| 40 | |
| 41 | static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 42 | SkPath path; |
| 43 | path.moveTo(cx - l / 2, cy); |
| 44 | path.lineTo(cx + l / 2, cy); |
| 45 | return path; |
| 46 | } |
| 47 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 48 | namespace { |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 49 | struct Style { |
| 50 | Style(SkPaint::Style paintStyle, sk_sp<SkPathEffect> pe = sk_sp<SkPathEffect>()) |
| 51 | : fPaintStyle(paintStyle) |
| 52 | , fPathEffect(std::move(pe)) {} |
| 53 | SkPaint::Style fPaintStyle; |
| 54 | sk_sp<SkPathEffect> fPathEffect; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 55 | }; |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 56 | |
| 57 | sk_sp<SkPathEffect> make_dash() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 58 | constexpr SkScalar kIntervals[] = { 4.f, 3.f }; |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 59 | return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0); |
| 60 | } |
| 61 | |
| 62 | Style styles[] { |
| 63 | {SkPaint::kStroke_Style}, |
| 64 | {SkPaint::kStrokeAndFill_Style}, |
| 65 | {SkPaint::kFill_Style}, |
| 66 | {SkPaint::kStroke_Style, make_dash()}, |
| 67 | }; |
| 68 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 69 | SkScalar pathSizes[] = { |
| 70 | 40, |
| 71 | 10, |
| 72 | 0 |
| 73 | }; |
| 74 | SkScalar strokeWidths[] = { |
| 75 | 10, |
| 76 | 0 |
| 77 | }; |
Ben Wagner | 1a1d241 | 2017-10-17 17:33:44 -0400 | [diff] [blame] | 78 | SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 79 | generate_square, |
| 80 | generate_rect_line, |
| 81 | generate_circle, |
| 82 | generate_line |
| 83 | }; |
| 84 | |
| 85 | const SkScalar slideWidth = 90, slideHeight = 90; |
| 86 | const SkScalar slideBoundary = 5; |
| 87 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 88 | } // namespace |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 89 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 90 | DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) { |
| 91 | SkScalar cx = slideWidth / 2 + slideBoundary; |
| 92 | SkScalar cy = slideHeight / 2 + slideBoundary; |
| 93 | SkScalar dx = slideWidth + 2 * slideBoundary; |
| 94 | SkScalar dy = slideHeight + 2 * slideBoundary; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 95 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 96 | SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary, |
| 97 | slideBoundary + slideWidth, |
| 98 | slideBoundary + slideHeight); |
| 99 | SkPaint clipPaint; |
| 100 | clipPaint.setStyle(SkPaint::kStroke_Style); |
| 101 | clipPaint.setStrokeWidth(SkIntToScalar(2)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 102 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 103 | SkPaint outlinePaint; |
| 104 | outlinePaint.setColor(0x40000000); |
| 105 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 106 | outlinePaint.setStrokeWidth(SkIntToScalar(0)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 107 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 108 | for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles); |
| 109 | styleIndex++) { |
| 110 | for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes); |
| 111 | sizeIndex++) { |
| 112 | SkScalar size = pathSizes[sizeIndex]; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 113 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 114 | canvas->save(); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 115 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 116 | for (size_t widthIndex = 0; |
| 117 | widthIndex < SK_ARRAY_COUNT(strokeWidths); |
| 118 | widthIndex++) { |
| 119 | SkPaint paint; |
| 120 | paint.setColor(0xff007000); |
| 121 | paint.setStrokeWidth(strokeWidths[widthIndex]); |
| 122 | paint.setStyle(styles[styleIndex].fPaintStyle); |
| 123 | paint.setPathEffect(styles[styleIndex].fPathEffect); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 124 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 125 | for (size_t pathIndex = 0; |
| 126 | pathIndex < SK_ARRAY_COUNT(paths); |
| 127 | pathIndex++) { |
| 128 | canvas->drawRect(clipRect, clipPaint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 129 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 130 | canvas->save(); |
| 131 | canvas->clipRect(clipRect); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 132 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 133 | SkPath path = paths[pathIndex](cx, cy, size); |
| 134 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 135 | canvas->drawPath(path, paint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 136 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 137 | path.setFillType(SkPath::kWinding_FillType); |
| 138 | canvas->drawPath(path, outlinePaint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 139 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 140 | canvas->restore(); |
| 141 | canvas->translate(dx, 0); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 142 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 143 | } |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 144 | canvas->restore(); |
| 145 | canvas->translate(0, dy); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 146 | } |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 147 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 148 | } |