ViewfinderSurfaceRequest


public final class ViewfinderSurfaceRequest


The request to get a Surface to display viewfinder input.

This request contains requirements for the surface resolution and viewfinder input and output information.

Calling ViewfinderSurfaceRequest.markSurfaceSafeToRelease will notify the surface provider that the surface is not needed and related resources can be released.

Creates a new surface request with surface resolution, viewfinder input and output information.

Summary

Nested types

Builder for ViewfinderSurfaceRequest.

Result of providing a surface to a ViewfinderSurfaceRequest via .provideSurface.

Constants

static final int

Horizontal mirror transform needs to be applied to the viewfinder output.

static final int

No mirror transform needs to be applied to the viewfinder output.

Public methods

final ImplementationMode

The {@link ImplementationMode} to apply to the viewfinder.

final int

The viewfinder output mirror mode.

final @NonNull Size

The requested surface resolution.

final int

The viewfinder source orientation.

final @NonNull Surface

Retrieves the Surface provided to the ViewfinderSurfaceRequest.

final @NonNull ListenableFuture<@NonNull Surface>

Retrieves the Surface provided to the ViewfinderSurfaceRequest.

final void

Closes the viewfinder surface to mark it as safe to release.

final void
provideSurface(
    @NonNull Surface surface,
    @NonNull Executor executor,
    @NonNull Consumer<ViewfinderSurfaceRequest.Result> resultListener
)

Completes the request for a Surface if it has not already been completed or cancelled.

final boolean

Signals that the request will never be fulfilled.

Constants

MIRROR_MODE_HORIZONTAL

public static final int MIRROR_MODE_HORIZONTAL = 1

Horizontal mirror transform needs to be applied to the viewfinder output.

The mirror transform should be applied in display coordinate.

MIRROR_MODE_NONE

public static final int MIRROR_MODE_NONE = 0

No mirror transform needs to be applied to the viewfinder output.

Public methods

getImplementationMode

Added in 1.4.0-alpha11
public final ImplementationMode getImplementationMode()

The {@link ImplementationMode} to apply to the viewfinder.

getOutputMirrorMode

Added in 1.4.0-alpha11
public final int getOutputMirrorMode()

The viewfinder output mirror mode.

getResolution

Added in 1.4.0-alpha11
public final @NonNull Size getResolution()

The requested surface resolution.

getSourceOrientation

Added in 1.4.0-alpha11
public final int getSourceOrientation()

The viewfinder source orientation.

getSurface

public final @NonNull Surface getSurface()

Retrieves the Surface provided to the ViewfinderSurfaceRequest.

This can be used to get access to the Surface that's provided to the ViewfinderSurfaceRequest.

If the application is shutting down and a Surface will never be provided, this will throw a kotlinx.coroutines.CancellationException.

The returned Surface must not be used after markSurfaceSafeToRelease has been called.

getSurfaceAsync

Added in 1.4.0-alpha11
public final @NonNull ListenableFuture<@NonNull SurfacegetSurfaceAsync()

Retrieves the Surface provided to the ViewfinderSurfaceRequest.

This can be used to get access to the Surface that's provided to the ViewfinderSurfaceRequest.

If the application is shutting down and a Surface will never be provided, the ListenableFuture will fail with a CancellationException.

The returned Surface must not be used after markSurfaceSafeToRelease has been called.

markSurfaceSafeToRelease

Added in 1.4.0-alpha11
public final void markSurfaceSafeToRelease()

Closes the viewfinder surface to mark it as safe to release.

This method should be called by the user when the requested surface is not needed and related resources can be released.

provideSurface

Added in 1.4.0-alpha11
public final void provideSurface(
    @NonNull Surface surface,
    @NonNull Executor executor,
    @NonNull Consumer<ViewfinderSurfaceRequest.Result> resultListener
)

Completes the request for a Surface if it has not already been completed or cancelled.

Once the producer no longer needs the provided surface, the resultListener will be invoked with a Result containing Result.RESULT_SURFACE_USED_SUCCESSFULLY. At this point it is safe to release the surface and any underlying resources. Releasing the surface before receiving this signal may cause undesired behavior on lower API levels.

If the request is cancelled by the producer before successfully attaching the provided surface to the producer, then the resultListener will be invoked with a Result containing Result.RESULT_REQUEST_CANCELLED.

If the request was previously completed via .willNotProvideSurface, then resultListener will be invoked with a Result containing Result.RESULT_WILL_NOT_PROVIDE_SURFACE.

Upon returning from this method, the surface request is guaranteed to be complete. However, only the resultListener provided to the first invocation of this method should be used to track when the provided Surface is no longer in use by the producer, as subsequent invocations will always invoke the resultListener with a Result containing Result.RESULT_SURFACE_ALREADY_PROVIDED.

Parameters
@NonNull Surface surface

The surface which will complete the request.

@NonNull Executor executor

Executor used to execute the resultListener.

@NonNull Consumer<ViewfinderSurfaceRequest.Result> resultListener

Listener used to track how the surface is used by the producer in response to being provided by this method.

willNotProvideSurface

Added in 1.4.0-alpha11
public final boolean willNotProvideSurface()

Signals that the request will never be fulfilled.

This may be called in the case where the application may be shutting down and a surface will never be produced to fulfill the request.

This will be called by the producer as soon as it is known that the request will not be fulfilled. Failure to complete the SurfaceRequest via willNotProvideSurface() or .provideSurface may cause long delays in shutting down the producer.

Upon returning from this method, the request is guaranteed to be complete, regardless of the return value. If the request was previously successfully completed by .provideSurface, invoking this method will return false, and will have no effect on how the surface is used by the producer.

Returns
boolean

true if this call to willNotProvideSurface() successfully completes the request, i.e., the request has not already been completed via .provideSurface or by a previous call to willNotProvideSurface() and has not already been cancelled by the producer.