OpenCAX/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp
JackLee 6e51b8ba36 重构布局.独立文件菜单.
应用模块独立为公共模块.方便模块切换.
未开发:需要对装饰器重新定义.
2026-03-05 17:17:40 +08:00

123 lines
3.4 KiB
C++

#include "EGLCore.h"
#include <cstdio>
#include "NativeEGLOCCT/common.h"
#ifndef NATIVE_TAG
#define NATIVE_TAG "EGLCore"
#endif
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;
HILOG_INFO(NATIVE_TAG,"Current Thread ID: %{public}d",std::this_thread::get_id());
// 获取EGL display
eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (eglDisplay == EGL_NO_DISPLAY) {
HILOG_ERROR(NATIVE_TAG,"eglGetDisplay failed:%{public}d",glGetError());
return false;
}
// 初始化EGL
EGLint majorVersion;
EGLint minorVersion;
if (!eglInitialize(eglDisplay, &majorVersion, &minorVersion)) {
HILOG_ERROR(NATIVE_TAG,"eglInitialize failed:%{public}d",glGetError());
return false;
}
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","EGLDisplay version:%{public}d",majorVersion+"."+minorVersion);
HILOG_ERROR(NATIVE_TAG,"EGLDisplay version:%{public}d%{public}d",majorVersion,minorVersion);
// 配置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)) {
HILOG_ERROR(NATIVE_TAG,"eglChooseConfig: unable to choose configs");
return false;
}
// 创建EGL surface
eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)nativeWindow, nullptr);
if (eglSurface == EGL_NO_SURFACE) {
HILOG_ERROR(NATIVE_TAG,"EGL_NO_SURFACE");
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) {
HILOG_ERROR(NATIVE_TAG,"EGL_NO_CONTEXT");
return false;
}
// 激活上下文
makeCurrent();
// 检查GL错误
GLenum glError = glGetError();
if (glError != GL_NO_ERROR) {
HILOG_ERROR(NATIVE_TAG,"glError:",glError);
return false;
}
return true;
}
void EGLCore::makeCurrent() {
if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
HILOG_ERROR(NATIVE_TAG,"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