样例代码来源: android-openGL-canvas
OpenGL文档参考:
OpenGL文档
本文只对流程做排序以及一些简单的说明,想了解原理的请查看OpenGL相关文档,有对相应函数有疑问的可查阅上述文档。
一、创建 eglContext。EGLContext 是一个比较重的对象,所以一般只创建一次。
例子代码代码主要在 EglHelper
- egl = EGLContext.getEGL()
- 获取 eglDisplay = eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY)
- 获取 EGLConfig eglChooseConfig(..)
- eglInitialize(mEgl, mEglDisplay, mEglConfig, eglContext)
创建 eglContext = eglCreateContext(display, config, eglContext, attrib_list))
大致的相关代码
public EGLContext start(EGLContext eglContext) {
if (GLThread.LOG_EGL) {
Log.w("EglHelper", "start() tid=" + Thread.currentThread().getId());
}
/*
* Get an EGL instance
*/
mEgl = (EGL10) EGLContext.getEGL();
/*
* Get to the default display.
*/
mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
throw new RuntimeException("eglGetDisplay failed");
}
/*
* We can now initialize EGL for that display
*/
int[] version = new int[2];
if (!mEgl.eglInitialize(mEglDisplay, version)) {
throw new RuntimeException("eglInitialize failed");
}
mEglConfig = eglConfigChooser.chooseConfig(mEgl, mEglDisplay);
/*
* Create an EGL context. We want to do this as rarely as we can, because an
* EGL context is a somewhat heavy object.
*/
mEglContext = eglContextFactory.createContext(mEgl, mEglDisplay, mEglConfig, eglContext);
if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
mEglContext = null;
throwEglException("createContext");
}
if (GLThread.LOG_EGL) {
Log.w("EglHelper", "createContext " + mEglContext + " tid=" + Thread.currentThread().getId());
}
mEglSurface = null;
return mEglContext;
}
二、创建 surface
eglCreateWindowSurface(display, config, nativeWindow, surfaceAttribs)
将 context 和 surface 联系起来。
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)如果需要更换surface,就先destroy现在的
eglDestroySurface大致的相关代码
public boolean createSurface(Object surface) {