Skip to content Skip to sidebar Skip to footer

How To Communicate Between Threads With Android Opengl

Suppose I get a stream of pixels buffer and I want to display them with OpelnGL; For that I use GLES20.glTexImage2D. Now I want to update the image each time I get a new buffer. Ho

Solution 1:

How can I do that from the MainActivity or another class that is not aware of the OpenGL's thread?

First up - let's correct some terminology. There is no such thing as "the OpenGL thread"; if you use that terminology it's just going to get very confusing very quickly.

What you have is a rendering context from your platform specific (e.g. EAGL on iOS, EGL on Android) rendering layer. Each rendering context can be active in exactly one thread at any point in time, BUT can move threads (e.g. via a call to eglMakeCurrent for the EGL platform layer), which is why thinking of an "OpenGL thread" is such a bad idea conceptually.

Binding a context to a thread will remove any existing context's binding, and unbind the new context from other threads. In addition to moving a thread around between threads, you can also have multiple different rendering contexts bound to multiple different threads running in parallel, and that is the important thing for this question.

Data sharing like you want is achieved by having multiple rendering contexts in the same share group (see the share_context parameter to eglCreateContext if you are using EGL, for example), bound to different threads. You can create a texture in one context, upload the data, and then the other context can bind it and use it. Synchronization (e.g. so the other thread knows when it is safe to use that texture binding) must be done via an application-level synchronization protocol - it's not provided by the API.

The rules around state synchronization between multiple contexts get a bit complicated (in summary it's usually lazy, and only synchronized when a context rebinds a resource), so I'd highly recommend reading Appendix C of the OpenGL ES 2.0 spec (or equivalent in the later ES versions - it hasn't changed).

Post a Comment for "How To Communicate Between Threads With Android Opengl"