# Swappy for OpenGL

OpenGL part of Swappy.

## Summary

| ### Functions ||
|---|---|
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gaa8c4eac2519c1c0bede3e54d848ecd4c()` | `void` Destroy resources and stop all threads that Swappy has created. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga18cb1dbe81e18bffe0f0985a63914508()` | `uint64_t` Get the fence timeout value, in nanoseconds. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga3c654ec58fabf351fdfe8aee197b830a()` | `uint64_t` Get the refresh period value, in nanoseconds. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga108fdf9c1e6c8fd19bd0c0fa74e9680f(uint64_t *out_refreshrates, int allocated_entries)` | `int` Get the supported refresh periods of this device. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gadbe509e8b27645217de49e21878f68c2()` | `uint64_t` Get the swap interval value, in nanoseconds. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga7647d272a20306d1fffffcda6eb493fd()` | `bool` |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga5610893162789d102403c90320f0ba3f(JNIEnv *env, jobject jactivity)` | `bool` Initialize Swappy, getting the required Android parameters from the display subsystem via JNI. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga06a36d0d125f582e64f476829569d0ab()` | `bool` Check if Swappy was successfully initialized. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gad673282bbc7ceeece5697015cc625a0c(int32_t n_frames)` | `void` Set the number of bad frames to wait before applying a fix for buffer stuffing. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga651eccfaffb9ac581f9c8dfe889945ae(uint64_t fence_timeout_ns)` | `void` Set the fence timeout parameter, for devices with faulty drivers. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gac7eb8311146d072c3eedfd37baf1ffc3(uint64_t swap_ns)` | `void` Override the swap interval. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga99e635c7de56fb397bb50dd78b63f15d(bool tf)` | `void` |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gaf3de4e52c019acb03d8ce1ff51d30277(ANativeWindow *window)` | `bool` Tell Swappy which ANativeWindow to use when calling to ANativeWindow_\* API. |
| `https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga28e09bcd1a33ae584e9b9da525ac1c39(EGLDisplay display, EGLSurface surface)` | `bool` Replace calls to eglSwapBuffers with this. |

## Functions

### SwappyGL_destroy

```scdoc
void SwappyGL_destroy()
```
Destroy resources and stop all threads that Swappy has created.


**See also:**
[SwappyGL_init](https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1ga5610893162789d102403c90320f0ba3f)

### SwappyGL_getFenceTimeoutNS

```scdoc
uint64_t SwappyGL_getFenceTimeoutNS()
```
Get the fence timeout value, in nanoseconds.

### SwappyGL_getRefreshPeriodNanos

```scdoc
uint64_t SwappyGL_getRefreshPeriodNanos()
```
Get the refresh period value, in nanoseconds.

### SwappyGL_getSupportedRefreshPeriodsNS

```scdoc
int SwappyGL_getSupportedRefreshPeriodsNS(
  uint64_t *out_refreshrates,
  int allocated_entries
)
```
Get the supported refresh periods of this device.

Call once with out_refreshrates set to nullptr to get the number of supported refresh periods, then call again passing that number as allocated_entries and an array of size equal to allocated_entries that will be filled with the refresh periods.

### SwappyGL_getSwapIntervalNS

```scdoc
uint64_t SwappyGL_getSwapIntervalNS()
```
Get the swap interval value, in nanoseconds.

### SwappyGL_getUseAffinity

```scdoc
bool SwappyGL_getUseAffinity()
```

### SwappyGL_init

```scdoc
bool SwappyGL_init(
  JNIEnv *env,
  jobject jactivity
)
```
Initialize Swappy, getting the required Android parameters from the display subsystem via JNI.


**See also:**
[SwappyGL_destroy](https://developer.android.com/games/sdk/reference/frame-pacing/group/swappy-g-l#group__swappy_g_l_1gaa8c4eac2519c1c0bede3e54d848ecd4c)

| Details ||
|---|---|
| Parameters | |---|---| | `env` | The JNI environment where Swappy is used | | `jactivity` | The activity where Swappy is used | |
| **Returns** | false if Swappy failed to initialize. |

### SwappyGL_isEnabled

```scdoc
bool SwappyGL_isEnabled()
```
Check if Swappy was successfully initialized.

<br />

| Details ||
|---|---|
| **Returns** | false if either the `swappy.disable` system property is not `false` or the required OpenGL extensions are not available for Swappy to work. |

### SwappyGL_setBufferStuffingFixWait

```scdoc
void SwappyGL_setBufferStuffingFixWait(
  int32_t n_frames
)
```
Set the number of bad frames to wait before applying a fix for buffer stuffing.

Set to zero in order to turn off this feature. Default value = 0.

### SwappyGL_setFenceTimeoutNS

```scdoc
void SwappyGL_setFenceTimeoutNS(
  uint64_t fence_timeout_ns
)
```
Set the fence timeout parameter, for devices with faulty drivers.

Its default value is 50,000,000ns (50ms).

### SwappyGL_setSwapIntervalNS

```scdoc
void SwappyGL_setSwapIntervalNS(
  uint64_t swap_ns
)
```
Override the swap interval.

By default, Swappy will adjust the swap interval based on actual frame rendering time.

If an app wants to override the swap interval calculated by Swappy, it can call this function:


- This will temporarily override Swappy's frame timings but, unless `SwappyGL_setAutoSwapInterval(false)` is called, the timings will continue to be be updated dynamically, so the swap interval may change.
- This set the **minimal** interval to run. For example, `SwappyGL_setSwapIntervalNS(SWAPPY_SWAP_30FPS)` will not allow Swappy to swap faster, even if auto mode decides that it can. But it can go slower if auto mode is on.

<br />

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `swap_ns` | The new swap interval value, in nanoseconds. | |

### SwappyGL_setUseAffinity

```scdoc
void SwappyGL_setUseAffinity(
  bool tf
)
```

### SwappyGL_setWindow

```scdoc
bool SwappyGL_setWindow(
  ANativeWindow *window
)
```
Tell Swappy which ANativeWindow to use when calling to ANativeWindow_\* API.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `window` | ANativeWindow that was used to create the EGLSurface. | |
| **Returns** | true on success, false if Swappy was not initialized. |

### SwappyGL_swap

```scdoc
bool SwappyGL_swap(
  EGLDisplay display,
  EGLSurface surface
)
```
Replace calls to eglSwapBuffers with this.

Swappy will wait for the previous frame's buffer to be processed by the GPU before actually calling eglSwapBuffers.

| Details ||
|---|---|
| **Returns** | true on success or false if 1) Swappy is not initialized or 2) eglSwapBuffers did not return EGL_TRUE. In the latter case, eglGetError can be used to get the error code. |