Skip to content

feat: parse masks and clip-path in mason-core - #256

Open
triniwiz wants to merge 22 commits into
mainfrom
feat/rust-mask-clip-path
Open

triniwiz wants to merge 22 commits into
mainfrom
feat/rust-mask-clip-path

Conversation

@triniwiz

@triniwiz triniwiz commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Builds on #246. Until that merges, this diff includes it; the commits from feat(core): parse mask layers and clip-path in mason-core onward are this PR.

mask-* and clip-path are now parsed once, in mason-core, on every platform. Each platform keeps its drawing code and its pseudo-state handling. When a resolved value changes, the platform calls the parser and decodes typed results.

Core

  • Mask layers use the background buffer layout, with MASK_MODE and COMPOSITE added to each layer and NO_CLIP added to the box codes.
  • clip-path has its own buffer (clip-path-buffer.schema): shape, reference box, fill rule, lengths with their units, polygon points, and path() as absolute commands (arcs become cubics).
  • Both schemas are generated for Rust, Kotlin, Swift and C++ by the existing style-keys generator.
  • C API: mason_css_parse_mask, mason_css_parse_clip_path. JNI: Style.nativeParseMask, Style.nativeParseClipPath. Unset longhands cross as null.

Platforms

  • Android and iOS: the Kotlin and Swift mask and clip-path parsers are removed. iOS decodes the buffers in place and borrows C strings rather than copying them.
  • Windows and Linux: Css.SetMask takes the mask longhands and clip-path as written. The TS mask and clip-path encoders and the path command encoder are removed. So are the unused SetBackgroundLayers method and the JSON layer decoding.
  • mask-border and border-image are unchanged.

Behaviour

  • An invalid mask-image list means no mask; an invalid longhand list falls back to its initial value. Previously each platform did something different.
  • closest-corner and farthest-corner radii in circle() and ellipse() (CSS Shapes 2) now work everywhere. Previously only Android supported them.
  • End of input closes an unclosed function or string, as in CSS syntax: circle( is circle().

Performance

Release builds, microseconds per call, from the value strings to typed results.

Value Android: Kotlin → Rust iOS: Swift → Rust Windows: TS + JSON → Rust Linux: TS + JSON → Rust
linear-gradient mask 11.7–12.2 → 1.2–1.4 29.7–63 → 1.30 57–63 → 11.9–12.5 5.5–5.6 → 1.1
SVG mask + size, position, repeat 4.7–5.5 → 1.4–1.7 12.6–92 → 1.47–1.73 47–49 → 15.2–16.4 4.1 → 1.4
circle() 3.1 → 0.59–0.70 4.5–40 → 0.36 27–28 → 5.2–5.4 2.2 → 0.48
polygon() 6.8–6.9 → 0.88–1.0 13.0–96 → 0.63–0.65 33–35 → 6.1–6.3 3.8 → 0.68
inset(... round ...) 3.8–3.9 → 0.88–1.04 5.2–41 → 0.66–0.68 37–41 → 5.2–5.8 3.4 → 0.54
  • Android: headless emulator, non-debuggable app, median after JIT warmup.
  • iOS: Release build on the simulator, best of 7 rounds; the Swift parser varied widely between rounds.
  • Windows: TS encode plus Css.SetMask, on a Windows x64 laptop.
  • Linux: TS encode under Node plus the native decode, on a Pop!_OS laptop (Intel), excluding the FFI call, which is the same in both.

Tests

  • mason-core gains tests for the mask, clip-path and path parsers.
  • Android's parse tests are now instrumented, because they reach native code: ClipPathParsingTest, MaskParsingTest, NineSliceTest.
  • iOS ClipPathTests and MaskTests are updated.
  • The Windows mask spec tests are updated.

Parse background, its longhands and <image> lists (linear, radial and
conic gradients, colour hints, interpolation methods, url) once in
mason-core on cssparser and cssparser-color. Longhands are kept as
lists and cycled per layer; invalid values are rejected whole.

Parsed layers are flattened into a background buffer whose layout is
generated from background-buffer.schema for Rust, Kotlin, Swift, C++
and TS. mason-c exposes set, get, buffer and detached image parsing.
Background, its longhands, mask-image and border-image gradients are
parsed in mason-core through new JNI calls; each setter returns the
background buffer, decoded once into BackgroundLayer. Gradients are
typed, so shader rebuilds no longer reparse stop strings.

Colour hints and currentColor stops now render. Longhands set before
the shorthand are kept, as core applies the shorthand last.
background, its longhands, mask-image and border-image sources are
parsed in mason-core through the C API and decoded from the background
buffer into BackgroundLayer, on iOS and macOS. Gradients are typed, so
redraws no longer reparse stop strings.

Colour hints, currentColor stops and every CSS colour syntax now render.
Axis-aligned angles use exact directions on iOS and Android.
A hint at the midpoint is a linear blend and one past either stop is a
hard edge, so neither needs samples. Other hints take samples in
proportion to the segment's drawn length, up to 16.
Linux sends background and its longhands to mason-core through
mason_gtk_css_set_background and paints the parsed layers directly,
resolving gradient geometry against the drawn rect. Colour hints,
currentColor, radial lengths and size keywords now render.

Fix the corner keyword angle in linear_points, which used atan(h/w)
instead of atan(w/h) and skewed to <corner> gradients on non-square
boxes.
Positions, sizes and repeats dispatch on one token instead of trying
alternatives, and keywords and units match without allocating a
lowercase copy. Parsing is 20-50% faster across the benchmark set.
Windows sends background and its longhands to mason-core through
Css.SetBackground and decodes the background buffer into the CssMask
layers it already paints. Colour hints, currentColor stops, radial
lengths and ellipse size keywords, and pixel offsets on gradient
centres now render. The TS background parsing and JSON encoding for
Windows and Linux are removed.

Add setAppFolder for the folder ~/ in image urls names, for hosts that
move it such as the Preview app; Windows and Linux follow core's app
folder by default, Android and iOS take the override natively.
Node.Style made a new Style on every read, so each JS access got a new
COM identity and the runtime built a new wrapper. Reading view.Style
drops from 0.4-1 us to 0.2 us.
Mason.Create* and the child getters return the live wrapper for a node
when there is one, keyed by engine and node id through a weak registry,
so JS gets one object per node and the runtime reuses its wrapper.
mason-c gains mason_node_id for the key.
The app folder comes from setAppFolder through
mason_gtk_css_set_app_folder, and url layers resolve ~/ against it.
Mask longhands encode into the background buffer layout with mode and
composite per layer. clip-path gets its own buffer: shape, reference box,
lengths with units, polygon points and path() as absolute commands.
SetMask takes the mask layer longhands and clip-path as written. The
TS mask and clip-path encoders, the unused background layer encoders
and the path command encoder are removed.
…ayer path

SetBackgroundLayers and the JSON mask layer and clip-path decoding have
no callers since backgrounds, masks and clip-path moved to mason-core.

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.

1 participant