Stay organized with collections Save and categorize content based on your preferences.

SurfaceControl.Transaction

public static class SurfaceControl.Transaction
extends Object implements Closeable, Parcelable

java.lang.Object
   ↳ android.view.SurfaceControl.Transaction


An atomic set of changes to a set of SurfaceControl.

Summary

Inherited constants

Fields

public static final Creator<SurfaceControl.Transaction> CREATOR

Public constructors

Transaction()

Open a new transaction object.

Public methods

SurfaceControl.Transaction addTransactionCommittedListener(Executor executor, SurfaceControl.TransactionCommittedListener listener)

Request to add a TransactionCommittedListener.

void apply()

Apply the transaction, clearing it's state, and making it usable as a new transaction.

void close()

Release the native transaction object, without applying it.

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

SurfaceControl.Transaction merge(SurfaceControl.Transaction other)

Merge the other transaction into this transaction, clearing the other transaction as if it had been applied.

SurfaceControl.Transaction reparent(SurfaceControl sc, SurfaceControl newParent)

Re-parents a given layer to a new parent.

SurfaceControl.Transaction setAlpha(SurfaceControl sc, float alpha)

Set the alpha for a given surface.

SurfaceControl.Transaction setBuffer(SurfaceControl sc, HardwareBuffer buffer, SyncFence fence)

Updates the HardwareBuffer displayed for the SurfaceControl.

SurfaceControl.Transaction setBuffer(SurfaceControl sc, HardwareBuffer buffer)

Updates the HardwareBuffer displayed for the SurfaceControl.

SurfaceControl.Transaction setBuffer(SurfaceControl sc, HardwareBuffer buffer, SyncFence fence, Consumer<SyncFence> releaseCallback)

Updates the HardwareBuffer displayed for the SurfaceControl.

SurfaceControl.Transaction setBufferSize(SurfaceControl sc, int w, int h)

Set the default buffer size for the SurfaceControl, if there is a Surface associated with the control, then this will be the default size for buffers dequeued from it.

SurfaceControl.Transaction setBufferTransform(SurfaceControl sc, int transform)

Sets the buffer transform that should be applied to the current buffer.

SurfaceControl.Transaction setCrop(SurfaceControl sc, Rect crop)

Bounds the surface and its children to the bounds specified.

SurfaceControl.Transaction setDamageRegion(SurfaceControl sc, Region region)

Updates the region for the content on this surface updated in this transaction.

SurfaceControl.Transaction setDataSpace(SurfaceControl sc, int dataspace)

Set the dataspace for the SurfaceControl.

SurfaceControl.Transaction setFrameRate(SurfaceControl sc, float frameRate, int compatibility, int changeFrameRateStrategy)

Sets the intended frame rate for the surface SurfaceControl.

SurfaceControl.Transaction setFrameRate(SurfaceControl sc, float frameRate, int compatibility)

Sets the intended frame rate for this surface.

SurfaceControl.Transaction setGeometry(SurfaceControl sc, Rect sourceCrop, Rect destFrame, int orientation)

This method was deprecated in API level 33. Use setCrop(android.view.SurfaceControl, android.graphics.Rect), setBufferTransform(android.view.SurfaceControl, int), setPosition(android.view.SurfaceControl, float, float) and setScale(android.view.SurfaceControl, float, float) instead.

SurfaceControl.Transaction setLayer(SurfaceControl sc, int z)

Set the Z-order for a given SurfaceControl, relative to it's siblings.

SurfaceControl.Transaction setOpaque(SurfaceControl sc, boolean isOpaque)

Indicates whether the surface must be considered opaque, even if its pixel format is set to translucent.

SurfaceControl.Transaction setPosition(SurfaceControl sc, float x, float y)

Sets the SurfaceControl to the specified position relative to the parent SurfaceControl

SurfaceControl.Transaction setScale(SurfaceControl sc, float scaleX, float scaleY)

Sets the SurfaceControl to the specified scale with (0, 0) as the center point of the scale.

SurfaceControl.Transaction setVisibility(SurfaceControl sc, boolean visible)

Toggle the visibility of a given Layer and it's sub-tree.

void writeToParcel(Parcel dest, int flags)

Writes the transaction to parcel, clearing the transaction as if it had been applied so it can be used to store future transactions.

Inherited methods

Fields

CREATOR

Added in API level 30
public static final Creator<SurfaceControl.Transaction> CREATOR

