Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | namespace sksg { |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 13 | // Matrix nodes don't generate damage on their own, but via aggregation ancestor Transform nodes. |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 14 | Matrix::Matrix(const SkMatrix& m, sk_sp<Matrix> parent) |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 15 | : INHERITED(kBubbleDamage_Trait) |
| 16 | , fParent(std::move(parent)) |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 17 | , fLocalMatrix(m) { |
| 18 | if (fParent) { |
| 19 | fParent->addInvalReceiver(this); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | Matrix::~Matrix() { |
| 24 | if (fParent) { |
| 25 | fParent->removeInvalReceiver(this); |
| 26 | } |
| 27 | } |
| 28 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 29 | SkRect Matrix::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 30 | fTotalMatrix = fLocalMatrix; |
| 31 | |
| 32 | if (fParent) { |
| 33 | fParent->revalidate(ic, ctm); |
| 34 | fTotalMatrix.postConcat(fParent->getTotalMatrix()); |
| 35 | } |
| 36 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 37 | return SkRect::MakeEmpty(); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Transform::Transform(sk_sp<RenderNode> child, sk_sp<Matrix> matrix) |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 41 | : INHERITED(std::move(child)) |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 42 | , fMatrix(std::move(matrix)) { |
| 43 | fMatrix->addInvalReceiver(this); |
| 44 | } |
| 45 | |
| 46 | Transform::~Transform() { |
| 47 | fMatrix->removeInvalReceiver(this); |
| 48 | } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 49 | |
| 50 | void Transform::onRender(SkCanvas* canvas) const { |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 51 | const auto& m = fMatrix->getTotalMatrix(); |
| 52 | SkAutoCanvasRestore acr(canvas, !m.isIdentity()); |
| 53 | canvas->concat(m); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 54 | this->INHERITED::onRender(canvas); |
| 55 | } |
| 56 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 57 | SkRect Transform::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 58 | SkASSERT(this->hasInval()); |
| 59 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 60 | // We don't care about matrix reval results. |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 61 | fMatrix->revalidate(ic, ctm); |
| 62 | |
| 63 | const auto& m = fMatrix->getTotalMatrix(); |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 64 | auto bounds = this->INHERITED::onRevalidate(ic, SkMatrix::Concat(ctm, m)); |
| 65 | m.mapRect(&bounds); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 66 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 67 | return bounds; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace sksg |