安卓要想在native层使用egl api,需要先执行eglMakeCurrent,下面是相关代码。
m_egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (m_egl_display == EGL_NO_DISPLAY) {
ml_logE << "eglGetDisplay failed." << std::endl;
return;
}
EGLint major_version, minor_version;
if (!eglInitialize(m_egl_display, &major_version, &minor_version)) {
m_egl_display = EGL_NO_DISPLAY;
ml_logE << "eglInitialize failed." << std::endl;
return;
}
EGLConfig egl_config;
EGLint config_count = 1;
const EGLint attr_list[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_NONE
};
if (!eglChooseConfig(m_egl_display, attr_list, &egl_config, 1, &config_count)) {
ml_logE << "egl choose config failed." << std::endl;
return;
}
m_egl_config = egl_config;
EGLint context_attr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
m_egl_context = eglCreateContext(m_egl_display, m_egl_config, m_egl_shared_context, context_attr);
if (m_egl_context == EGL_NO_CONTEXT) {
ml_logE << "egl create context failed." << std::endl;
return;
}
std::shared_ptr<Surface > surface_ = std::static_pointer_cast<Surface>(surface);
EGLNativeWindowType nativeWindow = (EGLNativeWindowType)ANativeWindow_fromSurface(ml_jni_helper::get_current_jni_env(),
surface_->jthis);
if (nativeWindow == nullptr) {
ml_logE << "native window from this surface is null. surface object:" << m_surface->jthis << std::endl;
return false;
}
if (m_cur_surface == nativeWindow) {
ml_logI << "already attach to this surface" << std::endl;
return true;
}
const EGLint attr_list[] = {EGL_NONE};
ml_logI << "eglCreateWindowSurface" << m_cur_surface << std::endl;
m_egl_surface = eglCreateWindowSurface(m_egl_display, m_egl_config, m_cur_surface, attr_list);
if (m_egl_surface != EGL_NO_SURFACE) {
int window_size[2] = {0};
eglQuerySurface(m_egl_display, m_egl_surface, EGL_WIDTH, &window_size[0]);
eglQuerySurface(m_egl_display, m_egl_surface, EGL_HEIGHT, &window_size[1]);
ml_logI << __func__ << ":egl surface width:" << window_size[0] << " height:" << window_size[1] << std::endl;
m_surface_size_width = window_size[0];
m_surface_size_height = window_size[1];
if (!eglMakeCurrent(m_egl_display, m_egl_surface, m_egl_surface, m_egl_context)) {
ml_logE << __func__ << "egl make current failed." << std::endl;
}
}