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/SkTArray.h" 22 #include "include/utils/SkRandom.h" 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: ShapesGM(const char * name,bool antialias)33 ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) { 34 if (!antialias) { 35 fName.append("_bw"); 36 } 37 } 38 onShortName()39 SkString onShortName() override final { return fName; } onISize()40 SkISize onISize() override { return SkISize::Make(500, 500); } 41 onOnceBeforeDraw()42 void onOnceBeforeDraw() override { 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 74 fPaint.setAntiAlias(fAntialias); 75 } 76 onDraw(SkCanvas * canvas)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: 97 typedef GM INHERITED; 98 }; 99 100 class SimpleShapesGM : public ShapesGM { 101 public: SimpleShapesGM(bool antialias)102 SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {} 103 104 private: drawShapes(SkCanvas * canvas) const105 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); 110 paint.setAlphaf(0.5f); // Use alpha to detect double blends. 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 129 typedef ShapesGM INHERITED; 130 }; 131 132 class InnerShapesGM : public ShapesGM { 133 public: InnerShapesGM(bool antialias)134 InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {} 135 136 private: drawShapes(SkCanvas * canvas) const137 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 * std::min(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); 156 paint.setAlphaf(0.5f); // Use alpha to detect double blends. 157 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 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 174 } 175