Chromium Code Reviews| Index: gm/shapes.cpp |
| diff --git a/gm/shapes.cpp b/gm/shapes.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9f37692c5965a686ccb9786fd2bbbae9c0e6ffaa |
| --- /dev/null |
| +++ b/gm/shapes.cpp |
| @@ -0,0 +1,187 @@ |
| + |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| +#include "gm.h" |
| +#include "SkRandom.h" |
| +#include "SkRRect.h" |
| + |
| +namespace skiagm { |
| + |
| +class ShapeSet { |
| +public: |
| + ShapeSet() { |
| + fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100)); |
| + fRotations.push_back(21); |
| + |
| + fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100)); |
| + fRotations.push_back(94); |
| + |
| + fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f, 1e-5f); |
| + fRotations.push_back(132); |
| + |
| + fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 15); |
| + fRotations.push_back(282); |
| + |
| + fSimpleCount = fShapes.count(); |
| + |
| + fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10, 5, 25, 35); |
| + fRotations.push_back(0); |
| + |
| + fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10, 60, 50, 30); |
| + fRotations.push_back(-35); |
| + |
| + fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1, 89, 59, 1); |
| + fRotations.push_back(65); |
| + |
| + SkVector radii[4] = {{4, 6}, {12, 8}, {24, 16}, {32, 48}}; |
| + fShapes.push_back().setRectRadii(SkRect::MakeXYWH(150, -129, 80, 160), radii); |
| + fRotations.push_back(265); |
| + |
| + SkVector radii2[4] = {{0, 0}, {80, 60}, {0, 0}, {80, 60}}; |
| + fShapes.push_back().setRectRadii(SkRect::MakeXYWH(180, -30, 80, 60), radii2); |
| + fRotations.push_back(295); |
| + } |
| + |
| + int count() const { |
| + SkASSERT(fShapes.count() == fRotations.count()); |
| + return fShapes.count(); |
| + } |
| + |
| + int simpleCount() const { |
| + return fSimpleCount; |
| + } |
| + |
| + const SkRRect& getShape(int idx) const { |
| + return fShapes[idx]; |
| + } |
| + |
| + SkScalar getRotation(int idx) const { |
| + return fRotations[idx]; |
| + } |
| + |
| +private: |
| + SkTArray<SkRRect> fShapes; |
| + SkTArray<SkScalar> fRotations; |
| + int fSimpleCount; |
| +}; |
| + |
| +class ShapesGM : public GM { |
| +protected: |
| + ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) { |
| + if (!antialias) { |
| + fName.append("_bw"); |
| + } |
| + } |
| + |
| + SkString onShortName() override final { return fName; } |
| + SkISize onISize() override { return SkISize::Make(500, 500); } |
| + |
| + void onOnceBeforeDraw() override { |
| + fPaint.setXfermodeMode(SkXfermode::kDarken_Mode); |
| + fPaint.setAntiAlias(fAntialias); |
| + } |
| + |
| + void onDraw(SkCanvas* canvas) override { |
| + canvas->clear(SK_ColorWHITE); |
| + |
| + canvas->save(); |
| + canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo().height() / 2.f); |
| + this->drawShapes(canvas); |
| + canvas->restore(); |
| + } |
| + |
| + SkString fName; |
| + bool fAntialias; |
| + ShapeSet fShapes; |
| + SkPaint fPaint; |
| + |
| + virtual void drawShapes(SkCanvas* canvas) const = 0; |
| + |
| +private: |
| + typedef GM INHERITED; |
| +}; |
| + |
| +class SimpleShapesGM : public ShapesGM { |
| +public: |
| + SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {} |
| + |
| +private: |
| + void drawShapes(SkCanvas* canvas) const override { |
| + SkRandom rand(2); |
| + for (int i = 0; i < fShapes.count(); i++) { |
| + SkPaint paint(fPaint); |
| + paint.setColor(rand.nextU() & ~0x808080); |
| + paint.setAlpha(128); // Use alpha to detect double blends. |
| + const SkRRect& shape = fShapes.getShape(i); |
| + canvas->save(); |
| + canvas->rotate(fShapes.getRotation(i)); |
| + switch (shape.getType()) { |
| + case SkRRect::kRect_Type: |
| + canvas->drawRect(shape.rect(), paint); |
| + break; |
| + case SkRRect::kOval_Type: |
| + canvas->drawOval(shape.rect(), paint); |
| + break; |
| + default: |
| + canvas->drawRRect(shape, paint); |
| + break; |
| + } |
| + canvas->restore(); |
| + } |
| + } |
| + |
| + typedef ShapesGM INHERITED; |
| +}; |
| + |
| +class InnerShapesGM : public ShapesGM { |
| +public: |
| + InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {} |
| + |
| +private: |
| + void drawShapes(SkCanvas* canvas) const override { |
| + SkRandom rand; |
| + for (int i = 0; i < fShapes.count(); i++) { |
| + const SkRRect& outer = fShapes.getShape(i); |
| + const SkRRect& inner = fShapes.getShape((i * 7 + 11) % fShapes.simpleCount()); |
| + float s = 0.95f * SkTMin(outer.rect().width() / inner.rect().width(), |
| + outer.rect().height() / inner.rect().height()); |
| + SkMatrix innerXform; |
| + float dx = (rand.nextF() - 0.5f) * (outer.rect().width() - s * inner.rect().width()); |
| + float dy = (rand.nextF() - 0.5f) * (outer.rect().height() - s * inner.rect().height()); |
| + innerXform.setTranslate(outer.rect().centerX() + dx, outer.rect().centerY() + dy); |
| + if (s < 1) { |
| + innerXform.preScale(s, s); |
| + } |
| + innerXform.preTranslate(-inner.rect().centerX(), -inner.rect().centerY()); |
| + SkRRect xformedInner; |
| + inner.transform(innerXform, &xformedInner); |
| + SkPaint paint(fPaint); |
| + paint.setColor(rand.nextU() & ~0x808080); |
| + paint.setAlpha(128); // Use alpha to detect double blends. |
| + canvas->save(); |
| + canvas->rotate(fShapes.getRotation(i)); |
| + canvas->drawDRRect(outer, xformedInner, paint); |
| + canvas->restore(); |
| + } |
| + } |
| + |
| + typedef ShapesGM INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +static GM* ShapesFactorySimple(void*) { return new SimpleShapesGM(true); } |
|
bsalomon
2016/02/10 14:55:46
Let's use DEF_GM here
Brian Osman
2016/02/10 16:49:21
Done.
|
| +static GM* ShapesFactorySimpleBW(void*) { return new SimpleShapesGM(false); } |
| +static GM* ShapesFactoryInner(void*) { return new InnerShapesGM(true); } |
| +static GM* ShapesFactoryInnerBW(void*) { return new InnerShapesGM(false); } |
| + |
| +static GMRegistry regSimple(ShapesFactorySimple); |
| +static GMRegistry regSimpleBW(ShapesFactorySimpleBW); |
| +static GMRegistry regInner(ShapesFactoryInner); |
| +static GMRegistry regInnerBW(ShapesFactoryInnerBW); |
| + |
| +} |