Skip to content

Gdk.DmabufTextureBuilder

class — extends GObject.Object

Constructs Texture objects from DMA buffers.

DMA buffers are commonly called dma-bufs.

DMA buffers are a feature of the Linux kernel to enable efficient buffer and memory sharing between hardware such as codecs, GPUs, displays, cameras and the kernel drivers controlling them. For example, a decoder may want its output to be directly shared with the display server for rendering without a copy.

Any device driver which participates in DMA buffer sharing, can do so as either the exporter or importer of buffers (or both).

The memory that is shared via DMA buffers is usually stored in non-system memory (maybe in device's local memory or something else not directly accessible by the CPU), and accessing this memory from the CPU may have higher-than-usual overhead.

In particular for graphics data, it is not uncommon that data consists of multiple separate blocks of memory, for example one block for each of the red, green and blue channels. These blocks are called planes. DMA buffers can have up to four planes. Even if the memory is a single block, the data can be organized in multiple planes, by specifying offsets from the beginning of the data.

DMA buffers are exposed to user-space as file descriptors allowing to pass them between processes. If a DMA buffer has multiple planes, more than one file descriptor may be present, up to the number of planes. If the number of file descriptors is less than the number of planes, the remaining ones should be set to -1.

The format of the data (for graphics data, essentially its colorspace) is described by a 32-bit integer. These format identifiers are defined in the header file drm_fourcc.h and commonly referred to as fourcc values, since they are identified by 4 ASCII characters. Additionally, each DMA buffer has a modifier, which is a 64-bit integer that describes driver-specific details of the memory layout, such as tiling or compression.

For historical reasons, some producers of dma-bufs don't provide an explicit modifier, but instead return DMA_FORMAT_MOD_INVALID to indicate that their modifier is implicit. GTK tries to accommodate this situation by accepting DMA_FORMAT_MOD_INVALID as modifier.

The operation of GdkDmabufTextureBuilder is quite simple: Create a texture builder, set all the necessary properties, and then call DmabufTextureBuilder.build to create the new texture.

The required properties for a dma-buf texture are

  • The width and height in pixels

  • The fourcc code and modifier which identify the format and memory layout of the dma-buf

  • The file descriptor, offset and stride for each of the planes

GdkDmabufTextureBuilder can be used for quick one-shot construction of textures as well as kept around and reused to construct multiple textures.

For further information, see

  • The Linux kernel documentation

  • The header file drm_fourcc.h

Constructors

new

@classmethod
def new(cls) -> DmabufTextureBuilder

Creates a new texture builder.

Methods

build

def build(self, destroy: GLib.DestroyNotify | None = ..., data: int | None = ...) -> Texture | None

Builds a new GdkTexture with the values set up in the builder.

It is a programming error to call this function if any mandatory property has not been set.

Not all formats defined in the drm_fourcc.h header are supported. You can use Display.get_dmabuf_formats to get a list of supported formats. If the format is not supported by GTK, None will be returned and error will be set.

The destroy function gets called when the returned texture gets released.

It is the responsibility of the caller to keep the file descriptors for the planes open until the created texture is no longer used, and close them afterwards (possibly using the destroy notify).

It is possible to call this function multiple times to create multiple textures, possibly with changing properties in between.

Parameters:

  • destroy — destroy function to be called when the texture is released
  • data — user data to pass to the destroy function

get_color_state

def get_color_state(self) -> ColorState | None

Gets the color state previously set via DmabufTextureBuilder.set_color_state.

get_display

def get_display(self) -> Display

Returns the display that this texture builder is associated with.

get_fd

def get_fd(self, plane: int) -> int

Gets the file descriptor for a plane or -1 if none.

Parameters:

  • plane — the plane to get the fd for

get_fourcc

def get_fourcc(self) -> int

Gets the format previously set via DmabufTextureBuilder.set_fourcc or 0 if the format wasn't set.

The format is specified as a fourcc code.

get_height

def get_height(self) -> int

Gets the height previously set via DmabufTextureBuilder.set_height or 0 if the height wasn't set.

get_modifier

def get_modifier(self) -> int

Gets the modifier value.

get_n_planes

def get_n_planes(self) -> int

Gets the number of planes.

get_offset

def get_offset(self, plane: int) -> int

Gets the offset value for a plane.

Parameters:

  • plane — the plane to get the offset for

get_premultiplied

def get_premultiplied(self) -> bool

Whether the data is premultiplied.

get_stride

def get_stride(self, plane: int) -> int

Gets the stride value for a plane.

Parameters:

  • plane — the plane to get the stride for

get_update_region

def get_update_region(self) -> cairo.Region | None

Gets the region previously set via DmabufTextureBuilder.set_update_region or None if none was set.

get_update_texture

def get_update_texture(self) -> Texture | None

Gets the texture previously set via DmabufTextureBuilder.set_update_texture or None if none was set.

get_width

def get_width(self) -> int

Gets the width previously set via DmabufTextureBuilder.set_width or 0 if the width wasn't set.

set_color_state

def set_color_state(self, color_state: ColorState | None = ...) -> None

Sets the color state for the texture.

By default, the colorstate is NULL. In that case, GTK will choose the correct colorstate based on the format. If you don't know what colorstates are, this is probably the right thing.

Parameters:

  • color_state — a GdkColorState or NULL to unset the colorstate.

set_display

def set_display(self, display: Display) -> None

Sets the display that this texture builder is associated with.

The display is used to determine the supported dma-buf formats.

Parameters:

  • display — the display

set_fd

def set_fd(self, plane: int, fd: int) -> None

Sets the file descriptor for a plane or to -1 to unset it.

Parameters:

  • plane — the plane to set the fd for
  • fd — the file descriptor

set_fourcc

def set_fourcc(self, fourcc: int) -> None

Sets the format of the texture.

The format is specified as a fourcc code.

The format must be set before calling DmabufTextureBuilder.build.

Parameters:

  • fourcc — the texture's format or 0 to unset

set_height

def set_height(self, height: int) -> None

Sets the height of the texture.

The height must be set before calling DmabufTextureBuilder.build.

Parameters:

  • height — the texture's height or 0 to unset

set_modifier

def set_modifier(self, modifier: int) -> None

Sets the modifier.

Parameters:

  • modifier — the modifier value

set_n_planes

def set_n_planes(self, n_planes: int) -> None

Sets the number of planes of the texture.

Parameters:

  • n_planes — the number of planes

set_offset

def set_offset(self, plane: int, offset: int) -> None

Sets the offset for a plane.

Parameters:

  • plane — the plane to set the offset for
  • offset — the offset value

set_premultiplied

def set_premultiplied(self, premultiplied: bool) -> None

Sets whether the data is premultiplied.

Unless otherwise specified, all formats including alpha channels are assumed to be premultiplied.

Parameters:

  • premultiplied — whether the data is premultiplied

set_stride

def set_stride(self, plane: int, stride: int) -> None

Sets the stride for a plane.

The stride must be set for all planes before calling DmabufTextureBuilder.build.

Parameters:

  • plane — the plane to set the stride for
  • stride — the stride value

set_update_region

def set_update_region(self, region: cairo.Region | None = ...) -> None

Sets the region to be updated by this texture. Together with DmabufTextureBuilder.update-texture this describes an update of a previous texture.

When rendering animations of large textures, it is possible that consecutive textures are only updating contents in parts of the texture. It is then possible to describe this update via these two properties, so that GTK can avoid rerendering parts that did not change.

An example would be a screen recording where only the mouse pointer moves.

Parameters:

  • region — the region to update

set_update_texture

def set_update_texture(self, texture: Texture | None = ...) -> None

Sets the texture to be updated by this texture. See DmabufTextureBuilder.set_update_region for an explanation.

Parameters:

  • texture — the texture to update

set_width

def set_width(self, width: int) -> None

Sets the width of the texture.

The width must be set before calling DmabufTextureBuilder.build.

Parameters:

  • width — The texture's width or 0 to unset

Properties

color_state

color_state: ColorState  # read/write

The color state of the texture.

display

display: Display  # read/write

The display that this texture will be used on.

fourcc

fourcc: int  # read/write

The format of the texture, as a fourcc value.

height

height: int  # read/write

The height of the texture.

modifier

modifier: int  # read/write

The modifier.

n_planes

n_planes: int  # read/write

The number of planes of the texture.

Note that you can set properties for other planes, but they will be ignored when constructing the texture.

premultiplied

premultiplied: bool  # read/write

Whether the alpha channel is premultiplied into the others.

Only relevant if the format has alpha.

update_region

update_region: cairo.Region  # read/write

The update region for DmabufTextureBuilder.update-texture.

update_texture

update_texture: Texture  # read/write

The texture DmabufTextureBuilder.update-region is an update for.

width

width: int  # read/write

The width of the texture.