diff --git a/engine/src/flutter/impeller/aiks/BUILD.gn b/engine/src/flutter/impeller/aiks/BUILD.gn new file mode 100644 index 0000000000..c2276fe759 --- /dev/null +++ b/engine/src/flutter/impeller/aiks/BUILD.gn @@ -0,0 +1,28 @@ +# 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. + +import("../tools/impeller.gni") + +impeller_component("aiks") { + sources = [ + "canvas.cc", + "canvas.h", + "paint.cc", + "paint.h", + ] + + public_deps = [ + ":base", + ":entity", + ] +} + +impeller_component("aiks_unittests") { + testonly = true + sources = [] + deps = [ + ":aiks", + "//flutter/testing", + ] +} diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc new file mode 100644 index 0000000000..eb34836057 --- /dev/null +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -0,0 +1,49 @@ +// 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/aiks/canvas.h" + +#include "flutter/fml/logging.h" + +namespace impeller { + +Canvas::Canvas() { + xformation_stack_.push(Matrix{}); +} + +Canvas::~Canvas() = default; + +void Canvas::Save() { + FML_DCHECK(xformation_stack_.size() > 0); + xformation_stack_.push(xformation_stack_.top()); +} + +bool Canvas::Restore() { + FML_DCHECK(xformation_stack_.size() > 0); + if (xformation_stack_.size() == 1) { + return false; + } + xformation_stack_.pop(); + return true; +} + +void Canvas::Concat(const Matrix& xformation) { + xformation_stack_.top() = xformation_stack_.top() * xformation; +} + +void Canvas::Translate(const Size& offset) { + Concat(Matrix::MakeTranslation(offset)); +} + +void Canvas::Scale(const Size& scale) { + Concat(Matrix::MakeScale(scale)); +} + +void Canvas::Rotate(Radians radians) { + Concat(Matrix::MakeRotationZ(radians)); +} + +void Canvas::DrawPath(Path path, Paint paint) {} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/canvas.h b/engine/src/flutter/impeller/aiks/canvas.h new file mode 100644 index 0000000000..c4e928d574 --- /dev/null +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -0,0 +1,44 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "impeller/aiks/paint.h" +#include "impeller/geometry/matrix.h" +#include "impeller/geometry/path.h" + +namespace impeller { + +class Canvas { + public: + Canvas(); + + ~Canvas(); + + void Save(); + + bool Restore(); + + size_t GetSaveCount() const; + + void Concat(const Matrix& xformation); + + void Translate(const Size& offset); + + void Scale(const Size& scale); + + void Rotate(Radians radians); + + void DrawPath(Path path, Paint paint); + + private: + std::stack xformation_stack_; + + FML_DISALLOW_COPY_AND_ASSIGN(Canvas); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc new file mode 100644 index 0000000000..a9613dbd63 --- /dev/null +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -0,0 +1,17 @@ +// 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/aiks/paint.h" + +namespace impeller { + +Paint::Paint() = default; + +Paint::~Paint() = default; + +void Paint::SetColor(Color color) { + color_ = std::move(color); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h new file mode 100644 index 0000000000..ae72142cb9 --- /dev/null +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -0,0 +1,27 @@ +// 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/color.h" + +namespace impeller { + +class Paint { + public: + Paint(); + + ~Paint(); + + void SetColor(Color color); + + private: + Color color_; + + FML_DISALLOW_COPY_AND_ASSIGN(Paint); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.h b/engine/src/flutter/impeller/compositor/texture_descriptor.h index fc647cbbb9..c5ba247664 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.h +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.h @@ -8,7 +8,7 @@ #include "impeller/compositor/formats.h" #include "impeller/geometry/size.h" -#include "impeller/image/image.h" +#include "impeller/image/decompressed_image.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 1ab3047e15..9554cc21e6 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -8,7 +8,7 @@ #include "impeller/geometry/matrix.h" #include "impeller/geometry/path.h" #include "impeller/geometry/rect.h" -#include "impeller/image/image.h" +#include "impeller/image/decompressed_image.h" namespace impeller { diff --git a/engine/src/flutter/impeller/geometry/constants.cc b/engine/src/flutter/impeller/geometry/constants.cc new file mode 100644 index 0000000000..e69de29bb2 diff --git a/engine/src/flutter/impeller/geometry/constants.h b/engine/src/flutter/impeller/geometry/constants.h new file mode 100644 index 0000000000..8385b7eccb --- /dev/null +++ b/engine/src/flutter/impeller/geometry/constants.h @@ -0,0 +1,46 @@ +// 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. + +namespace impeller { + +// e +constexpr float kE = 2.7182818284590452354; + +// log_2 e +constexpr float kLog2_E = 1.4426950408889634074; + +// log_10 e +constexpr float kLog10_E = 0.43429448190325182765; + +// log_e 2 +constexpr float klogE_2 = 0.69314718055994530942; + +// log_e 10 +constexpr float klogE_10 = 2.30258509299404568402; + +// pi */ +constexpr float kPi = 3.14159265358979323846; + +// pi/2 +constexpr float kPiOver2 = 1.57079632679489661923; + +// pi/4 +constexpr float kPiOver4 = 0.78539816339744830962; + +// 1/pi +constexpr float k1OverPi = 0.31830988618379067154; + +// 2/pi +constexpr float k2OverPi = 0.63661977236758134308; + +// 2/sqrt(pi) +constexpr float k2OverSqrtPi = 1.12837916709551257390; + +// sqrt(2) +constexpr float kSqrt2 = 1.41421356237309504880; + +// 1/sqrt(2) +constexpr float k1OverSqrt2 = 0.70710678118654752440; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 31694ed006..13b39bd34a 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -14,7 +14,7 @@ namespace impeller { namespace testing { TEST(GeometryTest, RotationMatrix) { - auto rotation = Matrix::MakeRotationZ(M_PI_4); + auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4}); auto expect = Matrix{0.707, 0.707, 0, 0, // -0.707, 0.707, 0, 0, // 0, 0, 1, 0, // @@ -23,7 +23,7 @@ TEST(GeometryTest, RotationMatrix) { } TEST(GeometryTest, InvertMultMatrix) { - auto rotation = Matrix::MakeRotationZ(M_PI_4); + auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4}); auto invert = rotation.Invert(); auto expect = Matrix{0.707, -0.707, 0, 0, // 0.707, 0.707, 0, 0, // @@ -33,7 +33,7 @@ TEST(GeometryTest, InvertMultMatrix) { } TEST(GeometryTest, MutliplicationMatrix) { - auto rotation = Matrix::MakeRotationZ(M_PI_4); + auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4}); auto invert = rotation.Invert(); ASSERT_MATRIX_NEAR(rotation * invert, Matrix{}); } @@ -60,7 +60,7 @@ TEST(GeometryTest, InvertMatrix) { } TEST(GeometryTest, TestDecomposition) { - auto rotated = Matrix::MakeRotationZ(M_PI_4); + auto rotated = Matrix::MakeRotationZ(Radians{M_PI_4}); auto result = rotated.Decompose(); @@ -73,7 +73,7 @@ TEST(GeometryTest, TestDecomposition) { } TEST(GeometryTest, TestDecomposition2) { - auto rotated = Matrix::MakeRotationZ(M_PI_4); + auto rotated = Matrix::MakeRotationZ(Radians{M_PI_4}); auto scaled = Matrix::MakeScale({2.0, 3.0, 1.0}); auto translated = Matrix::MakeTranslation({-200, 750, 20}); @@ -100,7 +100,7 @@ TEST(GeometryTest, TestRecomposition) { /* * Decomposition. */ - auto rotated = Matrix::MakeRotationZ(M_PI_4); + auto rotated = Matrix::MakeRotationZ(Radians{M_PI_4}); auto result = rotated.Decompose(); @@ -120,7 +120,7 @@ TEST(GeometryTest, TestRecomposition) { TEST(GeometryTest, TestRecomposition2) { auto matrix = Matrix::MakeTranslation({100, 100, 100}) * - Matrix::MakeRotationZ(M_PI_4) * + Matrix::MakeRotationZ(Radians{M_PI_4}) * Matrix::MakeScale({2.0, 2.0, 2.0}); auto result = matrix.Decompose(); @@ -265,5 +265,13 @@ TEST(GeometryTest, CanConvertTTypesExplicitly) { } } +TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { + { + auto deg = Degrees{90.0}; + Radians rad = deg; + ASSERT_FLOAT_EQ(rad.radians, kPiOver2); + } +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index 7be4168497..58bc83669d 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -53,6 +53,7 @@ struct Matrix { uint64_t GetComponentsMask() const; }; + // TODO(csg): Radar restructions of C++11 don't exist. Use optionals instead. using DecompositionResult = std::pair; @@ -126,9 +127,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationX(Scalar radians) { - Scalar cosine = cos(radians); - Scalar sine = sin(radians); + static Matrix MakeRotationX(Radians r) { + Scalar cosine = cos(r.radians); + Scalar sine = sin(r.radians); // clang-format off return Matrix( 1.0, 0.0, 0.0, 0.0, @@ -139,9 +140,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationY(Scalar radians) { - Scalar cosine = cos(radians); - Scalar sine = sin(radians); + static Matrix MakeRotationY(Radians r) { + Scalar cosine = cos(r.radians); + Scalar sine = sin(r.radians); // clang-format off return Matrix( @@ -153,9 +154,9 @@ struct Matrix { // clang-format on } - static Matrix MakeRotationZ(Scalar radians) { - Scalar cosine = cos(radians); - Scalar sine = sin(radians); + static Matrix MakeRotationZ(Radians r) { + Scalar cosine = cos(r.radians); + Scalar sine = sin(r.radians); // clang-format off return Matrix ( @@ -172,8 +173,8 @@ struct Matrix { return Matrix(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], - m[0] * t.x + m[4] * t.y + m[8] * t.z + m[12], - m[1] * t.x + m[5] * t.y + m[9] * t.z + m[13], + m[0] * t.x + m[4] * t.y + m[8] * t.z + m[12], + m[1] * t.x + m[5] * t.y + m[9] * t.z + m[13], m[2] * t.x + m[6] * t.y + m[10] * t.z + m[14], m[15]); // clang-format on @@ -181,10 +182,10 @@ struct Matrix { 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, + 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, m[8] * s.z, m[9] * s.z, m[10] * s.z, m[11] * s.z, - m[12] , m[13] , m[14] , m[15]); + m[12] , m[13] , m[14] , m[15] ); // clang-format on } diff --git a/engine/src/flutter/impeller/geometry/scalar.h b/engine/src/flutter/impeller/geometry/scalar.h index b7fe63c63f..9e8cee0d58 100644 --- a/engine/src/flutter/impeller/geometry/scalar.h +++ b/engine/src/flutter/impeller/geometry/scalar.h @@ -6,8 +6,32 @@ #include +#include "impeller/geometry/constants.h" + namespace impeller { using Scalar = float; +struct Degrees; + +struct Radians { + Scalar radians = 0.0; + + constexpr Radians() = default; + + explicit constexpr Radians(Scalar p_radians) : radians(p_radians) {} +}; + +struct Degrees { + Scalar degrees = 0.0; + + constexpr Degrees() = default; + + explicit constexpr Degrees(Scalar p_degrees) : degrees(p_degrees) {} + + constexpr operator Radians() const { + return Radians{degrees * kPi / 180.0f}; + }; +}; + } // namespace impeller diff --git a/engine/src/flutter/impeller/image/BUILD.gn b/engine/src/flutter/impeller/image/BUILD.gn index 35d7c0643c..5475257810 100644 --- a/engine/src/flutter/impeller/image/BUILD.gn +++ b/engine/src/flutter/impeller/image/BUILD.gn @@ -8,8 +8,8 @@ impeller_component("image") { sources = [ "compressed_image.cc", "compressed_image.h", - "image.cc", - "image.h", + "decompressed_image.cc", + "decompressed_image.h", ] deps = [ "../third_party/stb" ] diff --git a/engine/src/flutter/impeller/image/compressed_image.cc b/engine/src/flutter/impeller/image/compressed_image.cc index 6076be6988..2c78e7aba6 100644 --- a/engine/src/flutter/impeller/image/compressed_image.cc +++ b/engine/src/flutter/impeller/image/compressed_image.cc @@ -14,7 +14,7 @@ CompressedImage::CompressedImage( CompressedImage::~CompressedImage() = default; -Image CompressedImage::Decode() const { +DecompressedImage CompressedImage::Decode() const { if (!source_) { return {}; } @@ -47,32 +47,32 @@ Image CompressedImage::Decode() const { /* * Make sure we got a valid component set. */ - auto components = Image::Format::Invalid; + auto components = DecompressedImage::Format::Invalid; switch (comps) { case STBI_grey: - components = Image::Format::Grey; + components = DecompressedImage::Format::Grey; break; case STBI_grey_alpha: - components = Image::Format::GreyAlpha; + components = DecompressedImage::Format::GreyAlpha; break; case STBI_rgb: - components = Image::Format::RGB; + components = DecompressedImage::Format::RGB; break; case STBI_rgb_alpha: - components = Image::Format::RGBA; + components = DecompressedImage::Format::RGBA; break; default: - components = Image::Format::Invalid; + components = DecompressedImage::Format::Invalid; break; } - if (components == Image::Format::Invalid) { + if (components == DecompressedImage::Format::Invalid) { FML_LOG(ERROR) << "Could not detect image components when decoding."; return {}; } - return Image{ + return DecompressedImage{ ISize{width, height}, // size components, // components std::move(dest_allocation) // allocation diff --git a/engine/src/flutter/impeller/image/compressed_image.h b/engine/src/flutter/impeller/image/compressed_image.h index a30c32276b..7b79096f3d 100644 --- a/engine/src/flutter/impeller/image/compressed_image.h +++ b/engine/src/flutter/impeller/image/compressed_image.h @@ -7,7 +7,7 @@ #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "impeller/geometry/size.h" -#include "impeller/image/image.h" +#include "impeller/image/decompressed_image.h" namespace impeller { @@ -19,7 +19,7 @@ class CompressedImage { ~CompressedImage(); - [[nodiscard]] Image Decode() const; + [[nodiscard]] DecompressedImage Decode() const; bool IsValid() const; diff --git a/engine/src/flutter/impeller/image/image.cc b/engine/src/flutter/impeller/image/decompressed_image.cc similarity index 65% rename from engine/src/flutter/impeller/image/image.cc rename to engine/src/flutter/impeller/image/decompressed_image.cc index 443f1edbf0..6cf9c54607 100644 --- a/engine/src/flutter/impeller/image/image.cc +++ b/engine/src/flutter/impeller/image/decompressed_image.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "impeller/image/image.h" +#include "impeller/image/decompressed_image.h" #include @@ -11,11 +11,12 @@ namespace impeller { -Image::Image() = default; +DecompressedImage::DecompressedImage() = default; -Image::Image(ISize size, - Format format, - std::shared_ptr allocation) +DecompressedImage::DecompressedImage( + ISize size, + Format format, + std::shared_ptr allocation) : size_(size), format_(format), allocation_(std::move(allocation)) { if (!allocation_ || !size.IsPositive() || format_ == Format::Invalid) { return; @@ -23,47 +24,48 @@ Image::Image(ISize size, is_valid_ = true; } -Image::~Image() = default; +DecompressedImage::~DecompressedImage() = default; -bool Image::IsValid() const { +bool DecompressedImage::IsValid() const { return is_valid_; } -const ISize& Image::GetSize() const { +const ISize& DecompressedImage::GetSize() const { return size_; } -Image::Format Image::GetFormat() const { +DecompressedImage::Format DecompressedImage::GetFormat() const { return format_; } -const std::shared_ptr& Image::GetAllocation() const { +const std::shared_ptr& DecompressedImage::GetAllocation() + const { return allocation_; } -static size_t GetBytesPerPixel(Image::Format format) { +static size_t GetBytesPerPixel(DecompressedImage::Format format) { switch (format) { - case Image::Format::Invalid: + case DecompressedImage::Format::Invalid: return 0u; - case Image::Format::Grey: + case DecompressedImage::Format::Grey: return 1u; - case Image::Format::GreyAlpha: + case DecompressedImage::Format::GreyAlpha: return 1u; - case Image::Format::RGB: + case DecompressedImage::Format::RGB: return 3u; - case Image::Format::RGBA: + case DecompressedImage::Format::RGBA: return 4; } return 0u; } -Image Image::ConvertToRGBA() const { +DecompressedImage DecompressedImage::ConvertToRGBA() const { if (!is_valid_) { return {}; } if (format_ == Format::RGBA) { - return Image{size_, format_, allocation_}; + return DecompressedImage{size_, format_, allocation_}; } const auto bpp = GetBytesPerPixel(format_); @@ -82,26 +84,26 @@ Image Image::ConvertToRGBA() const { for (size_t i = 0, j = 0; i < source_byte_size; i += bpp, j += 4u) { switch (format_) { - case Image::Format::Grey: + case DecompressedImage::Format::Grey: dest[j + 0] = source[i]; dest[j + 1] = source[i]; dest[j + 2] = source[i]; dest[j + 3] = std::numeric_limits::max(); break; - case Image::Format::GreyAlpha: + case DecompressedImage::Format::GreyAlpha: dest[j + 0] = std::numeric_limits::max(); dest[j + 1] = std::numeric_limits::max(); dest[j + 2] = std::numeric_limits::max(); dest[j + 3] = source[i]; break; - case Image::Format::RGB: + case DecompressedImage::Format::RGB: dest[j + 0] = source[i + 0]; dest[j + 1] = source[i + 1]; dest[j + 2] = source[i + 2]; dest[j + 3] = std::numeric_limits::max(); break; - case Image::Format::Invalid: - case Image::Format::RGBA: + case DecompressedImage::Format::Invalid: + case DecompressedImage::Format::RGBA: // Should never happen. The necessary checks have already been // performed. FML_CHECK(false); @@ -109,7 +111,7 @@ Image Image::ConvertToRGBA() const { } } - return Image{ + return DecompressedImage{ size_, Format::RGBA, std::make_shared( rgba_allocation->GetBuffer(), // diff --git a/engine/src/flutter/impeller/image/image.h b/engine/src/flutter/impeller/image/decompressed_image.h similarity index 75% rename from engine/src/flutter/impeller/image/image.h rename to engine/src/flutter/impeller/image/decompressed_image.h index f888ab2445..c71af79d26 100644 --- a/engine/src/flutter/impeller/image/image.h +++ b/engine/src/flutter/impeller/image/decompressed_image.h @@ -13,7 +13,7 @@ namespace impeller { -class Image { +class DecompressedImage { public: enum class Format { Invalid, @@ -23,13 +23,13 @@ class Image { RGBA, }; - Image(); + DecompressedImage(); - Image(ISize size, - Format format, - std::shared_ptr allocation); + DecompressedImage(ISize size, + Format format, + std::shared_ptr allocation); - ~Image(); + ~DecompressedImage(); const ISize& GetSize() const; @@ -39,7 +39,7 @@ class Image { const std::shared_ptr& GetAllocation() const; - Image ConvertToRGBA() const; + DecompressedImage ConvertToRGBA() const; private: ISize size_; diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index 3e7400311a..a8e55faf0c 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -12,7 +12,7 @@ #include "impeller/compositor/surface.h" #include "impeller/compositor/vertex_buffer_builder.h" #include "impeller/image/compressed_image.h" -#include "impeller/image/image.h" +#include "impeller/image/decompressed_image.h" #include "impeller/playground/playground.h" #include "impeller/primitives/box_fade.frag.h" #include "impeller/primitives/box_fade.vert.h"