aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/sksg/SkSGScene.cpp
blob: 8d7e0b369babe09f7d849b275c53ced4a83add1b (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
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkSGScene.h"

#include "SkCanvas.h"
#include "SkMatrix.h"
#include "SkPaint.h"
#include "SkSGInvalidationController.h"
#include "SkSGRenderNode.h"

namespace sksg {

Animator::Animator()  = default;
Animator::~Animator() = default;

void Animator::tick(float t) {
    this->onTick(t);
}

GroupAnimator::GroupAnimator(AnimatorList&& animators)
    : fAnimators(std::move(animators)) {}

void GroupAnimator::onTick(float t) {
    for (const auto& a : fAnimators) {
        a->tick(t);
    }
}

std::unique_ptr<Scene> Scene::Make(sk_sp<RenderNode> root, AnimatorList&& anims) {
    return root ? std::unique_ptr<Scene>(new Scene(std::move(root), std::move(anims))) : nullptr;
}

Scene::Scene(sk_sp<RenderNode> root, AnimatorList&& animators)
    : fRoot(std::move(root))
    , fAnimators(std::move(animators)) {}

Scene::~Scene() = default;

void Scene::render(SkCanvas* canvas) const {
    InvalidationController ic;
    fRoot->revalidate(&ic, SkMatrix::I());
    fRoot->render(canvas);

    if (fShowInval) {
        SkPaint fill, stroke;
        fill.setAntiAlias(true);
        fill.setColor(0x40ff0000);
        stroke.setAntiAlias(true);
        stroke.setColor(0xffff0000);
        stroke.setStyle(SkPaint::kStroke_Style);

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

void Scene::animate(float t) {
    for (const auto& anim : fAnimators) {
        anim->tick(t);
    }
}

} // namespace sksg