blob: 2cf1e83c723af2f36ea24bf40ce6154c36b6f099 [file] [log] [blame]
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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 Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/effects/SkDashPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000018
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include <utility>
20
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000021static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
Mike Reedb746d5c2020-08-02 18:56:34 -040022 return SkPath::Rect(SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000023}
24
25static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
Mike Reedb746d5c2020-08-02 18:56:34 -040026 return SkPath::Rect(SkRect::MakeXYWH(cx - l / 2, cy, l, 0));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000027}
28
29static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
Mike Reedb746d5c2020-08-02 18:56:34 -040030 return SkPath::Circle(cx, cy, d/2, SkPathDirection::kCW);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000031}
32
33static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
Mike Reedb746d5c2020-08-02 18:56:34 -040034 return SkPath::Line({cx - l / 2, cy}, {cx + l / 2, cy});
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000035}
36
halcanary2a243382015-09-09 08:16:41 -070037namespace {
bsalomon687d9d22016-06-10 12:09:59 -070038struct 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.org92ffe7d2013-07-31 22:54:31 +000044};
bsalomon687d9d22016-06-10 12:09:59 -070045
46sk_sp<SkPathEffect> make_dash() {
mtkleindbfd7ab2016-09-01 11:24:54 -070047 constexpr SkScalar kIntervals[] = { 4.f, 3.f };
bsalomon687d9d22016-06-10 12:09:59 -070048 return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0);
49}
50
51Style styles[] {
52 {SkPaint::kStroke_Style},
53 {SkPaint::kStrokeAndFill_Style},
54 {SkPaint::kFill_Style},
55 {SkPaint::kStroke_Style, make_dash()},
56};
57
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000058SkScalar pathSizes[] = {
59 40,
60 10,
61 0
62};
63SkScalar strokeWidths[] = {
64 10,
65 0
66};
Ben Wagner1a1d2412017-10-17 17:33:44 -040067SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000068 generate_square,
69 generate_rect_line,
70 generate_circle,
71 generate_line
72};
73
74const SkScalar slideWidth = 90, slideHeight = 90;
75const SkScalar slideBoundary = 5;
76
halcanary2a243382015-09-09 08:16:41 -070077} // namespace
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000078
bsalomon687d9d22016-06-10 12:09:59 -070079DEF_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.org92ffe7d2013-07-31 22:54:31 +000084
bsalomon687d9d22016-06-10 12:09:59 -070085 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.org92ffe7d2013-07-31 22:54:31 +000091
bsalomon687d9d22016-06-10 12:09:59 -070092 SkPaint outlinePaint;
93 outlinePaint.setColor(0x40000000);
94 outlinePaint.setStyle(SkPaint::kStroke_Style);
95 outlinePaint.setStrokeWidth(SkIntToScalar(0));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000096
bsalomon687d9d22016-06-10 12:09:59 -070097 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.org92ffe7d2013-07-31 22:54:31 +0000102
bsalomon687d9d22016-06-10 12:09:59 -0700103 canvas->save();
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000104
bsalomon687d9d22016-06-10 12:09:59 -0700105 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.org92ffe7d2013-07-31 22:54:31 +0000113
bsalomon687d9d22016-06-10 12:09:59 -0700114 for (size_t pathIndex = 0;
115 pathIndex < SK_ARRAY_COUNT(paths);
116 pathIndex++) {
117 canvas->drawRect(clipRect, clipPaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000118
bsalomon687d9d22016-06-10 12:09:59 -0700119 canvas->save();
120 canvas->clipRect(clipRect);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000121
bsalomon687d9d22016-06-10 12:09:59 -0700122 SkPath path = paths[pathIndex](cx, cy, size);
Mike Reed7d34dc72019-11-26 12:17:17 -0500123 path.setFillType(SkPathFillType::kInverseWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700124 canvas->drawPath(path, paint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000125
Mike Reed7d34dc72019-11-26 12:17:17 -0500126 path.setFillType(SkPathFillType::kWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700127 canvas->drawPath(path, outlinePaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000128
bsalomon687d9d22016-06-10 12:09:59 -0700129 canvas->restore();
130 canvas->translate(dx, 0);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000131 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000132 }
bsalomon687d9d22016-06-10 12:09:59 -0700133 canvas->restore();
134 canvas->translate(0, dy);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000135 }
bsalomon687d9d22016-06-10 12:09:59 -0700136 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000137}