WgpuCanvasInterface

class wgpu.gui.WgpuCanvasInterface(*args, **kwargs)

Bases: object

The minimal interface to be a valid canvas.

Any object that implements these methods is a canvas that wgpu can work with. The object does not even have to derive from this class.

In most cases it’s more convenient to subclass WgpuCanvasBase.

get_context(kind='webgpu')

Get the GPUCanvasContext object corresponding to this canvas.

The context is used to obtain a texture to render to, and to present that texture to the canvas. This class provides a default implementation to get the appropriate context.

The kind argument is a remnant from the WebGPU spec and must always be “webgpu”.

get_physical_size()

Get the physical size of the canvas in integer pixels.

get_present_info()

Get information about the surface to render to.

It must return a small dict, used by the canvas-context to determine how the rendered result should be presented to the canvas. There are two possible methods.

If the method field is “screen”, the context will render directly to a surface representing the region on the screen. The dict should have a window field containing the window id. On Linux there should also be platform field to distinguish between “wayland” and “x11”, and a display field for the display id. This information is used by wgpu to obtain the required surface id.

When the method field is “image”, the context will render to a texture, download the result to RAM, and call canvas.present_image() with the image data. Additional info (like format) is passed as kwargs. This method enables various types of canvases (including remote ones), but note that it has a performance penalty compared to rendering directly to the screen.

The dict can further contain fields formats and alpha_modes to define the canvas capabilities. For the “image” method, the default formats is ["rgba8unorm-srgb", "rgba8unorm"], and the default alpha_modes is ["opaque"].

present_image(image, **kwargs)

Consume the final rendered image.

This is called when using the “image” method, see get_present_info(). Canvases that don’t support offscreen rendering don’t need to implement this method.