Chainable texture filters (flutter/engine#67)

This commit is contained in:
Brandon DeRosier
2022-03-10 12:35:53 -08:00
committed by Dan Field
parent d0e290089d
commit c89c9b9585
8 changed files with 339 additions and 5 deletions

View File

@@ -29,6 +29,8 @@ impeller_component("entity") {
"contents/content_context.h",
"contents/contents.cc",
"contents/contents.h",
"contents/filter_contents.cc",
"contents/filter_contents.h",
"contents/linear_gradient_contents.cc",
"contents/linear_gradient_contents.h",
"contents/solid_color_contents.cc",
@@ -55,7 +57,6 @@ impeller_component("entity") {
"../typographer",
]
deps = [
"//flutter/fml",

View File

@@ -159,6 +159,12 @@ class ContentContext {
color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
break;
case Entity::BlendMode::kPlus:
color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;
color0.dst_color_blend_factor = BlendFactor::kOne;
color0.src_alpha_blend_factor = BlendFactor::kSourceAlpha;
color0.src_color_blend_factor = BlendFactor::kOne;
break;
}
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
}

View File

@@ -0,0 +1,203 @@
// 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 "filter_contents.h"
#include <memory>
#include <optional>
#include <variant>
#include "flutter/fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
namespace impeller {
/*******************************************************************************
******* FilterContents
******************************************************************************/
std::shared_ptr<FilterContents> FilterContents::MakeBlend(
Entity::BlendMode blend_mode,
InputTextures input_textures) {
auto blend = std::make_shared<BlendFilterContents>();
blend->SetInputTextures(input_textures);
blend->SetBlendMode(blend_mode);
return blend;
}
FilterContents::FilterContents() = default;
FilterContents::~FilterContents() = default;
void FilterContents::SetInputTextures(InputTextures input_textures) {
input_textures_ = std::move(input_textures);
}
bool FilterContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
// Run the filter.
auto maybe_texture = RenderFilterToTexture(renderer, entity, pass);
if (!maybe_texture.has_value()) {
return false;
}
auto& texture = maybe_texture.value();
// Draw the resulting texture to the given destination rect, respecting the
// transform and clip stack.
auto contents = std::make_shared<TextureContents>();
contents->SetTexture(texture);
contents->SetSourceRect(IRect::MakeSize(texture->GetSize()));
return contents->Render(renderer, entity, pass);
}
std::optional<std::shared_ptr<Texture>> FilterContents::RenderFilterToTexture(
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
auto output_size = GetOutputSize();
if (output_size.IsZero()) {
return std::nullopt;
}
// Resolve all inputs as textures.
std::vector<std::shared_ptr<Texture>> input_textures;
input_textures.reserve(input_textures_.size());
for (const auto& input : input_textures_) {
if (auto filter = std::get_if<std::shared_ptr<FilterContents>>(&input)) {
auto texture =
filter->get()->RenderFilterToTexture(renderer, entity, pass);
if (!texture.has_value()) {
return std::nullopt;
}
input_textures.push_back(std::move(texture.value()));
} else if (auto texture = std::get_if<std::shared_ptr<Texture>>(&input)) {
input_textures.push_back(*texture);
} else {
FML_UNREACHABLE();
}
}
// Create a new texture and render the filter to it.
auto context = renderer.GetContext();
auto subpass_target = RenderTarget::CreateOffscreen(*context, output_size);
auto subpass_texture = subpass_target.GetRenderTargetTexture();
if (!subpass_texture) {
return std::nullopt;
}
auto sub_command_buffer = context->CreateRenderCommandBuffer();
sub_command_buffer->SetLabel("Offscreen Filter Command Buffer");
if (!sub_command_buffer) {
return std::nullopt;
}
auto sub_renderpass = sub_command_buffer->CreateRenderPass(subpass_target);
if (!sub_renderpass) {
return std::nullopt;
}
sub_renderpass->SetLabel("OffscreenFilterPass");
if (!RenderFilter(input_textures, renderer, *sub_renderpass)) {
return std::nullopt;
}
if (!sub_renderpass->EncodeCommands(*context->GetTransientsAllocator())) {
return std::nullopt;
}
if (!sub_command_buffer->SubmitCommands()) {
return std::nullopt;
}
return subpass_texture;
}
ISize FilterContents::GetOutputSize() const {
if (input_textures_.empty()) {
return {};
}
if (auto filter =
std::get_if<std::shared_ptr<FilterContents>>(&input_textures_[0])) {
return filter->get()->GetOutputSize();
}
if (auto texture =
std::get_if<std::shared_ptr<Texture>>(&input_textures_[0])) {
return texture->get()->GetSize();
}
FML_UNREACHABLE();
}
/*******************************************************************************
******* BlendFilterContents
******************************************************************************/
BlendFilterContents::BlendFilterContents() = default;
BlendFilterContents::~BlendFilterContents() = default;
void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
blend_mode_ = blend_mode;
}
bool BlendFilterContents::RenderFilter(
const std::vector<std::shared_ptr<Texture>>& input_textures,
const ContentContext& renderer,
RenderPass& pass) const {
using VS = TexturePipeline::VertexShader;
using FS = TexturePipeline::FragmentShader;
auto& host_buffer = pass.GetTransientsBuffer();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), Point(0, 0)},
{Point(1, 0), Point(1, 0)},
{Point(1, 1), Point(1, 1)},
{Point(0, 0), Point(0, 0)},
{Point(1, 1), Point(1, 1)},
{Point(0, 1), Point(0, 1)},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.alpha = 1;
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
Command cmd;
cmd.label = "Blend Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = blend_mode_;
cmd.pipeline = renderer.GetTexturePipeline(options);
cmd.BindVertices(vtx_buffer);
VS::BindFrameInfo(cmd, uniform_view);
for (const auto& texture : input_textures) {
FS::BindTextureSampler(cmd, texture, sampler);
pass.AddCommand(cmd);
}
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,94 @@
// 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 <memory>
#include <variant>
#include <vector>
#include "impeller/entity/contents/contents.h"
#include "impeller/entity/entity.h"
#include "impeller/renderer/formats.h"
namespace impeller {
/*******************************************************************************
******* FilterContents
******************************************************************************/
class FilterContents : public Contents {
public:
using InputVariant =
std::variant<std::shared_ptr<Texture>, std::shared_ptr<FilterContents>>;
using InputTextures = std::vector<InputVariant>;
static std::shared_ptr<FilterContents> MakeBlend(
Entity::BlendMode blend_mode,
InputTextures input_textures);
FilterContents();
~FilterContents() override;
/// @brief The input texture sources for this filter. All texture sources are
/// expected to have or produce premultiplied alpha colors.
/// Any input can either be a `Texture` or another `FilterContents`.
///
/// The number of required or optional textures depends on the
/// particular filter's implementation.
void SetInputTextures(InputTextures input_textures);
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
/// @brief Renders dependency filters, creates a subpass, and calls the
/// `RenderFilter` defined by the subclasses.
std::optional<std::shared_ptr<Texture>> RenderFilterToTexture(
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const;
private:
/// @brief Takes a set of zero or more input textures and writes to an output
/// texture.
virtual bool RenderFilter(
const std::vector<std::shared_ptr<Texture>>& input_textures,
const ContentContext& renderer,
RenderPass& pass) const = 0;
/// @brief Determines the size of the output texture.
virtual ISize GetOutputSize() const;
InputTextures input_textures_;
Rect destination_;
FML_DISALLOW_COPY_AND_ASSIGN(FilterContents);
};
/*******************************************************************************
******* BlendFilterContents
******************************************************************************/
class BlendFilterContents : public FilterContents {
public:
BlendFilterContents();
~BlendFilterContents() override;
void SetBlendMode(Entity::BlendMode blend_mode);
private:
bool RenderFilter(const std::vector<std::shared_ptr<Texture>>& input_textures,
const ContentContext& renderer,
RenderPass& pass) const override;
Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver;
FML_DISALLOW_COPY_AND_ASSIGN(BlendFilterContents);
};
} // namespace impeller

View File

@@ -65,7 +65,8 @@ void Entity::IncrementStencilDepth(uint32_t increment) {
stencil_depth_ += increment;
}
bool Entity::Render(ContentContext& renderer, RenderPass& parent_pass) const {
bool Entity::Render(const ContentContext& renderer,
RenderPass& parent_pass) const {
if (!contents_) {
return true;
}

View File

@@ -27,6 +27,7 @@ class Entity {
kDestination,
kSourceOver,
kDestinationOver,
kPlus,
};
Entity();
@@ -57,7 +58,7 @@ class Entity {
uint32_t GetStencilDepth() const;
bool Render(ContentContext& renderer, RenderPass& parent_pass) const;
bool Render(const ContentContext& renderer, RenderPass& parent_pass) const;
private:
Matrix transformation_;

View File

@@ -75,7 +75,7 @@ std::optional<Rect> EntityPass::GetSubpassCoverage(
return entities_coverage;
}
// If the delete tells us the coverage is smaller than it needs to be, then
// If the delegate tells us the coverage is smaller than it needs to be, then
// great. OTOH, if the delegate is being wasteful, limit coverage to what is
// actually needed.
return entities_coverage->Intersection(delegate_coverage.value());

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/testing/testing.h"
#include "impeller/entity/contents/filter_contents.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/solid_stroke_contents.h"
#include "impeller/entity/entity.h"
@@ -521,6 +522,9 @@ TEST_F(EntityTest, BlendingModeOptions) {
case Entity::BlendMode::kDestinationOver:
blend_mode_names.push_back("DestinationOver");
blend_mode_values.push_back(Entity::BlendMode::kDestinationOver);
case Entity::BlendMode::kPlus:
blend_mode_names.push_back("Plus");
blend_mode_values.push_back(Entity::BlendMode::kPlus);
};
}
@@ -616,10 +620,34 @@ TEST_F(EntityTest, BezierCircleScaled) {
.Close()
.TakePath();
entity.SetPath(path);
entity.SetTransformation(Matrix::MakeScale({20.0, 20.0, 1.0}).Translate({-80, -15, 0}));
entity.SetTransformation(
Matrix::MakeScale({20.0, 20.0, 1.0}).Translate({-80, -15, 0}));
entity.SetContents(SolidColorContents::Make(Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
TEST_F(EntityTest, Filters) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
auto kalimba = CreateTextureForFixture("kalimba.jpg");
ASSERT_TRUE(bridge && boston && kalimba);
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
// Draws kalimba and overwrites it with boston.
auto blend0 = FilterContents::MakeBlend(
Entity::BlendMode::kSourceOver, {kalimba, boston});
// Adds bridge*3 to boston.
auto blend1 = FilterContents::MakeBlend(
Entity::BlendMode::kPlus, {bridge, bridge, blend0, bridge});
Entity entity;
entity.SetPath(PathBuilder{}.AddRect({100, 100, 300, 300}).TakePath());
entity.SetContents(blend1);
return entity.Render(context, pass);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
} // namespace testing
} // namespace impeller