CanvasBufferedRenderer


@RequiresApi(value = 29)
public final class CanvasBufferedRenderer implements AutoCloseable


Creates an instance of a hardware-accelerated renderer. This is used to render a scene built from RenderNodes to an output HardwareBuffer. There can be as many CanvasBufferedRenderer instances as desired.

Resources & lifecycle

All CanvasBufferedRenderer instances share a common render thread. Therefore CanvasBufferedRenderer will share common resources and GPU utilization with hardware accelerated rendering initiated by the UI thread of an application. The render thread contains the GPU context & resources necessary to do GPU-accelerated rendering. As such, the first CanvasBufferedRenderer created comes with the cost of also creating the associated GPU contexts, however each incremental CanvasBufferedRenderer thereafter is fairly cheap.

This is useful in situations where a scene built with RenderNode.

CanvasBufferedRenderer can optionally persist contents before each draw invocation so previous contents in the HardwareBuffer target will be preserved across renders. This is determined by the argument provided to CanvasBufferedRenderer.RenderRequest.preserveContents which is set to false by default.

Summary

Nested types

Builder used to construct a CanvasBufferedRenderer instance.

public final inner class CanvasBufferedRenderer.RenderRequest

Sets the parameters that can be used to control a render request for a CanvasBufferedRenderer.

Class that contains data regarding the result of the render request.

Public methods

void

Releases the resources associated with this CanvasBufferedRenderer instance.

final int

Returns the HardwareBufferFormat of the buffers that are being rendered into by this CanvasBufferedRenderer

final int

Returns the number of buffers within the swap chain used for rendering with this CanvasBufferedRenderer

final long

Returns the current usage flag hints of the buffers that are being rendered into by this CanvasBufferedRenderer

final boolean

Returns if the CanvasBufferedRenderer has already been closed.

final @NonNull CanvasBufferedRenderer.RenderRequest

Returns a RenderRequest that can be used to render into the provided HardwareBuffer.

final void
releaseBuffer(
    @NonNull HardwareBuffer hardwareBuffer,
    SyncFenceCompat fence
)

Releases the HardwareBuffer back into the allocation pool to be reused in subsequent renders.

final void

Sets the content root to render.

final void
setLightSourceAlpha(float ambientShadowAlpha, float spotShadowAlpha)

Configures the ambient & spot shadow alphas.

final void
setLightSourceGeometry(
    float lightX,
    float lightY,
    float lightZ,
    float lightRadius
)

Sets the center of the light source.

Public methods

close

Added in 1.0.2
public void close()

Releases the resources associated with this CanvasBufferedRenderer instance. Note this does not call HardwareBuffer.close on the provided HardwareBuffer instance.

getBufferFormat

Added in 1.0.2
public final int getBufferFormat()

Returns the HardwareBufferFormat of the buffers that are being rendered into by this CanvasBufferedRenderer

getMaxBuffers

Added in 1.0.2
public final int getMaxBuffers()

Returns the number of buffers within the swap chain used for rendering with this CanvasBufferedRenderer

getUsageFlags

Added in 1.0.2
public final long getUsageFlags()

Returns the current usage flag hints of the buffers that are being rendered into by this CanvasBufferedRenderer

isClosed

Added in 1.0.2
public final boolean isClosed()

Returns if the CanvasBufferedRenderer has already been closed. That is CanvasBufferedRenderer.close has been invoked.

obtainRenderRequest

Added in 1.0.2
public final @NonNull CanvasBufferedRenderer.RenderRequest obtainRenderRequest()

Returns a RenderRequest that can be used to render into the provided HardwareBuffer. This is used to synchronize the RenderNode content provided by setContentRoot.

releaseBuffer

Added in 1.0.2
public final void releaseBuffer(
    @NonNull HardwareBuffer hardwareBuffer,
    SyncFenceCompat fence
)

Releases the HardwareBuffer back into the allocation pool to be reused in subsequent renders. The HardwareBuffer instance released here must be one that was originally obtained from this CanvasBufferedRenderer instance. This method also takes in an optional SyncFenceCompat instance that will be internally waited upon before re-using the buffer. This is useful in conjunction with SurfaceControlCompat.Transaction.setBuffer where the system will return a release fence that should be waited upon before the corresponding buffer can be re-used.

Parameters
@NonNull HardwareBuffer hardwareBuffer

HardwareBuffer to return back to the allocation pool

SyncFenceCompat fence

Optional SyncFenceCompat that should be waited upon before the buffer is reused.

setContentRoot

Added in 1.0.2
public final void setContentRoot(@NonNull RenderNode renderNode)

Sets the content root to render. It is not necessary to call this whenever the content recording changes. Any mutations to the RenderNode content, or any of the RenderNodes contained within the content node, will be applied whenever a new RenderRequest is issued via obtainRenderRequest and RenderRequest.drawAsync.

setLightSourceAlpha

Added in 1.0.2
public final void setLightSourceAlpha(float ambientShadowAlpha, float spotShadowAlpha)

Configures the ambient & spot shadow alphas. This is the alpha used when the shadow has max alpha, and ramps down from the values provided to zero.

These values are typically provided by the current theme, see R.attr.spotShadowAlpha and R.attr.ambientShadowAlpha.

This must be set at least once along with setLightSourceGeometry before shadows will work.

setLightSourceGeometry

Added in 1.0.2
public final void setLightSourceGeometry(
    float lightX,
    float lightY,
    float lightZ,
    float lightRadius
)

Sets the center of the light source. The light source point controls the directionality and shape of shadows rendered by RenderNode Z & elevation.

The light source should be setup both as part of initial configuration, and whenever the window moves to ensure the light source stays anchored in display space instead of in window space.

This must be set at least once along with setLightSourceAlpha before shadows will work.