Start stubbing out the aiks interfaces.

This commit is contained in:
Chinmay Garde
2021-07-20 13:00:51 -07:00
committed by Dan Field
parent ee0a9c8e34
commit 9c0f8af66a
18 changed files with 314 additions and 68 deletions

View File

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

View File

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

View File

@@ -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 <stack>
#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<Matrix> xformation_stack_;
FML_DISALLOW_COPY_AND_ASSIGN(Canvas);
};
} // namespace impeller

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<bool /* success */, Decomposition /* result */>;
@@ -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
}

View File

@@ -6,8 +6,32 @@
#include <cfloat>
#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

View File

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

View File

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

View File

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

View File

@@ -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 <limits>
@@ -11,11 +11,12 @@
namespace impeller {
Image::Image() = default;
DecompressedImage::DecompressedImage() = default;
Image::Image(ISize size,
Format format,
std::shared_ptr<const fml::Mapping> allocation)
DecompressedImage::DecompressedImage(
ISize size,
Format format,
std::shared_ptr<const fml::Mapping> 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<const fml::Mapping>& Image::GetAllocation() const {
const std::shared_ptr<const fml::Mapping>& 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<uint8_t>::max();
break;
case Image::Format::GreyAlpha:
case DecompressedImage::Format::GreyAlpha:
dest[j + 0] = std::numeric_limits<uint8_t>::max();
dest[j + 1] = std::numeric_limits<uint8_t>::max();
dest[j + 2] = std::numeric_limits<uint8_t>::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<uint8_t>::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<fml::NonOwnedMapping>(
rgba_allocation->GetBuffer(), //

View File

@@ -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<const fml::Mapping> allocation);
DecompressedImage(ISize size,
Format format,
std::shared_ptr<const fml::Mapping> allocation);
~Image();
~DecompressedImage();
const ISize& GetSize() const;
@@ -39,7 +39,7 @@ class Image {
const std::shared_ptr<const fml::Mapping>& GetAllocation() const;
Image ConvertToRGBA() const;
DecompressedImage ConvertToRGBA() const;
private:
ISize size_;

View File

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