diff --git a/engine/src/flutter/impeller/compositor/command.h b/engine/src/flutter/impeller/compositor/command.h index 6f8dcd640f..e03fd955f4 100644 --- a/engine/src/flutter/impeller/compositor/command.h +++ b/engine/src/flutter/impeller/compositor/command.h @@ -34,6 +34,7 @@ struct Command { size_t index_count = 0u; std::string label; PrimitiveType primitive_type = PrimitiveType::kTriangle; + WindingOrder winding = WindingOrder::kClockwise; bool BindVertices(const VertexBuffer& buffer); diff --git a/engine/src/flutter/impeller/compositor/render_pass.mm b/engine/src/flutter/impeller/compositor/render_pass.mm index 7d6b1fae02..458f527768 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.mm +++ b/engine/src/flutter/impeller/compositor/render_pass.mm @@ -357,7 +357,9 @@ bool RenderPass::EncodeCommands(Allocator& allocator, command.pipeline->GetMTLRenderPipelineState()); pass_bindings.SetDepthStencilState( command.pipeline->GetMTLDepthStencilState()); - [pass setFrontFacingWinding:MTLWindingClockwise]; + [pass setFrontFacingWinding:command.winding == WindingOrder::kClockwise + ? MTLWindingClockwise + : MTLWindingCounterClockwise]; [pass setCullMode:MTLCullModeBack]; if (!bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex)) { return false; diff --git a/engine/src/flutter/impeller/compositor/tessellator.cc b/engine/src/flutter/impeller/compositor/tessellator.cc index 386f72e7c3..66bb326f1a 100644 --- a/engine/src/flutter/impeller/compositor/tessellator.cc +++ b/engine/src/flutter/impeller/compositor/tessellator.cc @@ -88,6 +88,7 @@ bool Tessellator::Tessellate(const std::vector& contours, return false; } + // TODO(csg): This copy can be elided entirely for the current use case. std::vector points; std::vector indices; @@ -111,4 +112,8 @@ bool Tessellator::Tessellate(const std::vector& contours, return true; } +WindingOrder Tessellator::GetFrontFaceWinding() const { + return WindingOrder::kClockwise; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/tessellator.h b/engine/src/flutter/impeller/compositor/tessellator.h index 24f05c3878..71eceb40ec 100644 --- a/engine/src/flutter/impeller/compositor/tessellator.h +++ b/engine/src/flutter/impeller/compositor/tessellator.h @@ -8,6 +8,7 @@ #include #include "flutter/fml/macros.h" +#include "impeller/compositor/formats.h" #include "impeller/geometry/point.h" namespace impeller { @@ -30,6 +31,8 @@ class Tessellator { FillType GetFillType() const; + WindingOrder GetFrontFaceWinding() const; + using VertexCallback = std::function; [[nodiscard]] bool Tessellate(const std::vector& vertices, VertexCallback callback) const; diff --git a/engine/src/flutter/impeller/geometry/path_builder.cc b/engine/src/flutter/impeller/geometry/path_builder.cc index b6d419bede..52da21e3a8 100644 --- a/engine/src/flutter/impeller/geometry/path_builder.cc +++ b/engine/src/flutter/impeller/geometry/path_builder.cc @@ -145,50 +145,59 @@ PathBuilder& PathBuilder::SmoothCubicCurveTo(Point point, PathBuilder& PathBuilder::AddRect(Rect rect) { current_ = rect.origin; - auto topLeft = rect.origin; - auto bottomLeft = rect.origin + Point{0.0, rect.size.height}; - auto bottomRight = rect.origin + Point{rect.size.width, rect.size.height}; - auto topRight = rect.origin + Point{rect.size.width, 0.0}; + auto tl = rect.origin; + auto bl = rect.origin + Point{0.0, rect.size.height}; + auto br = rect.origin + Point{rect.size.width, rect.size.height}; + auto tr = rect.origin + Point{rect.size.width, 0.0}; - prototype_.AddLinearComponent(topLeft, bottomLeft) - .AddLinearComponent(bottomLeft, bottomRight) - .AddLinearComponent(bottomRight, topRight) - .AddLinearComponent(topRight, topLeft); + prototype_.AddLinearComponent(tl, tr) + .AddLinearComponent(tr, br) + .AddLinearComponent(br, bl) + .AddLinearComponent(bl, tl); return *this; } -PathBuilder& PathBuilder::AddCircle(const Point& center, Scalar radius) { - current_ = center + Point{0.0, radius}; +PathBuilder& PathBuilder::AddCircle(const Point& c, Scalar r) { + current_ = c + Point{0.0, r}; - const Scalar diameter = radius * 2.0; - const Scalar magic = kArcApproximationMagic * radius; + // m for magic + const auto m = kArcApproximationMagic * r; - prototype_.AddCubicComponent( - {center.x + radius, center.y}, // - {center.x + radius + magic, center.y}, // - {center.x + diameter, center.y + radius - magic}, // - {center.x + diameter, center.y + radius} // + //---------------------------------------------------------------------------- + /// Top right arc. + /// + prototype_.AddCubicComponent({c.x, c.y - r}, // p1 + {c.x + m, c.y - r}, // cp1 + {c.x + r, c.y - m}, // cp2 + {c.x + r, c.y} // p2 ); - prototype_.AddCubicComponent( - {center.x + diameter, center.y + radius}, // - {center.x + diameter, center.y + radius + magic}, // - {center.x + radius + magic, center.y + diameter}, // - {center.x + radius, center.y + diameter} // + //---------------------------------------------------------------------------- + /// Bottom right arc. + /// + prototype_.AddCubicComponent({c.x + r, c.y}, // p1 + {c.x + r, c.y + m}, // cp1 + {c.x + m, c.y + r}, // cp2 + {c.x, c.y + r} // p2 ); - prototype_.AddCubicComponent( - {center.x + radius, center.y + diameter}, // - {center.x + radius - magic, center.y + diameter}, // - {center.x, center.y + radius + magic}, // - {center.x, center.y + radius} // + //---------------------------------------------------------------------------- + /// Bottom left arc. + /// + prototype_.AddCubicComponent({c.x, c.y + r}, // p1 + {c.x - m, c.y + r}, // cp1 + {c.x - r, c.y + m}, // cp2 + {c.x - r, c.y} // p2 ); - prototype_.AddCubicComponent({center.x, center.y + radius}, // - {center.x, center.y + radius - magic}, // - {center.x + radius - magic, center.y}, // - {center.x + radius, center.y} // + //---------------------------------------------------------------------------- + /// Top left arc. + /// + prototype_.AddCubicComponent({c.x - r, c.y}, // p1 + {c.x - r, c.y - m}, // cp1 + {c.x - m, c.y - r}, // cp2 + {c.x, c.y - r} // p2 ); return *this; diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index e9690fbfae..d8dfee10c2 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -252,7 +252,7 @@ TEST_F(PrimitivesTest, CanRenderToTexture) { } TEST_F(PrimitivesTest, CanRenderPath) { - auto path = PathBuilder{}.AddRect({10, 10, 100, 100}).CreatePath(); + auto path = PathBuilder{}.AddCircle({550, 550}, 500).CreatePath(); ASSERT_FALSE(path.GetBoundingBox().IsZero()); using BoxPipeline = PipelineT; @@ -306,6 +306,7 @@ TEST_F(PrimitivesTest, CanRenderPath) { FS::BindContents2(cmd, bridge, sampler); cmd.primitive_type = PrimitiveType::kTriangle; + cmd.winding = tessellator.GetFrontFaceWinding(); VS::UniformBuffer uniforms; uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize());