2017-12-19 17:21:02 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/sksg/include/SkSGEffectNode.h"
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkMatrix44.h"
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
/**
|
2019-01-09 20:37:57 +00:00
|
|
|
* Transformations base class.
|
2018-01-05 02:11:55 +00:00
|
|
|
*/
|
2019-01-09 20:37:57 +00:00
|
|
|
class Transform : public Node {
|
2018-01-05 02:11:55 +00:00
|
|
|
public:
|
2019-04-25 16:18:19 +00:00
|
|
|
// Compose T' = A x B
|
2019-01-09 20:37:57 +00:00
|
|
|
static sk_sp<Transform> MakeConcat(sk_sp<Transform> a, sk_sp<Transform> b);
|
|
|
|
|
2019-04-25 16:18:19 +00:00
|
|
|
// T' = Inv(T)
|
|
|
|
static sk_sp<Transform> MakeInverse(sk_sp<Transform> t);
|
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
protected:
|
|
|
|
Transform();
|
|
|
|
|
2019-01-10 19:44:59 +00:00
|
|
|
virtual bool is44() const = 0;
|
|
|
|
|
2019-01-10 16:27:13 +00:00
|
|
|
virtual SkMatrix asMatrix () const = 0;
|
|
|
|
virtual SkMatrix44 asMatrix44() const = 0;
|
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
private:
|
2019-01-10 16:27:13 +00:00
|
|
|
friend class TransformPriv;
|
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
using INHERITED = Node;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-01-10 20:24:15 +00:00
|
|
|
* Concrete, matrix-backed Transform.
|
|
|
|
*
|
|
|
|
* Supported instantiations: SkMatrix, SkMatrix44.
|
|
|
|
*
|
|
|
|
* Sample use:
|
|
|
|
*
|
|
|
|
* auto m33 = Matrix<SkMatrix>::Make(SkMatrix::I());
|
|
|
|
* ...
|
|
|
|
* m33->setMatrix(SkMatrix::MakeTrans(10, 10));
|
|
|
|
*
|
2019-01-09 20:37:57 +00:00
|
|
|
*/
|
2019-01-10 20:24:15 +00:00
|
|
|
template <typename T>
|
2019-01-09 20:37:57 +00:00
|
|
|
class Matrix final : public Transform {
|
|
|
|
public:
|
2019-01-10 20:24:15 +00:00
|
|
|
template <typename = std::enable_if<std::is_same<T, SkMatrix >::value ||
|
|
|
|
std::is_same<T, SkMatrix44>::value>>
|
|
|
|
static sk_sp<Matrix> Make(const T& m) { return sk_sp<Matrix>(new Matrix(m)); }
|
2019-01-09 20:37:57 +00:00
|
|
|
|
2019-01-10 20:24:15 +00:00
|
|
|
SG_ATTRIBUTE(Matrix, T, fMatrix)
|
2019-01-09 20:37:57 +00:00
|
|
|
|
|
|
|
protected:
|
2019-01-10 20:24:15 +00:00
|
|
|
explicit Matrix(const T& m) : fMatrix(m) {}
|
2019-01-09 20:37:57 +00:00
|
|
|
|
2019-01-10 20:24:15 +00:00
|
|
|
SkRect onRevalidate(InvalidationController*, const SkMatrix&) override {
|
|
|
|
return SkRect::MakeEmpty();
|
|
|
|
}
|
2019-01-09 20:37:57 +00:00
|
|
|
|
2019-01-10 20:24:15 +00:00
|
|
|
bool is44() const override { return std::is_same<T, SkMatrix44>::value; }
|
2019-01-10 19:44:59 +00:00
|
|
|
|
2019-01-10 20:24:15 +00:00
|
|
|
SkMatrix asMatrix () const override { return fMatrix; }
|
|
|
|
SkMatrix44 asMatrix44() const override { return fMatrix; }
|
2019-01-10 16:27:13 +00:00
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
private:
|
2019-01-10 20:24:15 +00:00
|
|
|
T fMatrix;
|
2019-01-09 20:37:57 +00:00
|
|
|
|
|
|
|
using INHERITED = Transform;
|
2018-01-05 02:11:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-01-09 20:37:57 +00:00
|
|
|
* Concrete Effect node, binding a Transform to a RenderNode.
|
2017-12-19 17:21:02 +00:00
|
|
|
*/
|
2019-01-09 20:37:57 +00:00
|
|
|
class TransformEffect final : public EffectNode {
|
2017-12-19 17:21:02 +00:00
|
|
|
public:
|
2019-01-09 20:37:57 +00:00
|
|
|
static sk_sp<TransformEffect> Make(sk_sp<RenderNode> child, sk_sp<Transform> transform) {
|
|
|
|
return child && transform
|
|
|
|
? sk_sp<TransformEffect>(new TransformEffect(std::move(child), std::move(transform)))
|
2018-01-05 02:11:55 +00:00
|
|
|
: nullptr;
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
static sk_sp<TransformEffect> Make(sk_sp<RenderNode> child, const SkMatrix& m) {
|
2019-01-10 20:24:15 +00:00
|
|
|
return Make(std::move(child), Matrix<SkMatrix>::Make(m));
|
2018-01-05 02:11:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
~TransformEffect() override;
|
2018-01-05 02:11:55 +00:00
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
const sk_sp<Transform>& getTransform() const { return fTransform; }
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
protected:
|
2018-08-09 11:40:01 +00:00
|
|
|
void onRender(SkCanvas*, const RenderContext*) const override;
|
2019-02-12 14:33:21 +00:00
|
|
|
const RenderNode* onNodeAt(const SkPoint&) const override;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
private:
|
2019-01-09 20:37:57 +00:00
|
|
|
TransformEffect(sk_sp<RenderNode>, sk_sp<Transform>);
|
2018-01-19 20:07:29 +00:00
|
|
|
|
2019-01-09 20:37:57 +00:00
|
|
|
const sk_sp<Transform> fTransform;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
typedef EffectNode INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sksg
|
|
|
|
|
|
|
|
#endif // SkSGTransform_DEFINED
|