diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index aa5c6a0a1c..1fe4c9d032 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -556,6 +556,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/blend_filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/blend_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.h +FILE: ../../../flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc +FILE: ../../../flutter/impeller/entity/contents/filters/color_matrix_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.h FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -623,6 +625,8 @@ FILE: ../../../flutter/impeller/entity/shaders/blending/blend.frag FILE: ../../../flutter/impeller/entity/shaders/blending/blend.vert FILE: ../../../flutter/impeller/entity/shaders/border_mask_blur.frag FILE: ../../../flutter/impeller/entity/shaders/border_mask_blur.vert +FILE: ../../../flutter/impeller/entity/shaders/color_matrix_color_filter.frag +FILE: ../../../flutter/impeller/entity/shaders/color_matrix_color_filter.vert FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur.frag FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur.vert FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index a0678a2e8a..c14609c731 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -8,6 +8,7 @@ #include #include "display_list/display_list_blend_mode.h" +#include "display_list/display_list_color_filter.h" #include "display_list/display_list_path_effect.h" #include "display_list/display_list_tile_mode.h" #include "flutter/fml/logging.h" @@ -15,6 +16,7 @@ #include "impeller/display_list/display_list_image_impeller.h" #include "impeller/display_list/nine_patch_converter.h" #include "impeller/entity/contents/filters/filter_contents.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/linear_gradient_contents.h" #include "impeller/entity/contents/radial_gradient_contents.h" #include "impeller/entity/contents/solid_stroke_contents.h" @@ -330,10 +332,25 @@ void DisplayListDispatcher::setColorFilter( }; return; } - case flutter::DlColorFilterType::kMatrix: + case flutter::DlColorFilterType::kMatrix: { + const flutter::DlMatrixColorFilter* dl_matrix = filter->asMatrix(); + impeller::FilterContents::ColorMatrix color_matrix; + dl_matrix->get_matrix(color_matrix.array); + paint_.color_filter = [color_matrix](FilterInput::Ref input) { + return FilterContents::MakeColorMatrix({input}, color_matrix); + }; + return; + } case flutter::DlColorFilterType::kSrgbToLinearGamma: + FML_LOG(ERROR) << "requested DlColorFilterType::kSrgbToLinearGamma"; + UNIMPLEMENTED; + break; case flutter::DlColorFilterType::kLinearToSrgbGamma: + FML_LOG(ERROR) << "requested DlColorFilterType::kLinearToSrgbGamma"; + UNIMPLEMENTED; + break; case flutter::DlColorFilterType::kUnknown: + FML_LOG(ERROR) << "requested DlColorFilterType::kUnknown"; UNIMPLEMENTED; break; } diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 014d11484b..aa1adbe767 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -30,6 +30,8 @@ impeller_shaders("entity_shaders") { "shaders/blending/blend.vert", "shaders/border_mask_blur.frag", "shaders/border_mask_blur.vert", + "shaders/color_matrix_color_filter.frag", + "shaders/color_matrix_color_filter.vert", "shaders/gaussian_blur.frag", "shaders/gaussian_blur.vert", "shaders/glyph_atlas.frag", @@ -67,6 +69,8 @@ impeller_component("entity") { "contents/filters/blend_filter_contents.h", "contents/filters/border_mask_blur_filter_contents.cc", "contents/filters/border_mask_blur_filter_contents.h", + "contents/filters/color_matrix_filter_contents.cc", + "contents/filters/color_matrix_filter_contents.h", "contents/filters/filter_contents.cc", "contents/filters/filter_contents.h", "contents/filters/gaussian_blur_filter_contents.cc", diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 2cc6218e08..f63342cc6a 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -195,6 +195,8 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); border_mask_blur_pipelines_[{}] = CreateDefaultPipeline(*context_); + color_matrix_color_filter_pipelines_[{}] = + CreateDefaultPipeline(*context_); solid_stroke_pipelines_[{}] = CreateDefaultPipeline(*context_); glyph_atlas_pipelines_[{}] = diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index d31eb9435d..6ab014470a 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -33,6 +33,8 @@ #include "impeller/entity/blend.vert.h" #include "impeller/entity/border_mask_blur.frag.h" #include "impeller/entity/border_mask_blur.vert.h" +#include "impeller/entity/color_matrix_color_filter.frag.h" +#include "impeller/entity/color_matrix_color_filter.vert.h" #include "impeller/entity/entity.h" #include "impeller/entity/gaussian_blur.frag.h" #include "impeller/entity/gaussian_blur.vert.h" @@ -106,6 +108,9 @@ using GaussianBlurPipeline = PipelineT; using BorderMaskBlurPipeline = PipelineT; +using ColorMatrixColorFilterPipeline = + PipelineT; using SolidStrokePipeline = PipelineT; using GlyphAtlasPipeline = @@ -145,7 +150,7 @@ struct ContentContextOptions { class ContentContext { public: - ContentContext(std::shared_ptr context); + explicit ContentContext(std::shared_ptr context); ~ContentContext(); @@ -194,6 +199,11 @@ class ContentContext { return GetPipeline(border_mask_blur_pipelines_, opts); } + std::shared_ptr GetColorMatrixColorFilterPipeline( + ContentContextOptions opts) const { + return GetPipeline(color_matrix_color_filter_pipelines_, opts); + } + std::shared_ptr GetSolidStrokePipeline( ContentContextOptions opts) const { return GetPipeline(solid_stroke_pipelines_, opts); @@ -325,6 +335,8 @@ class ContentContext { mutable Variants texture_pipelines_; mutable Variants gaussian_blur_pipelines_; mutable Variants border_mask_blur_pipelines_; + mutable Variants + color_matrix_color_filter_pipelines_; mutable Variants solid_stroke_pipelines_; mutable Variants clip_pipelines_; mutable Variants glyph_atlas_pipelines_; diff --git a/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc new file mode 100644 index 0000000000..0d93195d64 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc @@ -0,0 +1,88 @@ +// 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/color_matrix_filter_contents.h" + +#include + +#include "impeller/entity/contents/content_context.h" +#include "impeller/entity/contents/contents.h" +#include "impeller/geometry/point.h" +#include "impeller/geometry/vector.h" +#include "impeller/renderer/render_pass.h" +#include "impeller/renderer/sampler_library.h" + +namespace impeller { + +ColorMatrixFilterContents::ColorMatrixFilterContents() = default; + +ColorMatrixFilterContents::~ColorMatrixFilterContents() = default; + +void ColorMatrixFilterContents::SetMatrix(const ColorMatrix& matrix) { + matrix_ = matrix; +} + +bool ColorMatrixFilterContents::RenderFilter(const FilterInput::Vector& inputs, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass, + const Rect& coverage) const { + if (inputs.empty()) { + return true; + } + + using VS = ColorMatrixColorFilterPipeline::VertexShader; + using FS = ColorMatrixColorFilterPipeline::FragmentShader; + + auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity); + if (!input_snapshot.has_value()) { + return true; + } + + Command cmd; + cmd.label = "Color Matrix Filter"; + + auto options = OptionsFromPass(pass); + options.blend_mode = Entity::BlendMode::kSource; + cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options); + + VertexBufferBuilder vtx_builder; + vtx_builder.AddVertices({ + {Point(0, 0)}, + {Point(1, 0)}, + {Point(1, 1)}, + {Point(0, 0)}, + {Point(1, 1)}, + {Point(0, 1)}, + }); + auto& host_buffer = pass.GetTransientsBuffer(); + auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer); + cmd.BindVertices(vtx_buffer); + + VS::FrameInfo frame_info; + frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); + + FS::FragInfo frag_info; + const float* matrix = matrix_.array; + frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]); + // clang-format off + frag_info.color_m = + Matrix( + matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3], + matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8], + matrix[10], matrix[11], matrix[12], matrix[13], + matrix[15], matrix[16], matrix[17], matrix[18] + ); + // clang-format on + + auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({}); + FS::BindInputTexture(cmd, input_snapshot->texture, sampler); + FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); + + VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + + return pass.AddCommand(std::move(cmd)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.h new file mode 100644 index 0000000000..e3cdfdcd62 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.h @@ -0,0 +1,38 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include +#include + +#include "impeller/entity/contents/filters/filter_contents.h" +#include "impeller/entity/contents/filters/inputs/filter_input.h" + +namespace impeller { + +// Look at example at: https://github.com/flutter/impeller/pull/132 + +class ColorMatrixFilterContents final : public FilterContents { + public: + ColorMatrixFilterContents(); + + ~ColorMatrixFilterContents() override; + + void SetMatrix(const ColorMatrix& matrix); + + private: + // |FilterContents| + bool RenderFilter(const FilterInput::Vector& input_textures, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass, + const Rect& coverage) const override; + + ColorMatrix matrix_; + + FML_DISALLOW_COPY_AND_ASSIGN(ColorMatrixFilterContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc index af42a5cca3..b600f40dbe 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -16,6 +16,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/filters/blend_filter_contents.h" #include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h" +#include "impeller/entity/contents/filters/color_matrix_filter_contents.h" #include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/texture_contents.h" @@ -112,6 +113,15 @@ std::shared_ptr FilterContents::MakeBorderMaskBlur( return filter; } +std::shared_ptr FilterContents::MakeColorMatrix( + FilterInput::Ref input, + const ColorMatrix& matrix) { + auto filter = std::make_shared(); + filter->SetInputs({input}); + filter->SetMatrix(matrix); + return filter; +} + FilterContents::FilterContents() = default; FilterContents::~FilterContents() = default; diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h index 84134060e1..06a5e6d4cc 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -31,6 +31,11 @@ class FilterContents : public Contents { kInner, }; + // Domain is kRGBA, we may decide to support more color modes later. + struct ColorMatrix { + float array[20]; + }; + static std::shared_ptr MakeBlend( Entity::BlendMode blend_mode, FilterInput::Vector inputs, @@ -57,6 +62,10 @@ class FilterContents : public Contents { Sigma sigma_y, BlurStyle blur_style = BlurStyle::kNormal); + static std::shared_ptr MakeColorMatrix( + FilterInput::Ref input, + const ColorMatrix& matrix); + FilterContents(); ~FilterContents() override; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 2e28a2f0d1..a10da41ce5 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1415,5 +1415,113 @@ TEST_P(EntityTest, RRectShadowTest) { ASSERT_TRUE(OpenPlaygroundHere(callback)); } +TEST_P(EntityTest, ColorMatrixFilterCoverageIsCorrect) { + // Set up a simple color background. + auto fill = std::make_shared(); + fill->SetPath( + PathBuilder{}.AddRect(Rect::MakeXYWH(0, 0, 300, 400)).TakePath()); + fill->SetColor(Color::Coral()); + + // Set the color matrix filter. + FilterContents::ColorMatrix matrix = { + 1, 1, 1, 1, 1, // + 1, 1, 1, 1, 1, // + 1, 1, 1, 1, 1, // + 1, 1, 1, 1, 1, // + }; + + auto filter = + FilterContents::MakeColorMatrix(FilterInput::Make(fill), matrix); + + Entity e; + e.SetTransformation(Matrix()); + + // Confirm that the actual filter coverage matches the expected coverage. + auto actual = filter->GetCoverage(e); + auto expected = Rect::MakeXYWH(0, 0, 300, 400); + + ASSERT_TRUE(actual.has_value()); + ASSERT_RECT_NEAR(actual.value(), expected); +} + +TEST_P(EntityTest, ColorMatrixFilter) { + auto image = CreateTextureForFixture("boston.jpg"); + ASSERT_TRUE(image); + + auto callback = [&](ContentContext& context, RenderPass& pass) -> bool { + // Set the color matrix filter. + FilterContents::ColorMatrix matrix = { + 1, 0, 0, 0, 0, // + 0, 3, 0, 0, 0, // + 0, 0, 1, 0, 0, // + 0, 0, 0, 1, 0, // + }; + + auto filter = + FilterContents::MakeColorMatrix(FilterInput::Make(image), matrix); + + // Define the entity with the color matrix filter. + Entity entity; + entity.SetTransformation(Matrix::MakeScale(GetContentScale()) * + Matrix::MakeTranslation({500, 300}) * + Matrix::MakeScale(Vector2{0.5, 0.5})); + entity.SetContents(filter); + return entity.Render(context, pass); + }; + + // Should output the boston image with a green filter. + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + +TEST_P(EntityTest, ColorMatrixFilterEditable) { + auto bay_bridge = CreateTextureForFixture("bay_bridge.jpg"); + ASSERT_TRUE(bay_bridge); + + bool first_frame = true; + auto callback = [&](ContentContext& context, RenderPass& pass) -> bool { + // If this is the first frame, set the ImGui's initial size and postion. + if (first_frame) { + first_frame = false; + ImGui::SetNextWindowSize({500, 150}); + ImGui::SetNextWindowPos({260, 600}); + } + + // UI state. + static FilterContents::ColorMatrix color_matrix = { + 1, 0, 0, 0, 0, // + 0, 3, 0, 0, 0, // + 0, 0, 1, 0, 0, // + 0, 0, 0, 1, 0, // + }; + + // Define the ImGui + ImGui::Begin("Color Matrix"); + std::string label = "##1"; + label.c_str(); + for (int i = 0; i < 20; i += 5) { + ImGui::InputScalarN(label.c_str(), ImGuiDataType_Float, + &(color_matrix.array[i]), 5, NULL, NULL, "%.2f", 0); + label[2]++; + } + ImGui::End(); + + // Set the color matrix filter. + auto filter = FilterContents::MakeColorMatrix(FilterInput::Make(bay_bridge), + color_matrix); + + // Define the entity with the color matrix filter. + Entity entity; + entity.SetTransformation(Matrix::MakeScale(GetContentScale()) * + Matrix::MakeTranslation({250, 200}) * + Matrix::MakeScale(Vector2{0.5, 0.5})); + entity.SetContents(filter); + entity.Render(context, pass); + + return true; + }; + + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.frag b/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.frag new file mode 100644 index 0000000000..08adb27b66 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.frag @@ -0,0 +1,48 @@ +// 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 + +// A color filter that transforms colors through a 4x5 color matrix. +// +// This filter can be used to change the saturation of pixels, convert from YUV to RGB, etc. +// +// 4x5 matrix for transforming the color and alpha components of a Bitmap. +// The matrix can be passed as single array, and is treated as follows: +// +// [ a, b, c, d, e, +// f, g, h, i, j, +// k, l, m, n, o, +// p, q, r, s, t ] +// +// When applied to a color [R, G, B, A], the resulting color is computed as: +// +// R’ = a*R + b*G + c*B + d*A + e; +// G’ = f*R + g*G + h*B + i*A + j; +// B’ = k*R + l*G + m*B + n*A + o; +// A’ = p*R + q*G + r*B + s*A + t; +// +// That resulting color [R’, G’, B’, A’] then has each channel clamped to the 0 to 255 range. + +uniform FragInfo { + mat4 color_m; + vec4 color_v; +} frag_info; + +uniform sampler2D input_texture; + +in vec2 v_position; +out vec4 frag_color; + +void main() { + vec4 input_color = texture(input_texture, v_position); + + // unpremultiply first, as filter inputs are premultiplied. + vec4 color = IPUnpremultiply(input_color); + + color = frag_info.color_m * color + frag_info.color_v; + + // premultiply the outputs + frag_color = vec4(color.rgb * color.a, color.a); +} diff --git a/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.vert b/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.vert new file mode 100644 index 0000000000..b741b2744e --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/color_matrix_color_filter.vert @@ -0,0 +1,15 @@ +// 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. + +uniform FrameInfo { + mat4 mvp; +} frame_info; + +in vec2 position; +out vec2 v_position; + +void main() { + v_position = position; + gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0); +}