[skottie] Switch SkMatrix44 -> SkM44

Change-Id: If58516a0dad5b51debf497b069713fb6f37999e6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/266940
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2020-01-27 16:13:18 -05:00 committed by Skia Commit-Bot
parent 5a36a3a4c3
commit b67ca7473f
8 changed files with 115 additions and 110 deletions

View File

@ -7,7 +7,6 @@
#include "modules/skottie/src/Camera.h"
#include "include/utils/Sk3D.h"
#include "modules/skottie/src/SkottieJson.h"
#include "modules/skottie/src/SkottiePriv.h"
#include "modules/sksg/include/SkSGTransform.h"
@ -17,31 +16,20 @@ namespace internal {
namespace {
SkMatrix44 ComputeCameraMatrix(const SkPoint3& position,
const SkPoint3& poi,
const SkPoint3& rotation,
SkM44 ComputeCameraMatrix(const SkV3& position,
const SkV3& poi,
const SkV3& rotation,
const SkSize& viewport_size,
float zoom) {
const auto pos = SkPoint3{ position.fX, position.fY, -position.fZ },
up = SkPoint3{ 0, 1, 0 };
// Initial camera vector.
SkMatrix44 cam_t;
Sk3LookAt(&cam_t, pos, poi, up);
// Rotation origin is camera position.
{
SkMatrix44 rot;
rot.setRotateDegreesAbout(1, 0, 0, rotation.fX);
cam_t.postConcat(rot);
rot.setRotateDegreesAbout(0, 1, 0, rotation.fY);
cam_t.postConcat(rot);
rot.setRotateDegreesAbout(0, 0, 1, -rotation.fZ);
cam_t.postConcat(rot);
}
// Flip world Z, as it is opposite of what Sk3D expects.
cam_t.preScale(1, 1, -1);
const auto cam_t = SkM44::Rotate({0, 0, 1}, SkDegreesToRadians(-rotation.z))
* SkM44::Rotate({0, 1, 0}, SkDegreesToRadians( rotation.y))
* SkM44::Rotate({1, 0, 0}, SkDegreesToRadians( rotation.x))
* Sk3LookAt({ position.x, position.y, -position.z },
{ poi.x, poi.y, poi.z },
{ 0, 1, 0 })
* SkM44::Scale(1, 1, -1);
// View parameters:
//
@ -52,16 +40,13 @@ SkMatrix44 ComputeCameraMatrix(const SkPoint3& position,
view_distance = zoom,
view_angle = std::atan(sk_ieee_float_divide(view_size * 0.5f, view_distance));
SkMatrix44 persp_t;
Sk3Perspective(&persp_t, 0, view_distance, 2 * view_angle);
persp_t.postScale(view_size * 0.5f, view_size * 0.5f, 1);
const auto persp_t = SkM44::Scale(view_size * 0.5f, view_size * 0.5f, 1)
* Sk3Perspective(0, view_distance, 2 * view_angle);
SkMatrix44 m;
m.setTranslate(viewport_size.width() * 0.5f, viewport_size.height() * 0.5f, 0);
m.preConcat(persp_t);
m.preConcat(cam_t);
return m;
return SkM44::Translate(viewport_size.width() * 0.5f,
viewport_size.height() * 0.5f,
0)
* persp_t * cam_t;
}
} // namespace
@ -82,7 +67,7 @@ CameraAdaper::CameraAdaper(const skjson::ObjectValue& jlayer,
CameraAdaper::~CameraAdaper() = default;
SkMatrix44 CameraAdaper::totalMatrix() const {
SkM44 CameraAdaper::totalMatrix() const {
// Camera parameters:
//
// * location -> position attribute
@ -98,7 +83,7 @@ SkMatrix44 CameraAdaper::totalMatrix() const {
fZoom);
}
SkPoint3 CameraAdaper::poi(const SkPoint3& pos) const {
SkV3 CameraAdaper::poi(const SkV3& pos) const {
// AE supports two camera types:
//
// - one-node camera: does not auto-orient, and starts off perpendicular
@ -108,12 +93,12 @@ SkPoint3 CameraAdaper::poi(const SkPoint3& pos) const {
// and auto-orients to point in its direction.
if (fType == CameraType::kOneNode) {
return { pos.fX, pos.fY, -pos.fZ - 1};
return { pos.x, pos.y, -pos.z - 1};
}
const auto ap = this->anchor_point();
return { ap.fX, ap.fY, -ap.fZ };
return { ap.x, ap.y, -ap.z };
}
sk_sp<sksg::Transform> CameraAdaper::DefaultCameraTransform(const SkSize& viewport_size) {
@ -122,11 +107,11 @@ sk_sp<sksg::Transform> CameraAdaper::DefaultCameraTransform(const SkSize& viewpo
static constexpr float kDefaultAEZoom = 879.13f;
const SkPoint3 pos = { center.fX, center.fY, -kDefaultAEZoom },
poi = { pos.fX, pos.fY, -pos.fZ - 1 },
const SkV3 pos = { center.fX, center.fY, -kDefaultAEZoom },
poi = { pos.x, pos.y, -pos.z - 1 },
rot = { 0, 0, 0 };
return sksg::Matrix<SkMatrix44>::Make(
return sksg::Matrix<SkM44>::Make(
ComputeCameraMatrix(pos, poi, rot, viewport_size, kDefaultAEZoom));
}

View File

@ -24,7 +24,7 @@ public:
// Used in the absence of an explicit camera layer.
static sk_sp<sksg::Transform> DefaultCameraTransform(const SkSize& viewport_size);
SkMatrix44 totalMatrix() const override;
SkM44 totalMatrix() const override;
private:
enum class CameraType {
@ -32,7 +32,7 @@ private:
kTwoNode, // explicitly facing a POI (the anchor point), auto-orients
};
SkPoint3 poi(const SkPoint3& pos) const;
SkV3 poi(const SkV3& pos) const;
const SkSize fViewportSize;
const CameraType fType;

View File

@ -9,8 +9,8 @@
#include "include/core/SkColor.h"
#include "include/core/SkPoint.h"
#include "include/core/SkPoint3.h"
#include "include/core/SkSize.h"
#include "include/private/SkM44.h"
#include "include/private/SkNx.h"
#include "modules/skottie/src/SkottieJson.h"
#include "modules/skottie/src/SkottiePriv.h"
@ -103,9 +103,9 @@ SkPoint ValueTraits<VectorValue>::As<SkPoint>(const VectorValue& vec) {
template <>
template <>
SkPoint3 ValueTraits<VectorValue>::As<SkPoint3>(const VectorValue& vec) {
SkV3 ValueTraits<VectorValue>::As<SkV3>(const VectorValue& vec) {
// best effort to turn this into a 3D point
return SkPoint3 {
return SkV3 {
vec.size() > 0 ? vec[0] : 0,
vec.size() > 1 ? vec[1] : 0,
vec.size() > 2 ? vec[2] : 0,

View File

@ -128,7 +128,7 @@ sk_sp<sksg::Transform> AnimationBuilder::attachMatrix2D(const skjson::ObjectValu
TransformAdapter3D::TransformAdapter3D(const skjson::ObjectValue& jtransform,
const AnimationBuilder& abuilder)
: INHERITED(sksg::Matrix<SkMatrix44>::Make(SkMatrix44::I())) {
: INHERITED(sksg::Matrix<SkM44>::Make(SkM44())) {
this->bind(abuilder, jtransform["a"], fAnchorPoint);
this->bind(abuilder, jtransform["p"], fPosition);
@ -148,40 +148,31 @@ void TransformAdapter3D::onSync() {
this->node()->setMatrix(this->totalMatrix());
}
SkPoint3 TransformAdapter3D::anchor_point() const {
return ValueTraits<VectorValue>::As<SkPoint3>(fAnchorPoint);
SkV3 TransformAdapter3D::anchor_point() const {
return ValueTraits<VectorValue>::As<SkV3>(fAnchorPoint);
}
SkPoint3 TransformAdapter3D::position() const {
return ValueTraits<VectorValue>::As<SkPoint3>(fPosition);
SkV3 TransformAdapter3D::position() const {
return ValueTraits<VectorValue>::As<SkV3>(fPosition);
}
SkVector3 TransformAdapter3D::rotation() const {
SkV3 TransformAdapter3D::rotation() const {
// orientation and axis-wise rotation map onto the same property.
return ValueTraits<VectorValue>::As<SkPoint3>(fOrientation) + SkPoint3{ fRx, fRy, fRz };
return ValueTraits<VectorValue>::As<SkV3>(fOrientation) + SkV3{ fRx, fRy, fRz };
}
SkMatrix44 TransformAdapter3D::totalMatrix() const {
SkM44 TransformAdapter3D::totalMatrix() const {
const auto anchor_point = this->anchor_point(),
positon = this->position(),
scale = ValueTraits<VectorValue>::As<SkPoint3>(fScale),
scale = ValueTraits<VectorValue>::As<SkV3>(fScale),
rotation = this->rotation();
SkMatrix44 m;
m.setTranslate(-anchor_point.fX, -anchor_point.fY, -anchor_point.fZ);
m.postScale(scale.fX / 100, scale.fY / 100, scale.fZ / 100);
SkMatrix44 r;
r.setRotateDegreesAbout(0, 0, 1, rotation.fZ);
m.postConcat(r);
r.setRotateDegreesAbout(0, 1, 0, rotation.fY);
m.postConcat(r);
r.setRotateDegreesAbout(1, 0, 0, rotation.fX);
m.postConcat(r);
m.postTranslate(positon.fX, positon.fY, positon.fZ);
return m;
return SkM44::Translate(positon.x, positon.y, positon.z)
* SkM44::Rotate({ 1, 0, 0 }, SkDegreesToRadians(rotation.x))
* SkM44::Rotate({ 0, 1, 0 }, SkDegreesToRadians(rotation.y))
* SkM44::Rotate({ 0, 0, 1 }, SkDegreesToRadians(rotation.z))
* SkM44::Scale(scale.x / 100, scale.y / 100, scale.z / 100)
* SkM44::Translate(-anchor_point.x, -anchor_point.y, -anchor_point.z);
}
sk_sp<sksg::Transform> AnimationBuilder::attachMatrix3D(const skjson::ObjectValue& jtransform,
@ -190,7 +181,8 @@ sk_sp<sksg::Transform> AnimationBuilder::attachMatrix3D(const skjson::ObjectValu
SkASSERT(adapter);
if (adapter->isStatic()) {
if (adapter->totalMatrix().isIdentity()) {
// TODO: SkM44::isIdentity?
if (adapter->totalMatrix() == SkM44()) {
// The transform has no observable effects - we can discard.
return parent;
}

View File

@ -11,9 +11,8 @@
#include "modules/skottie/src/SkottieAdapter.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkMatrix44.h"
#include "include/core/SkPoint.h"
#include "include/core/SkPoint3.h"
#include "include/private/SkM44.h"
#include "modules/skottie/src/Adapter.h"
namespace skjson {
@ -72,18 +71,17 @@ private:
using INHERITED = DiscardableAdapterBase<TransformAdapter2D, sksg::Matrix<SkMatrix>>;
};
class TransformAdapter3D : public DiscardableAdapterBase<TransformAdapter3D,
sksg::Matrix<SkMatrix44>> {
class TransformAdapter3D : public DiscardableAdapterBase<TransformAdapter3D, sksg::Matrix<SkM44>> {
public:
TransformAdapter3D(const skjson::ObjectValue&, const AnimationBuilder&);
~TransformAdapter3D() override;
virtual SkMatrix44 totalMatrix() const;
virtual SkM44 totalMatrix() const;
protected:
SkPoint3 anchor_point() const;
SkPoint3 position() const;
SkVector3 rotation() const;
SkV3 anchor_point() const;
SkV3 position() const;
SkV3 rotation() const;
private:
void onSync() final;
@ -96,7 +94,7 @@ private:
fRy = 0,
fRz = 0;
using INHERITED = DiscardableAdapterBase<TransformAdapter3D, sksg::Matrix<SkMatrix44>>;
using INHERITED = DiscardableAdapterBase<TransformAdapter3D, sksg::Matrix<SkM44>>;
};
} // namespace internal

View File

@ -11,7 +11,7 @@
#include "modules/sksg/include/SkSGEffectNode.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkMatrix44.h"
#include "include/private/SkM44.h"
namespace sksg {
@ -32,7 +32,7 @@ protected:
virtual bool is44() const = 0;
virtual SkMatrix asMatrix() const = 0;
virtual SkMatrix44 asMatrix44() const = 0;
virtual SkM44 asM44 () const = 0;
private:
friend class TransformPriv;
@ -43,7 +43,7 @@ private:
/**
* Concrete, matrix-backed Transform.
*
* Supported instantiations: SkMatrix, SkMatrix44.
* Supported instantiations: SkMatrix, SkM44.
*
* Sample use:
*
@ -56,7 +56,7 @@ template <typename T>
class Matrix final : public Transform {
public:
template <typename = std::enable_if<std::is_same<T, SkMatrix>::value ||
std::is_same<T, SkMatrix44>::value>>
std::is_same<T, SkM44 >::value>>
static sk_sp<Matrix> Make(const T& m) { return sk_sp<Matrix>(new Matrix(m)); }
SG_ATTRIBUTE(Matrix, T, fMatrix)
@ -68,10 +68,10 @@ protected:
return SkRect::MakeEmpty();
}
bool is44() const override { return std::is_same<T, SkMatrix44>::value; }
bool is44() const override { return std::is_same<T, SkM44>::value; }
SkMatrix asMatrix () const override { return SkMatrix(fMatrix); }
SkMatrix44 asMatrix44() const override { return fMatrix; }
SkMatrix asMatrix() const override;
SkM44 asM44 () const override;
private:
T fMatrix;

View File

@ -14,11 +14,29 @@ namespace sksg {
namespace {
template <typename T>
SkMatrix AsSkMatrix(const T&);
template <>
SkMatrix AsSkMatrix<SkMatrix>(const SkMatrix& m) { return m; }
template <>
SkMatrix AsSkMatrix<SkM44>(const SkM44& m) { return m.asM33(); }
template <typename T>
SkM44 AsSkM44(const T&);
template <>
SkM44 AsSkM44<SkMatrix>(const SkMatrix& m) { return SkM44(m); }
template <>
SkM44 AsSkM44<SkM44>(const SkM44& m) { return m; }
template <typename T>
class Concat final : public Transform {
public:
template <typename = std::enable_if<std::is_same<T, SkMatrix>::value ||
std::is_same<T, SkMatrix44>::value >>
std::is_same<T, SkM44 >::value >>
Concat(sk_sp<Transform> a, sk_sp<Transform> b)
: fA(std::move(a)), fB(std::move(b)) {
SkASSERT(fA);
@ -43,16 +61,16 @@ protected:
return SkRect::MakeEmpty();
}
bool is44() const override { return std::is_same<T, SkMatrix44>::value; }
bool is44() const override { return std::is_same<T, SkM44>::value; }
SkMatrix asMatrix() const override {
SkASSERT(!this->hasInval());
return SkMatrix(fComposed);
return AsSkMatrix(fComposed);
}
SkMatrix44 asMatrix44() const override {
SkM44 asM44() const override {
SkASSERT(!this->hasInval());
return fComposed;
return AsSkM44(fComposed);
}
private:
@ -66,7 +84,7 @@ template <typename T>
class Inverse final : public Transform {
public:
template <typename = std::enable_if<std::is_same<T, SkMatrix>::value ||
std::is_same<T, SkMatrix44>::value >>
std::is_same<T, SkM44 >::value >>
explicit Inverse(sk_sp<Transform> t)
: fT(std::move(t)) {
SkASSERT(fT);
@ -83,22 +101,22 @@ protected:
fT->revalidate(ic, ctm);
if (!TransformPriv::As<T>(fT).invert(&fInverted)) {
fInverted.reset();
fInverted.setIdentity();
}
return SkRect::MakeEmpty();
}
bool is44() const override { return std::is_same<T, SkMatrix44>::value; }
bool is44() const override { return std::is_same<T, SkM44>::value; }
SkMatrix asMatrix() const override {
SkASSERT(!this->hasInval());
return SkMatrix(fInverted);
return AsSkMatrix(fInverted);
}
SkMatrix44 asMatrix44() const override {
SkM44 asM44() const override {
SkASSERT(!this->hasInval());
return fInverted;
return AsSkM44(fInverted);
}
private:
@ -110,6 +128,18 @@ private:
} // namespace
template <>
SkMatrix Matrix<SkMatrix>::asMatrix() const { return fMatrix; }
template <>
SkM44 Matrix<SkMatrix>::asM44() const { return SkM44(fMatrix); }
template <>
SkMatrix Matrix<SkM44>::asMatrix() const { return fMatrix.asM33(); }
template <>
SkM44 Matrix<SkM44>::asM44() const { return fMatrix; }
// Transform nodes don't generate damage on their own, but via ancestor TransformEffects.
Transform::Transform() : INHERITED(kBubbleDamage_Trait) {}
@ -123,7 +153,7 @@ sk_sp<Transform> Transform::MakeConcat(sk_sp<Transform> a, sk_sp<Transform> b) {
}
return TransformPriv::Is44(a) || TransformPriv::Is44(b)
? sk_sp<Transform>(new Concat<SkMatrix44>(std::move(a), std::move(b)))
? sk_sp<Transform>(new Concat<SkM44 >(std::move(a), std::move(b)))
: sk_sp<Transform>(new Concat<SkMatrix>(std::move(a), std::move(b)));
}
@ -133,7 +163,7 @@ sk_sp<Transform> Transform::MakeInverse(sk_sp<Transform> t) {
}
return TransformPriv::Is44(t)
? sk_sp<Transform>(new Inverse<SkMatrix44>(std::move(t)))
? sk_sp<Transform>(new Inverse<SkM44 >(std::move(t)))
: sk_sp<Transform>(new Inverse<SkMatrix>(std::move(t)));
}

View File

@ -19,7 +19,7 @@ public:
static bool Is44(const sk_sp<Transform>&t) { return t->is44(); }
template <typename T, typename = std::enable_if<std::is_same<T, SkMatrix>::value ||
std::is_same<T, SkMatrix44>::value >>
std::is_same<T, SkM44 >::value >>
static T As(const sk_sp<Transform>&);
private:
@ -32,8 +32,8 @@ inline SkMatrix TransformPriv::As<SkMatrix>(const sk_sp<Transform>& t) {
}
template <>
inline SkMatrix44 TransformPriv::As<SkMatrix44>(const sk_sp<Transform>& t) {
return t->asMatrix44();
inline SkM44 TransformPriv::As<SkM44>(const sk_sp<Transform>& t) {
return t->asM44();
}
} // namespace sksg