Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent GLSurfaceView from recreating the the OpenGL surface frequently (on Android)?

I have a an app whose Main activity shows a GLSurfaceView. Every time that a new activity is launched, say for example Settings Activity, the OpenGl surface is destroyed, and a new one is created when the user returns to the main activity.

This is VERY slow, as I need to regenerate textures each time so that they can be bound to the new Surface. (Caching the textures is possible, but it would be my second choice, because of limited availability of memory.)

Is there a way to prevent the Surface being recreated every time?


My own analysis by looking at the code is:

There are two triggers for the Surface to be destroyed:

  1. GLSurfaceView.onPause() is called by the activity
  2. The view gets detached from the window

Is there a way to prevent #2 from happening when launching a new activity?

like image 661
HRJ Avatar asked Oct 20 '25 01:10

HRJ


1 Answers

If you're targeting 3.0 or later, take a look at GLSurfaceView.setPreserveEGLContextOnPause(). Keep in mind that devices might still support only one OpenGL context at a time, so if you're switching to another activity that uses OpenGL and back to yours, you will have to reupload anyway – so I'd recommend keeping a cache and dropping it when your Activity's onLowMemory() is called.

like image 144
thakis Avatar answered Oct 22 '25 15:10

thakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!