Mad Scientist
Mad Scientist

Reputation: 18553

GLSurfaceView.queueEvent does not execute in the GL thread

I'm trying to execute some OpenGL commands for my GLSurfaceView from my main activity. As the OpenGL renderer works in its own thread, I have to use queueEvent, as far as I understand.

I'm calling queueEvent with the following code in my GLSurfaceView:

queueEvent(new Runnable(){
     @Override
     public void run() {
          renderer.doSomething(data); //executes some OpenGL commands
          requestRender();
}});

The doSomething() method binds a texture and compiles shaders.

This does not work. glCreateProgram returns 0, which happens for example when a GL command is executed outside of the GL thread. Exactly the same code also works fine if I execute it from within my renderer. So it seems that the commands I execute using queueEvent are not executed within the GL context, but are executed in the wrong thread.

Is my understanding that calling queueEvent is sufficient to execute code inside the GL thread wrong? Is there anything else I have to do, or any mistake in how I call it now?

Upvotes: 9

Views: 3318

Answers (1)

Momchil Atanasov
Momchil Atanasov

Reputation: 518

It did some experimenting and it seems that in some cases queueEvent will execute the Runnable before onSurfaceCreated is actually called, though still on the GL thread. This can happen if you are using queueEvent immediately after onResume in the Activity.

I did the experiment with glClearColor and even though it called the command without any exceptions, the background had not changed. Maybe the GLContext is still not properly available and the commands do nothing.

Hope this helps!

Upvotes: 9

Related Questions