Add Canvas::set_clip_path — anti-aliased arbitrary-path clipping on both backends
Add `Canvas::set_clip_path(&[PathCmd])`, clipping subsequent draws to an arbitrary vector path with an anti-aliased edge, on both the software and GLES backends. It complements the existing rect clip (`set_clip_rects`) and is what an embedder needs to render a shaped clip — a circular avatar, a rounded card, a `VectorDrawable` mask — rather than a bounding box. Kept general rather than tied to any one consumer. Software backend: rasterise the path into an anti-aliased tiny-skia coverage `Mask` (Winding fill) and install it as the active clip mask. Every software primitive already threads `clip_mask` through tiny-skia (fills, strokes, lines, paths, images, text, blit), so the path clip applies uniformly with smooth edges. `clip_bounds` reports the path's bounding box while it is active. GLES backend: a 1-bit stencil would clip exactly but leave a hard, aliased edge, so instead the clipped draws are captured into an offscreen layer and composited back through an anti-aliased coverage mask. `set_clip_path` rasterises the path coverage (tiny-skia, anti-aliased), uploads it as a mask texture, allocates a full-canvas layer FBO on first use, and redirects subsequent draws to it via `activate_target`. Ending the clip (`clear_clip` / `set_clip_rects` / a new `set_clip_path`) composites the layer back onto the canvas FBO with a new two-sampler program (`CLIP_COMPOSITE_FRAG_SRC`) that multiplies the layer colour by the mask coverage and blends it premultiplied-over. The layer attaches to the canvas's own shadow FBO, so it needs no stencil bits in the EGL config; it is freed and reallocated on resize and freed on drop, and shared programs/uniforms are copied to sub-canvases like the rest. Usage: a path clip is bracketed — `set_clip_path` then, after the clipped draws, `clear_clip` or `set_clip_rects` to flush it (on GLES this is when the layer is composited). Snapshot the prior clip with `clip_bounds` beforehand and restore it with `set_clip_rects` to compose with an outer clip without leaking state. Add an `examples/clip_path.rs` demo (rounded rect, circle, triangle — same smooth result on both backends) and software-backend unit tests covering the bounding box, the empty-path clear, and a pixel-level check that a triangular clip masks a fill to the path silhouette rather than its bounding box. The GLES layer-composite path needs a live GL context and is exercised by the example. Also fix three rustdoc intra-doc-link warnings surfaced along the way: a private-item link in `app.rs` (`scroll`) and the new GLES doc (`SoftwareCanvas::set_clip_path`) demoted to code spans, and a redundant explicit link target in `chassis.rs`.
This commit is contained in:
@@ -83,22 +83,32 @@ impl GlesCanvas
|
||||
/// disable scissor when the canvas has no clip).
|
||||
pub( super ) fn activate_target( &self )
|
||||
{
|
||||
// SAFETY: rebinds canvas-owned FBO + viewport + scissor. All values
|
||||
// (`self.fbo`, `self.width`, `self.height`, `self.clip_scissor`)
|
||||
// While a path clip is active, draws are redirected to the offscreen clip
|
||||
// layer (unclipped); the anti-aliased mask is applied when the layer is
|
||||
// composited back on clip end. Otherwise draws go to `fbo` with the
|
||||
// active scissor.
|
||||
// SAFETY: rebinds a canvas-owned FBO + viewport + scissor. All values
|
||||
// (`self.fbo`, `self.clip_layer`, `self.width/height`, `self.clip_scissor`)
|
||||
// live as long as `&self`, and the bind is idempotent.
|
||||
let target = match ( self.clip_layer_active, self.clip_layer )
|
||||
{
|
||||
( true, Some( ( fbo, _ ) ) ) => fbo,
|
||||
_ => self.fbo,
|
||||
};
|
||||
let to_layer = self.clip_layer_active && self.clip_layer.is_some();
|
||||
unsafe
|
||||
{
|
||||
self.gl.bind_framebuffer( glow::FRAMEBUFFER, Some( self.fbo ) );
|
||||
self.gl.bind_framebuffer( glow::FRAMEBUFFER, Some( target ) );
|
||||
self.gl.viewport( 0, 0, self.width as i32, self.height as i32 );
|
||||
match self.clip_scissor
|
||||
{
|
||||
Some( r ) =>
|
||||
Some( r ) if !to_layer =>
|
||||
{
|
||||
let ( x, y, w, h ) = self.scissor_pixels( r );
|
||||
self.gl.enable( glow::SCISSOR_TEST );
|
||||
self.gl.scissor( x, y, w, h );
|
||||
}
|
||||
None =>
|
||||
_ =>
|
||||
{
|
||||
self.gl.disable( glow::SCISSOR_TEST );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user