@@ -1212,6 +1212,59 @@ TEST_P(AiksTest, CanDrawPaintWithAdvancedBlend) {
12121212 ASSERT_TRUE (OpenPlaygroundHere (canvas.EndRecordingAsPicture ()));
12131213}
12141214
1215+ #define BLEND_MODE_TUPLE (blend_mode ) {#blend_mode, BlendMode::k##blend_mode},
1216+
1217+ struct BlendModeSelection {
1218+ std::vector<const char *> blend_mode_names;
1219+ std::vector<BlendMode> blend_mode_values;
1220+ };
1221+
1222+ static BlendModeSelection GetBlendModeSelection () {
1223+ std::vector<const char *> blend_mode_names;
1224+ std::vector<BlendMode> blend_mode_values;
1225+ {
1226+ const std::vector<std::tuple<const char *, BlendMode>> blends = {
1227+ IMPELLER_FOR_EACH_BLEND_MODE (BLEND_MODE_TUPLE)};
1228+ assert (blends.size () ==
1229+ static_cast <size_t >(Entity::kLastAdvancedBlendMode ) + 1 );
1230+ for (const auto & [name, mode] : blends) {
1231+ blend_mode_names.push_back (name);
1232+ blend_mode_values.push_back (mode);
1233+ }
1234+ }
1235+
1236+ return {blend_mode_names, blend_mode_values};
1237+ }
1238+
1239+ TEST_P (AiksTest, CanDrawPaintMultipleTimesInteractive) {
1240+ auto modes = GetBlendModeSelection ();
1241+
1242+ auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
1243+ static Color background = Color::MediumTurquoise ();
1244+ static Color foreground = Color::Color::OrangeRed ().WithAlpha (0.5 );
1245+ static int current_blend_index = 3 ;
1246+
1247+ ImGui::Begin (" Controls" , nullptr , ImGuiWindowFlags_AlwaysAutoResize);
1248+ {
1249+ ImGui::ColorEdit4 (" Background" , reinterpret_cast <float *>(&background));
1250+ ImGui::ColorEdit4 (" Foreground" , reinterpret_cast <float *>(&foreground));
1251+ ImGui::ListBox (" Blend mode" , ¤t_blend_index,
1252+ modes.blend_mode_names .data (),
1253+ modes.blend_mode_names .size ());
1254+ }
1255+ ImGui::End ();
1256+
1257+ Canvas canvas;
1258+ canvas.Scale (Vector2 (0.2 , 0.2 ));
1259+ canvas.DrawPaint ({.color = background});
1260+ canvas.DrawPaint (
1261+ {.color = foreground,
1262+ .blend_mode = static_cast <BlendMode>(current_blend_index)});
1263+ return renderer.Render (canvas.EndRecordingAsPicture (), render_target);
1264+ };
1265+ ASSERT_TRUE (OpenPlaygroundHere (callback));
1266+ }
1267+
12151268TEST_P (AiksTest, PaintBlendModeIsRespected) {
12161269 Paint paint;
12171270 Canvas canvas;
@@ -1231,23 +1284,10 @@ TEST_P(AiksTest, PaintBlendModeIsRespected) {
12311284 ASSERT_TRUE (OpenPlaygroundHere (canvas.EndRecordingAsPicture ()));
12321285}
12331286
1234- #define BLEND_MODE_TUPLE (blend_mode ) {#blend_mode, BlendMode::k##blend_mode},
1235-
12361287TEST_P (AiksTest, ColorWheel) {
12371288 // Compare with https://fiddle.skia.org/c/@BlendModes
12381289
1239- std::vector<const char *> blend_mode_names;
1240- std::vector<BlendMode> blend_mode_values;
1241- {
1242- const std::vector<std::tuple<const char *, BlendMode>> blends = {
1243- IMPELLER_FOR_EACH_BLEND_MODE (BLEND_MODE_TUPLE)};
1244- assert (blends.size () ==
1245- static_cast <size_t >(Entity::kLastAdvancedBlendMode ) + 1 );
1246- for (const auto & [name, mode] : blends) {
1247- blend_mode_names.push_back (name);
1248- blend_mode_values.push_back (mode);
1249- }
1250- }
1290+ BlendModeSelection blend_modes = GetBlendModeSelection ();
12511291
12521292 auto draw_color_wheel = [](Canvas& canvas) {
12531293 // / color_wheel_sampler: r=0 -> fuchsia, r=2pi/3 -> yellow, r=4pi/3 ->
@@ -1303,7 +1343,8 @@ TEST_P(AiksTest, ColorWheel) {
13031343 {
13041344 ImGui::Checkbox (" Cache the wheel" , &cache_the_wheel);
13051345 ImGui::ListBox (" Blending mode" , ¤t_blend_index,
1306- blend_mode_names.data (), blend_mode_names.size ());
1346+ blend_modes.blend_mode_names .data (),
1347+ blend_modes.blend_mode_names .size ());
13071348 ImGui::SliderFloat (" Source alpha" , &src_alpha, 0 , 1 );
13081349 ImGui::ColorEdit4 (" Color A" , reinterpret_cast <float *>(&color0));
13091350 ImGui::ColorEdit4 (" Color B" , reinterpret_cast <float *>(&color1));
@@ -1356,8 +1397,9 @@ TEST_P(AiksTest, ColorWheel) {
13561397 canvas.Scale (Vector2 (3 , 3 ));
13571398
13581399 // Draw 3 circles to a subpass and blend it in.
1359- canvas.SaveLayer ({.color = Color::White ().WithAlpha (src_alpha),
1360- .blend_mode = blend_mode_values[current_blend_index]});
1400+ canvas.SaveLayer (
1401+ {.color = Color::White ().WithAlpha (src_alpha),
1402+ .blend_mode = blend_modes.blend_mode_values [current_blend_index]});
13611403 {
13621404 Paint paint;
13631405 paint.blend_mode = BlendMode::kPlus ;
0 commit comments