Reputation: 13357
i don't know very well openGL, but i have one context that is created in the main UI thread with:
eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglCreateWindowSurface(..)
eglCreateContext(..)
eglMakeCurrent(...)
but now i want to do also some graphic manipulation (mostly to create textures, and send later those texture to be draw in the main ui thread) in a background thread. but i don't know how to initialise the second context in the second thread (or how to use the first context in the second thread) :( maybe i must use eglCreatePbufferSurface in my second thread ? i don't know ...
thanks by advance for your help !
Upvotes: 1
Views: 1655
Reputation: 100622
Contexts are single threaded only: OpenGL is a highly modal API so allowing multiple threads simultaneously to use the same context would be painful — if thread 1 calls glBindTexture
then how does it know that it wasn't interrupted by thread 2, making a different glBindTexture
call before it gets to the next statement?
However, eglCreateContext
's third parameter is defined as:
share_context
Specifies another EGL rendering context with which to share data, as defined by the client API corresponding to the contexts. Data is also shared with all other contexts with which share_context shares data.
EGL_NO_CONTEXT
indicates that no sharing is to take place.
(a caveat in the spec warns that texture 0 isn't shareable; if you're consistently using only the results of glGenTextures
as your IDs then don't worry about it)
So your background context needs to nominate the original as a share context. It then owns its own context. It can proceed to do the expensive work of creating textures. When it's done, the texture names it has generated can be handed to the main thread and should be usable on its context.
The only thing to watch for is potential synchronisation issues between the command queue on your main thread and that on your background. Probably it's safest to call glFinish
in the background to ensure all work has completed, pass your texture ID to the foreground, then call glFlush
on that context to flush the unsubmitted command queue.
Upvotes: 1