Expose C API for tessellator, move to own subdir (flutter/engine#18)
This commit is contained in:
@@ -21,6 +21,7 @@ group("impeller") {
|
||||
"geometry",
|
||||
"image",
|
||||
"renderer",
|
||||
"tessellator",
|
||||
"typographer",
|
||||
]
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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" ]
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -131,11 +131,6 @@ enum class TextureUsage : TextureUsageMask {
|
||||
kRenderTarget = 1 << 2,
|
||||
};
|
||||
|
||||
enum class WindingOrder {
|
||||
kClockwise,
|
||||
kCounterClockwise,
|
||||
};
|
||||
|
||||
enum class CullMode {
|
||||
kNone,
|
||||
kFrontFace,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"
|
||||
|
||||
33
engine/src/flutter/impeller/tessellator/BUILD.gn
Normal file
33
engine/src/flutter/impeller/tessellator/BUILD.gn
Normal file
@@ -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",
|
||||
]
|
||||
}
|
||||
68
engine/src/flutter/impeller/tessellator/c/tessellator.cc
Normal file
68
engine/src/flutter/impeller/tessellator/c/tessellator.cc
Normal file
@@ -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 <vector>
|
||||
|
||||
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<float> 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
|
||||
44
engine/src/flutter/impeller/tessellator/c/tessellator.h
Normal file
44
engine/src/flutter/impeller/tessellator/c/tessellator.h
Normal file
@@ -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
|
||||
}
|
||||
@@ -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<ffi.Pointer<_Vertices>> _vertices = <ffi.Pointer<_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<ffi.Float> 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');
|
||||
11
engine/src/flutter/impeller/tessellator/dart/pubspec.yaml
Normal file
11
engine/src/flutter/impeller/tessellator/dart/pubspec.yaml
Normal file
@@ -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'
|
||||
121
engine/src/flutter/impeller/tessellator/tessellator.cc
Normal file
121
engine/src/flutter/impeller/tessellator/tessellator.cc
Normal file
@@ -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<TESStesselator, decltype(&DestroyTessellator)>;
|
||||
|
||||
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<Point> points;
|
||||
std::vector<uint32_t> 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
|
||||
@@ -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.
|
||||
Reference in New Issue
Block a user