• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
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 
SG_ATTRIBUTE(Matrix,SkMatrix,fLocalMatrix)30     SG_ATTRIBUTE(Matrix, SkMatrix, fLocalMatrix)
31 
32     const SkMatrix& getTotalMatrix() const { return fTotalMatrix; }
33 
34 protected:
35     Matrix(const SkMatrix&, sk_sp<Matrix>);
36 
37     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
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.
49  */
50 class Transform final : public EffectNode {
51 public:
Make(sk_sp<RenderNode> child,sk_sp<Matrix> matrix)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;
56     }
57 
Make(sk_sp<RenderNode> child,const SkMatrix & m)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 
getMatrix()64     const sk_sp<Matrix>& getMatrix() const { return fMatrix; }
65 
66 protected:
67     void onRender(SkCanvas*) const override;
68 
69     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
70 
71 private:
72     Transform(sk_sp<RenderNode>, sk_sp<Matrix>);
73 
74     const sk_sp<Matrix> fMatrix;
75 
76     typedef EffectNode INHERITED;
77 };
78 
79 } // namespace sksg
80 
81 #endif // SkSGTransform_DEFINED
82