This document describes the life of a Flatland Image. Image is a bitmap of pixels and the most common type of Flatland Content. The graphical applications are often interested in drawing their UI onto a buffer and sending it to the system compositor to be displayed, which happens through Image. Flatland relies on Sysmem and Allocator APIs for the allocation of these graphical buffers.
The sequence diagram below shows the steps a client follows to allocate, use and destroy these graphical buffers. The reasoning and details are explained for each step that is described in this diagram.
Client initiates the buffer allocation by creating a Sysmem token. This uses the common steps in the allocation process of Sysmem. Sysmem allows allocating shared memory for use by multiple applications and hardware blocks. This functionality of Sysmem is exactly what is needed for graphical buffers: a buffer for writing, that the GPU can access and which the display can read. It is also important to avoid any copies because graphical buffers are big and copying operations can be costly.
Client duplicates Sysmem tokens which will be sent to the other participants. In this sequence, the focus is on the Flatland Image, so one of these duplicate tokens is needed for Scenic. However, the client is free to duplicate more and send around as needed. i.e. a video player application may want to share it with the decoder. Note that all of the Sysmem tokens have to be used for the allocation to complete.
Client uses one of the tokens itself to bind into a
BufferCollectionprotocol allows for the client to interact with Sysmem.
Client sets constraints on
BufferCollection. Remember that each Sysmem token needs to be used and there are no exceptions. Client may choose to set no constraints here.
Client sends one of the Sysmem token duplicates to Flatland Allocator. This is the stage where Flatland becomes a participant in this buffer collection negotiation. Client does this by creating a
pair. Client connects to fuchsia::ui::composition::Allocator service and sendsBufferCollectionExportToken` and the duplicated Sysmem token.
Flatland sets buffer constraints behind the scenes. Flatland collects the necessary constraints from Vulkan and Display hardware. This ensures that Flatland can directly use the buffers. At this stage,
BufferCollectionExportTokenis registered for this allocation and any Flatland instance may access it by using the corresponding
BufferCollectionImportToken. This registration is not limited to a single Flatland instance in case the instances are torn down or the client has multiple Flatland instances, to avoid reallocation or copy of buffers which is costly.
Client waits for the buffer allocation completion through
BufferCollectionprotocol. This can only succeed if all of the duplicated Sysmem tokens are used and an overlap is found that satisfies each participant’s constraints. This call may hang if any of the tokens are unused. Since this may be blocking or unsuccessful, this is expected to be processed on the client side. Once this returns with a success signal, the shared memory is actually allocated.
Client creates a Flatland Image by using
BufferCollectionImportToken. Client may choose to allocate multiple Images by duplicating and using this
BufferCollectionImportToken. All of them will be pointing to the same memory allocation.
Clients drops all of its
BufferCollectionImportTokenswhen it is done creating Flatland Images. This sends a signal to Flatland Allocator that there will be no new Flatland Images created after this point. This is a token ownership model instead of an explicit release method on FIDL protocol to make it less likely to misuse and leak. This doesn’t free the buffers yet, because they stay around as long as the Flatland Images are references. The next step is required before freeing the buffers.
Client releases Flatland Image. Once all the Images referring to the
BufferCollectionare released, the memory will be freed.