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 | #ifndef SkSGTransform_DEFINED |
| 9 | #define SkSGTransform_DEFINED |
| 10 | |
| 11 | #include "SkSGEffectNode.h" |
| 12 | |
| 13 | #include "SkMatrix.h" |
| 14 | |
| 15 | namespace sksg { |
| 16 | |
| 17 | /** |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 18 | * Concrete node, wrapping an SkMatrix, with an optional parent Matrix (to allow chaining): |
| 19 | * |
| 20 | * M' = parent x M |
| 21 | */ |
| 22 | class Matrix : public Node { |
| 23 | public: |
| 24 | static sk_sp<Matrix> Make(const SkMatrix& m, sk_sp<Matrix> parent = nullptr) { |
| 25 | return sk_sp<Matrix>(new Matrix(m, std::move(parent))); |
| 26 | } |
| 27 | |
| 28 | ~Matrix() override; |
| 29 | |
| 30 | SG_ATTRIBUTE(Matrix, SkMatrix, fLocalMatrix) |
| 31 | |
| 32 | const SkMatrix& getTotalMatrix() const { return fTotalMatrix; } |
| 33 | |
| 34 | protected: |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 35 | Matrix(const SkMatrix&, sk_sp<Matrix>); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 36 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 37 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | sk_sp<Matrix> fParent; |
| 41 | SkMatrix fLocalMatrix, |
| 42 | fTotalMatrix; // cached during revalidation |
| 43 | |
| 44 | typedef Node INHERITED; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Concrete Effect node, binding a Matrix to a RenderNode. |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 49 | */ |
| 50 | class Transform : public EffectNode { |
| 51 | public: |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 52 | static sk_sp<Transform> Make(sk_sp<RenderNode> child, sk_sp<Matrix> matrix) { |
| 53 | return child && matrix |
| 54 | ? sk_sp<Transform>(new Transform(std::move(child), std::move(matrix))) |
| 55 | : nullptr; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 58 | static sk_sp<Transform> Make(sk_sp<RenderNode> child, const SkMatrix& m) { |
| 59 | return Make(std::move(child), Matrix::Make(m)); |
| 60 | } |
| 61 | |
| 62 | ~Transform() override; |
| 63 | |
| 64 | const sk_sp<Matrix>& getMatrix() const { return fMatrix; } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 65 | |
| 66 | protected: |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 67 | Transform(sk_sp<RenderNode>, sk_sp<Matrix>); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 68 | |
| 69 | void onRender(SkCanvas*) const override; |
| 70 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame^] | 71 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 72 | |
| 73 | private: |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 74 | sk_sp<Matrix> fMatrix; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 75 | |
| 76 | typedef EffectNode INHERITED; |
| 77 | }; |
| 78 | |
| 79 | } // namespace sksg |
| 80 | |
| 81 | #endif // SkSGTransform_DEFINED |