Skip to content

Commit e677fa4

Browse files
authored
[Impeller] Directly tessellate filled ellipses (flutter-team-archive/engine#48770)
Add support for filling ellipses/ovals and greatly simplify the primitive tessellation mechanisms.
1 parent 3bef2b4 commit e677fa4

28 files changed

Lines changed: 1410 additions & 901 deletions

‎engine/src/flutter/ci/licenses_golden/excluded_files‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@
194194
../../../flutter/impeller/scene/importer/importer_unittests.cc
195195
../../../flutter/impeller/scene/scene_unittests.cc
196196
../../../flutter/impeller/shader_archive/shader_archive_unittests.cc
197-
../../../flutter/impeller/tessellator/circle_tessellator_unittests.cc
198197
../../../flutter/impeller/tessellator/dart/.dart_tool
199198
../../../flutter/impeller/tessellator/dart/pubspec.lock
200199
../../../flutter/impeller/tessellator/dart/pubspec.yaml

‎engine/src/flutter/ci/licenses_golden/licenses_flutter‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,6 +5099,8 @@ ORIGIN: ../../../flutter/impeller/entity/entity_pass_target.cc + ../../../flutte
50995099
ORIGIN: ../../../flutter/impeller/entity/entity_pass_target.h + ../../../flutter/LICENSE
51005100
ORIGIN: ../../../flutter/impeller/entity/entity_playground.cc + ../../../flutter/LICENSE
51015101
ORIGIN: ../../../flutter/impeller/entity/entity_playground.h + ../../../flutter/LICENSE
5102+
ORIGIN: ../../../flutter/impeller/entity/geometry/circle_geometry.cc + ../../../flutter/LICENSE
5103+
ORIGIN: ../../../flutter/impeller/entity/geometry/circle_geometry.h + ../../../flutter/LICENSE
51025104
ORIGIN: ../../../flutter/impeller/entity/geometry/cover_geometry.cc + ../../../flutter/LICENSE
51035105
ORIGIN: ../../../flutter/impeller/entity/geometry/cover_geometry.h + ../../../flutter/LICENSE
51045106
ORIGIN: ../../../flutter/impeller/entity/geometry/ellipse_geometry.cc + ../../../flutter/LICENSE
@@ -5527,8 +5529,6 @@ ORIGIN: ../../../flutter/impeller/shader_archive/shader_archive_writer.cc + ../.
55275529
ORIGIN: ../../../flutter/impeller/shader_archive/shader_archive_writer.h + ../../../flutter/LICENSE
55285530
ORIGIN: ../../../flutter/impeller/tessellator/c/tessellator.cc + ../../../flutter/LICENSE
55295531
ORIGIN: ../../../flutter/impeller/tessellator/c/tessellator.h + ../../../flutter/LICENSE
5530-
ORIGIN: ../../../flutter/impeller/tessellator/circle_tessellator.cc + ../../../flutter/LICENSE
5531-
ORIGIN: ../../../flutter/impeller/tessellator/circle_tessellator.h + ../../../flutter/LICENSE
55325532
ORIGIN: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart + ../../../flutter/LICENSE
55335533
ORIGIN: ../../../flutter/impeller/tessellator/tessellator.cc + ../../../flutter/LICENSE
55345534
ORIGIN: ../../../flutter/impeller/tessellator/tessellator.h + ../../../flutter/LICENSE
@@ -7898,6 +7898,8 @@ FILE: ../../../flutter/impeller/entity/entity_pass_target.cc
78987898
FILE: ../../../flutter/impeller/entity/entity_pass_target.h
78997899
FILE: ../../../flutter/impeller/entity/entity_playground.cc
79007900
FILE: ../../../flutter/impeller/entity/entity_playground.h
7901+
FILE: ../../../flutter/impeller/entity/geometry/circle_geometry.cc
7902+
FILE: ../../../flutter/impeller/entity/geometry/circle_geometry.h
79017903
FILE: ../../../flutter/impeller/entity/geometry/cover_geometry.cc
79027904
FILE: ../../../flutter/impeller/entity/geometry/cover_geometry.h
79037905
FILE: ../../../flutter/impeller/entity/geometry/ellipse_geometry.cc
@@ -8327,8 +8329,6 @@ FILE: ../../../flutter/impeller/shader_archive/shader_archive_writer.cc
83278329
FILE: ../../../flutter/impeller/shader_archive/shader_archive_writer.h
83288330
FILE: ../../../flutter/impeller/tessellator/c/tessellator.cc
83298331
FILE: ../../../flutter/impeller/tessellator/c/tessellator.h
8330-
FILE: ../../../flutter/impeller/tessellator/circle_tessellator.cc
8331-
FILE: ../../../flutter/impeller/tessellator/circle_tessellator.h
83328332
FILE: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart
83338333
FILE: ../../../flutter/impeller/tessellator/tessellator.cc
83348334
FILE: ../../../flutter/impeller/tessellator/tessellator.h

‎engine/src/flutter/impeller/aiks/aiks_unittests.cc‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,77 @@ TEST_P(AiksTest, StrokedCirclesRenderCorrectly) {
21932193
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
21942194
}
21952195

2196+
TEST_P(AiksTest, FilledEllipsesRenderCorrectly) {
2197+
Canvas canvas;
2198+
canvas.Scale(GetContentScale());
2199+
Paint paint;
2200+
const int color_count = 3;
2201+
Color colors[color_count] = {
2202+
Color::Blue(),
2203+
Color::Green(),
2204+
Color::Crimson(),
2205+
};
2206+
2207+
paint.color = Color::White();
2208+
canvas.DrawPaint(paint);
2209+
2210+
int c_index = 0;
2211+
int long_radius = 600;
2212+
int short_radius = 600;
2213+
while (long_radius > 0 && short_radius > 0) {
2214+
paint.color = colors[(c_index++) % color_count];
2215+
canvas.DrawOval(Rect::MakeXYWH(10 - long_radius, 10 - short_radius,
2216+
long_radius * 2, short_radius * 2),
2217+
paint);
2218+
canvas.DrawOval(Rect::MakeXYWH(1000 - short_radius, 750 - long_radius,
2219+
short_radius * 2, long_radius * 2),
2220+
paint);
2221+
if (short_radius > 30) {
2222+
short_radius -= 10;
2223+
long_radius -= 5;
2224+
} else {
2225+
short_radius -= 2;
2226+
long_radius -= 1;
2227+
}
2228+
}
2229+
2230+
std::vector<Color> gradient_colors = {
2231+
Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
2232+
Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
2233+
Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
2234+
Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
2235+
Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
2236+
Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
2237+
Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
2238+
std::vector<Scalar> stops = {
2239+
0.0,
2240+
(1.0 / 6.0) * 1,
2241+
(1.0 / 6.0) * 2,
2242+
(1.0 / 6.0) * 3,
2243+
(1.0 / 6.0) * 4,
2244+
(1.0 / 6.0) * 5,
2245+
1.0,
2246+
};
2247+
auto texture = CreateTextureForFixture("airplane.jpg",
2248+
/*enable_mipmapping=*/true);
2249+
2250+
paint.color = Color::White().WithAlpha(0.5);
2251+
2252+
paint.color_source = ColorSource::MakeRadialGradient(
2253+
{300, 650}, 75, std::move(gradient_colors), std::move(stops),
2254+
Entity::TileMode::kMirror, {});
2255+
canvas.DrawOval(Rect::MakeXYWH(200, 625, 200, 50), paint);
2256+
canvas.DrawOval(Rect::MakeXYWH(275, 550, 50, 200), paint);
2257+
2258+
paint.color_source = ColorSource::MakeImage(
2259+
texture, Entity::TileMode::kRepeat, Entity::TileMode::kRepeat, {},
2260+
Matrix::MakeTranslation({610, 15}));
2261+
canvas.DrawOval(Rect::MakeXYWH(610, 90, 200, 50), paint);
2262+
canvas.DrawOval(Rect::MakeXYWH(685, 15, 50, 200), paint);
2263+
2264+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
2265+
}
2266+
21962267
TEST_P(AiksTest, GradientStrokesRenderCorrectly) {
21972268
// Compare with https://fiddle.skia.org/c/027392122bec8ac2b5d5de00a4b9bbe2
21982269
auto callback = [&](AiksContext& renderer) -> std::optional<Picture> {

‎engine/src/flutter/impeller/aiks/canvas.cc‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void Canvas::DrawLine(const Point& p0, const Point& p1, const Paint& paint) {
318318
GetCurrentPass().AddEntity(std::move(entity));
319319
}
320320

321-
void Canvas::DrawRect(Rect rect, const Paint& paint) {
321+
void Canvas::DrawRect(const Rect& rect, const Paint& paint) {
322322
if (paint.style == Paint::Style::kStroke) {
323323
DrawPath(PathBuilder{}.AddRect(rect).TakePath(), paint);
324324
return;
@@ -338,6 +338,33 @@ void Canvas::DrawRect(Rect rect, const Paint& paint) {
338338
GetCurrentPass().AddEntity(std::move(entity));
339339
}
340340

341+
void Canvas::DrawOval(const Rect& rect, const Paint& paint) {
342+
if (rect.IsSquare()) {
343+
// Circles have slightly less overhead and can do stroking
344+
DrawCircle(rect.GetCenter(), rect.GetSize().width * 0.5f, paint);
345+
return;
346+
}
347+
348+
if (paint.style == Paint::Style::kStroke) {
349+
// No stroked ellipses yet
350+
DrawPath(PathBuilder{}.AddOval(rect).TakePath(), paint);
351+
return;
352+
}
353+
354+
if (AttemptDrawBlurredRRect(rect, 0, paint)) {
355+
return;
356+
}
357+
358+
Entity entity;
359+
entity.SetTransform(GetCurrentTransform());
360+
entity.SetClipDepth(GetClipDepth());
361+
entity.SetBlendMode(paint.blend_mode);
362+
entity.SetContents(
363+
CreateContentsForGeometryWithFilters(paint, Geometry::MakeOval(rect)));
364+
365+
GetCurrentPass().AddEntity(std::move(entity));
366+
}
367+
341368
void Canvas::DrawRRect(Rect rect, Point corner_radii, const Paint& paint) {
342369
if (corner_radii.x == corner_radii.y &&
343370
AttemptDrawBlurredRRect(rect, corner_radii.x, paint)) {
@@ -362,7 +389,9 @@ void Canvas::DrawRRect(Rect rect, Point corner_radii, const Paint& paint) {
362389
DrawPath(std::move(path), paint);
363390
}
364391

365-
void Canvas::DrawCircle(Point center, Scalar radius, const Paint& paint) {
392+
void Canvas::DrawCircle(const Point& center,
393+
Scalar radius,
394+
const Paint& paint) {
366395
Size half_size(radius, radius);
367396
if (AttemptDrawBlurredRRect(
368397
Rect::MakeOriginSize(center - half_size, half_size * 2), radius,

‎engine/src/flutter/impeller/aiks/canvas.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ class Canvas {
102102

103103
void DrawLine(const Point& p0, const Point& p1, const Paint& paint);
104104

105-
void DrawRect(Rect rect, const Paint& paint);
105+
void DrawRect(const Rect& rect, const Paint& paint);
106+
107+
void DrawOval(const Rect& rect, const Paint& paint);
106108

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

109-
void DrawCircle(Point center, Scalar radius, const Paint& paint);
111+
void DrawCircle(const Point& center, Scalar radius, const Paint& paint);
110112

111-
void DrawPoints(std::vector<Point>,
113+
void DrawPoints(std::vector<Point> points,
112114
Scalar radius,
113115
const Paint& paint,
114116
PointStyle point_style);

‎engine/src/flutter/impeller/display_list/dl_dispatcher.cc‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -770,16 +770,7 @@ void DlDispatcher::drawRect(const SkRect& rect) {
770770

771771
// |flutter::DlOpReceiver|
772772
void DlDispatcher::drawOval(const SkRect& bounds) {
773-
if (bounds.width() == bounds.height()) {
774-
canvas_.DrawCircle(skia_conversions::ToPoint(bounds.center()),
775-
bounds.width() * 0.5, paint_);
776-
} else {
777-
auto path = PathBuilder{}
778-
.AddOval(skia_conversions::ToRect(bounds))
779-
.SetConvexity(Convexity::kConvex)
780-
.TakePath();
781-
canvas_.DrawPath(std::move(path), paint_);
782-
}
773+
canvas_.DrawOval(skia_conversions::ToRect(bounds), paint_);
783774
}
784775

785776
// |flutter::DlOpReceiver|
@@ -831,9 +822,8 @@ void DlDispatcher::SimplifyOrDrawPath(CanvasType& canvas,
831822
}
832823

833824
SkRect oval;
834-
if (path.isOval(&oval) && oval.width() == oval.height()) {
835-
canvas.DrawCircle(skia_conversions::ToPoint(oval.center()),
836-
oval.width() * 0.5, paint);
825+
if (path.isOval(&oval)) {
826+
canvas.DrawOval(skia_conversions::ToRect(oval), paint);
837827
return;
838828
}
839829

‎engine/src/flutter/impeller/entity/BUILD.gn‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ impeller_component("entity") {
191191
"entity_pass_delegate.h",
192192
"entity_pass_target.cc",
193193
"entity_pass_target.h",
194+
"geometry/circle_geometry.cc",
195+
"geometry/circle_geometry.h",
194196
"geometry/cover_geometry.cc",
195197
"geometry/cover_geometry.h",
196198
"geometry/ellipse_geometry.cc",
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <algorithm>
6+
7+
#include "flutter/impeller/entity/geometry/circle_geometry.h"
8+
9+
#include "flutter/impeller/entity/geometry/line_geometry.h"
10+
11+
namespace impeller {
12+
13+
CircleGeometry::CircleGeometry(const Point& center, Scalar radius)
14+
: center_(center), radius_(radius), stroke_width_(-1.0f) {
15+
FML_DCHECK(radius >= 0);
16+
}
17+
18+
CircleGeometry::CircleGeometry(const Point& center,
19+
Scalar radius,
20+
Scalar stroke_width)
21+
: center_(center),
22+
radius_(radius),
23+
stroke_width_(std::max(stroke_width, 0.0f)) {
24+
FML_DCHECK(radius >= 0);
25+
FML_DCHECK(stroke_width >= 0);
26+
}
27+
28+
GeometryResult CircleGeometry::GetPositionBuffer(const ContentContext& renderer,
29+
const Entity& entity,
30+
RenderPass& pass) const {
31+
auto& transform = entity.GetTransform();
32+
33+
Scalar half_width = stroke_width_ < 0 ? 0.0
34+
: LineGeometry::ComputePixelHalfWidth(
35+
transform, stroke_width_);
36+
37+
std::shared_ptr<Tessellator> tessellator = renderer.GetTessellator();
38+
39+
// We call the StrokedCircle method which will simplify to a
40+
// FilledCircleGenerator if the inner_radius is <= 0.
41+
auto generator =
42+
tessellator->StrokedCircle(transform, center_, radius_, half_width);
43+
44+
return ComputePositionGeometry(generator, entity, pass);
45+
}
46+
47+
// |Geometry|
48+
GeometryResult CircleGeometry::GetPositionUVBuffer(
49+
Rect texture_coverage,
50+
Matrix effect_transform,
51+
const ContentContext& renderer,
52+
const Entity& entity,
53+
RenderPass& pass) const {
54+
auto& transform = entity.GetTransform();
55+
auto uv_transform =
56+
texture_coverage.GetNormalizingTransform() * effect_transform;
57+
58+
Scalar half_width = stroke_width_ < 0 ? 0.0
59+
: LineGeometry::ComputePixelHalfWidth(
60+
transform, stroke_width_);
61+
std::shared_ptr<Tessellator> tessellator = renderer.GetTessellator();
62+
63+
// We call the StrokedCircle method which will simplify to a
64+
// FilledCircleGenerator if the inner_radius is <= 0.
65+
auto generator =
66+
tessellator->StrokedCircle(transform, center_, radius_, half_width);
67+
68+
return ComputePositionUVGeometry(generator, uv_transform, entity, pass);
69+
}
70+
71+
GeometryVertexType CircleGeometry::GetVertexType() const {
72+
return GeometryVertexType::kPosition;
73+
}
74+
75+
std::optional<Rect> CircleGeometry::GetCoverage(const Matrix& transform) const {
76+
Point corners[4]{
77+
{center_.x, center_.y - radius_},
78+
{center_.x + radius_, center_.y},
79+
{center_.x, center_.y + radius_},
80+
{center_.x - radius_, center_.y},
81+
};
82+
83+
for (int i = 0; i < 4; i++) {
84+
corners[i] = transform * corners[i];
85+
}
86+
return Rect::MakePointBounds(std::begin(corners), std::end(corners));
87+
}
88+
89+
bool CircleGeometry::CoversArea(const Matrix& transform,
90+
const Rect& rect) const {
91+
return false;
92+
}
93+
94+
bool CircleGeometry::IsAxisAlignedRect() const {
95+
return false;
96+
}
97+
98+
} // namespace impeller
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "impeller/entity/geometry/geometry.h"
8+
9+
namespace impeller {
10+
11+
// Geometry class that can generate vertices (with or without texture
12+
// coordinates) for either filled or stroked circles
13+
class CircleGeometry final : public Geometry {
14+
public:
15+
explicit CircleGeometry(const Point& center, Scalar radius);
16+
17+
explicit CircleGeometry(const Point& center,
18+
Scalar radius,
19+
Scalar stroke_width);
20+
21+
~CircleGeometry() = default;
22+
23+
// |Geometry|
24+
bool CoversArea(const Matrix& transform, const Rect& rect) const override;
25+
26+
// |Geometry|
27+
bool IsAxisAlignedRect() const override;
28+
29+
private:
30+
// |Geometry|
31+
GeometryResult GetPositionBuffer(const ContentContext& renderer,
32+
const Entity& entity,
33+
RenderPass& pass) const override;
34+
35+
// |Geometry|
36+
GeometryVertexType GetVertexType() const override;
37+
38+
// |Geometry|
39+
std::optional<Rect> GetCoverage(const Matrix& transform) const override;
40+
41+
// |Geometry|
42+
GeometryResult GetPositionUVBuffer(Rect texture_coverage,
43+
Matrix effect_transform,
44+
const ContentContext& renderer,
45+
const Entity& entity,
46+
RenderPass& pass) const override;
47+
48+
Point center_;
49+
Scalar radius_;
50+
Scalar stroke_width_;
51+
52+
CircleGeometry(const CircleGeometry&) = delete;
53+
54+
CircleGeometry& operator=(const CircleGeometry&) = delete;
55+
};
56+
57+
} // namespace impeller

0 commit comments

Comments
 (0)