Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Android OpenGL in Background as Rendering Resource for App?

I want to run an Android OpenGL-ES surface in the background of my app to provide dynamic graphical resources (bitmaps, etc.) for the application. The OpenGl GLSurfaceView is not the visible screen of my app; it is only used as a rendering factory to manufacture graphics for use by the main activity.

Does anyone know how to make an Android GLSurfaceView run when it is not an active view? The Google Android documentation is clear that when a GLSurfaceView loses its status as a primary view, the surface is destroyed along with all of its resources.

Is there any way to keep a GLSurfaceView and its associated GLSurfaceView.Renderer running normally when it is not (and never is) the application's primary view?

like image 271
Androider Avatar asked Jun 29 '11 16:06

Androider


2 Answers

For what you're trying to do, you should skip the GLSurfaceView entirely. Since you just want to do some OpenGL offscreen rendering, you can make use of an EGL pixel buffer and convert it to a bitmap. See this thread on anddev for a solution. I've used it very successfully in a few of my apps.

The implementation towards the bottom of that thread is a bit buggy, since it does some very simple EGL choosing. I'd recommend implementing a more robust chooser, which you can find in Robert Green's GLWallpaperService, if you'd like.

Another change I ended up making was to remove the vertical pixel mirroring. I just ended up flipping my viewport in onSurfaceCreate, rather than manually flip rendered pixels when writing to the bitmap.

Good luck!

like image 161
Josh Avatar answered Sep 29 '22 11:09

Josh


I would strongly recommend you don't try to do this. Many current drivers on Android devices don't support multiple active GL contexts across processes; if the driver does support this, the feature has not been exercised much because Android itself does not do this, so there are likely to be nasty bugs that result.

Multiple GL context is only being used by the platform starting with Android 3.0.

like image 40
hackbod Avatar answered Sep 29 '22 10:09

hackbod