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" |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 14 | #include "SkMatrix44.h" |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 15 | |
| 16 | namespace sksg { |
| 17 | |
| 18 | /** |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 19 | * Transformations base class. |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 20 | */ |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 21 | class Transform : public Node { |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 22 | public: |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 23 | // Compose T = A x B |
| 24 | static sk_sp<Transform> MakeConcat(sk_sp<Transform> a, sk_sp<Transform> b); |
| 25 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 26 | protected: |
| 27 | Transform(); |
| 28 | |
Florin Malita | df0e1af | 2019-01-10 11:27:13 -0500 | [diff] [blame^] | 29 | virtual SkMatrix asMatrix () const = 0; |
| 30 | virtual SkMatrix44 asMatrix44() const = 0; |
| 31 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 32 | private: |
Florin Malita | df0e1af | 2019-01-10 11:27:13 -0500 | [diff] [blame^] | 33 | friend class TransformPriv; |
| 34 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 35 | using INHERITED = Node; |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Concrete, SkMatrix-backed transformation. |
| 40 | */ |
| 41 | class Matrix final : public Transform { |
| 42 | public: |
| 43 | static sk_sp<Matrix> Make(const SkMatrix& m); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 44 | |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 45 | SG_ATTRIBUTE(Matrix, SkMatrix, fMatrix) |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 46 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 47 | protected: |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 48 | explicit Matrix(const SkMatrix&); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 49 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 50 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 51 | |
Florin Malita | df0e1af | 2019-01-10 11:27:13 -0500 | [diff] [blame^] | 52 | SkMatrix asMatrix () const override; |
| 53 | SkMatrix44 asMatrix44() const override; |
| 54 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 55 | private: |
Florin Malita | 1632263 | 2018-09-12 10:15:34 -0400 | [diff] [blame] | 56 | SkMatrix fMatrix; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 57 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 58 | using INHERITED = Transform; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /** |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 62 | * Concrete, SkMatrix44-backed transformation. |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 63 | */ |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 64 | class Matrix44 final : public Transform { |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 65 | public: |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 66 | static sk_sp<Matrix44> Make(const SkMatrix44& m); |
| 67 | |
| 68 | SG_ATTRIBUTE(Matrix, SkMatrix44, fMatrix) |
| 69 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 70 | protected: |
| 71 | explicit Matrix44(const SkMatrix44&); |
| 72 | |
| 73 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
| 74 | |
Florin Malita | df0e1af | 2019-01-10 11:27:13 -0500 | [diff] [blame^] | 75 | SkMatrix asMatrix () const override; |
| 76 | SkMatrix44 asMatrix44() const override; |
| 77 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 78 | private: |
| 79 | SkMatrix44 fMatrix; |
| 80 | |
| 81 | using INHERITED = Transform; |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * Concrete Effect node, binding a Transform to a RenderNode. |
| 86 | */ |
| 87 | class TransformEffect final : public EffectNode { |
| 88 | public: |
| 89 | static sk_sp<TransformEffect> Make(sk_sp<RenderNode> child, sk_sp<Transform> transform) { |
| 90 | return child && transform |
| 91 | ? sk_sp<TransformEffect>(new TransformEffect(std::move(child), std::move(transform))) |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 92 | : nullptr; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 95 | static sk_sp<TransformEffect> Make(sk_sp<RenderNode> child, const SkMatrix& m) { |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 96 | return Make(std::move(child), Matrix::Make(m)); |
| 97 | } |
| 98 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 99 | ~TransformEffect() override; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 100 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 101 | const sk_sp<Transform>& getTransform() const { return fTransform; } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 102 | |
| 103 | protected: |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 104 | void onRender(SkCanvas*, const RenderContext*) const override; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 105 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 106 | SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 107 | |
| 108 | private: |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 109 | TransformEffect(sk_sp<RenderNode>, sk_sp<Transform>); |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 110 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 111 | const sk_sp<Transform> fTransform; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 112 | |
| 113 | typedef EffectNode INHERITED; |
| 114 | }; |
| 115 | |
| 116 | } // namespace sksg |
| 117 | |
| 118 | #endif // SkSGTransform_DEFINED |