From 9ab707bf74799c0cdeee15dd9665b550ce282de8 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 24 Feb 2022 16:51:40 -0800 Subject: [PATCH] Support subcontours in strokes, lay groundwork for fills (flutter/engine#31) --- .../flutter/impeller/compiler/reflector.cc | 8 +++ .../src/flutter/impeller/entity/contents.cc | 34 ++++++---- .../impeller/entity/entity_unittests.cc | 42 ++++++++++++- .../impeller/entity/shaders/solid_stroke.frag | 2 + .../impeller/entity/shaders/solid_stroke.vert | 3 + .../impeller/geometry/geometry_unittests.cc | 5 +- engine/src/flutter/impeller/geometry/path.cc | 63 ++++++++++++++++--- engine/src/flutter/impeller/geometry/path.h | 28 ++++++++- .../flutter/impeller/geometry/path_builder.cc | 6 +- .../impeller/geometry/path_component.h | 13 ++++ .../flutter/impeller/renderer/tessellator.cc | 13 ++-- .../flutter/impeller/renderer/tessellator.h | 2 +- 12 files changed, 183 insertions(+), 36 deletions(-) diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 3266a292bf..b7f3f190e4 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -611,6 +611,14 @@ static VertexType VertexTypeFromInputResource( type.columns == 1u && type.vecsize == 3u && type.width == sizeof(float) * 8u) { result.type_name = "Vector3"; + } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float && + type.columns == 1u && type.vecsize == 1u && + type.width == sizeof(float) * 8u) { + result.type_name = "Scalar"; + } else if (type.basetype == spirv_cross::SPIRType::BaseType::Int && + type.columns == 1u && type.vecsize == 1u && + type.width == sizeof(int32_t) * 8u) { + result.type_name = "int32_t"; } else { // Catch all unknown padding. result.type_name = TypeNameWithPaddingOfSize(total_size); diff --git a/engine/src/flutter/impeller/entity/contents.cc b/engine/src/flutter/impeller/entity/contents.cc index f25a50d98c..9567d1c11f 100644 --- a/engine/src/flutter/impeller/entity/contents.cc +++ b/engine/src/flutter/impeller/entity/contents.cc @@ -235,8 +235,7 @@ bool TextureContents::Render(const ContentContext& renderer, auto coverage_coords = (vtx - coverage_rect->origin) / coverage_rect->size; data.texture_coords = - (source_rect_.origin + - source_rect_.size * coverage_coords) / + (source_rect_.origin + source_rect_.size * coverage_coords) / texture_size; vertex_builder.AppendVertex(data); }); @@ -301,11 +300,13 @@ static VertexBuffer CreateSolidStrokeVertices(const Path& path, VertexBufferBuilder vtx_builder; auto polyline = path.CreatePolyline(); - for (size_t i = 0, polyline_size = polyline.size(); i < polyline_size; i++) { + for (size_t i = 0, polyline_size = polyline.points.size(); i < polyline_size; + i++) { const auto is_last_point = i == polyline_size - 1; - const auto& p1 = polyline[i]; - const auto& p2 = is_last_point ? polyline[i - 1] : polyline[i + 1]; + const auto& p1 = polyline.points[i]; + const auto& p2 = + is_last_point ? polyline.points[i - 1] : polyline.points[i + 1]; const auto diff = p2 - p1; @@ -316,18 +317,27 @@ static VertexBuffer CreateSolidStrokeVertices(const Path& path, VS::PerVertexData vtx; vtx.vertex_position = p1; - - if (i == 0) { - vtx.vertex_normal = -normal; - vtx_builder.AppendVertex(vtx); - vtx.vertex_normal = normal; - vtx_builder.AppendVertex(vtx); - } + auto pen_down = + polyline.breaks.find(i) == polyline.breaks.end() ? 1.0 : 0.0; vtx.vertex_normal = normal; + vtx.pen_down = pen_down; vtx_builder.AppendVertex(vtx); + vtx.vertex_normal = -normal; + vtx.pen_down = pen_down; vtx_builder.AppendVertex(vtx); + + // Put the pen down again for the next contour. + if (!pen_down) { + vtx.vertex_normal = normal; + vtx.pen_down = 1.0; + vtx_builder.AppendVertex(vtx); + + vtx.vertex_normal = -normal; + vtx.pen_down = 1.0; + vtx_builder.AppendVertex(vtx); + } } return vtx_builder.CreateVertexBuffer(buffer); diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 8a678e7475..c5f9d52168 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -25,6 +25,47 @@ TEST_F(EntityTest, CanDrawRect) { ASSERT_TRUE(OpenPlaygroundHere(entity)); } +TEST_F(EntityTest, ThreeStrokesInOnePath) { + Path path = PathBuilder{} + .MoveTo({100, 100}) + .LineTo({100, 200}) + .MoveTo({100, 300}) + .LineTo({100, 400}) + .MoveTo({100, 500}) + .LineTo({100, 600}) + .TakePath(); + + Entity entity; + entity.SetPath(path); + auto contents = std::make_unique(); + contents->SetColor(Color::Red()); + contents->SetStrokeSize(5.0); + entity.SetContents(std::move(contents)); + ASSERT_TRUE(OpenPlaygroundHere(entity)); +} + +TEST_F(EntityTest, TriangleInsideASquare) { + Path path = PathBuilder{} + .MoveTo({10, 10}) + .LineTo({210, 10}) + .LineTo({210, 210}) + .LineTo({10, 210}) + .Close() + .MoveTo({50, 50}) + .LineTo({100, 50}) + .LineTo({50, 150}) + .Close() + .TakePath(); + + Entity entity; + entity.SetPath(path); + auto contents = std::make_unique(); + contents->SetColor(Color::Red()); + contents->SetStrokeSize(5.0); + entity.SetContents(std::move(contents)); + ASSERT_TRUE(OpenPlaygroundHere(entity)); +} + TEST_F(EntityTest, DISABLED_BadCubicCurveTest) { // Compare with https://fiddle.skia.org/c/b3625f26122c9de7afe7794fcf25ead3 Path path = @@ -45,7 +86,6 @@ TEST_F(EntityTest, DISABLED_BadCubicCurveTest) { entity.SetPath(path); entity.SetContents(SolidColorContents::Make(Color::Red())); ASSERT_TRUE(OpenPlaygroundHere(entity)); - } TEST_F(EntityTest, DISABLED_BadCubicCurveAndOverlapTest) { diff --git a/engine/src/flutter/impeller/entity/shaders/solid_stroke.frag b/engine/src/flutter/impeller/entity/shaders/solid_stroke.frag index 88b1f7635d..3d997a40ee 100644 --- a/engine/src/flutter/impeller/entity/shaders/solid_stroke.frag +++ b/engine/src/flutter/impeller/entity/shaders/solid_stroke.frag @@ -3,9 +3,11 @@ // found in the LICENSE file. in vec4 stroke_color; +in float v_pen_down; out vec4 frag_color; void main() { frag_color = stroke_color; + frag_color.a *= floor(v_pen_down); } diff --git a/engine/src/flutter/impeller/entity/shaders/solid_stroke.vert b/engine/src/flutter/impeller/entity/shaders/solid_stroke.vert index 896fb56599..b92e63828c 100644 --- a/engine/src/flutter/impeller/entity/shaders/solid_stroke.vert +++ b/engine/src/flutter/impeller/entity/shaders/solid_stroke.vert @@ -13,12 +13,15 @@ uniform StrokeInfo { in vec2 vertex_position; in vec2 vertex_normal; +in float pen_down; out vec4 stroke_color; +out float v_pen_down; void main() { // Push one vertex by the half stroke size along the normal vector. vec2 offset = vertex_normal * vec2(stroke_info.size * 0.5); gl_Position = frame_info.mvp * vec4(vertex_position + offset, 0.0, 1.0); stroke_color = stroke_info.color; + v_pen_down = pen_down; } diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 61702c55af..5791393800 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -178,7 +178,8 @@ TEST(GeometryTest, SimplePath) { ASSERT_EQ(cubic.cp1, cp1); ASSERT_EQ(cubic.cp2, cp2); ASSERT_EQ(cubic.p2, p2); - }); + }, + [](size_t index, const MovePathComponent& move) { ASSERT_TRUE(false); }); } TEST(GeometryTest, BoundingBoxCubic) { @@ -586,7 +587,7 @@ TEST(GeometryTest, RectContainsRect) { } TEST(GeometryTest, CubicPathComponentPolylineDoesNotIncludePointOne) { - CubicPathComponent component({10, 10}, {20,35}, {35, 20}, {40, 40}); + CubicPathComponent component({10, 10}, {20, 35}, {35, 20}, {40, 40}); SmoothingApproximation approximation; auto polyline = component.CreatePolyline(approximation); ASSERT_NE(polyline.front().x, 10); diff --git a/engine/src/flutter/impeller/geometry/path.cc b/engine/src/flutter/impeller/geometry/path.cc index a9e09e5b83..a5c0268bfc 100644 --- a/engine/src/flutter/impeller/geometry/path.cc +++ b/engine/src/flutter/impeller/geometry/path.cc @@ -42,10 +42,16 @@ Path& Path::AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2) { return *this; } -void Path::EnumerateComponents( - Applier linear_applier, - Applier quad_applier, - Applier cubic_applier) const { +Path& Path::AddMoveComponent(Point destination) { + moves_.emplace_back(destination); + components_.emplace_back(ComponentType::kMove, moves_.size() - 1); + return *this; +} + +void Path::EnumerateComponents(Applier linear_applier, + Applier quad_applier, + Applier cubic_applier, + Applier move_applier) const { size_t currentIndex = 0; for (const auto& component : components_) { switch (component.type) { @@ -64,6 +70,11 @@ void Path::EnumerateComponents( cubic_applier(currentIndex, cubics_[component.index]); } break; + case ComponentType::kMove: + if (move_applier) { + move_applier(currentIndex, moves_[component.index]); + } + break; } currentIndex++; } @@ -112,6 +123,20 @@ bool Path::GetCubicComponentAtIndex(size_t index, return true; } +bool Path::GetMoveComponentAtIndex(size_t index, + MovePathComponent& move) const { + if (index >= components_.size()) { + return false; + } + + if (components_[index].type != ComponentType::kMove) { + return false; + } + + move = moves_[components_[index].index]; + return true; +} + bool Path::UpdateLinearComponentAtIndex(size_t index, const LinearPathComponent& linear) { if (index >= components_.size()) { @@ -155,12 +180,27 @@ bool Path::UpdateCubicComponentAtIndex(size_t index, return true; } -std::vector Path::CreatePolyline( +bool Path::UpdateMoveComponentAtIndex(size_t index, + const MovePathComponent& move) { + if (index >= components_.size()) { + return false; + } + + if (components_[index].type != ComponentType::kMove) { + return false; + } + + moves_[components_[index].index] = move; + return true; +} + +Path::Polyline Path::CreatePolyline( const SmoothingApproximation& approximation) const { - std::vector points; - auto collect_points = [&points](const std::vector& collection) { - points.reserve(points.size() + collection.size()); - points.insert(points.end(), collection.begin(), collection.end()); + Polyline polyline; + auto collect_points = [&polyline](const std::vector& collection) { + polyline.points.reserve(polyline.points.size() + collection.size()); + polyline.points.insert(polyline.points.end(), collection.begin(), + collection.end()); }; for (const auto& component : components_) { switch (component.type) { @@ -173,9 +213,12 @@ std::vector Path::CreatePolyline( case ComponentType::kCubic: collect_points(cubics_[component.index].CreatePolyline(approximation)); break; + case ComponentType::kMove: + polyline.breaks.insert(polyline.points.size()); + break; } } - return points; + return polyline; } std::optional Path::GetBoundingBox() const { diff --git a/engine/src/flutter/impeller/geometry/path.h b/engine/src/flutter/impeller/geometry/path.h index e14f057e7f..450000e896 100644 --- a/engine/src/flutter/impeller/geometry/path.h +++ b/engine/src/flutter/impeller/geometry/path.h @@ -6,6 +6,7 @@ #include #include +#include #include #include "impeller/geometry/path_component.h" @@ -22,7 +23,9 @@ enum class FillType { //------------------------------------------------------------------------------ /// @brief Paths are lightweight objects that describe a collection of -/// linear, quadratic, or cubic segments. +/// linear, quadratic, or cubic segments. These segments may be +/// be broken up by move commands, which are effectively linear +/// commands that pick up the pen rather than continuing to draw. /// /// All shapes supported by Impeller are paths either directly or /// via approximation (in the case of circles). @@ -36,6 +39,17 @@ class Path { kLinear, kQuadratic, kCubic, + kMove, + }; + + /// One or more contours represented as a series of points and indices in + /// the point vector representing the start of a new contour. + struct Polyline { + /// Points in the polyline, which may represent multiple contours specified + /// by indices in |breaks|. + std::vector points; + /// Indices of points that end a subcontour. + std::set breaks; }; Path(); @@ -54,11 +68,14 @@ class Path { Path& AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2); + Path& AddMoveComponent(Point destination); + template using Applier = std::function; void EnumerateComponents(Applier linearApplier, Applier quadApplier, - Applier cubicApplier) const; + Applier cubicApplier, + Applier moveApplier) const; bool GetLinearComponentAtIndex(size_t index, LinearPathComponent& linear) const; @@ -68,6 +85,8 @@ class Path { bool GetCubicComponentAtIndex(size_t index, CubicPathComponent& cubic) const; + bool GetMoveComponentAtIndex(size_t index, MovePathComponent& move) const; + bool UpdateLinearComponentAtIndex(size_t index, const LinearPathComponent& linear); @@ -76,7 +95,9 @@ class Path { bool UpdateCubicComponentAtIndex(size_t index, CubicPathComponent& cubic); - std::vector CreatePolyline( + bool UpdateMoveComponentAtIndex(size_t index, const MovePathComponent& move); + + Polyline CreatePolyline( const SmoothingApproximation& approximation = {}) const; std::optional GetBoundingBox() const; @@ -99,6 +120,7 @@ class Path { std::vector linears_; std::vector quads_; std::vector cubics_; + std::vector moves_; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/path_builder.cc b/engine/src/flutter/impeller/geometry/path_builder.cc index d65c1dcf12..aba307b089 100644 --- a/engine/src/flutter/impeller/geometry/path_builder.cc +++ b/engine/src/flutter/impeller/geometry/path_builder.cc @@ -27,6 +27,7 @@ Path PathBuilder::TakePath(FillType fill) { PathBuilder& PathBuilder::MoveTo(Point point, bool relative) { current_ = relative ? current_ + point : point; subpath_start_ = current_; + prototype_.AddMoveComponent(current_); return *this; } @@ -343,7 +344,10 @@ PathBuilder& PathBuilder::AddPath(const Path& path) { auto cubic = [&](size_t index, const CubicPathComponent& c) { prototype_.AddCubicComponent(c.p1, c.cp1, c.cp2, c.p2); }; - path.EnumerateComponents(linear, quadratic, cubic); + auto move = [&](size_t index, const MovePathComponent& m) { + prototype_.AddMoveComponent(m.destination); + }; + path.EnumerateComponents(linear, quadratic, cubic, move); return *this; } diff --git a/engine/src/flutter/impeller/geometry/path_component.h b/engine/src/flutter/impeller/geometry/path_component.h index 289b72793e..c9f03ddad8 100644 --- a/engine/src/flutter/impeller/geometry/path_component.h +++ b/engine/src/flutter/impeller/geometry/path_component.h @@ -6,6 +6,7 @@ #include +#include "impeller/geometry/point.h" #include "impeller/geometry/rect.h" #include "impeller/geometry/scalar.h" @@ -106,4 +107,16 @@ struct CubicPathComponent { } }; +struct MovePathComponent { + Point destination; + + MovePathComponent() {} + + MovePathComponent(Point p) : destination(p) {} + + bool operator==(const MovePathComponent& other) const { + return destination == other.destination; + } +}; + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/tessellator.cc b/engine/src/flutter/impeller/renderer/tessellator.cc index 38d724b263..3bbcfa3042 100644 --- a/engine/src/flutter/impeller/renderer/tessellator.cc +++ b/engine/src/flutter/impeller/renderer/tessellator.cc @@ -4,6 +4,7 @@ #include "impeller/renderer/tessellator.h" +#include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" #include "third_party/libtess2/Include/tesselator.h" @@ -35,7 +36,7 @@ static void DestroyTessellator(TESStesselator* tessellator) { } } -bool Tessellator::Tessellate(const std::vector& contours, +bool Tessellator::Tessellate(const Path::Polyline& contours, VertexCallback callback) const { TRACE_EVENT0("impeller", "Tessellator::Tessellate"); if (!callback) { @@ -60,11 +61,11 @@ bool Tessellator::Tessellate(const std::vector& contours, /// Feed contour information to the tessellator. /// static_assert(sizeof(Point) == 2 * sizeof(float)); - ::tessAddContour(tessellator.get(), // the C tessellator - kVertexSize, // - contours.data(), // - sizeof(Point), // - contours.size() // + ::tessAddContour(tessellator.get(), // the C tessellator + kVertexSize, // + contours.points.data(), // + sizeof(Point), // + contours.points.size() // ); //---------------------------------------------------------------------------- diff --git a/engine/src/flutter/impeller/renderer/tessellator.h b/engine/src/flutter/impeller/renderer/tessellator.h index ee27d92474..db8a1f2910 100644 --- a/engine/src/flutter/impeller/renderer/tessellator.h +++ b/engine/src/flutter/impeller/renderer/tessellator.h @@ -38,7 +38,7 @@ class Tessellator { /// /// @return If tessellation was successful. /// - bool Tessellate(const std::vector& polyline, + bool Tessellate(const Path::Polyline& polyline, VertexCallback callback) const; private: