Skip to content

Commit

Permalink
[Impeller] Direct tessellation of simple filled round rects (flutter#…
Browse files Browse the repository at this point in the history
…48919)

Simple round rects with the same width and height at each corner are tessellated directly for fill operations, but not yet for stroke operations.
  • Loading branch information
flar committed Dec 12, 2023
1 parent 53a4d7c commit dabf364
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 29 deletions.
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -5115,6 +5115,8 @@ ORIGIN: ../../../flutter/impeller/entity/geometry/point_field_geometry.cc + ../.
ORIGIN: ../../../flutter/impeller/entity/geometry/point_field_geometry.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/rect_geometry.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/rect_geometry.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/round_rect_geometry.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/round_rect_geometry.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -7916,6 +7918,8 @@ FILE: ../../../flutter/impeller/entity/geometry/point_field_geometry.cc
FILE: ../../../flutter/impeller/entity/geometry/point_field_geometry.h
FILE: ../../../flutter/impeller/entity/geometry/rect_geometry.cc
FILE: ../../../flutter/impeller/entity/geometry/rect_geometry.h
FILE: ../../../flutter/impeller/entity/geometry/round_rect_geometry.cc
FILE: ../../../flutter/impeller/entity/geometry/round_rect_geometry.h
FILE: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.cc
FILE: ../../../flutter/impeller/entity/geometry/stroke_path_geometry.h
FILE: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc
Expand Down
74 changes: 71 additions & 3 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2264,6 +2264,74 @@ TEST_P(AiksTest, FilledEllipsesRenderCorrectly) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_P(AiksTest, FilledRoundRectsRenderCorrectly) {
Canvas canvas;
canvas.Scale(GetContentScale());
Paint paint;
const int color_count = 3;
Color colors[color_count] = {
Color::Blue(),
Color::Green(),
Color::Crimson(),
};

paint.color = Color::White();
canvas.DrawPaint(paint);

int c_index = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
paint.color = colors[(c_index++) % color_count];
canvas.DrawRRect(Rect::MakeXYWH(i * 100 + 10, j * 100 + 20, 80, 80),
Size(i * 5 + 10, j * 5 + 10), paint);
}
}