Public constructors

Transaction

Added in API level 29
public Transaction ()

Open a new transaction object. The transaction may be filed with commands to manipulate SurfaceControl instances, and then applied atomically with apply(). Eventually the user should invoke close(), when the object is no longer required. Note however that re-using a transaction after a call to apply is allowed as a convenience.

Public methods

addTransactionCommittedListener

Added in API level 33
public SurfaceControl.Transaction addTransactionCommittedListener (Executor executor, 
                SurfaceControl.TransactionCommittedListener listener)

Request to add a TransactionCommittedListener. The callback is invoked when transaction is applied and the updates are ready to be presented. This callback does not mean buffers have been released! It simply means that any new transactions applied will not overwrite the transaction for which we are receiving a callback and instead will be included in the next frame. If you are trying to avoid dropping frames (overwriting transactions), and unable to use timestamps (Which provide a more efficient solution), then this method provides a method to pace your transaction application.

Parameters
executor Executor: The executor that the callback should be invoked on. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.

listener SurfaceControl.TransactionCommittedListener: The callback that will be invoked when the transaction has been committed. This value cannot be null.

Returns
SurfaceControl.Transaction This value cannot be null.

apply

Added in API level 29
public void apply ()

Apply the transaction, clearing it's state, and making it usable as a new transaction.

close

Added in API level 29
public void close ()

Release the native transaction object, without applying it.

describeContents

Added in API level 30
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

merge

Added in API level 29
public SurfaceControl.Transaction merge (SurfaceControl.Transaction other)

Merge the other transaction into this transaction, clearing the other transaction as if it had been applied.

Parameters
other SurfaceControl.Transaction: The transaction to merge in to this one. This value cannot be null.

Returns
SurfaceControl.Transaction This transaction. This value cannot be null.

reparent

Added in API level 29
public SurfaceControl.Transaction reparent (SurfaceControl sc, 
                SurfaceControl newParent)

Re-parents a given layer to a new parent. Children inherit transform (position, scaling) crop, visibility, and Z-ordering from their parents, as if the children were pixels within the parent Surface.

Parameters
sc SurfaceControl: The SurfaceControl to reparent This value cannot be null.

newParent SurfaceControl: The new parent for the given control. This value may be null.

Returns
SurfaceControl.Transaction This Transaction This value cannot be null.

setAlpha

Added in API level 29
public SurfaceControl.Transaction setAlpha (SurfaceControl sc, 
                float alpha)

Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl will be blended with the Surfaces under it according to the specified ratio.

Parameters
sc SurfaceControl: The given SurfaceControl. This value cannot be null.

alpha float: The alpha to set. Value is between 0.0 and 1.0 inclusive

Returns
SurfaceControl.Transaction This value cannot be null.

setBuffer

Added in API level 33
public SurfaceControl.Transaction setBuffer (SurfaceControl sc, 
                HardwareBuffer buffer, 
                SyncFence fence)

Updates the HardwareBuffer displayed for the SurfaceControl. Note that the buffer must be allocated with HardwareBuffer#USAGE_COMPOSER_OVERLAY as well as HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE as the surface control might be composited using either an overlay or using the GPU. A presentation fence may be passed to improve performance by allowing the buffer to complete rendering while it is waiting for the transaction to be applied. For example, if the buffer is being produced by rendering with OpenGL ES then a fence created with EGLExt.eglDupNativeFenceFDANDROID(EGLDisplay, EGLSync) can be used to allow the GPU rendering to be concurrent with the transaction. The compositor will wait for the fence to be signaled before the buffer is displayed. If multiple buffers are set as part of the same transaction, the presentation fences of all of them must signal before any buffer is displayed. That is, the entire transaction is delayed until all presentation fences have signaled, ensuring the transaction remains consistent.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

buffer HardwareBuffer: The buffer to be displayed This value may be null.

fence SyncFence: The presentation fence. If null or invalid, this is equivalent to setBuffer(android.view.SurfaceControl, android.hardware.HardwareBuffer)

Returns
SurfaceControl.Transaction this This value cannot be null.

setBuffer

Added in API level 33
public SurfaceControl.Transaction setBuffer (SurfaceControl sc, 
                HardwareBuffer buffer)

Updates the HardwareBuffer displayed for the SurfaceControl. Note that the buffer must be allocated with HardwareBuffer#USAGE_COMPOSER_OVERLAY as well as HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE as the surface control might be composited using either an overlay or using the GPU.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

