Question

I would like to do all of my rendering on a background thread. Currently, I have this working on iOS using a CAEAGLLayer in a UIView subclass, and then doing all of the OpenGL spin-up on the background thread (including binding the layer via...:

[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];

...) However, when I try this on Android, the Apportable compatibility layer triggers errors in EGL because it is trying to use an EGL Surface from another thread...:

09-24 12:25:04.667    2622-2661/com.apportable.Spin E/EglHelper﹕ eglSwapBuffers returned 12301. tid=1535
09-24 12:25:04.677    2622-2661/com.apportable.Spin W/Adreno200-EGL﹕ <qeglDrvAPI_eglSwapBuffers:3415>: EGL_BAD_SURFACE

How can I do my rendering on a BG thread? Is there any Apportable thread documentation (e.g. is the Android UI thread used to run the iOS main thread? or is that a separate thread?)

Was it helpful?

Solution

  1. Create the view with the CAEAGLLayer and create the EAGLContext on the main thread

  2. Create shared EAGLContexts to be used for background threads via the sharegroup values from the primary context

  3. Make those shared contexts current on the background threads

  4. Be VERY careful about using libdispatch and contexts (ideally you should use the NSThread methods for running things on the main thread) Specifically, beware the optimization documented for dispatch_sync.

The Apportable threading model is a bit non-standard compared to the common Android one. This is mainly due to the fact that iOS runs OpenGL in it's main thread. The thread that your application is started on is the same thread that the initial OpenGL surface is created on. The interesting ramification to this is that our Android based views are created and managed in the GL thread as well (by some clever manipulations of the Looper/MessageQueue and Dialog constructs). As long as the first OpenGL context that you reference in Objective-C is on the main thread; most things for shared contexts should work as expected. However if this is not the case things could devolve very quickly. Ideally you should create shared contexts to swap to background threads based off of the primary context created from the main thread.

More at the Apportable discussion group

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top