Add some aiks test stubs.

This commit is contained in:
Chinmay Garde
2021-07-21 14:20:19 -07:00
committed by Dan Field
parent c71d4991c4
commit f98ed98437
8 changed files with 124 additions and 84 deletions

View File

@@ -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",

View File

@@ -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",
]
}

View File

@@ -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

View File

@@ -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

View File

@@ -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);

View File

@@ -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<const ParsedIR> ir,
std::shared_ptr<const CompilerMSL> compiler)
std::shared_ptr<const spirv_cross::ParsedIR> ir,
std::shared_ptr<const spirv_cross::CompilerMSL> compiler)
: options_(std::move(options)),
ir_(std::move(ir)),
compiler_(std::move(compiler)) {
@@ -226,17 +224,20 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
}
std::set<spirv_cross::ID> known_structs;
ir_->for_each_typed_id<SPIRType>([&](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<spirv_cross::SPIRType>(
[&](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<KnownType> ReadKnownScalarType(SPIRType::BaseType type) {
static std::optional<KnownType> 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<KnownType> ReadKnownScalarType(SPIRType::BaseType type) {
std::vector<Reflector::StructMember> 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<StructMember> result;
@@ -400,10 +402,10 @@ std::vector<Reflector::StructMember> 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::StructMember> 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::StructMember> 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::StructMember> 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::StructMember> Reflector::ReadStructMembers(
}
std::optional<Reflector::StructDefinition> 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::StructDefinition>
Reflector::ReflectPerVertexStructDefinition(
const SmallVector<spirv_cross::Resource>& stage_inputs) const {
const spirv_cross::SmallVector<spirv_cross::Resource>& 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);

View File

@@ -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_;

View File

@@ -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<bool /* success */, Decomposition /* result */>;
//----------------------------------------------------------------------------
/// Construts a default identity matrix.
///
constexpr Matrix()
// clang-format off
: vec{ Vector4(1.0, 0.0, 0.0, 0.0),