From 105b988e1a6fd898bbdc074b9de48744e2fc6c70 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 19 Oct 2021 13:27:55 -0700 Subject: [PATCH] Move MatrixDecomposition into its TU. --- engine/src/flutter/impeller/fixtures/BUILD.gn | 2 +- engine/src/flutter/impeller/geometry/BUILD.gn | 2 ++ .../impeller/geometry/geometry_unittests.cc | 16 ++++----- .../src/flutter/impeller/geometry/matrix.cc | 14 ++++---- engine/src/flutter/impeller/geometry/matrix.h | 28 +++------------- .../impeller/geometry/matrix_decomposition.cc | 11 +++++++ .../impeller/geometry/matrix_decomposition.h | 33 +++++++++++++++++++ 7 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 engine/src/flutter/impeller/geometry/matrix_decomposition.cc create mode 100644 engine/src/flutter/impeller/geometry/matrix_decomposition.h diff --git a/engine/src/flutter/impeller/fixtures/BUILD.gn b/engine/src/flutter/impeller/fixtures/BUILD.gn index 6e664a4e2f..3bbffb6091 100644 --- a/engine/src/flutter/impeller/fixtures/BUILD.gn +++ b/engine/src/flutter/impeller/fixtures/BUILD.gn @@ -30,7 +30,7 @@ test_fixtures("file_fixtures") { group("fixtures") { testonly = true - deps = [ + public_deps = [ ":file_fixtures", ":shader_fixtures", ] diff --git a/engine/src/flutter/impeller/geometry/BUILD.gn b/engine/src/flutter/impeller/geometry/BUILD.gn index f57a99bfe6..414b5026f0 100644 --- a/engine/src/flutter/impeller/geometry/BUILD.gn +++ b/engine/src/flutter/impeller/geometry/BUILD.gn @@ -10,6 +10,8 @@ impeller_component("geometry") { "color.h", "matrix.cc", "matrix.h", + "matrix_decomposition.cc", + "matrix_decomposition.h", "path.cc", "path.h", "path_builder.cc", diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 13b39bd34a..f7f270dfa6 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -64,9 +64,9 @@ TEST(GeometryTest, TestDecomposition) { auto result = rotated.Decompose(); - ASSERT_TRUE(result.first); + ASSERT_TRUE(result.has_value()); - Matrix::Decomposition res = result.second; + MatrixDecomposition res = result.value(); auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4}; ASSERT_QUATERNION_NEAR(res.rotation, quaternion); @@ -79,9 +79,9 @@ TEST(GeometryTest, TestDecomposition2) { auto result = (translated * rotated * scaled).Decompose(); - ASSERT_TRUE(result.first); + ASSERT_TRUE(result.has_value()); - Matrix::Decomposition res = result.second; + MatrixDecomposition res = result.value(); auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4}; @@ -104,9 +104,9 @@ TEST(GeometryTest, TestRecomposition) { auto result = rotated.Decompose(); - ASSERT_TRUE(result.first); + ASSERT_TRUE(result.has_value()); - Matrix::Decomposition res = result.second; + MatrixDecomposition res = result.value(); auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4}; @@ -125,9 +125,9 @@ TEST(GeometryTest, TestRecomposition2) { auto result = matrix.Decompose(); - ASSERT_TRUE(result.first); + ASSERT_TRUE(result.has_value()); - ASSERT_MATRIX_NEAR(matrix, Matrix{result.second}); + ASSERT_MATRIX_NEAR(matrix, Matrix{result.value()}); } TEST(GeometryTest, QuaternionLerp) { diff --git a/engine/src/flutter/impeller/geometry/matrix.cc b/engine/src/flutter/impeller/geometry/matrix.cc index 5f77fcced0..f26411039a 100644 --- a/engine/src/flutter/impeller/geometry/matrix.cc +++ b/engine/src/flutter/impeller/geometry/matrix.cc @@ -9,7 +9,7 @@ namespace impeller { -Matrix::Matrix(const Decomposition& d) : Matrix() { +Matrix::Matrix(const MatrixDecomposition& d) : Matrix() { /* * Apply perspective. */ @@ -197,14 +197,14 @@ Scalar Matrix::GetDeterminant() const { * Adapted for Impeller from Graphics Gems: * http://www.realtimerendering.com/resources/GraphicsGems/gemsii/unmatrix.c */ -Matrix::DecompositionResult Matrix::Decompose() const { +std::optional Matrix::Decompose() const { /* * Normalize the matrix. */ Matrix self = *this; if (self.e[3][3] == 0) { - return {false, {}}; + return std::nullopt; } for (int i = 0; i < 4; i++) { @@ -225,10 +225,10 @@ Matrix::DecompositionResult Matrix::Decompose() const { perpectiveMatrix.e[3][3] = 1; if (perpectiveMatrix.GetDeterminant() == 0.0) { - return {false, {}}; + return std::nullopt; } - Decomposition result; + MatrixDecomposition result; /* * ========================================================================== @@ -355,10 +355,10 @@ Matrix::DecompositionResult Matrix::Decompose() const { result.rotation.z = -result.rotation.z; } - return DecompositionResult(true, result); + return result; } -uint64_t Matrix::Decomposition::GetComponentsMask() const { +uint64_t MatrixDecomposition::GetComponentsMask() const { uint64_t mask = 0; Quaternion noRotation(0.0, 0.0, 0.0, 1.0); diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index f88eafb6e0..3156a81009 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -5,8 +5,10 @@ #pragma once #include +#include #include +#include "impeller/geometry/matrix_decomposition.h" #include "impeller/geometry/point.h" #include "impeller/geometry/quaternion.h" #include "impeller/geometry/scalar.h" @@ -35,28 +37,6 @@ struct Matrix { Vector4 vec[4]; }; - struct Decomposition { - Vector3 translation; - Vector3 scale; - Shear shear; - Vector4 perspective; - Quaternion rotation; - - enum class Component { - kTranslation = 1 << 0, - kScale = 1 << 1, - kShear = 1 << 2, - kPerspective = 1 << 3, - kRotation = 1 << 4, - }; - - uint64_t GetComponentsMask() const; - }; - - // TODO(csg): Radar restrictions of C++11 don't exist. Use optionals instead. - using DecompositionResult = - std::pair; - //---------------------------------------------------------------------------- /// Construts a default identity matrix. /// @@ -79,7 +59,7 @@ struct Matrix { Vector4(m12, m13, m14, m15)} {} // clang-format on - Matrix(const Decomposition& decomposition); + Matrix(const MatrixDecomposition& decomposition); static constexpr Matrix MakeTranslation(const Vector3& t) { // clang-format off @@ -243,7 +223,7 @@ struct Matrix { ); } - DecompositionResult Decompose() const; + std::optional Decompose() const; constexpr bool operator==(const Matrix& m) const { // clang-format off diff --git a/engine/src/flutter/impeller/geometry/matrix_decomposition.cc b/engine/src/flutter/impeller/geometry/matrix_decomposition.cc new file mode 100644 index 0000000000..a945345b30 --- /dev/null +++ b/engine/src/flutter/impeller/geometry/matrix_decomposition.cc @@ -0,0 +1,11 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/geometry/matrix_decomposition.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/matrix_decomposition.h b/engine/src/flutter/impeller/geometry/matrix_decomposition.h new file mode 100644 index 0000000000..def9029ea7 --- /dev/null +++ b/engine/src/flutter/impeller/geometry/matrix_decomposition.h @@ -0,0 +1,33 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "flutter/fml/macros.h" +#include "impeller/geometry/quaternion.h" +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/shear.h" +#include "impeller/geometry/vector.h" + +namespace impeller { + +struct MatrixDecomposition { + Vector3 translation; + Vector3 scale; + Shear shear; + Vector4 perspective; + Quaternion rotation; + + enum class Component { + kTranslation = 1 << 0, + kScale = 1 << 1, + kShear = 1 << 2, + kPerspective = 1 << 3, + kRotation = 1 << 4, + }; + + uint64_t GetComponentsMask() const; +}; + +} // namespace impeller