'ANR after Opengl context loss on Live Wallpaper Preview

I'm writing about an ANR that happens every time a user switches OpenGL context during a live wallpaper preview. This ANR is present on all Android 9 devices and on very few Android 8.0 - 8.1 devices.

Reproduction steps

The reproduction is really easy given a live wallpaper developed with OpenGL and an Android 9.0 (and above) device or an Android Studio emulator (9.0 and above):

  1. Install on an Android 9.0 device a live wallpaper from the store developed with OpenGL or any framework that uses OpenGL like Unity or Libgdx, for example, the well known Double Helix Live Wallpaper or build your own through the various live wallpaper examples.

  2. Open the live wallpaper preview (it is the view where you can see the live wallpaper running but you still haven't set it as your live wallpaper)

  3. Press the home button to switch to the home screen.
  4. Press the recent apps button and resume the preview of the live wallpaper app.

At this moment the screen goes black and an ARN occurs with the message:

E/libEGL: eglCreateWindowSurface: native_window_api_connect (win=0x7e53b97010) failed (0xffffffed) (already connected to another API?)
E/libEGL: eglCreateWindowSurface:896 error 3003 (EGL_BAD_ALLOC)

After some research, I found out that the ANR happens due to OpenGL context loss, for all live wallpapers that use OpenGL (an Android bug probably) and the possible workaround is given here: Opengls eglCreateWindowSurface GL Error EGL_BAD_ALLOC

In my case, I'm using Libgdx on all my live wallpaper apps and I have no idea how to implement the workaround (assuming it works) on this framework.

As you can imagine, these ANRs are very frequent and I have something like 1000+ reports per day across my live wallpaper apps and it is really strange that still, almost nobody reported this ANR. If someone has any clue on how to proceed that would be awesome. Many thanks for all your efforts.

Here is the Logcat of the ANR:

2019-07-25 16:23:49.191 12811-12811/com.mmm.mmm I/WallpaperService: engine paused
2019-07-25 16:23:49.191 12811-12811/com.mmm.mmm I/AndroidInput: sensor listener tear down
2019-07-25 16:23:49.212 12811-13241/com.mmm.mmm W/libEGL: EGLNativeWindowType 0x7e53b97010 disconnect failed
2019-07-25 16:23:49.212 12811-12811/com.mmm.mmm D/WallpaperService: reportVisibility onVisibilityChanged visible: false
2019-07-25 16:23:49.746 12811-12851/com.mmm.mmm D/WallpaperService: dispatchAppVisibility onVisibilityChanged(): false
2019-07-25 16:23:53.095 12811-12844/com.mmm.mmm D/WallpaperService: dispatchAppVisibility onVisibilityChanged(): true
2019-07-25 16:23:53.164 12811-12811/com.mmm.mmm D/WallpaperService: updateSurface forceRelayout=false forceReport=false redrawNeeded=false myWidth=1440 myHeight=2960 fixedSize=true x = 0 y = 0 mWidth=1440 mHeight=2960 mIsSleepMode=false
2019-07-25 16:23:53.164 12811-12811/com.mmm.mmm I/WallpaperService: engine resumed
2019-07-25 16:23:53.164 12811-12811/com.mmm.mmm I/AndroidInput: sensor listener setup
2019-07-25 16:23:53.165 12811-13241/com.mmm.mmm E/libEGL: eglCreateWindowSurface: native_window_api_connect (win=0x7e53b97010) failed (0xffffffed) (already connected to another API?)
2019-07-25 16:23:53.165 12811-13241/com.mmm.mmm E/libEGL: eglCreateWindowSurface:896 error 3003 (EGL_BAD_ALLOC)

Edit 1 - The ANR happens only during live wallpaper preview and it is not present once you set the live wallpaper.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source