What parts of UIKit, Core Graphics, Core Animation, OpenGL are allowed for non-main-thread?

In my OpenGL-ES 1.1 based application, I use CALayer as the source for OpenGL textures. Those CALayer consist of CGImage and text displayed through CoreGraphics. Another OpenGL texture source is a UIView screenshot taken using -[CALAyer renderInContext:] and UIGraphicsGetImageFromCurrentImageContext . I am currently fully running in the main thread.

The latter case, in particular, is pretty bad, because it stops rendering OpenGL for the entire time it takes to create a UIView and its screenshot.

Now I'm going to move the OpenGL code to a separate thread, hoping to get around this lock. Ideally, the screenshot will be taken in a different thread (main thread, if necessary) than the OpenGL rendering.

I could not find the full coverage in the documentation about what is needed to run in the main thread and what is not. I found some comments in the notes of iOS 4 and some comments in specific UIKit methods, but I miss the full image.

Code works on iOS 4.x or higher.

+6
multithreading iphone uikit core-graphics opengl-es
source share
2 answers

You can draw using OpenGL ES in a background thread if you are not trying to access the OpenGL context from another thread at the same time. See Apple Tech Q & QA1612 for details .

I ran into a number of problems updating CALayer content from a background thread, so I do any work with layers in the main thread. Anyway, Core Animation disables the animation in the background thread.

I have never updated anything related to UIKit from a background thread, but some aspects of drawing in UIKit were made thread safe in 4.0. David Duncan comments here that drawing in context is now thread safe.

In your case, I don't see a problem with rendering OpenGL ES rendering in the background thread (perhaps using a sequential send queue to GCD to prevent access to the context from multiple threads at the same time), and then executing your image captures another.

+3
source share

Core Animation is usually thread safe, but UIKit and OpenGL ES (at least on iOS) are not thread safe. UIKit should be used only in the main thread, and OpenGL ES should be used sequentially in one thread (usually in the main thread).

+2
source share

All Articles