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

[Impeller] replaced playground macros with functions #50602

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions impeller/aiks/aiks_blur_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ TEST_P(AiksTest, CanRenderForegroundAdvancedBlendWithMaskBlur) {

TEST_P(AiksTest, CanRenderBackdropBlurInteractive) {
auto callback = [&](AiksContext& renderer) -> std::optional<Picture> {
auto [a, b] = IMPELLER_PLAYGROUND_LINE(Point(50, 50), Point(300, 200), 30,
Color::White(), Color::White());
static PlaygroundPoint point_a(Point(50, 50), 30, Color::White());
static PlaygroundPoint point_b(Point(300, 200), 30, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention behind this is change is make tests where these are used golden-able, would just changing the macros to forward defaults when IMPELLER_GOLDEN_TESTS is enabled work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention is to get this code compliant with the c++ style guide: https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros

My next PR will get these running in golden image tests. This is just cleanup. That's an interesting idea with IMPELLER_GOLDEN_TESTS. I will probably avoid latching more decisions on preprocessor macros and instead rely on AiksPlayground vs GoldenPlayground.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool, I was able to just gate the logic on the presence of an ImGui context. I'll include you in the follow up PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is: #50606

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense... At some point we could change this to use ImGui's actual identity/storage system so that they can remain as one function without the statics. Added an issue here.


Canvas canvas;
canvas.DrawCircle({100, 100}, 50, {.color = Color::CornflowerBlue()});
Expand Down Expand Up @@ -143,7 +144,9 @@ TEST_P(AiksTest, CanRenderClippedBlur) {

TEST_P(AiksTest, ClippedBlurFilterRendersCorrectlyInteractive) {
auto callback = [&](AiksContext& renderer) -> std::optional<Picture> {
auto point = IMPELLER_PLAYGROUND_POINT(Point(400, 400), 20, Color::Green());
static PlaygroundPoint playground_point(Point(400, 400), 20,
Color::Green());
auto point = DrawPlaygroundPoint(playground_point);

Canvas canvas;
canvas.Translate(point - Point(400, 400));
Expand Down Expand Up @@ -465,8 +468,9 @@ TEST_P(AiksTest, GaussianBlurRotatedAndClippedInteractive) {
ImageFilter::MakeBlur(Sigma(20.0), Sigma(20.0),
FilterContents::BlurStyle::kNormal,
tile_modes[selected_tile_mode])};
auto [handle_a, handle_b] = IMPELLER_PLAYGROUND_LINE(
Point(362, 309), Point(662, 459), 20, Color::Red(), Color::Red());
static PlaygroundPoint point_a(Point(362, 309), 20, Color::Red());
static PlaygroundPoint point_b(Point(662, 459), 20, Color::Red());
auto [handle_a, handle_b] = DrawPlaygroundLine(point_a, point_b);
Vector2 center = Vector2(1024, 768) / 2;
canvas.Scale(GetContentScale());
canvas.ClipRect(
Expand Down Expand Up @@ -567,8 +571,9 @@ TEST_P(AiksTest, GaussianBlurAnimatedBackdrop) {
Point(1024 / 2 - boston->GetSize().width / 2,
(768 / 2 - boston->GetSize().height / 2) + y),
{});
auto [handle_a, handle_b] = IMPELLER_PLAYGROUND_LINE(
Point(100, 100), Point(900, 700), 20, Color::Red(), Color::Red());
static PlaygroundPoint point_a(Point(100, 100), 20, Color::Red());
static PlaygroundPoint point_b(Point(900, 700), 20, Color::Red());
auto [handle_a, handle_b] = DrawPlaygroundLine(point_a, point_b);
canvas.ClipRect(
Rect::MakeLTRB(handle_a.x, handle_a.y, handle_b.x, handle_b.y));
canvas.ClipRect(Rect::MakeLTRB(100, 100, 900, 700));
Expand Down
8 changes: 6 additions & 2 deletions impeller/aiks/aiks_gradient_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,12 @@ TEST_P(AiksTest, GradientStrokesRenderCorrectly) {
canvas.Scale(Vector2(scale, scale));

if (add_circle_clip) {
auto [handle_a, handle_b] = IMPELLER_PLAYGROUND_LINE(
Point(60, 300), Point(600, 300), 20, Color::Red(), Color::Red());
static PlaygroundPoint circle_clip_point_a(Point(60, 300), 20,
Color::Red());
static PlaygroundPoint circle_clip_point_b(Point(600, 300), 20,
Color::Red());
auto [handle_a, handle_b] =
DrawPlaygroundLine(circle_clip_point_a, circle_clip_point_b);

auto screen_to_canvas = canvas.GetCurrentTransform().Invert();
Point point_a = screen_to_canvas * handle_a * GetContentScale();
Expand Down
8 changes: 6 additions & 2 deletions impeller/aiks/aiks_path_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,12 @@ TEST_P(AiksTest, SolidStrokesRenderCorrectly) {
canvas.Scale(Vector2(scale, scale));

if (add_circle_clip) {
auto [handle_a, handle_b] = IMPELLER_PLAYGROUND_LINE(
Point(60, 300), Point(600, 300), 20, Color::Red(), Color::Red());
static PlaygroundPoint circle_clip_point_a(Point(60, 300), 20,
Color::Red());
static PlaygroundPoint circle_clip_point_b(Point(600, 300), 20,
Color::Red());
auto [handle_a, handle_b] =
DrawPlaygroundLine(circle_clip_point_a, circle_clip_point_b);

auto screen_to_canvas = canvas.GetCurrentTransform().Invert();
Point point_a = screen_to_canvas * handle_a * GetContentScale();
Expand Down
5 changes: 3 additions & 2 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,9 @@ TEST_P(AiksTest, CoverageOriginShouldBeAccountedForInSubpasses) {
const auto offset = Point{25, 25};
const auto size = Size(100, 100);

auto [b0, b1] = IMPELLER_PLAYGROUND_LINE(Point(40, 40), Point(160, 160), 10,
Color::White(), Color::White());
static PlaygroundPoint point_a(Point(40, 40), 10, Color::White());
static PlaygroundPoint point_b(Point(160, 160), 10, Color::White());
auto [b0, b1] = DrawPlaygroundLine(point_a, point_b);
auto bounds = Rect::MakeLTRB(b0.x, b0.y, b1.x, b1.y);

canvas.DrawRect(bounds, Paint{.color = Color::Yellow(),
Expand Down
14 changes: 8 additions & 6 deletions impeller/display_list/dl_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ TEST_P(DisplayListTest, CanDrawArc) {
break;
}

auto [p1, p2] = IMPELLER_PLAYGROUND_LINE(
Point(200, 200), Point(400, 400), 20, Color::White(), Color::White());
static PlaygroundPoint point_a(Point(200, 200), 20, Color::White());
static PlaygroundPoint point_b(Point(400, 400), 20, Color::White());
auto [p1, p2] = DrawPlaygroundLine(point_a, point_b);

flutter::DisplayListBuilder builder;
flutter::DlPaint paint;
Expand Down Expand Up @@ -676,8 +677,9 @@ TEST_P(DisplayListTest, CanDrawBackdropFilter) {

std::optional<SkRect> bounds;
if (use_bounds) {
auto [p1, p2] = IMPELLER_PLAYGROUND_LINE(
Point(350, 150), Point(800, 600), 20, Color::White(), Color::White());
static PlaygroundPoint point_a(Point(350, 150), 20, Color::White());
static PlaygroundPoint point_b(Point(800, 600), 20, Color::White());
auto [p1, p2] = DrawPlaygroundLine(point_a, point_b);
bounds = SkRect::MakeLTRB(p1.x, p1.y, p2.x, p2.y);
}

Expand All @@ -694,8 +696,8 @@ TEST_P(DisplayListTest, CanDrawBackdropFilter) {
&filter);

if (draw_circle) {
auto circle_center =
IMPELLER_PLAYGROUND_POINT(Point(500, 400), 20, Color::Red());
static PlaygroundPoint center_point(Point(500, 400), 20, Color::Red());
auto circle_center = DrawPlaygroundPoint(center_point);

flutter::DlPaint paint;
paint.setDrawStyle(flutter::DlDrawStyle::kStroke);
Expand Down
106 changes: 65 additions & 41 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,21 @@ TEST_P(EntityTest, TriangleInsideASquare) {
auto callback = [&](ContentContext& context, RenderPass& pass) {
Point offset(100, 100);

Point a =
IMPELLER_PLAYGROUND_POINT(Point(10, 10) + offset, 20, Color::White());
Point b =
IMPELLER_PLAYGROUND_POINT(Point(210, 10) + offset, 20, Color::White());
Point c =
IMPELLER_PLAYGROUND_POINT(Point(210, 210) + offset, 20, Color::White());
Point d =
IMPELLER_PLAYGROUND_POINT(Point(10, 210) + offset, 20, Color::White());
Point e =
IMPELLER_PLAYGROUND_POINT(Point(50, 50) + offset, 20, Color::White());
Point f =
IMPELLER_PLAYGROUND_POINT(Point(100, 50) + offset, 20, Color::White());
Point g =
IMPELLER_PLAYGROUND_POINT(Point(50, 150) + offset, 20, Color::White());
static PlaygroundPoint point_a(Point(10, 10) + offset, 20, Color::White());
Point a = DrawPlaygroundPoint(point_a);
static PlaygroundPoint point_b(Point(210, 10) + offset, 20, Color::White());
Point b = DrawPlaygroundPoint(point_b);
static PlaygroundPoint point_c(Point(210, 210) + offset, 20,
Color::White());
Point c = DrawPlaygroundPoint(point_c);
static PlaygroundPoint point_d(Point(10, 210) + offset, 20, Color::White());
Point d = DrawPlaygroundPoint(point_d);
static PlaygroundPoint point_e(Point(50, 50) + offset, 20, Color::White());
Point e = DrawPlaygroundPoint(point_e);
static PlaygroundPoint point_f(Point(100, 50) + offset, 20, Color::White());
Point f = DrawPlaygroundPoint(point_f);
static PlaygroundPoint point_g(Point(50, 150) + offset, 20, Color::White());
Point g = DrawPlaygroundPoint(point_g);
Path path = PathBuilder{}
.MoveTo(a)
.LineTo(b)
Expand Down Expand Up @@ -392,42 +393,54 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
// Cap::kButt demo.
{
Point off = Point(0, 0) * padding + margin;
auto [a, b] = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
Color::Black(), Color::White());
auto [c, d] = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
Color::Black(), Color::White());
static PlaygroundPoint point_a(off + a_def, r, Color::Black());
static PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
static PlaygroundPoint point_c(off + c_def, r, Color::Black());
static PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kButt, Join::kBevel);
}

// Cap::kSquare demo.
{
Point off = Point(1, 0) * padding + margin;
auto [a, b] = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
Color::Black(), Color::White());
auto [c, d] = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
Color::Black(), Color::White());
static PlaygroundPoint point_a(off + a_def, r, Color::Black());
static PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
static PlaygroundPoint point_c(off + c_def, r, Color::Black());
static PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kSquare, Join::kBevel);
}

// Cap::kRound demo.
{
Point off = Point(2, 0) * padding + margin;
auto [a, b] = IMPELLER_PLAYGROUND_LINE(off + a_def, off + b_def, r,
Color::Black(), Color::White());
auto [c, d] = IMPELLER_PLAYGROUND_LINE(off + c_def, off + d_def, r,
Color::Black(), Color::White());
static PlaygroundPoint point_a(off + a_def, r, Color::Black());
static PlaygroundPoint point_b(off + b_def, r, Color::White());
auto [a, b] = DrawPlaygroundLine(point_a, point_b);
static PlaygroundPoint point_c(off + c_def, r, Color::Black());
static PlaygroundPoint point_d(off + d_def, r, Color::White());
auto [c, d] = DrawPlaygroundLine(point_c, point_d);
render_path(PathBuilder{}.AddCubicCurve(a, b, d, c).TakePath(),
Cap::kRound, Join::kBevel);
}

// Join::kBevel demo.
{
Point off = Point(0, 1) * padding + margin;
Point a = IMPELLER_PLAYGROUND_POINT(off + a_def, r, Color::White());
Point b = IMPELLER_PLAYGROUND_POINT(off + e_def, r, Color::White());
Point c = IMPELLER_PLAYGROUND_POINT(off + c_def, r, Color::White());
static PlaygroundPoint point_a =
PlaygroundPoint(off + a_def, r, Color::White());
static PlaygroundPoint point_b =
PlaygroundPoint(off + e_def, r, Color::White());
static PlaygroundPoint point_c =
PlaygroundPoint(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kBevel);
Expand All @@ -436,9 +449,12 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
// Join::kMiter demo.
{
Point off = Point(1, 1) * padding + margin;
Point a = IMPELLER_PLAYGROUND_POINT(off + a_def, r, Color::White());
Point b = IMPELLER_PLAYGROUND_POINT(off + e_def, r, Color::White());
Point c = IMPELLER_PLAYGROUND_POINT(off + c_def, r, Color::White());
static PlaygroundPoint point_a(off + a_def, r, Color::White());
static PlaygroundPoint point_b(off + e_def, r, Color::White());
static PlaygroundPoint point_c(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kMiter);
Expand All @@ -447,9 +463,12 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
// Join::kRound demo.
{
Point off = Point(2, 1) * padding + margin;
Point a = IMPELLER_PLAYGROUND_POINT(off + a_def, r, Color::White());
Point b = IMPELLER_PLAYGROUND_POINT(off + e_def, r, Color::White());
Point c = IMPELLER_PLAYGROUND_POINT(off + c_def, r, Color::White());
static PlaygroundPoint point_a(off + a_def, r, Color::White());
static PlaygroundPoint point_b(off + e_def, r, Color::White());
static PlaygroundPoint point_c(off + c_def, r, Color::White());
Point a = DrawPlaygroundPoint(point_a);
Point b = DrawPlaygroundPoint(point_b);
Point c = DrawPlaygroundPoint(point_c);
render_path(
PathBuilder{}.MoveTo(a).LineTo(b).LineTo(c).Close().TakePath(),
Cap::kButt, Join::kRound);
Expand Down Expand Up @@ -905,10 +924,12 @@ TEST_P(EntityTest, BlendingModeOptions) {
BlendMode selected_mode = blend_mode_values[current_blend_index];

Point a, b, c, d;
std::tie(a, b) = IMPELLER_PLAYGROUND_LINE(
Point(400, 100), Point(200, 300), 20, Color::White(), Color::White());
std::tie(c, d) = IMPELLER_PLAYGROUND_LINE(
Point(470, 190), Point(270, 390), 20, Color::White(), Color::White());
static PlaygroundPoint point_a(Point(400, 100), 20, Color::White());
static PlaygroundPoint point_b(Point(200, 300), 20, Color::White());
std::tie(a, b) = DrawPlaygroundLine(point_a, point_b);
static PlaygroundPoint point_c(Point(470, 190), 20, Color::White());
static PlaygroundPoint point_d(Point(270, 390), 20, Color::White());
std::tie(c, d) = DrawPlaygroundLine(point_c, point_d);

bool result = true;
result = result &&
Expand Down Expand Up @@ -1739,8 +1760,11 @@ TEST_P(EntityTest, RRectShadowTest) {
}
ImGui::End();

auto [top_left, bottom_right] = IMPELLER_PLAYGROUND_LINE(
Point(200, 200), Point(600, 400), 30, Color::White(), Color::White());
static PlaygroundPoint top_left_point(Point(200, 200), 30, Color::White());
static PlaygroundPoint bottom_right_point(Point(600, 400), 30,
Color::White());
auto [top_left, bottom_right] =
DrawPlaygroundLine(top_left_point, bottom_right_point);
auto rect =
Rect::MakeLTRB(top_left.x, top_left.y, bottom_right.x, bottom_right.y);

Expand Down
54 changes: 53 additions & 1 deletion impeller/playground/widgets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,62 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "widgets.h"
#include "impeller/playground/widgets.h"

namespace impeller {

Point DrawPlaygroundPoint(PlaygroundPoint& point) {
impeller::Point mouse_pos(ImGui::GetMousePos().x, ImGui::GetMousePos().y);
if (!point.prev_mouse_pos.has_value()) {
point.prev_mouse_pos = mouse_pos;
}

if (ImGui::IsKeyPressed(ImGuiKey_R)) {
point.position = point.reset_position;
point.dragging = false;
}

bool hovering =
point.position.GetDistance(mouse_pos) < point.radius &&
point.position.GetDistance(point.prev_mouse_pos.value()) < point.radius;
if (!ImGui::IsMouseDown(0)) {
point.dragging = false;
} else if (hovering && ImGui::IsMouseClicked(0)) {
point.dragging = true;
}
if (point.dragging) {
point.position += mouse_pos - point.prev_mouse_pos.value();
}
ImGui::GetBackgroundDrawList()->AddCircleFilled(
{point.position.x, point.position.y}, point.radius,
ImColor(point.color.red, point.color.green, point.color.blue,
(hovering || point.dragging) ? 0.6f : 0.3f));
if (hovering || point.dragging) {
ImGui::GetBackgroundDrawList()->AddText(
{point.position.x - point.radius, point.position.y + point.radius + 10},
ImColor(point.color.red, point.color.green, point.color.blue, 1.0f),
impeller::SPrintF("x:%0.3f y:%0.3f", point.position.x, point.position.y)
.c_str());
}
point.prev_mouse_pos = mouse_pos;
return point.position;
}

std::tuple<Point, Point> DrawPlaygroundLine(PlaygroundPoint& point_a,
PlaygroundPoint& point_b) {
Point position_a = DrawPlaygroundPoint(point_a);
Point position_b = DrawPlaygroundPoint(point_b);

auto dir = (position_b - position_a).Normalize() * point_a.radius;
auto line_a = position_a + dir;
auto line_b = position_b - dir;
ImGui::GetBackgroundDrawList()->AddLine(
{line_a.x, line_a.y}, {line_b.x, line_b.y},
ImColor(point_b.color.red, point_b.color.green, point_b.color.blue,
0.3f));

return std::make_tuple(position_a, position_b);
}
//

} // namespace impeller
Loading