1 /*
2 * Copyright 2017 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 "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkSGColor.h"
11 #include "SkSGDraw.h"
12 #include "SkSGGroup.h"
13 #include "SkSGInvalidationController.h"
14 #include "SkSGRect.h"
15 #include "SkSGTransform.h"
16 #include "SkAnimTimer.h"
17
18 #include <cmath>
19
20 class SGInvalView final : public SampleView {
21 public:
SGInvalView()22 SGInvalView() {}
23
24 protected:
onOnceBeforeDraw()25 void onOnceBeforeDraw() override {
26 fRect1 = sksg::Rect::Make(SkRect::MakeLTRB(100, 100, 100, 100));
27 fRect2 = sksg::Rect::Make(SkRect::MakeLTRB(300, 200, 300, 200));
28 fColor1 = sksg::Color::Make(0);
29 fColor2 = sksg::Color::Make(0);
30
31 fRoot = sksg::Group::Make();
32 fRoot->addChild(sksg::Draw::Make(fRect1, fColor1));
33 fRoot->addChild(sksg::Transform::Make(sksg::Draw::Make(fRect2, fColor2),
34 SkMatrix::MakeScale(1.5f, 1.5f)));
35 }
36
onQuery(SkEvent * evt)37 bool onQuery(SkEvent* evt) override {
38 if (SampleCode::TitleQ(*evt)) {
39 SampleCode::TitleR(evt, "SGInval");
40 return true;
41 }
42
43 return this->INHERITED::onQuery(evt);
44 }
45
onDrawContent(SkCanvas * canvas)46 void onDrawContent(SkCanvas* canvas) override {
47 sksg::InvalidationController ic;
48 fRoot->revalidate(&ic, SkMatrix::I());
49
50 // TODO: clip/cull
51 fRoot->render(canvas);
52
53 SkPaint p;
54 p.setColor(0xffff0000);
55 p.setStyle(SkPaint::kStroke_Style);
56 p.setAntiAlias(true);
57 p.setStrokeWidth(0);
58
59 for (const auto& r : ic) {
60 canvas->drawRect(r, p);
61 }
62 }
63
onAnimate(const SkAnimTimer & timer)64 bool onAnimate(const SkAnimTimer& timer) override {
65 if (!fRoot) {
66 return true;
67 }
68
69 static constexpr SkScalar kSize = 50;
70 static constexpr SkScalar kRate = 1.0f / 500;
71 const auto t = timer.msec() * kRate;
72
73 fRect1->setR(fRect1->getL() + kSize * (1 + std::sin(t)));
74 fRect1->setB(fRect1->getT() + kSize * (1 + std::cos(t)));
75 fRect2->setR(fRect2->getL() + kSize * (1 + std::cos(SK_ScalarPI / 2 + t)));
76 fRect2->setB(fRect2->getT() + kSize * (1 + std::sin(SK_ScalarPI / 2 + t)));
77
78 fColor1->setColor(SkColorSetARGB(128 * (1 + std::sin(t)), 0, 0x80, 0));
79 fColor2->setColor(SkColorSetARGB(128 * (1 + std::cos(t)), 0, 0, 0x80));
80 return true;
81 }
82
83 private:
84 typedef SampleView INHERITED;
85
86 sk_sp<sksg::Rect> fRect1,
87 fRect2;
88 sk_sp<sksg::Color> fColor1,
89 fColor2;
90 sk_sp<sksg::Group> fRoot;
91 };
92
SGInvalFactory()93 static SkView* SGInvalFactory() { return new SGInvalView; }
94 static SkViewRegister reg(SGInvalFactory);
95