diff --git a/engine/src/flutter/impeller/compositor/formats_metal.mm b/engine/src/flutter/impeller/compositor/formats_metal.mm index 69a2919226..1e9ea463f8 100644 --- a/engine/src/flutter/impeller/compositor/formats_metal.mm +++ b/engine/src/flutter/impeller/compositor/formats_metal.mm @@ -92,7 +92,8 @@ static ColorRenderPassAttachment FromMTLRenderPassColorAttachmentDescriptor( ConfigureRenderPassAttachment(attachment, desc); auto clear = desc.clearColor; attachment.clear_color = - Color{clear.red, clear.green, clear.blue, clear.alpha}; + Color{static_cast(clear.red), static_cast(clear.green), + static_cast(clear.blue), static_cast(clear.alpha)}; return attachment; } diff --git a/engine/src/flutter/impeller/compositor/texture.mm b/engine/src/flutter/impeller/compositor/texture.mm index fec9cf47c6..aab70094cd 100644 --- a/engine/src/flutter/impeller/compositor/texture.mm +++ b/engine/src/flutter/impeller/compositor/texture.mm @@ -11,8 +11,8 @@ Texture::Texture(id texture) : texture_(texture) {} Texture::~Texture() = default; Size Texture::GetSize() const { - return {static_cast(texture_.width), - static_cast(texture_.height)}; + return {static_cast(texture_.width), + static_cast(texture_.height)}; } id Texture::GetMTLTexture() const { diff --git a/engine/src/flutter/impeller/geometry/BUILD.gn b/engine/src/flutter/impeller/geometry/BUILD.gn index f987641c93..c18b8ff5d8 100644 --- a/engine/src/flutter/impeller/geometry/BUILD.gn +++ b/engine/src/flutter/impeller/geometry/BUILD.gn @@ -22,6 +22,7 @@ impeller_component("geometry") { "quaternion.h", "rect.cc", "rect.h", + "scalar.h", "shear.cc", "shear.h", "size.cc", diff --git a/engine/src/flutter/impeller/geometry/color.h b/engine/src/flutter/impeller/geometry/color.h index 2b442d114c..3e0104dd61 100644 --- a/engine/src/flutter/impeller/geometry/color.h +++ b/engine/src/flutter/impeller/geometry/color.h @@ -7,6 +7,8 @@ #include #include +#include "impeller/geometry/scalar.h" + namespace impeller { struct ColorHSB; @@ -18,28 +20,28 @@ struct Color { /** * The red color component (0 to 1) */ - double red = 0.0; + Scalar red = 0.0; /** * The green color component (0 to 1) */ - double green = 0.0; + Scalar green = 0.0; /** * The blue color component (0 to 1) */ - double blue = 0.0; + Scalar blue = 0.0; /** * The alpha component of the color (0 to 1) */ - double alpha = 0.0; + Scalar alpha = 0.0; constexpr Color() {} Color(const ColorHSB& hsbColor); - constexpr Color(double r, double g, double b, double a) + constexpr Color(Scalar r, Scalar g, Scalar b, Scalar a) : red(r), green(g), blue(b), alpha(a) {} constexpr bool operator==(const Color& c) const { @@ -641,24 +643,24 @@ struct ColorHSB { /** * The hue of the color (0 to 1) */ - double hue; + Scalar hue; /** * The saturation of the color (0 to 1) */ - double saturation; + Scalar saturation; /** * The brightness of the color (0 to 1) */ - double brightness; + Scalar brightness; /** * The alpha of the color (0 to 1) */ - double alpha; + Scalar alpha; - ColorHSB(double h, double s, double b, double a) + constexpr ColorHSB(Scalar h, Scalar s, Scalar b, Scalar a) : hue(h), saturation(s), brightness(b), alpha(a) {} static ColorHSB FromRGB(Color rgb); @@ -668,4 +670,6 @@ struct ColorHSB { std::string ToString() const; }; +static_assert(sizeof(Color) == 4 * sizeof(Scalar)); + } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/matrix.cc b/engine/src/flutter/impeller/geometry/matrix.cc index 302d07d06e..3d8a457ef9 100644 --- a/engine/src/flutter/impeller/geometry/matrix.cc +++ b/engine/src/flutter/impeller/geometry/matrix.cc @@ -84,18 +84,18 @@ Matrix::Matrix(const Decomposition& d) : Matrix() { } } -Matrix Matrix::MakeOrthographic(double left, - double right, - double bottom, - double top, - double nearZ, - double farZ) { - double ral = right + left; - double rsl = right - left; - double tab = top + bottom; - double tsb = top - bottom; - double fan = farZ + nearZ; - double fsn = farZ - nearZ; +Matrix Matrix::MakeOrthographic(Scalar left, + Scalar right, + Scalar bottom, + Scalar top, + Scalar nearZ, + Scalar farZ) { + Scalar ral = right + left; + Scalar rsl = right - left; + Scalar tab = top + bottom; + Scalar tsb = top - bottom; + Scalar fan = farZ + nearZ; + Scalar fsn = farZ - nearZ; // clang-format off return Matrix(2.0 / rsl, 0.0, 0.0, 0.0, @@ -110,11 +110,11 @@ Matrix Matrix::MakeOrthographic(const Size& size) { INT_MAX); } -Matrix Matrix::MakePerspective(double fov, - double aspect, - double nearZ, - double farZ) { - double cotan = 1.0 / tan(fov / 2.0); +Matrix Matrix::MakePerspective(Scalar fov, + Scalar aspect, + Scalar nearZ, + Scalar farZ) { + Scalar cotan = 1.0 / tan(fov / 2.0); return Matrix(cotan / aspect, 0.0, 0.0, 0.0, // 0.0, cotan, 0.0, 0.0, // @@ -213,7 +213,7 @@ Matrix Matrix::Invert() const { m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5]}; - double det = + Scalar det = m[0] * tmp.m[0] + m[1] * tmp.m[4] + m[2] * tmp.m[8] + m[3] * tmp.m[12]; if (det == 0) { @@ -237,7 +237,7 @@ Matrix Matrix::Transpose() const { }; } -double Matrix::GetDeterminant() const { +Scalar Matrix::GetDeterminant() const { auto a00 = e[0][0]; auto a01 = e[0][1]; auto a02 = e[0][2]; diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index d35c59f622..f33f1755ba 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -7,18 +7,19 @@ #include #include -#include "point.h" -#include "quaternion.h" -#include "shear.h" -#include "size.h" -#include "vector.h" +#include "impeller/geometry/point.h" +#include "impeller/geometry/quaternion.h" +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/shear.h" +#include "impeller/geometry/size.h" +#include "impeller/geometry/vector.h" namespace impeller { struct Matrix { union { - double m[16]; - double e[4][4]; + Scalar m[16]; + Scalar e[4][4]; Vector4 vec[4]; }; @@ -45,7 +46,7 @@ struct Matrix { using DecompositionResult = std::pair; - Matrix() + constexpr Matrix() // clang-format off : vec{ Vector4(1.0, 0.0, 0.0, 0.0), Vector4(0.0, 1.0, 0.0, 0.0), @@ -54,10 +55,10 @@ struct Matrix { // clang-format on // clang-format off - Matrix(double m0, double m1, double m2, double m3, - double m4, double m5, double m6, double m7, - double m8, double m9, double m10, double m11, - double m12, double m13, double m14, double m15) + constexpr Matrix(Scalar m0, Scalar m1, Scalar m2, Scalar m3, + Scalar m4, Scalar m5, Scalar m6, Scalar m7, + Scalar m8, Scalar m9, Scalar m10, Scalar m11, + Scalar m12, Scalar m13, Scalar m14, Scalar m15) : vec{Vector4(m0, m1, m2, m3), Vector4(m4, m5, m6, m7), Vector4(m8, m9, m10, m11), @@ -66,7 +67,7 @@ struct Matrix { Matrix(const Decomposition& decomposition); - static Matrix MakeTranslation(const Vector3& t) { + static constexpr Matrix MakeTranslation(const Vector3& t) { // clang-format off return Matrix(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, @@ -75,7 +76,7 @@ struct Matrix { // clang-format on } - static Matrix MakeScale(const Vector3& s) { + static constexpr Matrix MakeScale(const Vector3& s) { // clang-format off return Matrix(s.x, 0.0, 0.0, 0.0, 0.0, s.y, 0.0, 0.0, @@ -84,12 +85,12 @@ struct Matrix { // clang-format on } - static Matrix MakeRotation(double radians, const Vector4& r) { + static Matrix MakeRotation(Scalar radians, const Vector4& r) { const Vector4 v = r.Normalize(); - const double cosine = cos(radians); - const double cosp = 1.0f - cosine; - const double sine = sin(radians); + const Scalar cosine = cos(radians); + const Scalar cosp = 1.0f - cosine; + const Scalar sine = sin(radians); // clang-format off return Matrix( @@ -115,9 +116,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationX(double radians) { - double cosine = cos(radians); - double sine = sin(radians); + static Matrix MakeRotationX(Scalar radians) { + Scalar cosine = cos(radians); + Scalar sine = sin(radians); // clang-format off return Matrix( 1.0, 0.0, 0.0, 0.0, @@ -128,9 +129,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationY(double radians) { - double cosine = cos(radians); - double sine = sin(radians); + static Matrix MakeRotationY(Scalar radians) { + Scalar cosine = cos(radians); + Scalar sine = sin(radians); // clang-format off return Matrix( @@ -142,9 +143,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationZ(double radians) { - double cosine = cos(radians); - double sine = sin(radians); + static Matrix MakeRotationZ(Scalar radians) { + Scalar cosine = cos(radians); + Scalar sine = sin(radians); // clang-format off return Matrix ( @@ -156,12 +157,12 @@ struct Matrix { // clang-format on } - static Matrix MakeOrthographic(double left, - double right, - double bottom, - double top, - double nearZ, - double farZ); + static Matrix MakeOrthographic(Scalar left, + Scalar right, + Scalar bottom, + Scalar top, + Scalar nearZ, + Scalar farZ); static Matrix MakeOrthographic(const Size& size); @@ -175,16 +176,16 @@ struct Matrix { * * @return the perspective projection matrix. */ - static Matrix MakePerspective(double fov, - double aspect, - double nearZ, - double farZ); + static Matrix MakePerspective(Scalar fov, + Scalar aspect, + Scalar nearZ, + Scalar farZ); static Matrix MakeLookAt(const Vector3& eye, const Vector3& center, const Vector3& up); - Matrix Translate(const Vector3& t) const { + constexpr Matrix Translate(const Vector3& t) const { // clang-format off return Matrix(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], @@ -196,7 +197,7 @@ struct Matrix { // clang-format on } - Matrix Scale(const Vector3& s) const { + constexpr Matrix Scale(const Vector3& s) const { // clang-format off return Matrix(m[0] * s.x, m[1] * s.x, m[2] * s.x , m[3] * s.x, m[4] * s.y, m[5] * s.y, m[6] * s.y , m[7] * s.y, @@ -205,7 +206,7 @@ struct Matrix { // clang-format on } - Matrix Multiply(const Matrix& o) const { + constexpr Matrix Multiply(const Matrix& o) const { // clang-format off return Matrix( m[0] * o.m[0] + m[4] * o.m[1] + m[8] * o.m[2] + m[12] * o.m[3], @@ -231,14 +232,14 @@ struct Matrix { Matrix Invert() const; - double GetDeterminant() const; + Scalar GetDeterminant() const; - bool IsAffine() const { + constexpr bool IsAffine() const { return (m[2] == 0 && m[3] == 0 && m[6] == 0 && m[7] == 0 && m[8] == 0 && m[9] == 0 && m[10] == 1 && m[11] == 0 && m[14] == 0 && m[15] == 1); } - bool IsIdentity() const { + constexpr bool IsIdentity() const { return ( // clang-format off m[0] == 1.0 && m[1] == 0.0 && m[2] == 0.0 && m[3] == 0.0 && @@ -251,7 +252,7 @@ struct Matrix { DecompositionResult Decompose() const; - bool operator==(const Matrix& m) const { + constexpr bool operator==(const Matrix& m) const { // clang-format off return vec[0] == m.vec[0] && vec[1] == m.vec[1] @@ -260,7 +261,7 @@ struct Matrix { // clang-format on } - bool operator!=(const Matrix& m) const { + constexpr bool operator!=(const Matrix& m) const { // clang-format off return vec[0] != m.vec[0] || vec[1] != m.vec[1] @@ -284,7 +285,7 @@ struct Matrix { void FromString(const std::string& str); }; -static_assert(sizeof(struct Matrix) == sizeof(double) * 16, +static_assert(sizeof(struct Matrix) == sizeof(Scalar) * 16, "The matrix must be of consistent size."); inline Vector4 operator*(const Vector4& v, const Matrix& m) { diff --git a/engine/src/flutter/impeller/geometry/path_builder.cc b/engine/src/flutter/impeller/geometry/path_builder.cc index 47c42d005e..b6d419bede 100644 --- a/engine/src/flutter/impeller/geometry/path_builder.cc +++ b/engine/src/flutter/impeller/geometry/path_builder.cc @@ -6,7 +6,7 @@ namespace impeller { -static const double kArcApproximationMagic = 0.551915024494; +static const Scalar kArcApproximationMagic = 0.551915024494; PathBuilder::PathBuilder() = default; @@ -34,7 +34,7 @@ PathBuilder& PathBuilder::LineTo(Point point, bool relative) { return *this; } -PathBuilder& PathBuilder::HorizontalLineTo(double x, bool relative) { +PathBuilder& PathBuilder::HorizontalLineTo(Scalar x, bool relative) { Point endpoint = relative ? Point{current_.x + x, current_.y} : Point{x, current_.y}; prototype_.AddLinearComponent(current_, endpoint); @@ -42,7 +42,7 @@ PathBuilder& PathBuilder::HorizontalLineTo(double x, bool relative) { return *this; } -PathBuilder& PathBuilder::VerticalLineTo(double y, bool relative) { +PathBuilder& PathBuilder::VerticalLineTo(Scalar y, bool relative) { Point endpoint = relative ? Point{current_.x, current_.y + y} : Point{current_.x, y}; prototype_.AddLinearComponent(current_, endpoint); @@ -158,11 +158,11 @@ PathBuilder& PathBuilder::AddRect(Rect rect) { return *this; } -PathBuilder& PathBuilder::AddCircle(const Point& center, double radius) { +PathBuilder& PathBuilder::AddCircle(const Point& center, Scalar radius) { current_ = center + Point{0.0, radius}; - const double diameter = radius * 2.0; - const double magic = kArcApproximationMagic * radius; + const Scalar diameter = radius * 2.0; + const Scalar magic = kArcApproximationMagic * radius; prototype_.AddCubicComponent( {center.x + radius, center.y}, // @@ -194,7 +194,7 @@ PathBuilder& PathBuilder::AddCircle(const Point& center, double radius) { return *this; } -PathBuilder& PathBuilder::AddRoundedRect(Rect rect, double radius) { +PathBuilder& PathBuilder::AddRoundedRect(Rect rect, Scalar radius) { return radius == 0.0 ? AddRect(rect) : AddRoundedRect(rect, {radius, radius, radius, radius}); } @@ -202,10 +202,10 @@ PathBuilder& PathBuilder::AddRoundedRect(Rect rect, double radius) { PathBuilder& PathBuilder::AddRoundedRect(Rect rect, RoundingRadii radii) { current_ = rect.origin + Point{radii.topLeft, 0.0}; - const double magicTopRight = kArcApproximationMagic * radii.topRight; - const double magicBottomRight = kArcApproximationMagic * radii.bottomRight; - const double magicBottomLeft = kArcApproximationMagic * radii.bottomLeft; - const double magicTopLeft = kArcApproximationMagic * radii.topLeft; + const Scalar magicTopRight = kArcApproximationMagic * radii.topRight; + const Scalar magicBottomRight = kArcApproximationMagic * radii.bottomRight; + const Scalar magicBottomLeft = kArcApproximationMagic * radii.bottomLeft; + const Scalar magicTopLeft = kArcApproximationMagic * radii.topLeft; /* * Top line. @@ -287,7 +287,7 @@ PathBuilder& PathBuilder::AddRoundedRect(Rect rect, RoundingRadii radii) { PathBuilder& PathBuilder::AddEllipse(const Point& center, const Size& radius) { current_ = center + Point{0.0, radius.height}; - const Size diameter = {radius.width * 2.0, radius.height * 2.0}; + const Size diameter = {radius.width * 2.0f, radius.height * 2.0f}; const Size magic = {kArcApproximationMagic * radius.width, kArcApproximationMagic * radius.height}; diff --git a/engine/src/flutter/impeller/geometry/path_builder.h b/engine/src/flutter/impeller/geometry/path_builder.h index 177bd077a8..786f4c26ab 100644 --- a/engine/src/flutter/impeller/geometry/path_builder.h +++ b/engine/src/flutter/impeller/geometry/path_builder.h @@ -5,8 +5,9 @@ #pragma once #include "flutter/fml/macros.h" -#include "path.h" -#include "rect.h" +#include "impeller/geometry/path.h" +#include "impeller/geometry/rect.h" +#include "impeller/geometry/scalar.h" namespace impeller { @@ -24,9 +25,9 @@ class PathBuilder { PathBuilder& LineTo(Point point, bool relative = false); - PathBuilder& HorizontalLineTo(double x, bool relative = false); + PathBuilder& HorizontalLineTo(Scalar x, bool relative = false); - PathBuilder& VerticalLineTo(double y, bool relative = false); + PathBuilder& VerticalLineTo(Scalar y, bool relative = false); PathBuilder& QuadraticCurveTo(Point point, Point controlPoint, @@ -45,24 +46,24 @@ class PathBuilder { PathBuilder& AddRect(Rect rect); - PathBuilder& AddRoundedRect(Rect rect, double radius); + PathBuilder& AddRoundedRect(Rect rect, Scalar radius); - PathBuilder& AddCircle(const Point& center, double radius); + PathBuilder& AddCircle(const Point& center, Scalar radius); PathBuilder& AddEllipse(const Point& center, const Size& size); struct RoundingRadii { - double topLeft = 0.0; - double bottomLeft = 0.0; - double topRight = 0.0; - double bottomRight = 0.0; + Scalar topLeft = 0.0; + Scalar bottomLeft = 0.0; + Scalar topRight = 0.0; + Scalar bottomRight = 0.0; RoundingRadii() {} - RoundingRadii(double pTopLeft, - double pBottomLeft, - double pTopRight, - double pBottomRight) + RoundingRadii(Scalar pTopLeft, + Scalar pBottomLeft, + Scalar pTopRight, + Scalar pBottomRight) : topLeft(pTopLeft), bottomLeft(pBottomLeft), topRight(pTopRight), diff --git a/engine/src/flutter/impeller/geometry/path_component.cc b/engine/src/flutter/impeller/geometry/path_component.cc index 3d5dad4513..f7055c39b7 100644 --- a/engine/src/flutter/impeller/geometry/path_component.cc +++ b/engine/src/flutter/impeller/geometry/path_component.cc @@ -8,54 +8,54 @@ namespace impeller { static const size_t kRecursionLimit = 32; -static const double kCurveCollinearityEpsilon = 1e-30; -static const double kCurveAngleToleranceEpsilon = 0.01; +static const Scalar kCurveCollinearityEpsilon = 1e-30; +static const Scalar kCurveAngleToleranceEpsilon = 0.01; /* * Based on: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases */ -static inline double LinearSolve(double t, double p0, double p1) { +static inline Scalar LinearSolve(Scalar t, Scalar p0, Scalar p1) { return p0 + t * (p1 - p0); } -static inline double QuadraticSolve(double t, double p0, double p1, double p2) { +static inline Scalar QuadraticSolve(Scalar t, Scalar p0, Scalar p1, Scalar p2) { return (1 - t) * (1 - t) * p0 + // 2 * (1 - t) * t * p1 + // t * t * p2; } -static inline double QuadraticSolveDerivative(double t, - double p0, - double p1, - double p2) { +static inline Scalar QuadraticSolveDerivative(Scalar t, + Scalar p0, + Scalar p1, + Scalar p2) { return 2 * (1 - t) * (p1 - p0) + // 2 * t * (p2 - p1); } -static inline double CubicSolve(double t, - double p0, - double p1, - double p2, - double p3) { +static inline Scalar CubicSolve(Scalar t, + Scalar p0, + Scalar p1, + Scalar p2, + Scalar p3) { return (1 - t) * (1 - t) * (1 - t) * p0 + // 3 * (1 - t) * (1 - t) * t * p1 + // 3 * (1 - t) * t * t * p2 + // t * t * t * p3; } -static inline double CubicSolveDerivative(double t, - double p0, - double p1, - double p2, - double p3) { +static inline Scalar CubicSolveDerivative(Scalar t, + Scalar p0, + Scalar p1, + Scalar p2, + Scalar p3) { return -3 * p0 * (1 - t) * (1 - t) + // p1 * (3 * (1 - t) * (1 - t) - 6 * (1 - t) * t) + p2 * (6 * (1 - t) * t - 3 * t * t) + // 3 * p3 * t * t; } -Point LinearPathComponent::Solve(double time) const { +Point LinearPathComponent::Solve(Scalar time) const { return { LinearSolve(time, p1.x, p2.x), // x LinearSolve(time, p1.y, p2.y), // y @@ -70,14 +70,14 @@ std::vector LinearPathComponent::Extrema() const { return {p1, p2}; } -Point QuadraticPathComponent::Solve(double time) const { +Point QuadraticPathComponent::Solve(Scalar time) const { return { QuadraticSolve(time, p1.x, cp.x, p2.x), // x QuadraticSolve(time, p1.y, cp.y, p2.y), // y }; } -Point QuadraticPathComponent::SolveDerivative(double time) const { +Point QuadraticPathComponent::SolveDerivative(Scalar time) const { return { QuadraticSolveDerivative(time, p1.x, cp.x, p2.x), // x QuadraticSolveDerivative(time, p1.y, cp.y, p2.y), // y @@ -95,14 +95,14 @@ std::vector QuadraticPathComponent::Sxtrema() const { return elevated.Extrema(); } -Point CubicPathComponent::Solve(double time) const { +Point CubicPathComponent::Solve(Scalar time) const { return { CubicSolve(time, p1.x, cp1.x, cp2.x, p2.x), // x CubicSolve(time, p1.y, cp1.y, cp2.y, p2.y), // y }; } -Point CubicPathComponent::SolveDerivative(double time) const { +Point CubicPathComponent::SolveDerivative(Scalar time) const { return { CubicSolveDerivative(time, p1.x, cp1.x, cp2.x, p2.x), // x CubicSolveDerivative(time, p1.y, cp1.y, cp2.y, p2.y), // y @@ -141,12 +141,12 @@ static void CubicPathSmoothenRecursive(const SmoothingApproximation& approx, * Attempt approximation using single straight line. */ auto d = p4 - p1; - double d2 = fabs(((p2.x - p4.x) * d.y - (p2.y - p4.y) * d.x)); - double d3 = fabs(((p3.x - p4.x) * d.y - (p3.y - p4.y) * d.x)); + Scalar d2 = fabs(((p2.x - p4.x) * d.y - (p2.y - p4.y) * d.x)); + Scalar d3 = fabs(((p3.x - p4.x) * d.y - (p3.y - p4.y) * d.x)); - double da1 = 0; - double da2 = 0; - double k = 0; + Scalar da1 = 0; + Scalar da2 = 0; + Scalar k = 0; switch ((static_cast(d2 > kCurveCollinearityEpsilon) << 1) + static_cast(d3 > kCurveCollinearityEpsilon)) { @@ -342,22 +342,22 @@ std::vector CubicPathComponent::SmoothPoints( return points; } -static inline bool NearEqual(double a, double b, double epsilon) { +static inline bool NearEqual(Scalar a, Scalar b, Scalar epsilon) { return (a > (b - epsilon)) && (a < (b + epsilon)); } -static inline bool NearZero(double a) { +static inline bool NearZero(Scalar a) { return NearEqual(a, 0.0, 1e-12); } -static void CubicPathBoundingPopulateValues(std::vector& values, - double p1, - double p2, - double p3, - double p4) { - const double a = 3.0 * (-p1 + 3.0 * p2 - 3.0 * p3 + p4); - const double b = 6.0 * (p1 - 2.0 * p2 + p3); - const double c = 3.0 * (p2 - p1); +static void CubicPathBoundingPopulateValues(std::vector& values, + Scalar p1, + Scalar p2, + Scalar p3, + Scalar p4) { + const Scalar a = 3.0 * (-p1 + 3.0 * p2 - 3.0 * p3 + p4); + const Scalar b = 6.0 * (p1 - 2.0 * p2 + p3); + const Scalar c = 3.0 * (p2 - p1); /* * Boundary conditions. @@ -367,30 +367,30 @@ static void CubicPathBoundingPopulateValues(std::vector& values, return; } - double t = -c / b; + Scalar t = -c / b; if (t >= 0.0 && t <= 1.0) { values.emplace_back(t); } return; } - double b2Minus4AC = (b * b) - (4.0 * a * c); + Scalar b2Minus4AC = (b * b) - (4.0 * a * c); if (b2Minus4AC < 0.0) { return; } - double rootB2Minus4AC = ::sqrt(b2Minus4AC); + Scalar rootB2Minus4AC = ::sqrt(b2Minus4AC); { - double t = (-b + rootB2Minus4AC) / (2.0 * a); + Scalar t = (-b + rootB2Minus4AC) / (2.0 * a); if (t >= 0.0 && t <= 1.0) { values.emplace_back(t); } } { - double t = (-b - rootB2Minus4AC) / (2.0 * a); + Scalar t = (-b - rootB2Minus4AC) / (2.0 * a); if (t >= 0.0 && t <= 1.0) { values.emplace_back(t); } @@ -401,7 +401,7 @@ std::vector CubicPathComponent::Extrema() const { /* * As described in: https://pomax.github.io/bezierinfo/#extremities */ - std::vector values; + std::vector values; CubicPathBoundingPopulateValues(values, p1.x, cp1.x, cp2.x, p2.x); CubicPathBoundingPopulateValues(values, p1.y, cp1.y, cp2.y, p2.y); diff --git a/engine/src/flutter/impeller/geometry/path_component.h b/engine/src/flutter/impeller/geometry/path_component.h index c4486da7cc..c9e1e23730 100644 --- a/engine/src/flutter/impeller/geometry/path_component.h +++ b/engine/src/flutter/impeller/geometry/path_component.h @@ -6,24 +6,25 @@ #include -#include "rect.h" +#include "impeller/geometry/rect.h" +#include "impeller/geometry/scalar.h" namespace impeller { struct SmoothingApproximation { - const double scale; - const double angleTolerance; - const double cuspLimit; - const double distanceToleranceSquare; + const Scalar scale; + const Scalar angleTolerance; + const Scalar cuspLimit; + const Scalar distanceToleranceSquare; SmoothingApproximation(/* default */) : SmoothingApproximation(1.0 /* scale */, 0.0 /* angle tolerance */, 0.0 /* cusp limit */) {} - SmoothingApproximation(double pScale, - double pAngleTolerance, - double pCuspLimit) + SmoothingApproximation(Scalar pScale, + Scalar pAngleTolerance, + Scalar pCuspLimit) : scale(pScale), angleTolerance(pAngleTolerance), cuspLimit(pCuspLimit), @@ -38,7 +39,7 @@ struct LinearPathComponent { LinearPathComponent(Point ap1, Point ap2) : p1(ap1), p2(ap2) {} - Point Solve(double time) const; + Point Solve(Scalar time) const; std::vector SmoothPoints() const; @@ -59,9 +60,9 @@ struct QuadraticPathComponent { QuadraticPathComponent(Point ap1, Point acp, Point ap2) : p1(ap1), cp(acp), p2(ap2) {} - Point Solve(double time) const; + Point Solve(Scalar time) const; - Point SolveDerivative(double time) const; + Point SolveDerivative(Scalar time) const; std::vector SmoothPoints( const SmoothingApproximation& approximation) const; @@ -90,9 +91,9 @@ struct CubicPathComponent { CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2) : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {} - Point Solve(double time) const; + Point Solve(Scalar time) const; - Point SolveDerivative(double time) const; + Point SolveDerivative(Scalar time) const; std::vector SmoothPoints( const SmoothingApproximation& approximation) const; diff --git a/engine/src/flutter/impeller/geometry/point.cc b/engine/src/flutter/impeller/geometry/point.cc index a0c942afd0..ca72d76550 100644 --- a/engine/src/flutter/impeller/geometry/point.cc +++ b/engine/src/flutter/impeller/geometry/point.cc @@ -20,14 +20,4 @@ void Point::FromString(const std::string& str) { stream >> y; } -double Point::GetDistanceSquared(const Point& p) const { - double dx = p.x - x; - double dy = p.y - y; - return dx * dx + dy * dy; -} - -double Point::GetDistance(const Point& p) const { - return sqrt(GetDistanceSquared(p)); -} - } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/point.h b/engine/src/flutter/impeller/geometry/point.h index 33f9e6d948..ba46e8283c 100644 --- a/engine/src/flutter/impeller/geometry/point.h +++ b/engine/src/flutter/impeller/geometry/point.h @@ -6,54 +6,75 @@ #include #include -#include "size.h" + +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/size.h" namespace impeller { struct Point { - double x = 0.0; - double y = 0.0; + Scalar x = 0.0; + Scalar y = 0.0; constexpr Point() = default; - constexpr Point(double x, double y) : x(x), y(y) {} + constexpr Point(Scalar x, Scalar y) : x(x), y(y) {} - /* - * Operator overloads - */ - bool operator==(const Point& p) const { return p.x == x && p.y == y; } + constexpr bool operator==(const Point& p) const { + return p.x == x && p.y == y; + } - bool operator!=(const Point& p) const { return p.x != x || p.y != y; } + constexpr bool operator!=(const Point& p) const { + return p.x != x || p.y != y; + } - Point operator-() const { return {-x, -y}; } + constexpr Point operator-() const { return {-x, -y}; } - Point operator+(const Point& p) const { return {x + p.x, y + p.y}; } + constexpr Point operator+(const Point& p) const { return {x + p.x, y + p.y}; } - Point operator+(const Size& s) const { return {x + s.width, y + s.height}; } + constexpr Point operator+(const Size& s) const { + return {x + s.width, y + s.height}; + } - Point operator-(const Point& p) const { return {x - p.x, y - p.y}; } + constexpr Point operator-(const Point& p) const { return {x - p.x, y - p.y}; } - Point operator-(const Size& s) const { return {x - s.width, y - s.height}; } + constexpr Point operator-(const Size& s) const { + return {x - s.width, y - s.height}; + } - Point operator*(double scale) const { return {x * scale, y * scale}; } + constexpr Point operator*(Scalar scale) const { + return {x * scale, y * scale}; + } - Point operator*(const Point& p) const { return {x * p.x, y * p.y}; } + constexpr Point operator*(const Point& p) const { return {x * p.x, y * p.y}; } - Point operator*(const Size& s) const { return {x * s.width, y * s.height}; } + constexpr Point operator*(const Size& s) const { + return {x * s.width, y * s.height}; + } - Point operator/(double d) const { return {x / d, y / d}; } + constexpr Point operator/(Scalar d) const { return {x / d, y / d}; } - Point operator/(const Point& p) const { return {x / p.x, y / p.y}; } + constexpr Point operator/(const Point& p) const { return {x / p.x, y / p.y}; } - Point operator/(const Size& s) const { return {x / s.width, y / s.height}; } + constexpr Point operator/(const Size& s) const { + return {x / s.width, y / s.height}; + } - double GetDistanceSquared(const Point& p) const; + constexpr Scalar GetDistanceSquared(const Point& p) const { + double dx = p.x - x; + double dy = p.y - y; + return dx * dx + dy * dy; + } - double GetDistance(const Point& p) const; + constexpr Scalar GetDistance(const Point& p) const { + return sqrt(GetDistanceSquared(p)); + } std::string ToString() const; void FromString(const std::string& str); }; +static_assert(sizeof(Point) == 2 * sizeof(Scalar)); + } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/rect.h b/engine/src/flutter/impeller/geometry/rect.h index 7439e1896e..21b5a905f1 100644 --- a/engine/src/flutter/impeller/geometry/rect.h +++ b/engine/src/flutter/impeller/geometry/rect.h @@ -5,8 +5,10 @@ #pragma once #include -#include "point.h" -#include "size.h" + +#include "impeller/geometry/point.h" +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/size.h" namespace impeller { @@ -20,11 +22,11 @@ struct Rect { constexpr Rect(Point origin, Size size) : origin(origin), size(size) {} - constexpr Rect(const double components[4]) + constexpr Rect(const Scalar components[4]) : origin(components[0], components[1]), size(components[2], components[3]) {} - constexpr Rect(double x, double y, double width, double height) + constexpr Rect(Scalar x, Scalar y, Scalar width, Scalar height) : origin(x, y), size(width, height) {} /* @@ -40,7 +42,7 @@ struct Rect { {size.width - r.size.width, size.height - r.size.height}); } - constexpr Rect operator*(double scale) const { + constexpr Rect operator*(Scalar scale) const { return Rect({origin.x * scale, origin.y * scale}, {size.width * scale, size.height * scale}); } @@ -70,4 +72,6 @@ struct Rect { void FromString(const std::string& str); }; +static_assert(sizeof(Rect) == 4 * sizeof(Scalar)); + } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/scalar.h b/engine/src/flutter/impeller/geometry/scalar.h new file mode 100644 index 0000000000..bf494586e7 --- /dev/null +++ b/engine/src/flutter/impeller/geometry/scalar.h @@ -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. + +#pragma once + +namespace impeller { + +using Scalar = float; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/size.h b/engine/src/flutter/impeller/geometry/size.h index 6df32feeb8..04bce608a5 100644 --- a/engine/src/flutter/impeller/geometry/size.h +++ b/engine/src/flutter/impeller/geometry/size.h @@ -7,25 +7,24 @@ #include #include +#include "impeller/geometry/scalar.h" + namespace impeller { struct Size { - double width = 0.0; - double height = 0.0; + Scalar width = 0.0; + Scalar height = 0.0; constexpr Size() {} - constexpr Size(double width, double height) : width(width), height(height) {} + constexpr Size(Scalar width, Scalar height) : width(width), height(height) {} static constexpr Size Infinite() { - return Size{std::numeric_limits::max(), - std::numeric_limits::max()}; + return Size{std::numeric_limits::max(), + std::numeric_limits::max()}; } - /* - * Operator overloads - */ - constexpr Size operator*(double scale) const { + constexpr Size operator*(Scalar scale) const { return {width * scale, height * scale}; } @@ -70,4 +69,6 @@ struct Size { void FromString(const std::string& str); }; +static_assert(sizeof(Size) == 2 * sizeof(Scalar)); + } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/vector.h b/engine/src/flutter/impeller/geometry/vector.h index 9360d7fe5d..b7625ffd96 100644 --- a/engine/src/flutter/impeller/geometry/vector.h +++ b/engine/src/flutter/impeller/geometry/vector.h @@ -6,48 +6,50 @@ #include #include -#include "point.h" -#include "size.h" + +#include "impeller/geometry/point.h" +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/size.h" namespace impeller { struct Vector3 { union { struct { - double x = 0.0; - double y = 0.0; - double z = 0.0; + Scalar x = 0.0; + Scalar y = 0.0; + Scalar z = 0.0; }; - double e[3]; + Scalar e[3]; }; - Vector3() {} + constexpr Vector3(){}; - Vector3(const Point& p) : x(p.x), y(p.y) {} + constexpr Vector3(const Point& p) : x(p.x), y(p.y) {} - Vector3(const Size& s) : x(s.width), y(s.height) {} + constexpr Vector3(const Size& s) : x(s.width), y(s.height) {} - Vector3(double x, double y) : x(x), y(y) {} + constexpr Vector3(Scalar x, Scalar y) : x(x), y(y) {} - Vector3(double x, double y, double z) : x(x), y(y), z(z) {} + constexpr Vector3(Scalar x, Scalar y, Scalar z) : x(x), y(y), z(z) {} /** * The length (or magnitude of the vector). * * @return the calculated length. */ - double Length() const { return sqrt(x * x + y * y + z * z); } + constexpr Scalar Length() const { return sqrt(x * x + y * y + z * z); } - Vector3 Normalize() const { + constexpr Vector3 Normalize() const { const auto len = Length(); return {x / len, y / len, z / len}; } - double Dot(const Vector3& other) const { + constexpr Scalar Dot(const Vector3& other) const { return ((x * other.x) + (y * other.y) + (z * other.z)); } - Vector3 Cross(const Vector3& other) const { + constexpr Vector3 Cross(const Vector3& other) const { return { (y * other.z) - (z * other.y), // (z * other.x) - (x * other.z), // @@ -55,21 +57,21 @@ struct Vector3 { }; } - bool operator==(const Vector3& v) const { + constexpr bool operator==(const Vector3& v) const { return v.x == x && v.y == y && v.z == z; } - bool operator!=(const Vector3& v) const { + constexpr bool operator!=(const Vector3& v) const { return v.x != x || v.y != y || v.z != z; } - Vector3 operator-() const { return Vector3(-x, -y, -z); } + constexpr Vector3 operator-() const { return Vector3(-x, -y, -z); } - Vector3 operator+(const Vector3& v) const { + constexpr Vector3 operator+(const Vector3& v) const { return Vector3(x + v.x, y + v.y, z + v.z); } - Vector3 operator-(const Vector3& v) const { + constexpr Vector3 operator-(const Vector3& v) const { return Vector3(x - v.x, y - v.y, z - v.z); } @@ -83,10 +85,10 @@ struct Vector3 { * * @return the combined vector. */ - static inline Vector3 Combine(const Vector3& a, - double aScale, - const Vector3& b, - double bScale) { + static constexpr Vector3 Combine(const Vector3& a, + Scalar aScale, + const Vector3& b, + Scalar bScale) { return { aScale * a.x + bScale * b.x, // aScale * a.y + bScale * b.y, // @@ -100,44 +102,48 @@ struct Vector3 { struct Vector4 { union { struct { - double x = 0.0; - double y = 0.0; - double z = 0.0; - double w = 1.0; + Scalar x = 0.0; + Scalar y = 0.0; + Scalar z = 0.0; + Scalar w = 1.0; }; - double e[4]; + Scalar e[4]; }; - Vector4() {} + constexpr Vector4() {} - Vector4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {} + constexpr Vector4(Scalar x, Scalar y, Scalar z, Scalar w) + : x(x), y(y), z(z), w(w) {} - Vector4(const Vector3& v) : x(v.x), y(v.y), z(v.z) {} + constexpr Vector4(const Vector3& v) : x(v.x), y(v.y), z(v.z) {} - Vector4(const Point& p) : x(p.x), y(p.y) {} + constexpr Vector4(const Point& p) : x(p.x), y(p.y) {} - Vector4 Normalize() const { - const double inverse = 1.0 / sqrt(x * x + y * y + z * z + w * w); + constexpr Vector4 Normalize() const { + const Scalar inverse = 1.0 / sqrt(x * x + y * y + z * z + w * w); return Vector4(x * inverse, y * inverse, z * inverse, w * inverse); } - bool operator==(const Vector4& v) const { + constexpr bool operator==(const Vector4& v) const { return (x == v.x) && (y == v.y) && (z == v.z) && (w == v.w); } - bool operator!=(const Vector4& v) const { + constexpr bool operator!=(const Vector4& v) const { return (x != v.x) || (y != v.y) || (z != v.z) || (w != v.w); } - Vector4 operator+(const Vector4& v) const { + constexpr Vector4 operator+(const Vector4& v) const { return Vector4(x + v.x, y + v.y, z + v.z, w + v.w); } - Vector4 operator-(const Vector4& v) const { + constexpr Vector4 operator-(const Vector4& v) const { return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); } std::string ToString() const; }; +static_assert(sizeof(Vector3) == 3 * sizeof(Scalar)); +static_assert(sizeof(Vector4) == 4 * sizeof(Scalar)); + } // namespace impeller diff --git a/engine/src/flutter/impeller/image/image.cc b/engine/src/flutter/impeller/image/image.cc index b8776eb8c4..aa0f1f9de1 100644 --- a/engine/src/flutter/impeller/image/image.cc +++ b/engine/src/flutter/impeller/image/image.cc @@ -71,7 +71,7 @@ ImageResult Image::Decode() const { } return ImageResult{ - Size{static_cast(width), static_cast(height)}, // size + Size{static_cast(width), static_cast(height)}, // size components, // components std::move(destinationAllocation) // allocation };