blob: 958659a5094233300866a24fbd4a362ca47871fc [file] [log] [blame]
// 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 <functional>
#include <memory>
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/point.h"
struct TESStesselator;
namespace impeller {
void DestroyTessellator(TESStesselator* tessellator);
using CTessellator =
std::unique_ptr<TESStesselator, decltype(&DestroyTessellator)>;
enum class WindingOrder {
kClockwise,
kCounterClockwise,
};
//------------------------------------------------------------------------------
/// @brief A utility that generates triangles of the specified fill type
/// given a polyline. This happens on the CPU.
///
/// This object is not thread safe, and its methods must not be
/// called from multiple threads.
///
class Tessellator {
public:
enum class Result {
kSuccess,
kInputError,
kTessellationError,
};
Tessellator();
~Tessellator();
/// @brief An arbitrary value to determine when a multi-contour non-zero fill
/// path should be split into multiple tessellations.
static constexpr size_t kMultiContourThreshold = 30u;
/// @brief A callback that returns the results of the tessellation.
///
/// The index buffer may not be populated, in which case [indices] will
/// be nullptr and indices_count will be 0.
using BuilderCallback = std::function<bool(const float* vertices,
size_t vertices_count,
const uint16_t* indices,
size_t indices_count)>;
//----------------------------------------------------------------------------
/// @brief Generates filled triangles from the path. A callback is
/// invoked once for the entire tessellation.
///
/// @param[in] path The path to tessellate.
/// @param[in] tolerance The tolerance value for conversion of the path to
/// a polyline. This value is often derived from the
/// Matrix::GetMaxBasisLength of the CTM applied to the
/// path for rendering.
/// @param[in] callback The callback, return false to indicate failure.
///
/// @return The result status of the tessellation.
///
Tessellator::Result Tessellate(const Path& path,
Scalar tolerance,
const BuilderCallback& callback);
//----------------------------------------------------------------------------
/// @brief Given a convex path, create a triangle fan structure.
///
/// @param[in] path The path to tessellate.
/// @param[in] tolerance The tolerance value for conversion of the path to
/// a polyline. This value is often derived from the
/// Matrix::GetMaxBasisLength of the CTM applied to the
/// path for rendering.
///
/// @return A point vector containing the vertices in triangle strip format.
///
std::vector<Point> TessellateConvex(const Path& path, Scalar tolerance);
private:
/// Used for polyline generation.
std::unique_ptr<std::vector<Point>> point_buffer_;
CTessellator c_tessellator_;
Tessellator(const Tessellator&) = delete;
Tessellator& operator=(const Tessellator&) = delete;
};
} // namespace impeller