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