aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleSGInval.cpp
blob: 44cdcb91d9e1bd91dd0b4510ed25363337513c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SampleCode.h"
#include "SkCanvas.h"
#include "SkSGColor.h"
#include "SkSGDraw.h"
#include "SkSGGroup.h"
#include "SkSGInvalidationController.h"
#include "SkSGRect.h"
#include "SkSGTransform.h"
#include "SkAnimTimer.h"

#include <cmath>

class SGInvalView final : public SampleView {
public:
    SGInvalView() {}

protected:
    void onOnceBeforeDraw() override {
        fRect1 = sksg::Rect::Make(SkRect::MakeLTRB(100, 100, 100, 100));
        fRect2 = sksg::Rect::Make(SkRect::MakeLTRB(300, 200, 300, 200));
        fColor1 = sksg::Color::Make(0);
        fColor2 = sksg::Color::Make(0);

        fRoot = sksg::Group::Make();
        fRoot->addChild(sksg::Draw::Make(fRect1, fColor1));
        fRoot->addChild(sksg::Transform::Make(sksg::Draw::Make(fRect2, fColor2),
                                              SkMatrix::MakeScale(1.5f, 1.5f)));
    }

    bool onQuery(SkEvent* evt) override {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "SGInval");
            return true;
        }

        return this->INHERITED::onQuery(evt);
    }

    void onDrawContent(SkCanvas* canvas) override {
        sksg::InvalidationController ic;
        fRoot->revalidate(&ic, SkMatrix::I());

        // TODO: clip/cull
        fRoot->render(canvas);

        SkPaint p;
        p.setColor(0xffff0000);
        p.setStyle(SkPaint::kStroke_Style);
        p.setAntiAlias(true);
        p.setStrokeWidth(0);

        for (const auto& r : ic) {
            canvas->drawRect(r, p);
        }
    }

    bool onAnimate(const SkAnimTimer& timer) override {
        if (!fRoot) {
            return true;
        }

        static constexpr SkScalar kSize = 50;
        static constexpr SkScalar kRate = 1.0f / 500;
        const auto t = timer.msec() * kRate;

        fRect1->setR(fRect1->getL() + kSize * (1 + std::sin(t)));
        fRect1->setB(fRect1->getT() + kSize * (1 + std::cos(t)));
        fRect2->setR(fRect2->getL() + kSize * (1 + std::cos(SK_ScalarPI / 2 + t)));
        fRect2->setB(fRect2->getT() + kSize * (1 + std::sin(SK_ScalarPI / 2 + t)));

        fColor1->setColor(SkColorSetARGB(128 * (1 + std::sin(t)), 0, 0x80, 0));
        fColor2->setColor(SkColorSetARGB(128 * (1 + std::cos(t)), 0, 0, 0x80));
        return true;
    }

private:
    typedef SampleView INHERITED;

    sk_sp<sksg::Rect>  fRect1,
                       fRect2;
    sk_sp<sksg::Color> fColor1,
                       fColor2;
    sk_sp<sksg::Group> fRoot;
};

static SkView* SGInvalFactory() { return new SGInvalView; }
static SkViewRegister reg(SGInvalFactory);