From 1314f9f5514a31482705aa4bd23a56c3654e28e0 Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Thu, 26 Feb 2026 21:04:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0NaitveWindow=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/CMakeLists.txt | 5 +- .../main/cpp/NativeEGLOCCT/NativeRender.cpp | 10 +-- .../src/main/cpp/NativeEGLOCCT/NativeRender.h | 7 +- .../cpp/NativeEGLOCCT/NativeRenderThread.cpp | 8 +- .../main/cpp/NativeEGLOCCT/NativeWindow.cpp | 86 +++++++++++++++++++ .../src/main/cpp/NativeEGLOCCT/NativeWindow.h | 38 ++++++++ 6 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 entry/src/main/cpp/NativeEGLOCCT/NativeWindow.cpp create mode 100644 entry/src/main/cpp/NativeEGLOCCT/NativeWindow.h diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt index 61ea1258..98306087 100644 --- a/entry/src/main/cpp/CMakeLists.txt +++ b/entry/src/main/cpp/CMakeLists.txt @@ -32,12 +32,15 @@ add_library(opencax SHARED NativeEGLOCCT/NativeRender.h NativeEGLOCCT/NativeRenderThread.h NativeEGLOCCT/NativeManager.h + NativeEGLOCCT/NativeWindow.h # Cpp Src NativeEGLOCCT/EGLCore.cpp NativeEGLOCCT/NativeRender.cpp NativeEGLOCCT/NativeRenderThread.cpp NativeEGLOCCT/NativeManager.cpp - napi_init.cpp) + NativeEGLOCCT/NativeWindow.cpp + napi_init.cpp + ) # 查找系统库 find_library(EGL-lib EGL) diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp index 0c1b54ba..c24ea604 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.cpp @@ -1,4 +1,5 @@ #include "NativeRender.h" + #include #include #include @@ -35,7 +36,8 @@ NativeRender::~NativeRender() { shapes_.clear(); } -bool NativeRender::init(int width, int height) { +bool NativeRender::init(OHNativeWindow* window,int width, int height) { + nativeWindow_=new NativeWindow(window); width_ = width; height_ = height; if (view_.IsNull()) { @@ -49,7 +51,6 @@ bool NativeRender::init(int width, int height) { void NativeRender::setupViewer() { Handle(Aspect_DisplayConnection) displayConnection; - // 创建图形驱动 static Handle(OpenGl_GraphicDriver) graphicDriver; if (graphicDriver.IsNull()) { @@ -57,7 +58,6 @@ void NativeRender::setupViewer() { graphicDriver->ChangeOptions().buffersNoSwap = Standard_True; } graphicDriver_ = graphicDriver; - // 创建V3d_Viewer viewer_ = new V3d_Viewer(graphicDriver_); viewer_->SetDefaultLights(); @@ -71,7 +71,6 @@ void NativeRender::setupContext() { void NativeRender::setupView() { view_ = viewer_->CreateView(); - // 设置渲染参数 view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION; view_->ChangeRenderingParams().IsShadowEnabled = Standard_False; @@ -97,6 +96,7 @@ void NativeRender::setupView() { } bool NativeRender::loadModel(const std::string& filePath) { + //view_->SetWindow(nativeWindow_); // 清除现有模型 for (auto& shape : shapes_) { context_->Remove(shape, Standard_False); @@ -243,7 +243,7 @@ void NativeRender::render() { void NativeRender::resize(int width, int height) { width_ = width; height_ = height; - if (!view_.IsNull() && nativeWindow_ != nullptr) { + if (!view_.IsNull()) { view_->SetWindow(nativeWindow_); view_->MustBeResized(); } diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h index aedb9d91..f049d9a0 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRender.h @@ -17,8 +17,9 @@ #include #include #include +#include "NativeWindow.h" -class Aspect_Window; +class NativeWindow; class gp_Quaternion; class Graphic3d_Camera; @@ -28,7 +29,7 @@ public: NativeRender(); ~NativeRender(); - bool init(int width, int height); + bool init(OHNativeWindow* window,int width, int height); bool loadModel(const std::string& filePath); void render(); void resize(int width, int height); @@ -58,7 +59,7 @@ private: float translationX_; float translationY_; Quantity_Color clearColor_; - Aspect_Window* nativeWindow_; + Handle(NativeWindow) nativeWindow_; }; } // namespace NativeRender #endif // NATIVE_RENDER_H diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp index 459ccf56..c0fb3553 100644 --- a/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeRenderThread.cpp @@ -1,7 +1,8 @@ #include "NativeRenderThread.h" +#include "NativeEGLOCCT/common.h" #include #include - +#include namespace NativeOpenCAX { NativeRenderThread::NativeRenderThread() @@ -28,7 +29,7 @@ bool NativeRenderThread::start(OHNativeWindow* window) { } // 初始化OCCT渲染器 - if (!renderer_.init(windowWidth_, windowHeight_)) { + if (!renderer_.init(nativeWindow_,windowWidth_, windowHeight_)) { printf("Failed to initialize OCCT renderer\n"); eglCore_.destroy(); return false; @@ -105,7 +106,6 @@ void NativeRenderThread::renderLoop() { break; } } - if (!isRunning_) { break; } @@ -114,7 +114,7 @@ void NativeRenderThread::renderLoop() { eglCore_.makeCurrent(); renderer_.render(); eglCore_.swapBuffers(); - + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderThead", "NativeRenderThead Done"); // 调用渲染完成回调 Callback callback; { diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.cpp b/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.cpp new file mode 100644 index 00000000..22bc3a4f --- /dev/null +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.cpp @@ -0,0 +1,86 @@ +// +// Created on 2026/2/26. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#include "NativeWindow.h" + +namespace NativeOpenCAX { + +NativeWindow::NativeWindow(OHNativeWindow *window) + :nativeWindow_(window), + width_(0), + height_(0) { + + if (!window) { + throw std::invalid_argument("OHNativeWindow cannot be null"); + } +} + +NativeWindow::~NativeWindow() {} + +void NativeWindow::Map() const { + +} +void NativeWindow::Unmap() const { + +}; +Standard_Real NativeWindow::Ratio() const { + if (height_ > 0) { + return static_cast(width_) / static_cast(height_); + } else { + // Return 1.0 if height is invalid (e.g., 0 or negative) + return 1.0; + } +} + +void NativeWindow::Position(int &theX1, int &theY1, int &theX2, int &theY2) const { + theX1 = 0; + theY1 = 0; + theX2 = width_; + theY2 = height_; +} + + +void NativeWindow::Size(int &theWidth, int &theHeight) const { + theWidth = width_; + theHeight = height_; +} +Standard_Boolean NativeWindow::IsMapped() const { + // 对于 NativeWindow,通常意味着它已经与原生窗口系统关联并准备就绪。 + // 可以根据 nativeWindow_ 是否有效来判断,或者根据实际状态。 + // 最简单的实现是返回 Standard_True。 + return Standard_True; +} + +Aspect_TypeOfResize NativeWindow::DoResize() { + return Aspect_TOR_UNKNOWN; +} + +Standard_Boolean NativeWindow::DoMapping() const { + // 通常对于嵌入式窗口,如果 NativeWindow 对象存在,我们就认为映射是有效的。 + // 这也可以根据 nativeWindow_ 的有效性来判断。 + return Standard_True; +} +Aspect_Drawable NativeWindow::NativeHandle() const { + // OHNativeWindow* 本身就可以被视为一个原生 Drawable 句柄 + // 或者你需要通过特定 API 获取更精确的句柄,例如 eglSurface_ + // 在这个场景下,直接返回 nativeWindow_ 的指针值通常是合适的。 + // 注意:Aspect_Drawable 通常是 void* 或一个整数类型。 + return reinterpret_cast(nativeWindow_); +} +Aspect_Drawable NativeWindow::NativeParentHandle() const { + // 对于 XComponent 等嵌入式窗口,通常没有明确的父窗口句柄, + // 或者其父窗口由框架管理,这里可以返回 0 或者调用特定 API 获取。 + // 暂时返回 0,表示没有或未知父窗口。 + return (Aspect_Drawable)0; + // 或者,如果你有方法获取父窗口句柄,则返回它: + // return (Aspect_Drawable)getParentNativeWindow(); +} +Aspect_FBConfig NativeWindow::NativeFBConfig() const { + // 对于大多数 Android/HarmonyOS 场景下的 OpenGL ES 应用, + // Framebuffer Config 由 EGL 管理,此函数可返回 0。 + return 0; +} +} // namespace NativeOpenCAX \ No newline at end of file diff --git a/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.h b/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.h new file mode 100644 index 00000000..b091ceef --- /dev/null +++ b/entry/src/main/cpp/NativeEGLOCCT/NativeWindow.h @@ -0,0 +1,38 @@ +// +// Created on 2026/2/26. +// +// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, +// please include "napi/native_api.h". + +#ifndef NATIVE_WINDOW_H +#define NATIVE_WINDOW_H +#include "Aspect_Window.hxx" +#include +namespace NativeOpenCAX { + +class NativeWindow : public Aspect_Window { +public: + explicit NativeWindow(OHNativeWindow *window); + virtual ~NativeWindow(); + + virtual void Map() const override; + virtual void Unmap() const override; + // Remove '= 0' from the following declarations + virtual Standard_Boolean IsMapped() const override; + virtual Aspect_TypeOfResize DoResize() override; + virtual Standard_Boolean DoMapping() const override; + virtual Standard_Real Ratio() const override; + virtual void Position(int &theX1, int &theY1, int &theX2, int &theY2) const override; + virtual void Size(int &theWidth, int &theHeight) const override; + // Remove '= 0' from the following declarations + virtual Aspect_Drawable NativeHandle() const override; + virtual Aspect_Drawable NativeParentHandle() const override; + virtual Aspect_FBConfig NativeFBConfig() const override; + OHNativeWindow *GetNativeWindow() const { return nativeWindow_; } +private: + OHNativeWindow *nativeWindow_; + mutable int width_; + mutable int height_; +}; +} // namespace NativeOpenCAX +#endif // OPENCAX_NATIVEWINDOW_H