cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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/SkTArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/utils/SkRandom.h" |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 23 | |
| 24 | namespace 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 | */ |
| 31 | class ShapesGM : public GM { |
| 32 | protected: |
| 33 | ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) { |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 34 | if (!antialias) { |
| 35 | fName.append("_bw"); |
| 36 | } |
| 37 | } |
| 38 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 39 | SkString onShortName() final { return fName; } |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 40 | SkISize onISize() override { return SkISize::Make(500, 500); } |
| 41 | |
| 42 | void onOnceBeforeDraw() override { |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 43 | 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 | |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 74 | 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 | |
| 88 | protected: |
| 89 | SkString fName; |
| 90 | bool fAntialias; |
| 91 | SkPaint fPaint; |
| 92 | SkTArray<SkRRect> fShapes; |
| 93 | SkTArray<SkScalar> fRotations; |
| 94 | int fSimpleShapeCount; |
| 95 | |
| 96 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 97 | using INHERITED = GM; |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class SimpleShapesGM : public ShapesGM { |
| 101 | public: |
| 102 | SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {} |
| 103 | |
| 104 | private: |
| 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 Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 110 | paint.setAlphaf(0.5f); // Use alpha to detect double blends. |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 111 | 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 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 129 | using INHERITED = ShapesGM; |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | class InnerShapesGM : public ShapesGM { |
| 133 | public: |
| 134 | InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {} |
| 135 | |
| 136 | private: |
| 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]; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 142 | float s = 0.95f * std::min(outer.rect().width() / inner.rect().width(), |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 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 Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 156 | paint.setAlphaf(0.5f); // Use alpha to detect double blends. |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 157 | canvas->save(); |
| 158 | canvas->rotate(fRotations[i]); |
| 159 | canvas->drawDRRect(outer, xformedInner, paint); |
| 160 | canvas->restore(); |
| 161 | } |
| 162 | } |
| 163 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 164 | using INHERITED = ShapesGM; |
cdalton | 4a6e40d | 2016-02-10 14:54:21 -0800 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | ////////////////////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | DEF_GM( return new SimpleShapesGM(true); ) |
| 170 | DEF_GM( return new SimpleShapesGM(false); ) |
| 171 | DEF_GM( return new InnerShapesGM(true); ) |
| 172 | DEF_GM( return new InnerShapesGM(false); ) |
| 173 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 174 | } // namespace skiagm |