# SyncFenceCompat

Artifact: [androidx.graphics:graphics-core](https://developer.android.com/jetpack/androidx/releases/graphics) [View Source](https://cs.android.com/search?q=file:androidx/hardware/SyncFenceCompat.kt+class:androidx.hardware.SyncFenceCompat) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

*** ** * ** ***


```
public final class SyncFenceCompat implements AutoCloseable
```

<br />

*** ** * ** ***

A synchronization primitive which signals when hardware units have completed work on a particular resource. They initially start in an unsignaled state and make a one-time transaction to either a signaled or error state.

`https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` is a presentation fence used in combination with `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat.Transaction#setBuffer(androidx.graphics.surface.SurfaceControlCompat,android.hardware.HardwareBuffer,androidx.hardware.SyncFenceCompat,kotlin.Function1)`. Note that depending on API level, this will utilize either `https://developer.android.com/reference/android/hardware/SyncFence.html` or a compatibility implementation.

## Summary

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#SIGNAL_TIME_INVALID()` An invalid signal time. |
| `static final long` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#SIGNAL_TIME_PENDING()` A pending signal time. |

| ### Public methods |
|---|---|
| `final boolean` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#await(kotlin.Long)(long timeoutNanos)` Waits for a `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` to signal for up to the timeout duration |
| `final boolean` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#awaitForever()()` Waits forever for a `https://developer.android.com/reference/androidx/hardware/SyncFenceImpl` to signal |
| `void` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#close()()` Close the `https://developer.android.com/reference/androidx/hardware/SyncFenceImpl` |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#createNativeSyncFence()()` Creates a native synchronization fence from an EGLSync object. |
| `final long` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#getSignalTimeNanos()()` Returns the time that the fence signaled in the `CLOCK_MONOTONIC` time domain. |
| `final boolean` | `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#isValid()()` Checks if the SyncFence object is valid. |

## Constants

### SIGNAL_TIME_INVALID

```
public static final long SIGNAL_TIME_INVALID
```

An invalid signal time. Represents either the signal time for a SyncFence that isn't valid (that is, `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#isValid()` is `false`), or if an error occurred while attempting to retrieve the signal time.

### SIGNAL_TIME_PENDING

```
public static final long SIGNAL_TIME_PENDING
```

A pending signal time. This is equivalent to the max value of a long, representing an infinitely far point in the future.

## Public methods

### await

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
public final boolean await(long timeoutNanos)
```

Waits for a `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` to signal for up to the timeout duration

| Parameters |
|---|---|
| `long timeoutNanos` | time in nanoseconds to wait for before timing out. |

### awaitForever

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
public final boolean awaitForever()
```

Waits forever for a `https://developer.android.com/reference/androidx/hardware/SyncFenceImpl` to signal

### close

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
public void close()
```

Close the `https://developer.android.com/reference/androidx/hardware/SyncFenceImpl`

### createNativeSyncFence

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
public static final @NonNull SyncFenceCompat createNativeSyncFence()
```

Creates a native synchronization fence from an EGLSync object.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if EGL dependencies cannot be resolved |

### getSignalTimeNanos

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
@RequiresApi(value = 26)
public final long getSignalTimeNanos()
```

Returns the time that the fence signaled in the `CLOCK_MONOTONIC` time domain. This corresponds to `https://developer.android.com/reference/java/lang/System.html#nanoTime()` but may also be compared to `https://developer.android.com/reference/android/os/SystemClock.html#uptimeMillis()` after adjusting for milliseconds vs. nanoseconds.

This returns an instant, `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#SIGNAL_TIME_INVALID()` if the SyncFence is invalid, and if the fence hasn't yet signaled, then `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat#SIGNAL_TIME_PENDING()` is returned.

### isValid

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
public final boolean isValid()
```

Checks if the SyncFence object is valid.

| Returns |
|---|---|
| `boolean` | `true` if it is valid, `false` otherwise |