From 5c2e32b0d9489d5c45d254153ed712ecfb068192 Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Fri, 27 Feb 2026 15:44:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp | 25 +++++++----- entry/src/main/cpp/NativeEGLOCCT/EGLCore.h | 3 +- .../main/cpp/NativeEGLOCCT/NativeManager.cpp | 4 +- .../main/cpp/NativeEGLOCCT/NativeRender.cpp | 31 +++++++++------ .../src/main/cpp/NativeEGLOCCT/NativeRender.h | 5 ++- .../cpp/NativeEGLOCCT/NativeRenderThread.cpp | 39 +++++++++---------- .../cpp/NativeEGLOCCT/NativeRenderThread.h | 2 +- entry/src/main/cpp/NativeEGLOCCT/common.h | 3 +- 8 files changed, 62 insertions(+), 50 deletions(-) diff --git a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp index 948ec071..75e020ca 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.cpp @@ -1,6 +1,7 @@ #include "EGLCore.h" #include #include +#include "NativeEGLOCCT/common.h" namespace NativeOpenCAX { @@ -25,13 +26,16 @@ bool EGLCore::init(OHNativeWindow* window) { // 获取EGL display eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (eglDisplay_ == EGL_NO_DISPLAY) { - printf("eglGetDisplay failed\n"); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglGetDisplay failed:%{public}d",std::this_thread::get_id()); + return false; } // 初始化EGL - if (!eglInitialize(eglDisplay_, nullptr, nullptr)) { - printf("eglInitialize failed\n"); + 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; } @@ -48,16 +52,17 @@ bool EGLCore::init(OHNativeWindow* window) { EGL_NONE }; + const EGLint maxConfigSize = 1; EGLint numConfigs; - if (!eglChooseConfig(eglDisplay_, attribs, &eglConfig_, 1, &numConfigs) || numConfigs == 0) { - printf("eglChooseConfig failed\n"); + 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) { - printf("eglCreateWindowSurface failed: 0x%x\n", eglGetError()); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateWindowSurface failed:%{public}d",std::this_thread::get_id()); return false; } @@ -69,7 +74,7 @@ bool EGLCore::init(OHNativeWindow* window) { eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs); if (eglContext_ == EGL_NO_CONTEXT) { - printf("eglCreateContext failed\n"); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","eglCreateContext failed:%{public}d",std::this_thread::get_id()); return false; } @@ -79,19 +84,19 @@ bool EGLCore::init(OHNativeWindow* window) { // 检查GL错误 GLenum glError = glGetError(); if (glError != GL_NO_ERROR) { - printf("OpenGL error after initialization: 0x%x\n", glError); + 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_)) { - printf("eglMakeCurrent failed: 0x%x\n", eglGetError()); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInitDone","eglMakeCurrent failed: 0x%{public}x\n",eglGetError()); } } diff --git a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h index 5b20491e..e5406dfd 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h +++ b/entry/src/main/cpp/NativeEGLOCCT/EGLCore.h @@ -29,7 +29,8 @@ public: bool swapBuffers(); void destroy(); EGLSurface getSurface() const { return eglSurface_; } - + EGLContext getContext() const { return eglContext_; } + EGLDisplay getDisplay() const { return eglDisplay_; } private: EGLDisplay eglDisplay_; EGLContext eglContext_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp index 4e381508..2d936e55 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeManager.cpp @@ -215,7 +215,7 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) { // 创建Node也名创建ROW Column等容器 ArkUI_NodeHandle nodeHandel = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER); // 节点默认宽度or高度 - ArkUI_NumberValue nodeSizeData[] = {800, 600}; + ArkUI_NumberValue nodeSizeData[] = {1280, 720}; ArkUI_NumberValue nodeMarginData[] = {{.u32 = 15}, {.f32 = 15}}; // ArkUI_AttributeItem // 参数1:指向数值数组 @@ -233,7 +233,7 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) { // 组件ID Item ArkUI_AttributeItem comIdItem = {.string = tag.c_str(), .size = 1}; // 组件Surface Size - ArkUI_NumberValue surfaceSizeData[] = {800, 600}; + ArkUI_NumberValue surfaceSizeData[] = {1280, 720}; ArkUI_AttributeItem surfaceSizeItem = {surfaceSizeData, 2}; // 创建组件 xc = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT); diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp index 270c7ffc..c834c27c 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp @@ -1,6 +1,7 @@ #include "NativeRender.h" +#include #include #include #include @@ -28,9 +29,7 @@ NativeRender::NativeRender() translationX_(0.0f), translationY_(0.0f) { - setupViewer(); - setupContext(); - setupView(); + } NativeRender::~NativeRender() { @@ -38,6 +37,9 @@ NativeRender::~NativeRender() { } bool NativeRender::init(int width, int height) { + setupViewer(); + setupContext(); + setupView(); width_ = width; height_ = height; if (view_.IsNull()) { @@ -46,18 +48,19 @@ bool NativeRender::init(int width, int height) { view_->SetBackgroundColor(clearColor_); view_->MustBeResized(); view_->Redraw(); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","NativeRender Init Done"); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","width: %{public}d",width_); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","height: %{public}d",height_); return true; } void NativeRender::setupViewer() { Handle(Aspect_DisplayConnection) displayConnection; // 创建图形驱动 - static Handle(OpenGl_GraphicDriver) graphicDriver; - if (graphicDriver.IsNull()) { - graphicDriver = new OpenGl_GraphicDriver(displayConnection, Standard_False); - graphicDriver->ChangeOptions().buffersNoSwap = Standard_True; + if (graphicDriver_.IsNull()) { + graphicDriver_ = new OpenGl_GraphicDriver(displayConnection, Standard_False); + graphicDriver_->ChangeOptions().buffersNoSwap = Standard_True; } - graphicDriver_ = graphicDriver; // 创建V3d_Viewer viewer_ = new V3d_Viewer(graphicDriver_); viewer_->SetDefaultLights(); @@ -88,7 +91,8 @@ void NativeRender::setupView() { // 设置默认相机位置 view_->SetProj(V3d_XposYnegZpos); - + //可选:显示坐标轴 + view_->ZBufferTriedronSetup(); // 设置相机参数 Handle(Graphic3d_Camera) camera = view_->Camera(); camera->SetFOVy(45.0); // 使用正确的FOV设置API @@ -156,7 +160,7 @@ bool NativeRender::loadModel(const std::string& filePath) { // 调整相机到合适位置 view_->FitAll(0.05, Standard_True); view_->ZFitAll(); - printf("Successfully loaded STEP file with %d shapes\n", numShapes); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "LoadModel","Successfully loaded STEP file with %{public}d shapes",numShapes); return true; } @@ -203,7 +207,7 @@ void NativeRender::loadDefaultModel() { // 调整相机 view_->FitAll(0.05, Standard_True); view_->ZFitAll(); - printf("Loaded default model (cube, sphere, cylinder)\n"); + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "LoadDefaultModel","Loaded default model (cube, sphere, cylinder)"); } //setTranslation @@ -214,9 +218,12 @@ void NativeRender::setTranslation(float x, float y) { void NativeRender::render() { if (view_.IsNull()) { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render","View Is Null"); return; } - + //glClearColor(1, 0, 0, 1); + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, width_, height_); // 应用旋转 Handle(Graphic3d_Camera) camera = view_->Camera(); // 计算旋转 diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h index 0de503d1..3a9de177 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h @@ -18,7 +18,7 @@ #include #include #include - +#include "common.h" class Aspect_Window; class gp_Quaternion; class Graphic3d_Camera; @@ -31,6 +31,7 @@ public: bool init(int width, int height); bool loadModel(const std::string& filePath); + void loadDefaultModel(); void render(); void resize(int width, int height); void setRotation(float xAngle, float yAngle); @@ -43,7 +44,7 @@ private: void setupViewer(); void setupContext(); void setupView(); - void loadDefaultModel(); + //void loadDefaultModel(); Handle(OpenGl_GraphicDriver) graphicDriver_; Handle(V3d_Viewer) viewer_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp index 85543cf2..6d659b54 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp @@ -9,7 +9,10 @@ NativeRenderThread::NativeRenderThread() : isRunning_(false), nativeWindow_(nullptr), windowWidth_(1280), - windowHeight_(720) {} + windowHeight_(720) +{ + eglCore_ = new EGLCore(); +} NativeRenderThread::~NativeRenderThread() { stop(); @@ -21,20 +24,7 @@ bool NativeRenderThread::start(OHNativeWindow* window) { } nativeWindow_ = window; - - // 初始化EGL - if (!eglCore_.init(nativeWindow_)) { - printf("Failed to initialize EGL\n"); - return false; - } - - // 初始化OCCT渲染器 - if (!renderer_.init(windowWidth_, windowHeight_)) { - printf("Failed to initialize OCCT renderer\n"); - eglCore_.destroy(); - return false; - } - + isRunning_ = true; renderThread_ = std::thread(&NativeRenderThread::renderLoop, this); @@ -57,11 +47,20 @@ void NativeRenderThread::stop() { if (renderThread_.joinable()) { renderThread_.join(); } - - eglCore_.destroy(); + eglCore_->destroy(); } void NativeRenderThread::renderLoop() { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "renderLoop","Thread ID: %{public}d",std::this_thread::get_id()); + // 初始化EGL + if (!eglCore_->init(nativeWindow_)) { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL"); + } + // 初始化OCCT渲染器 + if (!renderer_.init(windowWidth_, windowHeight_)) { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Failed to initialize OCCT renderer"); + eglCore_->destroy(); + } while (isRunning_) { RenderCommand command; bool hasCommand = false; @@ -109,12 +108,10 @@ void NativeRenderThread::renderLoop() { if (!isRunning_) { break; } - // 渲染 - eglCore_.makeCurrent(); + eglCore_->makeCurrent(); renderer_.render(); - eglCore_.swapBuffers(); - OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderThead", "NativeRenderThead Done"); + eglCore_->swapBuffers(); // 调用渲染完成回调 Callback callback; { diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.h b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.h index 4eb70fe2..ff56af1a 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.h @@ -37,7 +37,7 @@ private: std::atomic isRunning_; OHNativeWindow* nativeWindow_; - EGLCore eglCore_; + EGLCore* eglCore_; NativeRender renderer_; std::mutex commandMutex_; diff --git a/entry/src/main/cpp/NativeEGLOCCT/common.h b/entry/src/main/cpp/NativeEGLOCCT/common.h index dfdd2ab8..778b5d33 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/common.h +++ b/entry/src/main/cpp/NativeEGLOCCT/common.h @@ -21,7 +21,8 @@ #include #include #include - +#include "hilog/log.h" +#include namespace NativeOpenCAX { /** * Log print domain.