blob: 1ea1e619a88d6a140486a8bcc9a72fdbfed32b5b [file] [log] [blame]
Florin Malita4aa44412017-12-19 12:21:02 -05001/*
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 "SkSGTransform.h"
9
10#include "SkCanvas.h"
11
12namespace sksg {
13
Florin Malita18eafd92018-01-04 21:11:55 -050014Matrix::Matrix(const SkMatrix& m, sk_sp<Matrix> parent)
15 : fParent(std::move(parent))
16 , fLocalMatrix(m) {
17 if (fParent) {
18 fParent->addInvalReceiver(this);
19 }
20}
21
22Matrix::~Matrix() {
23 if (fParent) {
24 fParent->removeInvalReceiver(this);
25 }
26}
27
28Node::RevalidationResult Matrix::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
29 fTotalMatrix = fLocalMatrix;
30
31 if (fParent) {
32 fParent->revalidate(ic, ctm);
33 fTotalMatrix.postConcat(fParent->getTotalMatrix());
34 }
35
36 // A free-floating matrix contributes no damage.
37 return { SkRect::MakeEmpty(), Damage::kBlockSelf };
38}
39
40Transform::Transform(sk_sp<RenderNode> child, sk_sp<Matrix> matrix)
Florin Malita4aa44412017-12-19 12:21:02 -050041 : INHERITED(std::move(child))
Florin Malita18eafd92018-01-04 21:11:55 -050042 , fMatrix(std::move(matrix)) {
43 fMatrix->addInvalReceiver(this);
44}
45
46Transform::~Transform() {
47 fMatrix->removeInvalReceiver(this);
48}
Florin Malita4aa44412017-12-19 12:21:02 -050049
50void Transform::onRender(SkCanvas* canvas) const {
Florin Malita18eafd92018-01-04 21:11:55 -050051 const auto& m = fMatrix->getTotalMatrix();
52 SkAutoCanvasRestore acr(canvas, !m.isIdentity());
53 canvas->concat(m);
Florin Malita4aa44412017-12-19 12:21:02 -050054 this->INHERITED::onRender(canvas);
55}
56
Florin Malita0ebf4192018-01-04 19:21:58 -050057Node::RevalidationResult Transform::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
Florin Malitac75e2402018-01-03 16:17:29 -050058 SkASSERT(this->hasInval());
59
Florin Malita18eafd92018-01-04 21:11:55 -050060 // We don't care about matrix reval results, but we do care whether it was invalidated.
61 const auto localDamage = fMatrix->hasInval() ? Damage::kForceSelf : Damage::kDefault;
62 fMatrix->revalidate(ic, ctm);
63
64 const auto& m = fMatrix->getTotalMatrix();
65 auto result = this->INHERITED::onRevalidate(ic, SkMatrix::Concat(ctm, m));
66 m.mapRect(&result.fBounds);
67 result.fDamage = localDamage;
Florin Malitac75e2402018-01-03 16:17:29 -050068
Florin Malita0ebf4192018-01-04 19:21:58 -050069 return result;
Florin Malita4aa44412017-12-19 12:21:02 -050070}
71
72} // namespace sksg