增加一些日志输出.

This commit is contained in:
JackLee 2026-02-27 15:44:57 +08:00
parent acb34c3b4d
commit 5c2e32b0d9
8 changed files with 62 additions and 50 deletions

View File

@ -1,6 +1,7 @@
#include "EGLCore.h"
#include <cstdio>
#include <cstring>
#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());
}
}

View File

@ -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_;

View File

@ -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);

View File

@ -1,6 +1,7 @@
#include "NativeRender.h"
#include <GLES3/gl3.h>
#include <cstdio>
#include <cmath>
#include <OSD_Environment.hxx>
@ -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();
// 计算旋转

View File

@ -18,7 +18,7 @@
#include <gp_Quaternion.hxx>
#include <gp_EulerSequence.hxx>
#include <ace/xcomponent/native_interface_xcomponent.h>
#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_;

View File

@ -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;
{

View File

@ -37,7 +37,7 @@ private:
std::atomic<bool> isRunning_;
OHNativeWindow* nativeWindow_;
EGLCore eglCore_;
EGLCore* eglCore_;
NativeRender renderer_;
std::mutex commandMutex_;

View File

@ -21,7 +21,8 @@
#include <EGL/eglplatform.h>
#include <GLES3/gl3.h>
#include <napi/native_api.h>
#include "hilog/log.h"
#include <thread>
namespace NativeOpenCAX {
/**
* Log print domain.