Added in API level 34

HardwareBufferRenderer


open class HardwareBufferRenderer : AutoCloseable
kotlin.Any
   ↳ android.graphics.HardwareBufferRenderer

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 HardwareBufferRenderer instances as desired.

Resources & lifecycle

All HardwareBufferRenderer and HardwareRenderer instances share a common render thread. Therefore HardwareBufferRenderer 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 HardwareBufferRenderer created comes with the cost of also creating the associated GPU contexts, however each incremental HardwareBufferRenderer thereafter is fairly cheap. The expected usage is to have a HardwareBufferRenderer instance for every active HardwareBuffer.

This is useful in situations where a scene built with RenderNodes can be consumed directly by the system compositor through SurfaceControl.Transaction.setBuffer(SurfaceControl, HardwareBuffer). HardwareBufferRenderer will never clear contents before each draw invocation so previous contents in the HardwareBuffer target will be preserved across renders.

Summary

Nested classes

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

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

Public constructors

Creates a new instance of HardwareBufferRenderer with the provided HardwareBuffer as the output of the rendered scene.

Public methods
open Unit

Releases the resources associated with this HardwareBufferRenderer instance.

open Boolean

Returns if the HardwareBufferRenderer has already been closed.

open HardwareBufferRenderer.RenderRequest

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

open Unit

Sets the content root to render.

open Unit
setLightSourceAlpha(ambientShadowAlpha: Float, spotShadowAlpha: Float)

Configures the ambient & spot shadow alphas.

open Unit
setLightSourceGeometry(lightX: Float, lightY: Float, lightZ: Float, lightRadius: Float)

Sets the center of the light source.

Public constructors

HardwareBufferRenderer

Added in API level 34
HardwareBufferRenderer(buffer: HardwareBuffer)

Creates a new instance of HardwareBufferRenderer with the provided HardwareBuffer as the output of the rendered scene.

Parameters
buffer HardwareBuffer: This value cannot be null.

Public methods

close

Added in API level 34
open fun close(): Unit

Releases the resources associated with this HardwareBufferRenderer instance. **Note** this does not call HardwareBuffer.close() on the provided HardwareBuffer instance

Exceptions
java.lang.Exception if this resource cannot be closed

isClosed

Added in API level 34
open fun isClosed(): Boolean

Returns if the HardwareBufferRenderer has already been closed. That is HardwareBufferRenderer.close() has been invoked.

Return
Boolean True if the HardwareBufferRenderer has been closed, false otherwise.

obtainRenderRequest

Added in API level 34
open fun obtainRenderRequest(): HardwareBufferRenderer.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(android.graphics.RenderNode).

Return
HardwareBufferRenderer.RenderRequest An instance of RenderRequest. The instance may be reused for every frame, so the caller should not hold onto it for longer than a single render request. This value cannot be null.

setContentRoot

Added in API level 34
open fun setContentRoot(content: 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.draw(Executor,.

Parameters
content RenderNode?: The content to set as the root RenderNode. If null the content root is removed and the renderer will draw nothing.

setLightSourceAlpha

Added in API level 34
open 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 android.R.attr#spotShadowAlpha and android.R.attr#ambientShadowAlpha.

This must be set at least once along with setLightSourceGeometry(float,float,float,float) before shadows will work.

Parameters
ambientShadowAlpha Float: The alpha for the ambient shadow. If unsure, a reasonable default is 0.039f. Value is between 0.0f and 1.0f inclusive
spotShadowAlpha Float: The alpha for the spot shadow. If unsure, a reasonable default is 0.19f. Value is between 0.0f and 1.0f inclusive

setLightSourceGeometry

Added in API level 34
open 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(float,float) before shadows will work.

Parameters
lightX Float: The X position of the light source. If unsure, a reasonable default is 'displayWidth / 2f - windowLeft'.
lightY Float: The Y position of the light source. If unsure, a reasonable default is '0 - windowTop'
lightZ Float: The Z position of the light source. Must be >= 0. If unsure, a reasonable default is 600dp. Value is 0f or greater
lightRadius Float: The radius of the light source. Smaller radius will have sharper edges, larger radius will have softer shadows. If unsure, a reasonable default is 800 dp. Value is 0f or greater