Game Development Reference
In-Depth Information
Other important methods in the surface view include the following:
setRenderer() : This method creates the inner thread that does all the
work and starts it. The thread keeps a reference to the surface holder
available by calling getHolder() .
public void setRenderer(Renderer renderer) {
mGLThread = new GLThread(renderer, mHolder);
mGLThread.start();
}
queueEvent(Runnable r) : This method sends an event to be run by the
inner thread.
onDetachedFromWindow() : This method is called when the view is
detached from a window. At this point, it no longer has a surface for
drawing.
The surface view provides the drawing canvas for the next component, the GL thread. A thread
is required to perform tasks in the background, thus offloading processing time from the main
application thread to make the application run seamlessly. Let's see what it does.
GL Thread
The main loop of the animation is performed by GLThread . When started, this thread
performs the following steps:
1.
It creates a semaphore.
sEglSemaphore.acquire();
guardedRun(); // Only 1 thread can access this code
sEglSemaphore.release();
2.
It runs the critical animation loop. Within the loop, the actual drawing
is delegated to the CubeRenderer .
3.
When asked to quit, the loop terminates and the OpenGL resources
are released.
Note A semaphore is an object often used to restrict the number of threads that can access
the OpenGL context. When the Android framework launches a second instance of an activity,
the new instance's onCreate() method may be called before the first instance returns from
onDestroy() . A semaphore ensures that only one instance at a time accesses the GL API. You
must do this because OpenGL is a single-threaded API (which means that only one thread can
access the GLContext at a time).
 
 
Search WWH ::




Custom Search