Added in API level 1

Surface


open class Surface : Parcelable
kotlin.Any
   ↳ android.view.Surface

Handle onto a raw buffer that is being managed by the screen compositor.

A Surface is generally created by or from a consumer of image buffers (such as a android.graphics.SurfaceTexture, android.media.MediaRecorder, or android.renderscript.Allocation), and is handed to some kind of producer (such as OpenGL, MediaPlayer, or android.hardware.camera2.CameraDevice#createCaptureSession) to draw into.

Note: A Surface acts like a weak reference to the consumer it is associated with. By itself it will not keep its parent consumer from being reclaimed.

Summary

Nested classes
open

Exception thrown when a Canvas couldn't be locked with Surface.lockCanvas, or when a SurfaceTexture could not successfully be allocated.

Constants
static Int

Change the frame rate even if the transition is going to be non-seamless, i.

static Int

Change the frame rate only if the transition is going to be seamless.

static Int

The surface requests a frame rate that is at least the specified frame rate.

static Int

There are no inherent restrictions on the frame rate of this surface.

static Int

This surface is being used to display content with an inherently fixed frame rate, e.

static Int

Rotation constant: 0 degree rotation (natural orientation)

static Int

Rotation constant: 180 degree rotation.

static Int

Rotation constant: 270 degree rotation.

static Int

Rotation constant: 90 degree rotation.

Inherited constants
Public constructors
Surface(surfaceTexture: SurfaceTexture!)

Create Surface from a SurfaceTexture.

Create a Surface associated with a given SurfaceControl.

Public methods
open Unit

Clears the frame rate which was set for this surface.

open Int

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

open Boolean

Check if CPU throttling is enabled.

open Boolean

Returns true if this object holds a valid surface.

open Canvas!
lockCanvas(inOutDirty: Rect!)

Gets a Canvas for drawing into this surface.

open Canvas!

Gets a Canvas for drawing into this surface.

open Unit

open Unit

Release the local reference to the server-side surface.

open Unit
setFrameRate(frameRate: Float, compatibility: Int)

Sets the intended frame rate for this surface.

open Unit
setFrameRate(frameRate: Float, compatibility: Int, changeFrameRateStrategy: Int)

Sets the intended frame rate for this surface.

open Unit

Control CPU throttling for Vulkan/EGL producers.

open String

Returns a string representation of the object.

open Unit
unlockCanvas(canvas: Canvas!)

open Unit

Posts the new contents of the Canvas to the surface and releases the Canvas.

open Unit
writeToParcel(dest: Parcel, flags: Int)

Flatten this object in to a Parcel.

Protected methods
open Unit

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Properties
static Parcelable.Creator<Surface!>

Constants

CHANGE_FRAME_RATE_ALWAYS

Added in API level 31
static val CHANGE_FRAME_RATE_ALWAYS: Int

Change the frame rate even if the transition is going to be non-seamless, i.e. with visual interruptions for the user. Non-seamless switches might be used when the benefit of matching the content's frame rate outweighs the cost of the transition, for example when displaying long-running video content.

Value: 1

CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS

Added in API level 31
static val CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS: Int

Change the frame rate only if the transition is going to be seamless.

Value: 0

FRAME_RATE_COMPATIBILITY_AT_LEAST

Added in API level 36
static val FRAME_RATE_COMPATIBILITY_AT_LEAST: Int

The surface requests a frame rate that is at least the specified frame rate. This value should be used for UIs, animations, scrolling and fling, and anything that is not a game or video. For video, use FRAME_RATE_COMPATIBILITY_FIXED_SOURCE instead. For game content, use FRAME_RATE_COMPATIBILITY_DEFAULT.

Value: 2

FRAME_RATE_COMPATIBILITY_DEFAULT

Added in API level 30
static val FRAME_RATE_COMPATIBILITY_DEFAULT: Int

There are no inherent restrictions on the frame rate of this surface. When the system selects a frame rate other than what the app requested, the app will be able to run at the system frame rate without requiring pull down. This value should be used when displaying game content, UIs, and anything that isn't video. In Android version Build.VERSION_CODES.BAKLAVA and above, use FRAME_RATE_COMPATIBILITY_DEFAULT for game content. For other cases, see FRAME_RATE_COMPATIBILITY_FIXED_SOURCE and FRAME_RATE_COMPATIBILITY_AT_LEAST.

Value: 0

FRAME_RATE_COMPATIBILITY_FIXED_SOURCE

Added in API level 30
static val FRAME_RATE_COMPATIBILITY_FIXED_SOURCE: Int

This surface is being used to display content with an inherently fixed frame rate, e.g. a video that has a specific frame rate. When the system selects a frame rate other than what the app requested, the app will need to do pull down or use some other technique to adapt to the system's frame rate. The user experience is likely to be worse (e.g. more frame stuttering) than it would be if the system had chosen the app's requested frame rate. This value should be used for video content.

Value: 1

ROTATION_0

Added in API level 1
static val ROTATION_0: Int

Rotation constant: 0 degree rotation (natural orientation)

Value: 0

ROTATION_180

Added in API level 1
static val ROTATION_180: Int

Rotation constant: 180 degree rotation.

Value: 2

ROTATION_270

Added in API level 1
static val ROTATION_270: Int

Rotation constant: 270 degree rotation.

Value: 3

ROTATION_90

Added in API level 1
static val ROTATION_90: Int

Rotation constant: 90 degree rotation.

Value: 1

Public constructors

Surface

Added in API level 14
Surface(surfaceTexture: SurfaceTexture!)

Create Surface from a SurfaceTexture. Images drawn to the Surface will be made available to the SurfaceTexture, which can attach them to an OpenGL ES texture via SurfaceTexture.updateTexImage. Please note that holding onto the Surface created here is not enough to keep the provided SurfaceTexture from being reclaimed. In that sense, the Surface will act like a weak reference to the SurfaceTexture.

Parameters
surfaceTexture SurfaceTexture!: The SurfaceTexture that is updated by this Surface.
Exceptions
android.view.Surface.OutOfResourcesException if the surface could not be created.

Surface

Added in API level 29
Surface(from: SurfaceControl)

Create a Surface associated with a given SurfaceControl. Buffers submitted to this surface will be displayed by the system compositor according to the parameters specified by the control. Multiple surfaces may be constructed from one SurfaceControl, but only one can be connected (e.g. have an active EGL context) at a time.

Parameters
from SurfaceControl: The SurfaceControl to associate this Surface with.
This value cannot be null.

Public methods

clearFrameRate

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

Clears the frame rate which was set for this surface.

This is equivalent to calling setFrameRate(float,int,int) using 0 for frameRate.

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.

describeContents

Added in API level 1
open fun describeContents(): Int

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.

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

isProducerThrottlingEnabled

Added in API level 1
open fun isProducerThrottlingEnabled(): Boolean

Check if CPU throttling is enabled.

Return
Boolean true if back-pressure is enabled, false otherwise.
Exceptions
java.lang.IllegalArgumentException If the native window is invalid.

isValid

Added in API level 1
open fun isValid(): Boolean

Returns true if this object holds a valid surface.

Return
Boolean True if it holds a physical surface, so lockCanvas() will succeed. Otherwise returns false.

lockCanvas

Added in API level 1
open fun lockCanvas(inOutDirty: Rect!): Canvas!

Gets a Canvas for drawing into this surface. After drawing into the provided Canvas, the caller must invoke unlockCanvasAndPost to post the new contents to the surface.

Parameters
inOutDirty Rect!: A rectangle that represents the dirty region that the caller wants to redraw. This function may choose to expand the dirty rectangle if for example the surface has been resized or if the previous contents of the surface were not available. The caller must redraw the entire dirty region as represented by the contents of the inOutDirty rectangle upon return from this function. The caller may also pass null instead, in the case where the entire surface should be redrawn.
Return
Canvas! A canvas for drawing into the surface.
Exceptions
android.view.Surface.OutOfResourcesException If the canvas cannot be locked.
java.lang.IllegalArgumentException If the inOutDirty rectangle is not valid.

lockHardwareCanvas

Added in API level 23
open fun lockHardwareCanvas(): Canvas!

Gets a Canvas for drawing into this surface. After drawing into the provided Canvas, the caller must invoke unlockCanvasAndPost to post the new contents to the surface. Unlike lockCanvas(android.graphics.Rect) this will return a hardware-accelerated canvas. See the unsupported drawing operations for a list of what is and isn't supported in a hardware-accelerated canvas. It is also required to fully cover the surface every time lockHardwareCanvas() is called as the buffer is not preserved between frames. Partial updates are not supported.

Return
Canvas! A canvas for drawing into the surface.
Exceptions
java.lang.IllegalStateException If the canvas cannot be locked.

readFromParcel

Added in API level 1
open fun readFromParcel(source: Parcel!): Unit

release

Added in API level 14
open fun release(): Unit

Release the local reference to the server-side surface. Always call release() when you're done with a Surface. This will make the surface invalid.

setFrameRate

Added in API level 30
open fun setFrameRate(
    frameRate: Float,
    compatibility: Int
): Unit

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

Parameters
frameRate Float: Value is 0.0f or greater
compatibility Int: Value is one of the following:

setFrameRate

Added in API level 31
open fun setFrameRate(
    frameRate: Float,
    compatibility: Int,
    changeFrameRateStrategy: Int
): Unit

Sets the intended frame rate for this surface.

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 introduce frame rate throttling, or affect other aspects of 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
frameRate Float: The intended frame rate of 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 parameter 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.0f 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 one of the following:
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 one of the following:
Exceptions
java.lang.IllegalArgumentException If frameRate, compatibility or changeFrameRateStrategy are invalid.

setProducerThrottlingEnabled

Added in API level 1
open fun setProducerThrottlingEnabled(enabled: Boolean): Unit

Control CPU throttling for Vulkan/EGL producers.

By default Vulkan and EGL producers are CPU throttled when they queue a buffer and the consumer is still processing the previous buffer. In practice, it means that eglSwapBuffers() or vkPresentKHR() calls will stall the CPU until the GPU is done processing the previous frame. This API allows to disable this throttling while queueing a buffer.

While the default it to have throttling enabled, the more correct and efficient behavior is to have it disabled. Unfortunately, some Vulkan applications may inadvertently rely on this stall which effectively behaves as consumer/producer synchronization, albeit, inefficiently. It is therefore recommended to always disable throttling and perform proper synchronization in Vulkan.

If the CPU produces frames faster than the GPU, natural throttling will happen when a buffer is dequeued, based on the size of the queue. This typically happen during the first drawing in OpenGL ES and in vkAcquireNextImageKHR() in Vulkan.

This API has no effect in asynchronous mode, where throttling is always enabled.

Parameters
enabled Boolean: true to enable back-pressure, false to disable it.
Exceptions
java.lang.IllegalArgumentException If the native window is invalid.

toString

Added in API level 1
open fun toString(): String

Returns a string representation of the object.

Return
String a string representation of the object.

unlockCanvas

Added in API level 1
Deprecated in API level 17
open fun unlockCanvas(canvas: Canvas!): Unit

Deprecated: This API has been removed and is not supported. Do not use.

unlockCanvasAndPost

Added in API level 1
open fun unlockCanvasAndPost(canvas: Canvas!): Unit

Posts the new contents of the Canvas to the surface and releases the Canvas.

Parameters
canvas Canvas!: The canvas previously obtained from lockCanvas.

writeToParcel

Added in API level 1
open fun writeToParcel(
    dest: Parcel,
    flags: Int
): Unit

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written.
This value cannot be null.
flags Int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.
Value is either 0 or a combination of the following:

Protected methods

finalize

Added in API level 1
protected open fun finalize(): Unit

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Exceptions
java.lang.Throwable the Exception raised by this method

Properties

CREATOR

Added in API level 1
static val CREATOR: Parcelable.Creator<Surface!>