From f98ed9843792f6b55e2c553a4d3c6a85e224e679 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 21 Jul 2021 14:20:19 -0700 Subject: [PATCH] Add some aiks test stubs. --- engine/src/flutter/impeller/BUILD.gn | 2 + engine/src/flutter/impeller/aiks/BUILD.gn | 12 +- .../flutter/impeller/aiks/aiks_unittests.cc | 38 ++++++ engine/src/flutter/impeller/aiks/canvas.cc | 12 +- engine/src/flutter/impeller/aiks/canvas.h | 7 +- .../flutter/impeller/compiler/reflector.cc | 109 ++++++++++-------- engine/src/flutter/impeller/entity/entity.h | 23 ---- engine/src/flutter/impeller/geometry/matrix.h | 5 +- 8 files changed, 124 insertions(+), 84 deletions(-) create mode 100644 engine/src/flutter/impeller/aiks/aiks_unittests.cc diff --git a/engine/src/flutter/impeller/BUILD.gn b/engine/src/flutter/impeller/BUILD.gn index 8ba0af5630..167ba3b42b 100644 --- a/engine/src/flutter/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/BUILD.gn @@ -8,6 +8,7 @@ config("impeller_public_config") { group("impeller") { deps = [ + "aiks", "base", "compiler", "compositor", @@ -22,6 +23,7 @@ executable("impeller_unittests") { testonly = true deps = [ + "aiks:aiks_unittests", "base:base_unittests", "compiler:compiler_unittests", "compositor:compositor_unittests", diff --git a/engine/src/flutter/impeller/aiks/BUILD.gn b/engine/src/flutter/impeller/aiks/BUILD.gn index c6fe348cd6..c4b8829a14 100644 --- a/engine/src/flutter/impeller/aiks/BUILD.gn +++ b/engine/src/flutter/impeller/aiks/BUILD.gn @@ -8,23 +8,25 @@ impeller_component("aiks") { sources = [ "canvas.cc", "canvas.h", + "image.cc", + "image.h", "paint.cc", "paint.h", - "image.cc" - "image.h" ] public_deps = [ - ":base", - ":entity", + "../base", + "../entity", + "../geometry", ] } impeller_component("aiks_unittests") { testonly = true - sources = [] + sources = [ "aiks_unittests.cc" ] deps = [ ":aiks", + "../geometry:geometry_unittests", "//flutter/testing", ] } diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc new file mode 100644 index 0000000000..5825cbc9eb --- /dev/null +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -0,0 +1,38 @@ +// 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 "flutter/testing/testing.h" +#include "impeller/aiks/canvas.h" +#include "impeller/geometry/geometry_unittests.h" + +namespace impeller { +namespace testing { + +TEST(AiksTest, CanvasCTMCanBeUpdated) { + Canvas canvas; + Matrix identity; + ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), identity); + canvas.Translate(Size{100, 100}); + ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), + Matrix::MakeTranslation({100.0, 100.0, 0.0})); +} + +TEST(AiksTest, CanvasCanPushPopCTM) { + Canvas canvas; + ASSERT_EQ(canvas.GetSaveCount(), 1u); + ASSERT_EQ(canvas.Restore(), false); + + canvas.Translate(Size{100, 100}); + canvas.Save(); + ASSERT_EQ(canvas.GetSaveCount(), 2u); + ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), + Matrix::MakeTranslation({100.0, 100.0, 0.0})); + ASSERT_TRUE(canvas.Restore()); + ASSERT_EQ(canvas.GetSaveCount(), 1u); + ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), + Matrix::MakeTranslation({100.0, 100.0, 0.0})); +} + +} // namespace testing +} // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index eb34836057..4de595e6de 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -32,11 +32,15 @@ void Canvas::Concat(const Matrix& xformation) { xformation_stack_.top() = xformation_stack_.top() * xformation; } -void Canvas::Translate(const Size& offset) { +const Matrix& Canvas::GetCurrentTransformation() const { + return xformation_stack_.top(); +} + +void Canvas::Translate(const Vector3& offset) { Concat(Matrix::MakeTranslation(offset)); } -void Canvas::Scale(const Size& scale) { +void Canvas::Scale(const Vector3& scale) { Concat(Matrix::MakeScale(scale)); } @@ -44,6 +48,10 @@ void Canvas::Rotate(Radians radians) { Concat(Matrix::MakeRotationZ(radians)); } +size_t Canvas::GetSaveCount() const { + return xformation_stack_.size(); +} + 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 index c4e928d574..86f2ed048b 100644 --- a/engine/src/flutter/impeller/aiks/canvas.h +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -10,6 +10,7 @@ #include "impeller/aiks/paint.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/path.h" +#include "impeller/geometry/vector.h" namespace impeller { @@ -25,11 +26,13 @@ class Canvas { size_t GetSaveCount() const; + const Matrix& GetCurrentTransformation() const; + void Concat(const Matrix& xformation); - void Translate(const Size& offset); + void Translate(const Vector3& offset); - void Scale(const Size& scale); + void Scale(const Vector3& scale); void Rotate(Radians radians); diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 70c443aba9..e97d7f7c39 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -20,10 +20,8 @@ namespace impeller { namespace compiler { -using namespace spirv_cross; - -static std::string BaseTypeToString(SPIRType::BaseType type) { - using Type = SPIRType::BaseType; +static std::string BaseTypeToString(spirv_cross::SPIRType::BaseType type) { + using Type = spirv_cross::SPIRType::BaseType; switch (type) { case Type::Void: return "ShaderType::kVoid"; @@ -90,8 +88,8 @@ static std::string StringToShaderStage(std::string str) { } Reflector::Reflector(Options options, - std::shared_ptr ir, - std::shared_ptr compiler) + std::shared_ptr ir, + std::shared_ptr compiler) : options_(std::move(options)), ir_(std::move(ir)), compiler_(std::move(compiler)) { @@ -226,17 +224,20 @@ std::optional Reflector::GenerateTemplateArguments() const { } std::set known_structs; - ir_->for_each_typed_id([&](uint32_t, const SPIRType& type) { - if (known_structs.find(type.self) != known_structs.end()) { - // Iterating over types this way leads to duplicates which may cause - // duplicate struct definitions. - return; - } - known_structs.insert(type.self); - if (auto struc = ReflectStructDefinition(type.self); struc.has_value()) { - struct_definitions.emplace_back(EmitStructDefinition(struc.value())); - } - }); + ir_->for_each_typed_id( + [&](uint32_t, const spirv_cross::SPIRType& type) { + if (known_structs.find(type.self) != known_structs.end()) { + // Iterating over types this way leads to duplicates which may cause + // duplicate struct definitions. + return; + } + known_structs.insert(type.self); + if (auto struc = ReflectStructDefinition(type.self); + struc.has_value()) { + struct_definitions.emplace_back( + EmitStructDefinition(struc.value())); + } + }); } root["bind_prototypes"] = EmitBindPrototypes(shader_resources); @@ -342,24 +343,25 @@ struct KnownType { size_t byte_size = 0; }; -static std::optional ReadKnownScalarType(SPIRType::BaseType type) { +static std::optional ReadKnownScalarType( + spirv_cross::SPIRType::BaseType type) { switch (type) { - case SPIRType::BaseType::Boolean: + case spirv_cross::SPIRType::BaseType::Boolean: return KnownType{ .name = "bool", .byte_size = sizeof(bool), }; - case SPIRType::BaseType::Float: + case spirv_cross::SPIRType::BaseType::Float: return KnownType{ .name = "Scalar", .byte_size = sizeof(Scalar), }; - case SPIRType::BaseType::UInt: + case spirv_cross::SPIRType::BaseType::UInt: return KnownType{ .name = "uint32_t", .byte_size = sizeof(uint32_t), }; - case SPIRType::BaseType::Int: + case spirv_cross::SPIRType::BaseType::Int: return KnownType{ .name = "int32_t", .byte_size = sizeof(int32_t), @@ -373,7 +375,7 @@ static std::optional ReadKnownScalarType(SPIRType::BaseType type) { std::vector Reflector::ReadStructMembers( const spirv_cross::TypeID& type_id) const { const auto& struct_type = compiler_->get_type(type_id); - FML_CHECK(struct_type.basetype == SPIRType::BaseType::Struct); + FML_CHECK(struct_type.basetype == spirv_cross::SPIRType::BaseType::Struct); std::vector result; @@ -400,10 +402,10 @@ std::vector Reflector::ReadStructMembers( // Tightly packed 4x4 Matrix is special cased as we know how to work with // those. - if (member.basetype == SPIRType::BaseType::Float && // - member.width == sizeof(Scalar) * 8 && // - member.columns == 4 && // - member.vecsize == 4 // + if (member.basetype == spirv_cross::SPIRType::BaseType::Float && // + member.width == sizeof(Scalar) * 8 && // + member.columns == 4 && // + member.vecsize == 4 // ) { result.emplace_back(StructMember{ .type = "Matrix", @@ -416,10 +418,10 @@ std::vector Reflector::ReadStructMembers( } // Tightly packed Point (vec2). - if (member.basetype == SPIRType::BaseType::Float && // - member.width == sizeof(float) * 8 && // - member.columns == 1 && // - member.vecsize == 2 // + if (member.basetype == spirv_cross::SPIRType::BaseType::Float && // + member.width == sizeof(float) * 8 && // + member.columns == 1 && // + member.vecsize == 2 // ) { result.emplace_back(StructMember{ .type = "Point", @@ -432,10 +434,10 @@ std::vector Reflector::ReadStructMembers( } // Tightly packed Vector3. - if (member.basetype == SPIRType::BaseType::Float && // - member.width == sizeof(float) * 8 && // - member.columns == 1 && // - member.vecsize == 3 // + if (member.basetype == spirv_cross::SPIRType::BaseType::Float && // + member.width == sizeof(float) * 8 && // + member.columns == 1 && // + member.vecsize == 3 // ) { result.emplace_back(StructMember{ .type = "Vector3", @@ -448,10 +450,10 @@ std::vector Reflector::ReadStructMembers( } // Tightly packed Vector4. - if (member.basetype == SPIRType::BaseType::Float && // - member.width == sizeof(float) * 8 && // - member.columns == 1 && // - member.vecsize == 4 // + if (member.basetype == spirv_cross::SPIRType::BaseType::Float && // + member.width == sizeof(float) * 8 && // + member.columns == 1 && // + member.vecsize == 4 // ) { result.emplace_back(StructMember{ .type = "Vector4", @@ -501,9 +503,9 @@ std::vector Reflector::ReadStructMembers( } std::optional Reflector::ReflectStructDefinition( - const TypeID& type_id) const { + const spirv_cross::TypeID& type_id) const { const auto& type = compiler_->get_type(type_id); - if (type.basetype != SPIRType::BaseType::Struct) { + if (type.basetype != spirv_cross::SPIRType::BaseType::Struct) { return std::nullopt; } @@ -541,22 +543,26 @@ struct VertexType { size_t byte_length = 0u; }; -static VertexType VertexTypeFromInputResource(const CompilerMSL& compiler, - const Resource* resource) { +static VertexType VertexTypeFromInputResource( + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::Resource* resource) { VertexType result; result.variable_name = resource->name; auto type = compiler.get_type(resource->type_id); const auto total_size = type.columns * type.vecsize * type.width / 8u; result.byte_length = total_size; - if (type.basetype == SPIRType::BaseType::Float && type.columns == 1u && - type.vecsize == 2u && type.width == sizeof(float) * 8u) { + if (type.basetype == spirv_cross::SPIRType::BaseType::Float && + type.columns == 1u && type.vecsize == 2u && + type.width == sizeof(float) * 8u) { result.type_name = "Point"; - } else if (type.basetype == SPIRType::BaseType::Float && type.columns == 1u && - type.vecsize == 4u && type.width == sizeof(float) * 8u) { + } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float && + type.columns == 1u && type.vecsize == 4u && + type.width == sizeof(float) * 8u) { result.type_name = "Vector4"; - } else if (type.basetype == SPIRType::BaseType::Float && type.columns == 1u && - type.vecsize == 3u && type.width == sizeof(float) * 8u) { + } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float && + type.columns == 1u && type.vecsize == 3u && + type.width == sizeof(float) * 8u) { result.type_name = "Vector3"; } else { // Catch all unknown padding. @@ -568,7 +574,7 @@ static VertexType VertexTypeFromInputResource(const CompilerMSL& compiler, std::optional Reflector::ReflectPerVertexStructDefinition( - const SmallVector& stage_inputs) const { + const spirv_cross::SmallVector& stage_inputs) const { // Avoid emitting a zero sized structure. The code gen templates assume a // non-zero size. if (stage_inputs.empty()) { @@ -594,7 +600,8 @@ Reflector::ReflectPerVertexStructDefinition( } } - auto input_for_location = [&](uint32_t queried_location) -> const Resource* { + auto input_for_location = + [&](uint32_t queried_location) -> const spirv_cross::Resource* { for (const auto& input : stage_inputs) { auto location = compiler_->get_decoration( input.id, spv::Decoration::DecorationLocation); diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 2c35974498..cf0bc3d920 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -18,32 +18,12 @@ class Entity { ~Entity(); - /** - * The transformation that is applied to the entity about its anchor point - * - * @return the transformation applied to the node - */ const Matrix& GetTransformation() const; - /** - * Sets the transformation of the entity - * - * @param transformation the new transformation - */ void SetTransformation(const Matrix& transformation); - /** - * The background color of the entity - * - * @return the background color - */ const Color& GetBackgroundColor() const; - /** - * Set the new background color of the entity - * - * @param backgroundColor the new background color - */ void SetBackgroundColor(const Color& backgroundColor); const Color& GetStrokeColor() const; @@ -59,9 +39,6 @@ class Entity { void SetPath(Path path); private: - Rect bounds_; - Point position_; - Point anchor_point_ = {0.5, 0.5}; Matrix transformation_; Color background_color_; diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index 4d603845cc..f88eafb6e0 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -53,10 +53,13 @@ struct Matrix { uint64_t GetComponentsMask() const; }; - // TODO(csg): Radar restructions of C++11 don't exist. Use optionals instead. + // TODO(csg): Radar restrictions of C++11 don't exist. Use optionals instead. using DecompositionResult = std::pair; + //---------------------------------------------------------------------------- + /// Construts a default identity matrix. + /// constexpr Matrix() // clang-format off : vec{ Vector4(1.0, 0.0, 0.0, 0.0),