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" |
Kevin Lubick | 1ef156b | 2022-11-29 12:07:14 -0500 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Michael Ludwig | 0b124c4 | 2022-02-15 12:29:58 -0500 | [diff] [blame] | 11 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPathEffect.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/effects/SkDashPathEffect.h" |
Michael Ludwig | 0b124c4 | 2022-02-15 12:29:58 -0500 | [diff] [blame] | 20 | #include "include/effects/SkImageFilters.h" |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 21 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 24 | static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) { |
Mike Reed | b746d5c | 2020-08-02 18:56:34 -0400 | [diff] [blame] | 25 | 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] | 26 | } |
| 27 | |
| 28 | static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) { |
Mike Reed | b746d5c | 2020-08-02 18:56:34 -0400 | [diff] [blame] | 29 | 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] | 30 | } |
| 31 | |
| 32 | static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) { |
Mike Reed | b746d5c | 2020-08-02 18:56:34 -0400 | [diff] [blame] | 33 | return SkPath::Circle(cx, cy, d/2, SkPathDirection::kCW); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) { |
Mike Reed | b746d5c | 2020-08-02 18:56:34 -0400 | [diff] [blame] | 37 | 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] | 38 | } |
| 39 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 40 | namespace { |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 41 | struct Style { |
| 42 | Style(SkPaint::Style paintStyle, sk_sp<SkPathEffect> pe = sk_sp<SkPathEffect>()) |
| 43 | : fPaintStyle(paintStyle) |
| 44 | , fPathEffect(std::move(pe)) {} |
| 45 | SkPaint::Style fPaintStyle; |
| 46 | sk_sp<SkPathEffect> fPathEffect; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 47 | }; |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 48 | |
| 49 | sk_sp<SkPathEffect> make_dash() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 50 | constexpr SkScalar kIntervals[] = { 4.f, 3.f }; |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 51 | return SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | Style styles[] { |
| 55 | {SkPaint::kStroke_Style}, |
| 56 | {SkPaint::kStrokeAndFill_Style}, |
| 57 | {SkPaint::kFill_Style}, |
| 58 | {SkPaint::kStroke_Style, make_dash()}, |
| 59 | }; |
| 60 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 61 | SkScalar pathSizes[] = { |
| 62 | 40, |
| 63 | 10, |
| 64 | 0 |
| 65 | }; |
| 66 | SkScalar strokeWidths[] = { |
| 67 | 10, |
| 68 | 0 |
| 69 | }; |
Ben Wagner | 1a1d241 | 2017-10-17 17:33:44 -0400 | [diff] [blame] | 70 | SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 71 | generate_square, |
| 72 | generate_rect_line, |
| 73 | generate_circle, |
| 74 | generate_line |
| 75 | }; |
| 76 | |
| 77 | const SkScalar slideWidth = 90, slideHeight = 90; |
| 78 | const SkScalar slideBoundary = 5; |
| 79 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 80 | } // namespace |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 81 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 82 | DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) { |
| 83 | SkScalar cx = slideWidth / 2 + slideBoundary; |
| 84 | SkScalar cy = slideHeight / 2 + slideBoundary; |
| 85 | SkScalar dx = slideWidth + 2 * slideBoundary; |
| 86 | SkScalar dy = slideHeight + 2 * slideBoundary; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 87 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 88 | SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary, |
| 89 | slideBoundary + slideWidth, |
| 90 | slideBoundary + slideHeight); |
| 91 | SkPaint clipPaint; |
| 92 | clipPaint.setStyle(SkPaint::kStroke_Style); |
| 93 | clipPaint.setStrokeWidth(SkIntToScalar(2)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 94 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 95 | SkPaint outlinePaint; |
| 96 | outlinePaint.setColor(0x40000000); |
| 97 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 98 | outlinePaint.setStrokeWidth(SkIntToScalar(0)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 99 | |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 100 | for (size_t styleIndex = 0; styleIndex < std::size(styles); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 101 | styleIndex++) { |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 102 | for (size_t sizeIndex = 0; sizeIndex < std::size(pathSizes); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 103 | sizeIndex++) { |
| 104 | SkScalar size = pathSizes[sizeIndex]; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 105 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 106 | canvas->save(); |
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 widthIndex = 0; |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 109 | widthIndex < std::size(strokeWidths); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 110 | widthIndex++) { |
| 111 | SkPaint paint; |
| 112 | paint.setColor(0xff007000); |
| 113 | paint.setStrokeWidth(strokeWidths[widthIndex]); |
| 114 | paint.setStyle(styles[styleIndex].fPaintStyle); |
| 115 | paint.setPathEffect(styles[styleIndex].fPathEffect); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 116 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 117 | for (size_t pathIndex = 0; |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 118 | pathIndex < std::size(paths); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 119 | pathIndex++) { |
| 120 | canvas->drawRect(clipRect, clipPaint); |
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 | canvas->save(); |
| 123 | canvas->clipRect(clipRect); |
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 | SkPath path = paths[pathIndex](cx, cy, size); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 126 | path.setFillType(SkPathFillType::kInverseWinding); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 127 | canvas->drawPath(path, paint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 128 | |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 129 | path.setFillType(SkPathFillType::kWinding); |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 130 | canvas->drawPath(path, outlinePaint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 131 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 132 | canvas->restore(); |
| 133 | canvas->translate(dx, 0); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 134 | } |
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 | canvas->restore(); |
| 137 | canvas->translate(0, dy); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 138 | } |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 139 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 140 | } |
Michael Ludwig | 0b124c4 | 2022-02-15 12:29:58 -0500 | [diff] [blame] | 141 | |
| 142 | DEF_SIMPLE_GM(inverse_fill_filters, canvas, 384, 128) { |
| 143 | auto draw = [canvas](const SkPaint& paint) { |
| 144 | SkPath path = SkPath::Circle(65.f, 65.f, 30.f); |
| 145 | path.setFillType(SkPathFillType::kInverseWinding); |
| 146 | |
| 147 | canvas->save(); |
| 148 | canvas->clipRect({0, 0, 128, 128}); |
| 149 | canvas->drawPath(path, paint); |
| 150 | canvas->restore(); |
| 151 | |
| 152 | SkPaint stroke; |
| 153 | stroke.setStyle(SkPaint::kStroke_Style); |
| 154 | stroke.setColor(SK_ColorWHITE); |
| 155 | canvas->drawRect({0, 0, 128, 128}, stroke); |
| 156 | }; |
| 157 | |
| 158 | SkPaint paint; |
| 159 | paint.setAntiAlias(true); |
| 160 | |
| 161 | draw(paint); |
| 162 | |
| 163 | canvas->translate(128, 0); |
| 164 | paint.setImageFilter(SkImageFilters::Blur(5.f, 5.f, nullptr)); |
| 165 | draw(paint); |
| 166 | |
| 167 | canvas->translate(128, 0); |
| 168 | paint.setImageFilter(nullptr); |
| 169 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 5)); |
| 170 | draw(paint); |
| 171 | } |
| 172 | |
| 173 | DEF_SIMPLE_GM(inverse_windingmode_filters, canvas, 256, 100) { |
| 174 | SkPath path; |
| 175 | path.addRect({10, 10, 30, 30}, SkPathDirection::kCW); |
| 176 | path.addRect({20, 20, 40, 40}, SkPathDirection::kCW); |
| 177 | path.addRect({10, 60, 30, 80}, SkPathDirection::kCW); |
| 178 | path.addRect({20, 70, 40, 90}, SkPathDirection::kCCW); |
| 179 | SkPaint strokePaint; |
| 180 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 181 | SkRect clipRect = {0, 0, 51, 99}; |
| 182 | canvas->drawPath(path, strokePaint); |
| 183 | SkPaint fillPaint; |
| 184 | fillPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 1.0f)); |
| 185 | for (auto fillType : { SkPathFillType::kWinding, |
| 186 | SkPathFillType::kEvenOdd, |
| 187 | SkPathFillType::kInverseWinding, |
| 188 | SkPathFillType::kInverseEvenOdd } ) { |
| 189 | canvas->translate(51, 0); |
| 190 | canvas->save(); |
| 191 | canvas->clipRect(clipRect); |
| 192 | path.setFillType(fillType); |
| 193 | canvas->drawPath(path, fillPaint); |
| 194 | canvas->restore(); |
| 195 | SkPaint clipPaint; |
| 196 | clipPaint.setColor(SK_ColorRED); |
| 197 | clipPaint.setStyle(SkPaint::kStroke_Style); |
| 198 | clipPaint.setStrokeWidth(1.f); |
| 199 | canvas->drawRect(clipRect, clipPaint); |
| 200 | } |
| 201 | } |