xref: /aosp_15_r20/external/skia/gm/shapes.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRRect.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypes.h"
21 #include "include/private/base/SkTArray.h"
22 #include "src/base/SkRandom.h"
23 
24 using namespace skia_private;
25 
26 namespace 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  */
33 class ShapesGM : public GM {
34 protected:
ShapesGM(const char * name,bool antialias)35     ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) {
36         if (!antialias) {
37             fName.append("_bw");
38         }
39     }
40 
getName() const41     SkString getName() const override { return fName; }
getISize()42     SkISize getISize() override { return SkISize::Make(500, 500); }
43 
onOnceBeforeDraw()44     void onOnceBeforeDraw() override {
45         fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100));
46         fRotations.push_back(21);
47 
48         fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100));
49         fRotations.push_back(94);
50 
51         fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f, 1e-5f);
52         fRotations.push_back(132);
53 
54         fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 15);
55         fRotations.push_back(282);
56 
57         fSimpleShapeCount = fShapes.size();
58 
59         fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10, 5, 25, 35);
60         fRotations.push_back(0);
61 
62         fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10, 60, 50, 30);
63         fRotations.push_back(-35);
64 
65         fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1, 89, 59, 1);
66         fRotations.push_back(65);
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);
70         fRotations.push_back(265);
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);
74         fRotations.push_back(295);
75 
76         fPaint.setAntiAlias(fAntialias);
77     }
78 
onDraw(SkCanvas * canvas)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 
90 protected:
91     SkString             fName;
92     bool                 fAntialias;
93     SkPaint              fPaint;
94     TArray<SkRRect>    fShapes;
95     TArray<SkScalar>   fRotations;
96     int                  fSimpleShapeCount;
97 
98 private:
99     using INHERITED = GM;
100 };
101 
102 class SimpleShapesGM : public ShapesGM {
103 public:
SimpleShapesGM(bool antialias)104     SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {}
105 
106 private:
drawShapes(SkCanvas * canvas) const107     void drawShapes(SkCanvas* canvas) const override {
108         SkRandom rand(2);
109         for (int i = 0; i < fShapes.size(); i++) {
110             SkPaint paint(fPaint);
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()) {
117                 case SkRRect::kRect_Type:
118                     canvas->drawRect(shape.rect(), paint);
119                     break;
120                 case SkRRect::kOval_Type:
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 
134 class InnerShapesGM : public ShapesGM {
135 public:
InnerShapesGM(bool antialias)136     InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {}
137 
138 private:
drawShapes(SkCanvas * canvas) const139     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);
175             SkPaint paint(fPaint);
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 
190 DEF_GM( return new SimpleShapesGM(true); )
191 DEF_GM( return new SimpleShapesGM(false); )
192 DEF_GM( return new InnerShapesGM(true); )
193 DEF_GM( return new InnerShapesGM(false); )
194 
195 }  // namespace skiagm
196