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: |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 24 | static sk_sp<Matrix> Make(const SkMatrix& m, sk_sp<Matrix> parent = nullptr); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 25 | |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 26 | SG_ATTRIBUTE(Matrix, SkMatrix, fMatrix) |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 27 | |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 28 | virtual const SkMatrix& getTotalMatrix() const; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 29 | |
| 30 | protected: |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 31 | explicit Matrix(const SkMatrix&); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 32 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 33 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 34 | |
| 35 | private: |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 36 | SkMatrix fMatrix; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 37 | |
| 38 | typedef Node INHERITED; |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * Concrete Effect node, binding a Matrix to a RenderNode. |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 43 | */ |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 44 | class Transform final : public EffectNode { |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 45 | public: |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 46 | static sk_sp<Transform> Make(sk_sp<RenderNode> child, sk_sp<Matrix> matrix) { |
| 47 | return child && matrix |
| 48 | ? sk_sp<Transform>(new Transform(std::move(child), std::move(matrix))) |
| 49 | : nullptr; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 52 | static sk_sp<Transform> Make(sk_sp<RenderNode> child, const SkMatrix& m) { |
| 53 | return Make(std::move(child), Matrix::Make(m)); |
| 54 | } |
| 55 | |
| 56 | ~Transform() override; |
| 57 | |
| 58 | const sk_sp<Matrix>& getMatrix() const { return fMatrix; } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 59 | |
| 60 | protected: |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 61 | void onRender(SkCanvas*, const RenderContext*) const override; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 62 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 63 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 64 | |
| 65 | private: |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 66 | Transform(sk_sp<RenderNode>, sk_sp<Matrix>); |
| 67 | |
| 68 | const sk_sp<Matrix> fMatrix; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 69 | |
| 70 | typedef EffectNode INHERITED; |
| 71 | }; |
| 72 | |
| 73 | } // namespace sksg |
| 74 | |
| 75 | #endif // SkSGTransform_DEFINED |