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
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkRRect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/utils/SkRandom.h"
20
21 namespace skiagm {
22
23 // Test out various combinations of nested rects, ovals and rrects.
24 class NestedGM : public GM {
25 public:
NestedGM(bool doAA,bool flipped)26 NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
27 this->setBGColor(0xFFDDDDDD);
28 }
29
30 protected:
31
onShortName()32 SkString onShortName() override {
33 SkString name("nested");
34 if (fFlipped) {
35 name.append("_flipY");
36 }
37 if (fDoAA) {
38 name.append("_aa");
39 } else {
40 name.append("_bw");
41 }
42 return name;
43 }
44
onISize()45 SkISize onISize() override {
46 return SkISize::Make(kImageWidth, kImageHeight);
47 }
48
49 enum Shapes {
50 kRect_Shape = 0,
51 kRRect_Shape,
52 kOval_Shape,
53 kShapeCount
54 };
55
AddShape(SkPathBuilder * b,const SkRect & rect,Shapes shape,SkPathDirection dir)56 static void AddShape(SkPathBuilder* b, const SkRect& rect, Shapes shape, SkPathDirection dir) {
57 switch (shape) {
58 case kRect_Shape:
59 b->addRect(rect, dir);
60 break;
61 case kRRect_Shape: {
62 SkRRect rr;
63 rr.setRectXY(rect, 5, 5);
64 b->addRRect(rr, dir);
65 break;
66 }
67 case kOval_Shape:
68 b->addOval(rect, dir);
69 break;
70 default:
71 break;
72 }
73 }
74
onDraw(SkCanvas * canvas)75 void onDraw(SkCanvas* canvas) override {
76
77 SkPaint shapePaint;
78 shapePaint.setColor(SK_ColorBLACK);
79 shapePaint.setAntiAlias(fDoAA);
80
81 SkRect outerRect = SkRect::MakeWH(40, 40);
82
83 SkRect innerRects[] = {
84 { 10, 10, 30, 30 }, // small
85 { .5f, 18, 4.5f, 22 } // smaller and offset to left
86 };
87
88 // draw a background pattern to make transparency errors more apparent
89 SkRandom rand;
90
91 for (int y = 0; y < kImageHeight; y += 10) {
92 for (int x = 0; x < kImageWidth; x += 10) {
93 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
94 SkIntToScalar(y),
95 10, 10);
96 SkPaint p;
97 p.setColor(rand.nextU() | 0xFF000000);
98 canvas->drawRect(r, p);
99 }
100 }
101
102 SkScalar xOff = 2, yOff = 2;
103 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
104 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
105 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
106 SkPathBuilder builder;
107
108 AddShape(&builder, outerRect, (Shapes) outerShape, SkPathDirection::kCW);
109 AddShape(&builder, innerRects[innerRect], (Shapes) innerShape,
110 SkPathDirection::kCCW);
111
112 canvas->save();
113 if (fFlipped) {
114 canvas->scale(1.0f, -1.0f);
115 canvas->translate(xOff, -yOff - 40.0f);
116 } else {
117 canvas->translate(xOff, yOff);
118 }
119
120 canvas->drawPath(builder.detach(), shapePaint);
121 canvas->restore();
122
123 xOff += 45;
124 }
125 }
126
127 xOff = 2;
128 yOff += 45;
129 }
130
131 }
132
133 private:
134 inline static constexpr int kImageWidth = 269;
135 inline static constexpr int kImageHeight = 134;
136
137 bool fDoAA;
138 bool fFlipped;
139
140 using INHERITED = GM;
141 };
142
143 ///////////////////////////////////////////////////////////////////////////////
144
145 DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
DEF_GM(return new NestedGM (false,false);)146 DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
147 DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
148 DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
149
150 DEF_SIMPLE_GM(nested_hairline_square, canvas, 64, 64) {
151 // See crbug.com/1234194 - This should draw 1 row of 3 stroked squares, with a second 0.5px
152 // shifted row of squares below it.
__anon7641eed20102() 153 auto drawEllipses = [&]() {
154 canvas->save();
155 // Originally the SVG string "M5,14H0V9h5V14Z M1,13h3v-3H1V13Z" but that just specifies a
156 // 5px wide square outside a 3px wide square.
157 SkPath square;
158 square.addRect(SkRect::MakeLTRB(0.f, 9.f, 5.f, 14.f));
159 square.addRect(SkRect::MakeLTRB(1.f, 10.f, 4.f, 13.f), SkPathDirection::kCCW);
160
161 // From the bug, SVG viewbox was (0, 0, 24, 24), so the above coordinates are relative to
162 // that, but the svg was then the child of a div that was 16x16, so it's scaled down. This
163 // converts the 1px wide nested rects into subpixel nested rects.
164 canvas->scale(16.f / 24.f, 16.f / 24.f);
165
166 SkPaint paint;
167 paint.setColor(SkColorSetARGB(255, 70, 70, 70));
168 paint.setAntiAlias(true);
169
170 // The original SVG drew 3 separate paths, but these were just translations of the original
171 // path baked into a path string.
172 canvas->drawPath(square, paint);
173 canvas->translate(10.f, 0.f);
174 canvas->drawPath(square, paint);
175 canvas->translate(10.f, 0.f);
176 canvas->drawPath(square, paint);
177
178 canvas->restore();
179 };
180
181 drawEllipses();
182 canvas->translate(0.5f, 16.f);
183 drawEllipses();
184 }
185
186 } // namespace skiagm
187