1 /*
2  * Copyright 2015 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 
10 #ifndef SK_BUILD_FOR_GOOGLE3
11 
12 #include "experimental/xform/SkShape.h"
13 #include "experimental/xform/SkXform.h"
14 
15 #include "include/core/SkCanvas.h"
16 #include "include/core/SkPaint.h"
17 #include "tools/timer/TimeUtils.h"
18 
19 class XformGM : public skiagm::GM {
20     sk_sp<MatrixXF> fRoot, fRA, fRB, fA, fB;
21     sk_sp<Shape> fShape;
22 
23 public:
XformGM()24     XformGM() {
25         fRoot = MatrixXF::Make();
26 
27         fRA = MatrixXF::Make(fRoot);
28         fRB = MatrixXF::Make(fRoot);
29 
30         fA = MatrixXF::Make(fRA);
31         fB = MatrixXF::Make(fRB);
32 
33         fRA->setRotate(30);
34         fA->setTranslate(100, 0);
35 
36         fRB->setTranslate(100, 0);
37         fB->setRotate(30);
38 
39         sk_sp<GroupShape> g = GroupShape::Make();
40         g->append(GeoShape::Make(fA,  {0, 0, 100, 60}, SK_ColorRED));
41         g->append(GeoShape::Make(fB,  {0, 0, 100, 60}, SK_ColorGREEN));
42         g->append(GeoShape::Make(fRA, {0, 0, 100, 60}, SK_ColorBLUE));
43         g->append(GeoShape::Make(fRB, {0, 0, 100, 60}, SK_ColorGRAY));
44         g->append(GeoShape::Make(fRoot, {0, 0, 100, 60}, 0xFFCC8844));
45 
46         sk_sp<MatrixXF> sub = MatrixXF::Make();
47         SkMatrix m;
48         m.setScale(0.5, 0.5);
49         m.postTranslate(50, 50);
50         sub->setLocalMatrix(m);
51 
52         sk_sp<GroupShape> parent = GroupShape::Make();
53         parent->append(g);
54         parent->append(GroupShape::Make(sub, g));
55         fShape = parent;
56     }
57 
58 protected:
onShortName()59     SkString onShortName() override { return SkString("exp_xform"); }
60 
onISize()61     SkISize onISize() override { return SkISize::Make(520, 520); }
62 
onDraw(SkCanvas * canvas)63     void onDraw(SkCanvas* canvas) override {
64         auto ctx = XContext::Make(canvas);
65 
66         if (0) {
67             canvas->translate(2, 2);
68 
69             SkRect rect{0, 0, 100, 60};
70             SkPaint paint; paint.setStyle(SkPaint::kStroke_Style);
71             canvas->drawRect(rect, paint);
72             canvas->save(); canvas->translate(10, 10);
73             paint.setColor(SK_ColorRED); canvas->drawRect(rect, paint); canvas->restore();
74             canvas->save(); canvas->scale(2, 2);
75             paint.setColor(SK_ColorBLUE); canvas->drawRect(rect, paint); canvas->restore();
76             canvas->save(); canvas->scale(2, 2); canvas->translate(10, 10);
77             paint.setColor(SK_ColorBLACK); canvas->drawRect(rect, paint); canvas->restore();
78             canvas->save(); canvas->translate(10, 10); canvas->scale(2, 2);
79             paint.setColor(SK_ColorBLACK); canvas->drawRect(rect, paint); canvas->restore();
80 
81             auto x0 = MatrixXF::Make();
82             auto x1 = MatrixXF::Make(x0);
83             auto x2 = MatrixXF::Make(x1);
84             x1->setScale(2, 2);
85             x2->setTranslate(10, 10);
86 
87             auto sh = GeoShape::Make(x2,  {0, 0, 100, 60}, 0x8800FF00);
88             sh->draw(ctx.get());
89             return;
90         }
91         fShape->draw(ctx.get());
92     }
93 
onAnimate(double nanos)94     bool onAnimate(double nanos) override {
95         float scale = 3 + sinf(TimeUtils::Scaled(1e-9 * nanos, 1, 0)) * 2;
96         fRoot->setScale(scale, scale);
97         fRA->setRotate(TimeUtils::Scaled(1e-9 * nanos, 40, 0));
98         fB->setRotate(TimeUtils::Scaled(1e-9 * nanos, 40*sqrtf(2), 0));
99         return true;
100     }
101 
102 private:
103     using INHERITED = skiagm::GM;
104 };
105 DEF_GM( return new XformGM; )
106 
107 #endif
108