blob: a6cd1bd83a4cbe408e8eefc8ef3e51784d2338a9 [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) {
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
28static 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
35static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
36 SkPath path;
Mike Reed30bc5272019-11-22 18:34:02 +000037 path.addCircle(cx, cy, d/2, SkPathDirection::kCW);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000038 return path;
39}
40
41static 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
halcanary2a243382015-09-09 08:16:41 -070048namespace {
bsalomon687d9d22016-06-10 12:09:59 -070049struct 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.org92ffe7d2013-07-31 22:54:31 +000055};
bsalomon687d9d22016-06-10 12:09:59 -070056
57sk_sp<SkPathEffect> make_dash() {
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr SkScalar kIntervals[] = { 4.f, 3.f };
bsalomon687d9d22016-06-10 12:09:59 -070059 return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0);
60}
61
62Style styles[] {
63 {SkPaint::kStroke_Style},
64 {SkPaint::kStrokeAndFill_Style},
65 {SkPaint::kFill_Style},
66 {SkPaint::kStroke_Style, make_dash()},
67};
68
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000069SkScalar pathSizes[] = {
70 40,
71 10,
72 0
73};
74SkScalar strokeWidths[] = {
75 10,
76 0
77};
Ben Wagner1a1d2412017-10-17 17:33:44 -040078SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000079 generate_square,
80 generate_rect_line,
81 generate_circle,
82 generate_line
83};
84
85const SkScalar slideWidth = 90, slideHeight = 90;
86const SkScalar slideBoundary = 5;
87
halcanary2a243382015-09-09 08:16:41 -070088} // namespace
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000089
bsalomon687d9d22016-06-10 12:09:59 -070090DEF_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.org92ffe7d2013-07-31 22:54:31 +000095
bsalomon687d9d22016-06-10 12:09:59 -070096 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.org92ffe7d2013-07-31 22:54:31 +0000102
bsalomon687d9d22016-06-10 12:09:59 -0700103 SkPaint outlinePaint;
104 outlinePaint.setColor(0x40000000);
105 outlinePaint.setStyle(SkPaint::kStroke_Style);
106 outlinePaint.setStrokeWidth(SkIntToScalar(0));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000107
bsalomon687d9d22016-06-10 12:09:59 -0700108 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.org92ffe7d2013-07-31 22:54:31 +0000113
bsalomon687d9d22016-06-10 12:09:59 -0700114 canvas->save();
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000115
bsalomon687d9d22016-06-10 12:09:59 -0700116 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.org92ffe7d2013-07-31 22:54:31 +0000124
bsalomon687d9d22016-06-10 12:09:59 -0700125 for (size_t pathIndex = 0;
126 pathIndex < SK_ARRAY_COUNT(paths);
127 pathIndex++) {
128 canvas->drawRect(clipRect, clipPaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000129
bsalomon687d9d22016-06-10 12:09:59 -0700130 canvas->save();
131 canvas->clipRect(clipRect);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000132
bsalomon687d9d22016-06-10 12:09:59 -0700133 SkPath path = paths[pathIndex](cx, cy, size);
Mike Reed7d34dc72019-11-26 12:17:17 -0500134 path.setFillType(SkPathFillType::kInverseWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700135 canvas->drawPath(path, paint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000136
Mike Reed7d34dc72019-11-26 12:17:17 -0500137 path.setFillType(SkPathFillType::kWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700138 canvas->drawPath(path, outlinePaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000139
bsalomon687d9d22016-06-10 12:09:59 -0700140 canvas->restore();
141 canvas->translate(dx, 0);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000142 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000143 }
bsalomon687d9d22016-06-10 12:09:59 -0700144 canvas->restore();
145 canvas->translate(0, dy);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000146 }
bsalomon687d9d22016-06-10 12:09:59 -0700147 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000148}