Skip to content

Support SkMesh API - #1278

Draft
Ivan Matkov (MatkovIvan) wants to merge 2 commits into
masterfrom
ivan.matkov/skiko-1154-expose-skmesh
Draft

Ivan Matkov (MatkovIvan) wants to merge 2 commits into
masterfrom
ivan.matkov/skiko-1154-expose-skmesh

Conversation

@MatkovIvan

Copy link
Copy Markdown
Contributor

SKIKO-1154 Expose SkMesh

This adds the mesh API as one Kotlin type per Skia type:

  • MeshSpecification compiles the two SkSL programs and reports Skia's own message as an IllegalArgumentException when they are rejected. It also reports what Skia compiled: the attributes, the uniforms with the offset and size of each value, the children, the varyings and the color space.
  • MeshVertexBuffer and MeshIndexBuffer wrap the CPU-backed buffers from SkMeshes. They are separate objects, as in Skia, so one buffer can be shared between meshes. The GPU-backed buffers are Ganesh-only with no graphite equivalent, so they are left out rather than exposed on some backends.
  • Mesh covers the indexed and the non-indexed form and reports what it was made from. It is immutable, as SkMesh is.
  • MeshUniformBuilder packs the single Data that SkMesh takes, as RuntimeShaderBuilder does for runtime effect uniforms. It is a convenience over MeshSpecification.uniforms, not the only way to reach it.
  • Canvas.drawMesh takes either a Blender or a BlendMode.

RuntimeEffect.Child is a new sealed interface implemented by Shader, ColorFilter and Blender. It names the set of values SkRuntimeEffect::ChildPtr holds, so a mesh binds any of the three kinds of child its programs declare and the call site is checked at compile time. It sits on RuntimeEffect because that is where Skia declares it. It declares no members, so nothing changes for existing callers of those three classes.

This comment was marked as resolved.

@MatkovIvan
Ivan Matkov (MatkovIvan) force-pushed the ivan.matkov/skiko-1154-expose-skmesh branch from 9b1cbce to 296eaab Compare August 25, 2026 14:39
An SkSL program declares its children as `uniform shader`,
`uniform colorFilter` or `uniform blender`, and Skia passes values for them
as SkRuntimeEffect::ChildPtr, which holds exactly one of those three.
Kotlin had no name for that set, so an API taking a list of children had to
either narrow it to one kind or take an untyped pointer.

RuntimeEffect.Child is a sealed interface implemented by Shader, ColorFilter
and Blender. It closes the set to the three types Skia accepts, so a call
site binding children is checked at compile time and the binding can map
each value onto the right SkFlattenable subclass. It sits on RuntimeEffect
because that is where Skia declares it; the name drops the Ptr suffix, which
says nothing in Kotlin about what is a shader, a color filter or a blender.

The interface declares no members, so nothing changes for existing callers.
@MatkovIvan
Ivan Matkov (MatkovIvan) force-pushed the ivan.matkov/skiko-1154-expose-skmesh branch from 296eaab to 9b82782 Compare November 24, 2026 09:52
Compose draws custom meshes on Android through android.graphics.Mesh and
has no equivalent elsewhere: skiko exposed SkVertices but none of Skia's
mesh API, so a mesh whose vertices and colors are computed by SkSL could
not be built or drawn at all.

This adds the whole path:

- MeshSpecification takes the vertex attributes, the varyings and the two
  SkSL programs, and reports Skia's own message as IllegalArgumentException
  when it rejects them. A null color space means sRGB, which is what Skia's
  shorter Make overloads supply.
- MeshVertexBuffer and MeshIndexBuffer wrap the CPU-backed buffers from
  SkMeshes. The GPU-backed ones are Ganesh-only and have no graphite
  equivalent, so they are left out rather than exposed on some backends.
- Mesh covers both the indexed and the non-indexed form, plus the shader,
  color filter and blender children the programs may declare.
- MeshUniformBuilder resolves uniform offsets through the specification and
  packs the single Data that SkMesh takes, mirroring how
  RuntimeShaderBuilder handles runtime effect uniforms. Values reach it
  through two entry points taking an int and a float array: mesh uniforms
  are packed once per mesh rather than once per draw, so the array costs
  nothing that matters and halves the C++ that has to get the bounds right.
- Canvas.drawMesh takes either a Blender or a BlendMode, the latter
  building the blender natively to keep it off the per-frame path.

The specification also reports what Skia compiled out of the two programs:
its attributes, its uniforms with the offset and size of each value, its
children with the kind each one accepts, and the varyings by name, which
covers the position varying Skia adds when the caller does not supply one.
Packing uniform data by hand is what Skia's own API expects, so
MeshUniformBuilder is the convenience over that reflection rather than the
only way to reach it. The color space is reported too; Skia drops it when
the fragment program returns no color, so it is nullable.

A Mesh reports the specification, buffers, offsets, counts, uniform data,
mode and bounds it was made from. Its children are not reported back: doing
so would mean re-wrapping each SkMesh::ChildPtr by inspecting its type, and
nothing needs it yet.

Absent uniforms are handed to Skia as empty data rather than as null:
SkMesh::Make reads the uniform data while validating, so a null there
crashes the process whenever the specification declares a uniform. Empty
data turns the same mistake into a rejected mesh carrying the size Skia
expected.

Children cross the binding as parallel arrays of a pointer and the kind it
must be upcast from, because SkShader, SkColorFilter and SkBlender reach
SkFlattenable at different offsets and the pointer alone cannot say which
one it is. The kind is an SkRuntimeEffect::ChildType on both sides, so the
values the two ends agree on are Skia's own rather than a third set.

The documentation is Skia's own, carried over from SkMesh.h, SkCanvas.h and
SkRuntimeEffect.h with the type names mapped, as the existing bindings do.
Only what Skia leaves undocumented is written here: the accessors on Mesh,
the topology modes, and MeshUniformBuilder, which has no Skia counterpart.

The external functions are prefixed with their class name, as Data and
RuntimeShaderBuilder already do for their finalizers. Kotlin/Native mangles
top-level external functions by signature alone, so an unprefixed _nMake
clashes across files even though each one is private.

Meshes are rasterized by GPU-backed canvases; a raster surface accepts the
draw and produces no pixels for it. The tests that run everywhere therefore
assert that the draw reaches Skia and stays inside the mesh bounds, and the
one that reads back rasterized pixels runs where TestGlContext does, as the
other GPU tests do. The specification reflection, buffers, uniform packing
and rejection paths are covered directly on JVM, Kotlin/Native and wasm.

Fixes [SKIKO-1154](https://youtrack.jetbrains.com/issue/SKIKO-1154)
@MatkovIvan
Ivan Matkov (MatkovIvan) force-pushed the ivan.matkov/skiko-1154-expose-skmesh branch from 9b82782 to fd2089f Compare November 24, 2026 13:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants