Use Impeller geometry classes in DLMatrixClipTracker (flutter/engine#51919)

The `DisplayListMatrixClipTracker` class performs the working of maintaining a stack of cull rects and transforms for classes in the engine that require such functionality (such as `DisplayListBuilder`, `DiffContext`, and `LayerStateStack`.

Until now it has been using a mixture of SkMatrix and SkM44 along with an SkRect to maintain this information, but that complexity involves overhead and implementation complexity which complicates future work on these areas.

By reimplementing the class to use Impeller geometry objects, the internal architecture is greatly simplified allowing for easier future maintenance.
This commit is contained in:
Jim Graham
2024-04-08 17:45:19 -07:00
committed by GitHub
parent 591e0de86f
commit 434e229263
13 changed files with 2007 additions and 632 deletions

View File

@@ -10,13 +10,62 @@
#include "flutter/impeller/geometry/rect.h"
#include "third_party/skia/include/core/SkM44.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkPoint3.h"
namespace flutter {
namespace {
static constexpr float kPiOver4 = impeller::kPiOver4;
static constexpr float kFieldOfView = impeller::kPiOver2 + impeller::kPiOver4;
static constexpr float kSimplePerspective[16] = {
// clang-format off
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
};
// The following matrices slowly slide a perspective-skewed transform of
// the rect past the near clipping plane. Keeping the Y perspective factor
// at -0.006 allows the X perspective factor to slowly move the transformed
// rectangle into the clipping pane over the range of [-0.01, to -0.025].
static constexpr float kClipOneCornerPerspective[16] = {
// clang-format off
2.0f, 0.0f, 0.0f, -0.01f,
0.0f, 2.0f, 0.0f, -0.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
};
static constexpr float kClipTwoCornersPerspective[16] = {
// clang-format off
2.0f, 0.0f, 0.0f, -.015f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
};
static constexpr float kClipThreeCornersPerspective[16] = {
// clang-format off
2.0f, 0.0f, 0.0f, -.02f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
};
static constexpr float kClipFourCornersPerspective[16] = {
// clang-format off
2.0f, 0.0f, 0.0f, -.025f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
};
enum class AdapterType {
kSkMatrix,
@@ -70,11 +119,9 @@ class TransformAdapter {
virtual void InitPoint(TestPoint& point, float x, float y) const = 0;
// The actual methods that do work and are the meat of the benchmarks.
virtual void SetIdentity(TestTransform& result) const = 0;
virtual void SetPerspective(TestTransform& result,
float fov_radians,
float near,
float far) const = 0;
virtual void InitTransformIdentity(TestTransform& result) const = 0;
virtual void InitTransformColMatrix(TestTransform& result,
const float m[16]) const = 0;
virtual void Translate(TestTransform& result, float tx, float ty) const = 0;
virtual void Scale(TestTransform& result, float sx, float sy) const = 0;
@@ -91,9 +138,14 @@ class TransformAdapter {
const TestPoint in[],
TestPoint out[],
int n) const = 0;
virtual void TransformRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const = 0;
virtual void TransformRectFast(const TestTransform& transform,
const TestRect& in,
TestRect& out) const = 0;
virtual void TransformAndClipRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const = 0;
virtual int CountClippedCorners(const TestTransform& transform,
const TestRect& rect) const = 0;
virtual void InvertUnchecked(const TestTransform& transform,
TestTransform& result) const = 0;
virtual bool InvertAndCheck(const TestTransform& transform,
@@ -116,11 +168,6 @@ class SkiaAdapterBase : public TransformAdapter {
float bottom) const override {
rect.sk_rect.setLTRB(left, top, right, bottom);
}
protected:
static SkM44 MakePerspective(float fov_radians, float near, float far) {
return SkM44::Perspective(near, far, fov_radians);
}
};
class SkMatrixAdapter : public SkiaAdapterBase {
@@ -128,15 +175,13 @@ class SkMatrixAdapter : public SkiaAdapterBase {
SkMatrixAdapter() = default;
~SkMatrixAdapter() = default;
void SetIdentity(TestTransform& result) const override {
void InitTransformIdentity(TestTransform& result) const override {
result.sk_matrix.setIdentity();
}
virtual void SetPerspective(TestTransform& result,
float fov_radians,
float near,
float far) const override {
result.sk_matrix = MakePerspective(fov_radians, near, far).asM33();
virtual void InitTransformColMatrix(TestTransform& result,
const float m[16]) const override {
result.sk_matrix = SkM44::ColMajor(m).asM33();
}
void Translate(TestTransform& result, float tx, float ty) const override {
@@ -172,10 +217,33 @@ class SkMatrixAdapter : public SkiaAdapterBase {
reinterpret_cast<const SkPoint*>(in), n);
}
void TransformRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.sk_rect = transform.sk_matrix.mapRect(in.sk_rect);
void TransformRectFast(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.sk_rect =
transform.sk_matrix.mapRect(in.sk_rect, SkApplyPerspectiveClip::kNo);
}
void TransformAndClipRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.sk_rect =
transform.sk_matrix.mapRect(in.sk_rect, SkApplyPerspectiveClip::kYes);
}
int CountClippedCorners(const TestTransform& transform,
const TestRect& rect) const {
SkPoint3 homogenous[4];
SkPoint corners[4];
rect.sk_rect.toQuad(corners);
transform.sk_matrix.mapHomogeneousPoints(homogenous, corners, 4);
int count = 0;
for (SkPoint3 hpt : homogenous) {
if (hpt.fZ <= 0) {
count++;
}
}
return count;
}
void InvertUnchecked(const TestTransform& transform,
@@ -195,15 +263,13 @@ class SkM44Adapter : public SkiaAdapterBase {
SkM44Adapter() = default;
~SkM44Adapter() = default;
void SetIdentity(TestTransform& storage) const override {
void InitTransformIdentity(TestTransform& storage) const override {
storage.sk_m44.setIdentity();
}
virtual void SetPerspective(TestTransform& result,
float fov_radians,
float near,
float far) const override {
result.sk_m44 = MakePerspective(fov_radians, near, far);
virtual void InitTransformColMatrix(TestTransform& result,
const float m[16]) const override {
result.sk_m44 = SkM44::ColMajor(m);
}
void Translate(TestTransform& storage, float tx, float ty) const override {
@@ -239,10 +305,39 @@ class SkM44Adapter : public SkiaAdapterBase {
reinterpret_cast<const SkPoint*>(in), n);
}
void TransformRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.sk_rect = transform.sk_m44.asM33().mapRect(in.sk_rect);
void TransformRectFast(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
// clang-format off
out.sk_rect = transform.sk_m44
.asM33()
.mapRect(in.sk_rect, SkApplyPerspectiveClip::kNo);
// clang-format on
}
void TransformAndClipRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
// clang-format off
out.sk_rect = transform.sk_m44
.asM33()
.mapRect(in.sk_rect, SkApplyPerspectiveClip::kYes);
// clang-format on
}
int CountClippedCorners(const TestTransform& transform,
const TestRect& rect) const {
SkPoint3 homogenous[4];
SkPoint corners[4];
rect.sk_rect.toQuad(corners);
transform.sk_m44.asM33().mapHomogeneousPoints(homogenous, corners, 4);
int count = 0;
for (SkPoint3 hpt : homogenous) {
if (hpt.fZ <= 0) {
count++;
}
}
return count;
}
void InvertUnchecked(const TestTransform& transform,
@@ -276,17 +371,20 @@ class ImpellerMatrixAdapter : public TransformAdapter {
rect.impeller_rect = impeller::Rect::MakeLTRB(left, top, right, bottom);
}
void SetIdentity(TestTransform& storage) const override {
void InitTransformIdentity(TestTransform& storage) const override {
storage.impeller_matrix = impeller::Matrix();
}
virtual void SetPerspective(TestTransform& result,
float fov_radians,
float near,
float far) const override {
impeller::Radians fov = impeller::Radians(fov_radians);
result.impeller_matrix =
impeller::Matrix::MakePerspective(fov, 1.0f, near, far);
virtual void InitTransformColMatrix(TestTransform& result,
const float m[16]) const override {
// clang-format off
result.impeller_matrix = impeller::Matrix::MakeColumn(
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[12], m[13], m[14], m[15]
);
// clang-format on
}
void Translate(TestTransform& storage, float tx, float ty) const override {
@@ -324,13 +422,33 @@ class ImpellerMatrixAdapter : public TransformAdapter {
}
}
void TransformRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
void TransformRectFast(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.impeller_rect =
in.impeller_rect.TransformBounds(transform.impeller_matrix);
}
void TransformAndClipRect(const TestTransform& transform,
const TestRect& in,
TestRect& out) const override {
out.impeller_rect =
in.impeller_rect.TransformAndClipBounds(transform.impeller_matrix);
}
int CountClippedCorners(const TestTransform& transform,
const TestRect& rect) const {
auto corners = rect.impeller_rect.GetPoints();
int count = 0;
for (auto pt : corners) {
auto hpt = transform.impeller_matrix.TransformHomogenous(pt);
if (hpt.z <= 0) {
count++;
}
}
return count;
}
void InvertUnchecked(const TestTransform& transform,
TestTransform& result) const override {
result.impeller_matrix = transform.impeller_matrix.Invert();
@@ -343,44 +461,99 @@ class ImpellerMatrixAdapter : public TransformAdapter {
}
};
using SetupFunction = std::function<void(TransformAdapter*, TestTransform&)>;
// The rect argument is passed in by the TransformRect benchmarks so that
// the setup function can make sure that it sets up a transform that
// clips the desired number of corners during the bounds transform.
using SetupFunction =
std::function<void(TransformAdapter*, TestTransform&, const TestRect*)>;
static void SetupIdentity(const TransformAdapter* adapter,
TestTransform& transform) {
adapter->SetIdentity(transform);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformIdentity(transform);
}
static void SetupTranslate(const TransformAdapter* adapter,
TestTransform& transform) {
adapter->SetIdentity(transform);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformIdentity(transform);
adapter->Translate(transform, 10.2, 12.3);
}
static void SetupScale(const TransformAdapter* adapter,
TestTransform& transform) {
adapter->SetIdentity(transform);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformIdentity(transform);
adapter->Scale(transform, 2.0, 2.0);
}
static void SetupScaleTranslate(const TransformAdapter* adapter,
TestTransform& transform) {
adapter->SetIdentity(transform);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformIdentity(transform);
adapter->Scale(transform, 2.0, 2.0);
adapter->Translate(transform, 10.2, 12.3);
}
static void SetupRotate(const TransformAdapter* adapter,
TestTransform& transform) {
adapter->SetIdentity(transform);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformIdentity(transform);
adapter->RotateRadians(transform, kPiOver4);
}
static void SetupPerspective(const TransformAdapter* adapter,
TestTransform& transform) {
auto fov_radians = kFieldOfView;
auto near = 1.0f;
auto far = 100.0f;
adapter->SetPerspective(transform, fov_radians, near, far);
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kSimplePerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 0);
}
}
static void SetupPerspectiveClipNone(const TransformAdapter* adapter,
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kSimplePerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 0);
}
}
static void SetupPerspectiveClipOne(const TransformAdapter* adapter,
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kClipOneCornerPerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 1);
}
}
static void SetupPerspectiveClipTwo(const TransformAdapter* adapter,
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kClipTwoCornersPerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 2);
}
}
static void SetupPerspectiveClipThree(const TransformAdapter* adapter,
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kClipThreeCornersPerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 3);
}
}
static void SetupPerspectiveClipFour(const TransformAdapter* adapter,
TestTransform& transform,
const TestRect* rect) {
adapter->InitTransformColMatrix(transform, kClipFourCornersPerspective);
if (rect) {
FML_CHECK(adapter->CountClippedCorners(transform, *rect) == 4);
}
}
// We use a function to return the appropriate adapter so that all methods
@@ -414,18 +587,15 @@ static void BM_SetIdentity(benchmark::State& state, AdapterType type) {
auto adapter = GetAdapter(type);
TestTransform transform;
while (state.KeepRunning()) {
adapter->SetIdentity(transform);
adapter->InitTransformIdentity(transform);
}
}
static void BM_SetPerspective(benchmark::State& state, AdapterType type) {
auto adapter = GetAdapter(type);
TestTransform transform;
auto fov_radians = kFieldOfView;
auto near = 1.0f;
auto far = 100.0f;
while (state.KeepRunning()) {
adapter->SetPerspective(transform, fov_radians, near, far);
adapter->InitTransformColMatrix(transform, kSimplePerspective);
}
}
@@ -435,7 +605,7 @@ static void BM_Translate(benchmark::State& state,
float ty) {
auto adapter = GetAdapter(type);
TestTransform transform;
adapter->SetIdentity(transform);
adapter->InitTransformIdentity(transform);
bool flip = true;
while (state.KeepRunning()) {
if (flip) {
@@ -450,7 +620,7 @@ static void BM_Translate(benchmark::State& state,
static void BM_Scale(benchmark::State& state, AdapterType type, float scale) {
auto adapter = GetAdapter(type);
TestTransform transform;
adapter->SetIdentity(transform);
adapter->InitTransformIdentity(transform);
float inv_scale = 1.0f / scale;
bool flip = true;
while (state.KeepRunning()) {
@@ -468,7 +638,7 @@ static void BM_Rotate(benchmark::State& state,
float radians) {
auto adapter = GetAdapter(type);
TestTransform transform;
adapter->SetIdentity(transform);
adapter->InitTransformIdentity(transform);
while (state.KeepRunning()) {
adapter->RotateRadians(transform, radians);
}
@@ -480,8 +650,8 @@ static void BM_Concat(benchmark::State& state,
const SetupFunction& b_setup) {
auto adapter = GetAdapter(type);
TestTransform a, b, result;
a_setup(adapter.get(), a);
b_setup(adapter.get(), b);
a_setup(adapter.get(), a, nullptr);
b_setup(adapter.get(), b, nullptr);
while (state.KeepRunning()) {
adapter->Concat(a, b, result);
}
@@ -492,7 +662,7 @@ static void BM_TransformPoint(benchmark::State& state,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
setup(adapter.get(), transform);
setup(adapter.get(), transform, nullptr);
TestPoint point, result;
adapter->InitPoint(point, 25.7, 32.4);
while (state.KeepRunning()) {
@@ -505,7 +675,7 @@ static void BM_TransformPoints(benchmark::State& state,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
setup(adapter.get(), transform);
setup(adapter.get(), transform, nullptr);
const int Xs = 10;
const int Ys = 10;
const int N = Xs * Ys;
@@ -526,16 +696,29 @@ static void BM_TransformPoints(benchmark::State& state,
state.SetItemsProcessed(item_count);
}
static void BM_TransformRect(benchmark::State& state,
AdapterType type,
const SetupFunction& setup) {
static void BM_TransformRectFast(benchmark::State& state,
AdapterType type,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
setup(adapter.get(), transform);
TestRect rect, result;
adapter->InitRectLTRB(rect, 100, 100, 200, 200);
setup(adapter.get(), transform, &rect);
while (state.KeepRunning()) {
adapter->TransformRect(transform, rect, result);
adapter->TransformRectFast(transform, rect, result);
}
}
static void BM_TransformAndClipRect(benchmark::State& state,
AdapterType type,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
TestRect rect, result;
adapter->InitRectLTRB(rect, 100, 100, 200, 200);
setup(adapter.get(), transform, &rect);
while (state.KeepRunning()) {
adapter->TransformAndClipRect(transform, rect, result);
}
}
@@ -544,7 +727,7 @@ static void BM_InvertUnchecked(benchmark::State& state,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
setup(adapter.get(), transform);
setup(adapter.get(), transform, nullptr);
TestTransform result;
while (state.KeepRunning()) {
adapter->InvertUnchecked(transform, result);
@@ -556,7 +739,7 @@ static void BM_InvertAndCheck(benchmark::State& state,
const SetupFunction& setup) {
auto adapter = GetAdapter(type);
TestTransform transform;
setup(adapter.get(), transform);
setup(adapter.get(), transform, nullptr);
TestTransform result;
while (state.KeepRunning()) {
adapter->InvertAndCheck(transform, result);
@@ -642,11 +825,26 @@ BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformPoints, ScaleTranslate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformPoints, Rotate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformPoints, Perspective);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, Identity);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, Translate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, Scale);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, ScaleTranslate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, Rotate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRect, Perspective);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, Identity);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, Translate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, Scale);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, ScaleTranslate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, Rotate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, PerspectiveClipNone);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, PerspectiveClipOne);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, PerspectiveClipTwo);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, PerspectiveClipThree);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformRectFast, PerspectiveClipFour);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, Identity);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, Translate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, Scale);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, ScaleTranslate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, Rotate);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, PerspectiveClipNone);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, PerspectiveClipOne);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, PerspectiveClipTwo);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, PerspectiveClipThree);
BENCHMARK_CAPTURE_ALL_SETUP(BM_TransformAndClipRect, PerspectiveClipFour);
} // namespace flutter

View File

@@ -664,7 +664,8 @@ void DisplayListBuilder::saveLayer(const SkRect& bounds,
// The filtered bounds will be clipped to the existing clip rect when
// this layer is restored.
// If bounds is null then the original cull_rect will be used.
tracker_.resetCullRect(in_options.bounds_from_caller() ? &bounds : nullptr);
tracker_.resetLocalCullRect(in_options.bounds_from_caller() ? &bounds
: nullptr);
} else if (in_options.bounds_from_caller()) {
// Even though Skia claims that the bounds are only a hint, they actually
// use them as the temporary layer bounds during rendering the layer, so

View File

@@ -9,89 +9,6 @@
namespace flutter {
class Data4x4 : public DisplayListMatrixClipTracker::Data {
public:
Data4x4(const SkM44& m44, const SkRect& rect) : Data(rect), m44_(m44) {}
explicit Data4x4(const Data* copy)
: Data(copy->device_cull_rect()), m44_(copy->matrix_4x4()) {}
~Data4x4() override = default;
bool is_4x4() const override { return true; }
SkMatrix matrix_3x3() const override { return m44_.asM33(); }
SkM44 matrix_4x4() const override { return m44_; }
SkRect local_cull_rect() const override;
void translate(SkScalar tx, SkScalar ty) override {
m44_.preTranslate(tx, ty);
}
void scale(SkScalar sx, SkScalar sy) override { m44_.preScale(sx, sy); }
void skew(SkScalar skx, SkScalar sky) override {
m44_.preConcat(SkMatrix::Skew(skx, sky));
}
void rotate(SkScalar degrees) override {
m44_.preConcat(SkMatrix::RotateDeg(degrees));
}
void transform(const SkMatrix& matrix) override { m44_.preConcat(matrix); }
void transform(const SkM44& m44) override { m44_.preConcat(m44); }
void setTransform(const SkMatrix& matrix) override { m44_ = SkM44(matrix); }
void setTransform(const SkM44& m44) override { m44_ = m44; }
void setIdentity() override { m44_.setIdentity(); }
bool mapRect(const SkRect& rect, SkRect* mapped) const override {
return m44_.asM33().mapRect(mapped, rect);
}
bool canBeInverted() const override { return m44_.asM33().invert(nullptr); }
protected:
bool has_perspective() const override;
private:
SkM44 m44_;
};
class Data3x3 : public DisplayListMatrixClipTracker::Data {
public:
Data3x3(const SkMatrix& matrix, const SkRect& rect)
: Data(rect), matrix_(matrix) {}
explicit Data3x3(const Data* copy)
: Data(copy->device_cull_rect()), matrix_(copy->matrix_3x3()) {}
~Data3x3() override = default;
bool is_4x4() const override { return false; }
SkMatrix matrix_3x3() const override { return matrix_; }
SkM44 matrix_4x4() const override { return SkM44(matrix_); }
SkRect local_cull_rect() const override;
void translate(SkScalar tx, SkScalar ty) override {
matrix_.preTranslate(tx, ty);
}
void scale(SkScalar sx, SkScalar sy) override { matrix_.preScale(sx, sy); }
void skew(SkScalar skx, SkScalar sky) override { matrix_.preSkew(skx, sky); }
void rotate(SkScalar degrees) override { matrix_.preRotate(degrees); }
void transform(const SkMatrix& matrix) override { matrix_.preConcat(matrix); }
void transform(const SkM44& m44) override {
FML_CHECK(false) << "SkM44 was concatenated without upgrading Data";
}
void setTransform(const SkMatrix& matrix) override { matrix_ = matrix; }
void setTransform(const SkM44& m44) override {
FML_CHECK(false) << "SkM44 was set without upgrading Data";
}
void setIdentity() override { matrix_.setIdentity(); }
bool mapRect(const SkRect& rect, SkRect* mapped) const override {
return matrix_.mapRect(mapped, rect);
}
bool canBeInverted() const override { return matrix_.invert(nullptr); }
protected:
bool has_perspective() const override { return matrix_.hasPerspective(); }
private:
SkMatrix matrix_;
};
bool DisplayListMatrixClipTracker::is_3x3(const SkM44& m) {
// clang-format off
return ( m.rc(0, 2) == 0 &&
@@ -101,91 +18,64 @@ bool DisplayListMatrixClipTracker::is_3x3(const SkM44& m) {
// clang-format on
}
DisplayListMatrixClipState::DisplayListMatrixClipState(const DlRect& cull_rect,
const DlMatrix& matrix)
: cull_rect_(cull_rect), matrix_(matrix) {}
DisplayListMatrixClipState::DisplayListMatrixClipState(const SkRect& cull_rect,
const SkMatrix& matrix)
: cull_rect_(ToDlRect(cull_rect)), matrix_(ToDlMatrix(matrix)) {}
DisplayListMatrixClipState::DisplayListMatrixClipState(const SkRect& cull_rect,
const SkM44& matrix)
: cull_rect_(ToDlRect(cull_rect)), matrix_(ToDlMatrix(matrix)) {}
DisplayListMatrixClipTracker::DisplayListMatrixClipTracker(
const SkRect& cull_rect,
const SkMatrix& matrix)
: original_cull_rect_(cull_rect) {
const DlRect& cull_rect,
const DlMatrix& matrix) {
// isEmpty protects us against NaN as we normalize any empty cull rects
SkRect cull = cull_rect.isEmpty() ? SkRect::MakeEmpty() : cull_rect;
saved_.emplace_back(std::make_unique<Data3x3>(matrix, cull));
current_ = saved_.back().get();
DlRect cull = cull_rect.IsEmpty() ? DlRect() : cull_rect;
saved_.emplace_back(cull, matrix);
current_ = &saved_.back();
save(); // saved_[0] will always be the initial settings
}
DisplayListMatrixClipTracker::DisplayListMatrixClipTracker(
const SkRect& cull_rect,
const SkM44& m44)
: original_cull_rect_(cull_rect) {
const SkMatrix& matrix) {
// isEmpty protects us against NaN as we normalize any empty cull rects
SkRect cull = cull_rect.isEmpty() ? SkRect::MakeEmpty() : cull_rect;
if (is_3x3(m44)) {
saved_.emplace_back(std::make_unique<Data3x3>(m44.asM33(), cull));
} else {
saved_.emplace_back(std::make_unique<Data4x4>(m44, cull));
}
current_ = saved_.back().get();
saved_.emplace_back(cull, matrix);
current_ = &saved_.back();
save(); // saved_[0] will always be the initial settings
}
// clang-format off
void DisplayListMatrixClipTracker::transform2DAffine(
SkScalar mxx, SkScalar mxy, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myt) {
if (!current_->is_4x4()) {
transform(SkMatrix::MakeAll(mxx, mxy, mxt,
myx, myy, myt,
0, 0, 1));
} else {
transform(SkM44(mxx, mxy, 0, mxt,
myx, myy, 0, myt,
0, 0, 1, 0,
0, 0, 0, 1));
}
DisplayListMatrixClipTracker::DisplayListMatrixClipTracker(
const SkRect& cull_rect,
const SkM44& m44) {
// isEmpty protects us against NaN as we normalize any empty cull rects
SkRect cull = cull_rect.isEmpty() ? SkRect::MakeEmpty() : cull_rect;
saved_.emplace_back(cull, m44);
current_ = &saved_.back();
save(); // saved_[0] will always be the initial settings
}
void DisplayListMatrixClipTracker::transformFullPerspective(
SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) {
if (!current_->is_4x4()) {
if ( mxz == 0 &&
myz == 0 &&
mzx == 0 && mzy == 0 && mzz == 1 && mzt == 0 &&
mwz == 0) {
transform(SkMatrix::MakeAll(mxx, mxy, mxt,
myx, myy, myt,
mwx, mwy, mwt));
return;
}
}
transform(SkM44(mxx, mxy, mxz, mxt,
myx, myy, myz, myt,
mzx, mzy, mzz, mzt,
mwx, mwy, mwz, mwt));
}
// clang-format on
void DisplayListMatrixClipTracker::save() {
if (current_->is_4x4()) {
saved_.emplace_back(std::make_unique<Data4x4>(current_));
} else {
saved_.emplace_back(std::make_unique<Data3x3>(current_));
}
current_ = saved_.back().get();
saved_.emplace_back(*current_);
current_ = &saved_.back();
}
void DisplayListMatrixClipTracker::restore() {
if (saved_.size() > 2) {
saved_.pop_back();
current_ = saved_.back().get();
current_ = &saved_.back();
}
}
void DisplayListMatrixClipTracker::reset() {
while (saved_.size() > 1) {
saved_.pop_back();
current_ = saved_.back().get();
current_ = &saved_.back();
}
save(); // saved_[0] will always be the initial settings
}
@@ -200,51 +90,25 @@ void DisplayListMatrixClipTracker::restoreToCount(int restore_count) {
}
}
void DisplayListMatrixClipTracker::transform(const SkM44& m44) {
if (!current_->is_4x4()) {
if (is_3x3(m44)) {
current_->transform(m44.asM33());
return;
}
saved_.back() = std::make_unique<Data4x4>(current_);
current_ = saved_.back().get();
}
current_->transform(m44);
}
void DisplayListMatrixClipTracker::setTransform(const SkM44& m44) {
if (!current_->is_4x4()) {
if (is_3x3(m44)) {
current_->setTransform(m44.asM33());
return;
}
saved_.back() = std::make_unique<Data4x4>(current_);
current_ = saved_.back().get();
}
current_->setTransform(m44);
}
bool DisplayListMatrixClipTracker::inverseTransform(
const DisplayListMatrixClipTracker& tracker_) {
if (tracker_.using_4x4_matrix()) {
SkM44 inverse;
if (tracker_.matrix_4x4().invert(&inverse)) {
transform(inverse);
return true;
}
} else {
SkMatrix inverse;
if (tracker_.matrix_3x3().invert(&inverse)) {
transform(inverse);
return true;
}
bool DisplayListMatrixClipState::inverseTransform(
const DisplayListMatrixClipState& tracker) {
if (tracker.is_matrix_invertable()) {
matrix_ = matrix_ * tracker.matrix_.Invert();
return true;
}
return false;
}
void DisplayListMatrixClipTracker::clipRRect(const SkRRect& rrect,
ClipOp op,
bool is_aa) {
void DisplayListMatrixClipState::clipRect(const DlRect& rect,
ClipOp op,
bool is_aa) {
adjustCullRect(rect, op, is_aa);
}
void DisplayListMatrixClipState::clipRRect(const SkRRect& rrect,
ClipOp op,
bool is_aa) {
SkRect bounds = rrect.getBounds();
switch (op) {
case ClipOp::kIntersect:
break;
@@ -254,11 +118,12 @@ void DisplayListMatrixClipTracker::clipRRect(const SkRRect& rrect,
}
break;
}
current_->clipBounds(rrect.getBounds(), op, is_aa);
adjustCullRect(ToDlRect(bounds), op, is_aa);
}
void DisplayListMatrixClipTracker::clipPath(const SkPath& path,
ClipOp op,
bool is_aa) {
void DisplayListMatrixClipState::clipPath(const SkPath& path,
ClipOp op,
bool is_aa) {
// Map "kDifference of inverse path" to "kIntersect of the original path" and
// map "kIntersect of inverse path" to "kDifference of the original path"
if (path.isInverseFillType()) {
@@ -283,166 +148,105 @@ void DisplayListMatrixClipTracker::clipPath(const SkPath& path,
}
break;
}
current_->clipBounds(bounds, op, is_aa);
adjustCullRect(ToDlRect(bounds), op, is_aa);
}
bool DisplayListMatrixClipTracker::Data::content_culled(
const SkRect& content_bounds) const {
if (cull_rect_.isEmpty() || content_bounds.isEmpty()) {
bool DisplayListMatrixClipState::content_culled(
const DlRect& content_bounds) const {
if (cull_rect_.IsEmpty() || content_bounds.IsEmpty()) {
return true;
}
if (!canBeInverted()) {
if (!is_matrix_invertable()) {
return true;
}
if (has_perspective()) {
return false;
}
SkRect mapped;
DlRect mapped;
mapRect(content_bounds, &mapped);
return !mapped.intersects(cull_rect_);
return !mapped.IntersectsWithRect(cull_rect_);
}
void DisplayListMatrixClipTracker::Data::resetBounds(const SkRect& cull_rect) {
if (!cull_rect.isEmpty()) {
SkRect rect;
mapRect(cull_rect, &rect);
if (!rect.isEmpty()) {
cull_rect_ = rect;
void DisplayListMatrixClipState::resetDeviceCullRect(const DlRect& cull_rect) {
if (cull_rect.IsEmpty()) {
cull_rect_ = DlRect();
} else {
cull_rect_ = cull_rect;
}
}
void DisplayListMatrixClipState::resetLocalCullRect(const DlRect& cull_rect) {
if (!cull_rect.IsEmpty()) {
mapRect(cull_rect, &cull_rect_);
if (!cull_rect_.IsEmpty()) {
return;
}
}
cull_rect_.setEmpty();
cull_rect_ = DlRect();
}
void DisplayListMatrixClipTracker::Data::clipBounds(const SkRect& clip,
ClipOp op,
bool is_aa) {
if (cull_rect_.isEmpty()) {
// No point in intersecting further.
void DisplayListMatrixClipState::adjustCullRect(const DlRect& clip,
ClipOp op,
bool is_aa) {
if (cull_rect_.IsEmpty()) {
// No point in constraining further.
return;
}
if (has_perspective()) {
if (matrix_.HasPerspective()) {
// We can conservatively ignore this clip.
return;
}
switch (op) {
case ClipOp::kIntersect: {
if (clip.isEmpty()) {
cull_rect_.setEmpty();
if (clip.IsEmpty()) {
cull_rect_ = DlRect();
break;
}
SkRect rect;
DlRect rect;
mapRect(clip, &rect);
if (is_aa) {
rect.roundOut(&rect);
}
if (!cull_rect_.intersect(rect)) {
cull_rect_.setEmpty();
rect = DlRect::RoundOut(rect);
}
cull_rect_ = cull_rect_.Intersection(rect).value_or(DlRect());
break;
}
case ClipOp::kDifference: {
if (clip.isEmpty()) {
if (clip.IsEmpty()) {
break;
}
SkRect rect;
DlRect rect;
if (mapRect(clip, &rect)) {
// This technique only works if the transform is rect -> rect
if (is_aa) {
SkIRect rounded;
rect.round(&rounded);
if (rounded.isEmpty()) {
rect = DlRect::Round(rect);
if (rect.IsEmpty()) {
break;
}
rect.set(rounded);
}
if (!rect.intersects(cull_rect_)) {
break;
}
if (rect.fLeft <= cull_rect_.fLeft &&
rect.fRight >= cull_rect_.fRight) {
// bounds spans entire width of cull_rect_
// therefore we can slice off a top or bottom
// edge of the cull_rect_.
SkScalar top = cull_rect_.fTop;
SkScalar btm = cull_rect_.fBottom;
if (rect.fTop <= top) {
top = rect.fBottom;
}
if (rect.fBottom >= btm) {
btm = rect.fTop;
}
if (top < btm) {
cull_rect_.fTop = top;
cull_rect_.fBottom = btm;
} else {
cull_rect_.setEmpty();
}
} else if (rect.fTop <= cull_rect_.fTop &&
rect.fBottom >= cull_rect_.fBottom) {
// bounds spans entire height of cull_rect_
// therefore we can slice off a left or right
// edge of the cull_rect_.
SkScalar lft = cull_rect_.fLeft;
SkScalar rgt = cull_rect_.fRight;
if (rect.fLeft <= lft) {
lft = rect.fRight;
}
if (rect.fRight >= rgt) {
rgt = rect.fLeft;
}
if (lft < rgt) {
cull_rect_.fLeft = lft;
cull_rect_.fRight = rgt;
} else {
cull_rect_.setEmpty();
}
}
cull_rect_ = cull_rect_.CutoutOrEmpty(rect);
}
break;
}
}
}
SkRect Data4x4::local_cull_rect() const {
if (cull_rect_.isEmpty()) {
return cull_rect_;
}
SkMatrix inverse;
if (!m44_.asM33().invert(&inverse)) {
SkRect DisplayListMatrixClipState::local_cull_rect() const {
if (cull_rect_.IsEmpty()) {
return SkRect::MakeEmpty();
}
if (has_perspective()) {
if (!is_matrix_invertable()) {
return SkRect::MakeEmpty();
}
if (matrix_.HasPerspective2D()) {
// We could do a 4-point long-form conversion, but since this is
// only used for culling, let's just return a non-constricting
// cull rect.
return DisplayListBuilder::kMaxCullRect;
}
return inverse.mapRect(cull_rect_);
}
bool Data4x4::has_perspective() const {
return (m44_.rc(3, 0) != 0 || //
m44_.rc(3, 1) != 0 || //
m44_.rc(3, 2) != 0 || //
m44_.rc(3, 3) != 1);
}
SkRect Data3x3::local_cull_rect() const {
if (cull_rect_.isEmpty()) {
return cull_rect_;
}
SkMatrix inverse;
if (!matrix_.invert(&inverse)) {
return SkRect::MakeEmpty();
}
if (matrix_.hasPerspective()) {
// We could do a 4-point long-form conversion, but since this is
// only used for culling, let's just return a non-constricting
// cull rect.
return DisplayListBuilder::kMaxCullRect;
}
return inverse.mapRect(cull_rect_);
DlMatrix inverse = matrix_.Invert();
// We eliminated perspective above so we can use the cheaper non-clipping
// bounds transform method.
return ToSkRect(cull_rect_.TransformBounds(inverse));
}
} // namespace flutter

View File

@@ -9,6 +9,8 @@
#include "flutter/display_list/dl_canvas.h"
#include "flutter/fml/logging.h"
#include "flutter/impeller/geometry/matrix.h"
#include "flutter/impeller/geometry/rect.h"
#include "third_party/skia/include/core/SkM44.h"
#include "third_party/skia/include/core/SkMatrix.h"
@@ -19,13 +21,55 @@
namespace flutter {
class DisplayListMatrixClipTracker {
class DisplayListMatrixClipState {
private:
using ClipOp = DlCanvas::ClipOp;
using DlRect = impeller::Rect;
using DlMatrix = impeller::Matrix;
using DlDegrees = impeller::Degrees;
static_assert(sizeof(SkRect) == sizeof(DlRect));
static const DlRect& ToDlRect(const SkRect& rect) {
return *reinterpret_cast<const DlRect*>(&rect);
}
static constexpr DlMatrix ToDlMatrix(const SkMatrix& matrix) {
// clang-format off
return DlMatrix::MakeColumn(
matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY], 0.0f, matrix[SkMatrix::kMPersp0],
matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY], 0.0f, matrix[SkMatrix::kMPersp1],
0.0f, 0.0f, 1.0f, 0.0f,
matrix[SkMatrix::kMTransX], matrix[SkMatrix::kMTransY], 0.0f, matrix[SkMatrix::kMPersp2]
);
// clang-format on
}
static constexpr DlMatrix ToDlMatrix(const SkM44& matrix) {
DlMatrix dl_matrix;
matrix.getColMajor(dl_matrix.m);
return dl_matrix;
}
static const SkRect& ToSkRect(const DlRect& rect) {
return *reinterpret_cast<const SkRect*>(&rect);
}
static constexpr SkMatrix ToSkMatrix(const DlMatrix& matrix) {
return SkMatrix::MakeAll(matrix.m[0], matrix.m[4], matrix.m[12],
matrix.m[1], matrix.m[5], matrix.m[13],
matrix.m[3], matrix.m[7], matrix.m[15]);
}
static constexpr SkM44 ToSkM44(const DlMatrix& matrix) {
return SkM44::ColMajor(matrix.m);
}
public:
DisplayListMatrixClipTracker(const SkRect& cull_rect, const SkMatrix& matrix);
DisplayListMatrixClipTracker(const SkRect& cull_rect, const SkM44& matrix);
DisplayListMatrixClipState(const DlRect& cull_rect, const DlMatrix& matrix);
DisplayListMatrixClipState(const SkRect& cull_rect, const SkMatrix& matrix);
DisplayListMatrixClipState(const SkRect& cull_rect, const SkM44& matrix);
DisplayListMatrixClipState(const DisplayListMatrixClipState& other) = default;
// This method should almost never be used as it breaks the encapsulation
// of the enclosing clips. However it is needed for practical purposes in
@@ -36,16 +80,163 @@ class DisplayListMatrixClipTracker {
// layer into the enclosing clipped area.
// Omitting the |cull_rect| argument, or passing nullptr, will restore the
// cull rect to the initial value it had when the tracker was constructed.
void resetCullRect(const SkRect* cull_rect = nullptr) {
current_->resetBounds(cull_rect ? *cull_rect : original_cull_rect_);
void resetDeviceCullRect(const DlRect& cull_rect);
void resetDeviceCullRect(const SkRect& cull_rect) {
resetDeviceCullRect(ToDlRect(cull_rect));
}
void resetLocalCullRect(const DlRect& cull_rect);
void resetLocalCullRect(const SkRect& cull_rect) {
resetLocalCullRect(ToDlRect(cull_rect));
}
bool using_4x4_matrix() const { return !matrix_.IsAffine(); }
bool is_matrix_invertable() const { return matrix_.GetDeterminant() != 0.0f; }
bool has_perspective() const { return matrix_.HasPerspective(); }
const DlMatrix& matrix() const { return matrix_; }
SkM44 matrix_4x4() const { return SkM44::ColMajor(matrix_.m); }
SkMatrix matrix_3x3() const { return ToSkMatrix(matrix_); }
SkRect local_cull_rect() const;
SkRect device_cull_rect() const { return ToSkRect(cull_rect_); }
bool content_culled(const DlRect& content_bounds) const;
bool content_culled(const SkRect& content_bounds) const {
return content_culled(ToDlRect(content_bounds));
}
bool is_cull_rect_empty() const { return cull_rect_.IsEmpty(); }
void translate(SkScalar tx, SkScalar ty) {
matrix_ = matrix_.Translate({tx, ty});
}
void scale(SkScalar sx, SkScalar sy) {
matrix_ = matrix_.Scale({sx, sy, 1.0f});
}
void skew(SkScalar skx, SkScalar sky) {
matrix_ = matrix_ * DlMatrix::MakeSkew(skx, sky);
}
void rotate(SkScalar degrees) {
matrix_ = matrix_ * DlMatrix::MakeRotationZ(DlDegrees(degrees));
}
void transform(const DlMatrix& matrix) { matrix_ = matrix_ * matrix; }
void transform(const SkM44& m44) { transform(ToDlMatrix(m44)); }
void transform(const SkMatrix& matrix) { transform(ToDlMatrix(matrix)); }
// clang-format off
void transform2DAffine(
SkScalar mxx, SkScalar mxy, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myt) {
matrix_ = matrix_ * DlMatrix::MakeColumn(
mxx, myx, 0.0f, 0.0f,
mxy, myy, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
mxt, myt, 0.0f, 1.0f
);
}
void transformFullPerspective(
SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) {
matrix_ = matrix_ * DlMatrix::MakeColumn(
mxx, myx, mzx, mwx,
mxy, myy, mzy, mwy,
mxz, myz, mzz, mwz,
mxt, myt, mzt, mwt
);
}
// clang-format on
void setTransform(const DlMatrix& matrix) { matrix_ = matrix; }
void setTransform(const SkMatrix& matrix) { matrix_ = ToDlMatrix(matrix); }
void setTransform(const SkM44& m44) { matrix_ = ToDlMatrix(m44); }
void setIdentity() { matrix_ = DlMatrix(); }
// If the matrix in |other_tracker| is invertible then transform this
// tracker by the inverse of its matrix and return true. Otherwise,
// return false and leave this tracker unmodified.
bool inverseTransform(const DisplayListMatrixClipState& other_tracker);
bool mapRect(DlRect* rect) const { return mapRect(*rect, rect); }
bool mapRect(const DlRect& src, DlRect* mapped) const {
*mapped = src.TransformAndClipBounds(matrix_);
return matrix_.IsAligned2D();
}
bool mapRect(SkRect* rect) const { return mapRect(*rect, rect); }
bool mapRect(const SkRect& src, SkRect* mapped) const {
*mapped = ToSkRect(ToDlRect(src).TransformAndClipBounds(matrix_));
return matrix_.IsAligned2D();
}
void clipRect(const DlRect& rect, ClipOp op, bool is_aa);
void clipRect(const SkRect& rect, ClipOp op, bool is_aa) {
clipRect(ToDlRect(rect), op, is_aa);
}
void clipRRect(const SkRRect& rrect, ClipOp op, bool is_aa);
void clipPath(const SkPath& path, ClipOp op, bool is_aa);
private:
DlRect cull_rect_;
DlMatrix matrix_;
void adjustCullRect(const DlRect& clip, ClipOp op, bool is_aa);
friend class DisplayListMatrixClipTracker;
};
class DisplayListMatrixClipTracker {
private:
using ClipOp = DlCanvas::ClipOp;
using DlRect = impeller::Rect;
using DlMatrix = impeller::Matrix;
public:
DisplayListMatrixClipTracker(const DlRect& cull_rect, const DlMatrix& matrix);
DisplayListMatrixClipTracker(const SkRect& cull_rect, const SkMatrix& matrix);
DisplayListMatrixClipTracker(const SkRect& cull_rect, const SkM44& matrix);
// These methods should almost never be used as they breaks the encapsulation
// of the enclosing clips. However they are needed for practical purposes in
// some rare cases - such as when a saveLayer is collecting rendering
// operations prior to applying a filter on the entire layer bounds and
// some of those operations fall outside the enclosing clip, but their
// filtered content will spread out from where they were rendered on the
// layer into the enclosing clipped area.
// Omitting the |cull_rect| argument, or passing nullptr, will restore the
// cull rect to the initial value it had when the tracker was constructed.
void resetDeviceCullRect(const DlRect* cull_rect = nullptr) {
if (cull_rect) {
current_->resetDeviceCullRect(*cull_rect);
} else {
current_->resetDeviceCullRect(saved_[0].cull_rect_);
}
}
void resetDeviceCullRect(const SkRect* cull_rect = nullptr) {
if (cull_rect) {
current_->resetDeviceCullRect(*cull_rect);
} else {
current_->resetDeviceCullRect(saved_[0].cull_rect_);
}
}
void resetLocalCullRect(const DlRect* cull_rect = nullptr) {
if (cull_rect) {
current_->resetLocalCullRect(*cull_rect);
} else {
current_->resetDeviceCullRect(saved_[0].cull_rect_);
}
}
void resetLocalCullRect(const SkRect* cull_rect = nullptr) {
if (cull_rect) {
current_->resetLocalCullRect(*cull_rect);
} else {
current_->resetDeviceCullRect(saved_[0].cull_rect_);
}
}
static bool is_3x3(const SkM44& m44);
SkRect base_device_cull_rect() const { return saved_[0]->device_cull_rect(); }
SkRect base_device_cull_rect() const { return saved_[0].device_cull_rect(); }
bool using_4x4_matrix() const { return current_->is_4x4(); }
bool using_4x4_matrix() const { return current_->using_4x4_matrix(); }
DlMatrix matrix() const { return current_->matrix(); }
SkM44 matrix_4x4() const { return current_->matrix_4x4(); }
SkMatrix matrix_3x3() const { return current_->matrix_3x3(); }
SkRect local_cull_rect() const { return current_->local_cull_rect(); }
@@ -69,81 +260,64 @@ class DisplayListMatrixClipTracker {
void scale(SkScalar sx, SkScalar sy) { current_->scale(sx, sy); }
void skew(SkScalar skx, SkScalar sky) { current_->skew(skx, sky); }
void rotate(SkScalar degrees) { current_->rotate(degrees); }
void transform(const SkM44& m44);
void transform(const DlMatrix& matrix) { current_->transform(matrix); }
void transform(const SkM44& m44) { current_->transform(m44); }
void transform(const SkMatrix& matrix) { current_->transform(matrix); }
// clang-format off
void transform2DAffine(
SkScalar mxx, SkScalar mxy, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myt);
SkScalar myx, SkScalar myy, SkScalar myt) {
current_->transform2DAffine(mxx, mxy, mxt, myx, myy, myt);
}
void transformFullPerspective(
SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt);
SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) {
current_->transformFullPerspective(
mxx, mxy, mxz, mxt,
myx, myy, myz, myt,
mzx, mzy, mzz, mzt,
mwx, mwy, mwz, mwt
);
}
// clang-format on
void setTransform(const DlMatrix& matrix) { current_->setTransform(matrix); }
void setTransform(const SkMatrix& matrix) { current_->setTransform(matrix); }
void setTransform(const SkM44& m44);
void setTransform(const SkM44& m44) { current_->setTransform(m44); }
void setIdentity() { current_->setIdentity(); }
// If the matrix in |other_tracker| is invertible then transform this
// tracker by the inverse of its matrix and return true. Otherwise,
// return false and leave this tracker unmodified.
bool inverseTransform(const DisplayListMatrixClipTracker& other_tracker);
bool inverseTransform(const DisplayListMatrixClipTracker& other_tracker) {
return current_->inverseTransform(*other_tracker.current_);
}
bool mapRect(DlRect* rect) const { return current_->mapRect(*rect, rect); }
bool mapRect(const DlRect& src, DlRect* mapped) {
return current_->mapRect(src, mapped);
}
bool mapRect(SkRect* rect) const { return current_->mapRect(*rect, rect); }
bool mapRect(const SkRect& src, SkRect* mapped) {
return current_->mapRect(src, mapped);
}
void clipRect(const SkRect& rect, ClipOp op, bool is_aa) {
current_->clipBounds(rect, op, is_aa);
void clipRect(const DlRect& rect, ClipOp op, bool is_aa) {
current_->clipRect(rect, op, is_aa);
}
void clipRect(const SkRect& rect, ClipOp op, bool is_aa) {
current_->clipRect(rect, op, is_aa);
}
void clipRRect(const SkRRect& rrect, ClipOp op, bool is_aa) {
current_->clipRRect(rrect, op, is_aa);
}
void clipPath(const SkPath& path, ClipOp op, bool is_aa) {
current_->clipPath(path, op, is_aa);
}
void clipRRect(const SkRRect& rrect, ClipOp op, bool is_aa);
void clipPath(const SkPath& path, ClipOp op, bool is_aa);
private:
class Data {
public:
virtual ~Data() = default;
virtual bool is_4x4() const = 0;
virtual SkMatrix matrix_3x3() const = 0;
virtual SkM44 matrix_4x4() const = 0;
SkRect device_cull_rect() const { return cull_rect_; }
virtual SkRect local_cull_rect() const = 0;
virtual bool content_culled(const SkRect& content_bounds) const;
bool is_cull_rect_empty() const { return cull_rect_.isEmpty(); }
virtual void translate(SkScalar tx, SkScalar ty) = 0;
virtual void scale(SkScalar sx, SkScalar sy) = 0;
virtual void skew(SkScalar skx, SkScalar sky) = 0;
virtual void rotate(SkScalar degrees) = 0;
virtual void transform(const SkMatrix& matrix) = 0;
virtual void transform(const SkM44& m44) = 0;
virtual void setTransform(const SkMatrix& matrix) = 0;
virtual void setTransform(const SkM44& m44) = 0;
virtual void setIdentity() = 0;
virtual bool mapRect(const SkRect& rect, SkRect* mapped) const = 0;
virtual bool canBeInverted() const = 0;
virtual void clipBounds(const SkRect& clip, ClipOp op, bool is_aa);
virtual void resetBounds(const SkRect& cull_rect);
protected:
explicit Data(const SkRect& rect) : cull_rect_(rect) {}
virtual bool has_perspective() const = 0;
SkRect cull_rect_;
};
friend class Data3x3;
friend class Data4x4;
SkRect original_cull_rect_;
Data* current_;
std::vector<std::unique_ptr<Data>> saved_;
DisplayListMatrixClipState* current_;
std::vector<DisplayListMatrixClipState> saved_;
};
} // namespace flutter

View File

@@ -167,18 +167,19 @@ TEST_F(TransformLayerTest, SimpleM44) {
TEST_F(TransformLayerTest, ComplexM44) {
SkPath child_path;
child_path.addRect(5.0f, 6.0f, 20.5f, 21.5f);
SkMatrix initial_transform = SkMatrix::Translate(-0.5f, -0.5f);
SkRect local_cull_rect = SkRect::MakeXYWH(2.0f, 2.0f, 14.0f, 14.0f);
SkMatrix initial_transform = SkMatrix::Translate(-2.0f, -2.0f);
SkRect local_cull_rect = SkRect::MakeXYWH(4.0f, 4.0f, 16.0f, 16.0f);
SkRect device_cull_rect = initial_transform.mapRect(local_cull_rect);
SkM44 layer_transform44 = SkM44();
layer_transform44.preTranslate(2.5f, 2.5f);
layer_transform44.preTranslate(1.0f, 1.0f);
// 20 degrees around the X axis
layer_transform44.preConcat(SkM44::Rotate({1.0f, 0.0f, 0.0f}, M_PI / 9.0f));
// 10 degrees around the Y axis
layer_transform44.preConcat(SkM44::Rotate({0.0f, 1.0f, 0.0f}, M_PI / 18.0f));
SkM44 inverse_layer_transform44;
EXPECT_TRUE(layer_transform44.invert(&inverse_layer_transform44));
SkMatrix layer_transform = layer_transform44.asM33();
SkMatrix inverse_layer_transform;
EXPECT_TRUE(layer_transform.invert(&inverse_layer_transform));
SkMatrix inverse_layer_transform = inverse_layer_transform44.asM33();
auto mock_layer = std::make_shared<MockLayer>(child_path, DlPaint());
auto layer = std::make_shared<TransformLayer>(layer_transform44);
@@ -195,8 +196,19 @@ TEST_F(TransformLayerTest, ComplexM44) {
EXPECT_TRUE(layer->needs_painting(paint_context()));
EXPECT_EQ(mock_layer->parent_matrix(),
SkMatrix::Concat(initial_transform, layer_transform));
EXPECT_EQ(mock_layer->parent_cull_rect(),
inverse_layer_transform.mapRect(local_cull_rect));
// Even having switched to binary-ieee friendly numbers for the
// initial conditions, these numbers which are based on a matrix
// concatenation and inversion still aren't exact, so we are using
// fuzzy float comparisons to check them.
SkRect parent_cull_rect = mock_layer->parent_cull_rect();
SkRect expect_parent_cull_rect =
inverse_layer_transform.mapRect(local_cull_rect);
EXPECT_FLOAT_EQ(parent_cull_rect.fLeft, expect_parent_cull_rect.fLeft);
EXPECT_FLOAT_EQ(parent_cull_rect.fTop, expect_parent_cull_rect.fTop);
EXPECT_FLOAT_EQ(parent_cull_rect.fRight, expect_parent_cull_rect.fRight);
EXPECT_FLOAT_EQ(parent_cull_rect.fBottom, expect_parent_cull_rect.fBottom);
EXPECT_EQ(mock_layer->parent_mutators(),
std::vector({Mutator(layer_transform)}));

View File

@@ -2514,7 +2514,11 @@ TEST_P(EntityTest, CoverageForStrokePathWithNegativeValuesInTransform) {
auto transform = Matrix::MakeTranslation({300, 300}) *
Matrix::MakeRotationZ(Radians(kPiOver2));
EXPECT_LT(transform.e[0][0], 0.f);
// Note that e[0][0] used to be tested here, but it was -epsilon solely
// due to floating point inaccuracy in the transcendental trig functions.
// e[1][0] is the intended negative value that we care about (-1.0) as it
// comes from the rotation of pi/2.
EXPECT_LT(transform.e[1][0], 0.0f);
auto coverage = geometry->GetCoverage(transform);
ASSERT_RECT_NEAR(coverage.value(), Rect::MakeXYWH(102.5, 342.5, 85, 155));
}

View File

@@ -462,20 +462,6 @@ TEST(GeometryTest, MatrixGetDirectionScale) {
}
}
TEST(GeometryTest, MatrixIsAligned) {
{
auto m = Matrix::MakeTranslation({1, 2, 3});
bool result = m.IsAligned();
ASSERT_TRUE(result);
}
{
auto m = Matrix::MakeRotationZ(Degrees{123});
bool result = m.IsAligned();
ASSERT_FALSE(result);
}
}
TEST(GeometryTest, MatrixTranslationScaleOnly) {
{
auto m = Matrix();

View File

@@ -148,12 +148,13 @@ struct Matrix {
// clang-format on
}
static Matrix MakeRotation(Scalar radians, const Vector4& r) {
static Matrix MakeRotation(Radians radians, const Vector4& r) {
const Vector4 v = r.Normalize();
const Scalar cosine = cos(radians);
const Vector2 cos_sin = CosSin(radians);
const Scalar cosine = cos_sin.x;
const Scalar cosp = 1.0f - cosine;
const Scalar sine = sin(radians);
const Scalar sine = cos_sin.y;
// clang-format off
return Matrix(
@@ -180,8 +181,10 @@ struct Matrix {
}
static Matrix MakeRotationX(Radians r) {
const Scalar cosine = cos(r.radians);
const Scalar sine = sin(r.radians);
const Vector2 cos_sin = CosSin(r);
const Scalar cosine = cos_sin.x;
const Scalar sine = cos_sin.y;
// clang-format off
return Matrix(
1.0f, 0.0f, 0.0f, 0.0f,
@@ -193,8 +196,9 @@ struct Matrix {
}
static Matrix MakeRotationY(Radians r) {
const Scalar cosine = cos(r.radians);
const Scalar sine = sin(r.radians);
const Vector2 cos_sin = CosSin(r);
const Scalar cosine = cos_sin.x;
const Scalar sine = cos_sin.y;
// clang-format off
return Matrix(
@@ -207,8 +211,9 @@ struct Matrix {
}
static Matrix MakeRotationZ(Radians r) {
const Scalar cosine = cos(r.radians);
const Scalar sine = sin(r.radians);
const Vector2 cos_sin = CosSin(r);
const Scalar cosine = cos_sin.x;
const Scalar sine = cos_sin.y;
// clang-format off
return Matrix (
@@ -318,11 +323,33 @@ struct Matrix {
m[9] == 0 && m[10] == 1 && m[11] == 0 && m[14] == 0 && m[15] == 1);
}
constexpr bool HasPerspective2D() const {
return m[3] != 0 || m[7] != 0 || m[15] != 1;
}
constexpr bool HasPerspective() const {
return m[3] != 0 || m[7] != 0 || m[11] != 0 || m[15] != 1;
}
constexpr bool IsAligned2D(Scalar tolerance = 0) const {
if (HasPerspective2D()) {
return false;
}
if (ScalarNearlyZero(m[1], tolerance) &&
ScalarNearlyZero(m[4], tolerance)) {
return true;
}
if (ScalarNearlyZero(m[0], tolerance) &&
ScalarNearlyZero(m[5], tolerance)) {
return true;
}
return false;
}
constexpr bool IsAligned(Scalar tolerance = 0) const {
if (HasPerspective()) {
return false;
}
int v[] = {!ScalarNearlyZero(m[0], tolerance), //
!ScalarNearlyZero(m[1], tolerance), //
!ScalarNearlyZero(m[2], tolerance), //
@@ -433,6 +460,12 @@ struct Matrix {
return result * w;
}
constexpr Vector3 TransformHomogenous(const Point& v) const {
return Vector3(v.x * m[0] + v.y * m[4] + m[12],
v.x * m[1] + v.y * m[5] + m[13],
v.x * m[3] + v.y * m[7] + m[15]);
}
constexpr Vector4 TransformDirection(const Vector4& v) const {
return Vector4(v.x * m[0] + v.y * m[4] + v.z * m[8],
v.x * m[1] + v.y * m[5] + v.z * m[9],
@@ -510,6 +543,43 @@ struct Matrix {
};
// clang-format on
}
private:
static constexpr Vector2 CosSin(Radians radians) {
// The precision of a float around 1.0 is much lower than it is
// around 0.0, so we end up with cases on quadrant rotations where
// we get a +/-1.0 for one of the values and a non-zero value for
// the other. This happens around quadrant rotations which makes it
// especially common and results in unclean quadrant rotation
// matrices which do not return true from |IsAligned[2D]| even
// though that is exactly where you need them to exhibit that property.
// It also injects small floating point mantissa errors into the
// matrices whenever you concatenate them with a quadrant rotation.
//
// This issue is also exacerbated by the fact that, in radians, the
// angles for quadrant rotations are irrational numbers. The measuring
// error for representing 90 degree multiples is small enough that
// either sin or cos will return a value near +/-1.0, but not small
// enough that the other value will be a clean 0.0.
//
// Some geometry packages simply discard very small numbers from
// sin/cos, but the following approach specifically targets just the
// area around a quadrant rotation (where either the sin or cos are
// measuring as +/-1.0) for symmetry of precision.
Scalar sin = std::sin(radians.radians);
if (std::abs(sin) == 1.0f) {
// 90 or 270 degrees (mod 360)
return {0.0f, sin};
} else {
Scalar cos = std::cos(radians.radians);
if (std::abs(cos) == 1.0f) {
// 0 or 180 degrees (mod 360)
return {cos, 0.0f};
}
return {cos, sin};
}
}
};
static_assert(sizeof(struct Matrix) == sizeof(Scalar) * 16,

View File

@@ -24,5 +24,134 @@ TEST(MatrixTest, Multiply) {
11.0, 21.0, 0.0, 1.0)));
}
TEST(MatrixTest, HasPerspective2D) {
EXPECT_FALSE(Matrix().HasPerspective2D());
auto test = [](int index, bool expect) {
Matrix matrix;
EXPECT_FALSE(matrix.HasPerspective2D());
matrix.m[index] = 0.5f;
EXPECT_EQ(matrix.HasPerspective2D(), expect) << "index: " << index;
};
// clang-format off
test( 0, false); test( 1, false); test( 2, false); test( 3, true);
test( 4, false); test( 5, false); test( 6, false); test( 7, true);
test( 8, false); test( 9, false); test(10, false); test(11, false);
test(12, false); test(13, false); test(14, false); test(15, true);
// clang-format on
}
TEST(MatrixTest, HasPerspective) {
EXPECT_FALSE(Matrix().HasPerspective());
auto test = [](int index, bool expect) {
Matrix matrix;
EXPECT_FALSE(matrix.HasPerspective());
matrix.m[index] = 0.5f;
EXPECT_EQ(matrix.HasPerspective(), expect) << "index: " << index;
};
// clang-format off
test( 0, false); test( 1, false); test( 2, false); test( 3, true);
test( 4, false); test( 5, false); test( 6, false); test( 7, true);
test( 8, false); test( 9, false); test(10, false); test(11, true);
test(12, false); test(13, false); test(14, false); test(15, true);
// clang-format on
}
TEST(MatrixTest, IsAligned2D) {
EXPECT_TRUE(Matrix().IsAligned2D());
EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned2D());
auto test = [](int index, bool expect) {
Matrix matrix;
EXPECT_TRUE(matrix.IsAligned2D());
matrix.m[index] = 0.5f;
EXPECT_EQ(matrix.IsAligned2D(), expect) << "index: " << index;
};
// clang-format off
test( 0, true); test( 1, false); test( 2, true); test( 3, false);
test( 4, false); test( 5, true); test( 6, true); test( 7, false);
test( 8, true); test( 9, true); test(10, true); test(11, true);
test(12, true); test(13, true); test(14, true); test(15, false);
// clang-format on
// True for quadrant rotations from -250 to +250 full circles
for (int i = -1000; i < 1000; i++) {
Degrees d = Degrees(i * 90);
Matrix matrix = Matrix::MakeRotationZ(Degrees(d));
EXPECT_TRUE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
}
// False for half degree rotations from -999.5 to +1000.5 degrees
for (int i = -1000; i < 1000; i++) {
Degrees d = Degrees(i + 0.5f);
Matrix matrix = Matrix::MakeRotationZ(Degrees(d));
EXPECT_FALSE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
}
}
TEST(MatrixTest, IsAligned) {
EXPECT_TRUE(Matrix().IsAligned());
EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned());
// Begin Legacy tests transferred over from geometry_unittests.cc
{
auto m = Matrix::MakeTranslation({1, 2, 3});
bool result = m.IsAligned();
ASSERT_TRUE(result);
}
{
auto m = Matrix::MakeRotationZ(Degrees{123});
bool result = m.IsAligned();
ASSERT_FALSE(result);
}
// End Legacy tests transferred over from geometry_unittests.cc
auto test = [](int index, bool expect) {
Matrix matrix;
EXPECT_TRUE(matrix.IsAligned());
matrix.m[index] = 0.5f;
EXPECT_EQ(matrix.IsAligned(), expect) << "index: " << index;
};
// clang-format off
test( 0, true); test( 1, false); test( 2, false); test( 3, false);
test( 4, false); test( 5, true); test( 6, false); test( 7, false);
test( 8, false); test( 9, false); test(10, true); test(11, false);
test(12, true); test(13, true); test(14, true); test(15, false);
// clang-format on
// True for quadrant rotations from -250 to +250 full circles
for (int i = -1000; i < 1000; i++) {
Degrees d = Degrees(i * 90);
Matrix matrix = Matrix::MakeRotationZ(Degrees(d));
EXPECT_TRUE(matrix.IsAligned()) << "degrees: " << d.degrees;
}
// False for half degree rotations from -999.5 to +1000.5 degrees
for (int i = -1000; i < 1000; i++) {
Degrees d = Degrees(i + 0.5f);
Matrix matrix = Matrix::MakeRotationZ(Degrees(d));
EXPECT_FALSE(matrix.IsAligned()) << "degrees: " << d.degrees;
}
}
TEST(MatrixTest, TransformHomogenous) {
Matrix matrix = Matrix::MakeColumn(
// clang-format off
2.0f, 3.0f, 5.0f, 7.0f,
11.0f, 13.0f, 17.0f, 19.0f,
23.0f, 29.0f, 31.0f, 37.0f,
41.0f, 43.0f, 47.0f, 53.0f
// clang-format on
);
EXPECT_EQ(matrix.TransformHomogenous({1.0f, -1.0f}),
Vector3(32.0f, 33.0f, 41.0f));
}
} // namespace testing
} // namespace impeller

View File

@@ -400,6 +400,41 @@ struct TRect {
return points;
}
/// @brief Creates a new bounding box that contains this transformed
/// rectangle, clipped against the near clipping plane if
/// necessary.
[[nodiscard]] constexpr TRect TransformAndClipBounds(
const Matrix& transform) const {
if (!transform.HasPerspective2D()) {
return TransformBounds(transform);
}
if (IsEmpty()) {
return {};
}
auto ul = transform.TransformHomogenous({left_, top_});
auto ur = transform.TransformHomogenous({right_, top_});
auto ll = transform.TransformHomogenous({left_, bottom_});
auto lr = transform.TransformHomogenous({right_, bottom_});
// It can probably be proven that we only ever have 5 points at most
// which happens when only 1 corner is clipped and we get 2 points
// in return for it as we interpolate against its neighbors.
Point points[8];
int index = 0;
// Process (clip and interpolate) each point against its 2 neighbors:
// left, pt, right
index = ClipAndInsert(points, index, ll, ul, ur);
index = ClipAndInsert(points, index, ul, ur, lr);
index = ClipAndInsert(points, index, ur, lr, ll);
index = ClipAndInsert(points, index, lr, ll, ul);
auto bounds = TRect::MakePointBounds(points, points + index);
return bounds.value_or(TRect{});
}
/// @brief Creates a new bounding box that contains this transformed
/// rectangle.
[[nodiscard]] constexpr TRect TransformBounds(const Matrix& transform) const {
@@ -612,6 +647,14 @@ struct TRect {
saturated::Cast<U, Type>(ceil(r.GetBottom())));
}
ONLY_ON_FLOAT_M([[nodiscard]] constexpr static, TRect)
Round(const TRect<U>& r) {
return TRect::MakeLTRB(saturated::Cast<U, Type>(round(r.GetLeft())),
saturated::Cast<U, Type>(round(r.GetTop())),
saturated::Cast<U, Type>(round(r.GetRight())),
saturated::Cast<U, Type>(round(r.GetBottom())));
}
[[nodiscard]] constexpr static std::optional<TRect> Union(
const TRect& a,
const std::optional<TRect> b) {
@@ -656,6 +699,48 @@ struct TRect {
Type top_;
Type right_;
Type bottom_;
static constexpr Scalar kMinimumHomogenous = 1.0f / (1 << 14);
// Clip p against the near clipping plane (W = kMinimumHomogenous)
// and interpolate a crossing point against the nearby neighbors
// left and right if p is clipped and either of them is not.
// This method can produce 0, 1, or 2 points per call depending on
// how many of the points are clipped.
// 0 - all points are clipped
// 1 - p is unclipped OR
// p is clipped and exactly one of the neighbors is not
// 2 - p is clipped and both neighbors are not
static constexpr int ClipAndInsert(Point clipped[],
int index,
const Vector3& left,
const Vector3& p,
const Vector3& right) {
if (p.z >= kMinimumHomogenous) {
clipped[index++] = {p.x / p.z, p.y / p.z};
} else {
index = InterpolateAndInsert(clipped, index, p, left);
index = InterpolateAndInsert(clipped, index, p, right);
}
return index;
}
// Interpolate (a clipped) point p against one of its neighbors
// and insert the point into the array where the line between them
// veers from clipped space to unclipped, if such a point exists.
static constexpr int InterpolateAndInsert(Point clipped[],
int index,
const Vector3& p,
const Vector3& neighbor) {
if (neighbor.z >= kMinimumHomogenous) {
auto t = (kMinimumHomogenous - p.z) / (neighbor.z - p.z);
clipped[index++] = {
(t * p.x + (1.0f - t) * neighbor.x) / kMinimumHomogenous,
(t * p.y + (1.0f - t) * neighbor.y) / kMinimumHomogenous,
};
}
return index;
}
};
using Rect = TRect<Scalar>;

View File

@@ -2840,27 +2840,206 @@ TEST(RectTest, RectProject) {
TEST(RectTest, RectRoundOut) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto r = Rect::MakeLTRB(-100, -200, 300, 400);
EXPECT_EQ(Rect::RoundOut(r), r);
}
{
auto r = Rect::MakeLTRB(-100.1, -100.1, 100.1, 100.1);
EXPECT_EQ(Rect::RoundOut(r), Rect::MakeLTRB(-101, -101, 101, 101));
auto r = Rect::MakeLTRB(-100.1, -200.1, 300.1, 400.1);
EXPECT_EQ(Rect::RoundOut(r), Rect::MakeLTRB(-101, -201, 301, 401));
}
}
TEST(RectTest, IRectRoundOut) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto ir = IRect::MakeLTRB(-100, -100, 100, 100);
auto r = Rect::MakeLTRB(-100, -200, 300, 400);
auto ir = IRect::MakeLTRB(-100, -200, 300, 400);
EXPECT_EQ(IRect::RoundOut(r), ir);
}
{
auto r = Rect::MakeLTRB(-100.1, -100.1, 100.1, 100.1);
auto ir = IRect::MakeLTRB(-101, -101, 101, 101);
auto r = Rect::MakeLTRB(-100.1, -200.1, 300.1, 400.1);
auto ir = IRect::MakeLTRB(-101, -201, 301, 401);
EXPECT_EQ(IRect::RoundOut(r), ir);
}
}
TEST(RectTest, RectRound) {
{
auto r = Rect::MakeLTRB(-100, -200, 300, 400);
EXPECT_EQ(Rect::Round(r), r);
}
{
auto r = Rect::MakeLTRB(-100.4, -200.4, 300.4, 400.4);
EXPECT_EQ(Rect::Round(r), Rect::MakeLTRB(-100, -200, 300, 400));
}
{
auto r = Rect::MakeLTRB(-100.5, -200.5, 300.5, 400.5);
EXPECT_EQ(Rect::Round(r), Rect::MakeLTRB(-101, -201, 301, 401));
}
}
TEST(RectTest, IRectRound) {
{
auto r = Rect::MakeLTRB(-100, -200, 300, 400);
auto ir = IRect::MakeLTRB(-100, -200, 300, 400);
EXPECT_EQ(IRect::Round(r), ir);
}
{
auto r = Rect::MakeLTRB(-100.4, -200.4, 300.4, 400.4);
auto ir = IRect::MakeLTRB(-100, -200, 300, 400);
EXPECT_EQ(IRect::Round(r), ir);
}
{
auto r = Rect::MakeLTRB(-100.5, -200.5, 300.5, 400.5);
auto ir = IRect::MakeLTRB(-101, -201, 301, 401);
EXPECT_EQ(IRect::Round(r), ir);
}
}
// EXPECT_RECT_NEAR will allow a fixed difference between the values that
// assumes a compatible range of values being tested. Some of the values
// below are well outside that range and so if the values are a single
// "bit" off, then they will differ by far more than the fixed allowable
// difference. The EXPECT_FLOAT_EQ macro will compare the values and
// fail only if their difference in mantissa is in the last N bits,
// which is a range agnostic way to compare floats with allowances for
// bit errors in computations.
#define EXPECT_RECT_EQ(a, b) \
do { \
EXPECT_FLOAT_EQ(a.GetLeft(), b.GetLeft()); \
EXPECT_FLOAT_EQ(a.GetTop(), b.GetTop()); \
EXPECT_FLOAT_EQ(a.GetRight(), b.GetRight()); \
EXPECT_FLOAT_EQ(a.GetBottom(), b.GetBottom()); \
} while (0)
TEST(RectTest, TransformAndClipBounds) {
{
// This matrix should clip no corners.
auto matrix = impeller::Matrix::MakeColumn(
// clang-format off
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 4.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 8.0f
// clang-format on
);
Rect src = Rect::MakeLTRB(100.0f, 100.0f, 200.0f, 200.0f);
// None of these should have a W<0
EXPECT_EQ(matrix.TransformHomogenous(src.GetLeftTop()),
Vector3(200.0f, 400.0f, 8.0f));
EXPECT_EQ(matrix.TransformHomogenous(src.GetRightTop()),
Vector3(400.0f, 400.0f, 8.0f));
EXPECT_EQ(matrix.TransformHomogenous(src.GetLeftBottom()),
Vector3(200.0f, 800.0f, 8.0f));
EXPECT_EQ(matrix.TransformHomogenous(src.GetRightBottom()),
Vector3(400.0f, 800.0f, 8.0f));
Rect expect = Rect::MakeLTRB(25.0f, 50.0f, 50.0f, 100.0f);
EXPECT_FALSE(src.TransformAndClipBounds(matrix).IsEmpty());
EXPECT_EQ(src.TransformAndClipBounds(matrix), expect);
}
{
// This matrix should clip one corner.
auto matrix = impeller::Matrix::MakeColumn(
// clang-format off
2.0f, 0.0f, 0.0f, -0.01f,
0.0f, 2.0f, 0.0f, -0.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
);
Rect src = Rect::MakeLTRB(100.0f, 100.0f, 200.0f, 200.0f);
// Exactly one of these should have a W<0
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftTop()),
Vector3(200.0f, 200.0f, 1.4f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightTop()),
Vector3(400.0f, 200.0f, 0.4f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftBottom()),
Vector3(200.0f, 400.0f, 0.8f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightBottom()),
Vector3(400.0f, 400.0f, -0.2f));
Rect expect = Rect::MakeLTRB(142.85715f, 142.85715f, 6553600.f, 6553600.f);
EXPECT_FALSE(src.TransformAndClipBounds(matrix).IsEmpty());
EXPECT_RECT_EQ(src.TransformAndClipBounds(matrix), expect);
}
{
// This matrix should clip two corners.
auto matrix = impeller::Matrix::MakeColumn(
// clang-format off
2.0f, 0.0f, 0.0f, -.015f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
);
Rect src = Rect::MakeLTRB(100.0f, 100.0f, 200.0f, 200.0f);
// Exactly two of these should have a W<0
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftTop()),
Vector3(200.0f, 200.0f, 0.9f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightTop()),
Vector3(400.0f, 200.0f, -0.6f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftBottom()),
Vector3(200.0f, 400.0f, 0.3f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightBottom()),
Vector3(400.0f, 400.0f, -1.2f));
Rect expect = Rect::MakeLTRB(222.2222f, 222.2222f, 5898373.f, 6553600.f);
EXPECT_FALSE(src.TransformAndClipBounds(matrix).IsEmpty());
EXPECT_RECT_EQ(src.TransformAndClipBounds(matrix), expect);
}
{
// This matrix should clip three corners.
auto matrix = impeller::Matrix::MakeColumn(
// clang-format off
2.0f, 0.0f, 0.0f, -.02f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
);
Rect src = Rect::MakeLTRB(100.0f, 100.0f, 200.0f, 200.0f);
// Exactly three of these should have a W<0
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftTop()),
Vector3(200.0f, 200.0f, 0.4f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightTop()),
Vector3(400.0f, 200.0f, -1.6f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftBottom()),
Vector3(200.0f, 400.0f, -0.2f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightBottom()),
Vector3(400.0f, 400.0f, -2.2f));
Rect expect = Rect::MakeLTRB(499.99988f, 499.99988f, 5898340.f, 4369400.f);
EXPECT_FALSE(src.TransformAndClipBounds(matrix).IsEmpty());
EXPECT_RECT_EQ(src.TransformAndClipBounds(matrix), expect);
}
{
// This matrix should clip all four corners.
auto matrix = impeller::Matrix::MakeColumn(
// clang-format off
2.0f, 0.0f, 0.0f, -.025f,
0.0f, 2.0f, 0.0f, -.006f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 3.0f
// clang-format on
);
Rect src = Rect::MakeLTRB(100.0f, 100.0f, 200.0f, 200.0f);
// All of these should have a W<0
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftTop()),
Vector3(200.0f, 200.0f, -0.1f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightTop()),
Vector3(400.0f, 200.0f, -2.6f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetLeftBottom()),
Vector3(200.0f, 400.0f, -0.7f));
EXPECT_VECTOR3_NEAR(matrix.TransformHomogenous(src.GetRightBottom()),
Vector3(400.0f, 400.0f, -3.2f));
EXPECT_TRUE(src.TransformAndClipBounds(matrix).IsEmpty());
}
}
} // namespace testing
} // namespace impeller

View File

@@ -164,7 +164,7 @@ TEST_P(SceneTest, TwoTriangles) {
ImGui::End();
Node& node = *scene.GetRoot().GetChildren()[0];
node.SetLocalTransform(node.GetLocalTransform() *
Matrix::MakeRotation(0.02, {0, 1, 0, 0}));
Matrix::MakeRotation(Radians(0.02), {0, 1, 0, 0}));
static ImVec2 mouse_pos_prev = ImGui::GetMousePos();
ImVec2 mouse_pos = ImGui::GetMousePos();
@@ -269,7 +269,7 @@ TEST_P(SceneTest, Dash) {
ImGui::End();
Node& node = *scene.GetRoot().GetChildren()[0];
node.SetLocalTransform(node.GetLocalTransform() *
Matrix::MakeRotation(0.02, {0, 1, 0, 0}));
Matrix::MakeRotation(Radians(0.02), {0, 1, 0, 0}));
static ImVec2 mouse_pos_prev = ImGui::GetMousePos();
ImVec2 mouse_pos = ImGui::GetMousePos();