增加NaitveWindow类

This commit is contained in:
JackLee 2026-02-26 21:04:50 +08:00
parent eed0b54b30
commit 1314f9f551
6 changed files with 141 additions and 13 deletions

View File

@ -32,12 +32,15 @@ add_library(opencax SHARED
NativeEGLOCCT/NativeRender.h NativeEGLOCCT/NativeRender.h
NativeEGLOCCT/NativeRenderThread.h NativeEGLOCCT/NativeRenderThread.h
NativeEGLOCCT/NativeManager.h NativeEGLOCCT/NativeManager.h
NativeEGLOCCT/NativeWindow.h
# Cpp Src # Cpp Src
NativeEGLOCCT/EGLCore.cpp NativeEGLOCCT/EGLCore.cpp
NativeEGLOCCT/NativeRender.cpp NativeEGLOCCT/NativeRender.cpp
NativeEGLOCCT/NativeRenderThread.cpp NativeEGLOCCT/NativeRenderThread.cpp
NativeEGLOCCT/NativeManager.cpp NativeEGLOCCT/NativeManager.cpp
napi_init.cpp) NativeEGLOCCT/NativeWindow.cpp
napi_init.cpp
)
# #
find_library(EGL-lib EGL) find_library(EGL-lib EGL)

View File

@ -1,4 +1,5 @@
#include "NativeRender.h" #include "NativeRender.h"
#include <cstdio> #include <cstdio>
#include <cmath> #include <cmath>
#include <OSD_Environment.hxx> #include <OSD_Environment.hxx>
@ -35,7 +36,8 @@ NativeRender::~NativeRender() {
shapes_.clear(); shapes_.clear();
} }
bool NativeRender::init(int width, int height) { bool NativeRender::init(OHNativeWindow* window,int width, int height) {
nativeWindow_=new NativeWindow(window);
width_ = width; width_ = width;
height_ = height; height_ = height;
if (view_.IsNull()) { if (view_.IsNull()) {
@ -49,7 +51,6 @@ bool NativeRender::init(int width, int height) {
void NativeRender::setupViewer() { void NativeRender::setupViewer() {
Handle(Aspect_DisplayConnection) displayConnection; Handle(Aspect_DisplayConnection) displayConnection;
// 创建图形驱动 // 创建图形驱动
static Handle(OpenGl_GraphicDriver) graphicDriver; static Handle(OpenGl_GraphicDriver) graphicDriver;
if (graphicDriver.IsNull()) { if (graphicDriver.IsNull()) {
@ -57,7 +58,6 @@ void NativeRender::setupViewer() {
graphicDriver->ChangeOptions().buffersNoSwap = Standard_True; graphicDriver->ChangeOptions().buffersNoSwap = Standard_True;
} }
graphicDriver_ = graphicDriver; graphicDriver_ = graphicDriver;
// 创建V3d_Viewer // 创建V3d_Viewer
viewer_ = new V3d_Viewer(graphicDriver_); viewer_ = new V3d_Viewer(graphicDriver_);
viewer_->SetDefaultLights(); viewer_->SetDefaultLights();
@ -71,7 +71,6 @@ void NativeRender::setupContext() {
void NativeRender::setupView() { void NativeRender::setupView() {
view_ = viewer_->CreateView(); view_ = viewer_->CreateView();
// 设置渲染参数 // 设置渲染参数
view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION; view_->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
view_->ChangeRenderingParams().IsShadowEnabled = Standard_False; view_->ChangeRenderingParams().IsShadowEnabled = Standard_False;
@ -97,6 +96,7 @@ void NativeRender::setupView() {
} }
bool NativeRender::loadModel(const std::string& filePath) { bool NativeRender::loadModel(const std::string& filePath) {
//view_->SetWindow(nativeWindow_);
// 清除现有模型 // 清除现有模型
for (auto& shape : shapes_) { for (auto& shape : shapes_) {
context_->Remove(shape, Standard_False); context_->Remove(shape, Standard_False);
@ -243,7 +243,7 @@ void NativeRender::render() {
void NativeRender::resize(int width, int height) { void NativeRender::resize(int width, int height) {
width_ = width; width_ = width;
height_ = height; height_ = height;
if (!view_.IsNull() && nativeWindow_ != nullptr) { if (!view_.IsNull()) {
view_->SetWindow(nativeWindow_); view_->SetWindow(nativeWindow_);
view_->MustBeResized(); view_->MustBeResized();
} }

View File

@ -17,8 +17,9 @@
#include <Quantity_Color.hxx> #include <Quantity_Color.hxx>
#include <gp_Quaternion.hxx> #include <gp_Quaternion.hxx>
#include <gp_EulerSequence.hxx> #include <gp_EulerSequence.hxx>
#include "NativeWindow.h"
class Aspect_Window; class NativeWindow;
class gp_Quaternion; class gp_Quaternion;
class Graphic3d_Camera; class Graphic3d_Camera;
@ -28,7 +29,7 @@ public:
NativeRender(); NativeRender();
~NativeRender(); ~NativeRender();
bool init(int width, int height); bool init(OHNativeWindow* window,int width, int height);
bool loadModel(const std::string& filePath); bool loadModel(const std::string& filePath);
void render(); void render();
void resize(int width, int height); void resize(int width, int height);
@ -58,7 +59,7 @@ private:
float translationX_; float translationX_;
float translationY_; float translationY_;
Quantity_Color clearColor_; Quantity_Color clearColor_;
Aspect_Window* nativeWindow_; Handle(NativeWindow) nativeWindow_;
}; };
} // namespace NativeRender } // namespace NativeRender
#endif // NATIVE_RENDER_H #endif // NATIVE_RENDER_H

View File

@ -1,7 +1,8 @@
#include "NativeRenderThread.h" #include "NativeRenderThread.h"
#include "NativeEGLOCCT/common.h"
#include <cstdio> #include <cstdio>
#include <unistd.h> #include <unistd.h>
#include <hilog/log.h>
namespace NativeOpenCAX { namespace NativeOpenCAX {
NativeRenderThread::NativeRenderThread() NativeRenderThread::NativeRenderThread()
@ -28,7 +29,7 @@ bool NativeRenderThread::start(OHNativeWindow* window) {
} }
// 初始化OCCT渲染器 // 初始化OCCT渲染器
if (!renderer_.init(windowWidth_, windowHeight_)) { if (!renderer_.init(nativeWindow_,windowWidth_, windowHeight_)) {
printf("Failed to initialize OCCT renderer\n"); printf("Failed to initialize OCCT renderer\n");
eglCore_.destroy(); eglCore_.destroy();
return false; return false;
@ -105,7 +106,6 @@ void NativeRenderThread::renderLoop() {
break; break;
} }
} }
if (!isRunning_) { if (!isRunning_) {
break; break;
} }
@ -114,7 +114,7 @@ void NativeRenderThread::renderLoop() {
eglCore_.makeCurrent(); eglCore_.makeCurrent();
renderer_.render(); renderer_.render();
eglCore_.swapBuffers(); eglCore_.swapBuffers();
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderThead", "NativeRenderThead Done");
// 调用渲染完成回调 // 调用渲染完成回调
Callback callback; Callback callback;
{ {

View File

@ -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<Standard_Real>(width_) / static_cast<Standard_Real>(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<Aspect_Drawable>(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

View File

@ -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 <ace/xcomponent/native_interface_xcomponent.h>
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