Setting up an EGL Context

Sets up the EGLContext, creating it and then installing it to the current thread.

EGL needs a way to know that any subsequent EGL calls are going to be affecting OpenGL ES, rather than any other API (such as OpenVG).

Make OpenGL ES the current API.

 	eglBindAPI(EGL_OPENGL_ES_API);
 	if (!testEGLError(_surfaceData, "eglBindAPI"))
 	{
 		return false;
 	}
 

EGL has to create what is known as a context for OpenGL ES.

What are Contexts?:

The concept of a context is OpenGL ES's way of encapsulating any resources and state. What appear to be "global" functions in OpenGL ES actually only operate on the current context. A context is required for any operations in OpenGL ES.

Similar to an EGLConfig, a context takes in a list of attributes specifying some of its capabilities. However, in most cases this is limited to just requiring the version of the OpenGL ES context; in this case, OpenGL ES 2.0.

The EGL_NONE element is used as before to show the end of the list.

 	EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
 

This creates the context using the context attributes supplied and tests for any EGL errors.

 	context = eglCreateContext(_eglDisplay, config, NULL, contextAttributes);
 	if (!testEGLError(_surfaceData, "eglCreateContext"))
 	{
 		return false;
 	}
 

Due to the way OpenGL ES uses global functions, contexts need to be made current so that any function call can operate on the correct context. Specifically, eglMakeCurrent will bind the context to the current rendering thread it has been called from. If the calling thread already has a current rendering context then that context is flushed and marked as no longer current. It is not valid to call eglMakeCurrent with a context which is current on another thread.

To use multiple contexts at the same time, use multiple threads and synchronise between them.

This function also binds the context to the draw and read surfaces. This ensures the correct surface is rendered to. There is only one surface in this example so both the draw and read surfaces are set to _eglSurface.

 	eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, context);
 	

Check for any EGL Errors after an EGL call.

 	if (!testEGLError(_surfaceData, "eglMakeCurrent"))
 	{
 		return false;
 	}
 	return true;