blob: edfe4bd90d4210a3b4846bdc6b712c1ad31c7f20 [file] [log] [blame]
cdalton4a6e40d2016-02-10 14:54:21 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#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/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/utils/SkRandom.h"
cdalton4a6e40d2016-02-10 14:54:21 -080023
24namespace skiagm {
25
26/*
27 * This is the base class for two GMs that cover various corner cases with primitive Skia shapes
28 * (zero radius, near-zero radius, inner shape overlap, etc.) It uses an xfermode of darken to help
29 * double-blended and/or dropped pixels stand out.
30 */
31class ShapesGM : public GM {
32protected:
33 ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) {
Cary Clark58627cb2018-04-10 09:16:41 -040034 if (!antialias) {
35 fName.append("_bw");
36 }
37 }
38
39 SkString onShortName() override final { return fName; }
40 SkISize onISize() override { return SkISize::Make(500, 500); }
41
42 void onOnceBeforeDraw() override {
cdalton4a6e40d2016-02-10 14:54:21 -080043 fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100));
44 fRotations.push_back(21);
45
46 fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100));
47 fRotations.push_back(94);
48
49 fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f, 1e-5f);
50 fRotations.push_back(132);
51
52 fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 15);
53 fRotations.push_back(282);
54
55 fSimpleShapeCount = fShapes.count();
56
57 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10, 5, 25, 35);
58 fRotations.push_back(0);
59
60 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10, 60, 50, 30);
61 fRotations.push_back(-35);
62
63 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1, 89, 59, 1);
64 fRotations.push_back(65);
65
66 SkVector radii[4] = {{4, 6}, {12, 8}, {24, 16}, {32, 48}};
67 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(150, -129, 80, 160), radii);
68 fRotations.push_back(265);
69
70 SkVector radii2[4] = {{0, 0}, {80, 60}, {0, 0}, {80, 60}};
71 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(180, -30, 80, 60), radii2);
72 fRotations.push_back(295);
73
cdalton4a6e40d2016-02-10 14:54:21 -080074 fPaint.setAntiAlias(fAntialias);
75 }
76
77 void onDraw(SkCanvas* canvas) override {
78 canvas->clear(SK_ColorWHITE);
79
80 canvas->save();
81 canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo().height() / 2.f);
82 this->drawShapes(canvas);
83 canvas->restore();
84 }
85
86 virtual void drawShapes(SkCanvas* canvas) const = 0;
87
88protected:
89 SkString fName;
90 bool fAntialias;
91 SkPaint fPaint;
92 SkTArray<SkRRect> fShapes;
93 SkTArray<SkScalar> fRotations;
94 int fSimpleShapeCount;
95
96private:
97 typedef GM INHERITED;
98};
99
100class SimpleShapesGM : public ShapesGM {
101public:
102 SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {}
103
104private:
105 void drawShapes(SkCanvas* canvas) const override {
106 SkRandom rand(2);
107 for (int i = 0; i < fShapes.count(); i++) {
108 SkPaint paint(fPaint);
109 paint.setColor(rand.nextU() & ~0x808080);
Mike Reed9407e242019-02-15 16:13:57 -0500110 paint.setAlphaf(0.5f); // Use alpha to detect double blends.
cdalton4a6e40d2016-02-10 14:54:21 -0800111 const SkRRect& shape = fShapes[i];
112 canvas->save();
113 canvas->rotate(fRotations[i]);
114 switch (shape.getType()) {
115 case SkRRect::kRect_Type:
116 canvas->drawRect(shape.rect(), paint);
117 break;
118 case SkRRect::kOval_Type:
119 canvas->drawOval(shape.rect(), paint);
120 break;
121 default:
122 canvas->drawRRect(shape, paint);
123 break;
124 }
125 canvas->restore();
126 }
127 }
128
129 typedef ShapesGM INHERITED;
130};
131
132class InnerShapesGM : public ShapesGM {
133public:
134 InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {}
135
136private:
137 void drawShapes(SkCanvas* canvas) const override {
138 SkRandom rand;
139 for (int i = 0; i < fShapes.count(); i++) {
140 const SkRRect& outer = fShapes[i];
141 const SkRRect& inner = fShapes[(i * 7 + 11) % fSimpleShapeCount];
142 float s = 0.95f * SkTMin(outer.rect().width() / inner.rect().width(),
143 outer.rect().height() / inner.rect().height());
144 SkMatrix innerXform;
145 float dx = (rand.nextF() - 0.5f) * (outer.rect().width() - s * inner.rect().width());
146 float dy = (rand.nextF() - 0.5f) * (outer.rect().height() - s * inner.rect().height());
147 innerXform.setTranslate(outer.rect().centerX() + dx, outer.rect().centerY() + dy);
148 if (s < 1) {
149 innerXform.preScale(s, s);
150 }
151 innerXform.preTranslate(-inner.rect().centerX(), -inner.rect().centerY());
152 SkRRect xformedInner;
153 inner.transform(innerXform, &xformedInner);
154 SkPaint paint(fPaint);
155 paint.setColor(rand.nextU() & ~0x808080);
Mike Reed9407e242019-02-15 16:13:57 -0500156 paint.setAlphaf(0.5f); // Use alpha to detect double blends.
cdalton4a6e40d2016-02-10 14:54:21 -0800157 canvas->save();
158 canvas->rotate(fRotations[i]);
159 canvas->drawDRRect(outer, xformedInner, paint);
160 canvas->restore();
161 }
162 }
163
164 typedef ShapesGM INHERITED;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169DEF_GM( return new SimpleShapesGM(true); )
170DEF_GM( return new SimpleShapesGM(false); )
171DEF_GM( return new InnerShapesGM(true); )
172DEF_GM( return new InnerShapesGM(false); )
173
174}