Sunday 5 May 2013

iOS: OpenGL ES frame is not updated for changes made in another thread

It is common to use display link in iOS to set a frame rate for OpenGL ES and use a function doFrame to update frame regularly.

-(void) doFrame {

  if (_needUpdate) {      // line A
    if ( // condition is satisfied) {
      // draw the scene
      _needUpdate = false;  // line B
    }
  }

}

In another thread, you do the following

//change the scene
_needUpdate = true;  // line C

so that the scene will be redrawn in doFrame.

However there is a bug with this approach. If execution time between line A and line B is long, there is high chance that line C happens between line A and line B and has no effect. The symptom is that frame is not updated for changes made in another thread.

One simple workaround is to move line B up and make the execution time between line A and line B short. Use a counter instead of boolean for _needUpdate could be another solution.





No comments:

Post a Comment