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