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

Commit 807c2ba

Browse files
committed
[Impeller] Add interactive DrawPaint blend test
1 parent 326de1d commit 807c2ba

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,59 @@ TEST_P(AiksTest, CanDrawPaintMultipleTimes) {
12031203
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
12041204
}
12051205

1206+
#define BLEND_MODE_TUPLE(blend_mode) {#blend_mode, BlendMode::k##blend_mode},
1207+
1208+
struct BlendModeSelection {
1209+
std::vector<const char*> blend_mode_names;
1210+
std::vector<BlendMode> blend_mode_values;
1211+
};
1212+
1213+
static BlendModeSelection GetBlendModeSelection() {
1214+
std::vector<const char*> blend_mode_names;
1215+
std::vector<BlendMode> blend_mode_values;
1216+
{
1217+
const std::vector<std::tuple<const char*, BlendMode>> blends = {
1218+
IMPELLER_FOR_EACH_BLEND_MODE(BLEND_MODE_TUPLE)};
1219+
assert(blends.size() ==
1220+
static_cast<size_t>(Entity::kLastAdvancedBlendMode) + 1);
1221+
for (const auto& [name, mode] : blends) {
1222+
blend_mode_names.push_back(name);
1223+
blend_mode_values.push_back(mode);
1224+
}
1225+
}
1226+
1227+
return {blend_mode_names, blend_mode_values};
1228+
}
1229+
1230+
TEST_P(AiksTest, CanDrawPaintMultipleTimesInteractive) {
1231+
auto modes = GetBlendModeSelection();
1232+
1233+
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
1234+
static Color background = Color::MediumTurquoise();
1235+
static Color foreground = Color::Color::OrangeRed().WithAlpha(0.5);
1236+
static int current_blend_index = 3;
1237+
1238+
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
1239+
{
1240+
ImGui::ColorEdit4("Background", reinterpret_cast<float*>(&background));
1241+
ImGui::ColorEdit4("Foreground", reinterpret_cast<float*>(&foreground));
1242+
ImGui::ListBox("Blend mode", &current_blend_index,
1243+
modes.blend_mode_names.data(),
1244+
modes.blend_mode_names.size());
1245+
}
1246+
ImGui::End();
1247+
1248+
Canvas canvas;
1249+
canvas.Scale(Vector2(0.2, 0.2));
1250+
canvas.DrawPaint({.color = background});
1251+
canvas.DrawPaint(
1252+
{.color = foreground,
1253+
.blend_mode = static_cast<BlendMode>(current_blend_index)});
1254+
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
1255+
};
1256+
ASSERT_TRUE(OpenPlaygroundHere(callback));
1257+
}
1258+
12061259
TEST_P(AiksTest, PaintBlendModeIsRespected) {
12071260
Paint paint;
12081261
Canvas canvas;
@@ -1222,23 +1275,10 @@ TEST_P(AiksTest, PaintBlendModeIsRespected) {
12221275
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
12231276
}
12241277

1225-
#define BLEND_MODE_TUPLE(blend_mode) {#blend_mode, BlendMode::k##blend_mode},
1226-
12271278
TEST_P(AiksTest, ColorWheel) {
12281279
// Compare with https://fiddle.skia.org/c/@BlendModes
12291280

1230-
std::vector<const char*> blend_mode_names;
1231-
std::vector<BlendMode> blend_mode_values;
1232-
{
1233-
const std::vector<std::tuple<const char*, BlendMode>> blends = {
1234-
IMPELLER_FOR_EACH_BLEND_MODE(BLEND_MODE_TUPLE)};
1235-
assert(blends.size() ==
1236-
static_cast<size_t>(Entity::kLastAdvancedBlendMode) + 1);
1237-
for (const auto& [name, mode] : blends) {
1238-
blend_mode_names.push_back(name);
1239-
blend_mode_values.push_back(mode);
1240-
}
1241-
}
1281+
BlendModeSelection blend_modes = GetBlendModeSelection();
12421282

12431283
auto draw_color_wheel = [](Canvas& canvas) {
12441284
/// color_wheel_sampler: r=0 -> fuchsia, r=2pi/3 -> yellow, r=4pi/3 ->
@@ -1294,7 +1334,8 @@ TEST_P(AiksTest, ColorWheel) {
12941334
{
12951335
ImGui::Checkbox("Cache the wheel", &cache_the_wheel);
12961336
ImGui::ListBox("Blending mode", &current_blend_index,
1297-
blend_mode_names.data(), blend_mode_names.size());
1337+
blend_modes.blend_mode_names.data(),
1338+
blend_modes.blend_mode_names.size());
12981339
ImGui::SliderFloat("Source alpha", &src_alpha, 0, 1);
12991340
ImGui::ColorEdit4("Color A", reinterpret_cast<float*>(&color0));
13001341
ImGui::ColorEdit4("Color B", reinterpret_cast<float*>(&color1));
@@ -1347,8 +1388,9 @@ TEST_P(AiksTest, ColorWheel) {
13471388
canvas.Scale(Vector2(3, 3));
13481389

13491390
// Draw 3 circles to a subpass and blend it in.
1350-
canvas.SaveLayer({.color = Color::White().WithAlpha(src_alpha),
1351-
.blend_mode = blend_mode_values[current_blend_index]});
1391+
canvas.SaveLayer(
1392+
{.color = Color::White().WithAlpha(src_alpha),
1393+
.blend_mode = blend_modes.blend_mode_values[current_blend_index]});
13521394
{
13531395
Paint paint;
13541396
paint.blend_mode = BlendMode::kPlus;

0 commit comments

Comments
 (0)