Swappy for OpenGL

OpenGL part of Swappy.

Summary

Functions

SwappyGL_destroy()
void
Destroy resources and stop all threads that Swappy has created.
SwappyGL_getFenceTimeoutNS()
uint64_t
Get the fence timeout value, in nanoseconds.
SwappyGL_getRefreshPeriodNanos()
uint64_t
Get the refresh period value, in nanoseconds.
SwappyGL_getSupportedRefreshPeriodsNS(uint64_t *out_refreshrates, int allocated_entries)
int
Get the supported refresh periods of this device.
SwappyGL_getSwapIntervalNS()
uint64_t
Get the swap interval value, in nanoseconds.
SwappyGL_getUseAffinity()
bool
SwappyGL_init(JNIEnv *env, jobject jactivity)
bool
Initialize Swappy, getting the required Android parameters from the display subsystem via JNI.
SwappyGL_isEnabled()
bool
Check if Swappy was successfully initialized.
SwappyGL_setBufferStuffingFixWait(int32_t n_frames)
void
Set the number of bad frames to wait before applying a fix for buffer stuffing.
SwappyGL_setFenceTimeoutNS(uint64_t fence_timeout_ns)
void
Set the fence timeout parameter, for devices with faulty drivers.
SwappyGL_setSwapIntervalNS(uint64_t swap_ns)
void
Override the swap interval.
SwappyGL_setUseAffinity(bool tf)
void
SwappyGL_setWindow(ANativeWindow *window)
bool
Tell Swappy which ANativeWindow to use when calling to ANativeWindow_* API.
SwappyGL_swap(EGLDisplay display, EGLSurface surface)
bool
Replace calls to eglSwapBuffers with this.

Functions

SwappyGL_destroy

void SwappyGL_destroy()

Destroy resources and stop all threads that Swappy has created.

See also: SwappyGL_init

SwappyGL_getFenceTimeoutNS

uint64_t SwappyGL_getFenceTimeoutNS()

Get the fence timeout value, in nanoseconds.

SwappyGL_getRefreshPeriodNanos

uint64_t SwappyGL_getRefreshPeriodNanos()

Get the refresh period value, in nanoseconds.

SwappyGL_getSupportedRefreshPeriodsNS

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

uint64_t SwappyGL_getSwapIntervalNS()

Get the swap interval value, in nanoseconds.

SwappyGL_getUseAffinity

bool SwappyGL_getUseAffinity()

SwappyGL_init

bool SwappyGL_init(
  JNIEnv *env,
  jobject jactivity
)

Initialize Swappy, getting the required Android parameters from the display subsystem via JNI.

See also: SwappyGL_destroy

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

bool SwappyGL_isEnabled()

Check if Swappy was successfully initialized.

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

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

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

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.

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

SwappyGL_setUseAffinity

void SwappyGL_setUseAffinity(
  bool tf
)

SwappyGL_setWindow

bool SwappyGL_setWindow(
  ANativeWindow *window
)

Tell Swappy which ANativeWindow to use when calling to ANativeWindow_* API.

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

SwappyGL_swap

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.