From 3ba2e3aa36b5cbc12ee6f5e46537c0f5f2fc255a Mon Sep 17 00:00:00 2001 From: Jelle Foks Date: Thu, 25 Apr 2024 10:29:04 -0700 Subject: [PATCH] [Android] Stop calling eglSwapBuffers immediately. Since we're seeing ANRs at SCREEN_OFF where the rasterizer thread appears to be hanging during eglSwapBuffers, this change attempts to avoid those ANRs by immediately skip calls to eglSwapBuffers when a surfaceDestroyed event is received, until a the next SurfaceCreated. Since Cobalt uses GameActivity, those are received in calls to OnNativeWindowDestroyed respectively OnNativeWindowCreated. b/225209442 --- starboard/android/shared/android_main.cc | 5 +++++ starboard/android/shared/egl_swap_buffers.cc | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/starboard/android/shared/android_main.cc b/starboard/android/shared/android_main.cc index ce7e75ae9ea3..514bc8d28f39 100644 --- a/starboard/android/shared/android_main.cc +++ b/starboard/android/shared/android_main.cc @@ -38,6 +38,9 @@ namespace starboard { namespace android { namespace shared { + +atomic_bool g_block_swapbuffers; + namespace { using ::starboard::shared::starboard::CommandLine; @@ -314,6 +317,7 @@ void OnWindowFocusChanged(GameActivity* activity, bool focused) { } void OnNativeWindowCreated(GameActivity* activity, ANativeWindow* window) { + g_block_swapbuffers.store(false); if (g_app_running.load()) { ApplicationAndroid::Get()->SendAndroidCommand( AndroidCommand::kNativeWindowCreated, window); @@ -321,6 +325,7 @@ void OnNativeWindowCreated(GameActivity* activity, ANativeWindow* window) { } void OnNativeWindowDestroyed(GameActivity* activity, ANativeWindow* window) { + g_block_swapbuffers.store(true); if (g_app_running.load()) { ApplicationAndroid::Get()->SendAndroidCommand( AndroidCommand::kNativeWindowDestroyed); diff --git a/starboard/android/shared/egl_swap_buffers.cc b/starboard/android/shared/egl_swap_buffers.cc index ed9d8fc1b259..8cc824127f3b 100644 --- a/starboard/android/shared/egl_swap_buffers.cc +++ b/starboard/android/shared/egl_swap_buffers.cc @@ -16,14 +16,25 @@ #include #include "starboard/android/shared/video_window.h" +#include "starboard/common/atomic.h" #include "starboard/shared/gles/gl_call.h" +namespace starboard { +namespace android { +namespace shared { +extern atomic_bool g_block_swapbuffers; +} // namespace shared +} // namespace android +} // namespace starboard + extern "C" { EGLBoolean __real_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface); // This needs to be exported to ensure shared_library targets include it. SB_EXPORT_PLATFORM EGLBoolean __wrap_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { + if (starboard::android::shared::g_block_swapbuffers.load()) + return; // Kick off the GPU while waiting for new player bounds to take effect. GL_CALL(glFlush());