blob: 6b7fbc010bfb05ab5d0d4d03dcff7ffce035e46b [file] [log] [blame]
Florin Malita4aa44412017-12-19 12:21:02 -05001/*
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
15namespace sksg {
16
17/**
Florin Malita18eafd92018-01-04 21:11:55 -050018 * Concrete node, wrapping an SkMatrix, with an optional parent Matrix (to allow chaining):
19 *
20 * M' = parent x M
21 */
22class Matrix : public Node {
23public:
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
34protected:
Florin Malitac14f1442018-01-05 11:32:31 -050035 Matrix(const SkMatrix&, sk_sp<Matrix>);
Florin Malita18eafd92018-01-04 21:11:55 -050036
Florin Malitac14f1442018-01-05 11:32:31 -050037 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
Florin Malita18eafd92018-01-04 21:11:55 -050038
39private:
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 Malita4aa44412017-12-19 12:21:02 -050049 */
Florin Malita16d0ad02018-01-19 15:07:29 -050050class Transform final : public EffectNode {
Florin Malita4aa44412017-12-19 12:21:02 -050051public:
Florin Malita18eafd92018-01-04 21:11:55 -050052 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 Malita4aa44412017-12-19 12:21:02 -050056 }
57
Florin Malita18eafd92018-01-04 21:11:55 -050058 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 Malita4aa44412017-12-19 12:21:02 -050065
66protected:
Florin Malita4aa44412017-12-19 12:21:02 -050067 void onRender(SkCanvas*) const override;
68
Florin Malitac14f1442018-01-05 11:32:31 -050069 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
Florin Malita4aa44412017-12-19 12:21:02 -050070
71private:
Florin Malita16d0ad02018-01-19 15:07:29 -050072 Transform(sk_sp<RenderNode>, sk_sp<Matrix>);
73
74 const sk_sp<Matrix> fMatrix;
Florin Malita4aa44412017-12-19 12:21:02 -050075
76 typedef EffectNode INHERITED;
77};
78
79} // namespace sksg
80
81#endif // SkSGTransform_DEFINED