blob: 790373b7627c3d00b58fb003b6cbd57c0195bdeb [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:
Florin Malita16322632018-09-12 10:15:34 -040024 static sk_sp<Matrix> Make(const SkMatrix& m, sk_sp<Matrix> parent = nullptr);
Florin Malita18eafd92018-01-04 21:11:55 -050025
Florin Malita16322632018-09-12 10:15:34 -040026 SG_ATTRIBUTE(Matrix, SkMatrix, fMatrix)
Florin Malita18eafd92018-01-04 21:11:55 -050027
Florin Malita16322632018-09-12 10:15:34 -040028 virtual const SkMatrix& getTotalMatrix() const;
Florin Malita18eafd92018-01-04 21:11:55 -050029
30protected:
Florin Malita16322632018-09-12 10:15:34 -040031 explicit Matrix(const SkMatrix&);
Florin Malita18eafd92018-01-04 21:11:55 -050032
Florin Malitac14f1442018-01-05 11:32:31 -050033 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
Florin Malita18eafd92018-01-04 21:11:55 -050034
35private:
Florin Malita16322632018-09-12 10:15:34 -040036 SkMatrix fMatrix;
Florin Malita18eafd92018-01-04 21:11:55 -050037
38 typedef Node INHERITED;
39};
40
41/**
42 * Concrete Effect node, binding a Matrix to a RenderNode.
Florin Malita4aa44412017-12-19 12:21:02 -050043 */
Florin Malita16d0ad02018-01-19 15:07:29 -050044class Transform final : public EffectNode {
Florin Malita4aa44412017-12-19 12:21:02 -050045public:
Florin Malita18eafd92018-01-04 21:11:55 -050046 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 Malita4aa44412017-12-19 12:21:02 -050050 }
51
Florin Malita18eafd92018-01-04 21:11:55 -050052 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 Malita4aa44412017-12-19 12:21:02 -050059
60protected:
Florin Malitac0132ff2018-08-09 07:40:01 -040061 void onRender(SkCanvas*, const RenderContext*) const override;
Florin Malita4aa44412017-12-19 12:21:02 -050062
Florin Malitac14f1442018-01-05 11:32:31 -050063 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
Florin Malita4aa44412017-12-19 12:21:02 -050064
65private:
Florin Malita16d0ad02018-01-19 15:07:29 -050066 Transform(sk_sp<RenderNode>, sk_sp<Matrix>);
67
68 const sk_sp<Matrix> fMatrix;
Florin Malita4aa44412017-12-19 12:21:02 -050069
70 typedef EffectNode INHERITED;
71};
72
73} // namespace sksg
74
75#endif // SkSGTransform_DEFINED