[Impeller] sRGB to Linear Gamma filter implementation. (flutter/engine#35441)

* Update display list dispatcher

* Add new shader

* add filter contents files

* update filter contents

* Update content context

* update unit tests

* Update build file.

* Formatting.

* Formatting.

* Fix typo

* Update filter.

* Formatting.

* Update license

* license

* Remove unnecessary library

* Fix typo

* Formatting

* Formatting
This commit is contained in:
Bernardo Eilert Trevisan
2022-08-19 11:00:04 -07:00
committed by GitHub
parent 47978c2a21
commit d6c219efef
12 changed files with 240 additions and 3 deletions

View File

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

View File

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

View File

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

View File

@@ -201,6 +201,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
CreateDefaultPipeline<ColorMatrixColorFilterPipeline>(*context_);
linear_to_srgb_filter_pipelines_[{}] =
CreateDefaultPipeline<LinearToSrgbFilterPipeline>(*context_);
srgb_to_linear_filter_pipelines_[{}] =
CreateDefaultPipeline<SrgbToLinearFilterPipeline>(*context_);
solid_stroke_pipelines_[{}] =
CreateDefaultPipeline<SolidStrokePipeline>(*context_);
glyph_atlas_pipelines_[{}] =

View File

@@ -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<LinearToSrgbFilterVertexShader, LinearToSrgbFilterFragmentShader>;
using SrgbToLinearFilterPipeline =
PipelineT<SrgbToLinearFilterVertexShader, SrgbToLinearFilterFragmentShader>;
using SolidStrokePipeline =
PipelineT<SolidStrokeVertexShader, SolidStrokeFragmentShader>;
using GlyphAtlasPipeline =
@@ -220,6 +224,11 @@ class ContentContext {
return GetPipeline(linear_to_srgb_filter_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetSrgbToLinearFilterPipeline(
ContentContextOptions opts) const {
return GetPipeline(srgb_to_linear_filter_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetSolidStrokePipeline(
ContentContextOptions opts) const {
return GetPipeline(solid_stroke_pipelines_, opts);
@@ -355,6 +364,7 @@ class ContentContext {
mutable Variants<ColorMatrixColorFilterPipeline>
color_matrix_color_filter_pipelines_;
mutable Variants<LinearToSrgbFilterPipeline> linear_to_srgb_filter_pipelines_;
mutable Variants<SrgbToLinearFilterPipeline> srgb_to_linear_filter_pipelines_;
mutable Variants<SolidStrokePipeline> solid_stroke_pipelines_;
mutable Variants<ClipPipeline> clip_pipelines_;
mutable Variants<GlyphAtlasPipeline> glyph_atlas_pipelines_;

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/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> FilterContents::MakeLinearToSrgbFilter(
return filter;
}
std::shared_ptr<FilterContents> FilterContents::MakeSrgbToLinearFilter(
FilterInput::Ref input) {
auto filter = std::make_shared<SrgbToLinearFilterContents>();
filter->SetInputs({input});
return filter;
}
FilterContents::FilterContents() = default;
FilterContents::~FilterContents() = default;

View File

@@ -69,6 +69,9 @@ class FilterContents : public Contents {
static std::shared_ptr<FilterContents> MakeLinearToSrgbFilter(
FilterInput::Ref input);
static std::shared_ptr<FilterContents> MakeSrgbToLinearFilter(
FilterInput::Ref input);
FilterContents();
~FilterContents() override;

View File

@@ -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<Snapshot> 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<VS::PerVertexData> 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

View File

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

View File

@@ -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<SolidColorContents>();
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

View File

@@ -0,0 +1,27 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/color.glsl>
// 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;
}

View File

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