#include <EGL/egl.h>
//EGL相关参数
typedef struct _ANDROID_EGL_CONTEXT_T
{
EGLConfig eglConf;
EGLSurface eglSurface;
EGLContext eglCtx;
EGLDisplay eglDisp;
} ANDROID_EGL_CONTEXT_T;
ANDROID_EGL_CONTEXT_T ctx;//实例
//初始化EGL环境
static bool initEGLContext(ANDROID_EGL_CONTEXT_T &ctx, int image_width, int image_height)
{
EGLSurface &eglConf = ctx.eglConf;
EGLSurface &eglSurface = ctx.eglSurface;
EGLContext &eglCtx = ctx.eglCtx;
EGLDisplay &eglDisp = ctx.eglDisp;
// EGL config attributes
const EGLint confAttr[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,// very important!
EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,//EGL_WINDOW_BIT EGL_PBUFFER_BIT we will create a pixelbuffer surface
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,// if you need the alpha channel
EGL_DEPTH_SIZE, 8,// if you need the depth buffer
EGL_STENCIL_SIZE, 8,
EGL_NONE
};
// EGL context attributes
const EGLint ctxAttr[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,// very important!
EGL_NONE
};
// surface attributes
// the surface size is set to the input frame size
const EGLint surfaceAttr[] =
{
EGL_WIDTH, image_width,
EGL_HEIGHT, image_height,
EGL_NONE
};
EGLint eglMajVers, eglMinVers;
EGLint numConfigs;
eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (eglDisp == EGL_NO_DISPLAY)
{
//Unable to open connection to local windowing system
printf("Unable to open connection to local windowing system\n");
return false;
}
if (!eglInitialize(eglDisp, &eglMajVers, &eglMinVers))
{
// Unable to initialize EGL. Handle and recover
printf("Unable to initialize EGL\n");
return false;
}
printf("EGL init with version %d.%d", eglMajVers, eglMinVers);
// choose the first config, i.e. best config
if (!eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs))
{
printf("some config is wrong\n");
return false;
}
// else
// {
// LOGE("all configs is OK");
// }
// create a pixelbuffer surface
eglSurface = eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr);
if (eglSurface == EGL_NO_SURFACE)
{
switch (eglGetError())
{
case EGL_BAD_ALLOC:
// Not enough resources available. Handle and recover
printf("Not enough resources available");
break;
case EGL_BAD_CONFIG:
// Verify that provided EGLConfig is valid
printf("provided EGLConfig is invalid");
break;
case EGL_BAD_PARAMETER:
// Verify that the EGL_WIDTH and EGL_HEIGHT are
// non-negative values
printf("provided EGL_WIDTH and EGL_HEIGHT is invalid");
break;
case EGL_BAD_MATCH:
// Check window and EGLConfig attributes to determine
// compatibility and pbuffer-texture parameters
printf("Check window and EGLConfig attributes");
break;
}
return false;
}
eglCtx = eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr);
if (eglCtx == EGL_NO_CONTEXT)
{
EGLint glError = eglGetError();
if (glError == EGL_BAD_CONFIG)
{
// Handle error and recover
printf("EGL_BAD_CONFIG\n");
return false;
}
}
if (!eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx))
{
printf("MakeCurrent failed\n");
return false;
}
//LOGE("EGL SUCCESS");
return true;
}
//释放销毁EGL环境
static void releaseEGL(ANDROID_EGL_CONTEXT_T &ctx)
{
EGLSurface &eglSurface = ctx.eglSurface;
EGLContext &eglCtx = ctx.eglCtx;
EGLDisplay &eglDisp = ctx.eglDisp;
eglMakeCurrent(eglDisp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(eglDisp, eglCtx);
eglDestroySurface(eglDisp, eglSurface);
eglTerminate(eglDisp);
eglDisp = EGL_NO_DISPLAY;
eglSurface = EGL_NO_SURFACE;
eglCtx = EGL_NO_CONTEXT;
}
在CMake中target_link_libraries
需要链接到EGL
,如下:
target_link_libraries(render_util
GLESv2
EGL
${log-lib}
jnigraphics
)