The Android Game SDK is a new set of libraries from Google, designed to help facilitate better game development. Or at least, that is what it promises to be in the not-too-distant future. For right now, the Android Game SDK is in fact just one library: the Android Frame Pacing Library, apparently also known as “Swappy.”
The announcement came at the start of December 2019 and could be considered a long-overdue inclusion. Hopefully, future iterations will help to streamline the game development process, allowing developers to more quickly design and release puzzle games, platformers, or even 3D titles without relying on third-party engines like Unity and Unreal.
This post will be updated frequently as new elements are added to the Android Game SDK, so bookmark this page and check back. For now, we’ll be taking a look at the Frame Pacing Library.
How the Frame Pacing Library works
This library is designed to help games maintain a steady frame rate with minimal input latency. It does this by synchronizing the game’s rendering loop with the OS display subsystem and hardware. The display subsystem aims to reduce tearing that can sometimes occur when hardware switches from one frame to another mid-way through an update. It does this by buffering previous frames, detecting late submissions, and repeating the display of those past frames should a late frame be detected.
However, this can allow mismatches in synchronization that result in inconsistent frame display times. For example, if a frame gets rendered more quickly, this might shorten the amount of time the previous frame spends on the screen. Should frames take too long to render however, then it may be presented for an additional frame.
The Frame Pacing Library addresses these issues using Android’s Choreographer API in order to synchronize the game with the display subsystem. It achieves this by using a presentation timestamp extension on OpenGL and Vulkan APIs, which ensures that frames are presented at the correct time. It also uses sync fences in order to prevent buffer stuffing. Multiple refresh rates are supported too, allowing developers to target different device types – including 90Hz and 120Hz displays – and customization options. Frame presentation times are automatically adjusted to account for the device hardware.
How to use the Android Game SDK’s Frame Pacing Library
If your game uses either Vulkan on OpenGL, then you will be able to utilize this library. To do so, first you will need to download the Android Game SDK here.
To see how this works, you can also download the Bouncy Ball sample here to test with OpenGL. You can find instructions for Vulkan here. Open the project, then run the app to make sure it works.
Next, link the project to the library. For static libraries, do this by adding gamesdk/include to compiler include paths, and swappy/swappyGL.h for integration with OpenGL ES. In most cases, the header file will contain all necessary functions.
Finally, add the following path to your linker library paths:
gamesdk/libs/architecture_APIapiLevel_NDKndkVersion_stlVersion_Release
Of course, you will swap the bold text for the relevant processor/NDK version etc.
Initialize an instance of Android Frame Pacing using:
void SwappyGL_init(JNIEnv *env, jobject jactivity);
And destroy said instance with:
void SwappyGL_destroy();
Now the following functions will enable you to configure your swap intervals and refresh periods:
void SwappyGL_setSwapIntervalNS(uint64_t swap_ns); void SwappyGL_setFenceTimeoutNS(uint64_t fence_timeout_ns); void SwappyGL_setUseAffinity(bool tf);
Call these immediately after you have initialized SwappyGL_init(), which should be as close to when your engine launches as possible. Pass the duration of the frame into SwappyGL_setSwapIntervalNS() using the SWAPPY_SWAP_60FPS, SWAPPY_SWAP_30FPS, or SWAPPY_SWAP_20FPS constants (where appropriate).
Now perform the frame swap using bool SwappyGL_swap(EGLDisplay display, EGLSurface surface). This wraps the eglSwapBuffers() method used by Open GL ES, so you should replace all instances with the new version.
You can check that Frame Pacing has been enabled at any point using bool SwappyGL_isEnabled().
For more detailed instructions on how to use the Frame Pacing Library, check out its official Android developer guide page.
Using Android Game SDK in Unity
The Frame Pacing Library is also included in Unity version 2019.2 and above. Simply select the optimized Frame Pacing checkbox in Android Settings, and you’ll automatically enable smoother frame rates for your games.
Once again, Unity makes life a lot easier for game developers!
Looking ahead
Personally, I feel it’s high time that Google gave game devs some love, so I see this as a very positive thing. The Frame Pacing Library itself is also likely to be a welcome addition, especially for more demanding games looking to offer silky-smooth framerates. It’s a small start though, so I have my fingers crossed for some more broadly useful libraries making their debut with Android Game SDK soon.
More posts about Android development
Via Phandroid