OpenCAX/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp

124 lines
3.8 KiB
C++

#include "EGLCore.h"
#include <cstdio>
#include <cstring>
#include "NativeEGLOCCT/common.h"
namespace NativeOpenCAX {
EGLCore::EGLCore()
: eglDisplay_(EGL_NO_DISPLAY),
eglContext_(EGL_NO_CONTEXT),
eglSurface_(EGL_NO_SURFACE),
nativeWindow_(nullptr) {}
EGLCore::~EGLCore() {
destroy();
}
bool EGLCore::init(OHNativeWindow* window) {
if (!window) {
printf("Native window is null\n");
return false;
}
nativeWindow_ = window;
// 获取EGL display
eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (eglDisplay_ == EGL_NO_DISPLAY) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglGetDisplay failed:%{public}d",std::this_thread::get_id());
return false;
}
// 初始化EGL
EGLint majorVersion;
EGLint minorVersion;
if (!eglInitialize(eglDisplay_, &majorVersion, &minorVersion)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglInitialize failed:%{public}d",std::this_thread::get_id());
return false;
}
// 配置EGL
EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_NONE
};
const EGLint maxConfigSize = 1;
EGLint numConfigs;
if (!eglChooseConfig(eglDisplay_, attribs, &eglConfig_, maxConfigSize, &numConfigs)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglChooseConfig: unable to choose configs");
return false;
}
// 创建EGL surface
eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, (NativeWindowType)nativeWindow_, nullptr);
if (eglSurface_ == EGL_NO_SURFACE) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateWindowSurface failed:%{public}d",std::this_thread::get_id());
return false;
}
// 创建EGL context
EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
if (eglContext_ == EGL_NO_CONTEXT) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateContext failed:%{public}d",std::this_thread::get_id());
return false;
}
// 激活上下文
makeCurrent();
// 检查GL错误
GLenum glError = glGetError();
if (glError != GL_NO_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","OpenGL error after initialization:%{public}d",glError);
return false;
}
// 设置GL视口
glViewport(0, 0, 1280, 720); // 默认大小,实际会随窗口变化
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInitDone","Thread ID: %{public}d",std::this_thread::get_id());
return true;
}
void EGLCore::makeCurrent() {
if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInitDone","eglMakeCurrent failed: 0x%{public}x\n",eglGetError());
}
}
bool EGLCore::swapBuffers() {
return eglSwapBuffers(eglDisplay_, eglSurface_);
}
void EGLCore::destroy() {
if (eglDisplay_ != EGL_NO_DISPLAY) {
if (eglContext_ != EGL_NO_CONTEXT) {
eglDestroyContext(eglDisplay_, eglContext_);
eglContext_ = EGL_NO_CONTEXT;
}
if (eglSurface_ != EGL_NO_SURFACE) {
eglDestroySurface(eglDisplay_, eglSurface_);
eglSurface_ = EGL_NO_SURFACE;
}
eglTerminate(eglDisplay_);
eglDisplay_ = EGL_NO_DISPLAY;
}
}
} // namespace NativeOpenCAX