blob: a5731b1a3ff0e979c62986fd8b135460911287d0 [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 {
Florin Malitac14f1442018-01-05 11:32:31 -050013// Matrix nodes don't generate damage on their own, but via aggregation ancestor Transform nodes.
Florin Malita18eafd92018-01-04 21:11:55 -050014Matrix::Matrix(const SkMatrix& m, sk_sp<Matrix> parent)
Florin Malitac14f1442018-01-05 11:32:31 -050015 : INHERITED(kBubbleDamage_Trait)
16 , fParent(std::move(parent))
Florin Malita18eafd92018-01-04 21:11:55 -050017 , fLocalMatrix(m) {
18 if (fParent) {
19 fParent->addInvalReceiver(this);
20 }
21}
22
23Matrix::~Matrix() {
24 if (fParent) {
25 fParent->removeInvalReceiver(this);
26 }
27}
28
Florin Malitac14f1442018-01-05 11:32:31 -050029SkRect Matrix::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
Florin Malita18eafd92018-01-04 21:11:55 -050030 fTotalMatrix = fLocalMatrix;
31
32 if (fParent) {
33 fParent->revalidate(ic, ctm);
34 fTotalMatrix.postConcat(fParent->getTotalMatrix());
35 }
36
Florin Malitac14f1442018-01-05 11:32:31 -050037 return SkRect::MakeEmpty();
Florin Malita18eafd92018-01-04 21:11:55 -050038}
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 Malitac14f1442018-01-05 11:32:31 -050057SkRect Transform::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
Florin Malitac75e2402018-01-03 16:17:29 -050058 SkASSERT(this->hasInval());
59
Florin Malitac14f1442018-01-05 11:32:31 -050060 // We don't care about matrix reval results.
Florin Malita18eafd92018-01-04 21:11:55 -050061 fMatrix->revalidate(ic, ctm);
62
63 const auto& m = fMatrix->getTotalMatrix();
Florin Malitac14f1442018-01-05 11:32:31 -050064 auto bounds = this->INHERITED::onRevalidate(ic, SkMatrix::Concat(ctm, m));
65 m.mapRect(&bounds);
Florin Malitac75e2402018-01-03 16:17:29 -050066
Florin Malitac14f1442018-01-05 11:32:31 -050067 return bounds;
Florin Malita4aa44412017-12-19 12:21:02 -050068}
69
70} // namespace sksg