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