diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index e32f539581..ff50b5a9d0 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -584,6 +584,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/srgb_to_linear_filter_contents.cc +FILE: ../../../flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.h FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.cc FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.h FILE: ../../../flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -655,6 +657,8 @@ FILE: ../../../flutter/impeller/entity/shaders/solid_fill.frag FILE: ../../../flutter/impeller/entity/shaders/solid_fill.vert FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.frag FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.vert +FILE: ../../../flutter/impeller/entity/shaders/srgb_to_linear_filter.frag +FILE: ../../../flutter/impeller/entity/shaders/srgb_to_linear_filter.vert FILE: ../../../flutter/impeller/entity/shaders/sweep_gradient_fill.frag FILE: ../../../flutter/impeller/entity/shaders/texture_fill.frag FILE: ../../../flutter/impeller/entity/shaders/texture_fill.vert 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 0fbd95d891..8cdc086523 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -438,9 +438,10 @@ void DisplayListDispatcher::setColorFilter( return; } case flutter::DlColorFilterType::kSrgbToLinearGamma: - FML_LOG(ERROR) << "requested DlColorFilterType::kSrgbToLinearGamma"; - UNIMPLEMENTED; - break; + paint_.color_filter = [](FilterInput::Ref input) { + return FilterContents::MakeSrgbToLinearFilter({input}); + }; + return; case flutter::DlColorFilterType::kLinearToSrgbGamma: paint_.color_filter = [](FilterInput::Ref input) { return FilterContents::MakeLinearToSrgbFilter({input}); diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 270f3c8a64..ec16b8e584 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -47,6 +47,8 @@ impeller_shaders("entity_shaders") { "shaders/solid_fill.vert", "shaders/solid_stroke.frag", "shaders/solid_stroke.vert", + "shaders/srgb_to_linear_filter.frag", + "shaders/srgb_to_linear_filter.vert", "shaders/sweep_gradient_fill.frag", "shaders/texture_fill.frag", "shaders/texture_fill.vert", @@ -89,6 +91,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/srgb_to_linear_filter_contents.cc", + "contents/filters/srgb_to_linear_filter_contents.h", "contents/linear_gradient_contents.cc", "contents/linear_gradient_contents.h", "contents/radial_gradient_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 030fb6b139..7dd30168df 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -201,6 +201,8 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); linear_to_srgb_filter_pipelines_[{}] = CreateDefaultPipeline(*context_); + srgb_to_linear_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 f984a1da06..7c79b72e74 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -51,6 +51,8 @@ #include "impeller/entity/solid_fill.vert.h" #include "impeller/entity/solid_stroke.frag.h" #include "impeller/entity/solid_stroke.vert.h" +#include "impeller/entity/srgb_to_linear_filter.frag.h" +#include "impeller/entity/srgb_to_linear_filter.vert.h" #include "impeller/entity/sweep_gradient_fill.frag.h" #include "impeller/entity/texture_fill.frag.h" #include "impeller/entity/texture_fill.vert.h" @@ -117,6 +119,8 @@ using ColorMatrixColorFilterPipeline = ColorMatrixColorFilterFragmentShader>; using LinearToSrgbFilterPipeline = PipelineT; +using SrgbToLinearFilterPipeline = + PipelineT; using SolidStrokePipeline = PipelineT; using GlyphAtlasPipeline = @@ -220,6 +224,11 @@ class ContentContext { return GetPipeline(linear_to_srgb_filter_pipelines_, opts); } + std::shared_ptr GetSrgbToLinearFilterPipeline( + ContentContextOptions opts) const { + return GetPipeline(srgb_to_linear_filter_pipelines_, opts); + } + std::shared_ptr GetSolidStrokePipeline( ContentContextOptions opts) const { return GetPipeline(solid_stroke_pipelines_, opts); @@ -355,6 +364,7 @@ class ContentContext { mutable Variants color_matrix_color_filter_pipelines_; mutable Variants linear_to_srgb_filter_pipelines_; + mutable Variants srgb_to_linear_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/filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc index 48a79fd878..694c56ef95 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -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/srgb_to_linear_filter_contents.h" #include "impeller/entity/contents/texture_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/path_builder.h" @@ -130,6 +131,13 @@ std::shared_ptr FilterContents::MakeLinearToSrgbFilter( return filter; } +std::shared_ptr FilterContents::MakeSrgbToLinearFilter( + FilterInput::Ref input) { + auto filter = std::make_shared(); + filter->SetInputs({input}); + 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 677ebc1dba..1ee7a5813a 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -69,6 +69,9 @@ class FilterContents : public Contents { static std::shared_ptr MakeLinearToSrgbFilter( FilterInput::Ref input); + static std::shared_ptr MakeSrgbToLinearFilter( + FilterInput::Ref input); + FilterContents(); ~FilterContents() override; diff --git a/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc new file mode 100644 index 0000000000..bef5543d24 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc @@ -0,0 +1,83 @@ +// 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/srgb_to_linear_filter_contents.h" + +#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 { + +SrgbToLinearFilterContents::SrgbToLinearFilterContents() = default; + +SrgbToLinearFilterContents::~SrgbToLinearFilterContents() = default; + +std::optional SrgbToLinearFilterContents::RenderFilter( + const FilterInput::Vector& inputs, + const ContentContext& renderer, + const Entity& entity, + const Rect& coverage) const { + if (inputs.empty()) { + return std::nullopt; + } + + using VS = SrgbToLinearFilterPipeline::VertexShader; + using FS = SrgbToLinearFilterPipeline::FragmentShader; + + auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity); + if (!input_snapshot.has_value()) { + return std::nullopt; + } + + ContentContext::SubpassCallback callback = [&](const ContentContext& renderer, + RenderPass& pass) { + Command cmd; + cmd.label = "sRGB to Linear Filter"; + + auto options = OptionsFromPass(pass); + options.blend_mode = Entity::BlendMode::kSource; + cmd.pipeline = renderer.GetSrgbToLinearFilterPipeline(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)); + + auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({}); + FS::BindInputTexture(cmd, input_snapshot->texture, sampler); + + VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + + return pass.AddCommand(std::move(cmd)); + }; + + auto out_texture = + renderer.MakeSubpass(input_snapshot->texture->GetSize(), callback); + if (!out_texture) { + return std::nullopt; + } + out_texture->SetLabel("SrgbToLinear Texture"); + + return Snapshot{.texture = out_texture, + .transform = input_snapshot->transform, + .sampler_descriptor = input_snapshot->sampler_descriptor}; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.h new file mode 100644 index 0000000000..099aced7bf --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.h @@ -0,0 +1,29 @@ +// 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 SrgbToLinearFilterContents final : public FilterContents { + public: + SrgbToLinearFilterContents(); + + ~SrgbToLinearFilterContents() override; + + private: + // |FilterContents| + std::optional RenderFilter( + const FilterInput::Vector& input_textures, + const ContentContext& renderer, + const Entity& entity, + const Rect& coverage) const override; + + FML_DISALLOW_COPY_AND_ASSIGN(SrgbToLinearFilterContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 260b9e4805..76c62dc976 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1594,5 +1594,56 @@ TEST_P(EntityTest, LinearToSrgbFilter) { ASSERT_TRUE(OpenPlaygroundHere(callback)); } +TEST_P(EntityTest, SrgbToLinearFilterCoverageIsCorrect) { + // 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::DeepPink()); + + auto filter = FilterContents::MakeSrgbToLinearFilter(FilterInput::Make(fill)); + + 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, SrgbToLinearFilter) { + auto image = CreateTextureForFixture("embarcadero.jpg"); + ASSERT_TRUE(image); + + auto callback = [&](ContentContext& context, RenderPass& pass) -> bool { + auto filtered = + FilterContents::MakeSrgbToLinearFilter(FilterInput::Make(image)); + + // Define the entity that will serve as the control image as a Gaussian blur + // filter with no filter at all. + Entity entity_left; + entity_left.SetTransformation(Matrix::MakeScale(GetContentScale()) * + Matrix::MakeTranslation({100, 300}) * + Matrix::MakeScale(Vector2{0.5, 0.5})); + auto unfiltered = FilterContents::MakeGaussianBlur(FilterInput::Make(image), + Sigma{0}, Sigma{0}); + entity_left.SetContents(unfiltered); + + // Define the entity that will be filtered from sRGB to linear. + Entity entity_right; + entity_right.SetTransformation(Matrix::MakeScale(GetContentScale()) * + Matrix::MakeTranslation({500, 300}) * + Matrix::MakeScale(Vector2{0.5, 0.5})); + entity_right.SetContents(filtered); + return entity_left.Render(context, pass) && + entity_right.Render(context, pass); + }; + + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_filter.frag b/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_filter.frag new file mode 100644 index 0000000000..6c51b59a1d --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_filter.frag @@ -0,0 +1,27 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +// Creates a color filter that applies the inverse of the sRGB gamma curve +// to the RGB channels. + +uniform sampler2D input_texture; + +in vec2 v_position; +out vec4 frag_color; + +void main() { + vec4 input_color = texture(input_texture, v_position); + + for (int i = 0; i < 4; i++) { + if (input_color[i] <= 0.04045) { + input_color[i] = input_color[i] / 12.92; + } else { + input_color[i] = pow((input_color[i] + 0.055) / 1.055, 2.4); + } + } + + frag_color = input_color; +} diff --git a/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_filter.vert b/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_filter.vert new file mode 100644 index 0000000000..b741b2744e --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/srgb_to_linear_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); +}