Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shapes.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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"
16#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
22#include "src/base/SkRandom.h"
23
24using namespace skia_private;
25
26namespace skiagm {
27
28/*
29 * This is the base class for two GMs that cover various corner cases with primitive Skia shapes
30 * (zero radius, near-zero radius, inner shape overlap, etc.) It uses an xfermode of darken to help
31 * double-blended and/or dropped pixels stand out.
32 */
33class ShapesGM : public GM {
34protected:
35 ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) {
36 if (!antialias) {
37 fName.append("_bw");
38 }
39 }
40
41 SkString getName() const override { return fName; }
42 SkISize getISize() override { return SkISize::Make(500, 500); }
43
44 void onOnceBeforeDraw() override {
45 fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100));
47
48 fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100));
50
51 fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f, 1e-5f);
53
54 fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 15);
56
58
59 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10, 5, 25, 35);
61
62 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10, 60, 50, 30);
64
65 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1, 89, 59, 1);
67
68 SkVector radii[4] = {{4, 6}, {12, 8}, {24, 16}, {32, 48}};
69 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(150, -129, 80, 160), radii);
71
72 SkVector radii2[4] = {{0, 0}, {80, 60}, {0, 0}, {80, 60}};
73 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(180, -30, 80, 60), radii2);
75
77 }
78
79 void onDraw(SkCanvas* canvas) override {
80 canvas->clear(SK_ColorWHITE);
81
82 canvas->save();
83 canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo().height() / 2.f);
84 this->drawShapes(canvas);
85 canvas->restore();
86 }
87
88 virtual void drawShapes(SkCanvas* canvas) const = 0;
89
90protected:
97
98private:
99 using INHERITED = GM;
100};
101
102class SimpleShapesGM : public ShapesGM {
103public:
104 SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {}
105
106private:
107 void drawShapes(SkCanvas* canvas) const override {
108 SkRandom rand(2);
109 for (int i = 0; i < fShapes.size(); i++) {
111 paint.setColor(rand.nextU() & ~0x808080);
112 paint.setAlphaf(0.5f); // Use alpha to detect double blends.
113 const SkRRect& shape = fShapes[i];
114 canvas->save();
115 canvas->rotate(fRotations[i]);
116 switch (shape.getType()) {
118 canvas->drawRect(shape.rect(), paint);
119 break;
121 canvas->drawOval(shape.rect(), paint);
122 break;
123 default:
124 canvas->drawRRect(shape, paint);
125 break;
126 }
127 canvas->restore();
128 }
129 }
130
131 using INHERITED = ShapesGM;
132};
133
134class InnerShapesGM : public ShapesGM {
135public:
136 InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {}
137
138private:
139 void drawShapes(SkCanvas* canvas) const override {
140 SkRandom rand;
141 for (int i = 0; i < fShapes.size(); i++) {
142 const SkRRect& outer = fShapes[i];
143 const SkRRect& inner = fShapes[(i * 7 + 11) % fSimpleShapeCount];
144 float s = 0.95f * std::min(outer.rect().width() / inner.rect().width(),
145 outer.rect().height() / inner.rect().height());
146 SkMatrix innerXform;
147 float dx = (rand.nextF() - 0.5f) * (outer.rect().width() - s * inner.rect().width());
148 float dy = (rand.nextF() - 0.5f) * (outer.rect().height() - s * inner.rect().height());
149 // Fixup inner rects so they don't reach outside the outer rect.
150 switch (i) {
151 case 0:
152 s *= .85f;
153 break;
154 case 8:
155 s *= .4f;
156 dx = dy = 0;
157 break;
158 case 5:
159 s *= .75f;
160 dx = dy = 0;
161 break;
162 case 6:
163 s *= .65f;
164 dx = -5;
165 dy = 10;
166 break;
167 }
168 innerXform.setTranslate(outer.rect().centerX() + dx, outer.rect().centerY() + dy);
169 if (s < 1) {
170 innerXform.preScale(s, s);
171 }
172 innerXform.preTranslate(-inner.rect().centerX(), -inner.rect().centerY());
173 SkRRect xformedInner;
174 inner.transform(innerXform, &xformedInner);
176 paint.setColor(rand.nextU() & ~0x808080);
177 paint.setAlphaf(0.5f); // Use alpha to detect double blends.
178 canvas->save();
179 canvas->rotate(fRotations[i]);
180 canvas->drawDRRect(outer, xformedInner, paint);
181 canvas->restore();
182 }
183 }
184
185 using INHERITED = ShapesGM;
186};
187
188//////////////////////////////////////////////////////////////////////////////
189
190DEF_GM( return new SimpleShapesGM(true); )
191DEF_GM( return new SimpleShapesGM(false); )
192DEF_GM( return new InnerShapesGM(true); )
193DEF_GM( return new InnerShapesGM(false); )
194
195} // namespace skiagm
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawOval(const SkRect &oval, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
void rotate(SkScalar degrees)
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
int save()
Definition SkCanvas.cpp:451
void drawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
SkImageInfo imageInfo() const
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:254
SkMatrix & preTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:263
SkMatrix & preScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:315
void setAntiAlias(bool aa)
Definition SkPaint.h:170
Type getType() const
Definition SkRRect.h:76
const SkRect & rect() const
Definition SkRRect.h:264
@ kOval_Type
non-zero width and height filled with radii
Definition SkRRect.h:69
@ kRect_Type
non-zero width and height, and zeroed radii
Definition SkRRect.h:68
bool transform(const SkMatrix &matrix, SkRRect *dst) const
Definition SkRRect.cpp:436
void setOval(const SkRect &oval)
Definition SkRRect.cpp:30
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Definition SkRRect.cpp:189
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition SkRRect.cpp:52
void setNinePatch(const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
Definition SkRRect.cpp:115
void setRect(const SkRect &rect)
Definition SkRRect.h:126
uint32_t nextU()
Definition SkRandom.h:42
float nextF()
Definition SkRandom.h:55
void append(const char text[])
Definition SkString.h:203
int size() const
Definition SkTArray.h:416
void drawShapes(SkCanvas *canvas) const override
Definition shapes.cpp:139
InnerShapesGM(bool antialias)
Definition shapes.cpp:136
SkPaint fPaint
Definition shapes.cpp:93
SkString getName() const override
Definition shapes.cpp:41
SkString fName
Definition shapes.cpp:91
virtual void drawShapes(SkCanvas *canvas) const =0
ShapesGM(const char *name, bool antialias)
Definition shapes.cpp:35
int fSimpleShapeCount
Definition shapes.cpp:96
void onDraw(SkCanvas *canvas) override
Definition shapes.cpp:79
TArray< SkScalar > fRotations
Definition shapes.cpp:95
void onOnceBeforeDraw() override
Definition shapes.cpp:44
SkISize getISize() override
Definition shapes.cpp:42
TArray< SkRRect > fShapes
Definition shapes.cpp:94
SimpleShapesGM(bool antialias)
Definition shapes.cpp:104
void drawShapes(SkCanvas *canvas) const override
Definition shapes.cpp:107
const Paint & paint
struct MyStruct s
const char * name
Definition fuchsia.cc:50
#define DEF_GM(CODE)
Definition gm.h:40
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
int width() const
int height() const
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
constexpr float centerX() const
Definition SkRect.h:776
constexpr float height() const
Definition SkRect.h:769
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762