diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index faffbfe792..579d36b541 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -882,13 +882,13 @@ TEST_P(AiksTest, BlendModeShouldCoverWholeScreen) { paint.color = Color::Red(); canvas.DrawPaint(paint); - paint.blend_mode = Entity::BlendMode::kSourceOver; + paint.blend_mode = BlendMode::kSourceOver; canvas.SaveLayer(paint); paint.color = Color::White(); canvas.DrawRect({100, 100, 400, 400}, paint); - paint.blend_mode = Entity::BlendMode::kSource; + paint.blend_mode = BlendMode::kSource; canvas.SaveLayer(paint); paint.color = Color::Blue(); @@ -1168,7 +1168,7 @@ TEST_P(AiksTest, CanRenderTextInSaveLayer) { canvas.Scale(Vector2{0.5, 0.5}); // Blend the layer with the parent pass using kClear to expose the coverage. - canvas.SaveLayer({.blend_mode = Entity::BlendMode::kClear}); + canvas.SaveLayer({.blend_mode = BlendMode::kClear}); ASSERT_TRUE(RenderTextInCanvas( GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?", "Roboto-Regular.ttf")); @@ -1199,7 +1199,7 @@ TEST_P(AiksTest, PaintBlendModeIsRespected) { paint.color = Color(0, 1, 0, 0.5); canvas.DrawCircle(Point(250, 200), 100, paint); - paint.blend_mode = Entity::BlendMode::kPlus; + paint.blend_mode = BlendMode::kPlus; paint.color = Color::Red(); canvas.DrawCircle(Point(450, 250), 100, paint); paint.color = Color::Green(); @@ -1213,43 +1213,43 @@ TEST_P(AiksTest, ColorWheel) { // Compare with https://fiddle.skia.org/c/@BlendModes std::vector blend_mode_names; - std::vector blend_mode_values; + std::vector blend_mode_values; { - const std::vector> blends = { + const std::vector> blends = { // Pipeline blends (Porter-Duff alpha compositing) - {"Clear", Entity::BlendMode::kClear}, - {"Source", Entity::BlendMode::kSource}, - {"Destination", Entity::BlendMode::kDestination}, - {"SourceOver", Entity::BlendMode::kSourceOver}, - {"DestinationOver", Entity::BlendMode::kDestinationOver}, - {"SourceIn", Entity::BlendMode::kSourceIn}, - {"DestinationIn", Entity::BlendMode::kDestinationIn}, - {"SourceOut", Entity::BlendMode::kSourceOut}, - {"DestinationOut", Entity::BlendMode::kDestinationOut}, - {"SourceATop", Entity::BlendMode::kSourceATop}, - {"DestinationATop", Entity::BlendMode::kDestinationATop}, - {"Xor", Entity::BlendMode::kXor}, - {"Plus", Entity::BlendMode::kPlus}, - {"Modulate", Entity::BlendMode::kModulate}, + {"Clear", BlendMode::kClear}, + {"Source", BlendMode::kSource}, + {"Destination", BlendMode::kDestination}, + {"SourceOver", BlendMode::kSourceOver}, + {"DestinationOver", BlendMode::kDestinationOver}, + {"SourceIn", BlendMode::kSourceIn}, + {"DestinationIn", BlendMode::kDestinationIn}, + {"SourceOut", BlendMode::kSourceOut}, + {"DestinationOut", BlendMode::kDestinationOut}, + {"SourceATop", BlendMode::kSourceATop}, + {"DestinationATop", BlendMode::kDestinationATop}, + {"Xor", BlendMode::kXor}, + {"Plus", BlendMode::kPlus}, + {"Modulate", BlendMode::kModulate}, // Advanced blends (color component blends) - {"Screen", Entity::BlendMode::kScreen}, - {"Overlay", Entity::BlendMode::kOverlay}, - {"Darken", Entity::BlendMode::kDarken}, - {"Lighten", Entity::BlendMode::kLighten}, - {"ColorDodge", Entity::BlendMode::kColorDodge}, - {"ColorBurn", Entity::BlendMode::kColorBurn}, - {"HardLight", Entity::BlendMode::kHardLight}, - {"SoftLight", Entity::BlendMode::kSoftLight}, - {"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}, + {"Screen", BlendMode::kScreen}, + {"Overlay", BlendMode::kOverlay}, + {"Darken", BlendMode::kDarken}, + {"Lighten", BlendMode::kLighten}, + {"ColorDodge", BlendMode::kColorDodge}, + {"ColorBurn", BlendMode::kColorBurn}, + {"HardLight", BlendMode::kHardLight}, + {"SoftLight", BlendMode::kSoftLight}, + {"Difference", BlendMode::kDifference}, + {"Exclusion", BlendMode::kExclusion}, + {"Multiply", BlendMode::kMultiply}, + {"Hue", BlendMode::kHue}, + {"Saturation", BlendMode::kSaturation}, + {"Color", BlendMode::kColor}, + {"Luminosity", BlendMode::kLuminosity}, }; assert(blends.size() == - static_cast(Entity::BlendMode::kLastAdvancedBlendMode) + 1); + static_cast(Entity::kLastAdvancedBlendMode) + 1); for (const auto& [name, mode] : blends) { blend_mode_names.push_back(name); blend_mode_values.push_back(mode); @@ -1274,7 +1274,7 @@ TEST_P(AiksTest, ColorWheel) { }; Paint paint; - paint.blend_mode = Entity::BlendMode::kSourceOver; + paint.blend_mode = BlendMode::kSourceOver; // Draw a fancy color wheel for the backdrop. // https://www.desmos.com/calculator/xw7kafthwd @@ -1363,7 +1363,7 @@ TEST_P(AiksTest, ColorWheel) { .blend_mode = blend_mode_values[current_blend_index]}); { Paint paint; - paint.blend_mode = Entity::BlendMode::kPlus; + paint.blend_mode = BlendMode::kPlus; const Scalar x = std::sin(k2Pi / 3); const Scalar y = -std::cos(k2Pi / 3); paint.color = color0; @@ -1615,7 +1615,7 @@ TEST_P(AiksTest, CanRenderClippedLayers) { // collapsed into the parent pass. canvas.DrawRect( Rect::MakeSize(Size{400, 400}), - {.color = Color::Green(), .blend_mode = Entity::BlendMode::kColorBurn}); + {.color = Color::Green(), .blend_mode = BlendMode::kColorBurn}); } ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 4978325e05..a767649fbf 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -46,7 +46,7 @@ void Canvas::Save() { void Canvas::Save( bool create_subpass, - Entity::BlendMode blend_mode, + BlendMode blend_mode, std::optional backdrop_filter) { auto entry = CanvasStackEntry{}; entry.xformation = xformation_stack_.back().xformation; @@ -352,7 +352,7 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) { } void Canvas::DrawVertices(Vertices vertices, - Entity::BlendMode blend_mode, + BlendMode blend_mode, Paint paint) { std::shared_ptr contents = std::make_shared(std::move(vertices)); @@ -371,7 +371,7 @@ void Canvas::DrawAtlas(std::shared_ptr atlas, std::vector transforms, std::vector texture_coordinates, std::vector colors, - Entity::BlendMode blend_mode, + BlendMode blend_mode, SamplerDescriptor sampler, std::optional cull_rect, Paint paint) { diff --git a/engine/src/flutter/impeller/aiks/canvas.h b/engine/src/flutter/impeller/aiks/canvas.h index 684edb8972..e9fbb5351d 100644 --- a/engine/src/flutter/impeller/aiks/canvas.h +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -95,15 +95,13 @@ class Canvas { void DrawTextFrame(TextFrame text_frame, Point position, Paint paint); - void DrawVertices(Vertices vertices, - Entity::BlendMode blend_mode, - Paint paint); + void DrawVertices(Vertices vertices, BlendMode blend_mode, Paint paint); void DrawAtlas(std::shared_ptr atlas, std::vector transforms, std::vector texture_coordinates, std::vector colors, - Entity::BlendMode blend_mode, + BlendMode blend_mode, SamplerDescriptor sampler, std::optional cull_rect, Paint paint); @@ -124,7 +122,7 @@ class Canvas { size_t GetStencilDepth() const; void Save(bool create_subpass, - Entity::BlendMode = Entity::BlendMode::kSourceOver, + BlendMode = BlendMode::kSourceOver, std::optional backdrop_filter = std::nullopt); diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 02da68de57..d5c156ac18 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -53,7 +53,7 @@ struct Paint { SolidStrokeContents::Join stroke_join = SolidStrokeContents::Join::kMiter; Scalar stroke_miter = 4.0; Style style = Style::kFill; - Entity::BlendMode blend_mode = Entity::BlendMode::kSourceOver; + BlendMode blend_mode = BlendMode::kSourceOver; std::optional image_filter; std::optional color_filter; diff --git a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc index 453da01671..faaacf12f5 100644 --- a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc +++ b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc @@ -23,7 +23,7 @@ std::optional PaintPassDelegate::GetCoverageRect() { // |EntityPassDelgate| bool PaintPassDelegate::CanElide() { - return paint_.blend_mode == Entity::BlendMode::kDestination; + return paint_.blend_mode == BlendMode::kDestination; } // |EntityPassDelgate| diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index e47e167645..28ef0d7345 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -44,66 +44,66 @@ DisplayListDispatcher::DisplayListDispatcher() = default; DisplayListDispatcher::~DisplayListDispatcher() = default; -static Entity::BlendMode ToBlendMode(flutter::DlBlendMode mode) { +static BlendMode ToBlendMode(flutter::DlBlendMode mode) { switch (mode) { case flutter::DlBlendMode::kClear: - return Entity::BlendMode::kClear; + return BlendMode::kClear; case flutter::DlBlendMode::kSrc: - return Entity::BlendMode::kSource; + return BlendMode::kSource; case flutter::DlBlendMode::kDst: - return Entity::BlendMode::kDestination; + return BlendMode::kDestination; case flutter::DlBlendMode::kSrcOver: - return Entity::BlendMode::kSourceOver; + return BlendMode::kSourceOver; case flutter::DlBlendMode::kDstOver: - return Entity::BlendMode::kDestinationOver; + return BlendMode::kDestinationOver; case flutter::DlBlendMode::kSrcIn: - return Entity::BlendMode::kSourceIn; + return BlendMode::kSourceIn; case flutter::DlBlendMode::kDstIn: - return Entity::BlendMode::kDestinationIn; + return BlendMode::kDestinationIn; case flutter::DlBlendMode::kSrcOut: - return Entity::BlendMode::kSourceOut; + return BlendMode::kSourceOut; case flutter::DlBlendMode::kDstOut: - return Entity::BlendMode::kDestinationOut; + return BlendMode::kDestinationOut; case flutter::DlBlendMode::kSrcATop: - return Entity::BlendMode::kSourceATop; + return BlendMode::kSourceATop; case flutter::DlBlendMode::kDstATop: - return Entity::BlendMode::kDestinationATop; + return BlendMode::kDestinationATop; case flutter::DlBlendMode::kXor: - return Entity::BlendMode::kXor; + return BlendMode::kXor; case flutter::DlBlendMode::kPlus: - return Entity::BlendMode::kPlus; + return BlendMode::kPlus; case flutter::DlBlendMode::kModulate: - return Entity::BlendMode::kModulate; + return BlendMode::kModulate; case flutter::DlBlendMode::kScreen: - return Entity::BlendMode::kScreen; + return BlendMode::kScreen; case flutter::DlBlendMode::kOverlay: - return Entity::BlendMode::kOverlay; + return BlendMode::kOverlay; case flutter::DlBlendMode::kDarken: - return Entity::BlendMode::kDarken; + return BlendMode::kDarken; case flutter::DlBlendMode::kLighten: - return Entity::BlendMode::kLighten; + return BlendMode::kLighten; case flutter::DlBlendMode::kColorDodge: - return Entity::BlendMode::kColorDodge; + return BlendMode::kColorDodge; case flutter::DlBlendMode::kColorBurn: - return Entity::BlendMode::kColorBurn; + return BlendMode::kColorBurn; case flutter::DlBlendMode::kHardLight: - return Entity::BlendMode::kHardLight; + return BlendMode::kHardLight; case flutter::DlBlendMode::kSoftLight: - return Entity::BlendMode::kSoftLight; + return BlendMode::kSoftLight; case flutter::DlBlendMode::kDifference: - return Entity::BlendMode::kDifference; + return BlendMode::kDifference; case flutter::DlBlendMode::kExclusion: - return Entity::BlendMode::kExclusion; + return BlendMode::kExclusion; case flutter::DlBlendMode::kMultiply: - return Entity::BlendMode::kMultiply; + return BlendMode::kMultiply; case flutter::DlBlendMode::kHue: - return Entity::BlendMode::kHue; + return BlendMode::kHue; case flutter::DlBlendMode::kSaturation: - return Entity::BlendMode::kSaturation; + return BlendMode::kSaturation; case flutter::DlBlendMode::kColor: - return Entity::BlendMode::kColor; + return BlendMode::kColor; case flutter::DlBlendMode::kLuminosity: - return Entity::BlendMode::kLuminosity; + return BlendMode::kLuminosity; } FML_UNREACHABLE(); } diff --git a/engine/src/flutter/impeller/docs/blending.md b/engine/src/flutter/impeller/docs/blending.md index 9274385595..56b3b7c1ba 100644 --- a/engine/src/flutter/impeller/docs/blending.md +++ b/engine/src/flutter/impeller/docs/blending.md @@ -10,10 +10,10 @@ configuration (called "Pipeline Blends"), and those which cannot (called All blend modes conform to the [W3C Compositing and Blending recommendation](https://www.w3.org/TR/compositing-1/). -Blend operations are driven by the `Entity::BlendMode` enum. In the Aiks layer, +Blend operations are driven by the `BlendMode` enum. In the Aiks layer, all drawing operations conform to the given `Paint::blend_mode`. In the Entities layer, all Entities have an associated blend mode, which can be set via -`Entity::SetBlendMode(Entity::BlendMode)`. +`Entity::SetBlendMode(BlendMode)`. ## Glossary of blending terms | Term | Definition | diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc index 8e82a568c1..5c9d0bec22 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc @@ -45,7 +45,7 @@ void AtlasContents::SetAlpha(Scalar alpha) { alpha_ = alpha; } -void AtlasContents::SetBlendMode(Entity::BlendMode blend_mode) { +void AtlasContents::SetBlendMode(BlendMode blend_mode) { // TODO(jonahwilliams): blending of colors with texture. blend_mode_ = blend_mode; } @@ -100,7 +100,7 @@ bool AtlasContents::Render(const ContentContext& renderer, for (size_t i = 0; i < texture_coords_.size(); i++) { auto sample_rect = texture_coords_[i]; auto matrix = transforms_[i]; - auto color = (colors_.size() > 0 ? colors_[i] : Color::Black()); + auto color = colors_.size() > 0 ? colors_[i] : Color::Black(); auto transformed_points = Rect::MakeSize(sample_rect.size).GetTransformedPoints(matrix); diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.h b/engine/src/flutter/impeller/entity/contents/atlas_contents.h index 2ff284c1a5..7b879892da 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.h +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.h @@ -27,7 +27,7 @@ class AtlasContents final : public Contents { void SetTransforms(std::vector transforms); - void SetBlendMode(Entity::BlendMode blend_mode); + void SetBlendMode(BlendMode blend_mode); void SetTextureCoordinates(std::vector texture_coords); @@ -54,7 +54,7 @@ class AtlasContents final : public Contents { std::vector texture_coords_; std::vector colors_; std::vector transforms_; - Entity::BlendMode blend_mode_; + BlendMode blend_mode_; std::optional cull_rect_; Scalar alpha_ = 1.0; SamplerDescriptor sampler_descriptor_ = {}; diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 632d2bea47..28475830e6 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -6,6 +6,7 @@ #include +#include "impeller/entity/entity.h" #include "impeller/renderer/command_buffer.h" #include "impeller/renderer/formats.h" #include "impeller/renderer/render_pass.h" @@ -16,10 +17,10 @@ namespace impeller { void ContentContextOptions::ApplyToPipelineDescriptor( PipelineDescriptor& desc) const { auto pipeline_blend = blend_mode; - if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) { + if (blend_mode > Entity::kLastPipelineBlendMode) { VALIDATION_LOG << "Cannot use blend mode " << static_cast(blend_mode) << " as a pipeline blend."; - pipeline_blend = Entity::BlendMode::kSourceOver; + pipeline_blend = BlendMode::kSourceOver; } desc.SetSampleCount(sample_count); @@ -28,89 +29,86 @@ void ContentContextOptions::ApplyToPipelineDescriptor( color0.alpha_blend_op = BlendOperation::kAdd; color0.color_blend_op = BlendOperation::kAdd; - static_assert(Entity::BlendMode::kLastPipelineBlendMode == - Entity::BlendMode::kModulate); - switch (pipeline_blend) { - case Entity::BlendMode::kClear: + case BlendMode::kClear: color0.dst_alpha_blend_factor = BlendFactor::kZero; color0.dst_color_blend_factor = BlendFactor::kZero; color0.src_alpha_blend_factor = BlendFactor::kZero; color0.src_color_blend_factor = BlendFactor::kZero; break; - case Entity::BlendMode::kSource: + case BlendMode::kSource: color0.dst_alpha_blend_factor = BlendFactor::kZero; color0.dst_color_blend_factor = BlendFactor::kZero; color0.src_alpha_blend_factor = BlendFactor::kOne; color0.src_color_blend_factor = BlendFactor::kOne; break; - case Entity::BlendMode::kDestination: + case BlendMode::kDestination: color0.dst_alpha_blend_factor = BlendFactor::kDestinationAlpha; color0.dst_color_blend_factor = BlendFactor::kOne; color0.src_alpha_blend_factor = BlendFactor::kZero; color0.src_color_blend_factor = BlendFactor::kZero; break; - case Entity::BlendMode::kSourceOver: + case BlendMode::kSourceOver: color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kOne; color0.src_color_blend_factor = BlendFactor::kOne; break; - case Entity::BlendMode::kDestinationOver: + case BlendMode::kDestinationOver: color0.dst_alpha_blend_factor = BlendFactor::kDestinationAlpha; color0.dst_color_blend_factor = BlendFactor::kOne; color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha; break; - case Entity::BlendMode::kSourceIn: + case BlendMode::kSourceIn: color0.dst_alpha_blend_factor = BlendFactor::kZero; color0.dst_color_blend_factor = BlendFactor::kZero; color0.src_alpha_blend_factor = BlendFactor::kDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kDestinationAlpha; break; - case Entity::BlendMode::kDestinationIn: + case BlendMode::kDestinationIn: color0.dst_alpha_blend_factor = BlendFactor::kSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kZero; color0.src_color_blend_factor = BlendFactor::kZero; break; - case Entity::BlendMode::kSourceOut: + case BlendMode::kSourceOut: color0.dst_alpha_blend_factor = BlendFactor::kZero; color0.dst_color_blend_factor = BlendFactor::kZero; color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha; break; - case Entity::BlendMode::kDestinationOut: + case BlendMode::kDestinationOut: color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kZero; color0.src_color_blend_factor = BlendFactor::kZero; break; - case Entity::BlendMode::kSourceATop: + case BlendMode::kSourceATop: color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kDestinationAlpha; break; - case Entity::BlendMode::kDestinationATop: + case BlendMode::kDestinationATop: color0.dst_alpha_blend_factor = BlendFactor::kSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha; break; - case Entity::BlendMode::kXor: + case BlendMode::kXor: color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha; color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha; color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha; break; - case Entity::BlendMode::kPlus: + case BlendMode::kPlus: color0.dst_alpha_blend_factor = BlendFactor::kOne; color0.dst_color_blend_factor = BlendFactor::kOne; color0.src_alpha_blend_factor = BlendFactor::kOne; color0.src_color_blend_factor = BlendFactor::kOne; break; - case Entity::BlendMode::kModulate: + case BlendMode::kModulate: // kSourceColor and kDestinationColor override the alpha blend factor. color0.dst_alpha_blend_factor = BlendFactor::kZero; color0.dst_color_blend_factor = BlendFactor::kSourceColor; diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index 3b671f1e39..548ba4dd6d 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -155,7 +155,7 @@ using ClipPipeline = struct ContentContextOptions { SampleCount sample_count = SampleCount::kCount1; - Entity::BlendMode blend_mode = Entity::BlendMode::kSourceOver; + BlendMode blend_mode = BlendMode::kSourceOver; CompareFunction stencil_compare = CompareFunction::kEqual; StencilOperation stencil_operation = StencilOperation::kKeep; diff --git a/engine/src/flutter/impeller/entity/contents/contents.cc b/engine/src/flutter/impeller/entity/contents/contents.cc index 877b8d1521..6b96fd45d5 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.cc +++ b/engine/src/flutter/impeller/entity/contents/contents.cc @@ -50,7 +50,7 @@ std::optional Contents::RenderToSnapshot( [&contents = *this, &entity, &coverage](const ContentContext& renderer, RenderPass& pass) -> bool { Entity sub_entity; - sub_entity.SetBlendMode(Entity::BlendMode::kSourceOver); + sub_entity.SetBlendMode(BlendMode::kSourceOver); sub_entity.SetTransformation( Matrix::MakeTranslation(Vector3(-coverage->origin)) * entity.GetTransformation()); diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc index c7d569e939..69968adfb2 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc @@ -12,6 +12,7 @@ #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/entity/entity.h" #include "impeller/geometry/path_builder.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" @@ -19,7 +20,7 @@ namespace impeller { BlendFilterContents::BlendFilterContents() { - SetBlendMode(Entity::BlendMode::kSourceOver); + SetBlendMode(BlendMode::kSourceOver); } BlendFilterContents::~BlendFilterContents() = default; @@ -148,7 +149,7 @@ static std::optional PipelineBlend( const ContentContext& renderer, const Entity& entity, const Rect& coverage, - Entity::BlendMode pipeline_blend, + BlendMode pipeline_blend, std::optional foreground_color) { using VS = BlendPipeline::VertexShader; using FS = BlendPipeline::FragmentShader; @@ -201,7 +202,7 @@ static std::optional PipelineBlend( // Draw the first texture using kSource. - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetBlendPipeline(options); if (!add_blend_command(inputs[0]->GetSnapshot(renderer, entity))) { return true; @@ -256,7 +257,7 @@ static std::optional PipelineBlend( } #define BLEND_CASE(mode) \ - case Entity::BlendMode::k##mode: \ + case BlendMode::k##mode: \ advanced_blend_proc_ = [](const FilterInput::Vector& inputs, \ const ContentContext& renderer, \ const Entity& entity, const Rect& coverage, \ @@ -267,18 +268,15 @@ static std::optional PipelineBlend( }; \ break; -void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) { - if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) { +void BlendFilterContents::SetBlendMode(BlendMode blend_mode) { + if (blend_mode > Entity::kLastAdvancedBlendMode) { VALIDATION_LOG << "Invalid blend mode " << static_cast(blend_mode) << " assigned to BlendFilterContents."; } blend_mode_ = blend_mode; - if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) { - static_assert(Entity::BlendMode::kLastAdvancedBlendMode == - Entity::BlendMode::kLuminosity); - + if (blend_mode > Entity::kLastPipelineBlendMode) { switch (blend_mode) { BLEND_CASE(Screen) BLEND_CASE(Overlay) @@ -317,16 +315,16 @@ std::optional BlendFilterContents::RenderFilter( if (inputs.size() == 1 && !foreground_color_.has_value()) { // Nothing to blend. - return PipelineBlend(inputs, renderer, entity, coverage, - Entity::BlendMode::kSource, std::nullopt); + return PipelineBlend(inputs, renderer, entity, coverage, BlendMode::kSource, + std::nullopt); } - if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) { + if (blend_mode_ <= Entity::kLastPipelineBlendMode) { return PipelineBlend(inputs, renderer, entity, coverage, blend_mode_, foreground_color_); } - if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) { + if (blend_mode_ <= Entity::kLastAdvancedBlendMode) { return advanced_blend_proc_(inputs, renderer, entity, coverage, foreground_color_); } diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h index b0010f2db7..466842e61d 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.h @@ -22,7 +22,7 @@ class BlendFilterContents : public FilterContents { ~BlendFilterContents() override; - void SetBlendMode(Entity::BlendMode blend_mode); + void SetBlendMode(BlendMode blend_mode); /// @brief Sets a source color which is blended after all of the inputs have /// been blended. @@ -36,7 +36,7 @@ class BlendFilterContents : public FilterContents { const Matrix& effect_transform, const Rect& coverage) const override; - Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver; + BlendMode blend_mode_ = BlendMode::kSourceOver; AdvancedBlendProc advanced_blend_proc_; std::optional foreground_color_; diff --git a/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc index 619f457a4e..a7d578531d 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc @@ -96,7 +96,7 @@ std::optional BorderMaskBlurFilterContents::RenderFilter( Command cmd; cmd.label = "Border Mask Blur Filter"; auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options); cmd.BindVertices(vtx_buffer); diff --git a/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc index b8084c9525..eba9d3a867 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/color_matrix_filter_contents.cc @@ -55,7 +55,7 @@ std::optional ColorMatrixFilterContents::RenderFilter( cmd.label = "Color Matrix Filter"; auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options); VertexBufferBuilder vtx_builder; diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc index 800efadf6f..6c1865b518 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -33,18 +33,17 @@ namespace impeller { std::shared_ptr FilterContents::MakeBlend( - Entity::BlendMode blend_mode, + BlendMode blend_mode, FilterInput::Vector inputs, std::optional foreground_color) { - if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) { + if (blend_mode > Entity::kLastAdvancedBlendMode) { VALIDATION_LOG << "Invalid blend mode " << static_cast(blend_mode) << " passed to FilterContents::MakeBlend."; return nullptr; } size_t total_inputs = inputs.size() + (foreground_color.has_value() ? 1 : 0); - if (total_inputs < 2 || - blend_mode <= Entity::BlendMode::kLastPipelineBlendMode) { + if (total_inputs < 2 || blend_mode <= Entity::kLastPipelineBlendMode) { auto blend = std::make_shared(); blend->SetInputs(inputs); blend->SetBlendMode(blend_mode); diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h index eb7d27ee94..11981abf4f 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -37,7 +37,7 @@ class FilterContents : public Contents { enum class MorphType { kDilate, kErode }; static std::shared_ptr MakeBlend( - Entity::BlendMode blend_mode, + BlendMode blend_mode, FilterInput::Vector inputs, std::optional foreground_color = std::nullopt); diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index c48bb85f66..50fd91a406 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -207,7 +207,7 @@ std::optional DirectionalGaussianBlurFilterContents::RenderFilter( cmd.label = SPrintF("Gaussian Blur Filter (Radius=%.2f)", transformed_blur_radius_length); auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetGaussianBlurPipeline(options); cmd.BindVertices(vtx_buffer); diff --git a/engine/src/flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc index 3d7ffdae9c..445ffa9e46 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc @@ -41,7 +41,7 @@ std::optional LinearToSrgbFilterContents::RenderFilter( cmd.label = "Linear to sRGB Filter"; auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetLinearToSrgbFilterPipeline(options); VertexBufferBuilder vtx_builder; diff --git a/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc index 398a4246a6..9156a55397 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/morphology_filter_contents.cc @@ -115,7 +115,7 @@ std::optional DirectionalMorphologyFilterContents::RenderFilter( Command cmd; cmd.label = "Morphology Filter"; auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetMorphologyFilterPipeline(options); cmd.BindVertices(vtx_buffer); diff --git a/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc index 3c3a5fa1be..6f5a7bc143 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/srgb_to_linear_filter_contents.cc @@ -41,7 +41,7 @@ std::optional SrgbToLinearFilterContents::RenderFilter( cmd.label = "sRGB to Linear Filter"; auto options = OptionsFromPass(pass); - options.blend_mode = Entity::BlendMode::kSource; + options.blend_mode = BlendMode::kSource; cmd.pipeline = renderer.GetSrgbToLinearFilterPipeline(options); VertexBufferBuilder vtx_builder; diff --git a/engine/src/flutter/impeller/entity/contents/vertices_contents.cc b/engine/src/flutter/impeller/entity/contents/vertices_contents.cc index 88821c8580..4ab78afbe9 100644 --- a/engine/src/flutter/impeller/entity/contents/vertices_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/vertices_contents.cc @@ -5,7 +5,6 @@ #include "vertices_contents.h" #include "impeller/entity/contents/content_context.h" -#include "impeller/entity/entity.h" #include "impeller/entity/vertices.frag.h" #include "impeller/entity/vertices.vert.h" #include "impeller/geometry/color.h" @@ -29,7 +28,7 @@ void VerticesContents::SetColor(Color color) { color_ = color.Premultiply(); } -void VerticesContents::SetBlendMode(Entity::BlendMode blend_mode) { +void VerticesContents::SetBlendMode(BlendMode blend_mode) { blend_mode_ = blend_mode; } @@ -66,13 +65,12 @@ bool VerticesContents::Render(const ContentContext& renderer, const auto& positions = vertices_.GetPositions(); const auto& colors = vertices_.GetColors(); for (size_t i = 0; i < positions.size(); i++) { + auto color = i < colors.size() + ? Color::BlendColor(color_, colors[i], blend_mode_) + : color_; vertex_data.push_back(VS::PerVertexData{ .position = positions[i], - // TODO(108047): Blend these colors together when available. Use - // colors[i] as the destination and color_ as the - // source. Always use color_ when vertex colors are not - // supplied. - .color = i < colors.size() ? colors[i] : color_, + .color = color, }); } } diff --git a/engine/src/flutter/impeller/entity/contents/vertices_contents.h b/engine/src/flutter/impeller/entity/contents/vertices_contents.h index ed7b153a43..ae8637f7a9 100644 --- a/engine/src/flutter/impeller/entity/contents/vertices_contents.h +++ b/engine/src/flutter/impeller/entity/contents/vertices_contents.h @@ -27,7 +27,7 @@ class VerticesContents final : public Contents { void SetColor(Color color); - void SetBlendMode(Entity::BlendMode blend_mode); + void SetBlendMode(BlendMode blend_mode); // |Contents| std::optional GetCoverage(const Entity& entity) const override; @@ -40,7 +40,7 @@ class VerticesContents final : public Contents { public: Vertices vertices_; Color color_; - Entity::BlendMode blend_mode_ = Entity::BlendMode::kSource; + BlendMode blend_mode_ = BlendMode::kSource; FML_DISALLOW_COPY_AND_ASSIGN(VerticesContents); }; diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 6931228cbb..680dd6364a 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -4,15 +4,21 @@ #include "impeller/entity/entity.h" +#include #include #include "impeller/base/validation.h" #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/filters/filter_contents.h" +#include "impeller/entity/entity_pass.h" +#include "impeller/geometry/vector.h" #include "impeller/renderer/render_pass.h" namespace impeller { +const BlendMode Entity::kLastPipelineBlendMode = BlendMode::kModulate; +const BlendMode Entity::kLastAdvancedBlendMode = BlendMode::kLuminosity; + Entity::Entity() = default; Entity::~Entity() = default; @@ -69,7 +75,7 @@ void Entity::SetBlendMode(BlendMode blend_mode) { blend_mode_ = blend_mode; } -Entity::BlendMode Entity::GetBlendMode() const { +BlendMode Entity::GetBlendMode() const { return blend_mode_; } diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 105c0a4803..14481b29cf 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -19,48 +19,8 @@ class RenderPass; class Entity { public: - /// All blend modes assume that both the source (fragment output) and - /// destination (first color attachment) have colors with premultiplied alpha. - enum class BlendMode { - // The following blend modes are able to be used as pipeline blend modes or - // via `BlendFilterContents`. - kClear, - kSource, - kDestination, - kSourceOver, - kDestinationOver, - kSourceIn, - kDestinationIn, - kSourceOut, - kDestinationOut, - kSourceATop, - kDestinationATop, - kXor, - kPlus, - kModulate, - - // The following blend modes use equations that are not available for - // pipelines on most graphics devices without extensions, and so they are - // only able to be used via `BlendFilterContents`. - kScreen, - kOverlay, - kDarken, - kLighten, - kColorDodge, - kColorBurn, - kHardLight, - kSoftLight, - kDifference, - kExclusion, - kMultiply, - kHue, - kSaturation, - kColor, - kLuminosity, - - kLastPipelineBlendMode = kModulate, - kLastAdvancedBlendMode = kLuminosity, - }; + static const BlendMode kLastPipelineBlendMode; + static const BlendMode kLastAdvancedBlendMode; /// An enum to define how to repeat, fold, or omit colors outside of the /// typically defined range of the source of the colors (such as the diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index b444ce593e..71a7bd735c 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -15,6 +15,7 @@ #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/contents/texture_contents.h" +#include "impeller/entity/entity.h" #include "impeller/entity/inline_pass_context.h" #include "impeller/geometry/path_builder.h" #include "impeller/renderer/allocator.h" @@ -38,7 +39,7 @@ void EntityPass::SetDelegate(std::unique_ptr delegate) { } void EntityPass::AddEntity(Entity entity) { - if (entity.GetBlendMode() > Entity::BlendMode::kLastPipelineBlendMode) { + if (entity.GetBlendMode() > Entity::kLastPipelineBlendMode) { reads_from_pass_texture_ += 1; } @@ -131,7 +132,7 @@ EntityPass* EntityPass::AddSubpass(std::unique_ptr pass) { FML_DCHECK(pass->superpass_ == nullptr); pass->superpass_ = this; - if (pass->blend_mode_ > Entity::BlendMode::kLastPipelineBlendMode || + if (pass->blend_mode_ > Entity::kLastPipelineBlendMode || pass->backdrop_filter_proc_.has_value()) { reads_from_pass_texture_ += 1; } @@ -204,7 +205,7 @@ bool EntityPass::Render(ContentContext& renderer, Entity entity; entity.SetContents(contents); - entity.SetBlendMode(Entity::BlendMode::kSourceOver); + entity.SetBlendMode(BlendMode::kSourceOver); entity.Render(renderer, *render_pass); } @@ -419,7 +420,7 @@ bool EntityPass::OnRender( Entity msaa_backdrop_entity; msaa_backdrop_entity.SetContents(std::move(msaa_backdrop_contents)); - msaa_backdrop_entity.SetBlendMode(Entity::BlendMode::kSource); + msaa_backdrop_entity.SetBlendMode(BlendMode::kSource); if (!msaa_backdrop_entity.Render(renderer, *result.pass)) { return false; } @@ -505,8 +506,7 @@ bool EntityPass::OnRender( /// Setup advanced blends. /// - if (result.entity.GetBlendMode() > - Entity::BlendMode::kLastPipelineBlendMode) { + if (result.entity.GetBlendMode() > Entity::kLastPipelineBlendMode) { // End the active pass and flush the buffer before rendering "advanced" // blends. Advanced blends work by binding the current render target // texture as an input ("destination"), blending with a second texture @@ -535,7 +535,7 @@ bool EntityPass::OnRender( FilterContents::MakeBlend(result.entity.GetBlendMode(), inputs); contents->SetCoverageCrop(result.entity.GetCoverage()); result.entity.SetContents(std::move(contents)); - result.entity.SetBlendMode(Entity::BlendMode::kSourceOver); + result.entity.SetBlendMode(BlendMode::kSourceOver); } //-------------------------------------------------------------------------- @@ -599,7 +599,7 @@ void EntityPass::SetStencilDepth(size_t stencil_depth) { stencil_depth_ = stencil_depth; } -void EntityPass::SetBlendMode(Entity::BlendMode blend_mode) { +void EntityPass::SetBlendMode(BlendMode blend_mode) { blend_mode_ = blend_mode; cover_whole_screen_ = Entity::BlendModeShouldCoverWholeScreen(blend_mode); } diff --git a/engine/src/flutter/impeller/entity/entity_pass.h b/engine/src/flutter/impeller/entity/entity_pass.h index 2a5879ef85..5a5d3fffdf 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.h +++ b/engine/src/flutter/impeller/entity/entity_pass.h @@ -57,7 +57,7 @@ class EntityPass { void SetStencilDepth(size_t stencil_depth); - void SetBlendMode(Entity::BlendMode blend_mode); + void SetBlendMode(BlendMode blend_mode); void SetBackdropFilter(std::optional proc); @@ -115,7 +115,7 @@ class EntityPass { EntityPass* superpass_ = nullptr; Matrix xformation_; size_t stencil_depth_ = 0u; - Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver; + BlendMode blend_mode_ = BlendMode::kSourceOver; bool cover_whole_screen_ = false; /// This value is incremented whenever something is added to the pass that diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 34c00a5b6b..ce7fdffa26 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -2,10 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include #include #include +#include +#include #include "flutter/testing/testing.h" +#include "fml/logging.h" +#include "gtest/gtest.h" #include "impeller/entity/contents/atlas_contents.h" #include "impeller/entity/contents/clip_contents.h" #include "impeller/entity/contents/contents.h" @@ -21,6 +26,7 @@ #include "impeller/entity/entity_pass.h" #include "impeller/entity/entity_pass_delegate.h" #include "impeller/entity/entity_playground.h" +#include "impeller/geometry/color.h" #include "impeller/geometry/geometry_unittests.h" #include "impeller/geometry/path_builder.h" #include "impeller/geometry/sigma.h" @@ -29,6 +35,7 @@ #include "impeller/renderer/render_pass.h" #include "impeller/renderer/vertex_buffer_builder.h" #include "impeller/tessellator/tessellator.h" +#include "include/core/SkBlendMode.h" #include "third_party/imgui/imgui.h" namespace impeller { @@ -139,7 +146,7 @@ TEST_P(EntityTest, EntityPassCoverageRespectsCoverageLimit) { TEST_P(EntityTest, FilterCoverageRespectsCropRect) { auto image = CreateTextureForFixture("boston.jpg"); - auto filter = FilterContents::MakeBlend(Entity::BlendMode::kSoftLight, + auto filter = FilterContents::MakeBlend(BlendMode::kSoftLight, FilterInput::Make({image})); // Without the crop rect (default behavior). @@ -645,59 +652,59 @@ TEST_P(EntityTest, SolidStrokeContentsSetMiter) { TEST_P(EntityTest, BlendingModeOptions) { std::vector blend_mode_names; - std::vector blend_mode_values; + std::vector blend_mode_values; { // Force an exhausiveness check with a switch. When adding blend modes, // update this switch with a new name/value to to make it selectable in the // test GUI. - const Entity::BlendMode b{}; - static_assert(b == Entity::BlendMode::kClear); // Ensure the first item in - // the switch is the first - // item in the enum. + const BlendMode b{}; + static_assert(b == BlendMode::kClear); // Ensure the first item in + // the switch is the first + // item in the enum. switch (b) { - case Entity::BlendMode::kClear: + case BlendMode::kClear: blend_mode_names.push_back("Clear"); - blend_mode_values.push_back(Entity::BlendMode::kClear); - case Entity::BlendMode::kSource: + blend_mode_values.push_back(BlendMode::kClear); + case BlendMode::kSource: blend_mode_names.push_back("Source"); - blend_mode_values.push_back(Entity::BlendMode::kSource); - case Entity::BlendMode::kDestination: + blend_mode_values.push_back(BlendMode::kSource); + case BlendMode::kDestination: blend_mode_names.push_back("Destination"); - blend_mode_values.push_back(Entity::BlendMode::kDestination); - case Entity::BlendMode::kSourceOver: + blend_mode_values.push_back(BlendMode::kDestination); + case BlendMode::kSourceOver: blend_mode_names.push_back("SourceOver"); - blend_mode_values.push_back(Entity::BlendMode::kSourceOver); - case Entity::BlendMode::kDestinationOver: + blend_mode_values.push_back(BlendMode::kSourceOver); + case BlendMode::kDestinationOver: blend_mode_names.push_back("DestinationOver"); - blend_mode_values.push_back(Entity::BlendMode::kDestinationOver); - case Entity::BlendMode::kSourceIn: + blend_mode_values.push_back(BlendMode::kDestinationOver); + case BlendMode::kSourceIn: blend_mode_names.push_back("SourceIn"); - blend_mode_values.push_back(Entity::BlendMode::kSourceIn); - case Entity::BlendMode::kDestinationIn: + blend_mode_values.push_back(BlendMode::kSourceIn); + case BlendMode::kDestinationIn: blend_mode_names.push_back("DestinationIn"); - blend_mode_values.push_back(Entity::BlendMode::kDestinationIn); - case Entity::BlendMode::kSourceOut: + blend_mode_values.push_back(BlendMode::kDestinationIn); + case BlendMode::kSourceOut: blend_mode_names.push_back("SourceOut"); - blend_mode_values.push_back(Entity::BlendMode::kSourceOut); - case Entity::BlendMode::kDestinationOut: + blend_mode_values.push_back(BlendMode::kSourceOut); + case BlendMode::kDestinationOut: blend_mode_names.push_back("DestinationOut"); - blend_mode_values.push_back(Entity::BlendMode::kDestinationOut); - case Entity::BlendMode::kSourceATop: + blend_mode_values.push_back(BlendMode::kDestinationOut); + case BlendMode::kSourceATop: blend_mode_names.push_back("SourceATop"); - blend_mode_values.push_back(Entity::BlendMode::kSourceATop); - case Entity::BlendMode::kDestinationATop: + blend_mode_values.push_back(BlendMode::kSourceATop); + case BlendMode::kDestinationATop: blend_mode_names.push_back("DestinationATop"); - blend_mode_values.push_back(Entity::BlendMode::kDestinationATop); - case Entity::BlendMode::kXor: + blend_mode_values.push_back(BlendMode::kDestinationATop); + case BlendMode::kXor: blend_mode_names.push_back("Xor"); - blend_mode_values.push_back(Entity::BlendMode::kXor); - case Entity::BlendMode::kPlus: + blend_mode_values.push_back(BlendMode::kXor); + case BlendMode::kPlus: blend_mode_names.push_back("Plus"); - blend_mode_values.push_back(Entity::BlendMode::kPlus); - case Entity::BlendMode::kModulate: + blend_mode_values.push_back(BlendMode::kPlus); + case BlendMode::kModulate: blend_mode_names.push_back("Modulate"); - blend_mode_values.push_back(Entity::BlendMode::kModulate); + blend_mode_values.push_back(BlendMode::kModulate); }; } @@ -711,8 +718,7 @@ TEST_P(EntityTest, BlendingModeOptions) { auto world_matrix = Matrix::MakeScale(GetContentScale()); auto draw_rect = [&context, &pass, &world_matrix]( - Rect rect, Color color, - Entity::BlendMode blend_mode) -> bool { + Rect rect, Color color, BlendMode blend_mode) -> bool { using VS = SolidFillPipeline::VertexShader; using FS = SolidFillPipeline::FragmentShader; @@ -762,7 +768,7 @@ TEST_P(EntityTest, BlendingModeOptions) { blend_mode_names.data(), blend_mode_names.size()); ImGui::End(); - Entity::BlendMode selected_mode = blend_mode_values[current_blend_index]; + BlendMode selected_mode = blend_mode_values[current_blend_index]; Point a, b, c, d; std::tie(a, b) = IMPELLER_PLAYGROUND_LINE( @@ -773,9 +779,9 @@ TEST_P(EntityTest, BlendingModeOptions) { bool result = true; result = result && draw_rect(Rect(0, 0, pass.GetRenderTargetSize().width, pass.GetRenderTargetSize().height), - Color(), Entity::BlendMode::kClear); + Color(), BlendMode::kClear); result = result && draw_rect(Rect::MakeLTRB(a.x, a.y, b.x, b.y), color1, - Entity::BlendMode::kSourceOver); + BlendMode::kSourceOver); result = result && draw_rect(Rect::MakeLTRB(c.x, c.y, d.x, d.y), color2, selected_mode); return result; @@ -819,11 +825,11 @@ TEST_P(EntityTest, Filters) { auto fi_boston = FilterInput::Make(boston); auto fi_kalimba = FilterInput::Make(kalimba); - auto blend0 = FilterContents::MakeBlend(Entity::BlendMode::kModulate, + auto blend0 = FilterContents::MakeBlend(BlendMode::kModulate, {fi_kalimba, fi_boston}); auto blend1 = FilterContents::MakeBlend( - Entity::BlendMode::kScreen, + BlendMode::kScreen, {fi_bridge, FilterInput::Make(blend0), fi_bridge, fi_bridge}); Entity entity; @@ -1100,9 +1106,9 @@ TEST_P(EntityTest, MorphologyFilter) { TEST_P(EntityTest, SetBlendMode) { Entity entity; - ASSERT_EQ(entity.GetBlendMode(), Entity::BlendMode::kSourceOver); - entity.SetBlendMode(Entity::BlendMode::kClear); - ASSERT_EQ(entity.GetBlendMode(), Entity::BlendMode::kClear); + ASSERT_EQ(entity.GetBlendMode(), BlendMode::kSourceOver); + entity.SetBlendMode(BlendMode::kClear); + ASSERT_EQ(entity.GetBlendMode(), BlendMode::kClear); } TEST_P(EntityTest, ContentsGetBoundsForEmptyPathReturnsNullopt) { @@ -1192,14 +1198,16 @@ TEST_P(EntityTest, BorderMaskBlurCoverageIsCorrect) { TEST_P(EntityTest, DrawVerticesSolidColorTrianglesWithoutIndices) { std::vector positions = {Point(100, 300), Point(200, 100), Point(300, 300)}; - std::vector colors = {Color::White(), Color::White(), Color::White()}; + std::vector colors = {Color::White(), Color::Green(), Color::White()}; Vertices vertices = Vertices(positions, {} /* indices */, colors, VertexMode::kTriangle, Rect(100, 100, 300, 300)); std::shared_ptr contents = std::make_shared(vertices); - contents->SetColor(Color::White()); + contents->SetBlendMode(BlendMode::kSourceOver); + contents->SetColor(Color::Red().WithAlpha(0.5)); + Entity e; e.SetTransformation(Matrix::MakeScale(GetContentScale())); e.SetContents(contents); @@ -1248,7 +1256,7 @@ TEST_P(EntityTest, DrawAtlasNoColor) { contents->SetTransforms(std::move(transforms)); contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); - contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetBlendMode(BlendMode::kSource); Entity e; e.SetTransformation(Matrix::MakeScale(GetContentScale())); @@ -1284,7 +1292,7 @@ TEST_P(EntityTest, DrawAtlasWithColor) { contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); contents->SetColors(colors); - contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetBlendMode(BlendMode::kSource); Entity e; e.SetTransformation(Matrix::MakeScale(GetContentScale())); @@ -1315,7 +1323,7 @@ TEST_P(EntityTest, DrawAtlasUsesProvidedCullRectForCoverage) { contents->SetTransforms(std::move(transforms)); contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); - contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetBlendMode(BlendMode::kSource); auto transform = Matrix::MakeScale(GetContentScale()); Entity e; @@ -1356,7 +1364,7 @@ TEST_P(EntityTest, DrawAtlasWithOpacity) { contents->SetTransforms(std::move(transforms)); contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); - contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetBlendMode(BlendMode::kSource); contents->SetAlpha(0.5); Entity e; @@ -1377,7 +1385,7 @@ TEST_P(EntityTest, DrawAtlasNoColorFullSize) { contents->SetTransforms(std::move(transforms)); contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); - contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetBlendMode(BlendMode::kSource); Entity e; e.SetTransformation(Matrix::MakeScale(GetContentScale())); @@ -1736,5 +1744,237 @@ TEST_P(EntityTest, SrgbToLinearFilter) { ASSERT_TRUE(OpenPlaygroundHere(callback)); } +TEST_P(EntityTest, TTTBlendColor) { + { + Color src = {1, 0, 0, 0.5}; + Color dst = {1, 0, 1, 1}; + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(1, 0, 0, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(1.5, 0, 0.5, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(1, 0, 0, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0.5, 0, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0.5, 0, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(1.5, 0, 0.5, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.5, 0, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(0.5, 0, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(1, 0, 0, 0.5)); + } + + { + Color src = {1, 1, 0, 1}; + Color dst = {1, 0, 1, 1}; + + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(1, 1, 0, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(1, 1, 0, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(1, 1, 0, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(1, 1, 0, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(1, 0, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), Color(1, 1, 1, 1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(1, 0, 0, 1)); + } + + { + Color src = {1, 1, 0, 0.2}; + Color dst = {1, 1, 1, 0.5}; + + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(1, 1, 0, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(1, 1, 1, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(1.8, 1.8, 0.8, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(1.5, 1.5, 1, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(0.5, 0.5, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0.2, 0.2, 0.2, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0.5, 0.5, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0.8, 0.8, 0.8, 0.4)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(1.3, 1.3, 0.8, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.7, 0.7, 0.2, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(1.3, 1.3, 0.8, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), + Color(1, 1, 1, 0.7)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(1, 1, 0, 0.1)); + } + + { + Color src = {1, 0.5, 0, 0.2}; + Color dst = {1, 1, 0.5, 0.5}; + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(1, 0.5, 0, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(1, 1, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(1.8, 1.3, 0.4, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(1.5, 1.25, 0.5, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(0.5, 0.25, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0.2, 0.2, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0.5, 0.25, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0.8, 0.8, 0.4, 0.4)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(1.3, 1.05, 0.4, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.7, 0.45, 0.1, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(1.3, 1.05, 0.4, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), + Color(1, 1, 0.5, 0.7)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(1, 0.5, 0, 0.1)); + } + + { + Color src = {0.5, 0.5, 0, 0.2}; + Color dst = {0, 1, 0.5, 0.5}; + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(0.5, 0.5, 0, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(0, 1, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(0.5, 1.3, 0.4, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(0.25, 1.25, 0.5, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(0.25, 0.25, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0, 0.2, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0.25, 0.25, 0, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0, 0.8, 0.4, 0.4)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(0.25, 1.05, 0.4, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.25, 0.45, 0.1, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(0.25, 1.05, 0.4, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), + Color(0.5, 1, 0.5, 0.7)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(0, 0.5, 0, 0.1)); + } + + { + Color src = {0.5, 0.5, 0.2, 0.2}; + Color dst = {0.2, 1, 0.5, 0.5}; + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(0.5, 0.5, 0.2, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(0.2, 1, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(0.66, 1.3, 0.6, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(0.45, 1.25, 0.6, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(0.25, 0.25, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0.04, 0.2, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0.25, 0.25, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0.16, 0.8, 0.4, 0.4)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(0.41, 1.05, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.29, 0.45, 0.2, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(0.41, 1.05, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), + Color(0.7, 1, 0.7, 0.7)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(0.1, 0.5, 0.1, 0.1)); + } + + { + Color src = {0.5, 0.5, 0.2, 0.2}; + Color dst = {0.2, 0.2, 0.5, 0.5}; + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kClear), + Color(0, 0, 0, 0)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSource), + Color(0.5, 0.5, 0.2, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestination), + Color(0.2, 0.2, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOver), + Color(0.66, 0.66, 0.6, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOver), + Color(0.45, 0.45, 0.6, 0.6)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceIn), + Color(0.25, 0.25, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationIn), + Color(0.04, 0.04, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceOut), + Color(0.25, 0.25, 0.1, 0.1)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationOut), + Color(0.16, 0.16, 0.4, 0.4)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kSourceATop), + Color(0.41, 0.41, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kDestinationATop), + Color(0.29, 0.29, 0.2, 0.2)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kXor), + Color(0.41, 0.41, 0.5, 0.5)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kPlus), + Color(0.7, 0.7, 0.7, 0.7)); + ASSERT_EQ(Color::BlendColor(src, dst, BlendMode::kModulate), + Color(0.1, 0.1, 0.1, 0.1)); + } +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/color.cc b/engine/src/flutter/impeller/geometry/color.cc index cc45636cdc..70946f66a8 100644 --- a/engine/src/flutter/impeller/geometry/color.cc +++ b/engine/src/flutter/impeller/geometry/color.cc @@ -8,6 +8,9 @@ #include #include +#include "impeller/geometry/scalar.h" +#include "impeller/geometry/vector.h" + namespace impeller { ColorHSB ColorHSB::FromRGB(Color rgb) { @@ -85,6 +88,156 @@ Color ColorHSB::ToRGBA() const { return Color(0, 0, 0, alpha); } +Color Color::operator+(const Color& c) const { + return Color(Vector4(*this) + Vector4(c)); +} + +Color Color::operator-(const Color& c) const { + return Color(Vector4(*this) - Vector4(c)); +} + +Color Color::operator*(Scalar value) const { + return Color(red * value, green * value, blue * value, alpha * value); +} + Color::Color(const ColorHSB& hsbColor) : Color(hsbColor.ToRGBA()) {} +Color::Color(const Vector4& value) + : red(value.x), green(value.y), blue(value.z), alpha(value.w) {} + +Color Min(Color c, float threshold) { + return Color(std::min(c.red, threshold), std::min(c.green, threshold), + std::min(c.blue, threshold), std::min(c.alpha, threshold)); +} + +Color Color::BlendColor(const Color& src, + const Color& dst, + BlendMode blend_mode) { + static auto apply_rgb_srcover_alpha = [&](auto f) -> Color { + return Color(f(src.red, dst.red), f(src.green, dst.green), + f(src.blue, dst.blue), + dst.alpha * (1 - src.alpha) + src.alpha // srcOver alpha + ); + }; + + switch (blend_mode) { + case BlendMode::kClear: + return Color::BlackTransparent(); + case BlendMode::kSource: + return src; + case BlendMode::kDestination: + return dst; + case BlendMode::kSourceOver: + // r = s + (1-sa)*d + return src + dst * (1 - src.alpha); + case BlendMode::kDestinationOver: + // r = d + (1-da)*s + return dst + src * (1 - dst.alpha); + case BlendMode::kSourceIn: + // r = s * da + return src * dst.alpha; + case BlendMode::kDestinationIn: + // r = d * sa + return dst * src.alpha; + case BlendMode::kSourceOut: + // r = s * ( 1- da) + return src * (1 - dst.alpha); + case BlendMode::kDestinationOut: + // r = d * (1-sa) + return dst * (1 - src.alpha); + case BlendMode::kSourceATop: + // r = s*da + d*(1-sa) + return src * dst.alpha + dst * (1 - src.alpha); + case BlendMode::kDestinationATop: + // r = d*sa + s*(1-da) + return dst * src.alpha + src * (1 - dst.alpha); + case BlendMode::kXor: + // r = s*(1-da) + d*(1-sa) + return src * (1 - dst.alpha) + dst * (1 - src.alpha); + case BlendMode::kPlus: + // r = min(s + d, 1) + return Min(src + dst, 1); + case BlendMode::kModulate: + // r = s*d + return src * dst; + case BlendMode::kScreen: { + // r = s + d - s*d + return src + dst - src * dst; + } + case BlendMode::kOverlay: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + if (d * 2 < dst.alpha) { + return 2 * s * d; + } + return src.alpha * dst.alpha - 2 * (dst.alpha - s) * (src.alpha - d); + }); + case BlendMode::kDarken: { + return apply_rgb_srcover_alpha([&](auto s, auto d) { + return (1 - dst.alpha) * s + (1 - src.alpha) * d + std::min(s, d); + }); + } + case BlendMode::kLighten: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + return (1 - dst.alpha) * s + (1 - src.alpha) * d + std::max(s, d); + }); + case BlendMode::kColorDodge: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + if (d == 0) { + return s * (1 - src.alpha); + } + if (s == src.alpha) { + return s + dst.alpha * (1 - src.alpha); + } + return src.alpha * + std::min(dst.alpha, d * src.alpha / (src.alpha - s)) + + s * (1 - dst.alpha + dst.alpha * (1 - src.alpha)); + }); + case BlendMode::kColorBurn: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + if (s == 0) { + return dst.alpha * (1 - src.alpha); + } + if (d == dst.alpha) { + return d + s * (1 - dst.alpha); + } + // s.a * (d.a - min(d.a, (d.a - s) * s.a/s)) + s * (1-d.a) + d.a * (1 - + // s.a) + return src.alpha * + (dst.alpha - + std::min(dst.alpha, (dst.alpha - d) * src.alpha / s)) + + s * (1 - dst.alpha) + dst.alpha * (1 - src.alpha); + }); + case BlendMode::kHardLight: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + if (src.alpha >= s * (1 - dst.alpha) + d * (1 - src.alpha) + 2 * s) { + return 2 * s * d; + } + // s.a * d.a - 2 * (d.a - d) * (s.a - s) + return src.alpha * dst.alpha - 2 * (dst.alpha - d) * (src.alpha - s); + }); + case BlendMode::kDifference: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + // s + d - 2 * min(s * d.a, d * s.a); + return s + d - 2 * std::min(s * dst.alpha, d * src.alpha); + }); + case BlendMode::kExclusion: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + // s + d - 2 * s * d + return s + d - 2 * s * d; + }); + case BlendMode::kMultiply: + return apply_rgb_srcover_alpha([&](auto s, auto d) { + // s * (1 - d.a) + d * (1 - s.a) + (s * d) + return s * (1 - dst.alpha) + d * (1 - src.alpha) + (s * d); + }); + case BlendMode::kHue: + case BlendMode::kSaturation: + case BlendMode::kColor: + case BlendMode::kLuminosity: + case BlendMode::kSoftLight: + default: + return src + dst * (1 - src.alpha); + } +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/color.h b/engine/src/flutter/impeller/geometry/color.h index 623cb510be..64c1d6b984 100644 --- a/engine/src/flutter/impeller/geometry/color.h +++ b/engine/src/flutter/impeller/geometry/color.h @@ -8,12 +8,52 @@ #include #include #include - #include "impeller/geometry/scalar.h" namespace impeller { struct ColorHSB; +struct Vector4; + +/// All blend modes assume that both the source (fragment output) and +/// destination (first color attachment) have colors with premultiplied alpha. +enum class BlendMode { + // The following blend modes are able to be used as pipeline blend modes or + // via `BlendFilterContents`. + kClear, + kSource, + kDestination, + kSourceOver, + kDestinationOver, + kSourceIn, + kDestinationIn, + kSourceOut, + kDestinationOut, + kSourceATop, + kDestinationATop, + kXor, + kPlus, + kModulate, + + // The following blend modes use equations that are not available for + // pipelines on most graphics devices without extensions, and so they are + // only able to be used via `BlendFilterContents`. + kScreen, + kOverlay, + kDarken, + kLighten, + kColorDodge, + kColorBurn, + kHardLight, + kSoftLight, + kDifference, + kExclusion, + kMultiply, + kHue, + kSaturation, + kColor, + kLuminosity, +}; /** * Represents a RGBA color @@ -43,6 +83,8 @@ struct Color { explicit Color(const ColorHSB& hsbColor); + Color(const Vector4& value); + constexpr Color(Scalar r, Scalar g, Scalar b, Scalar a) : red(r), green(g), blue(b), alpha(a) {} @@ -52,8 +94,8 @@ struct Color { } constexpr bool operator==(const Color& c) const { - return red == c.red && green == c.green && blue == c.blue && - alpha == c.alpha; + return ScalarNearlyEqual(red, c.red) && ScalarNearlyEqual(green, c.green) && + ScalarNearlyEqual(blue, c.blue) && ScalarNearlyEqual(alpha, c.alpha); } constexpr Color Premultiply() const { @@ -690,6 +732,20 @@ struct Color { }; } + static Color BlendColor(const Color& src, + const Color& dst, + BlendMode blend_mode); + + Color operator*(const Color& c) const { + return Color(red * c.red, green * c.green, blue * c.blue, alpha * c.alpha); + } + + Color operator+(const Color& c) const; + + Color operator-(const Color& c) const; + + Color operator*(Scalar value) const; + constexpr bool IsTransparent() const { return alpha == 0.0; } constexpr bool IsOpaque() const { return alpha == 1.0; } diff --git a/engine/src/flutter/impeller/geometry/vector.h b/engine/src/flutter/impeller/geometry/vector.h index b1621f0147..8a30854e5b 100644 --- a/engine/src/flutter/impeller/geometry/vector.h +++ b/engine/src/flutter/impeller/geometry/vector.h @@ -186,6 +186,10 @@ struct Vector4 { return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); } + constexpr Vector4 operator*(float f) const { + return Vector4(x * f, y * f, z * f, w * f); + } + std::string ToString() const; };