Can't call glGenTextures on multithreaded android app
Asked Answered
I

4

2

I'm making an OpenGLES Android app using Android NDK, expanding from android's gljni example, which can be found here

It's using GLSurfaceView. Textures are initialized in a JNI function called from onSurfaceChanged() of GLSurfaceView.Renderer

When the user touches screen, the app needs more textures. In order to do so, glGenTextures() is called in a JNI function called in onTouchEvent().

The problem is that the thread id (which gettid() returns) seems completely arbitrary and not always the same to the thread id that has the OpenGL context.

It loads and shows up textures if the JNI function is called in the same thread, but fails if it's on another thread. So it's acting quite randomly.

Can I do something like :

  • share the OpenGL context so that I can call glGenTextures() successfully on any thread.

  • make onTouchEvent() to be called in only one thread that has the OpenGl Context

  • or anything that I can make it working

?

Thanks

Irritant answered 20/8, 2010 at 2:57 Comment(0)
M
2

It's not random behaviour, that's how OpenGL interacts with threads. A context is only current on only ONE thread, other threads don't have a GL context unless you specifically create a context for each thread you want to use with OpenGL. Without a context, all GL calls fail.

Microwatt answered 20/8, 2010 at 4:32 Comment(0)
N
2

I haven't worked with the NDK and OpenGL. But with the pure Java version, you can not share threads. GLSurfaceView doesn't like sharing GL contexts between threads. The reason for this (from what I have been able to tell) is that after a drawFrame() call, the context gets lost. If you try to use that context while not inside onSurfaceCreated, onSurfaceChanged, or onDrawFrame(), your GL methods won't work. Therefore, with a different thread there is a high chance that when that other thread executes, the GL thread has already finished its drawFrame() method, invalidating the context.

Niki answered 20/8, 2010 at 3:9 Comment(1)
The NDK version is identical to the OpenGL version in this respect.Libreville
I
2

I got it working using GLSurfaceView.queueEvent().

The document says that GLSurfaceView is careful of separating UI thread and rendering thread, and

queueEvent() makes a code run in its rendering thread.

Irritant answered 20/8, 2010 at 3:13 Comment(0)
M
2

It's not random behaviour, that's how OpenGL interacts with threads. A context is only current on only ONE thread, other threads don't have a GL context unless you specifically create a context for each thread you want to use with OpenGL. Without a context, all GL calls fail.

Microwatt answered 20/8, 2010 at 4:32 Comment(0)
A
0

I described a java-only solution for uploading textures on a separate thread as an answer to another question: Threading textures load process for android opengl game

It should work fairly similar with the NDK.

Appropriation answered 2/11, 2013 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.