Android UI Internal : EGL


EGL is a hard topic to tackle. I didn't find any good tutorial that explained it well. After reading its wiki page, I ended up having more questions such as what is windowing system, what is surface binding and more.

EGL is an interface rendering APIs (such as OpenGL, OpenGL ES or OpenVG) and the underlying native platform windowing system. EGL handles graphics context management, surface/buffer binding, rendering synchronization
Nevertheless, I don't think I can do any better in explaining what EGL is a less than 100 pages as the EGL spec does. Instead, I would like to pick an easier task and only talk about following items.
  • Why we need EGL? Isn't OpenGL enough? 
In short, it is for separation of concerns. 
OpenGL is responsible for drawing while EGL is responsible for the others - context management, surface binding and rendering synchronization. Read EGL spec for details. But for now just remember we always need to use EGL with OpenGL. The next item explains how they cooperate.
  •  How EGL is typically used with OpenGL?
To draw a frame below is the typical calling sequence, which can be simplified as setupWithEGL, drawWithOpenGL, tearDownWithEGL.


eglGetDisplay
eglInitialize
eglGetConfig
eglChooseConfig
eglCreateWIndowSuface
eglCreateContext
eglMakeCurrent
drawUsingGLAPI
eglSwapBuffer
eglDestroyContext
eglDestorySurface
eglTerminate

To echo the EGL responsilibity described in item 1 or in quoted Wiki page , here is the mapping from API to its functionality. 

eglGetConfig/eglChooseConfig                       - configuration management 
eglCreateWIndowSurface/eglDestorySurface  - surface binding
eglCreateContext/eglMakeCurrent                   - context management 
eglSwapBuffer                                                 -  buffer managment 

For detail explanation of each API, please refer to EGL spec or  this ref card 
  •  EGL and GLSurfaceView 
"But I did not use EGL in my 3D application with GLSurfaceView." You questioned. That is because GLSurfaceView did it for you, with EglHelper.

In article GLSurfaceView, I raised the question of what are the APIs SetEGLxxx for.  Now, I think you have the answer. 

GLSurfaceView and EGL
Besides GLSurfaceView, SurfaceFlinger and HWUI module also use EGL. One for UI composition and one for View drawing.
  •  EGL and native window system
EGL also aims to hide the native window system of the operation system. In Android, the native window is of type  - ANativeWindow - which does not tell too much by it name.  We will talk about it later since it is a very important concept for framework developer and NDK developer.


-------------
Check out other articles in Android UI Internal series