Search code examples
c++skia

How to obtain mesh from Skia path geometry?


I'm trying to figure out how to make Skia produce mesh from path geometry. I've checked SkPath::dump, SkPath::writeToMemory, SkPath::serialize, however all of them seem to output path content rather than mesh.

Note: by mesh i mean triangle mesh produced by tessellation of path shape that can be later used to be rendered with non-vector graphical APIs, such as OpenGL / Vulkan. Something similar to output of ID2D1Geometry::Tessellate.


Solution

  • I have extracted this part of the code, exposed the triangulation interface, and then used OpenGL rendering in libpag.

    // SkPath.h
    int toAATriangles(float tolerance, const SkRect& clipBounds, std::vector<float>* vertex) const;
    
    // SkPath.cpp
    int SkPath::toAATriangles(float tolerance,
                              const SkRect& clipBounds,
                              std::vector<float>* vertex) const {
      return GrAATriangulator::PathToAATriangles(*this, tolerance, clipBounds, vertex);
    }
    

    https://github.com/libpag/pathkit/blob/5bf0bc2acf8dbd74efb5713627de144a8f1ef2b1/include/core/SkPath.h#L898 https://github.com/libpag/pathkit/blob/5bf0bc2acf8dbd74efb5713627de144a8f1ef2b1/src/core/SkPath.cpp#L2453