Support subcontours in strokes, lay groundwork for fills (flutter/engine#31)

This commit is contained in:
Dan Field
2022-02-24 16:51:40 -08:00
committed by Dan Field
parent 3c58878bbc
commit 9ab707bf74
12 changed files with 183 additions and 36 deletions

View File

@@ -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);

View File

@@ -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<VS::PerVertexData> 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);

View File

@@ -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<SolidStrokeContents>();
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<SolidStrokeContents>();
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) {

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -42,10 +42,16 @@ Path& Path::AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2) {
return *this;
}
void Path::EnumerateComponents(
Applier<LinearPathComponent> linear_applier,
Applier<QuadraticPathComponent> quad_applier,
Applier<CubicPathComponent> 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<LinearPathComponent> linear_applier,
Applier<QuadraticPathComponent> quad_applier,
Applier<CubicPathComponent> cubic_applier,
Applier<MovePathComponent> 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<Point> 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<Point> points;
auto collect_points = [&points](const std::vector<Point>& collection) {
points.reserve(points.size() + collection.size());
points.insert(points.end(), collection.begin(), collection.end());
Polyline polyline;
auto collect_points = [&polyline](const std::vector<Point>& 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<Point> 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<Rect> Path::GetBoundingBox() const {

View File

@@ -6,6 +6,7 @@
#include <functional>
#include <optional>
#include <set>
#include <vector>
#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<Point> points;
/// Indices of points that end a subcontour.
std::set<size_t> breaks;
};
Path();
@@ -54,11 +68,14 @@ class Path {
Path& AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2);
Path& AddMoveComponent(Point destination);
template <class T>
using Applier = std::function<void(size_t index, const T& component)>;
void EnumerateComponents(Applier<LinearPathComponent> linearApplier,
Applier<QuadraticPathComponent> quadApplier,
Applier<CubicPathComponent> cubicApplier) const;
Applier<CubicPathComponent> cubicApplier,
Applier<MovePathComponent> 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<Point> CreatePolyline(
bool UpdateMoveComponentAtIndex(size_t index, const MovePathComponent& move);
Polyline CreatePolyline(
const SmoothingApproximation& approximation = {}) const;
std::optional<Rect> GetBoundingBox() const;
@@ -99,6 +120,7 @@ class Path {
std::vector<LinearPathComponent> linears_;
std::vector<QuadraticPathComponent> quads_;
std::vector<CubicPathComponent> cubics_;
std::vector<MovePathComponent> moves_;
};
} // namespace impeller

View File

@@ -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;
}

View File

@@ -6,6 +6,7 @@
#include <vector>
#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

View File

@@ -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<Point>& 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<Point>& 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() //
);
//----------------------------------------------------------------------------

View File

@@ -38,7 +38,7 @@ class Tessellator {
///
/// @return If tessellation was successful.
///
bool Tessellate(const std::vector<Point>& polyline,
bool Tessellate(const Path::Polyline& polyline,
VertexCallback callback) const;
private: