Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6d86877

Browse files
authored
[Impeller] Include CPU blends + Flutter blend reference image in goldens (#43117)
Part of flutter/flutter#128606. - Adds blend imagery that matches the Flutter BlendMode documentation: https://api.flutter.dev/flutter/dart-ui/BlendMode.html - Adds a clear side-by-side comparison of SaveLayer blending vs CPU blending to make verifying correctness possible. - Top squares are SaveLayer-applied blends. - Bottom squares are CPU blends.
1 parent 64a20eb commit 6d86877

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,20 +2081,94 @@ TEST_P(AiksTest, SrgbToLinearFilterSubpassCollapseOptimization) {
20812081
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
20822082
}
20832083

2084-
static Picture BlendModeSaveLayerTest(BlendMode blend_mode) {
2084+
static Picture BlendModeTest(BlendMode blend_mode,
2085+
const std::shared_ptr<Image>& src_image,
2086+
const std::shared_ptr<Image>& dst_image) {
2087+
Color destination_color = Color::CornflowerBlue().WithAlpha(0.75);
2088+
auto source_colors =
2089+
std::vector<Color>({Color::White(), Color::LimeGreen(), Color::Black()});
2090+
20852091
Canvas canvas;
2086-
canvas.DrawPaint({.color = Color::CornflowerBlue().WithAlpha(0.75)});
2087-
canvas.SaveLayer({.blend_mode = blend_mode});
2088-
for (auto& color : {Color::White(), Color::LimeGreen(), Color::Black()}) {
2089-
canvas.DrawRect({100, 100, 200, 200}, {.color = color.WithAlpha(0.75)});
2090-
canvas.Translate(Vector2(150, 100));
2092+
2093+
canvas.DrawPaint({.color = Color::Black()});
2094+
2095+
//----------------------------------------------------------------------------
2096+
/// 1. Save layer blending (top left).
2097+
///
2098+
2099+
canvas.Save();
2100+
for (auto& color : source_colors) {
2101+
canvas.Save();
2102+
{
2103+
canvas.ClipRect(Rect::MakeXYWH(50, 50, 100, 100));
2104+
// Perform the blend in a SaveLayer so that the initial backdrop color is
2105+
// fully transparent black. SourceOver blend the result onto the parent
2106+
// pass.
2107+
canvas.SaveLayer({});
2108+
{
2109+
canvas.DrawPaint({.color = destination_color});
2110+
// Draw the source color in an offscreen pass and blend it to the parent
2111+
// pass.
2112+
canvas.SaveLayer({.blend_mode = blend_mode});
2113+
{ //
2114+
canvas.DrawRect({50, 50, 100, 100}, {.color = color.WithAlpha(0.75)});
2115+
}
2116+
canvas.Restore();
2117+
}
2118+
canvas.Restore();
2119+
}
2120+
canvas.Restore();
2121+
canvas.Translate(Vector2(100, 0));
20912122
}
2123+
canvas.RestoreToCount(0);
2124+
2125+
//----------------------------------------------------------------------------
2126+
/// 2. CPU blend modes (top left).
2127+
///
2128+
2129+
canvas.Save();
2130+
canvas.Translate({0, 100});
2131+
2132+
// Perform the blend in a SaveLayer so that the initial backdrop color is
2133+
// fully transparent black. SourceOver blend the result onto the parent pass.
2134+
canvas.SaveLayer({});
2135+
// canvas.DrawPaint({.color = destination_color});
2136+
for (auto& color : source_colors) {
2137+
// Simply write the CPU blended color to the pass.
2138+
canvas.DrawRect({50, 50, 100, 100}, {.color = destination_color.Blend(
2139+
color.WithAlpha(0.75), blend_mode),
2140+
.blend_mode = BlendMode::kSource});
2141+
canvas.Translate(Vector2(100, 0));
2142+
}
2143+
canvas.RestoreToCount(0);
2144+
2145+
//----------------------------------------------------------------------------
2146+
/// 3. Image blending (top right).
2147+
///
2148+
/// Compare these results with the images in the Flutter blend mode
2149+
/// documentation: https://api.flutter.dev/flutter/dart-ui/BlendMode.html
2150+
///
2151+
2152+
canvas.Save();
2153+
// canvas.ClipRect(Rect::MakeXYWH(500, 0, 500, 500));
2154+
canvas.SaveLayer({.blend_mode = BlendMode::kSourceOver});
2155+
{
2156+
canvas.DrawImage(dst_image, {400, 50}, {.blend_mode = BlendMode::kSource});
2157+
canvas.DrawImage(src_image, {400, 50}, {.blend_mode = blend_mode});
2158+
}
2159+
canvas.RestoreToCount(0);
2160+
20922161
return canvas.EndRecordingAsPicture();
20932162
}
20942163

2095-
#define BLEND_MODE_TEST(blend_mode) \
2096-
TEST_P(AiksTest, BlendModeSaveLayer##blend_mode) { \
2097-
OpenPlaygroundHere(BlendModeSaveLayerTest(BlendMode::k##blend_mode)); \
2164+
#define BLEND_MODE_TEST(blend_mode) \
2165+
TEST_P(AiksTest, BlendMode##blend_mode) { \
2166+
auto src_image = std::make_shared<Image>( \
2167+
CreateTextureForFixture("blend_mode_src.png")); \
2168+
auto dst_image = std::make_shared<Image>( \
2169+
CreateTextureForFixture("blend_mode_dst.png")); \
2170+
OpenPlaygroundHere( \
2171+
BlendModeTest(BlendMode::k##blend_mode, src_image, dst_image)); \
20982172
}
20992173
IMPELLER_FOR_EACH_BLEND_MODE(BLEND_MODE_TEST)
21002174

impeller/fixtures/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ test_fixtures("file_fixtures") {
7070
"//flutter/third_party/txt/third_party/fonts/Roboto-Regular.ttf",
7171
"airplane.jpg",
7272
"bay_bridge.jpg",
73+
"blend_mode_dst.png",
74+
"blend_mode_src.png",
7375
"blue_noise.png",
7476
"boston.jpg",
7577
"embarcadero.jpg",

impeller/fixtures/blend_mode_dst.png

171 KB
Loading

impeller/fixtures/blend_mode_src.png

33.7 KB
Loading

0 commit comments

Comments
 (0)