XRender
Optional vx11.render import, linked with libXrender. It binds every public
function, type and constant in Xrender.h and render.h (protocol 0.11), and
adds native helpers built on owned picture and glyph set handles.
import vx11
import vx11.render
mut c := vx11.connect('')!
defer { c.close() }
rr := render.bind(c)!
root := c.root()!
pixmap := vx11.create_pixmap(c.raw()!, vx11.Drawable(root), 200, 100, 32)
mut canvas := rr.create_picture(vx11.Drawable(pixmap), rr.standard_format(render.standard_argb32)!)!
mut sky := rr.linear_gradient(render.Point{0, 0}, render.Point{0, 100}, [
render.ColorStop{0, render.Color{ blue: 0xffff, alpha: 0xffff }},
render.ColorStop{1, render.Color{ alpha: 0xffff }},
])!
canvas.composite(render.op_src, sky, width: 200, height: 100)!
sky.close()
canvas.close()
vx11.free_pixmap(c.raw()!, pixmap)
| Version | Native helpers |
|---|---|
| 0.0 | formats, find_format, standard_format, visual_format, create_picture, window_picture, fill, composite, clipping, glyph sets and glyphs |
| 0.2 | Disjoint and conjoint operators |
| 0.4 | polygon, triangles |
| 0.5 | create_cursor |
| 0.6 | set_transform, set_filter, filters |
| 0.7 | index_values |
| 0.8 | create_anim_cursor |
| 0.10 | solid_fill, linear_gradient, radial_gradient, conical_gradient, pad and reflect repeat |
| 0.11 | Blend mode operators (op_multiply to op_hsl_luminosity) |
bind negotiates the version and loads libXrender's format cache; helpers
check supports(major, minor) and return an error on older servers.
available and dimmed_snapshot work without binding. Raw functions
document the version they require.
Colors and coordinates
Color is XRenderColor. Fill and solid-fill colors are premultiplied by
alpha: build them with premultiplied(r, g, b, a) or parse_color, which
accepts X color names and rgba:r/g/b/a. Gradient stop colors are not
premultiplied. Positions in Point are pixels; helpers convert them to 16.16
Fixed values with to_fixed, which rejects values outside ±32768. Raw calls
take Fixed directly; fixed and fixed_to_double mirror the C macros.
A picture transform maps destination pixels to source pixels, so
scale_transform(0.5, 0.5) enlarges a source twofold. Composite and gradient
sources are sampled in their own coordinates; use src_x and src_y to align
them with the destination.
Ownership and lifetimes
Extension borrows its connection and owns nothing on the server.
PictureHandle and GlyphSetHandle each own one server resource, borrow the
Extension, and close idempotently. Close them before the connection. A
window's pictures are destroyed with the window, so close a window picture
before destroying the window. Pixmap pictures keep their pixmap alive on the
server, but free the pixmap only once you no longer draw through it.
Format, filter and index-value results are V copies; libXrender's format cache
is never freed. create_cursor and create_anim_cursor return cursors that
the caller frees with vx11.free_cursor; animated cursors copy their frames.
dimmed_snapshot returns a pixmap the caller frees with vx11.free_pixmap.
Glyph images are Z-format bitmaps whose rows are padded to 32 bits:
stride(width) gives the bytes per row for the glyph set's format, and add
checks the image size. remove only accepts glyphs added through the same
handle, because freeing a missing glyph is a fatal Match error.
Raw functions keep Xlib's rules. Free query_filters and
query_pict_index_values results with vx11.free; free pictures, glyph sets and
cursors with their matching free_* call. Formats from the find_* functions
are borrowed.
Errors
Helpers reject what the server would answer with a fatal error when local state can tell: operators and repeat modes the version lacks, depth mismatches between a format and its drawable, unknown filter names, filter parameters that the standard filters refuse, malformed convolution kernels, unordered gradient stops, identical linear gradient end points, coordinates outside the protocol's 16-bit fields, cursor hot spots outside their picture, and requests longer than the server accepts. Errors that depend on server resources the helpers cannot see arrive asynchronously, like other Xlib errors. Methods that take an ID expect one that still exists.
polygon lets libXrender tessellate in memory proportional to the square of
the point count, so it accepts at most max_polygon_points points.
libXrender's XRenderReferenceGlyphSet sends its request with the wrong length
(renderproto.h gives sz_xRenderReferenceGlyphSetReq as 24 bytes instead of
12), so Xorg and Xvfb answer with BadLength. The raw reference_glyph_set is
bound for completeness; avoid it, or call it under an error handler.
Tests
v -path '..|@vlib|@vmodules' test render
v -path '..|@vlib|@vmodules' -o /tmp/vx11-render render/tests/integration.v
xvfb-run -a -s '-screen 0 640x480x24' /tmp/vx11-render
xvfb-run -a -s '-screen 0 640x480x24 -extension RENDER' /tmp/vx11-render --unavailable
The unit tests compare every constant with its C macro and probe C long and short field widths. The integration test checks rendered pixels for fills, gradients, transforms, filters, clipping, polygons and glyphs, creates cursors, calls every raw entry point, and checks closed handles. Xvfb has no depth-4 pixmaps, so the A4 standard format is optional there.