[Impeller] Implement DlColorFilterType::kBlend (flutter/engine#33656)
This commit is contained in:
@@ -566,12 +566,12 @@ FILE: ../../../flutter/impeller/entity/entity_pass_delegate.h
|
||||
FILE: ../../../flutter/impeller/entity/entity_playground.cc
|
||||
FILE: ../../../flutter/impeller/entity/entity_playground.h
|
||||
FILE: ../../../flutter/impeller/entity/entity_unittests.cc
|
||||
FILE: ../../../flutter/impeller/entity/shaders/advanced_blend.vert
|
||||
FILE: ../../../flutter/impeller/entity/shaders/advanced_blend_colorburn.frag
|
||||
FILE: ../../../flutter/impeller/entity/shaders/advanced_blend_screen.frag
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blend.frag
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blend.vert
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blending.glsl
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colorburn.frag
|
||||
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_screen.frag
|
||||
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/gaussian_blur.frag
|
||||
|
||||
@@ -48,6 +48,11 @@ std::shared_ptr<Contents> Paint::WithFilters(
|
||||
input = filter(FilterInput::Make(input), is_solid_color_val);
|
||||
}
|
||||
|
||||
if (color_filter.has_value()) {
|
||||
const ColorFilterProc& filter = color_filter.value();
|
||||
input = filter(FilterInput::Make(input));
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ struct Paint {
|
||||
using MaskFilterProc =
|
||||
std::function<std::shared_ptr<FilterContents>(FilterInput::Ref,
|
||||
bool is_solid_color)>;
|
||||
using ColorFilterProc =
|
||||
std::function<std::shared_ptr<FilterContents>(FilterInput::Ref)>;
|
||||
|
||||
enum class Style {
|
||||
kFill,
|
||||
@@ -37,6 +39,7 @@ struct Paint {
|
||||
Entity::BlendMode blend_mode = Entity::BlendMode::kSourceOver;
|
||||
|
||||
std::optional<MaskFilterProc> mask_filter;
|
||||
std::optional<ColorFilterProc> color_filter;
|
||||
|
||||
/// @brief Wrap this paint's configured filters to the given contents.
|
||||
/// @param[in] input The contents to wrap with paint's filters.
|
||||
|
||||
@@ -5,8 +5,11 @@
|
||||
#include "impeller/display_list/display_list_dispatcher.h"
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "display_list/display_list_blend_mode.h"
|
||||
#include "display_list/display_list_path_effect.h"
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/entity/contents/filters/filter_contents.h"
|
||||
#include "impeller/entity/contents/linear_gradient_contents.h"
|
||||
@@ -17,6 +20,7 @@
|
||||
#include "impeller/geometry/scalar.h"
|
||||
#include "impeller/geometry/vertices.h"
|
||||
#include "impeller/typographer/backends/skia/text_frame_skia.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkColor.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -28,6 +32,48 @@ DisplayListDispatcher::DisplayListDispatcher() = default;
|
||||
|
||||
DisplayListDispatcher::~DisplayListDispatcher() = default;
|
||||
|
||||
static std::optional<Entity::BlendMode> ToBlendMode(flutter::DlBlendMode mode) {
|
||||
// TODO(100086): Implement remaining advanced blends.
|
||||
static const std::unordered_map<flutter::DlBlendMode,
|
||||
std::optional<Entity::BlendMode>>
|
||||
blends = {
|
||||
{flutter::DlBlendMode::kClear, Entity::BlendMode::kClear},
|
||||
{flutter::DlBlendMode::kSrc, Entity::BlendMode::kSource},
|
||||
{flutter::DlBlendMode::kDst, Entity::BlendMode::kDestination},
|
||||
{flutter::DlBlendMode::kSrcOver, Entity::BlendMode::kSourceOver},
|
||||
{flutter::DlBlendMode::kDstOver, Entity::BlendMode::kDestinationOver},
|
||||
{flutter::DlBlendMode::kSrcIn, Entity::BlendMode::kSourceIn},
|
||||
{flutter::DlBlendMode::kDstIn, Entity::BlendMode::kDestinationIn},
|
||||
{flutter::DlBlendMode::kSrcOut, Entity::BlendMode::kSourceOut},
|
||||
{flutter::DlBlendMode::kDstOut, Entity::BlendMode::kDestinationOut},
|
||||
{flutter::DlBlendMode::kSrcATop, Entity::BlendMode::kSourceATop},
|
||||
{flutter::DlBlendMode::kDstATop, Entity::BlendMode::kDestinationATop},
|
||||
{flutter::DlBlendMode::kXor, Entity::BlendMode::kXor},
|
||||
{flutter::DlBlendMode::kPlus, Entity::BlendMode::kPlus},
|
||||
{flutter::DlBlendMode::kModulate, Entity::BlendMode::kModulate},
|
||||
{flutter::DlBlendMode::kScreen, Entity::BlendMode::kScreen},
|
||||
{flutter::DlBlendMode::kOverlay, std::nullopt},
|
||||
{flutter::DlBlendMode::kDarken, std::nullopt},
|
||||
{flutter::DlBlendMode::kLighten, std::nullopt},
|
||||
{flutter::DlBlendMode::kColorDodge, std::nullopt},
|
||||
{flutter::DlBlendMode::kColorBurn, Entity::BlendMode::kColorBurn},
|
||||
{flutter::DlBlendMode::kHardLight, std::nullopt},
|
||||
{flutter::DlBlendMode::kSoftLight, std::nullopt},
|
||||
{flutter::DlBlendMode::kDifference, std::nullopt},
|
||||
{flutter::DlBlendMode::kExclusion, std::nullopt},
|
||||
{flutter::DlBlendMode::kMultiply, std::nullopt},
|
||||
{flutter::DlBlendMode::kHue, std::nullopt},
|
||||
{flutter::DlBlendMode::kSaturation, std::nullopt},
|
||||
{flutter::DlBlendMode::kColor, std::nullopt},
|
||||
{flutter::DlBlendMode::kLuminosity, std::nullopt},
|
||||
};
|
||||
FML_DCHECK(blends.size() ==
|
||||
static_cast<size_t>(Entity::BlendMode::kLastAdvancedBlendMode) +
|
||||
1);
|
||||
|
||||
return blends.at(mode);
|
||||
}
|
||||
|
||||
// |flutter::Dispatcher|
|
||||
void DisplayListDispatcher::setAntiAlias(bool aa) {
|
||||
// Nothing to do because AA is implicit.
|
||||
@@ -167,10 +213,28 @@ void DisplayListDispatcher::setColorFilter(
|
||||
// Needs https://github.com/flutter/flutter/issues/95434
|
||||
if (filter == nullptr) {
|
||||
// Reset everything
|
||||
paint_.color_filter = std::nullopt;
|
||||
return;
|
||||
}
|
||||
switch (filter->type()) {
|
||||
case flutter::DlColorFilterType::kBlend:
|
||||
case flutter::DlColorFilterType::kBlend: {
|
||||
auto dl_blend = filter->asBlend();
|
||||
|
||||
auto blend_mode = ToBlendMode(dl_blend->mode());
|
||||
if (!blend_mode.has_value()) {
|
||||
// TODO(100086): Implement remaining advanced blends.
|
||||
UNIMPLEMENTED;
|
||||
blend_mode = Entity::BlendMode::kSourceOver;
|
||||
}
|
||||
|
||||
auto color = ToColor(dl_blend->color());
|
||||
|
||||
paint_.color_filter = [blend_mode = blend_mode.value(),
|
||||
color](FilterInput::Ref input) {
|
||||
return FilterContents::MakeBlend(blend_mode, {input}, color);
|
||||
};
|
||||
return;
|
||||
}
|
||||
case flutter::DlColorFilterType::kMatrix:
|
||||
case flutter::DlColorFilterType::kSrgbToLinearGamma:
|
||||
case flutter::DlColorFilterType::kLinearToSrgbGamma:
|
||||
@@ -185,59 +249,6 @@ void DisplayListDispatcher::setInvertColors(bool invert) {
|
||||
UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
static std::optional<Entity::BlendMode> ToBlendMode(flutter::DlBlendMode mode) {
|
||||
switch (mode) {
|
||||
case flutter::DlBlendMode::kClear:
|
||||
return Entity::BlendMode::kClear;
|
||||
case flutter::DlBlendMode::kSrc:
|
||||
return Entity::BlendMode::kSource;
|
||||
case flutter::DlBlendMode::kDst:
|
||||
return Entity::BlendMode::kDestination;
|
||||
case flutter::DlBlendMode::kSrcOver:
|
||||
return Entity::BlendMode::kSourceOver;
|
||||
case flutter::DlBlendMode::kDstOver:
|
||||
return Entity::BlendMode::kDestinationOver;
|
||||
case flutter::DlBlendMode::kSrcIn:
|
||||
return Entity::BlendMode::kSourceIn;
|
||||
case flutter::DlBlendMode::kDstIn:
|
||||
return Entity::BlendMode::kDestinationIn;
|
||||
case flutter::DlBlendMode::kSrcOut:
|
||||
return Entity::BlendMode::kSourceOut;
|
||||
case flutter::DlBlendMode::kDstOut:
|
||||
return Entity::BlendMode::kDestinationOut;
|
||||
case flutter::DlBlendMode::kSrcATop:
|
||||
return Entity::BlendMode::kSourceATop;
|
||||
case flutter::DlBlendMode::kDstATop:
|
||||
return Entity::BlendMode::kDestinationATop;
|
||||
case flutter::DlBlendMode::kXor:
|
||||
return Entity::BlendMode::kXor;
|
||||
case flutter::DlBlendMode::kPlus:
|
||||
return Entity::BlendMode::kPlus;
|
||||
case flutter::DlBlendMode::kModulate:
|
||||
return Entity::BlendMode::kModulate;
|
||||
case flutter::DlBlendMode::kScreen:
|
||||
return Entity::BlendMode::kScreen;
|
||||
case flutter::DlBlendMode::kColorBurn:
|
||||
return Entity::BlendMode::kColorBurn;
|
||||
case flutter::DlBlendMode::kOverlay:
|
||||
case flutter::DlBlendMode::kDarken:
|
||||
case flutter::DlBlendMode::kLighten:
|
||||
case flutter::DlBlendMode::kColorDodge:
|
||||
case flutter::DlBlendMode::kHardLight:
|
||||
case flutter::DlBlendMode::kSoftLight:
|
||||
case flutter::DlBlendMode::kDifference:
|
||||
case flutter::DlBlendMode::kExclusion:
|
||||
case flutter::DlBlendMode::kMultiply:
|
||||
case flutter::DlBlendMode::kHue:
|
||||
case flutter::DlBlendMode::kSaturation:
|
||||
case flutter::DlBlendMode::kColor:
|
||||
case flutter::DlBlendMode::kLuminosity:
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// |flutter::Dispatcher|
|
||||
void DisplayListDispatcher::setBlendMode(flutter::DlBlendMode dl_mode) {
|
||||
if (auto mode = ToBlendMode(dl_mode); mode.has_value()) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "display_list/display_list_blend_mode.h"
|
||||
#include "display_list/display_list_color_filter.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "third_party/imgui/imgui.h"
|
||||
#include "third_party/skia/include/core/SkColor.h"
|
||||
@@ -222,5 +224,30 @@ TEST_P(DisplayListTest, CanDrawWithMaskBlur) {
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawWithBlendColorFilter) {
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
|
||||
// Pipeline blended image.
|
||||
{
|
||||
auto filter = flutter::DlBlendColorFilter(SK_ColorYELLOW,
|
||||
flutter::DlBlendMode::kModulate);
|
||||
builder.setColorFilter(&filter);
|
||||
builder.drawImage(DlImageImpeller::Make(texture), SkPoint::Make(100, 100),
|
||||
SkSamplingOptions{}, true);
|
||||
}
|
||||
|
||||
// Advanced blended image.
|
||||
{
|
||||
auto filter =
|
||||
flutter::DlBlendColorFilter(SK_ColorRED, flutter::DlBlendMode::kScreen);
|
||||
builder.setColorFilter(&filter);
|
||||
builder.drawImage(DlImageImpeller::Make(texture), SkPoint::Make(250, 250),
|
||||
SkSamplingOptions{}, true);
|
||||
}
|
||||
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
@@ -8,11 +8,11 @@ impeller_shaders("entity_shaders") {
|
||||
name = "entity"
|
||||
|
||||
shaders = [
|
||||
"shaders/advanced_blend.vert",
|
||||
"shaders/advanced_blend_colorburn.frag",
|
||||
"shaders/advanced_blend_screen.frag",
|
||||
"shaders/blend.frag",
|
||||
"shaders/blend.vert",
|
||||
"shaders/blending/advanced_blend.vert",
|
||||
"shaders/blending/advanced_blend_colorburn.frag",
|
||||
"shaders/blending/advanced_blend_screen.frag",
|
||||
"shaders/blending/blend.frag",
|
||||
"shaders/blending/blend.vert",
|
||||
"shaders/border_mask_blur.frag",
|
||||
"shaders/border_mask_blur.vert",
|
||||
"shaders/gaussian_blur.frag",
|
||||
|
||||
@@ -4,9 +4,15 @@
|
||||
|
||||
#include "impeller/entity/contents/filters/blend_filter_contents.h"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#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/solid_color_contents.h"
|
||||
#include "impeller/geometry/path_builder.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/sampler_library.h"
|
||||
|
||||
@@ -27,15 +33,18 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
|
||||
const Entity& entity,
|
||||
RenderPass& pass,
|
||||
const Rect& coverage,
|
||||
std::optional<Color> foreground_color,
|
||||
PipelineProc pipeline_proc) {
|
||||
using VS = typename TPipeline::VertexShader;
|
||||
using FS = typename TPipeline::FragmentShader;
|
||||
|
||||
if (inputs.size() < 2) {
|
||||
const size_t total_inputs =
|
||||
inputs.size() + (foreground_color.has_value() ? 1 : 0);
|
||||
if (total_inputs < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto dst_snapshot = inputs[1]->GetSnapshot(renderer, entity);
|
||||
auto dst_snapshot = inputs[0]->GetSnapshot(renderer, entity);
|
||||
if (!dst_snapshot.has_value()) {
|
||||
return true;
|
||||
}
|
||||
@@ -45,15 +54,19 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
|
||||
}
|
||||
auto dst_uvs = maybe_dst_uvs.value();
|
||||
|
||||
auto src_snapshot = inputs[0]->GetSnapshot(renderer, entity);
|
||||
if (!src_snapshot.has_value()) {
|
||||
return true;
|
||||
std::optional<Snapshot> src_snapshot;
|
||||
std::array<Point, 4> src_uvs;
|
||||
if (!foreground_color.has_value()) {
|
||||
src_snapshot = inputs[1]->GetSnapshot(renderer, entity);
|
||||
if (!src_snapshot.has_value()) {
|
||||
return true;
|
||||
}
|
||||
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
|
||||
if (!maybe_src_uvs.has_value()) {
|
||||
return true;
|
||||
}
|
||||
src_uvs = maybe_src_uvs.value();
|
||||
}
|
||||
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
|
||||
if (!maybe_src_uvs.has_value()) {
|
||||
return true;
|
||||
}
|
||||
auto src_uvs = maybe_src_uvs.value();
|
||||
|
||||
auto& host_buffer = pass.GetTransientsBuffer();
|
||||
|
||||
@@ -80,7 +93,17 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
|
||||
|
||||
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
|
||||
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, sampler);
|
||||
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, sampler);
|
||||
|
||||
typename FS::BlendInfo blend_info;
|
||||
if (foreground_color.has_value()) {
|
||||
blend_info.color_factor = 1;
|
||||
blend_info.color = foreground_color.value();
|
||||
} else {
|
||||
blend_info.color_factor = 0;
|
||||
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, sampler);
|
||||
}
|
||||
auto blend_uniform = host_buffer.EmplaceUniform(blend_info);
|
||||
FS::BindBlendInfo(cmd, blend_uniform);
|
||||
|
||||
typename VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(size);
|
||||
@@ -92,51 +115,13 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
|
||||
return true;
|
||||
}
|
||||
|
||||
void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
|
||||
if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
|
||||
<< " assigned to BlendFilterContents.";
|
||||
}
|
||||
|
||||
blend_mode_ = blend_mode;
|
||||
|
||||
if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
static_assert(Entity::BlendMode::kLastAdvancedBlendMode ==
|
||||
Entity::BlendMode::kColorBurn);
|
||||
|
||||
switch (blend_mode) {
|
||||
case Entity::BlendMode::kScreen:
|
||||
advanced_blend_proc_ = [](const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity, RenderPass& pass,
|
||||
const Rect& coverage) {
|
||||
PipelineProc p = &ContentContext::GetBlendScreenPipeline;
|
||||
return AdvancedBlend<BlendScreenPipeline>(inputs, renderer, entity,
|
||||
pass, coverage, p);
|
||||
};
|
||||
break;
|
||||
case Entity::BlendMode::kColorBurn:
|
||||
advanced_blend_proc_ = [](const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity, RenderPass& pass,
|
||||
const Rect& coverage) {
|
||||
PipelineProc p = &ContentContext::GetBlendColorburnPipeline;
|
||||
return AdvancedBlend<BlendColorburnPipeline>(inputs, renderer, entity,
|
||||
pass, coverage, p);
|
||||
};
|
||||
break;
|
||||
default:
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool BasicBlend(const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
RenderPass& pass,
|
||||
const Rect& coverage,
|
||||
Entity::BlendMode basic_blend) {
|
||||
static bool PipelineBlend(const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
RenderPass& pass,
|
||||
const Rect& coverage,
|
||||
Entity::BlendMode pipeline_blend,
|
||||
std::optional<Color> foreground_color) {
|
||||
using VS = BlendPipeline::VertexShader;
|
||||
using FS = BlendPipeline::FragmentShader;
|
||||
|
||||
@@ -145,7 +130,7 @@ static bool BasicBlend(const FilterInput::Vector& inputs,
|
||||
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "Basic Blend Filter";
|
||||
cmd.label = "Pipeline Blend Filter";
|
||||
auto options = OptionsFromPass(pass);
|
||||
|
||||
auto add_blend_command = [&](std::optional<Snapshot> input) {
|
||||
@@ -192,26 +177,87 @@ static bool BasicBlend(const FilterInput::Vector& inputs,
|
||||
return true;
|
||||
}
|
||||
|
||||
if (inputs.size() < 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Write subsequent textures using the selected blend mode.
|
||||
|
||||
options.blend_mode = basic_blend;
|
||||
cmd.pipeline = renderer.GetBlendPipeline(options);
|
||||
if (inputs.size() >= 2) {
|
||||
options.blend_mode = pipeline_blend;
|
||||
cmd.pipeline = renderer.GetBlendPipeline(options);
|
||||
|
||||
for (auto texture_i = inputs.begin() + 1; texture_i < inputs.end();
|
||||
texture_i++) {
|
||||
auto input = texture_i->get()->GetSnapshot(renderer, entity);
|
||||
if (!add_blend_command(input)) {
|
||||
return true;
|
||||
for (auto texture_i = inputs.begin() + 1; texture_i < inputs.end();
|
||||
texture_i++) {
|
||||
auto input = texture_i->get()->GetSnapshot(renderer, entity);
|
||||
if (!add_blend_command(input)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a foreground color is set, blend it in.
|
||||
|
||||
if (foreground_color.has_value()) {
|
||||
auto contents = std::make_shared<SolidColorContents>();
|
||||
contents->SetPath(
|
||||
PathBuilder{}
|
||||
.AddRect(Rect::MakeSize(Size(pass.GetRenderTargetSize())))
|
||||
.TakePath());
|
||||
contents->SetColor(foreground_color.value());
|
||||
|
||||
Entity foreground_entity;
|
||||
foreground_entity.SetBlendMode(pipeline_blend);
|
||||
foreground_entity.SetContents(contents);
|
||||
if (!foreground_entity.Render(renderer, pass)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
|
||||
if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
|
||||
<< " assigned to BlendFilterContents.";
|
||||
}
|
||||
|
||||
blend_mode_ = blend_mode;
|
||||
|
||||
if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
static_assert(Entity::BlendMode::kLastAdvancedBlendMode ==
|
||||
Entity::BlendMode::kColorBurn);
|
||||
|
||||
switch (blend_mode) {
|
||||
case Entity::BlendMode::kScreen:
|
||||
advanced_blend_proc_ = [](const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity, RenderPass& pass,
|
||||
const Rect& coverage,
|
||||
std::optional<Color> fg_color) {
|
||||
PipelineProc p = &ContentContext::GetBlendScreenPipeline;
|
||||
return AdvancedBlend<BlendScreenPipeline>(
|
||||
inputs, renderer, entity, pass, coverage, fg_color, p);
|
||||
};
|
||||
break;
|
||||
case Entity::BlendMode::kColorBurn:
|
||||
advanced_blend_proc_ = [](const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity, RenderPass& pass,
|
||||
const Rect& coverage,
|
||||
std::optional<Color> fg_color) {
|
||||
PipelineProc p = &ContentContext::GetBlendColorburnPipeline;
|
||||
return AdvancedBlend<BlendColorburnPipeline>(
|
||||
inputs, renderer, entity, pass, coverage, fg_color, p);
|
||||
};
|
||||
break;
|
||||
default:
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BlendFilterContents::SetForegroundColor(std::optional<Color> color) {
|
||||
foreground_color_ = color;
|
||||
}
|
||||
|
||||
bool BlendFilterContents::RenderFilter(const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
@@ -221,18 +267,20 @@ bool BlendFilterContents::RenderFilter(const FilterInput::Vector& inputs,
|
||||
return true;
|
||||
}
|
||||
|
||||
if (inputs.size() == 1) {
|
||||
if (inputs.size() == 1 && !foreground_color_.has_value()) {
|
||||
// Nothing to blend.
|
||||
return BasicBlend(inputs, renderer, entity, pass, coverage,
|
||||
Entity::BlendMode::kSource);
|
||||
return PipelineBlend(inputs, renderer, entity, pass, coverage,
|
||||
Entity::BlendMode::kSource, std::nullopt);
|
||||
}
|
||||
|
||||
if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
return BasicBlend(inputs, renderer, entity, pass, coverage, blend_mode_);
|
||||
return PipelineBlend(inputs, renderer, entity, pass, coverage, blend_mode_,
|
||||
foreground_color_);
|
||||
}
|
||||
|
||||
if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
return advanced_blend_proc_(inputs, renderer, entity, pass, coverage);
|
||||
return advanced_blend_proc_(inputs, renderer, entity, pass, coverage,
|
||||
foreground_color_);
|
||||
}
|
||||
|
||||
FML_UNREACHABLE();
|
||||
|
||||
@@ -16,7 +16,8 @@ class BlendFilterContents : public FilterContents {
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
RenderPass& pass,
|
||||
const Rect& coverage)>;
|
||||
const Rect& coverage,
|
||||
std::optional<Color> foreground_color)>;
|
||||
|
||||
BlendFilterContents();
|
||||
|
||||
@@ -24,6 +25,10 @@ class BlendFilterContents : public FilterContents {
|
||||
|
||||
void SetBlendMode(Entity::BlendMode blend_mode);
|
||||
|
||||
/// @brief Sets a source color which is blended after all of the inputs have
|
||||
/// been blended.
|
||||
void SetForegroundColor(std::optional<Color> color);
|
||||
|
||||
private:
|
||||
// |FilterContents|
|
||||
bool RenderFilter(const FilterInput::Vector& inputs,
|
||||
@@ -32,8 +37,9 @@ class BlendFilterContents : public FilterContents {
|
||||
RenderPass& pass,
|
||||
const Rect& coverage) const override;
|
||||
|
||||
Entity::BlendMode blend_mode_;
|
||||
Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver;
|
||||
AdvancedBlendProc advanced_blend_proc_;
|
||||
std::optional<Color> foreground_color_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(BlendFilterContents);
|
||||
};
|
||||
|
||||
@@ -29,38 +29,44 @@ namespace impeller {
|
||||
|
||||
std::shared_ptr<FilterContents> FilterContents::MakeBlend(
|
||||
Entity::BlendMode blend_mode,
|
||||
FilterInput::Vector inputs) {
|
||||
FilterInput::Vector inputs,
|
||||
std::optional<Color> foreground_color) {
|
||||
if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
|
||||
<< " passed to FilterContents::MakeBlend.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (inputs.size() < 2 ||
|
||||
size_t total_inputs = inputs.size() + (foreground_color.has_value() ? 1 : 0);
|
||||
if (total_inputs < 2 ||
|
||||
blend_mode <= Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
auto blend = std::make_shared<BlendFilterContents>();
|
||||
blend->SetInputs(inputs);
|
||||
blend->SetBlendMode(blend_mode);
|
||||
blend->SetForegroundColor(foreground_color);
|
||||
return blend;
|
||||
}
|
||||
|
||||
if (blend_mode <= Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
auto blend_input = inputs[0];
|
||||
std::shared_ptr<BlendFilterContents> new_blend;
|
||||
for (auto in_i = inputs.begin() + 1; in_i < inputs.end(); in_i++) {
|
||||
new_blend = std::make_shared<BlendFilterContents>();
|
||||
new_blend->SetInputs({blend_input, *in_i});
|
||||
new_blend->SetBlendMode(blend_mode);
|
||||
if (in_i < inputs.end() - 1) {
|
||||
blend_input = FilterInput::Make(
|
||||
std::static_pointer_cast<FilterContents>(new_blend));
|
||||
}
|
||||
auto blend_input = inputs[0];
|
||||
std::shared_ptr<BlendFilterContents> new_blend;
|
||||
for (auto in_i = inputs.begin() + 1; in_i < inputs.end(); in_i++) {
|
||||
new_blend = std::make_shared<BlendFilterContents>();
|
||||
new_blend->SetInputs({*in_i, blend_input});
|
||||
new_blend->SetBlendMode(blend_mode);
|
||||
if (in_i < inputs.end() - 1 || foreground_color.has_value()) {
|
||||
blend_input = FilterInput::Make(
|
||||
std::static_pointer_cast<FilterContents>(new_blend));
|
||||
}
|
||||
// new_blend will always be assigned because inputs.size() >= 2.
|
||||
return new_blend;
|
||||
}
|
||||
|
||||
FML_UNREACHABLE();
|
||||
if (foreground_color.has_value()) {
|
||||
new_blend = std::make_shared<BlendFilterContents>();
|
||||
new_blend->SetInputs({blend_input});
|
||||
new_blend->SetBlendMode(blend_mode);
|
||||
new_blend->SetForegroundColor(foreground_color);
|
||||
}
|
||||
|
||||
return new_blend;
|
||||
}
|
||||
|
||||
std::shared_ptr<FilterContents> FilterContents::MakeDirectionalGaussianBlur(
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
@@ -80,8 +81,10 @@ class FilterContents : public Contents {
|
||||
};
|
||||
};
|
||||
|
||||
static std::shared_ptr<FilterContents> MakeBlend(Entity::BlendMode blend_mode,
|
||||
FilterInput::Vector inputs);
|
||||
static std::shared_ptr<FilterContents> MakeBlend(
|
||||
Entity::BlendMode blend_mode,
|
||||
FilterInput::Vector inputs,
|
||||
std::optional<Color> foreground_color = std::nullopt);
|
||||
|
||||
static std::shared_ptr<FilterContents> MakeDirectionalGaussianBlur(
|
||||
FilterInput::Ref input,
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
// 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 "blending.glsl"
|
||||
|
||||
uniform sampler2D texture_sampler_dst;
|
||||
uniform sampler2D texture_sampler_src;
|
||||
|
||||
in vec2 v_dst_texture_coords;
|
||||
in vec2 v_src_texture_coords;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
vec3 ComponentIsValue(vec3 n, float value) {
|
||||
return vec3(n.r == value, n.g == value, n.b == value);
|
||||
}
|
||||
|
||||
void main() {
|
||||
const vec4 dst = Unpremultiply(
|
||||
SampleWithBorder(texture_sampler_dst, v_dst_texture_coords));
|
||||
const vec4 src = Unpremultiply(
|
||||
SampleWithBorder(texture_sampler_src, v_src_texture_coords));
|
||||
|
||||
vec3 color = 1 - min(vec3(1), (1 - dst.rgb) / src.rgb);
|
||||
color = mix(color, vec3(1), ComponentIsValue(dst.rgb, 1.0));
|
||||
color = mix(color, vec3(0), ComponentIsValue(src.rgb, 0.0));
|
||||
|
||||
frag_color = vec4(color * src.a, src.a);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
// 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 "blending.glsl"
|
||||
|
||||
uniform sampler2D texture_sampler_dst;
|
||||
uniform sampler2D texture_sampler_src;
|
||||
|
||||
in vec2 v_dst_texture_coords;
|
||||
in vec2 v_src_texture_coords;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
const vec4 dst = Unpremultiply(
|
||||
SampleWithBorder(texture_sampler_dst, v_dst_texture_coords));
|
||||
const vec4 src = Unpremultiply(
|
||||
SampleWithBorder(texture_sampler_src, v_src_texture_coords));
|
||||
|
||||
vec3 color = dst.rgb + src.rgb - (dst.rgb * src.rgb);
|
||||
frag_color = vec4(color * src.a, src.a);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// Emulate SamplerAddressMode::ClampToBorder.
|
||||
vec4 SampleWithBorder(sampler2D tex, vec2 uv) {
|
||||
if (uv.x > 0 && uv.y > 0 && uv.x < 1 && uv.y < 1) {
|
||||
return texture(tex, uv);
|
||||
}
|
||||
return vec4(0);
|
||||
}
|
||||
|
||||
vec4 Unpremultiply(vec4 color) {
|
||||
if (color.a == 0) {
|
||||
return vec4(0);
|
||||
}
|
||||
return vec4(color.rgb / color.a, color.a);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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 BlendInfo {
|
||||
float color_factor;
|
||||
vec4 color; // This color input is expected to be unpremultiplied.
|
||||
}
|
||||
blend_info;
|
||||
|
||||
uniform sampler2D texture_sampler_dst;
|
||||
uniform sampler2D texture_sampler_src;
|
||||
|
||||
in vec2 v_dst_texture_coords;
|
||||
in vec2 v_src_texture_coords;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
// Emulate SamplerAddressMode::ClampToBorder.
|
||||
vec4 SampleWithBorder(sampler2D tex, vec2 uv) {
|
||||
if (uv.x > 0 && uv.y > 0 && uv.x < 1 && uv.y < 1) {
|
||||
return texture(tex, uv);
|
||||
}
|
||||
return vec4(0);
|
||||
}
|
||||
|
||||
vec4 Unpremultiply(vec4 color) {
|
||||
if (color.a == 0) {
|
||||
return vec4(0);
|
||||
}
|
||||
return vec4(color.rgb / color.a, color.a);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec4 dst = Unpremultiply(
|
||||
SampleWithBorder(texture_sampler_dst, v_dst_texture_coords));
|
||||
vec4 src = blend_info.color_factor > 0
|
||||
? blend_info.color
|
||||
: Unpremultiply(SampleWithBorder(texture_sampler_src,
|
||||
v_src_texture_coords));
|
||||
|
||||
vec3 blended = Blend(dst.rgb, src.rgb);
|
||||
|
||||
frag_color = vec4(blended * src.a, src.a);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
vec3 ComponentIsValue(vec3 n, float value) {
|
||||
return vec3(n.r == value, n.g == value, n.b == value);
|
||||
}
|
||||
|
||||
vec3 Blend(vec3 dst, vec3 src) {
|
||||
vec3 color = 1 - min(vec3(1), (1 - dst) / src);
|
||||
color = mix(color, vec3(1), ComponentIsValue(dst, 1.0));
|
||||
color = mix(color, vec3(0), ComponentIsValue(src, 0.0));
|
||||
return color;
|
||||
}
|
||||
|
||||
#include "advanced_blend.glsl"
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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.
|
||||
|
||||
vec3 Blend(vec3 dst, vec3 src) {
|
||||
return dst + src - (dst * src);
|
||||
}
|
||||
|
||||
#include "advanced_blend.glsl"
|
||||
Reference in New Issue
Block a user