[Impeller] Add non-separable blend modes (flutter/engine#33862)

This commit is contained in:
Brandon DeRosier
2022-06-07 13:21:29 -07:00
committed by GitHub
parent 7c2d495b95
commit e2d5740ee7
14 changed files with 181 additions and 35 deletions

View File

@@ -570,15 +570,19 @@ FILE: ../../../flutter/impeller/entity/entity_playground.h
FILE: ../../../flutter/impeller/entity/entity_unittests.cc
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_color.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colorburn.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colordodge.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_darken.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_difference.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_exclusion.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_lighten.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_multiply.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_overlay.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_screen.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_softlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl

View File

@@ -10,6 +10,7 @@
#include "impeller/aiks/aiks_playground.h"
#include "impeller/aiks/canvas.h"
#include "impeller/aiks/image.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/geometry_unittests.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/playground/widgets.h"
@@ -544,6 +545,10 @@ TEST_P(AiksTest, ColorWheel) {
{"Difference", Entity::BlendMode::kDifference},
{"Exclusion", Entity::BlendMode::kExclusion},
{"Multiply", Entity::BlendMode::kMultiply},
{"Hue", Entity::BlendMode::kHue},
{"Saturation", Entity::BlendMode::kSaturation},
{"Color", Entity::BlendMode::kColor},
{"Luminosity", Entity::BlendMode::kLuminosity},
};
assert(blends.size() ==
static_cast<size_t>(Entity::BlendMode::kLastAdvancedBlendMode) + 1);
@@ -594,19 +599,25 @@ TEST_P(AiksTest, ColorWheel) {
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
if (first_frame) {
first_frame = false;
ImGui::SetNextWindowSize({350, 200});
ImGui::SetNextWindowPos({325, 550});
ImGui::SetNextWindowSize({350, 260});
ImGui::SetNextWindowPos({25, 25});
}
// UI state.
static int current_blend_index = 3;
static float alpha = 1;
static Color color0 = Color::Red();
static Color color1 = Color::Green();
static Color color2 = Color::Blue();
ImGui::Begin("Controls");
{
ImGui::ListBox("Blending mode", &current_blend_index,
blend_mode_names.data(), blend_mode_names.size());
ImGui::SliderFloat("Alpha", &alpha, 0, 1);
ImGui::ColorEdit4("Color A", reinterpret_cast<float*>(&color0));
ImGui::ColorEdit4("Color B", reinterpret_cast<float*>(&color1));
ImGui::ColorEdit4("Color C", reinterpret_cast<float*>(&color2));
}
ImGui::End();
@@ -629,11 +640,11 @@ TEST_P(AiksTest, ColorWheel) {
paint.blend_mode = Entity::BlendMode::kPlus;
const Scalar x = std::sin(k2Pi / 3);
const Scalar y = -std::cos(k2Pi / 3);
paint.color = Color::Red();
paint.color = color0;
canvas.DrawCircle(Point(-x, y) * 45, 65, paint);
paint.color = Color::Green();
paint.color = color1;
canvas.DrawCircle(Point(0, -1) * 45, 65, paint);
paint.color = Color::Blue();
paint.color = color2;
canvas.DrawCircle(Point(x, y) * 45, 65, paint);
}
canvas.Restore();

View File

@@ -33,8 +33,7 @@ DisplayListDispatcher::DisplayListDispatcher() = default;
DisplayListDispatcher::~DisplayListDispatcher() = default;
static std::optional<Entity::BlendMode> ToBlendMode(flutter::DlBlendMode mode) {
// TODO(100086): Implement remaining advanced blends.
static Entity::BlendMode ToBlendMode(flutter::DlBlendMode mode) {
switch (mode) {
case flutter::DlBlendMode::kClear:
return Entity::BlendMode::kClear;
@@ -87,10 +86,13 @@ static std::optional<Entity::BlendMode> ToBlendMode(flutter::DlBlendMode mode) {
case flutter::DlBlendMode::kMultiply:
return Entity::BlendMode::kMultiply;
case flutter::DlBlendMode::kHue:
return Entity::BlendMode::kHue;
case flutter::DlBlendMode::kSaturation:
return Entity::BlendMode::kSaturation;
case flutter::DlBlendMode::kColor:
return Entity::BlendMode::kColor;
case flutter::DlBlendMode::kLuminosity:
return std::nullopt;
return Entity::BlendMode::kLuminosity;
}
FML_UNREACHABLE();
}
@@ -242,16 +244,9 @@ void DisplayListDispatcher::setColorFilter(
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) {
paint_.color_filter = [blend_mode, color](FilterInput::Ref input) {
return FilterContents::MakeBlend(blend_mode, {input}, color);
};
return;
@@ -272,12 +267,7 @@ void DisplayListDispatcher::setInvertColors(bool invert) {
// |flutter::Dispatcher|
void DisplayListDispatcher::setBlendMode(flutter::DlBlendMode dl_mode) {
if (auto mode = ToBlendMode(dl_mode); mode.has_value()) {
paint_.blend_mode = mode.value();
} else {
UNIMPLEMENTED;
paint_.blend_mode = Entity::BlendMode::kSourceOver;
}
paint_.blend_mode = ToBlendMode(dl_mode);
}
// |flutter::Dispatcher|
@@ -643,11 +633,7 @@ void DisplayListDispatcher::drawColor(flutter::DlColor color,
flutter::DlBlendMode dl_mode) {
Paint paint;
paint.color = ToColor(color);
if (auto mode = ToBlendMode(dl_mode); mode.has_value()) {
paint.blend_mode = mode.value();
} else {
FML_DLOG(ERROR) << "Unimplemented blend mode in " << __FUNCTION__;
}
paint.blend_mode = ToBlendMode(dl_mode);
canvas_.DrawPaint(paint);
}
@@ -727,11 +713,7 @@ void DisplayListDispatcher::drawSkVertices(const sk_sp<SkVertices> vertices,
// |flutter::Dispatcher|
void DisplayListDispatcher::drawVertices(const flutter::DlVertices* vertices,
flutter::DlBlendMode dl_mode) {
if (auto mode = ToBlendMode(dl_mode); mode.has_value()) {
canvas_.DrawVertices(ToVertices(vertices), mode.value(), paint_);
} else {
FML_DLOG(ERROR) << "Unimplemented blend mode in " << __FUNCTION__;
}
canvas_.DrawVertices(ToVertices(vertices), ToBlendMode(dl_mode), paint_);
}
// |flutter::Dispatcher|

View File

@@ -9,15 +9,19 @@ impeller_shaders("entity_shaders") {
shaders = [
"shaders/blending/advanced_blend.vert",
"shaders/blending/advanced_blend_color.frag",
"shaders/blending/advanced_blend_colorburn.frag",
"shaders/blending/advanced_blend_colordodge.frag",
"shaders/blending/advanced_blend_darken.frag",
"shaders/blending/advanced_blend_difference.frag",
"shaders/blending/advanced_blend_exclusion.frag",
"shaders/blending/advanced_blend_hardlight.frag",
"shaders/blending/advanced_blend_hue.frag",
"shaders/blending/advanced_blend_lighten.frag",
"shaders/blending/advanced_blend_luminosity.frag",
"shaders/blending/advanced_blend_multiply.frag",
"shaders/blending/advanced_blend_overlay.frag",
"shaders/blending/advanced_blend_saturation.frag",
"shaders/blending/advanced_blend_screen.frag",
"shaders/blending/advanced_blend_softlight.frag",
"shaders/blending/blend.frag",

View File

@@ -155,6 +155,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
CreateDefaultPipeline<SolidFillPipeline>(*context_);
texture_blend_pipelines_[{}] =
CreateDefaultPipeline<BlendPipeline>(*context_);
blend_color_pipelines_[{}] =
CreateDefaultPipeline<BlendColorPipeline>(*context_);
blend_colorburn_pipelines_[{}] =
CreateDefaultPipeline<BlendColorBurnPipeline>(*context_);
blend_colordodge_pipelines_[{}] =
@@ -167,12 +169,17 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
CreateDefaultPipeline<BlendExclusionPipeline>(*context_);
blend_hardlight_pipelines_[{}] =
CreateDefaultPipeline<BlendHardLightPipeline>(*context_);
blend_hue_pipelines_[{}] = CreateDefaultPipeline<BlendHuePipeline>(*context_);
blend_lighten_pipelines_[{}] =
CreateDefaultPipeline<BlendLightenPipeline>(*context_);
blend_luminosity_pipelines_[{}] =
CreateDefaultPipeline<BlendLuminosityPipeline>(*context_);
blend_multiply_pipelines_[{}] =
CreateDefaultPipeline<BlendMultiplyPipeline>(*context_);
blend_overlay_pipelines_[{}] =
CreateDefaultPipeline<BlendOverlayPipeline>(*context_);
blend_saturation_pipelines_[{}] =
CreateDefaultPipeline<BlendSaturationPipeline>(*context_);
blend_screen_pipelines_[{}] =
CreateDefaultPipeline<BlendScreenPipeline>(*context_);
blend_softlight_pipelines_[{}] =

View File

@@ -12,15 +12,19 @@
#include "fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/entity/advanced_blend.vert.h"
#include "impeller/entity/advanced_blend_color.frag.h"
#include "impeller/entity/advanced_blend_colorburn.frag.h"
#include "impeller/entity/advanced_blend_colordodge.frag.h"
#include "impeller/entity/advanced_blend_darken.frag.h"
#include "impeller/entity/advanced_blend_difference.frag.h"
#include "impeller/entity/advanced_blend_exclusion.frag.h"
#include "impeller/entity/advanced_blend_hardlight.frag.h"
#include "impeller/entity/advanced_blend_hue.frag.h"
#include "impeller/entity/advanced_blend_lighten.frag.h"
#include "impeller/entity/advanced_blend_luminosity.frag.h"
#include "impeller/entity/advanced_blend_multiply.frag.h"
#include "impeller/entity/advanced_blend_overlay.frag.h"
#include "impeller/entity/advanced_blend_saturation.frag.h"
#include "impeller/entity/advanced_blend_screen.frag.h"
#include "impeller/entity/advanced_blend_softlight.frag.h"
#include "impeller/entity/blend.frag.h"
@@ -51,6 +55,8 @@ using GradientFillPipeline =
using SolidFillPipeline =
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
using BlendPipeline = PipelineT<BlendVertexShader, BlendFragmentShader>;
using BlendColorPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendColorFragmentShader>;
using BlendColorBurnPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendColorburnFragmentShader>;
using BlendColorDodgePipeline =
@@ -63,12 +69,18 @@ using BlendExclusionPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendExclusionFragmentShader>;
using BlendHardLightPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendHardlightFragmentShader>;
using BlendHuePipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendHueFragmentShader>;
using BlendLightenPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendLightenFragmentShader>;
using BlendLuminosityPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendLuminosityFragmentShader>;
using BlendMultiplyPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendMultiplyFragmentShader>;
using BlendOverlayPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendOverlayFragmentShader>;
using BlendSaturationPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendSaturationFragmentShader>;
using BlendScreenPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendScreenFragmentShader>;
using BlendSoftLightPipeline =
@@ -173,6 +185,11 @@ class ContentContext {
// Advanced blends.
std::shared_ptr<Pipeline> GetBlendColorPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_color_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendColorBurnPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_colorburn_pipelines_, opts);
@@ -203,11 +220,21 @@ class ContentContext {
return GetPipeline(blend_hardlight_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendHuePipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_hue_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendLightenPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_lighten_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendLuminosityPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_luminosity_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendMultiplyPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_multiply_pipelines_, opts);
@@ -218,6 +245,11 @@ class ContentContext {
return GetPipeline(blend_overlay_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendSaturationPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_saturation_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetBlendScreenPipeline(
ContentContextOptions opts) const {
return GetPipeline(blend_screen_pipelines_, opts);
@@ -261,15 +293,19 @@ class ContentContext {
mutable Variants<GlyphAtlasPipeline> glyph_atlas_pipelines_;
mutable Variants<VerticesPipeline> vertices_pipelines_;
// Advanced blends.
mutable Variants<BlendColorPipeline> blend_color_pipelines_;
mutable Variants<BlendColorBurnPipeline> blend_colorburn_pipelines_;
mutable Variants<BlendColorDodgePipeline> blend_colordodge_pipelines_;
mutable Variants<BlendDarkenPipeline> blend_darken_pipelines_;
mutable Variants<BlendDifferencePipeline> blend_difference_pipelines_;
mutable Variants<BlendExclusionPipeline> blend_exclusion_pipelines_;
mutable Variants<BlendHardLightPipeline> blend_hardlight_pipelines_;
mutable Variants<BlendHuePipeline> blend_hue_pipelines_;
mutable Variants<BlendLightenPipeline> blend_lighten_pipelines_;
mutable Variants<BlendLuminosityPipeline> blend_luminosity_pipelines_;
mutable Variants<BlendMultiplyPipeline> blend_multiply_pipelines_;
mutable Variants<BlendOverlayPipeline> blend_overlay_pipelines_;
mutable Variants<BlendSaturationPipeline> blend_saturation_pipelines_;
mutable Variants<BlendScreenPipeline> blend_screen_pipelines_;
mutable Variants<BlendSoftLightPipeline> blend_softlight_pipelines_;

View File

@@ -238,7 +238,7 @@ void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) {
static_assert(Entity::BlendMode::kLastAdvancedBlendMode ==
Entity::BlendMode::kMultiply);
Entity::BlendMode::kLuminosity);
switch (blend_mode) {
BLEND_CASE(Screen)
@@ -252,6 +252,10 @@ void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
BLEND_CASE(Difference)
BLEND_CASE(Exclusion)
BLEND_CASE(Multiply)
BLEND_CASE(Hue)
BLEND_CASE(Saturation)
BLEND_CASE(Color)
BLEND_CASE(Luminosity)
default:
FML_UNREACHABLE();
}

View File

@@ -53,9 +53,13 @@ class Entity {
kDifference,
kExclusion,
kMultiply,
kHue,
kSaturation,
kColor,
kLuminosity,
kLastPipelineBlendMode = kModulate,
kLastAdvancedBlendMode = kMultiply,
kLastAdvancedBlendMode = kLuminosity,
};
enum class ClipOperation {

View File

@@ -0,0 +1,12 @@
// 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 "advanced_blend_utils.glsl"
vec3 Blend(vec3 dst, vec3 src) {
// https://www.w3.org/TR/compositing-1/#blendingcolor
return SetLuminosity(src, Luminosity(dst));
}
#include "advanced_blend.glsl"

View File

@@ -5,6 +5,7 @@
#include "advanced_blend_utils.glsl"
vec3 Blend(vec3 dst, vec3 src) {
// https://www.w3.org/TR/compositing-1/#blendinghardlight
return BlendHardLight(dst, src);
}

View File

@@ -0,0 +1,12 @@
// 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 "advanced_blend_utils.glsl"
vec3 Blend(vec3 dst, vec3 src) {
// https://www.w3.org/TR/compositing-1/#blendinghue
return SetLuminosity(SetSaturation(src, Saturation(dst)), Luminosity(dst));
}
#include "advanced_blend.glsl"

View File

@@ -0,0 +1,12 @@
// 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 "advanced_blend_utils.glsl"
vec3 Blend(vec3 dst, vec3 src) {
// https://www.w3.org/TR/compositing-1/#blendingluminosity
return SetLuminosity(dst, Luminosity(src));
}
#include "advanced_blend.glsl"

View File

@@ -0,0 +1,12 @@
// 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 "advanced_blend_utils.glsl"
vec3 Blend(vec3 dst, vec3 src) {
// https://www.w3.org/TR/compositing-1/#blendingsaturation
return SetLuminosity(SetSaturation(dst, Saturation(src)), Luminosity(dst));
}
#include "advanced_blend.glsl"

View File

@@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const float kEhCloseEnough = 0.000001;
vec3 ComponentIsValue(vec3 n, float value) {
vec3 diff = abs(n - value);
return vec3(diff.r < 0.0001, diff.g < 0.0001, diff.b < 0.0001);
return vec3(diff.r < kEhCloseEnough, //
diff.g < kEhCloseEnough, //
diff.b < kEhCloseEnough);
}
vec3 MixComponents(vec3 a, vec3 b, vec3 weight, float cutoff) {
@@ -24,3 +28,44 @@ vec3 BlendScreen(vec3 dst, vec3 src) {
vec3 BlendHardLight(vec3 dst, vec3 src) {
return MixHalf(dst * (2 * src), BlendScreen(dst, 2 * src - 1), src);
}
//------------------------------------------------------------------------------
// HSV utilities.
//
float Luminosity(vec3 color) {
return color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
}
/// Scales the color's luma by the amount necessary to place the color
/// components in a 1-0 range.
vec3 ClipColor(vec3 color) {
float lum = Luminosity(color);
float mn = min(min(color.r, color.g), color.b);
float mx = max(max(color.r, color.g), color.b);
// `lum - mn` and `mx - lum` will always be >= 0 in the following conditions,
// so adding a tiny value is enough to make these divisions safe.
if (mn < 0) {
color = lum + (((color - lum) * lum) / (lum - mn + kEhCloseEnough));
}
if (mx > 1) {
color = lum + (((color - lum) * (1 - lum)) / (mx - lum + kEhCloseEnough));
}
return color;
}
vec3 SetLuminosity(vec3 color, float luminosity) {
float relative_lum = luminosity - Luminosity(color);
return ClipColor(color + relative_lum);
}
float Saturation(vec3 color) {
return max(max(color.r, color.g), color.b) -
min(min(color.r, color.g), color.b);
}
vec3 SetSaturation(vec3 color, float saturation) {
float mn = min(min(color.r, color.g), color.b);
float mx = max(max(color.r, color.g), color.b);
return (mn < mx) ? ((color - mn) * saturation) / (mx - mn) : vec3(0);
}