CanvasBufferedRenderer
@RequiresApi(value = 29)
class CanvasBufferedRenderer : 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 |
inner class CanvasBufferedRenderer.RenderRequestSets the parameters that can be used to control a render request for a |
|
Class that contains data regarding the result of the render request. |
Public functions |
|
|---|---|
open Unit |
close()Releases the resources associated with this |
CanvasBufferedRenderer.RenderRequest |
Returns a |
Unit |
releaseBuffer(hardwareBuffer: HardwareBuffer, fence: SyncFenceCompat?)Releases the |
Unit |
setContentRoot(renderNode: RenderNode)Sets the content root to render. |
Unit |
setLightSourceAlpha(ambientShadowAlpha: Float, spotShadowAlpha: Float)Configures the ambient & spot shadow alphas. |
Unit |
setLightSourceGeometry(Sets the center of the light source. |
Public properties |
|
|---|---|
Int |
Returns the |
Boolean |
Returns if the |
Int |
Returns the number of buffers within the swap chain used for rendering with this |
Long |
Returns the current usage flag hints of the buffers that are being rendered into by this |
Public functions
close
open fun close(): Unit
Releases the resources associated with this CanvasBufferedRenderer instance. Note this does not call HardwareBuffer.close on the provided HardwareBuffer instance.
obtainRenderRequest
fun obtainRenderRequest(): CanvasBufferedRenderer.RenderRequest
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
fun releaseBuffer(hardwareBuffer: HardwareBuffer, fence: SyncFenceCompat? = null): Unit
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 | |
|---|---|
hardwareBuffer: HardwareBuffer |
|
fence: SyncFenceCompat? = null |
Optional |
setContentRoot
fun setContentRoot(renderNode: RenderNode): Unit
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
fun setLightSourceAlpha(ambientShadowAlpha: Float, spotShadowAlpha: Float): Unit
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
fun setLightSourceGeometry(
lightX: Float,
lightY: Float,
lightZ: Float,
lightRadius: Float
): Unit
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.
Public properties
bufferFormat
val bufferFormat: Int
Returns the HardwareBufferFormat of the buffers that are being rendered into by this CanvasBufferedRenderer
isClosed
val isClosed: Boolean
Returns if the CanvasBufferedRenderer has already been closed. That is CanvasBufferedRenderer.close has been invoked.
maxBuffers
val maxBuffers: Int
Returns the number of buffers within the swap chain used for rendering with this CanvasBufferedRenderer
usageFlags
val usageFlags: Long
Returns the current usage flag hints of the buffers that are being rendered into by this CanvasBufferedRenderer