std::vector<Color> gradient_colors = {
Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
std::vector<Scalar> stops = {
0.0,
(1.0 / 6.0) * 1,
(1.0 / 6.0) * 2,
(1.0 / 6.0) * 3,
(1.0 / 6.0) * 4,
(1.0 / 6.0) * 5,
1.0,
};
auto texture = CreateTextureForFixture("airplane.jpg",
/*enable_mipmapping=*/true);

paint.color = Color::White().WithAlpha(0.1);

paint.color_source = ColorSource::MakeRadialGradient(
{500, 550}, 75, std::move(gradient_colors), std::move(stops),
Entity::TileMode::kMirror, {});
for (int i = 1; i <= 10; i++) {
int j = 11 - i;
canvas.DrawRRect(Rect::MakeLTRB(500 - i * 20, 550 - j * 20, //
500 + i * 20, 550 + j * 20),
Size(i * 10, j * 10), paint);
}

paint.color_source = ColorSource::MakeImage(
texture, Entity::TileMode::kRepeat, Entity::TileMode::kRepeat, {},
Matrix::MakeTranslation({500, 20}));
for (int i = 1; i <= 10; i++) {
int j = 11 - i;
canvas.DrawRRect(Rect::MakeLTRB(700 - i * 20, 220 - j * 20, //
700 + i * 20, 220 + j * 20),
Size(i * 10, j * 10), paint);
}

ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_P(AiksTest, GradientStrokesRenderCorrectly) {
// Compare with https://fiddle.skia.org/c/027392122bec8ac2b5d5de00a4b9bbe2
auto callback = [&](AiksContext& renderer) -> std::optional<Picture> {
Expand Down Expand Up @@ -4335,9 +4403,9 @@ TEST_P(AiksTest, GaussianBlurAtPeripheryVertical) {

canvas.Scale(GetContentScale());
canvas.DrawRRect(Rect::MakeLTRB(0, 0, GetWindowSize().width, 100),
Point(10, 10), Paint{.color = Color::LimeGreen()});
Size(10, 10), Paint{.color = Color::LimeGreen()});
canvas.DrawRRect(Rect::MakeLTRB(0, 110, GetWindowSize().width, 210),
Point(10, 10), Paint{.color = Color::Magenta()});
Size(10, 10), Paint{.color = Color::Magenta()});
canvas.ClipRect(Rect::MakeLTRB(100, 0, 200, GetWindowSize().height));
canvas.SaveLayer({.blend_mode = BlendMode::kSource}, std::nullopt,
ImageFilter::MakeBlur(Sigma(20.0), Sigma(20.0),
Expand All @@ -4358,7 +4426,7 @@ TEST_P(AiksTest, GaussianBlurAtPeripheryHorizontal) {
Rect::MakeXYWH(0, 0, boston->GetSize().width, boston->GetSize().height),
Rect::MakeLTRB(0, 0, GetWindowSize().width, 100), Paint{});
canvas.DrawRRect(Rect::MakeLTRB(0, 110, GetWindowSize().width, 210),
Point(10, 10), Paint{.color = Color::Magenta()});
Size(10, 10), Paint{.color = Color::Magenta()});
canvas.ClipRect(Rect::MakeLTRB(0, 50, GetWindowSize().width, 150));
canvas.SaveLayer({.blend_mode = BlendMode::kSource}, std::nullopt,
ImageFilter::MakeBlur(Sigma(20.0), Sigma(20.0),
Expand Down
34 changes: 19 additions & 15 deletions impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,27 +365,31 @@ void Canvas::DrawOval(const Rect& rect, const Paint& paint) {
GetCurrentPass().AddEntity(std::move(entity));
}

void Canvas::DrawRRect(Rect rect, Point corner_radii, const Paint& paint) {
if (corner_radii.x == corner_radii.y &&
AttemptDrawBlurredRRect(rect, corner_radii.x, paint)) {
void Canvas::DrawRRect(const Rect& rect,
const Size& corner_radii,
const Paint& paint) {
if (corner_radii.IsSquare() &&
AttemptDrawBlurredRRect(rect, corner_radii.width, paint)) {
return;
}
auto path = PathBuilder{}
.SetConvexity(Convexity::kConvex)
.AddRoundedRect(rect, corner_radii)
.SetBounds(rect)
.TakePath();

if (paint.style == Paint::Style::kFill) {
Entity entity;
entity.SetTransform(GetCurrentTransform());
entity.SetClipDepth(GetClipDepth());
entity.SetBlendMode(paint.blend_mode);
entity.SetContents(CreateContentsForGeometryWithFilters(
paint, Geometry::MakeFillPath(std::move(path))));
paint, Geometry::MakeRoundRect(rect, corner_radii)));

GetCurrentPass().AddEntity(std::move(entity));
return;
}

auto path = PathBuilder{}
.SetConvexity(Convexity::kConvex)
.AddRoundedRect(rect, corner_radii)
.SetBounds(rect)
.TakePath();
DrawPath(std::move(path), paint);
}

Expand Down Expand Up @@ -445,7 +449,7 @@ void Canvas::ClipRect(const Rect& rect, Entity::ClipOperation clip_op) {
}

void Canvas::ClipRRect(const Rect& rect,
Point corner_radii,
const Size& corner_radii,
Entity::ClipOperation clip_op) {
auto path = PathBuilder{}
.SetConvexity(Convexity::kConvex)
Expand All @@ -455,8 +459,8 @@ void Canvas::ClipRRect(const Rect& rect,

auto size = rect.GetSize();
// Does the rounded rect have a flat part on the top/bottom or left/right?
bool flat_on_TB = corner_radii.x * 2 < size.width;
bool flat_on_LR = corner_radii.y * 2 < size.height;
bool flat_on_TB = corner_radii.width * 2 < size.width;
bool flat_on_LR = corner_radii.height * 2 < size.height;
std::optional<Rect> inner_rect = (flat_on_LR && flat_on_TB)
? rect.Expand(-corner_radii)
: std::make_optional<Rect>();
Expand All @@ -475,7 +479,7 @@ void Canvas::ClipRRect(const Rect& rect,
IntersectCulling(rect);
break;
case Entity::ClipOperation::kDifference:
if (corner_radii.x <= 0.0 || corner_radii.y <= 0) {
if (corner_radii.IsEmpty()) {
SubtractCulling(rect);
} else {
// We subtract the inner "tall" and "wide" rectangle pieces
Expand All @@ -484,10 +488,10 @@ void Canvas::ClipRRect(const Rect& rect,
// Since this is a subtract operation, we can subtract each
// rectangle piece individually without fear of interference.
if (flat_on_TB) {
SubtractCulling(rect.Expand({-corner_radii.x, 0.0}));
SubtractCulling(rect.Expand(Size{-corner_radii.width, 0.0}));
}
if (flat_on_LR) {
SubtractCulling(rect.Expand({0.0, -corner_radii.y}));
SubtractCulling(rect.Expand(Size{0.0, -corner_radii.height}));
}
}
break;
Expand Down
6 changes: 4 additions & 2 deletions impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class Canvas {

void DrawOval(const Rect& rect, const Paint& paint);

void DrawRRect(Rect rect, Point corner_radii, const Paint& paint);
void DrawRRect(const Rect& rect,
const Size& corner_radii,
const Paint& paint);

void DrawCircle(const Point& center, Scalar radius, const Paint& paint);

Expand Down Expand Up @@ -136,7 +138,7 @@ class Canvas {

void ClipRRect(
const Rect& rect,
Point corner_radii,
const Size& corner_radii,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);

void DrawPicture(const Picture& picture);
Expand Down
6 changes: 4 additions & 2 deletions impeller/aiks/canvas_recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ class CanvasRecorder {
paint);
}

void DrawRRect(Rect rect, Point corner_radii, const Paint& paint) {
void DrawRRect(const Rect& rect,
const Size& corner_radii,
const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawRRect), rect,
corner_radii, paint);
}
Expand Down Expand Up @@ -248,7 +250,7 @@ class CanvasRecorder {

void ClipRRect(
const Rect& rect,
Point corner_radii,
const Size& corner_radii,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipRRect), rect,
corner_radii, clip_op);
Expand Down
7 changes: 3 additions & 4 deletions impeller/display_list/dl_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ void DlDispatcher::clipRRect(const SkRRect& rrect, ClipOp clip_op, bool is_aa) {
ToClipOperation(clip_op));
} else if (rrect.isSimple()) {
canvas_.ClipRRect(skia_conversions::ToRect(rrect.rect()),
skia_conversions::ToPoint(rrect.getSimpleRadii()),
skia_conversions::ToSize(rrect.getSimpleRadii()),
ToClipOperation(clip_op));
} else {
canvas_.ClipPath(skia_conversions::ToPath(rrect), ToClipOperation(clip_op));
Expand Down Expand Up @@ -782,8 +782,7 @@ void DlDispatcher::drawCircle(const SkPoint& center, SkScalar radius) {
void DlDispatcher::drawRRect(const SkRRect& rrect) {
if (rrect.isSimple()) {
canvas_.DrawRRect(skia_conversions::ToRect(rrect.rect()),
skia_conversions::ToPoint(rrect.getSimpleRadii()),
paint_);
skia_conversions::ToSize(rrect.getSimpleRadii()), paint_);
} else {
canvas_.DrawPath(skia_conversions::ToPath(rrect), paint_);
}
Expand Down Expand Up @@ -817,7 +816,7 @@ void DlDispatcher::SimplifyOrDrawPath(CanvasType& canvas,
SkRRect rrect;
if (path.isRRect(&rrect) && rrect.isSimple()) {
canvas.DrawRRect(skia_conversions::ToRect(rrect.rect()),
skia_conversions::ToPoint(rrect.getSimpleRadii()), paint);
skia_conversions::ToSize(rrect.getSimpleRadii()), paint);
return;
}

Expand Down
4 changes: 4 additions & 0 deletions impeller/display_list/skia_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Point ToPoint(const SkPoint& point) {
return Point::MakeXY(point.fX, point.fY);
}

Size ToSize(const SkPoint& point) {
return Size(point.fX, point.fY);
}

Color ToColor(const flutter::DlColor& color) {
return {
static_cast<Scalar>(color.getRedF()), //
Expand Down
2 changes: 2 additions & 0 deletions impeller/display_list/skia_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ std::vector<Point> ToPoints(const SkPoint points[], int count);

Point ToPoint(const SkPoint& point);

Size ToSize(const SkPoint& point);

Color ToColor(const flutter::DlColor& color);

std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count);
Expand Down
18 changes: 18 additions & 0 deletions impeller/display_list/skia_conversions_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
namespace impeller {
namespace testing {

TEST(SkiaConversionsTest, SkPointToPoint) {
for (int x = -100; x < 100; x += 4) {
for (int y = -100; y < 100; y += 4) {
EXPECT_EQ(skia_conversions::ToPoint(SkPoint::Make(x * 0.25f, y * 0.25f)),
Point(x * 0.25f, y * 0.25f));
}
}
}

TEST(SkiaConversionsTest, SkPointToSize) {
for (int x = -100; x < 100; x += 4) {
for (int y = -100; y < 100; y += 4) {
EXPECT_EQ(skia_conversions::ToSize(SkPoint::Make(x * 0.25f, y * 0.25f)),
Size(x * 0.25f, y * 0.25f));
}
}
}

TEST(SkiaConversionsTest, ToColor) {
// Create a color with alpha, red, green, and blue values that are all
// trivially divisible by 255 so that we can test the conversion results in
Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ impeller_component("entity") {
"geometry/point_field_geometry.h",
"geometry/rect_geometry.cc",
"geometry/rect_geometry.h",
"geometry/round_rect_geometry.cc",
"geometry/round_rect_geometry.h",
"geometry/stroke_path_geometry.cc",
"geometry/stroke_path_geometry.h",
"geometry/vertices_geometry.cc",
Expand Down
6 changes: 6 additions & 0 deletions impeller/entity/geometry/geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "impeller/entity/geometry/line_geometry.h"
#include "impeller/entity/geometry/point_field_geometry.h"
#include "impeller/entity/geometry/rect_geometry.h"
#include "impeller/entity/geometry/round_rect_geometry.h"
#include "impeller/entity/geometry/stroke_path_geometry.h"
#include "impeller/geometry/rect.h"

Expand Down Expand Up @@ -204,6 +205,11 @@ std::shared_ptr<Geometry> Geometry::MakeStrokedCircle(const Point& center,
return std::make_shared<CircleGeometry>(center, radius, stroke_width);
}

std::shared_ptr<Geometry> Geometry::MakeRoundRect(const Rect& rect,
const Size& radii) {
return std::make_shared<RoundRectGeometry>(rect, radii);
}

bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions impeller/entity/geometry/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Geometry {
Scalar radius,
Scalar stroke_width);

static std::shared_ptr<Geometry> MakeRoundRect(const Rect& rect,
const Size& radii);

static std::shared_ptr<Geometry> MakePointField(std::vector<Point> points,
Scalar radius,
bool round);
Expand Down
9 changes: 9 additions & 0 deletions impeller/entity/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,14 @@ TEST(EntityGeometryTest, LineGeometryCoverage) {
}
}

TEST(EntityGeometryTest, RoundRectGeometryCoversArea) {
auto geometry =
Geometry::MakeRoundRect(Rect::MakeLTRB(0, 0, 100, 100), Size(20, 20));
EXPECT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(15, 15, 85, 85)));
EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(20, 20, 80, 80)));
EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(30, 1, 70, 99)));
EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 30, 99, 70)));
}

} // namespace testing
} // namespace impeller
Loading

0 comments on commit dabf364

Please sign in to comment.