/* * Copyright 2017 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkSGTransform_DEFINED #define SkSGTransform_DEFINED #include "modules/sksg/include/SkSGEffectNode.h" #include "include/core/SkMatrix.h" #include "include/core/SkMatrix44.h" namespace sksg { /** * Transformations base class. */ class Transform : public Node { public: // Compose T' = A x B static sk_sp MakeConcat(sk_sp a, sk_sp b); // T' = Inv(T) static sk_sp MakeInverse(sk_sp t); protected: Transform(); virtual bool is44() const = 0; virtual SkMatrix asMatrix () const = 0; virtual SkMatrix44 asMatrix44() const = 0; private: friend class TransformPriv; using INHERITED = Node; }; /** * Concrete, matrix-backed Transform. * * Supported instantiations: SkMatrix, SkMatrix44. * * Sample use: * * auto m33 = Matrix::Make(SkMatrix::I()); * ... * m33->setMatrix(SkMatrix::MakeTrans(10, 10)); * */ template class Matrix final : public Transform { public: template ::value || std::is_same::value>> static sk_sp Make(const T& m) { return sk_sp(new Matrix(m)); } SG_ATTRIBUTE(Matrix, T, fMatrix) protected: explicit Matrix(const T& m) : fMatrix(m) {} SkRect onRevalidate(InvalidationController*, const SkMatrix&) override { return SkRect::MakeEmpty(); } bool is44() const override { return std::is_same::value; } SkMatrix asMatrix () const override { return fMatrix; } SkMatrix44 asMatrix44() const override { return fMatrix; } private: T fMatrix; using INHERITED = Transform; }; /** * Concrete Effect node, binding a Transform to a RenderNode. */ class TransformEffect final : public EffectNode { public: static sk_sp Make(sk_sp child, sk_sp transform) { return child && transform ? sk_sp(new TransformEffect(std::move(child), std::move(transform))) : nullptr; } static sk_sp Make(sk_sp child, const SkMatrix& m) { return Make(std::move(child), Matrix::Make(m)); } ~TransformEffect() override; const sk_sp& getTransform() const { return fTransform; } protected: void onRender(SkCanvas*, const RenderContext*) const override; const RenderNode* onNodeAt(const SkPoint&) const override; SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; private: TransformEffect(sk_sp, sk_sp); const sk_sp fTransform; typedef EffectNode INHERITED; }; } // namespace sksg #endif // SkSGTransform_DEFINED