aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/sksg/effects/SkSGTransform.cpp
blob: 1ea1e619a88d6a140486a8bcc9a72fdbfed32b5b (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
/*
 * 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 "SkSGTransform.h"

#include "SkCanvas.h"

namespace sksg {

Matrix::Matrix(const SkMatrix& m, sk_sp<Matrix> parent)
    : fParent(std::move(parent))
    , fLocalMatrix(m) {
    if (fParent) {
        fParent->addInvalReceiver(this);
    }
}

Matrix::~Matrix() {
    if (fParent) {
        fParent->removeInvalReceiver(this);
    }
}

Node::RevalidationResult Matrix::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
    fTotalMatrix = fLocalMatrix;

    if (fParent) {
        fParent->revalidate(ic, ctm);
        fTotalMatrix.postConcat(fParent->getTotalMatrix());
    }

    // A free-floating matrix contributes no damage.
    return { SkRect::MakeEmpty(), Damage::kBlockSelf };
}

Transform::Transform(sk_sp<RenderNode> child, sk_sp<Matrix> matrix)
    : INHERITED(std::move(child))
    , fMatrix(std::move(matrix)) {
    fMatrix->addInvalReceiver(this);
}

Transform::~Transform() {
    fMatrix->removeInvalReceiver(this);
}

void Transform::onRender(SkCanvas* canvas) const {
    const auto& m = fMatrix->getTotalMatrix();
    SkAutoCanvasRestore acr(canvas, !m.isIdentity());
    canvas->concat(m);
    this->INHERITED::onRender(canvas);
}

Node::RevalidationResult Transform::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
    SkASSERT(this->hasInval());

    // We don't care about matrix reval results, but we do care whether it was invalidated.
    const auto localDamage = fMatrix->hasInval() ? Damage::kForceSelf : Damage::kDefault;
    fMatrix->revalidate(ic, ctm);

    const auto& m = fMatrix->getTotalMatrix();
    auto result = this->INHERITED::onRevalidate(ic, SkMatrix::Concat(ctm, m));
    m.mapRect(&result.fBounds);
    result.fDamage = localDamage;

    return result;
}

} // namespace sksg