diff --git a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc index bd06de87ff..870d9faa32 100644 --- a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.cc @@ -4,10 +4,55 @@ #include "impeller/entity/geometry/stroke_path_geometry.h" +#include "impeller/core/buffer_view.h" +#include "impeller/core/formats.h" +#include "impeller/entity/texture_fill.vert.h" #include "impeller/geometry/path_builder.h" +#include "impeller/geometry/path_component.h" namespace impeller { +class PositionWriter : public VertexWriter { + public: + void AppendVertex(const Point& point) override { + data_.emplace_back(SolidFillVertexShader::PerVertexData{.position = point}); + } + + const std::vector& GetData() const { + return data_; + } + + private: + std::vector data_ = {}; +}; + +class PositionUVWriter : public VertexWriter { + public: + PositionUVWriter(Point texture_origin, + Size texture_coverage, + const Matrix& effect_transform) + : texture_origin_(texture_origin), + texture_coverage_(texture_coverage), + effect_transform_(effect_transform) {} + + const std::vector& GetData() const { + return data_; + } + + void AppendVertex(const Point& point) override { + data_.emplace_back(TextureFillVertexShader::PerVertexData{ + .position = point, + .texture_coords = + effect_transform_ * (point - texture_origin_) / texture_coverage_}); + } + + private: + std::vector data_ = {}; + const Point texture_origin_; + const Size texture_coverage_; + const Matrix effect_transform_; +}; + StrokePathGeometry::StrokePathGeometry(const Path& path, Scalar stroke_width, Scalar miter_limit, @@ -38,225 +83,312 @@ Join StrokePathGeometry::GetStrokeJoin() const { } // static -Scalar StrokePathGeometry::CreateBevelAndGetDirection( - VertexBufferBuilder& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset) { - SolidFillVertexShader::PerVertexData vtx; - vtx.position = position; +Scalar StrokePathGeometry::CreateBevelAndGetDirection(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset) { + Point vtx = position; vtx_builder.AppendVertex(vtx); Scalar dir = start_offset.Cross(end_offset) > 0 ? -1 : 1; - vtx.position = position + start_offset * dir; + vtx = position + start_offset * dir; vtx_builder.AppendVertex(vtx); - vtx.position = position + end_offset * dir; + vtx = position + end_offset * dir; vtx_builder.AppendVertex(vtx); return dir; } +// static +void StrokePathGeometry::CreateMiterJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale) { + Point start_normal = start_offset.Normalize(); + Point end_normal = end_offset.Normalize(); + + // 1 for no joint (straight line), 0 for max joint (180 degrees). + Scalar alignment = (start_normal.Dot(end_normal) + 1) / 2; + if (ScalarNearlyEqual(alignment, 1)) { + return; + } + + Scalar direction = CreateBevelAndGetDirection(vtx_builder, position, + start_offset, end_offset); + + Point miter_point = (((start_offset + end_offset) / 2) / alignment); + if (miter_point.GetDistanceSquared({0, 0}) > miter_limit * miter_limit) { + return; // Convert to bevel when we exceed the miter limit. + } + + // Outer miter point. + VS::PerVertexData vtx; + vtx.position = position + miter_point * direction; + vtx_builder.AppendVertex(vtx.position); +} +// static +void StrokePathGeometry::CreateRoundJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale) { + Point start_normal = start_offset.Normalize(); + Point end_normal = end_offset.Normalize(); + + // 0 for no joint (straight line), 1 for max joint (180 degrees). + Scalar alignment = 1 - (start_normal.Dot(end_normal) + 1) / 2; + if (ScalarNearlyEqual(alignment, 0)) { + return; + } + + Scalar direction = CreateBevelAndGetDirection(vtx_builder, position, + start_offset, end_offset); + + Point middle = + (start_offset + end_offset).Normalize() * start_offset.GetLength(); + Point middle_normal = middle.Normalize(); + + Point middle_handle = middle + Point(-middle.y, middle.x) * + PathBuilder::kArcApproximationMagic * + alignment * direction; + Point start_handle = start_offset + Point(start_offset.y, -start_offset.x) * + PathBuilder::kArcApproximationMagic * + alignment * direction; + + VS::PerVertexData vtx; + CubicPathComponent(start_offset, start_handle, middle_handle, middle) + .ToLinearPathComponents(scale, [&vtx_builder, direction, &vtx, position, + middle_normal](const Point& point) { + vtx.position = position + point * direction; + vtx_builder.AppendVertex(vtx.position); + vtx.position = position + (-point * direction).Reflect(middle_normal); + vtx_builder.AppendVertex(vtx.position); + }); +} + +// static +void StrokePathGeometry::CreateBevelJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale) { + CreateBevelAndGetDirection(vtx_builder, position, start_offset, end_offset); +} + // static StrokePathGeometry::JoinProc StrokePathGeometry::GetJoinProc(Join stroke_join) { - using VS = SolidFillVertexShader; - StrokePathGeometry::JoinProc join_proc; switch (stroke_join) { case Join::kBevel: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - CreateBevelAndGetDirection(vtx_builder, position, start_offset, - end_offset); - }; - break; + return &StrokePathGeometry::CreateBevelJoin; case Join::kMiter: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - Point start_normal = start_offset.Normalize(); - Point end_normal = end_offset.Normalize(); - - // 1 for no joint (straight line), 0 for max joint (180 degrees). - Scalar alignment = (start_normal.Dot(end_normal) + 1) / 2; - if (ScalarNearlyEqual(alignment, 1)) { - return; - } - - Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, - start_offset, end_offset); - - Point miter_point = (start_offset + end_offset) / 2 / alignment; - if (miter_point.GetDistanceSquared({0, 0}) > - miter_limit * miter_limit) { - return; // Convert to bevel when we exceed the miter limit. - } - - // Outer miter point. - VS::PerVertexData vtx; - vtx.position = position + miter_point * dir; - vtx_builder.AppendVertex(vtx); - }; - break; + return &StrokePathGeometry::CreateMiterJoin; case Join::kRound: - join_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& start_offset, - const Point& end_offset, Scalar miter_limit, - Scalar scale) { - Point start_normal = start_offset.Normalize(); - Point end_normal = end_offset.Normalize(); - - // 0 for no joint (straight line), 1 for max joint (180 degrees). - Scalar alignment = 1 - (start_normal.Dot(end_normal) + 1) / 2; - if (ScalarNearlyEqual(alignment, 0)) { - return; - } - - Scalar dir = CreateBevelAndGetDirection(vtx_builder, position, - start_offset, end_offset); - - Point middle = - (start_offset + end_offset).Normalize() * start_offset.GetLength(); - Point middle_normal = middle.Normalize(); - - Point middle_handle = middle + Point(-middle.y, middle.x) * - PathBuilder::kArcApproximationMagic * - alignment * dir; - Point start_handle = - start_offset + Point(start_offset.y, -start_offset.x) * - PathBuilder::kArcApproximationMagic * alignment * - dir; - - std::vector arc_points; - CubicPathComponent(start_offset, start_handle, middle_handle, middle) - .AppendPolylinePoints(scale, arc_points); - - VS::PerVertexData vtx; - for (const auto& point : arc_points) { - vtx.position = position + point * dir; - vtx_builder.AppendVertex(vtx); - vtx.position = position + (-point * dir).Reflect(middle_normal); - vtx_builder.AppendVertex(vtx); - } - }; - break; + return &StrokePathGeometry::CreateRoundJoin; } - return join_proc; +} + +// static +void StrokePathGeometry::CreateButtCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + VS::PerVertexData vtx; + vtx.position = position + orientation; + vtx_builder.AppendVertex(vtx.position); + vtx.position = position - orientation; + vtx_builder.AppendVertex(vtx.position); +} + +// static +void StrokePathGeometry::CreateRoundCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + Point forward(offset.y, -offset.x); + Point forward_normal = forward.Normalize(); + + CubicPathComponent arc; + if (reverse) { + arc = CubicPathComponent( + forward, forward + orientation * PathBuilder::kArcApproximationMagic, + orientation + forward * PathBuilder::kArcApproximationMagic, + orientation); + } else { + arc = CubicPathComponent( + orientation, + orientation + forward * PathBuilder::kArcApproximationMagic, + forward + orientation * PathBuilder::kArcApproximationMagic, forward); + } + + Point vtx = position + orientation; + vtx_builder.AppendVertex(vtx); + vtx = position - orientation; + vtx_builder.AppendVertex(vtx); + + arc.ToLinearPathComponents(scale, [&vtx_builder, &vtx, forward_normal, + position](const Point& point) { + vtx = position + point; + vtx_builder.AppendVertex(vtx); + vtx = position + (-point).Reflect(forward_normal); + vtx_builder.AppendVertex(vtx); + }); +} + +// static +void StrokePathGeometry::CreateSquareCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse) { + Point orientation = offset * (reverse ? -1 : 1); + Point forward(offset.y, -offset.x); + + Point vtx = position + orientation; + vtx_builder.AppendVertex(vtx); + vtx = position - orientation; + vtx_builder.AppendVertex(vtx); + vtx = position + orientation + forward; + vtx_builder.AppendVertex(vtx); + vtx = position - orientation + forward; + vtx_builder.AppendVertex(vtx); } // static StrokePathGeometry::CapProc StrokePathGeometry::GetCapProc(Cap stroke_cap) { - using VS = SolidFillVertexShader; - StrokePathGeometry::CapProc cap_proc; switch (stroke_cap) { case Cap::kButt: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - VS::PerVertexData vtx; - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - }; - break; + return &StrokePathGeometry::CreateButtCap; case Cap::kRound: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - - VS::PerVertexData vtx; - - Point forward(offset.y, -offset.x); - Point forward_normal = forward.Normalize(); - - CubicPathComponent arc; - if (reverse) { - arc = CubicPathComponent( - forward, - forward + orientation * PathBuilder::kArcApproximationMagic, - orientation + forward * PathBuilder::kArcApproximationMagic, - orientation); - } else { - arc = CubicPathComponent( - orientation, - orientation + forward * PathBuilder::kArcApproximationMagic, - forward + orientation * PathBuilder::kArcApproximationMagic, - forward); - } - - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - std::vector arc_points; - arc.AppendPolylinePoints(scale, arc_points); - for (const auto& point : arc_points) { - vtx.position = position + point; - vtx_builder.AppendVertex(vtx); - vtx.position = position + (-point).Reflect(forward_normal); - vtx_builder.AppendVertex(vtx); - } - }; - break; + return &StrokePathGeometry::CreateRoundCap; case Cap::kSquare: - cap_proc = [](VertexBufferBuilder& vtx_builder, - const Point& position, const Point& offset, Scalar scale, - bool reverse) { - Point orientation = offset * (reverse ? -1 : 1); - - VS::PerVertexData vtx; - - Point forward(offset.y, -offset.x); - - vtx.position = position + orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation; - vtx_builder.AppendVertex(vtx); - vtx.position = position + orientation + forward; - vtx_builder.AppendVertex(vtx); - vtx.position = position - orientation + forward; - vtx_builder.AppendVertex(vtx); - }; - break; + return &StrokePathGeometry::CreateSquareCap; } - return cap_proc; } -// static -VertexBufferBuilder -StrokePathGeometry::CreateSolidStrokeVertices( - const Path& path, - Scalar stroke_width, - Scalar scaled_miter_limit, - const StrokePathGeometry::JoinProc& join_proc, - const StrokePathGeometry::CapProc& cap_proc, - Scalar scale) { - VertexBufferBuilder vtx_builder; - auto point_buffer = std::make_unique>(); - // 512 is an arbitrary choice that should be big enough for most paths without - // needing to reallocate. If we have motivating benchmarks we should raise or - // lower this number, cause dnfield just made it up! - point_buffer->reserve(512); - auto polyline = path.CreatePolyline(scale, std::move(point_buffer)); +class StrokeGenerator { + public: + StrokeGenerator(const Path::Polyline& p_polyline, + const Scalar p_stroke_width, + const Scalar p_scaled_miter_limit, + const StrokePathGeometry::JoinProc& p_join_proc, + const StrokePathGeometry::CapProc& p_cap_proc, + const Scalar p_scale) + : polyline(p_polyline), + stroke_width(p_stroke_width), + scaled_miter_limit(p_scaled_miter_limit), + join_proc(p_join_proc), + cap_proc(p_cap_proc), + scale(p_scale) {} - VS::PerVertexData vtx; + void Generate(VertexWriter& vtx_builder) { + for (size_t contour_i = 0; contour_i < polyline.contours.size(); + contour_i++) { + const Path::PolylineContour& contour = polyline.contours[contour_i]; + size_t contour_start_point_i, contour_end_point_i; + std::tie(contour_start_point_i, contour_end_point_i) = + polyline.GetContourPointBounds(contour_i); - // Offset state. - Point offset; - Point previous_offset; // Used for computing joins. + auto contour_delta = contour_end_point_i - contour_start_point_i; + if (contour_delta == 1) { + Point p = polyline.GetPoint(contour_start_point_i); + cap_proc(vtx_builder, p, {-stroke_width * 0.5f, 0}, scale, + /*reverse=*/false); + cap_proc(vtx_builder, p, {stroke_width * 0.5f, 0}, scale, + /*reverse=*/false); + continue; + } else if (contour_delta == 0) { + continue; // This contour has no renderable content. + } - // Computes offset by calculating the direction from point_i - 1 to point_i if - // point_i is within `contour_start_point_i` and `contour_end_point_i`; - // Otherwise, it uses direction from contour. - auto compute_offset = [&polyline, &offset, &previous_offset, &stroke_width]( - const size_t point_i, - const size_t contour_start_point_i, - const size_t contour_end_point_i, - const Path::PolylineContour& contour) { + previous_offset = offset; + offset = ComputeOffset(contour_start_point_i, contour_start_point_i, + contour_end_point_i, contour); + const Point contour_first_offset = offset; + + if (contour_i > 0) { + // This branch only executes when we've just finished drawing a contour + // and are switching to a new one. + // We're drawing a triangle strip, so we need to "pick up the pen" by + // appending two vertices at the end of the previous contour and two + // vertices at the start of the new contour (thus connecting the two + // contours with two zero volume triangles, which will be discarded by + // the rasterizer). + vtx.position = polyline.GetPoint(contour_start_point_i - 1); + // Append two vertices when "picking up" the pen so that the triangle + // drawn when moving to the beginning of the new contour will have zero + // volume. + vtx_builder.AppendVertex(vtx.position); + vtx_builder.AppendVertex(vtx.position); + + vtx.position = polyline.GetPoint(contour_start_point_i); + // Append two vertices at the beginning of the new contour, which + // appends two triangles of zero area. + vtx_builder.AppendVertex(vtx.position); + vtx_builder.AppendVertex(vtx.position); + } + + // Generate start cap. + if (!polyline.contours[contour_i].is_closed) { + Point cap_offset = + Vector2(-contour.start_direction.y, contour.start_direction.x) * + stroke_width * 0.5f; // Counterclockwise normal + cap_proc(vtx_builder, polyline.GetPoint(contour_start_point_i), + cap_offset, scale, /*reverse=*/true); + } + + for (size_t contour_component_i = 0; + contour_component_i < contour.components.size(); + contour_component_i++) { + const Path::PolylineContour::Component& component = + contour.components[contour_component_i]; + bool is_last_component = + contour_component_i == contour.components.size() - 1; + + size_t component_start_index = component.component_start_index; + size_t component_end_index = + is_last_component ? contour_end_point_i - 1 + : contour.components[contour_component_i + 1] + .component_start_index; + if (component.is_curve) { + AddVerticesForCurveComponent( + vtx_builder, component_start_index, component_end_index, + contour_start_point_i, contour_end_point_i, contour); + } else { + AddVerticesForLinearComponent( + vtx_builder, component_start_index, component_end_index, + contour_start_point_i, contour_end_point_i, contour); + } + } + + // Generate end cap or join. + if (!contour.is_closed) { + auto cap_offset = + Vector2(-contour.end_direction.y, contour.end_direction.x) * + stroke_width * 0.5f; // Clockwise normal + cap_proc(vtx_builder, polyline.GetPoint(contour_end_point_i - 1), + cap_offset, scale, /*reverse=*/false); + } else { + join_proc(vtx_builder, polyline.GetPoint(contour_start_point_i), offset, + contour_first_offset, scaled_miter_limit, scale); + } + } + } + + /// Computes offset by calculating the direction from point_i - 1 to point_i + /// if point_i is within `contour_start_point_i` and `contour_end_point_i`; + /// Otherwise, it uses direction from contour. + Point ComputeOffset(const size_t point_i, + const size_t contour_start_point_i, + const size_t contour_end_point_i, + const Path::PolylineContour& contour) const { Point direction; if (point_i >= contour_end_point_i) { direction = contour.end_direction; @@ -266,174 +398,105 @@ StrokePathGeometry::CreateSolidStrokeVertices( direction = (polyline.GetPoint(point_i) - polyline.GetPoint(point_i - 1)) .Normalize(); } - previous_offset = offset; - offset = Vector2{-direction.y, direction.x} * stroke_width * 0.5; - }; + return Vector2{-direction.y, direction.x} * stroke_width * 0.5f; + } - auto add_vertices_for_linear_component = - [&vtx_builder, &offset, &previous_offset, &vtx, &polyline, - &compute_offset, scaled_miter_limit, scale, &join_proc]( - const size_t component_start_index, const size_t component_end_index, - const size_t contour_start_point_i, const size_t contour_end_point_i, - const Path::PolylineContour& contour) { - auto is_last_component = - component_start_index == - contour.components.back().component_start_index; + void AddVerticesForLinearComponent(VertexWriter& vtx_builder, + const size_t component_start_index, + const size_t component_end_index, + const size_t contour_start_point_i, + const size_t contour_end_point_i, + const Path::PolylineContour& contour) { + bool is_last_component = component_start_index == + contour.components.back().component_start_index; - for (size_t point_i = component_start_index; - point_i < component_end_index; point_i++) { - auto is_end_of_component = point_i == component_end_index - 1; - vtx.position = polyline.GetPoint(point_i) + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.GetPoint(point_i) - offset; - vtx_builder.AppendVertex(vtx); + for (size_t point_i = component_start_index; point_i < component_end_index; + point_i++) { + bool is_end_of_component = point_i == component_end_index - 1; + vtx.position = polyline.GetPoint(point_i) + offset; + vtx_builder.AppendVertex(vtx.position); + vtx.position = polyline.GetPoint(point_i) - offset; + vtx_builder.AppendVertex(vtx.position); - // For line components, two additional points need to be appended - // prior to appending a join connecting the next component. - vtx.position = polyline.GetPoint(point_i + 1) + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.GetPoint(point_i + 1) - offset; - vtx_builder.AppendVertex(vtx); + // For line components, two additional points need to be appended + // prior to appending a join connecting the next component. + vtx.position = polyline.GetPoint(point_i + 1) + offset; + vtx_builder.AppendVertex(vtx.position); + vtx.position = polyline.GetPoint(point_i + 1) - offset; + vtx_builder.AppendVertex(vtx.position); - compute_offset(point_i + 2, contour_start_point_i, - contour_end_point_i, contour); - if (!is_last_component && is_end_of_component) { - // Generate join from the current line to the next line. - join_proc(vtx_builder, polyline.GetPoint(point_i + 1), - previous_offset, offset, scaled_miter_limit, scale); - } - } - }; - - auto add_vertices_for_curve_component = - [&vtx_builder, &offset, &previous_offset, &vtx, &polyline, - &compute_offset, scaled_miter_limit, scale, &join_proc]( - const size_t component_start_index, const size_t component_end_index, - const size_t contour_start_point_i, const size_t contour_end_point_i, - const Path::PolylineContour& contour) { - auto is_last_component = - component_start_index == - contour.components.back().component_start_index; - - for (size_t point_i = component_start_index; - point_i < component_end_index; point_i++) { - auto is_end_of_component = point_i == component_end_index - 1; - - vtx.position = polyline.GetPoint(point_i) + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.GetPoint(point_i) - offset; - vtx_builder.AppendVertex(vtx); - - compute_offset(point_i + 2, contour_start_point_i, - contour_end_point_i, contour); - // For curve components, the polyline is detailed enough such that - // it can avoid worrying about joins altogether. - if (is_end_of_component) { - vtx.position = polyline.GetPoint(point_i + 1) + offset; - vtx_builder.AppendVertex(vtx); - vtx.position = polyline.GetPoint(point_i + 1) - offset; - vtx_builder.AppendVertex(vtx); - // Generate join from the current line to the next line. - if (!is_last_component) { - join_proc(vtx_builder, polyline.GetPoint(point_i + 1), - previous_offset, offset, scaled_miter_limit, scale); - } - } - } - }; - - for (size_t contour_i = 0; contour_i < polyline.contours.size(); - contour_i++) { - auto contour = polyline.contours[contour_i]; - size_t contour_start_point_i, contour_end_point_i; - std::tie(contour_start_point_i, contour_end_point_i) = - polyline.GetContourPointBounds(contour_i); - - switch (contour_end_point_i - contour_start_point_i) { - case 1: { - Point p = polyline.GetPoint(contour_start_point_i); - cap_proc(vtx_builder, p, {-stroke_width * 0.5f, 0}, scale, false); - cap_proc(vtx_builder, p, {stroke_width * 0.5f, 0}, scale, false); - continue; + previous_offset = offset; + offset = ComputeOffset(point_i + 2, contour_start_point_i, + contour_end_point_i, contour); + if (!is_last_component && is_end_of_component) { + // Generate join from the current line to the next line. + join_proc(vtx_builder, polyline.GetPoint(point_i + 1), previous_offset, + offset, scaled_miter_limit, scale); } - case 0: - continue; // This contour has no renderable content. - default: - break; - } - - compute_offset(contour_start_point_i, contour_start_point_i, - contour_end_point_i, contour); - const Point contour_first_offset = offset; - - if (contour_i > 0) { - // This branch only executes when we've just finished drawing a contour - // and are switching to a new one. - // We're drawing a triangle strip, so we need to "pick up the pen" by - // appending two vertices at the end of the previous contour and two - // vertices at the start of the new contour (thus connecting the two - // contours with two zero volume triangles, which will be discarded by - // the rasterizer). - vtx.position = polyline.GetPoint(contour_start_point_i - 1); - // Append two vertices when "picking up" the pen so that the triangle - // drawn when moving to the beginning of the new contour will have zero - // volume. - vtx_builder.AppendVertex(vtx); - vtx_builder.AppendVertex(vtx); - - vtx.position = polyline.GetPoint(contour_start_point_i); - // Append two vertices at the beginning of the new contour, which - // appends two triangles of zero area. - vtx_builder.AppendVertex(vtx); - vtx_builder.AppendVertex(vtx); - } - - // Generate start cap. - if (!polyline.contours[contour_i].is_closed) { - auto cap_offset = - Vector2(-contour.start_direction.y, contour.start_direction.x) * - stroke_width * 0.5; // Counterclockwise normal - cap_proc(vtx_builder, polyline.GetPoint(contour_start_point_i), - cap_offset, scale, true); - } - - for (size_t contour_component_i = 0; - contour_component_i < contour.components.size(); - contour_component_i++) { - auto component = contour.components[contour_component_i]; - auto is_last_component = - contour_component_i == contour.components.size() - 1; - - auto component_start_index = component.component_start_index; - auto component_end_index = - is_last_component ? contour_end_point_i - 1 - : contour.components[contour_component_i + 1] - .component_start_index; - if (component.is_curve) { - add_vertices_for_curve_component( - component_start_index, component_end_index, contour_start_point_i, - contour_end_point_i, contour); - } else { - add_vertices_for_linear_component( - component_start_index, component_end_index, contour_start_point_i, - contour_end_point_i, contour); - } - } - - // Generate end cap or join. - if (!contour.is_closed) { - auto cap_offset = - Vector2(-contour.end_direction.y, contour.end_direction.x) * - stroke_width * 0.5; // Clockwise normal - cap_proc(vtx_builder, polyline.GetPoint(contour_end_point_i - 1), - cap_offset, scale, false); - } else { - join_proc(vtx_builder, polyline.GetPoint(contour_start_point_i), offset, - contour_first_offset, scaled_miter_limit, scale); } } - return vtx_builder; + void AddVerticesForCurveComponent(VertexWriter& vtx_builder, + const size_t component_start_index, + const size_t component_end_index, + const size_t contour_start_point_i, + const size_t contour_end_point_i, + const Path::PolylineContour& contour) { + bool is_last_component = component_start_index == + contour.components.back().component_start_index; + + for (size_t point_i = component_start_index; point_i < component_end_index; + point_i++) { + bool is_end_of_component = point_i == component_end_index - 1; + + vtx.position = polyline.GetPoint(point_i) + offset; + vtx_builder.AppendVertex(vtx.position); + vtx.position = polyline.GetPoint(point_i) - offset; + vtx_builder.AppendVertex(vtx.position); + + previous_offset = offset; + offset = ComputeOffset(point_i + 2, contour_start_point_i, + contour_end_point_i, contour); + // For curve components, the polyline is detailed enough such that + // it can avoid worrying about joins altogether. + if (is_end_of_component) { + vtx.position = polyline.GetPoint(point_i + 1) + offset; + vtx_builder.AppendVertex(vtx.position); + vtx.position = polyline.GetPoint(point_i + 1) - offset; + vtx_builder.AppendVertex(vtx.position); + // Generate join from the current line to the next line. + if (!is_last_component) { + join_proc(vtx_builder, polyline.GetPoint(point_i + 1), + previous_offset, offset, scaled_miter_limit, scale); + } + } + } + } + + const Path::Polyline& polyline; + const Scalar stroke_width; + const Scalar scaled_miter_limit; + const StrokePathGeometry::JoinProc& join_proc; + const StrokePathGeometry::CapProc& cap_proc; + const Scalar scale; + + Point previous_offset; + Point offset; + SolidFillVertexShader::PerVertexData vtx; +}; + +// static +void StrokePathGeometry::CreateSolidStrokeVertices( + VertexWriter& vtx_builder, + const Path::Polyline& polyline, + Scalar stroke_width, + Scalar scaled_miter_limit, + const StrokePathGeometry::JoinProc& join_proc, + const CapProc& cap_proc, + Scalar scale) { + StrokeGenerator stroke_generator(polyline, stroke_width, scaled_miter_limit, + join_proc, cap_proc, scale); + stroke_generator.Generate(vtx_builder); } GeometryResult StrokePathGeometry::GetPositionBuffer( @@ -452,14 +515,29 @@ GeometryResult StrokePathGeometry::GetPositionBuffer( Scalar stroke_width = std::max(stroke_width_, min_size); auto& host_buffer = renderer.GetTransientsBuffer(); - auto vertex_builder = CreateSolidStrokeVertices( - path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, - GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), - entity.GetTransform().GetMaxBasisLength()); + auto scale = entity.GetTransform().GetMaxBasisLength(); + + PositionWriter position_writer; + auto polyline = renderer.GetTessellator()->CreateTempPolyline(path_, scale); + CreateSolidStrokeVertices(position_writer, polyline, stroke_width, + miter_limit_ * stroke_width_ * 0.5f, + GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), + scale); + + BufferView buffer_view = + host_buffer.Emplace(position_writer.GetData().data(), + position_writer.GetData().size() * + sizeof(SolidFillVertexShader::PerVertexData), + alignof(SolidFillVertexShader::PerVertexData)); return GeometryResult{ .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), + .vertex_buffer = + { + .vertex_buffer = buffer_view, + .vertex_count = position_writer.GetData().size(), + .index_type = IndexType::kNone, + }, .transform = pass.GetOrthographicTransform() * entity.GetTransform(), .prevent_overdraw = true, }; @@ -483,16 +561,28 @@ GeometryResult StrokePathGeometry::GetPositionUVBuffer( Scalar stroke_width = std::max(stroke_width_, min_size); auto& host_buffer = renderer.GetTransientsBuffer(); - auto stroke_builder = CreateSolidStrokeVertices( - path_, stroke_width, miter_limit_ * stroke_width_ * 0.5, - GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), - entity.GetTransform().GetMaxBasisLength()); - auto vertex_builder = ComputeUVGeometryCPU( - stroke_builder, {0, 0}, texture_coverage.GetSize(), effect_transform); + auto scale = entity.GetTransform().GetMaxBasisLength(); + auto polyline = renderer.GetTessellator()->CreateTempPolyline(path_, scale); + + PositionUVWriter writer(Point{0, 0}, texture_coverage.GetSize(), + effect_transform); + CreateSolidStrokeVertices( + writer, polyline, stroke_width, miter_limit_ * stroke_width_ * 0.5f, + GetJoinProc(stroke_join_), GetCapProc(stroke_cap_), scale); + + BufferView buffer_view = host_buffer.Emplace( + writer.GetData().data(), + writer.GetData().size() * sizeof(TextureFillVertexShader::PerVertexData), + alignof(TextureFillVertexShader::PerVertexData)); return GeometryResult{ .type = PrimitiveType::kTriangleStrip, - .vertex_buffer = vertex_builder.CreateVertexBuffer(host_buffer), + .vertex_buffer = + { + .vertex_buffer = buffer_view, + .vertex_count = writer.GetData().size(), + .index_type = IndexType::kNone, + }, .transform = pass.GetOrthographicTransform() * entity.GetTransform(), .prevent_overdraw = true, }; diff --git a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h index 7b0d044326..886ec1a7dc 100644 --- a/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h +++ b/engine/src/flutter/impeller/entity/geometry/stroke_path_geometry.h @@ -9,6 +9,11 @@ namespace impeller { +class VertexWriter { + public: + virtual void AppendVertex(const Point& point) = 0; +}; + /// @brief A geometry that is created from a stroked path object. class StrokePathGeometry final : public Geometry { public: @@ -28,23 +33,21 @@ class StrokePathGeometry final : public Geometry { Join GetStrokeJoin() const; + using CapProc = std::function; + using JoinProc = std::function; + private: using VS = SolidFillVertexShader; - using CapProc = - std::function& vtx_builder, - const Point& position, - const Point& offset, - Scalar scale, - bool reverse)>; - using JoinProc = - std::function& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset, - Scalar miter_limit, - Scalar scale)>; - // |Geometry| GeometryResult GetPositionBuffer(const ContentContext& renderer, const Entity& entity, @@ -65,23 +68,61 @@ class StrokePathGeometry final : public Geometry { bool SkipRendering() const; - static Scalar CreateBevelAndGetDirection( - VertexBufferBuilder& vtx_builder, - const Point& position, - const Point& start_offset, - const Point& end_offset); + static Scalar CreateBevelAndGetDirection(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset); - static VertexBufferBuilder - CreateSolidStrokeVertices(const Path& path, - Scalar stroke_width, - Scalar scaled_miter_limit, - const JoinProc& join_proc, - const CapProc& cap_proc, - Scalar scale); + static void CreateSolidStrokeVertices(VertexWriter& vtx_builder, + const Path::Polyline& path, + Scalar stroke_width, + Scalar scaled_miter_limit, + const JoinProc& join_proc, + const CapProc& cap_proc, + Scalar scale); static StrokePathGeometry::JoinProc GetJoinProc(Join stroke_join); - static StrokePathGeometry::CapProc GetCapProc(Cap stroke_cap); + static StrokePathGeometry::CapProc GetCapProc(Cap cap); + + static void CreateButtCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse); + + static void CreateRoundCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse); + + static void CreateSquareCap(VertexWriter& vtx_builder, + const Point& position, + const Point& offset, + Scalar scale, + bool reverse); + + static void CreateMiterJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale); + + static void CreateRoundJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale); + + static void CreateBevelJoin(VertexWriter& vtx_builder, + const Point& position, + const Point& start_offset, + const Point& end_offset, + Scalar miter_limit, + Scalar scale); Path path_; Scalar stroke_width_; diff --git a/engine/src/flutter/impeller/geometry/path.h b/engine/src/flutter/impeller/geometry/path.h index ea0133881b..be1bf4730b 100644 --- a/engine/src/flutter/impeller/geometry/path.h +++ b/engine/src/flutter/impeller/geometry/path.h @@ -7,7 +7,6 @@ #include #include -#include #include #include diff --git a/engine/src/flutter/impeller/geometry/path_component.cc b/engine/src/flutter/impeller/geometry/path_component.cc index 96cc8bc78c..55a4565708 100644 --- a/engine/src/flutter/impeller/geometry/path_component.cc +++ b/engine/src/flutter/impeller/geometry/path_component.cc @@ -106,6 +106,14 @@ static Scalar ApproximateParabolaIntegral(Scalar x) { void QuadraticPathComponent::AppendPolylinePoints( Scalar scale_factor, std::vector& points) const { + ToLinearPathComponents(scale_factor, [&points](const Point& point) { + points.emplace_back(point); + }); +} + +void QuadraticPathComponent::ToLinearPathComponents( + Scalar scale_factor, + const PointProc& proc) const { auto tolerance = kDefaultCurveTolerance / scale_factor; auto sqrt_tolerance = sqrt(tolerance); @@ -141,9 +149,9 @@ void QuadraticPathComponent::AppendPolylinePoints( auto u = i * step; auto a = a0 + (a2 - a0) * u; auto t = (ApproximateParabolaIntegral(a) - u0) * uscale; - points.emplace_back(Solve(t)); + proc(Solve(t)); } - points.emplace_back(p2); + proc(p2); } std::vector QuadraticPathComponent::Extrema() const { @@ -188,10 +196,8 @@ Point CubicPathComponent::SolveDerivative(Scalar time) const { void CubicPathComponent::AppendPolylinePoints( Scalar scale, std::vector& points) const { - auto quads = ToQuadraticPathComponents(.1); - for (const auto& quad : quads) { - quad.AppendPolylinePoints(scale, points); - } + ToLinearPathComponents( + scale, [&points](const Point& point) { points.emplace_back(point); }); } inline QuadraticPathComponent CubicPathComponent::Lower() const { @@ -209,9 +215,9 @@ CubicPathComponent CubicPathComponent::Subsegment(Scalar t0, Scalar t1) const { return CubicPathComponent(p0, p1, p2, p3); } -std::vector -CubicPathComponent::ToQuadraticPathComponents(Scalar accuracy) const { - std::vector quads; +void CubicPathComponent::ToLinearPathComponents(Scalar scale, + const PointProc& proc) const { + constexpr Scalar accuracy = 0.1; // The maximum error, as a vector from the cubic to the best approximating // quadratic, is proportional to the third derivative, which is constant // across the segment. Thus, the error scales down as the third power of @@ -229,17 +235,15 @@ CubicPathComponent::ToQuadraticPathComponents(Scalar accuracy) const { auto p = p2x2 - p1x2; auto err = p.Dot(p); auto quad_count = std::max(1., ceil(pow(err / max_hypot2, 1. / 6.0))); - quads.reserve(quad_count); for (size_t i = 0; i < quad_count; i++) { auto t0 = i / quad_count; auto t1 = (i + 1) / quad_count; auto seg = Subsegment(t0, t1); auto p1x2 = 3.0 * seg.cp1 - seg.p1; auto p2x2 = 3.0 * seg.cp2 - seg.p2; - quads.emplace_back( - QuadraticPathComponent(seg.p1, ((p1x2 + p2x2) / 4.0), seg.p2)); + QuadraticPathComponent(seg.p1, ((p1x2 + p2x2) / 4.0), seg.p2) + .ToLinearPathComponents(scale, proc); } - return quads; } static inline bool NearEqual(Scalar a, Scalar b, Scalar epsilon) { diff --git a/engine/src/flutter/impeller/geometry/path_component.h b/engine/src/flutter/impeller/geometry/path_component.h index 8bf1018925..58aa1f9f83 100644 --- a/engine/src/flutter/impeller/geometry/path_component.h +++ b/engine/src/flutter/impeller/geometry/path_component.h @@ -5,6 +5,7 @@ #ifndef FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_ #define FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_ +#include #include #include #include @@ -79,6 +80,10 @@ struct QuadraticPathComponent { void AppendPolylinePoints(Scalar scale_factor, std::vector& points) const; + using PointProc = std::function; + + void ToLinearPathComponents(Scalar scale_factor, const PointProc& proc) const; + std::vector Extrema() const; bool operator==(const QuadraticPathComponent& other) const { @@ -125,8 +130,9 @@ struct CubicPathComponent { std::vector Extrema() const; - std::vector ToQuadraticPathComponents( - Scalar accuracy) const; + using PointProc = std::function; + + void ToLinearPathComponents(Scalar scale, const PointProc& proc) const; CubicPathComponent Subsegment(Scalar t0, Scalar t1) const; diff --git a/engine/src/flutter/impeller/tessellator/tessellator.cc b/engine/src/flutter/impeller/tessellator/tessellator.cc index 952569f85a..dcc516758e 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator.cc @@ -170,10 +170,22 @@ Tessellator::Result Tessellator::Tessellate(const Path& path, return Result::kSuccess; } +Path::Polyline Tessellator::CreateTempPolyline(const Path& path, + Scalar tolerance) { + FML_DCHECK(point_buffer_); + point_buffer_->clear(); + auto polyline = + path.CreatePolyline(tolerance, std::move(point_buffer_), + [this](Path::Polyline::PointBufferPtr point_buffer) { + point_buffer_ = std::move(point_buffer); + }); + return polyline; +} + std::vector Tessellator::TessellateConvex(const Path& path, Scalar tolerance) { + FML_DCHECK(point_buffer_); std::vector output; - point_buffer_->clear(); auto polyline = path.CreatePolyline(tolerance, std::move(point_buffer_), diff --git a/engine/src/flutter/impeller/tessellator/tessellator.h b/engine/src/flutter/impeller/tessellator/tessellator.h index c84cc9ca5a..6251e814aa 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.h +++ b/engine/src/flutter/impeller/tessellator/tessellator.h @@ -9,7 +9,6 @@ #include #include -#include "flutter/fml/macros.h" #include "impeller/core/formats.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" @@ -213,6 +212,14 @@ class Tessellator { /// std::vector TessellateConvex(const Path& path, Scalar tolerance); + //---------------------------------------------------------------------------- + /// @brief Create a temporary polyline. Only one per-process can exist at + /// a time. + /// + /// The tessellator itself is not a thread safe class and should + /// only be used from the raster thread. + Path::Polyline CreateTempPolyline(const Path& path, Scalar tolerance); + /// @brief The pixel tolerance used by the algorighm to determine how /// many divisions to create for a circle. /// diff --git a/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc b/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc index ec6afc069e..0a2ceae2cb 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator_unittests.cc @@ -484,5 +484,18 @@ TEST(TessellatorTest, FilledRoundRectTessellationVertices) { Rect::MakeXYWH(5000, 10000, 2000, 3000), {50, 70}); } +#if !NDEBUG +TEST(TessellatorTest, ChecksConcurrentPolylineUsage) { + auto tessellator = std::make_shared(); + PathBuilder builder; + builder.AddLine({0, 0}, {100, 100}); + auto path = builder.TakePath(); + + auto polyline = tessellator->CreateTempPolyline(path, 0.1); + EXPECT_DEBUG_DEATH(tessellator->CreateTempPolyline(path, 0.1), + "point_buffer_"); +} +#endif // NDEBUG + } // namespace testing } // namespace impeller