Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which parts of UIKit, Core Graphics, Core Animation, OpenGL are allowed on non main-thread?

In my OpenGL-ES 1.1 based app, I'm using CALayers as a source for OpenGL textures. Those CALayers comprise of CGImages and text rendered through CoreGraphics. Another OpenGL texture source is a screenshot of a UIView taken using -[CALAyer renderInContext:] and UIGraphicsGetImageFromCurrentImageContext. Currently, I'm running completely on the main thread.

The latter case in particular is pretty bad because it halts the OpenGL rendering for the whole time it takes to create the UIView and its screenshot.

Now I'm considering to move the OpenGL code into a separate thread hoping to get around this blocking. Ideally, the screenshot would be taken on a different thread (main thread if needed) than the OpenGL rendering.

I wasn't able to find a complete coverage in the documentation about what requires running on the main thread and what not. I found some comments in the iOS 4 release notes, and some comments in specific UIKit methods but I'm missing the complete picture.

The code runs on iOS 4.x or higher.

like image 384
Ortwin Gentz Avatar asked Dec 29 '10 17:12

Ortwin Gentz


2 Answers

You can do drawing with OpenGL ES on a background thread, as long as you do don't try to access the OpenGL context from another thread at the same time. See Apple's Technical Q&A QA1612 for a little more on this.

I have run into a number of issues with updating CALayer content from a background thread, so I do any work with layers on the main thread. Core Animation fires off its animations on a background thread, anyway.

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

In your case, I wouldn't see a problem with running the OpenGL ES rendering on a background thread (perhaps using a serial dispatch queue in GCD to prevent access to the context from multiple threads at once) and then doing your image grabs on another.

like image 194
Brad Larson Avatar answered Sep 30 '22 16:09

Brad Larson


Core Animation is generally thread safe, but UIKit and OpenGL ES (on iOS, at least) are not thread-safe. UIKit must only be used on the main thread, and OpenGL ES must be consistently used on a single thread (usually the main thread.)

like image 26
Jonathan Grynspan Avatar answered Sep 30 '22 16:09

Jonathan Grynspan