buffer HardwareBuffer: The buffer to be displayed This value may be null.

Returns
SurfaceControl.Transaction this This value cannot be null.

setBuffer

Added in API level 33
public SurfaceControl.Transaction setBuffer (SurfaceControl sc, 
                HardwareBuffer buffer, 
                SyncFence fence, 
                Consumer<SyncFence> releaseCallback)

Updates the HardwareBuffer displayed for the SurfaceControl. Note that the buffer must be allocated with HardwareBuffer#USAGE_COMPOSER_OVERLAY as well as HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE as the surface control might be composited using either an overlay or using the GPU. A presentation fence may be passed to improve performance by allowing the buffer to complete rendering while it is waiting for the transaction to be applied. For example, if the buffer is being produced by rendering with OpenGL ES then a fence created with EGLExt.eglDupNativeFenceFDANDROID(EGLDisplay, EGLSync) can be used to allow the GPU rendering to be concurrent with the transaction. The compositor will wait for the fence to be signaled before the buffer is displayed. If multiple buffers are set as part of the same transaction, the presentation fences of all of them must signal before any buffer is displayed. That is, the entire transaction is delayed until all presentation fences have signaled, ensuring the transaction remains consistent. A releaseCallback may be passed to know when the buffer is safe to be reused. This is recommended when attempting to render continuously using SurfaceControl transactions instead of through Surface, as it provides a safe & reliable way to know when a buffer can be re-used. The callback will be invoked with a SyncFence which, if valid, must be waited on prior to using the buffer. This can either be done directly with SyncFence#awaitForever() or it may be done indirectly such as passing it as a release fence to Image.setFence(SyncFence) when using ImageReader.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

buffer HardwareBuffer: The buffer to be displayed This value may be null.

fence SyncFence: The presentation fence. If null or invalid, this is equivalent to setBuffer(android.view.SurfaceControl, android.hardware.HardwareBuffer)

releaseCallback Consumer: The callback to invoke when the buffer being set has been released by a later transaction. That is, the point at which it is safe to re-use the buffer. This value may be null.

Returns
SurfaceControl.Transaction this This value cannot be null.

setBufferSize

Added in API level 29
public SurfaceControl.Transaction setBufferSize (SurfaceControl sc, 
                int w, 
                int h)

Set the default buffer size for the SurfaceControl, if there is a Surface associated with the control, then this will be the default size for buffers dequeued from it.

Parameters
sc SurfaceControl: The surface to set the buffer size for. This value cannot be null.

w int: The default width Value is 0 or greater

h int: The default height Value is 0 or greater

Returns
SurfaceControl.Transaction This Transaction This value cannot be null.

setBufferTransform

Added in API level 33
public SurfaceControl.Transaction setBufferTransform (SurfaceControl sc, 
                int transform)

Sets the buffer transform that should be applied to the current buffer. This can be used in combination with AttachedSurfaceControl#addOnBufferTransformHintChangedListener(AttachedSurfaceControl.OnBufferTransformHintChangedListener) to pre-rotate the buffer for the current display orientation. This can improve the performance of displaying the associated buffer.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

transform int: The transform to apply to the buffer. Value is SurfaceControl.BUFFER_TRANSFORM_IDENTITY, SurfaceControl.BUFFER_TRANSFORM_MIRROR_HORIZONTAL, SurfaceControl.BUFFER_TRANSFORM_MIRROR_VERTICAL, SurfaceControl.BUFFER_TRANSFORM_ROTATE_90, SurfaceControl.BUFFER_TRANSFORM_ROTATE_180, SurfaceControl.BUFFER_TRANSFORM_ROTATE_270, BUFFER_TRANSFORM_MIRROR_HORIZONTAL | BUFFER_TRANSFORM_ROTATE_90, or BUFFER_TRANSFORM_MIRROR_VERTICAL | BUFFER_TRANSFORM_ROTATE_90

Returns
SurfaceControl.Transaction this This value cannot be null.

setCrop

Added in API level 33
public SurfaceControl.Transaction setCrop (SurfaceControl sc, 
                Rect crop)

Bounds the surface and its children to the bounds specified. Size of the surface will be ignored and only the crop and buffer size will be used to determine the bounds of the surface. If no crop is specified and the surface has no buffer, the surface bounds is only constrained by the size of its parent bounds.

