thread - eglmakecurrent




Android: Synchronization with OpenGL renderer thread (2)

I am using OpenGL ES to perform drawing in an Android app.

I want to write a function called from the UI thread which would call the renderer thread and get it to read and return the image to the caller.

I was thinking about using a Future so that the UI thread would sleep while the renderer thread writes the image data (that operation is very fast, so the user would not feel like the app became unresponsive).

But I am at a loss about how to submit the Future to the renderer thread. All the examples I found create their own threads and then submit the future to these threads. The documentation about GLSurfaceView mentions "The queueEvent() method is used to safely communicate between the UI thread and the rendering thread. If you prefer, you can use some other Java cross-thread communication technique, such as synchronized methods on the Renderer class itself." so it looks like using a Future instead of calling queueEvent() is possible, but I have no idea how to do that.


I'd suggest not extending GLSurfaceView, but instead doing asynchronous communication between threads. See my answer here for a couple of examples

Essentially you queue tasks in your implementation of GLSurfaceView.Renderer via method calls to it. Then, when the GL thread calls your onDraw() method in the Renderer, you execute those queued tasks. When finishing the tasks, indicate to the other threads that the tasks are completed using Handler

Also do not sleep the UI thread ever. If the UI thread gets put to sleep, the android OS gets no response from it and misinterprets that the app has crashed, (is caught in an infinite loop or something) and will throw up a Force close dialogue to the user


Thy this, I did not test it, so might not work:

FutureTask<YourReturnObject> futureTask = new FutureTask<YourReturnObject>(new Callable<YourReturnObject>() {
    @Override
    public YourReturnObject call() throws Exception {
        //your gl stuff here
        return // result
    }
});
glSurfaceView.queueEvent(futureTask);
YourReturnObject result=futureTask.get(); // should block until gl thread is done.