| OLD | NEW |
| (Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2016 Google Inc. |
| 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. |
| 7 */ |
| 8 |
| 9 #include "gm.h" |
| 10 #include "SkRandom.h" |
| 11 #include "SkRRect.h" |
| 12 |
| 13 namespace skiagm { |
| 14 |
| 15 class ShapeSet { |
| 16 public: |
| 17 ShapeSet() { |
| 18 fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100)); |
| 19 fRotations.push_back(21); |
| 20 |
| 21 fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100)); |
| 22 fRotations.push_back(94); |
| 23 |
| 24 fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f,
1e-5f); |
| 25 fRotations.push_back(132); |
| 26 |
| 27 fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 1
5); |
| 28 fRotations.push_back(282); |
| 29 |
| 30 fSimpleCount = fShapes.count(); |
| 31 |
| 32 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10
, 5, 25, 35); |
| 33 fRotations.push_back(0); |
| 34 |
| 35 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10,
60, 50, 30); |
| 36 fRotations.push_back(-35); |
| 37 |
| 38 fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1,
89, 59, 1); |
| 39 fRotations.push_back(65); |
| 40 |
| 41 SkVector radii[4] = {{4, 6}, {12, 8}, {24, 16}, {32, 48}}; |
| 42 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(150, -129, 80, 160), r
adii); |
| 43 fRotations.push_back(265); |
| 44 |
| 45 SkVector radii2[4] = {{0, 0}, {80, 60}, {0, 0}, {80, 60}}; |
| 46 fShapes.push_back().setRectRadii(SkRect::MakeXYWH(180, -30, 80, 60), rad
ii2); |
| 47 fRotations.push_back(295); |
| 48 } |
| 49 |
| 50 int count() const { |
| 51 SkASSERT(fShapes.count() == fRotations.count()); |
| 52 return fShapes.count(); |
| 53 } |
| 54 |
| 55 int simpleCount() const { |
| 56 return fSimpleCount; |
| 57 } |
| 58 |
| 59 const SkRRect& getShape(int idx) const { |
| 60 return fShapes[idx]; |
| 61 } |
| 62 |
| 63 SkScalar getRotation(int idx) const { |
| 64 return fRotations[idx]; |
| 65 } |
| 66 |
| 67 private: |
| 68 SkTArray<SkRRect> fShapes; |
| 69 SkTArray<SkScalar> fRotations; |
| 70 int fSimpleCount; |
| 71 }; |
| 72 |
| 73 class ShapesGM : public GM { |
| 74 protected: |
| 75 ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antiali
as) { |
| 76 if (!antialias) { |
| 77 fName.append("_bw"); |
| 78 } |
| 79 } |
| 80 |
| 81 SkString onShortName() override final { return fName; } |
| 82 SkISize onISize() override { return SkISize::Make(500, 500); } |
| 83 |
| 84 void onOnceBeforeDraw() override { |
| 85 fPaint.setXfermodeMode(SkXfermode::kDarken_Mode); |
| 86 fPaint.setAntiAlias(fAntialias); |
| 87 } |
| 88 |
| 89 void onDraw(SkCanvas* canvas) override { |
| 90 canvas->clear(SK_ColorWHITE); |
| 91 |
| 92 canvas->save(); |
| 93 canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo()
.height() / 2.f); |
| 94 this->drawShapes(canvas); |
| 95 canvas->restore(); |
| 96 } |
| 97 |
| 98 SkString fName; |
| 99 bool fAntialias; |
| 100 ShapeSet fShapes; |
| 101 SkPaint fPaint; |
| 102 |
| 103 virtual void drawShapes(SkCanvas* canvas) const = 0; |
| 104 |
| 105 private: |
| 106 typedef GM INHERITED; |
| 107 }; |
| 108 |
| 109 class SimpleShapesGM : public ShapesGM { |
| 110 public: |
| 111 SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {} |
| 112 |
| 113 private: |
| 114 void drawShapes(SkCanvas* canvas) const override { |
| 115 SkRandom rand(2); |
| 116 for (int i = 0; i < fShapes.count(); i++) { |
| 117 SkPaint paint(fPaint); |
| 118 paint.setColor(rand.nextU() & ~0x808080); |
| 119 paint.setAlpha(128); // Use alpha to detect double blends. |
| 120 const SkRRect& shape = fShapes.getShape(i); |
| 121 canvas->save(); |
| 122 canvas->rotate(fShapes.getRotation(i)); |
| 123 switch (shape.getType()) { |
| 124 case SkRRect::kRect_Type: |
| 125 canvas->drawRect(shape.rect(), paint); |
| 126 break; |
| 127 case SkRRect::kOval_Type: |
| 128 canvas->drawOval(shape.rect(), paint); |
| 129 break; |
| 130 default: |
| 131 canvas->drawRRect(shape, paint); |
| 132 break; |
| 133 } |
| 134 canvas->restore(); |
| 135 } |
| 136 } |
| 137 |
| 138 typedef ShapesGM INHERITED; |
| 139 }; |
| 140 |
| 141 class InnerShapesGM : public ShapesGM { |
| 142 public: |
| 143 InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {} |
| 144 |
| 145 private: |
| 146 void drawShapes(SkCanvas* canvas) const override { |
| 147 SkRandom rand; |
| 148 for (int i = 0; i < fShapes.count(); i++) { |
| 149 const SkRRect& outer = fShapes.getShape(i); |
| 150 const SkRRect& inner = fShapes.getShape((i * 7 + 11) % fShapes.simpl
eCount()); |
| 151 float s = 0.95f * SkTMin(outer.rect().width() / inner.rect().width()
, |
| 152 outer.rect().height() / inner.rect().height
()); |
| 153 SkMatrix innerXform; |
| 154 float dx = (rand.nextF() - 0.5f) * (outer.rect().width() - s * inner
.rect().width()); |
| 155 float dy = (rand.nextF() - 0.5f) * (outer.rect().height() - s * inne
r.rect().height()); |
| 156 innerXform.setTranslate(outer.rect().centerX() + dx, outer.rect().ce
nterY() + dy); |
| 157 if (s < 1) { |
| 158 innerXform.preScale(s, s); |
| 159 } |
| 160 innerXform.preTranslate(-inner.rect().centerX(), -inner.rect().cente
rY()); |
| 161 SkRRect xformedInner; |
| 162 inner.transform(innerXform, &xformedInner); |
| 163 SkPaint paint(fPaint); |
| 164 paint.setColor(rand.nextU() & ~0x808080); |
| 165 paint.setAlpha(128); // Use alpha to detect double blends. |
| 166 canvas->save(); |
| 167 canvas->rotate(fShapes.getRotation(i)); |
| 168 canvas->drawDRRect(outer, xformedInner, paint); |
| 169 canvas->restore(); |
| 170 } |
| 171 } |
| 172 |
| 173 typedef ShapesGM INHERITED; |
| 174 }; |
| 175 |
| 176 ////////////////////////////////////////////////////////////////////////////// |
| 177 |
| 178 DEF_GM( return new SimpleShapesGM(true); ) |
| 179 DEF_GM( return new SimpleShapesGM(false); ) |
| 180 DEF_GM( return new InnerShapesGM(true); ) |
| 181 DEF_GM( return new InnerShapesGM(false); ) |
| 182 |
| 183 } |
| OLD | NEW |