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