CanvasFrontBufferedRenderer


@RequiresApi(value = 29)
class CanvasFrontBufferedRenderer<T : Any?>


Class responsible for supporting a "front buffered" rendering system. This allows for lower latency graphics by leveraging a combination of front buffered and multi buffered content layers. Active content is rendered first into the front buffered layer which is simultaneously being presented to the display. Periodically content is rendered into the multi buffered layer which will have more traditional latency guarantees, however, minimizes the impact of visual artifacts due to graphical tearing.

Summary

Nested types

Provides callbacks for consumers to draw into the front and multi buffered layers as well as provide opportunities to synchronize SurfaceControlCompat.Transactions to submit the layers to the hardware compositor.

Public constructors

<T : Any?> CanvasFrontBufferedRenderer(
    surfaceView: SurfaceView,
    callback: CanvasFrontBufferedRenderer.Callback<T>,
    bufferFormat: Int
)

Public functions

Unit

Requests to cancel rendering and hides the front buffered layer.

Unit

Clears the contents of both the front and multi buffered layers.

Unit

Requests to render the entire scene to the multi buffered layer and schedules a call to Callback.onDrawMultiBufferedLayer.

Boolean

Determines whether or not the CanvasFrontBufferedRenderer is in a valid state.

Unit
release(cancelPending: Boolean, onReleaseComplete: (() -> Unit)?)

Releases the CanvasFrontBufferedRenderer.

Unit

Render content to the front buffered layer providing optional parameters to be consumed in Callback.onDrawFrontBufferedLayer.

Unit

Requests to render to the multi buffered layer.

Public properties

Int

format of the underlying buffers being rendered into by CanvasFrontBufferedRenderer.

ColorSpace

Configures the ColorSpace that the content should be rendered with for the front and multi buffered layers.

Public constructors

CanvasFrontBufferedRenderer

<T : Any?> CanvasFrontBufferedRenderer(
    surfaceView: SurfaceView,
    callback: CanvasFrontBufferedRenderer.Callback<T>,
    bufferFormat: Int = HardwareBuffer.RGBA_8888
)
Parameters
surfaceView: SurfaceView

Target SurfaceView to act as the parent rendering layer for multi buffered content

callback: CanvasFrontBufferedRenderer.Callback<T>

Callbacks used to render into front and multi buffered layers as well as configuring SurfaceControlCompat.Transactions for controlling these layers in addition to other SurfaceControlCompat instances that must be updated atomically within the user interface. These callbacks are invoked on an internal rendering thread. The templated type here is consumer defined to represent the data structures to be consumed for rendering within Callback.onDrawFrontBufferedLayer and Callback.onDrawMultiBufferedLayer and are provided by the CanvasFrontBufferedRenderer.renderFrontBufferedLayer and CanvasFrontBufferedRenderer.renderMultiBufferedLayer methods.

bufferFormat: Int = HardwareBuffer.RGBA_8888

format of the underlying buffers being rendered into by CanvasFrontBufferedRenderer. The particular valid combinations for a given Android version and implementation should be documented by that version. HardwareBuffer.RGBA_8888 and HardwareBuffer.RGBX_8888 are guaranteed to be supported. However, consumers are recommended to query the desired HardwareBuffer configuration using HardwareBuffer.isSupported. The default is HardwareBuffer.RGBA_8888.

Public functions

cancel

Added in 1.0.2
fun cancel(): Unit

Requests to cancel rendering and hides the front buffered layer. Unlike commit, this does not schedule a call to render into the multi buffered layer. This is useful in palm rejection use cases, where some initial touch events might be processed before a corresponding cancel event is received indicating the touch gesture is coming from a palm rather than intentional user input. In the case where MotionEvent#getAction returns ACTION_CANCEL, this is to be invoked.

If this GLFrontBufferedRenderer has been released, that is isValid returns false, this call is ignored.

clear

Added in 1.0.2
fun clear(): Unit

Clears the contents of both the front and multi buffered layers. This triggers a call to Callback.onMultiBufferedLayerRenderComplete and hides the front buffered layer.

commit

Added in 1.0.2
fun commit(): Unit

Requests to render the entire scene to the multi buffered layer and schedules a call to Callback.onDrawMultiBufferedLayer. The parameters provided to Callback.onDrawMultiBufferedLayer will include each argument provided to every renderFrontBufferedLayer call since the last call to commit has been made. When rendering to the multi-buffered layer is complete, this synchronously hides the front buffer and updates the multi buffered layer.

If this CanvasFrontBufferedRenderer has been released, that is isValid returns false, this call is ignored.

isValid

Added in 1.0.2
fun isValid(): Boolean

Determines whether or not the CanvasFrontBufferedRenderer is in a valid state. That is the release method has not been called. If this returns false, then subsequent calls to renderFrontBufferedLayer, renderMultiBufferedLayer, commit, and release are ignored

Returns
Boolean

true if this CanvasFrontBufferedRenderer has been released, false otherwise

release

Added in 1.0.2
fun release(cancelPending: Boolean, onReleaseComplete: (() -> Unit)? = null): Unit

Releases the CanvasFrontBufferedRenderer. In process requests are ignored. If the CanvasFrontBufferedRenderer is already released, that is isValid returns false, this method does nothing.

renderFrontBufferedLayer

Added in 1.0.2
fun renderFrontBufferedLayer(param: T): Unit

Render content to the front buffered layer providing optional parameters to be consumed in Callback.onDrawFrontBufferedLayer. Additionally the parameter provided here will also be consumed in Callback.onDrawMultiBufferedLayer when the corresponding commit method is invoked, which will include all params in each call made to this method up to the corresponding commit call.

If this CanvasFrontBufferedRenderer has been released, that is isValid returns false, this call is ignored.

Parameters
param: T

Optional parameter to be consumed when rendering content into the commit layer

renderMultiBufferedLayer

fun renderMultiBufferedLayer(params: Collection<T>): Unit

Requests to render to the multi buffered layer. This schedules a call to Callback.onDrawMultiBufferedLayer with the parameters provided. If the front buffered layer is visible, this will hide this layer after rendering to the multi buffered layer is complete. This is equivalent to calling CanvasFrontBufferedRenderer.renderFrontBufferedLayer for each parameter provided in the collection followed by a single call to CanvasFrontBufferedRenderer.commit. This is useful for re-rendering the multi buffered scene when the corresponding Activity is being resumed from the background in which the contents should be re-drawn. Additionally this allows for applications to decide to dynamically render to either front or multi buffered layers.

If this CanvasFrontBufferedRenderer has been released, that is isValid returns 'false', this call is ignored.

Parameters
params: Collection<T>

Parameters that to be consumed when rendering to the multi buffered layer. These parameters will be provided in the corresponding call to Callback.onDrawMultiBufferedLayer

Public properties

bufferFormat

Added in 1.0.2
val bufferFormatInt

format of the underlying buffers being rendered into by CanvasFrontBufferedRenderer. The particular valid combinations for a given Android version and implementation should be documented by that version. HardwareBuffer.RGBA_8888 and HardwareBuffer.RGBX_8888 are guaranteed to be supported. However, consumers are recommended to query the desired HardwareBuffer configuration using HardwareBuffer.isSupported. The default is HardwareBuffer.RGBA_8888.

colorSpace

Added in 1.0.2
var colorSpaceColorSpace

Configures the ColorSpace that the content should be rendered with for the front and multi buffered layers. This parameter is only consumed on Android U and above. For older API levels this is ignored.