Parameters
sc SurfaceControl: SurfaceControl to set crop of. This value cannot be null.

crop Rect: Bounds of the crop to apply. This value may be null.

Returns
SurfaceControl.Transaction this This transaction for chaining This value cannot be null.

setDamageRegion

Added in API level 33
public SurfaceControl.Transaction setDamageRegion (SurfaceControl sc, 
                Region region)

Updates the region for the content on this surface updated in this transaction. The damage region is the area of the buffer that has changed since the previously sent buffer. This can be used to reduce the amount of recomposition that needs to happen when only a small region of the buffer is being updated, such as for a small blinking cursor or a loading indicator.

Parameters
sc SurfaceControl: The SurfaceControl on which to set the damage region This value cannot be null.

region Region: The region to set. If null, the entire buffer is assumed dirty. This is equivalent to not setting a damage region at all.

Returns
SurfaceControl.Transaction This value cannot be null.

setDataSpace

Added in API level 33
public SurfaceControl.Transaction setDataSpace (SurfaceControl sc, 
                int dataspace)

Set the dataspace for the SurfaceControl. This will control how the buffer set with setBuffer(android.view.SurfaceControl, android.hardware.HardwareBuffer) is displayed.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

dataspace int: The dataspace to set it to Value is either 0 or a combination of DataSpace.DATASPACE_UNKNOWN, DataSpace.DATASPACE_SCRGB_LINEAR, DataSpace.DATASPACE_SRGB, DataSpace.DATASPACE_SCRGB, DataSpace.DATASPACE_DISPLAY_P3, DataSpace.DATASPACE_BT2020_PQ, DataSpace.DATASPACE_ADOBE_RGB, DataSpace.DATASPACE_JFIF, DataSpace.DATASPACE_BT601_625, DataSpace.DATASPACE_BT601_525, DataSpace.DATASPACE_BT2020, DataSpace.DATASPACE_BT709, DataSpace.DATASPACE_DCI_P3, and DataSpace.DATASPACE_SRGB_LINEAR

Returns
SurfaceControl.Transaction this This value cannot be null.

setFrameRate

Added in API level 31
public SurfaceControl.Transaction setFrameRate (SurfaceControl sc, 
                float frameRate, 
                int compatibility, 
                int changeFrameRateStrategy)

Sets the intended frame rate for the surface SurfaceControl.

On devices that are capable of running the display at different refresh rates, the system may choose a display refresh rate to better match this surface's frame rate. Usage of this API won't directly affect the application's frame production pipeline. However, because the system may change the display refresh rate, calls to this function may result in changes to Choreographer callback timings, and changes to the time interval at which the system releases buffers back to the application.

Note that this only has an effect for surfaces presented on the display. If this surface is consumed by something other than the system compositor, e.g. a media codec, this call has no effect.

Parameters
sc SurfaceControl: The SurfaceControl to specify the frame rate of. This value cannot be null.

frameRate float: The intended frame rate for this surface, in frames per second. 0 is a special value that indicates the app will accept the system's choice for the display frame rate, which is the default behavior if this function isn't called. The frameRate param does not need to be a valid refresh rate for this device's display - e.g., it's fine to pass 30fps to a device that can only run the display at 60fps. Value is 0.0 or greater

compatibility int: The frame rate compatibility of this surface. The compatibility value may influence the system's choice of display frame rate. This parameter is ignored when frameRate is 0. Value is Surface.FRAME_RATE_COMPATIBILITY_DEFAULT, or Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE

changeFrameRateStrategy int: Whether display refresh rate transitions caused by this surface should be seamless. A seamless transition is one that doesn't have any visual interruptions, such as a black screen for a second or two. This parameter is ignored when frameRate is 0. Value is Surface.CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, or Surface.CHANGE_FRAME_RATE_ALWAYS

Returns
SurfaceControl.Transaction This transaction object. This value cannot be null.

setFrameRate

Added in API level 30
public SurfaceControl.Transaction setFrameRate (SurfaceControl sc, 
                float frameRate, 
                int compatibility)

Sets the intended frame rate for this surface. Any switching of refresh rates is most probably going to be seamless.

Parameters
sc SurfaceControl: This value cannot be null.

frameRate float: Value is 0.0 or greater

compatibility int: Value is Surface.FRAME_RATE_COMPATIBILITY_DEFAULT, or Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE

Returns
SurfaceControl.Transaction This value cannot be null.

setGeometry

