Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ impeller_component("entity") {
"contents/filters/srgb_to_linear_filter_contents.h",
"contents/filters/yuv_to_rgb_filter_contents.cc",
"contents/filters/yuv_to_rgb_filter_contents.h",
"contents/foreground_blend_contents.cc",
"contents/foreground_blend_contents.h",
"contents/framebuffer_blend_contents.cc",
"contents/framebuffer_blend_contents.h",
"contents/gradient_generator.cc",
Expand Down
18 changes: 18 additions & 0 deletions impeller/entity/contents/filters/blend_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
#include "impeller/entity/contents/foreground_blend_contents.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
Expand Down Expand Up @@ -373,6 +374,23 @@ std::optional<Entity> BlendFilterContents::RenderFilter(
}

if (blend_mode_ <= Entity::kLastAdvancedBlendMode) {
auto potential_alpha = GetAlpha().value_or(1.0);
if (inputs.size() == 1 && foreground_color_.has_value() &&
potential_alpha >= 1.0 - kEhCloseEnough) {
auto contents = std::make_shared<AdvancedForegroundBlendContents>();
contents->SetBlendMode(blend_mode_);
contents->SetCoverage(coverage);
contents->SetSrcInput(inputs[0]);
contents->SetForegroundColor(foreground_color_.value());

Entity sub_entity;
sub_entity.SetContents(std::move(contents));
sub_entity.SetStencilDepth(entity.GetStencilDepth());
sub_entity.SetTransformation(Matrix::MakeTranslation(coverage.origin) *
entity.GetTransformation());
return sub_entity;
}

return advanced_blend_proc_(inputs, renderer, entity, coverage,
foreground_color_, GetAbsorbOpacity(),
GetAlpha());
Expand Down
169 changes: 169 additions & 0 deletions impeller/entity/contents/foreground_blend_contents.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// 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 "foreground_blend_contents.h"
#include <iostream>

#include "flutter/impeller/entity/contents/content_context.h"
#include "flutter/impeller/renderer/render_pass.h"
#include "flutter/impeller/renderer/sampler_library.h"

namespace impeller {

AdvancedForegroundBlendContents::AdvancedForegroundBlendContents() {}

AdvancedForegroundBlendContents::~AdvancedForegroundBlendContents() {}

void AdvancedForegroundBlendContents::SetBlendMode(BlendMode blend_mode) {
FML_DCHECK(blend_mode > Entity::kLastPipelineBlendMode);
blend_mode_ = blend_mode;
}

void AdvancedForegroundBlendContents::SetSrcInput(
std::shared_ptr<FilterInput> input) {
input_ = std::move(input);
}

void AdvancedForegroundBlendContents::SetForegroundColor(Color color) {
foreground_color_ = color;
}

void AdvancedForegroundBlendContents::SetCoverage(Rect rect) {
rect_ = rect;
}

std::optional<Rect> AdvancedForegroundBlendContents::GetCoverage(
const Entity& entity) const {
return rect_.TransformBounds(entity.GetTransformation());
}

bool AdvancedForegroundBlendContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
using VS = BlendScreenPipeline::VertexShader;
using FS = BlendScreenPipeline::FragmentShader;

auto& host_buffer = pass.GetTransientsBuffer();

auto dst_snapshot = input_->GetSnapshot(renderer, entity);
if (!dst_snapshot.has_value()) {
return false;
}
auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(rect_);
if (!maybe_dst_uvs.has_value()) {
return false;
}
auto dst_uvs = maybe_dst_uvs.value();

auto size = rect_.size;
VertexBufferBuilder<VS::PerVertexData> vtx_builder;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From reading render_filter, it appears that the coverage rect we are given already has the CTM applied. So we have two choices here: either directly use the values for the coordinates (and don't apply it to the MVP - or invert the transform and likewise.

Of course, if you don't invert the transform and fix the coordinates, then the UVs come out wrong and that's why I've "Fixed" them below.

I'm not sure exactly why this worked before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there is some difference in a snapshot that is created by a contents, and a snapshot that is forwarded through from an entity pass. I'm tempted just to say, for this particular case, we should just set the UVs using the coverage rect, since that coverage rect already includes the CTM it is correct for both entity subpasses and for contents. But I'm still not really sure how I should be using the snapshot GetCoverageUVs directly which is concerning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess more of a question for @bdero, but what is the correct way to use snapshot.GetCoverageUVs ?

// hack
dst_uvs[0] = Point(0.0, 0.0);
dst_uvs[1] = Point(1.0, 0.0);
dst_uvs[2] = Point(0.0, 1.0);
dst_uvs[3] = Point(1.0, 1.0);

vtx_builder.AddVertices({
{rect_.origin, dst_uvs[0], dst_uvs[0]},
{Point(rect_.origin.x + size.width, rect_.origin.y), dst_uvs[1],
dst_uvs[1]},
{Point(rect_.origin.x + size.width, rect_.origin.y + size.height),
dst_uvs[3], dst_uvs[3]},
{rect_.origin, dst_uvs[0], dst_uvs[0]},
{Point(rect_.origin.x + size.width, rect_.origin.y + size.height),
dst_uvs[3], dst_uvs[3]},
{Point(rect_.origin.x, rect_.origin.y + size.height), dst_uvs[2],
dst_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);

Command cmd;
cmd.label = "Foreground Advanced Blend Filter";
cmd.BindVertices(vtx_buffer);
cmd.stencil_reference = entity.GetStencilDepth();
auto options = OptionsFromPass(pass);

switch (blend_mode_) {
case BlendMode::kScreen:
cmd.pipeline = renderer.GetBlendScreenPipeline(options);
break;
case BlendMode::kOverlay:
cmd.pipeline = renderer.GetBlendOverlayPipeline(options);
break;
case BlendMode::kDarken:
cmd.pipeline = renderer.GetBlendDarkenPipeline(options);
break;
case BlendMode::kLighten:
cmd.pipeline = renderer.GetBlendLightenPipeline(options);
break;
case BlendMode::kColorDodge:
cmd.pipeline = renderer.GetBlendColorDodgePipeline(options);
break;
case BlendMode::kColorBurn:
cmd.pipeline = renderer.GetBlendColorBurnPipeline(options);
break;
case BlendMode::kHardLight:
cmd.pipeline = renderer.GetBlendHardLightPipeline(options);
break;
case BlendMode::kSoftLight:
cmd.pipeline = renderer.GetBlendSoftLightPipeline(options);
break;
case BlendMode::kDifference:
cmd.pipeline = renderer.GetBlendDifferencePipeline(options);
break;
case BlendMode::kExclusion:
cmd.pipeline = renderer.GetBlendExclusionPipeline(options);
break;
case BlendMode::kMultiply:
cmd.pipeline = renderer.GetBlendMultiplyPipeline(options);
break;
case BlendMode::kHue:
cmd.pipeline = renderer.GetBlendHuePipeline(options);
break;
case BlendMode::kSaturation:
cmd.pipeline = renderer.GetBlendSaturationPipeline(options);
break;
case BlendMode::kColor:
cmd.pipeline = renderer.GetBlendColorPipeline(options);
break;
case BlendMode::kLuminosity:
cmd.pipeline = renderer.GetBlendLuminosityPipeline(options);
break;
default:
return false;
}

FS::BlendInfo blend_info;
VS::FrameInfo frame_info;

auto dst_sampler_descriptor = dst_snapshot->sampler_descriptor;
if (renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);
frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
blend_info.dst_input_alpha = dst_snapshot->opacity;

blend_info.color_factor = 1;
blend_info.color = foreground_color_;
// This texture will not be sampled from due to the color factor. But
// this is present so that validation doesn't trip on a missing
// binding.
FS::BindTextureSamplerSrc(cmd, dst_snapshot->texture, dst_sampler);

auto blend_uniform = host_buffer.EmplaceUniform(blend_info);
FS::BindBlendInfo(cmd, blend_uniform);

frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());

auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);

return pass.AddCommand(cmd);
}

} // namespace impeller
50 changes: 50 additions & 0 deletions impeller/entity/contents/foreground_blend_contents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 "flutter/fml/macros.h"
#include "flutter/impeller/core/texture.h"
#include "flutter/impeller/entity/contents/color_source_contents.h"
#include "flutter/impeller/entity/contents/filters/inputs/filter_input.h"
#include "flutter/impeller/entity/entity.h"

namespace impeller {

/// @brief Optimized advanced blend that avoids a second subpass when there is
/// only a single input and a foreground color.
///
/// These contents cannot absorb opacity.
class AdvancedForegroundBlendContents : public Contents {
public:
AdvancedForegroundBlendContents();

~AdvancedForegroundBlendContents();

void SetBlendMode(BlendMode blend_mode);

void SetSrcInput(std::shared_ptr<FilterInput> input);

void SetForegroundColor(Color color);

void SetCoverage(Rect rect);

private:
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;

Color foreground_color_;
BlendMode blend_mode_;
std::shared_ptr<FilterInput> input_;
Rect rect_;

FML_DISALLOW_COPY_AND_ASSIGN(AdvancedForegroundBlendContents);
};

} // namespace impeller
16 changes: 16 additions & 0 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2451,5 +2451,21 @@ TEST_P(EntityTest, InheritOpacityTest) {
ASSERT_FALSE(runtime_effect->CanInheritOpacity(entity));
}

TEST_P(EntityTest, ColorFilterWithForegroundColorAdvancedBlend) {
auto image = CreateTextureForFixture("boston.jpg");
auto filter = ColorFilterContents::MakeBlend(
BlendMode::kColorBurn, FilterInput::Make({image}), Color::Red());

auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
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);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}

} // namespace testing
} // namespace impeller