WgpuCanvasInterface¶
- class wgpu.gui.WgpuCanvasInterface(*args, **kwargs)¶
Bases:
objectThe 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
GPUCanvasContextobject 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
kindargument 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
methodfield is “screen”, the context will render directly to a surface representing the region on the screen. The dict should have awindowfield containing the window id. On Linux there should also beplatformfield to distinguish between “wayland” and “x11”, and adisplayfield for the display id. This information is used by wgpu to obtain the required surface id.When the
methodfield is “image”, the context will render to a texture, download the result to RAM, and callcanvas.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
formatsandalpha_modesto 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.