@@ -2081,20 +2081,94 @@ TEST_P(AiksTest, SrgbToLinearFilterSubpassCollapseOptimization) {
2081
2081
ASSERT_TRUE (OpenPlaygroundHere (canvas.EndRecordingAsPicture ()));
2082
2082
}
2083
2083
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
+
2085
2091
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 ));
2091
2122
}
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
+
2092
2161
return canvas.EndRecordingAsPicture ();
2093
2162
}
2094
2163
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)); \
2098
2172
}
2099
2173
IMPELLER_FOR_EACH_BLEND_MODE (BLEND_MODE_TEST)
2100
2174
0 commit comments