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