diff --git a/engine/src/flutter/impeller/BUILD.gn b/engine/src/flutter/impeller/BUILD.gn index d3b6bb6de7..aa258109d9 100644 --- a/engine/src/flutter/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/BUILD.gn @@ -21,6 +21,7 @@ group("impeller") { "geometry", "image", "renderer", + "tessellator", "typographer", ] diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index 14ba73123b..55b9416c86 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -7,7 +7,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity.h" #include "impeller/renderer/render_pass.h" -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc index ef61a1adab..71318f8cb6 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc @@ -8,7 +8,7 @@ #include "impeller/entity/entity.h" #include "impeller/geometry/path.h" #include "impeller/renderer/render_pass.h" -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index 8e8d7fa5c7..149f142e93 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -9,7 +9,7 @@ #include "impeller/geometry/path_builder.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" #include "impeller/typographer/glyph_atlas.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/contents/texture_contents.cc b/engine/src/flutter/impeller/entity/contents/texture_contents.cc index 9717df6bfc..7e57278afb 100644 --- a/engine/src/flutter/impeller/entity/contents/texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/texture_contents.cc @@ -10,7 +10,7 @@ #include "impeller/entity/texture_fill.vert.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/sampler_library.h" -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" namespace impeller { diff --git a/engine/src/flutter/impeller/renderer/BUILD.gn b/engine/src/flutter/impeller/renderer/BUILD.gn index 8e208638f2..540e1e45eb 100644 --- a/engine/src/flutter/impeller/renderer/BUILD.gn +++ b/engine/src/flutter/impeller/renderer/BUILD.gn @@ -91,8 +91,6 @@ impeller_component("renderer") { "shader_types.h", "surface.h", "surface.cc", - "tessellator.cc", - "tessellator.h", "texture.h", "texture.cc", "texture_descriptor.h", @@ -109,10 +107,9 @@ impeller_component("renderer") { "../base", "../geometry", "../image", + "../tessellator", ] - deps = [ "//third_party/libtess2" ] - frameworks = [ "Metal.framework" ] } diff --git a/engine/src/flutter/impeller/renderer/command.h b/engine/src/flutter/impeller/renderer/command.h index 0e4b1c99ef..3fdb0ee171 100644 --- a/engine/src/flutter/impeller/renderer/command.h +++ b/engine/src/flutter/impeller/renderer/command.h @@ -20,6 +20,7 @@ #include "impeller/renderer/texture.h" #include "impeller/renderer/vertex_buffer.h" #include "impeller/renderer/vertex_buffer_builder.h" +#include "impeller/tessellator/tessellator.h" namespace impeller { diff --git a/engine/src/flutter/impeller/renderer/formats.h b/engine/src/flutter/impeller/renderer/formats.h index dedc080a80..f1083e3e7c 100644 --- a/engine/src/flutter/impeller/renderer/formats.h +++ b/engine/src/flutter/impeller/renderer/formats.h @@ -131,11 +131,6 @@ enum class TextureUsage : TextureUsageMask { kRenderTarget = 1 << 2, }; -enum class WindingOrder { - kClockwise, - kCounterClockwise, -}; - enum class CullMode { kNone, kFrontFace, diff --git a/engine/src/flutter/impeller/renderer/renderer_unittests.cc b/engine/src/flutter/impeller/renderer/renderer_unittests.cc index ca08dab43b..01cc768328 100644 --- a/engine/src/flutter/impeller/renderer/renderer_unittests.cc +++ b/engine/src/flutter/impeller/renderer/renderer_unittests.cc @@ -21,7 +21,7 @@ #include "impeller/renderer/sampler_descriptor.h" #include "impeller/renderer/sampler_library.h" #include "impeller/renderer/surface.h" -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" #include "impeller/renderer/vertex_buffer_builder.h" namespace impeller { diff --git a/engine/src/flutter/impeller/renderer/tessellator.cc b/engine/src/flutter/impeller/renderer/tessellator.cc index 44f18306e0..eb8bb3ec43 100644 --- a/engine/src/flutter/impeller/renderer/tessellator.cc +++ b/engine/src/flutter/impeller/renderer/tessellator.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "impeller/renderer/tessellator.h" +#include "impeller/tessellator/tessellator.h" #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" diff --git a/engine/src/flutter/impeller/tessellator/BUILD.gn b/engine/src/flutter/impeller/tessellator/BUILD.gn new file mode 100644 index 0000000000..cde82096ff --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/BUILD.gn @@ -0,0 +1,33 @@ +# Copyright 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//flutter/impeller/tools/impeller.gni") + +impeller_component("tessellator") { + sources = [ + "tessellator.cc", + "tessellator.h", + ] + + public_deps = [ "../geometry" ] + + deps = [ "//third_party/libtess2" ] +} + +shared_library("tessellator_shared") { + output_name = "tessellator" + + sources = [ + "c/tessellator.h", + "c/tessellator.cc", + "tessellator.cc", + "tessellator.h", + ] + + deps = [ + "../geometry", + "//flutter/fml", + "//third_party/libtess2", + ] +} diff --git a/engine/src/flutter/impeller/tessellator/c/tessellator.cc b/engine/src/flutter/impeller/tessellator/c/tessellator.cc new file mode 100644 index 0000000000..119835dd3f --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/c/tessellator.cc @@ -0,0 +1,68 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "tessellator.h" + +#include + +namespace impeller { +PathBuilder* CreatePathBuilder() { + return new PathBuilder(); +} + +void DestroyPathBuilder(PathBuilder* builder) { + delete builder; +} + +void MoveTo(PathBuilder* builder, Scalar x, Scalar y) { + builder->MoveTo(Point(x, y)); +} + +void LineTo(PathBuilder* builder, Scalar x, Scalar y) { + builder->LineTo(Point(x, y)); +} + +void CubicTo(PathBuilder* builder, + Scalar x1, + Scalar y1, + Scalar x2, + Scalar y2, + Scalar x3, + Scalar y3) { + builder->CubicCurveTo(Point(x1, y1), Point(x2, y2), Point(x3, y3)); +} + +void Close(PathBuilder* builder) { + builder->Close(); +} + +struct Vertices* Tessellate(PathBuilder* builder) { + auto path = builder->CopyPath(); + auto polyline = path.CreatePolyline(); + + std::vector points; + if (!Tessellator{path.GetFillType()}.Tessellate(polyline, + [&points](Point vertex) { + points.push_back(vertex.x); + points.push_back(vertex.y); + })) { + return nullptr; + } + + Vertices* vertices = new Vertices(); + vertices->points = new float[points.size()]; + if (!vertices->points) { + return nullptr; + } + vertices->length = points.size(); + std::copy(points.begin(), points.end(), vertices->points); + return vertices; +} + +void DestroyVertices(Vertices* vertices) { + delete vertices->points; + delete vertices; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/tessellator/c/tessellator.h b/engine/src/flutter/impeller/tessellator/c/tessellator.h new file mode 100644 index 0000000000..1ca6efd880 --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/c/tessellator.h @@ -0,0 +1,44 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include "impeller/geometry/path_builder.h" +#include "impeller/tessellator/tessellator.h" + +#define IMPELLER_API __attribute__((visibility("default"))) + +extern "C" { + +namespace impeller { + +struct IMPELLER_API Vertices { + float* points; + uint32_t length; +}; + +IMPELLER_API PathBuilder* CreatePathBuilder(); + +IMPELLER_API void DestroyPathBuilder(PathBuilder* builder); + +IMPELLER_API void MoveTo(PathBuilder* builder, Scalar x, Scalar y); + +IMPELLER_API void LineTo(PathBuilder* builder, Scalar x, Scalar y); + +IMPELLER_API void CubicTo(PathBuilder* builder, + Scalar x1, + Scalar y1, + Scalar x2, + Scalar y2, + Scalar x3, + Scalar y3); + +IMPELLER_API void Close(PathBuilder* builder); + +IMPELLER_API struct Vertices* Tessellate(PathBuilder* builder); + +IMPELLER_API void DestroyVertices(Vertices* vertices); + +} // namespace impeller +} diff --git a/engine/src/flutter/impeller/tessellator/dart/lib/tessellator.dart b/engine/src/flutter/impeller/tessellator/dart/lib/tessellator.dart new file mode 100644 index 0000000000..2cc9358ebb --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/dart/lib/tessellator.dart @@ -0,0 +1,155 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: camel_case_types +import 'dart:ffi' as ffi; +import 'dart:io'; +import 'dart:typed_data'; + +/// Creates vertices from path commands. +/// +/// First, build up the path contours with the [moveTo], [lineTo], [cubicTo], +/// and [close] methods. All methods expect absolute coordinates. +/// +/// Then, use the [tessellate] method to create a [Float32List] of vertex pairs. +/// +/// Finally, use the [dispose] method to clean up native resources. After +/// [dispose] has been called, this class must not be used again. +class VerticesBuilder { + VerticesBuilder() : _builder = _createPathFn(); + + ffi.Pointer<_PathBuilder>? _builder; + final List> _vertices = >[]; + + /// Adds a move verb to the absolute coordinates x,y. + void moveTo(double x, double y) { + assert(_builder != null); + _moveToFn(_builder!, x, y); + } + + /// Adds a line verb to the absolute coordinates x,y. + void lineTo(double x, double y) { + assert(_builder != null); + _lineToFn(_builder!, x, y); + } + + /// Adds a cubic Bezier curve with x1,y1 as the first control point, x2,y2 as + /// the second control point, and end point x3,y3. + void cubicTo( + double x1, + double y1, + double x2, + double y2, + double x3, + double y3, + ) { + assert(_builder != null); + _cubicToFn(_builder!, x1, y1, x2, y2, x3, y3); + } + + /// Adds a close command to the start of the current contour. + void close() { + assert(_builder != null); + closeFn(_builder!, true); + } + + /// Tessellates the path created by the previous method calls into a list of + /// vertices. + Float32List tessellate() { + assert(_vertices.isEmpty); + assert(_builder != null); + final ffi.Pointer<_Vertices> vertices = _tessellateFn(_builder!); + _vertices.add(vertices); + return vertices.ref.points.asTypedList(vertices.ref.size); + } + + /// Releases native resources. + /// + /// After calling dispose, this class must not be used again. + void dispose() { + assert(_builder != null); + _vertices.forEach(_destroyVerticesFn); + _destroyFn(_builder!); + _vertices.clear(); + _builder = null; + } +} + +// TODO(dnfield): Figure out where to put this. +// https://github.com/flutter/flutter/issues/99563 +final ffi.DynamicLibrary _dylib = () { + if (Platform.isWindows) { + return ffi.DynamicLibrary.open('tessellator.dll'); + } else if (Platform.isIOS || Platform.isMacOS) { + return ffi.DynamicLibrary.open('libtessellator.dylib'); + } else if (Platform.isAndroid || Platform.isLinux) { + return ffi.DynamicLibrary.open('libtessellator.so'); + } + throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}'); +}(); + +class _Vertices extends ffi.Struct { + external ffi.Pointer points; + + @ffi.Uint32() + external int size; +} + +class _PathBuilder extends ffi.Opaque {} + +typedef _CreatePathBuilderType = ffi.Pointer<_PathBuilder> Function(); +typedef _create_path_builder_type = ffi.Pointer<_PathBuilder> Function(); + +final _CreatePathBuilderType _createPathFn = + _dylib.lookupFunction<_create_path_builder_type, _CreatePathBuilderType>( + 'CreatePathBuilder'); + +typedef _MoveToType = void Function(ffi.Pointer<_PathBuilder>, double, double); +typedef _move_to_type = ffi.Void Function( + ffi.Pointer<_PathBuilder>, ffi.Float, ffi.Float); + +final _MoveToType _moveToFn = + _dylib.lookupFunction<_move_to_type, _MoveToType>('MoveTo'); + +typedef _LineToType = void Function(ffi.Pointer<_PathBuilder>, double, double); +typedef _line_to_type = ffi.Void Function( + ffi.Pointer<_PathBuilder>, ffi.Float, ffi.Float); + +final _LineToType _lineToFn = + _dylib.lookupFunction<_line_to_type, _LineToType>('LineTo'); + +typedef _CubicToType = void Function( + ffi.Pointer<_PathBuilder>, double, double, double, double, double, double); +typedef _cubic_to_type = ffi.Void Function(ffi.Pointer<_PathBuilder>, ffi.Float, + ffi.Float, ffi.Float, ffi.Float, ffi.Float, ffi.Float); + +final _CubicToType _cubicToFn = + _dylib.lookupFunction<_cubic_to_type, _CubicToType>('CubicTo'); + +typedef _CloseType = void Function(ffi.Pointer<_PathBuilder>, bool); +typedef _close_type = ffi.Void Function(ffi.Pointer<_PathBuilder>, ffi.Bool); + +final _CloseType closeFn = + _dylib.lookupFunction<_close_type, _CloseType>('Close'); + +typedef _TessellateType = ffi.Pointer<_Vertices> Function( + ffi.Pointer<_PathBuilder>); +typedef _tessellate_type = ffi.Pointer<_Vertices> Function( + ffi.Pointer<_PathBuilder>); + +final _TessellateType _tessellateFn = + _dylib.lookupFunction<_tessellate_type, _TessellateType>('Tessellate'); + +typedef _DestroyType = void Function(ffi.Pointer<_PathBuilder>); +typedef _destroy_type = ffi.Void Function(ffi.Pointer<_PathBuilder>); + +final _DestroyType _destroyFn = + _dylib.lookupFunction<_destroy_type, _DestroyType>('DestroyPathBuilder'); + +typedef _DestroyVerticesType = void Function(ffi.Pointer<_Vertices>); +typedef _destroy_vertices_type = ffi.Void Function(ffi.Pointer<_Vertices>); + +final _DestroyVerticesType _destroyVerticesFn = + _dylib.lookupFunction<_destroy_vertices_type, _DestroyVerticesType>( + 'DestroyVertices'); diff --git a/engine/src/flutter/impeller/tessellator/dart/pubspec.yaml b/engine/src/flutter/impeller/tessellator/dart/pubspec.yaml new file mode 100644 index 0000000000..4f09e8272d --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/dart/pubspec.yaml @@ -0,0 +1,11 @@ +# Copyright 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +name: tessellator +description: A Dart FFI wrapper for Impeller's tessellator. +version: 0.0.0 +publish_to: none +homepage: https://github.com/flutter/impeller/tree/main/tessellator/dart + +environment: + sdk: '>=2.12.0 <3.0.0' diff --git a/engine/src/flutter/impeller/tessellator/tessellator.cc b/engine/src/flutter/impeller/tessellator/tessellator.cc new file mode 100644 index 0000000000..eb8bb3ec43 --- /dev/null +++ b/engine/src/flutter/impeller/tessellator/tessellator.cc @@ -0,0 +1,121 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/tessellator/tessellator.h" + +#include "flutter/fml/logging.h" +#include "flutter/fml/trace_event.h" +#include "third_party/libtess2/Include/tesselator.h" + +namespace impeller { + +Tessellator::Tessellator(FillType type) : fill_type_(type) {} + +Tessellator::~Tessellator() = default; + +static int ToTessWindingRule(FillType fill_type) { + switch (fill_type) { + case FillType::kOdd: + return TESS_WINDING_ODD; + case FillType::kNonZero: + return TESS_WINDING_NONZERO; + case FillType::kPositive: + return TESS_WINDING_POSITIVE; + case FillType::kNegative: + return TESS_WINDING_NEGATIVE; + case FillType::kAbsGeqTwo: + return TESS_WINDING_ABS_GEQ_TWO; + } + return TESS_WINDING_ODD; +} + +static void DestroyTessellator(TESStesselator* tessellator) { + if (tessellator != nullptr) { + ::tessDeleteTess(tessellator); + } +} + +bool Tessellator::Tessellate(const Path::Polyline& polyline, + VertexCallback callback) const { + TRACE_EVENT0("impeller", "Tessellator::Tessellate"); + if (!callback) { + return false; + } + + using CTessellator = + std::unique_ptr; + + CTessellator tessellator( + ::tessNewTess(nullptr /* the default ::malloc based allocator */), + DestroyTessellator); + + if (!tessellator) { + return false; + } + + constexpr int kVertexSize = 2; + constexpr int kPolygonSize = 3; + + //---------------------------------------------------------------------------- + /// Feed contour information to the tessellator. + /// + static_assert(sizeof(Point) == 2 * sizeof(float)); + for (size_t contour_i = 0; contour_i < polyline.contours.size(); + contour_i++) { + size_t start_point_index, end_point_index; + std::tie(start_point_index, end_point_index) = + polyline.GetContourPointBounds(contour_i); + + ::tessAddContour(tessellator.get(), // the C tessellator + kVertexSize, // + polyline.points.data() + start_point_index, // + sizeof(Point), // + end_point_index - start_point_index // + ); + } + + //---------------------------------------------------------------------------- + /// Let's tessellate. + /// + auto result = ::tessTesselate(tessellator.get(), // tessellator + ToTessWindingRule(fill_type_), // winding + TESS_POLYGONS, // element type + kPolygonSize, // polygon size + kVertexSize, // vertex size + nullptr // normal (null is automatic) + ); + + if (result != 1) { + return false; + } + + // TODO(csg): This copy can be elided entirely for the current use case. + std::vector points; + std::vector indices; + + int vertexItemCount = tessGetVertexCount(tessellator.get()) * kVertexSize; + auto vertices = tessGetVertices(tessellator.get()); + for (int i = 0; i < vertexItemCount; i += 2) { + points.emplace_back(vertices[i], vertices[i + 1]); + } + + int elementItemCount = tessGetElementCount(tessellator.get()) * kPolygonSize; + auto elements = tessGetElements(tessellator.get()); + for (int i = 0; i < elementItemCount; i++) { + indices.emplace_back(elements[i]); + } + + for (auto index : indices) { + auto vtx = points[index]; + callback(vtx); + } + + return true; +} + +WindingOrder Tessellator::GetFrontFaceWinding() const { + return WindingOrder::kClockwise; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/tessellator.h b/engine/src/flutter/impeller/tessellator/tessellator.h similarity index 95% rename from engine/src/flutter/impeller/renderer/tessellator.h rename to engine/src/flutter/impeller/tessellator/tessellator.h index db8a1f2910..d60e078315 100644 --- a/engine/src/flutter/impeller/renderer/tessellator.h +++ b/engine/src/flutter/impeller/tessellator/tessellator.h @@ -10,10 +10,14 @@ #include "flutter/fml/macros.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" -#include "impeller/renderer/formats.h" namespace impeller { +enum class WindingOrder { + kClockwise, + kCounterClockwise, +}; + //------------------------------------------------------------------------------ /// @brief A utility that generates triangles of the specified fill type /// given a polyline. This happens on the CPU.