[Impeller] Implement matrix filter (flutter/engine#36036)

This commit is contained in:
Brandon DeRosier
2022-09-09 13:13:44 -07:00
committed by GitHub
parent 4b4e952701
commit 6b1153f520
11 changed files with 191 additions and 9 deletions

View File

@@ -586,6 +586,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_in
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h
FILE: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/filters/matrix_filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/matrix_filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/filters/morphology_filter_contents.cc
FILE: ../../../flutter/impeller/entity/contents/filters/morphology_filter_contents.h
FILE: ../../../flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc

View File

@@ -577,7 +577,15 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
};
break;
}
case flutter::DlImageFilterType::kMatrix:
case flutter::DlImageFilterType::kMatrix: {
auto matrix_filter = filter->asMatrix();
FML_DCHECK(matrix_filter);
auto matrix = ToMatrix(matrix_filter->matrix());
return [matrix](FilterInput::Ref input, const Matrix& effect_transform) {
return FilterContents::MakeMatrixFilter(input, matrix);
};
break;
}
case flutter::DlImageFilterType::kLocalMatrixFilter:
case flutter::DlImageFilterType::kComposeFilter:
case flutter::DlImageFilterType::kColorFilter:

View File

@@ -109,7 +109,7 @@ TEST_P(DisplayListTest, CanDrawArc) {
static float sweep_angle = 270;
static bool use_center = true;
ImGui::Begin("Controls");
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::SliderFloat("Start angle", &start_angle, -360, 360);
ImGui::SliderFloat("Sweep angle", &sweep_angle, -360, 360);
ImGui::Checkbox("Use center", &use_center);
@@ -583,5 +583,85 @@ TEST_P(DisplayListTest, CanDrawZeroWidthLine) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_P(DisplayListTest, CanDrawWithMatrixFilter) {
auto boston = CreateTextureForFixture("boston.jpg");
bool first_frame = true;
auto callback = [&]() {
if (first_frame) {
first_frame = false;
ImGui::SetNextWindowPos({10, 10});
}
static float ctm_translation[2] = {200, 200};
static float ctm_scale[2] = {0.65, 0.65};
static float ctm_skew[2] = {0, 0};
static bool enable = true;
static float translation[2] = {100, 100};
static float scale[2] = {0.8, 0.8};
static float skew[2] = {0.2, 0.2};
static bool enable_savelayer = true;
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
{
ImGui::TextWrapped("Current Transform");
ImGui::SliderFloat2("CTM Translation", ctm_translation, 0, 1000);
ImGui::SliderFloat2("CTM Scale", ctm_scale, 0, 3);
ImGui::SliderFloat2("CTM Skew", ctm_skew, -3, 3);
ImGui::TextWrapped(
"The MatrixFilter applies a matrix in world space as opposed to "
"local filter space.");
ImGui::Checkbox("Enable", &enable);
ImGui::SliderFloat2("Filter Translation", translation, 0, 1000);
ImGui::SliderFloat2("Filter Scale", scale, 0, 3);
ImGui::SliderFloat2("Filter Skew", skew, -3, 3);
ImGui::TextWrapped(
"Rendering the filtered image within a layer can expose bounds "
"issues. If the rendered image gets cut off when this setting is "
"enabled, there's a coverage bug in the filter.");
ImGui::Checkbox("Render in layer", &enable_savelayer);
}
ImGui::End();
flutter::DisplayListBuilder builder;
SkPaint paint;
builder.saveLayer(nullptr, nullptr);
{
auto content_scale = GetContentScale();
builder.scale(content_scale.x, content_scale.y);
// Set the current transform
auto ctm_matrix =
SkMatrix::MakeAll(ctm_scale[0], ctm_skew[0], ctm_translation[0], //
ctm_skew[1], ctm_scale[1], ctm_translation[1], //
0, 0, 1);
builder.transform(ctm_matrix);
// Set the matrix filter
auto filter_matrix =
SkMatrix::MakeAll(scale[0], skew[0], translation[0], //
skew[1], scale[1], translation[1], //
0, 0, 1);
auto filter = flutter::DlMatrixImageFilter(
filter_matrix, flutter::DlImageSampling::kLinear);
if (enable) {
builder.setImageFilter(&filter);
}
builder.drawImage(DlImageImpeller::Make(boston), {},
flutter::DlImageSampling::kLinear, true);
builder.restore();
}
return builder.Build();
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
} // namespace testing
} // namespace impeller

View File

@@ -93,6 +93,8 @@ impeller_component("entity") {
"contents/filters/inputs/texture_filter_input.h",
"contents/filters/linear_to_srgb_filter_contents.cc",
"contents/filters/linear_to_srgb_filter_contents.h",
"contents/filters/matrix_filter_contents.cc",
"contents/filters/matrix_filter_contents.h",
"contents/filters/morphology_filter_contents.cc",
"contents/filters/morphology_filter_contents.h",
"contents/filters/srgb_to_linear_filter_contents.cc",

View File

@@ -20,6 +20,7 @@
#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
#include "impeller/entity/contents/filters/linear_to_srgb_filter_contents.h"
#include "impeller/entity/contents/filters/matrix_filter_contents.h"
#include "impeller/entity/contents/filters/morphology_filter_contents.h"
#include "impeller/entity/contents/filters/srgb_to_linear_filter_contents.h"
#include "impeller/entity/contents/texture_contents.h"
@@ -155,10 +156,10 @@ std::shared_ptr<FilterContents> FilterContents::MakeMorphology(
std::shared_ptr<FilterContents> FilterContents::MakeColorMatrix(
FilterInput::Ref input,
const ColorMatrix& matrix) {
const ColorMatrix& color_matrix) {
auto filter = std::make_shared<ColorMatrixFilterContents>();
filter->SetInputs({input});
filter->SetMatrix(matrix);
filter->SetMatrix(color_matrix);
return filter;
}
@@ -176,6 +177,15 @@ std::shared_ptr<FilterContents> FilterContents::MakeSrgbToLinearFilter(
return filter;
}
std::shared_ptr<FilterContents> FilterContents::MakeMatrixFilter(
FilterInput::Ref input,
const Matrix& matrix) {
auto filter = std::make_shared<MatrixFilterContents>();
filter->SetInputs({input});
filter->SetMatrix(matrix);
return filter;
}
FilterContents::FilterContents() = default;
FilterContents::~FilterContents() = default;
@@ -284,12 +294,12 @@ std::optional<Snapshot> FilterContents::RenderToSnapshot(
effect_transform_, coverage.value());
}
Matrix FilterContents::GetLocalTransform() const {
Matrix FilterContents::GetLocalTransform(const Matrix& parent_transform) const {
return Matrix();
}
Matrix FilterContents::GetTransform(const Matrix& parent_transform) const {
return parent_transform * GetLocalTransform();
return parent_transform * GetLocalTransform(parent_transform);
}
} // namespace impeller

View File

@@ -82,7 +82,7 @@ class FilterContents : public Contents {
static std::shared_ptr<FilterContents> MakeColorMatrix(
FilterInput::Ref input,
const ColorMatrix& matrix);
const ColorMatrix& color_matrix);
static std::shared_ptr<FilterContents> MakeLinearToSrgbFilter(
FilterInput::Ref input);
@@ -90,6 +90,10 @@ class FilterContents : public Contents {
static std::shared_ptr<FilterContents> MakeSrgbToLinearFilter(
FilterInput::Ref input);
static std::shared_ptr<FilterContents> MakeMatrixFilter(
FilterInput::Ref input,
const Matrix& matrix);
FilterContents();
~FilterContents() override;
@@ -120,7 +124,7 @@ class FilterContents : public Contents {
std::optional<Snapshot> RenderToSnapshot(const ContentContext& renderer,
const Entity& entity) const override;
virtual Matrix GetLocalTransform() const;
virtual Matrix GetLocalTransform(const Matrix& parent_transform) const;
Matrix GetTransform(const Matrix& parent_transform) const;

View File

@@ -34,7 +34,7 @@ std::optional<Rect> FilterContentsFilterInput::GetCoverage(
Matrix FilterContentsFilterInput::GetLocalTransform(
const Entity& entity) const {
return filter_->GetLocalTransform();
return filter_->GetLocalTransform(entity.GetTransformation());
}
Matrix FilterContentsFilterInput::GetTransform(const Entity& entity) const {

View File

@@ -53,6 +53,12 @@ Matrix FilterInput::GetLocalTransform(const Entity& entity) const {
return Matrix();
}
std::optional<Rect> FilterInput::GetLocalCoverage(const Entity& entity) const {
Entity local_entity = entity;
local_entity.SetTransformation(GetLocalTransform(entity));
return GetCoverage(local_entity);
}
Matrix FilterInput::GetTransform(const Entity& entity) const {
return entity.GetTransformation() * GetLocalTransform(entity);
}

View File

@@ -48,6 +48,8 @@ class FilterInput {
virtual std::optional<Snapshot> GetSnapshot(const ContentContext& renderer,
const Entity& entity) const = 0;
std::optional<Rect> GetLocalCoverage(const Entity& entity) const;
virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;
/// @brief Get the local transform of this filter input. This transform is

View File

@@ -0,0 +1,31 @@
// 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/entity/contents/filters/matrix_filter_contents.h"
namespace impeller {
MatrixFilterContents::MatrixFilterContents() = default;
MatrixFilterContents::~MatrixFilterContents() = default;
void MatrixFilterContents::SetMatrix(Matrix matrix) {
matrix_ = matrix;
}
Matrix MatrixFilterContents::GetLocalTransform(
const Matrix& parent_transform) const {
return parent_transform.Invert() * matrix_ * parent_transform;
}
std::optional<Snapshot> MatrixFilterContents::RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const {
return inputs[0]->GetSnapshot(renderer, entity);
}
} // namespace impeller

View File

@@ -0,0 +1,37 @@
// 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 "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
namespace impeller {
class MatrixFilterContents final : public FilterContents {
public:
MatrixFilterContents();
~MatrixFilterContents() override;
void SetMatrix(Matrix matrix);
// |FilterContents|
Matrix GetLocalTransform(const Matrix& parent_transform) const override;
private:
// |FilterContents|
std::optional<Snapshot> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Matrix& effect_transform,
const Rect& coverage) const override;
Matrix matrix_;
FML_DISALLOW_COPY_AND_ASSIGN(MatrixFilterContents);
};
} // namespace impeller