Added in API level 29
Deprecated in API level 33
public SurfaceControl.Transaction setGeometry (SurfaceControl sc, 
                Rect sourceCrop, 
                Rect destFrame, 
                int orientation)

This method was deprecated in API level 33.
Use setCrop(android.view.SurfaceControl, android.graphics.Rect), setBufferTransform(android.view.SurfaceControl, int), setPosition(android.view.SurfaceControl, float, float) and setScale(android.view.SurfaceControl, float, float) instead.

Specify how the buffer associated with this Surface is mapped in to the parent coordinate space. The source frame will be scaled to fit the destination frame, after being rotated according to the orientation parameter.

Parameters
sc SurfaceControl: The SurfaceControl to specify the geometry of This value cannot be null.

sourceCrop Rect: The source rectangle in buffer space. Or null for the entire buffer.

destFrame Rect: The destination rectangle in parent space. Or null for the source frame.

orientation int: The buffer rotation Value is Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270

Returns
SurfaceControl.Transaction This transaction object. This value cannot be null.

setLayer

Added in API level 29
public SurfaceControl.Transaction setLayer (SurfaceControl sc, 
                int z)

Set the Z-order for a given SurfaceControl, relative to it's siblings. If two siblings share the same Z order the ordering is undefined. Surfaces with a negative Z will be placed below the parent surface.

Parameters
sc SurfaceControl: The SurfaceControl to set the Z order on This value cannot be null.

z int: The Z-order Value is between Integer.MIN_VALUE and Integer.MAX_VALUE inclusive

Returns
SurfaceControl.Transaction This Transaction. This value cannot be null.

setOpaque

Added in API level 33
public SurfaceControl.Transaction setOpaque (SurfaceControl sc, 
                boolean isOpaque)

Indicates whether the surface must be considered opaque, even if its pixel format is set to translucent. This can be useful if an application needs full RGBA 8888 support for instance but will still draw every pixel opaque.

This flag only determines whether opacity will be sampled from the alpha channel. Plane-alpha from calls to setAlpha() can still result in blended composition regardless of the opaque setting. Combined effects are (assuming a buffer format with an alpha channel):

  • OPAQUE + alpha(1.0) == opaque composition
  • OPAQUE + alpha(0.x) == blended composition
  • OPAQUE + alpha(0.0) == no composition
  • !OPAQUE + alpha(1.0) == blended composition
  • !OPAQUE + alpha(0.x) == blended composition
  • !OPAQUE + alpha(0.0) == no composition
If the underlying buffer lacks an alpha channel, it is as if setOpaque(true) were set automatically.

Parameters
sc SurfaceControl: The SurfaceControl to update This value cannot be null.

isOpaque boolean: true if the buffer's alpha should be ignored, false otherwise

Returns
SurfaceControl.Transaction this This value cannot be null.

setPosition

Added in API level 33
public SurfaceControl.Transaction setPosition (SurfaceControl sc, 
                float x, 
                float y)

Sets the SurfaceControl to the specified position relative to the parent SurfaceControl

Parameters
sc SurfaceControl: The SurfaceControl to change position This value cannot be null.

x float: the X position

y float: the Y position

Returns
SurfaceControl.Transaction this transaction This value cannot be null.

setScale

Added in API level 33
public SurfaceControl.Transaction setScale (SurfaceControl sc, 
                float scaleX, 
                float scaleY)

Sets the SurfaceControl to the specified scale with (0, 0) as the center point of the scale.

Parameters
sc SurfaceControl: The SurfaceControl to change scale This value cannot be null.

scaleX float: the X scale

scaleY float: the Y scale

Returns
SurfaceControl.Transaction this transaction This value cannot be null.

setVisibility

Added in API level 29
public SurfaceControl.Transaction setVisibility (SurfaceControl sc, 
                boolean visible)

Toggle the visibility of a given Layer and it's sub-tree.

Parameters
sc SurfaceControl: The SurfaceControl for which to set the visibility This value cannot be null.

visible boolean: The new visibility

Returns
SurfaceControl.Transaction This transaction object. This value cannot be null.

writeToParcel

Added in API level 30
public void writeToParcel (Parcel dest, 
                int flags)

Writes the transaction to parcel, clearing the transaction as if it had been applied so it can be used to store future transactions. It's the responsibility of the parcel reader to apply the original transaction.

Parameters
dest Parcel: parcel to write the transaction to This value cannot be null.

flags int: Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES