blob: 87a977a919e5e806bbb810e0877b313548fd63ef [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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11
12namespace skiagm {
13
14static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
15 SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w);
16 SkPath path;
17 path.addRect(rect);
18 return path;
19}
20
21static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
22 SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0);
23 SkPath path;
24 path.addRect(rect);
25 return path;
26}
27
28static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
29 SkPath path;
30 path.addCircle(cx, cy, d/2, SkPath::kCW_Direction);
31 return path;
32}
33
34static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
35 SkPath path;
36 path.moveTo(cx - l / 2, cy);
37 path.lineTo(cx + l / 2, cy);
38 return path;
39}
40
41SkPaint::Style styles[] = {
42 SkPaint::kStroke_Style,
43 SkPaint::kStrokeAndFill_Style,
44 SkPaint::kFill_Style
45};
46SkScalar pathSizes[] = {
47 40,
48 10,
49 0
50};
51SkScalar strokeWidths[] = {
52 10,
53 0
54};
55SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = {
56 generate_square,
57 generate_rect_line,
58 generate_circle,
59 generate_line
60};
61
62const SkScalar slideWidth = 90, slideHeight = 90;
63const SkScalar slideBoundary = 5;
64
65
66class InversePathsGM : public GM {
67public:
68 InversePathsGM() {
69
70 }
71
72protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000073
mtklein72c9faa2015-01-09 10:06:39 -080074 SkString onShortName() SK_OVERRIDE {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000075 return SkString("inverse_paths");
76 }
77
mtklein72c9faa2015-01-09 10:06:39 -080078 SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070079 return SkISize::Make(800, 900);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000080 }
81
mtklein72c9faa2015-01-09 10:06:39 -080082 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000083 SkScalar cx = slideWidth / 2 + slideBoundary;
84 SkScalar cy = slideHeight / 2 + slideBoundary;
85 SkScalar dx = slideWidth + 2 * slideBoundary;
86 SkScalar dy = slideHeight + 2 * slideBoundary;
87
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));
94
95 SkPaint outlinePaint;
96 outlinePaint.setColor(0x40000000);
97 outlinePaint.setStyle(SkPaint::kStroke_Style);
98 outlinePaint.setStrokeWidth(SkIntToScalar(0));
99
100 for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles);
101 styleIndex++) {
102 for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes);
103 sizeIndex++) {
104 SkScalar size = pathSizes[sizeIndex];
105
106 canvas->save();
107
108 for (size_t widthIndex = 0;
109 widthIndex < SK_ARRAY_COUNT(strokeWidths);
110 widthIndex++) {
111 SkPaint paint;
112 paint.setColor(0xff007000);
113 paint.setStrokeWidth(strokeWidths[widthIndex]);
114 paint.setStyle(styles[styleIndex]);
115
116 for (size_t pathIndex = 0;
117 pathIndex < SK_ARRAY_COUNT(paths);
118 pathIndex++) {
119 canvas->drawRect(clipRect, clipPaint);
120
121 canvas->save();
122 canvas->clipRect(clipRect);
123
124 SkPath path = paths[pathIndex](cx, cy, size);
125 path.setFillType(SkPath::kInverseWinding_FillType);
126 canvas->drawPath(path, paint);
127
128 path.setFillType(SkPath::kWinding_FillType);
129 canvas->drawPath(path, outlinePaint);
130
131 canvas->restore();
132 canvas->translate(dx, 0);
133 }
134 }
135
136 canvas->restore();
137 canvas->translate(0, dy);
138 }
139 }
140 }
141
142private:
143 typedef GM INHERITED;
144};
145
146DEF_GM( return new InversePathsGM; )
147}