修复部分多例代码
This commit is contained in:
parent
68a4979a93
commit
c0d534af0a
@ -4,120 +4,243 @@
|
|||||||
#ifndef NATIVE_TAG
|
#ifndef NATIVE_TAG
|
||||||
#define NATIVE_TAG "EGLCore"
|
#define NATIVE_TAG "EGLCore"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace NativeOpenCAX {
|
namespace NativeOpenCAX {
|
||||||
|
|
||||||
EGLCore::EGLCore()
|
// 配置EGL
|
||||||
: eglDisplay(EGL_NO_DISPLAY),
|
EGLint cfgAttribs[] = {
|
||||||
eglContext(EGL_NO_CONTEXT),
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||||
eglSurface(EGL_NO_SURFACE),
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
|
||||||
nativeWindow(nullptr)
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
// 创建EGL context
|
||||||
|
EGLint ctxAttribs[] = {
|
||||||
|
EGL_CONTEXT_CLIENT_VERSION, 3,
|
||||||
|
EGL_NONE
|
||||||
|
};
|
||||||
|
|
||||||
|
EGLCore::EGLCore():
|
||||||
|
eglDisplay(EGL_NO_DISPLAY),
|
||||||
|
eglConfig(nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EGLCore::~EGLCore() {
|
EGLCore::~EGLCore() {
|
||||||
destroy();
|
destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EGLCore::init(OHNativeWindow* window) {
|
bool EGLCore::InitEglCtx(OHNativeWindow* nativeWin, std::string id) {
|
||||||
if (!window) {
|
if (!nativeWin || id.empty()) {
|
||||||
printf("Native window is null\n");
|
HILOG_ERROR(NATIVE_TAG, "nativeWin is null or id is empty");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
nativeWindow=window;
|
|
||||||
HILOG_INFO(NATIVE_TAG,"Current Thread ID: %{public}d",std::this_thread::get_id());
|
EGLint err;
|
||||||
// 获取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;
|
const EGLint maxConfigSize = 1;
|
||||||
EGLint numConfigs;
|
EGLint numConfigs;
|
||||||
if (!eglChooseConfig(eglDisplay, attribs, &eglConfig, maxConfigSize, &numConfigs)) {
|
EGLSurface nSur = EGL_NO_SURFACE;
|
||||||
HILOG_ERROR(NATIVE_TAG,"eglChooseConfig: unable to choose configs");
|
EGLContext nCtx = EGL_NO_CONTEXT;
|
||||||
return false;
|
|
||||||
}
|
if (eglCtx.empty()&&eglSur.empty()&&eglWin.empty()) {
|
||||||
|
eglMainId=id;
|
||||||
// 创建EGL surface
|
eglCrtId = id;
|
||||||
eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)nativeWindow, nullptr);
|
eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||||
if (eglSurface == EGL_NO_SURFACE) {
|
if (eglDisplay == EGL_NO_DISPLAY) {
|
||||||
HILOG_ERROR(NATIVE_TAG,"EGL_NO_SURFACE");
|
HILOG_ERROR(NATIVE_TAG, "EGLDisplay is invalid, err: 0x%x", eglGetError());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建EGL context
|
if (!eglInitialize(eglDisplay, nullptr, nullptr)) {
|
||||||
EGLint contextAttribs[] = {
|
err = eglGetError();
|
||||||
EGL_CONTEXT_CLIENT_VERSION, 3,
|
HILOG_ERROR(NATIVE_TAG, "Main Ctx EGL Init Failed: 0x%x", err);
|
||||||
EGL_NONE
|
eglDisplay = EGL_NO_DISPLAY;
|
||||||
};
|
return false;
|
||||||
|
}
|
||||||
eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, contextAttribs);
|
HILOG_INFO(NATIVE_TAG, "EGLDisplay init success");
|
||||||
if (eglContext == EGL_NO_CONTEXT) {
|
|
||||||
HILOG_ERROR(NATIVE_TAG,"EGL_NO_CONTEXT");
|
if (!eglChooseConfig(eglDisplay, cfgAttribs, &eglConfig, maxConfigSize, &numConfigs) || numConfigs == 0) {
|
||||||
return false;
|
err = eglGetError();
|
||||||
}
|
HILOG_ERROR(NATIVE_TAG, "Main EGL ChooseConfig failed, err: 0x%x", err);
|
||||||
|
eglTerminate(eglDisplay);
|
||||||
// 激活上下文
|
eglDisplay = EGL_NO_DISPLAY;
|
||||||
makeCurrent();
|
eglConfig = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nSur = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)nativeWin, nullptr);
|
||||||
|
if (nSur == EGL_NO_SURFACE) {
|
||||||
|
err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Main EGL SURFACE Init Failed, err: 0x%x", err);
|
||||||
|
eglTerminate(eglDisplay);
|
||||||
|
eglDisplay = EGL_NO_DISPLAY;
|
||||||
|
eglConfig = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nCtx = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctxAttribs);
|
||||||
|
if (nCtx == EGL_NO_CONTEXT) {
|
||||||
|
err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Main EGL Context Init Failed, err: 0x%x", err);
|
||||||
|
eglDestroySurface(eglDisplay, nSur);
|
||||||
|
eglTerminate(eglDisplay);
|
||||||
|
eglDisplay = EGL_NO_DISPLAY;
|
||||||
|
eglConfig = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eglCrtId = id;
|
||||||
|
if (eglDisplay == EGL_NO_DISPLAY || eglConfig == nullptr || eglCtx.find(eglMainId) == eglCtx.end()) {
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Main EGL context not initialized, can't create sub context");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 新增:校验主Context是否为有效状态
|
||||||
|
EGLContext mainCtx = eglCtx[eglMainId];
|
||||||
|
EGLBoolean isMainCtxValid = eglQueryContext(eglDisplay, mainCtx, EGL_CONTEXT_CLIENT_VERSION, nullptr);
|
||||||
|
if (isMainCtxValid == EGL_FALSE) {
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Main EGL context is invalid, err: 0x%x", eglGetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nSur = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)nativeWin, nullptr);
|
||||||
|
if (nSur == EGL_NO_SURFACE) {
|
||||||
|
err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Sub EGL SURFACE Init Failed, err: 0x%x", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nCtx = eglCreateContext(eglDisplay, eglConfig, eglCtx[eglMainId], ctxAttribs);
|
||||||
|
if (nCtx == EGL_NO_CONTEXT) {
|
||||||
|
err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "Sub EGL Context Init Failed, err: 0x%x", err);
|
||||||
|
eglDestroySurface(eglDisplay, nSur);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!eglMakeCurrent(eglDisplay, nSur, nSur, nCtx)) {
|
||||||
|
err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "eglMakeCurrent failed after create, err: 0x%x", err);
|
||||||
|
eglDestroyContext(eglDisplay, nCtx);
|
||||||
|
eglDestroySurface(eglDisplay, nSur);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// 检查GL错误
|
|
||||||
GLenum glError = glGetError();
|
GLenum glError = glGetError();
|
||||||
if (glError != GL_NO_ERROR) {
|
if (glError != GL_NO_ERROR) {
|
||||||
HILOG_ERROR(NATIVE_TAG,"glError:",glError);
|
HILOG_ERROR(NATIVE_TAG, "GL error after init: %d", glError);
|
||||||
|
eglDestroyContext(eglDisplay, nCtx);
|
||||||
|
eglDestroySurface(eglDisplay, nSur);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eglWin[id] = nativeWin;
|
||||||
|
eglSur[id] = nSur;
|
||||||
|
eglCtx[id] = nCtx;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 切换上下文
|
||||||
void EGLCore::makeCurrent() {
|
void EGLCore::makeCurrent() {
|
||||||
if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
|
std::lock_guard<std::mutex> lock(crtMutex);
|
||||||
HILOG_ERROR(NATIVE_TAG,"eglMakeCurrent failed: 0x%{public}x\n:",eglGetError());
|
eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||||
|
// 校验资源是否存在
|
||||||
|
auto surIt = eglSur.find(eglCrtId);
|
||||||
|
auto ctxIt = eglCtx.find(eglCrtId);
|
||||||
|
if (surIt == eglSur.end() || ctxIt == eglCtx.end()) {
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "No surface/context for id: %s", eglCrtId.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!eglMakeCurrent(eglDisplay, surIt->second, surIt->second, ctxIt->second)) {
|
||||||
|
EGLint err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "eglMakeCurrent failed: 0x%x, id: %s", err, eglCrtId.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 交换缓冲区
|
||||||
bool EGLCore::swapBuffers() {
|
bool EGLCore::swapBuffers() {
|
||||||
return eglSwapBuffers(eglDisplay, eglSurface);
|
std::lock_guard<std::mutex> lock(swapMutex);
|
||||||
|
|
||||||
|
auto surIt = eglSur.find(eglCrtId);
|
||||||
|
if (surIt == eglSur.end() || surIt->second == EGL_NO_SURFACE) {
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "No valid surface for id: %s", eglCrtId.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ret = eglSwapBuffers(eglDisplay, surIt->second);
|
||||||
|
if (!ret) {
|
||||||
|
EGLint err = eglGetError();
|
||||||
|
HILOG_ERROR(NATIVE_TAG, "eglSwapBuffers failed: 0x%{public}x, id: %{public}s", err, eglCrtId.c_str());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 销毁指定id的资源
|
||||||
void EGLCore::destroy() {
|
void EGLCore::destroy() {
|
||||||
if (eglDisplay != EGL_NO_DISPLAY) {
|
if (eglDisplay == EGL_NO_DISPLAY) {
|
||||||
if (eglContext != EGL_NO_CONTEXT) {
|
return;
|
||||||
eglDestroyContext(eglDisplay, eglContext);
|
}
|
||||||
eglContext = EGL_NO_CONTEXT;
|
eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||||
}
|
// 销毁上下文
|
||||||
|
auto ctxIt = eglCtx.find(eglCrtId);
|
||||||
if (eglSurface != EGL_NO_SURFACE) {
|
if (ctxIt != eglCtx.end() && ctxIt->second != EGL_NO_CONTEXT) {
|
||||||
eglDestroySurface(eglDisplay, eglSurface);
|
eglDestroyContext(eglDisplay, ctxIt->second);
|
||||||
eglSurface = EGL_NO_SURFACE;
|
eglCtx.erase(ctxIt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 销毁Surface
|
||||||
|
auto surIt = eglSur.find(eglCrtId);
|
||||||
|
if (surIt != eglSur.end() && surIt->second != EGL_NO_SURFACE) {
|
||||||
|
eglDestroySurface(eglDisplay, surIt->second);
|
||||||
|
eglSur.erase(surIt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除Window映射(不销毁Window,由外部管理)
|
||||||
|
eglWin.erase(eglCrtId);
|
||||||
|
|
||||||
|
HILOG_INFO(NATIVE_TAG, "Destroy EGL resources for id: %s", eglCrtId.c_str());
|
||||||
|
if(eglCrtId==eglMainId){
|
||||||
eglTerminate(eglDisplay);
|
eglTerminate(eglDisplay);
|
||||||
eglDisplay = EGL_NO_DISPLAY;
|
eglDisplay = EGL_NO_DISPLAY;
|
||||||
|
eglConfig = nullptr;
|
||||||
|
eglCrtId = "";
|
||||||
|
eglMainId= "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string EGLCore::GetCurrentXCompId() {
|
||||||
|
return eglCrtId;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLSurface EGLCore::GetSurface() {
|
||||||
|
auto it = eglSur.find(eglCrtId);
|
||||||
|
return (it != eglSur.end()) ? it->second : EGL_NO_SURFACE;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLContext EGLCore::GetContext() {
|
||||||
|
auto it = eglCtx.find(eglCrtId);
|
||||||
|
return (it != eglCtx.end()) ? it->second : EGL_NO_CONTEXT;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLDisplay EGLCore::GetDisplay() {
|
||||||
|
return eglDisplay;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLConfig EGLCore::GetConfig() {
|
||||||
|
return eglConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
OHNativeWindow* EGLCore::GetOHNativeWindow() {
|
||||||
|
auto it = eglWin.find(eglCrtId);
|
||||||
|
return (it != eglWin.end()) ? it->second : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace NativeOpenCAX
|
} // namespace NativeOpenCAX
|
||||||
@ -12,33 +12,45 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#ifndef EGLCORE_H
|
#ifndef EGLMGR_H
|
||||||
#define EGLCORE_H
|
#define EGLMGR_H
|
||||||
#include <EGL/egl.h>
|
#include <EGL/egl.h>
|
||||||
#include <GLES3/gl3.h>
|
#include <GLES3/gl3.h>
|
||||||
|
#include <map>
|
||||||
|
#include <iostream>
|
||||||
#include <native_window/external_window.h>
|
#include <native_window/external_window.h>
|
||||||
|
|
||||||
namespace NativeOpenCAX {
|
namespace NativeOpenCAX {
|
||||||
|
|
||||||
|
enum EGL_TYPE{
|
||||||
|
EGL_MAIN,
|
||||||
|
EGL_SUB
|
||||||
|
};
|
||||||
|
|
||||||
class EGLCore {
|
class EGLCore {
|
||||||
public:
|
public:
|
||||||
EGLCore();
|
EGLCore();
|
||||||
~EGLCore();
|
~EGLCore();
|
||||||
|
bool InitEglCtx(OHNativeWindow* window,std::string id);
|
||||||
bool init(OHNativeWindow* window);
|
std::string GetCurrentXCompId();
|
||||||
void makeCurrent();
|
void makeCurrent();
|
||||||
bool swapBuffers();
|
bool swapBuffers();
|
||||||
void destroy();
|
void destroy();
|
||||||
EGLSurface getSurface() const { return eglSurface; }
|
EGLSurface GetSurface();
|
||||||
EGLContext getContext() const { return eglContext; }
|
EGLContext GetContext();
|
||||||
EGLDisplay getDisplay() const { return eglDisplay; }
|
EGLDisplay GetDisplay();
|
||||||
EGLConfig getConfig() const { return eglConfig; }
|
EGLConfig GetConfig();
|
||||||
OHNativeWindow* getOHNativeWindow()const{return nativeWindow;}
|
OHNativeWindow* GetOHNativeWindow();
|
||||||
private:
|
private:
|
||||||
EGLDisplay eglDisplay;
|
std::mutex crtMutex;
|
||||||
EGLContext eglContext;
|
std::mutex swapMutex;
|
||||||
EGLSurface eglSurface;
|
std::string eglCrtId;
|
||||||
EGLConfig eglConfig;
|
std::string eglMainId;
|
||||||
OHNativeWindow* nativeWindow;
|
EGLDisplay eglDisplay;
|
||||||
|
EGLConfig eglConfig;
|
||||||
|
std::unordered_map<std::string, EGLContext> eglCtx;
|
||||||
|
std::unordered_map<std::string, EGLSurface> eglSur;
|
||||||
|
std::unordered_map<std::string, OHNativeWindow*> eglWin;
|
||||||
};
|
};
|
||||||
} // namespace NativeOpenCAX
|
} // namespace NativeOpenCAX
|
||||||
#endif // EGLCORE_H
|
#endif // EGLCORE_H
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <iostream>
|
||||||
#define COLUMN_MARGIN 10
|
#define COLUMN_MARGIN 10
|
||||||
#define XC_WIDTH 800
|
#define XC_WIDTH 800
|
||||||
#define XC_HEIGHT 600
|
#define XC_HEIGHT 600
|
||||||
@ -30,11 +31,12 @@ NativeMgr NativeMgr::pluginManager_;
|
|||||||
OH_NativeXComponent_Callback NativeMgr::xSurfaceTouchEventCallBack;
|
OH_NativeXComponent_Callback NativeMgr::xSurfaceTouchEventCallBack;
|
||||||
OH_NativeXComponent_MouseEvent_Callback NativeMgr::xMouseEventCallBack;
|
OH_NativeXComponent_MouseEvent_Callback NativeMgr::xMouseEventCallBack;
|
||||||
|
|
||||||
std::string comId;
|
std::string NativeMgr::crtXCompId;
|
||||||
std::string nodeId;
|
std::unordered_map<std::string,NXInfo*> NativeMgr::NxInfos;
|
||||||
|
|
||||||
int32_t NativeMgr::hasDraw_ = 0;
|
int32_t NativeMgr::hasDraw_ = 0;
|
||||||
int32_t NativeMgr::hasChangeColor_ = 0;
|
int32_t NativeMgr::hasChangeColor_ = 0;
|
||||||
static std::map<int64_t, std::shared_ptr<NativeRenderThread>> renderThreadMap;
|
static std::map<std::string, std::shared_ptr<NativeRenderThread>> rdThreads;
|
||||||
static std::mutex mapMutex;
|
static std::mutex mapMutex;
|
||||||
|
|
||||||
std::string uuid_v4() {
|
std::string uuid_v4() {
|
||||||
@ -57,15 +59,13 @@ std::string uuid_v4() {
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NativeMgr::~NativeMgr() {
|
NativeMgr::~NativeMgr() {
|
||||||
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeMgr");
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeMgr");
|
||||||
nativeXComponentMap_.clear();
|
nativeXComponentMap_.clear();
|
||||||
int64_t id = 0;
|
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) != renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) != rdThreads.end()) {
|
||||||
renderThreadMap[id]->stop();
|
rdThreads[crtXCompId]->stop();
|
||||||
renderThreadMap.erase(id);
|
rdThreads.erase(crtXCompId);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto iter = pluginManagerMap_.begin(); iter != pluginManagerMap_.end(); ++iter) {
|
for (auto iter = pluginManagerMap_.begin(); iter != pluginManagerMap_.end(); ++iter) {
|
||||||
@ -142,46 +142,40 @@ void NativeMgr::OnSurfaceCreated(OH_NativeXComponent *component, void *window) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OH_NativeXComponent_GetXComponentSize(component, window, &bfWidth, &bfHeight);
|
OH_NativeXComponent_GetXComponentSize(component, window, &bfWidth, &bfHeight);
|
||||||
int64_t id = 0;
|
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) == renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) == rdThreads.end()) {
|
||||||
auto renderThread = std::make_shared<NativeRenderThread>();
|
auto renderThread = std::make_shared<NativeRenderThread>();
|
||||||
renderThread->initNativeReader(bfWidth, bfHeight);
|
|
||||||
HILOG_ERROR(NATIVE_TAG,"uint64_t bfSize:%{public}dX%{public}d",bfWidth,bfHeight);
|
HILOG_ERROR(NATIVE_TAG,"uint64_t bfSize:%{public}dX%{public}d",bfWidth,bfHeight);
|
||||||
renderThreadMap[id] = renderThread;
|
rdThreads[crtXCompId] = renderThread;
|
||||||
renderThread->start(reinterpret_cast<OHNativeWindow*>(window));
|
renderThread->InitRender(crtXCompId,bfWidth, bfHeight,reinterpret_cast<OHNativeWindow*>(window));
|
||||||
|
renderThread->start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void NativeMgr::OnSurfaceDestroyed(OH_NativeXComponent *component, void *window) {
|
void NativeMgr::OnSurfaceDestroyed(OH_NativeXComponent *component, void *window) {
|
||||||
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "PluginManager::OnSurfaceDestroyed");
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "PluginManager::OnSurfaceDestroyed");
|
||||||
int64_t id = 0;
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) != renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) != rdThreads.end()) {
|
||||||
renderThreadMap[id]->stop();
|
rdThreads[crtXCompId]->stop();
|
||||||
renderThreadMap.erase(id);
|
rdThreads.erase(crtXCompId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeMgr::OnSurfaceChanged(OH_NativeXComponent *component, void *window) {
|
void NativeMgr::OnSurfaceChanged(OH_NativeXComponent *component, void *window) {
|
||||||
OH_NativeXComponent_GetXComponentSize(component, window, &afWidth, &afHeight);
|
OH_NativeXComponent_GetXComponentSize(component, window, &afWidth, &afHeight);
|
||||||
|
|
||||||
int64_t id = 0;
|
|
||||||
{
|
{
|
||||||
if(bfWidth==afWidth && bfHeight==afHeight){
|
if(bfWidth==afWidth && bfHeight==afHeight){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) != renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) != rdThreads.end()) {
|
||||||
HILOG_ERROR(NATIVE_TAG,"uint64_t Size:%{public}dX%{public}d",bfWidth,bfHeight);
|
HILOG_ERROR(NATIVE_TAG,"uint64_t Size:%{public}dX%{public}d",bfWidth,bfHeight);
|
||||||
bfWidth=afWidth;
|
bfWidth=afWidth;
|
||||||
bfHeight=afHeight;
|
bfHeight=afHeight;
|
||||||
renderThreadMap[id]->resizeWindow(bfWidth, bfHeight);
|
rdThreads[crtXCompId]->resizeWindow(bfWidth, bfHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnMouseEventCB(OH_NativeXComponent* component, void* window) {
|
void OnMouseEventCB(OH_NativeXComponent* component, void* window) {
|
||||||
auto* pluginManager = NativeMgr::GetInstance();
|
auto* pluginManager = NativeMgr::GetInstance();
|
||||||
if (!pluginManager || !window) {
|
if (!pluginManager || !window) {
|
||||||
@ -212,9 +206,8 @@ void NativeMgr::OnMouseEvent(OH_NativeXComponent *comp, void *win) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NativeMgr::timestamp=mouseEvent.timestamp;
|
NativeMgr::timestamp=mouseEvent.timestamp;
|
||||||
int64_t id = 0;
|
auto it = rdThreads.find(crtXCompId);
|
||||||
auto it = renderThreadMap.find(id);
|
if (it == rdThreads.end()) {
|
||||||
if (it == renderThreadMap.end()) {
|
|
||||||
HILOG_WARN(NATIVE_TAG, "Render thread not found for rotation.");
|
HILOG_WARN(NATIVE_TAG, "Render thread not found for rotation.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -302,8 +295,10 @@ NativeMgr::NativeMgr() {
|
|||||||
xMouseEventCallBack.DispatchMouseEvent= OnMouseEventCB;
|
xMouseEventCallBack.DispatchMouseEvent= OnMouseEventCB;
|
||||||
}
|
}
|
||||||
// 创建节点组件
|
// 创建节点组件
|
||||||
ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
|
ArkUI_NodeHandle CreateNodeHandle(void* userData) {
|
||||||
nodeId=tag;
|
auto ud=static_cast<NXInfo*>(userData);
|
||||||
|
HILOG_INFO(NATIVE_TAG,"CreateNodeHandle NODE ID:%{public}s",ud->nid.c_str());
|
||||||
|
HILOG_INFO(NATIVE_TAG,"CreateNodeHandle XCOMP ID:%{public}s",ud->xid.c_str());
|
||||||
// 创建Node也名创建ROW Column等容器
|
// 创建Node也名创建ROW Column等容器
|
||||||
ArkUI_NodeHandle nodeHandel = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
|
ArkUI_NodeHandle nodeHandel = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
|
||||||
// 节点默认宽度or高度
|
// 节点默认宽度or高度
|
||||||
@ -312,17 +307,16 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
|
|||||||
// 参数1:指向数值数组
|
// 参数1:指向数值数组
|
||||||
// 参数2:数组元素个数
|
// 参数2:数组元素个数
|
||||||
// 参数3:属性名(可隐藏)
|
// 参数3:属性名(可隐藏)
|
||||||
ArkUI_AttributeItem nodeMarginItem = {nodeMarginData, 2};
|
ArkUI_AttributeItem nodeMarginItem = {nodeMarginData};
|
||||||
// 设置Node宽度or高度
|
// 设置Node宽度or高度
|
||||||
nodeAPI->setAttribute(nodeHandel, NODE_MARGIN, &nodeMarginItem);
|
nodeAPI->setAttribute(nodeHandel, NODE_MARGIN, &nodeMarginItem);
|
||||||
NativeMgr::nodeHandleMap_[tag]=nodeHandel;
|
NativeMgr::nodeHandleMap_[ud->nid]=nodeHandel;
|
||||||
// 创建XComponent组件
|
// 创建XComponent组件
|
||||||
// 组件类型Item
|
// 组件类型Item
|
||||||
ArkUI_NumberValue comTypeData[] = {ARKUI_XCOMPONENT_TYPE_SURFACE};
|
ArkUI_NumberValue comTypeData[] = {ARKUI_XCOMPONENT_TYPE_SURFACE};
|
||||||
ArkUI_AttributeItem comTypeItem = {comTypeData, 1};
|
ArkUI_AttributeItem comTypeItem = {comTypeData};
|
||||||
// 组件ID Item
|
// 组件ID Item
|
||||||
comId=uuid_v4();
|
ArkUI_AttributeItem comIdItem = {.string = ud->xid.c_str()};
|
||||||
ArkUI_AttributeItem comIdItem = {.string = comId.c_str(), .size = 1};
|
|
||||||
// 组件Surface Size
|
// 组件Surface Size
|
||||||
// 创建组件
|
// 创建组件
|
||||||
ArkUI_NodeHandle xc;
|
ArkUI_NodeHandle xc;
|
||||||
@ -334,10 +328,10 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
|
|||||||
// 焦点设置
|
// 焦点设置
|
||||||
ArkUI_NumberValue focusable[] = {1};
|
ArkUI_NumberValue focusable[] = {1};
|
||||||
focusable[0].i32 = 1;
|
focusable[0].i32 = 1;
|
||||||
ArkUI_AttributeItem focusableItem = {focusable, 1};
|
ArkUI_AttributeItem focusableItem = {focusable};
|
||||||
nodeAPI->setAttribute(xc, NODE_FOCUSABLE, &focusableItem);
|
nodeAPI->setAttribute(xc, NODE_FOCUSABLE, &focusableItem);
|
||||||
// 节点ID
|
// 节点ID
|
||||||
ArkUI_AttributeItem nodeIdItem = {.string = uuid_v4().c_str(), .size = 1};
|
ArkUI_AttributeItem nodeIdItem = {.string = ud->nid.c_str()};
|
||||||
nodeAPI->setAttribute(xc, NODE_ID, &nodeIdItem);
|
nodeAPI->setAttribute(xc, NODE_ID, &nodeIdItem);
|
||||||
|
|
||||||
auto *nativeXComponent = OH_NativeXComponent_GetNativeXComponent(xc);
|
auto *nativeXComponent = OH_NativeXComponent_GetNativeXComponent(xc);
|
||||||
@ -404,23 +398,32 @@ napi_value NativeMgr::initNativeNode(napi_env env, napi_callback_info info) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
//获取传入NodeContent实例
|
//获取传入NodeContent实例
|
||||||
size_t argCnt = 3;
|
size_t argCnt = 4;
|
||||||
napi_value args[3] = {nullptr,nullptr,nullptr};
|
napi_value args[4] = {nullptr};
|
||||||
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
|
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
|
||||||
HILOG_ERROR(NATIVE_TAG,"CreateNativeNode napi_get_cb_info failed");
|
HILOG_ERROR(NATIVE_TAG,"CreateNativeNode napi_get_cb_info failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool type;
|
||||||
|
NXInfo* nxInfo=new NXInfo;
|
||||||
|
nxInfo->nid=value2String(env, args[0]);
|
||||||
|
nxInfo->xid=value2String(env, args[1]);
|
||||||
|
napi_get_value_bool(env, args[2], &type);
|
||||||
|
|
||||||
|
crtXCompId=nxInfo->xid;
|
||||||
|
NxInfos[nxInfo->xid]=nxInfo;
|
||||||
ArkUI_NodeContentHandle _nodeContentHandle = nullptr;
|
ArkUI_NodeContentHandle _nodeContentHandle = nullptr;
|
||||||
// 获取ArkTS侧创建的NodeContent对象,映射到Native侧的
|
// 获取ArkTS侧创建的NodeContent对象,映射到Native侧的
|
||||||
OH_ArkUI_GetNodeContentFromNapiValue(env, args[2], &_nodeContentHandle);
|
OH_ArkUI_GetNodeContentFromNapiValue(env, args[3], &_nodeContentHandle);
|
||||||
// 查询指定的模块接口名
|
// 查询指定的模块接口名
|
||||||
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
|
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
|
||||||
//下面的代码主要用于创建一个Native侧和arkui侧绑定数据的结构传递传输的操作
|
//下面的代码主要用于创建一个Native侧和arkui侧绑定数据的结构传递传输的操作
|
||||||
//node_data可以理解为一个结构体等等之类的指针
|
//node_data可以理解为一个结构体等等之类的指针
|
||||||
// 生成节点名采用NODE_ID_+UUID
|
// 生成节点名采用NODE_ID_+UUID
|
||||||
std::string node_data = uuid_v4();
|
HILOG_INFO(NATIVE_TAG,"Native NODE ID:%{public}s",nxInfo->nid.c_str());
|
||||||
HILOG_INFO(NATIVE_TAG,"NODE_DATA:%{public}d",node_data.c_str());
|
HILOG_INFO(NATIVE_TAG,"Native XCOMP ID:%{public}s",nxInfo->xid.c_str());
|
||||||
// 在NodeContent对象上保存自定义数据。
|
// 在NodeContent对象上保存自定义数据。
|
||||||
int32_t ret = OH_ArkUI_NodeContent_SetUserData(_nodeContentHandle, new std::string(node_data));
|
int32_t ret = OH_ArkUI_NodeContent_SetUserData(_nodeContentHandle, static_cast<void*>(nxInfo));
|
||||||
if (ret != ARKUI_ERROR_CODE_NO_ERROR) {
|
if (ret != ARKUI_ERROR_CODE_NO_ERROR) {
|
||||||
HILOG_ERROR(NATIVE_TAG,"setUserData failed error=%{public}d",ret);
|
HILOG_ERROR(NATIVE_TAG,"setUserData failed error=%{public}d",ret);
|
||||||
}
|
}
|
||||||
@ -429,15 +432,15 @@ napi_value NativeMgr::initNativeNode(napi_env env, napi_callback_info info) {
|
|||||||
// 获取Node连接事件的对应Node的Handle
|
// 获取Node连接事件的对应Node的Handle
|
||||||
ArkUI_NodeContentHandle handle = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event);
|
ArkUI_NodeContentHandle handle = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event);
|
||||||
// 获取对应Handle的UserData
|
// 获取对应Handle的UserData
|
||||||
std::string *userDate = reinterpret_cast<std::string *>(OH_ArkUI_NodeContent_GetUserData(handle));
|
void* ud=OH_ArkUI_NodeContent_GetUserData(handle);
|
||||||
if (OH_ArkUI_NodeContentEvent_GetEventType(event) == NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) {
|
if (OH_ArkUI_NodeContentEvent_GetEventType(event) == NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) {
|
||||||
ArkUI_NodeHandle testNode;
|
ArkUI_NodeHandle testNode;
|
||||||
if (userDate) {
|
if (ud) {
|
||||||
testNode = CreateNodeHandle(*userDate);
|
testNode = CreateNodeHandle(ud);
|
||||||
delete userDate;
|
delete ud;
|
||||||
userDate = nullptr;
|
ud = nullptr;
|
||||||
} else {
|
} else {
|
||||||
testNode = CreateNodeHandle("noUserData");
|
//testNode = CreateNodeHandle(nullptr);
|
||||||
}
|
}
|
||||||
// 向NodeContent中添加子组件
|
// 向NodeContent中添加子组件
|
||||||
OH_ArkUI_NodeContent_AddNode(handle, testNode);
|
OH_ArkUI_NodeContent_AddNode(handle, testNode);
|
||||||
@ -490,11 +493,10 @@ napi_value NativeMgr::NapiLoadModel(napi_env env, napi_callback_info info) {
|
|||||||
napi_value args[1];
|
napi_value args[1];
|
||||||
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||||
std::string path = value2String(env, args[0]);
|
std::string path = value2String(env, args[0]);
|
||||||
int64_t id = 0;
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) != renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) != rdThreads.end()) {
|
||||||
renderThreadMap[id]->loadModel(std::string(path));
|
rdThreads[crtXCompId]->loadModel(std::string(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -506,11 +508,10 @@ napi_value NativeMgr::SwitchView(napi_env env, napi_callback_info info){
|
|||||||
napi_value args[1];
|
napi_value args[1];
|
||||||
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||||
std::string viewName = value2String(env, args[0]);
|
std::string viewName = value2String(env, args[0]);
|
||||||
int64_t id = 0;
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mapMutex);
|
std::lock_guard<std::mutex> lock(mapMutex);
|
||||||
if (renderThreadMap.find(id) != renderThreadMap.end()) {
|
if (rdThreads.find(crtXCompId) != rdThreads.end()) {
|
||||||
renderThreadMap[id]->swicthView(viewName);
|
rdThreads[crtXCompId]->swicthView(viewName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@ -35,11 +35,16 @@
|
|||||||
|
|
||||||
namespace NativeOpenCAX {
|
namespace NativeOpenCAX {
|
||||||
|
|
||||||
|
constexpr const int ZERO_ARG = 0;
|
||||||
constexpr const int FIRST_ARG = 1;
|
constexpr const int FIRST_ARG = 1;
|
||||||
constexpr const int SECOND_ARG = 2;
|
constexpr const int SECOND_ARG = 2;
|
||||||
constexpr const int THIRD_ARG = 3;
|
constexpr const int THIRD_ARG = 3;
|
||||||
constexpr const int FRAME_COUNT = 50;
|
constexpr const int FRAME_COUNT = 50;
|
||||||
|
|
||||||
|
struct NXInfo{
|
||||||
|
std::string nid;
|
||||||
|
std::string xid;
|
||||||
|
};
|
||||||
|
|
||||||
class NativeMgr {
|
class NativeMgr {
|
||||||
public:
|
public:
|
||||||
@ -81,7 +86,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
// [StartExclude plugin_manager_h_part]
|
// [StartExclude plugin_manager_h_part]
|
||||||
OH_NativeXComponent_TouchEvent touchEvent_;
|
OH_NativeXComponent_TouchEvent touchEvent_;
|
||||||
|
static std::string crtXCompId;
|
||||||
|
static std::unordered_map<std::string,NXInfo*> NxInfos;
|
||||||
std::string modelPath_;
|
std::string modelPath_;
|
||||||
//窗体Size改变前
|
//窗体Size改变前
|
||||||
uint64_t bfWidth;
|
uint64_t bfWidth;
|
||||||
|
|||||||
@ -29,10 +29,9 @@ v3dwin(new V3dWin)
|
|||||||
|
|
||||||
NativeRender::~NativeRender() { shapes_.clear(); }
|
NativeRender::~NativeRender() { shapes_.clear(); }
|
||||||
|
|
||||||
bool NativeRender::init(EGLCore &_eglCore) {
|
bool NativeRender::init(EGLDisplay eglDisp,EGLContext ctx,EGLConfig eglConfig) {
|
||||||
eglCore = _eglCore;
|
|
||||||
//初始化OpenGL
|
//初始化OpenGL
|
||||||
if (!v3dogd->InitV3dOGD(eglCore)) {
|
if (!v3dogd->InitV3dOGD(eglDisp,ctx,eglConfig)) {
|
||||||
HILOG_ERROR(NATIVE_TAG, "Init GraphicDriver Fail!");
|
HILOG_ERROR(NATIVE_TAG, "Init GraphicDriver Fail!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -55,7 +54,7 @@ bool NativeRender::init(EGLCore &_eglCore) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//初始化视图
|
//初始化视图
|
||||||
if (!v3dview->InitV3dView(v3dviewer->viewer,v3dwin->win)) {
|
if (!v3dview->InitV3dView(v3dviewer->viewer,ctx,v3dwin->win)) {
|
||||||
HILOG_ERROR(NATIVE_TAG, "Init View Fail!");
|
HILOG_ERROR(NATIVE_TAG, "Init View Fail!");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -32,7 +32,7 @@ public:
|
|||||||
NativeRender(int width, int height);
|
NativeRender(int width, int height);
|
||||||
~NativeRender();
|
~NativeRender();
|
||||||
|
|
||||||
bool init(EGLCore& eglCore);
|
bool init(EGLDisplay eglDisp,EGLContext ctx,EGLConfig eglConfig);
|
||||||
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);
|
||||||
@ -48,7 +48,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
EGLCore eglCore;
|
|
||||||
V3dTri* v3dTri;
|
V3dTri* v3dTri;
|
||||||
V3dTriCube* v3dTriCube;
|
V3dTriCube* v3dTriCube;
|
||||||
V3dCa* v3dcr;
|
V3dCa* v3dcr;
|
||||||
|
|||||||
@ -7,28 +7,32 @@
|
|||||||
namespace NativeOpenCAX {
|
namespace NativeOpenCAX {
|
||||||
|
|
||||||
NativeRenderThread::NativeRenderThread()
|
NativeRenderThread::NativeRenderThread()
|
||||||
: isRunning_(false),
|
: isRunning_(false),
|
||||||
nativeWindow_(nullptr)
|
EGLCoreMgr(nullptr),
|
||||||
|
RenderCoreMgr(std::map<std::string,NativeRender*>())
|
||||||
{
|
{
|
||||||
eglCore_ = new EGLCore();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeRenderThread::~NativeRenderThread() {
|
NativeRenderThread::~NativeRenderThread() {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
void NativeRenderThread::initNativeReader(int width,int height){
|
void NativeRenderThread::InitRender(std::string id,int _width,int _height,OHNativeWindow* win){
|
||||||
renderer_= new NativeRender(width,height);
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "renderLoop","Thread ID: %{public}d",std::this_thread::get_id());
|
||||||
|
currentId=id;
|
||||||
|
natWin=win;
|
||||||
|
width=_width;
|
||||||
|
height=_height;
|
||||||
}
|
}
|
||||||
bool NativeRenderThread::start(OHNativeWindow* window) {
|
|
||||||
|
bool NativeRenderThread::start() {
|
||||||
if (isRunning_) {
|
if (isRunning_) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
nativeWindow_ = window;
|
|
||||||
|
|
||||||
isRunning_ = true;
|
isRunning_ = true;
|
||||||
renderThread_ = std::thread(&NativeRenderThread::renderLoop, this);
|
|
||||||
|
rdThread = std::thread(&NativeRenderThread::renderLoop, this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,18 +49,13 @@ void NativeRenderThread::stop() {
|
|||||||
}
|
}
|
||||||
commandCondition_.notify_one();
|
commandCondition_.notify_one();
|
||||||
|
|
||||||
if (renderThread_.joinable()) {
|
if (rdThread.joinable()) {
|
||||||
renderThread_.join();
|
rdThread.join();
|
||||||
}
|
}
|
||||||
eglCore_->destroy();
|
EGLCoreMgr->destroy();
|
||||||
}
|
}
|
||||||
|
void NativeRenderThread::initFontMgr(){
|
||||||
void NativeRenderThread::renderLoop() {
|
ftMgr=Font_FontMgr::GetInstance();
|
||||||
// 初始化EGL
|
|
||||||
if (!eglCore_->init(nativeWindow_)) {
|
|
||||||
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL");
|
|
||||||
}
|
|
||||||
ftMgr=Font_FontMgr::GetInstance();
|
|
||||||
ftMgr->InitFontDataBase();
|
ftMgr->InitFontDataBase();
|
||||||
ftMgr->SetPrintErrors(true);
|
ftMgr->SetPrintErrors(true);
|
||||||
ftMgr->SetTraceAliases(true);
|
ftMgr->SetTraceAliases(true);
|
||||||
@ -89,12 +88,25 @@ void NativeRenderThread::renderLoop() {
|
|||||||
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font: %{public}s", fta->FontName().ToCString());
|
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font: %{public}s", fta->FontName().ToCString());
|
||||||
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font Path: %{public}s", fta->FontPath(Font_FontAspect_Regular).ToCString());
|
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font Path: %{public}s", fta->FontPath(Font_FontAspect_Regular).ToCString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
void NativeRenderThread::renderLoop() {
|
||||||
// 初始化OCCT渲染器
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "renderLoop",
|
||||||
if (!renderer_->init(*eglCore_)) {
|
"Thread ID: %{public}s",
|
||||||
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Failed to initialize OCCT renderer");
|
reinterpret_cast<std::ostringstream&>(std::ostringstream() << std::this_thread::get_id()).str().c_str());
|
||||||
eglCore_->destroy();
|
//初始化OCCT字体管理
|
||||||
|
initFontMgr();
|
||||||
|
if(EGLCoreMgr==nullptr){
|
||||||
|
EGLCoreMgr=new EGLCore();
|
||||||
|
}
|
||||||
|
if(EGLCoreMgr->InitEglCtx(natWin, currentId)){
|
||||||
|
NativeRender* render=new NativeRender(width,height);
|
||||||
|
RenderCoreMgr[currentId]=render;
|
||||||
|
if (render->init(EGLCoreMgr->GetDisplay(),EGLCoreMgr->GetContext(),EGLCoreMgr->GetConfig())) {
|
||||||
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit ","Render Init Done");
|
||||||
|
}else{
|
||||||
|
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Render Init Failed");
|
||||||
|
EGLCoreMgr->destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
while (isRunning_) {
|
while (isRunning_) {
|
||||||
RenderCommand command;
|
RenderCommand command;
|
||||||
@ -115,27 +127,27 @@ void NativeRenderThread::renderLoop() {
|
|||||||
if (hasCommand) {
|
if (hasCommand) {
|
||||||
switch (command.type) {
|
switch (command.type) {
|
||||||
case CMD_LOAD_MODEL:
|
case CMD_LOAD_MODEL:
|
||||||
renderer_->loadModel(command.param1);
|
RenderCoreMgr[currentId]->loadModel(command.param1);
|
||||||
break;
|
break;
|
||||||
case CMD_SET_ROTATION:
|
case CMD_SET_ROTATION:
|
||||||
renderer_->setRotation(command.param2, command.param3);
|
RenderCoreMgr[currentId]->setRotation(command.param2, command.param3);
|
||||||
break;
|
break;
|
||||||
case CMD_SET_TRANSLATION:
|
case CMD_SET_TRANSLATION:
|
||||||
renderer_->setTranslation(command.param2, command.param3);
|
RenderCoreMgr[currentId]->setTranslation(command.param2, command.param3);
|
||||||
break;
|
break;
|
||||||
case CMD_RESET_VIEW:
|
case CMD_RESET_VIEW:
|
||||||
renderer_->resetView();
|
RenderCoreMgr[currentId]->resetView();
|
||||||
break;
|
break;
|
||||||
case CMD_SET_CLEAR_COLOR:
|
case CMD_SET_CLEAR_COLOR:
|
||||||
renderer_->setClearColor(command.param2, command.param3, command.param4, command.param5);
|
RenderCoreMgr[currentId]->setClearColor(command.param2, command.param3, command.param4, command.param5);
|
||||||
break;
|
break;
|
||||||
case CMD_RESIZE:
|
case CMD_RESIZE:
|
||||||
rdWidth = static_cast<int>(command.param2);
|
rdWidth = static_cast<int>(command.param2);
|
||||||
rdHeight = static_cast<int>(command.param3);
|
rdHeight = static_cast<int>(command.param3);
|
||||||
renderer_->resize(rdWidth, rdHeight);
|
RenderCoreMgr[currentId]->resize(rdWidth, rdHeight);
|
||||||
break;
|
break;
|
||||||
case CMD_SWITCH_VIEW:
|
case CMD_SWITCH_VIEW:
|
||||||
renderer_->SwitchView(command.param1);
|
RenderCoreMgr[currentId]->SwitchView(command.param1);
|
||||||
break;
|
break;
|
||||||
case CMD_EXIT:
|
case CMD_EXIT:
|
||||||
isRunning_ = false;
|
isRunning_ = false;
|
||||||
@ -146,9 +158,9 @@ void NativeRenderThread::renderLoop() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 渲染
|
// 渲染
|
||||||
eglCore_->makeCurrent();
|
EGLCoreMgr->makeCurrent();
|
||||||
renderer_->render();
|
RenderCoreMgr[currentId]->render();
|
||||||
eglCore_->swapBuffers();
|
EGLCoreMgr->swapBuffers();
|
||||||
// 调用渲染完成回调
|
// 调用渲染完成回调
|
||||||
Callback callback;
|
Callback callback;
|
||||||
{
|
{
|
||||||
|
|||||||
@ -16,14 +16,15 @@
|
|||||||
|
|
||||||
namespace NativeOpenCAX {
|
namespace NativeOpenCAX {
|
||||||
|
|
||||||
|
|
||||||
class NativeRenderThread {
|
class NativeRenderThread {
|
||||||
public:
|
public:
|
||||||
NativeRenderThread();
|
NativeRenderThread();
|
||||||
~NativeRenderThread();
|
~NativeRenderThread();
|
||||||
|
|
||||||
bool start(OHNativeWindow* window);
|
bool start();
|
||||||
void stop();
|
void stop();
|
||||||
void initNativeReader(int width,int height);
|
void InitRender(std::string id,int width,int height,OHNativeWindow* window);
|
||||||
void loadModel(const std::string& filePath);
|
void loadModel(const std::string& filePath);
|
||||||
void setCameraRotationMode(bool state);
|
void setCameraRotationMode(bool state);
|
||||||
void setRotation(float xAngle, float yAngle);
|
void setRotation(float xAngle, float yAngle);
|
||||||
@ -34,20 +35,20 @@ public:
|
|||||||
void swicthView(std::string strView);
|
void swicthView(std::string strView);
|
||||||
using Callback = std::function<void()>;
|
using Callback = std::function<void()>;
|
||||||
void registerRenderCompleteCallback(Callback callback);
|
void registerRenderCompleteCallback(Callback callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void initFontMgr();
|
||||||
void renderLoop();
|
void renderLoop();
|
||||||
|
std::thread rdThread;
|
||||||
std::thread renderThread_;
|
|
||||||
std::atomic<bool> isRunning_;
|
std::atomic<bool> isRunning_;
|
||||||
|
std::string currentId;
|
||||||
OHNativeWindow* nativeWindow_;
|
std::map<std::string,NativeRender*> RenderCoreMgr;
|
||||||
EGLCore* eglCore_;
|
EGLCore* EGLCoreMgr;
|
||||||
NativeRender* renderer_;
|
|
||||||
|
|
||||||
std::mutex commandMutex_;
|
std::mutex commandMutex_;
|
||||||
std::condition_variable commandCondition_;
|
std::condition_variable commandCondition_;
|
||||||
|
OHNativeWindow* natWin;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
enum CommandType {
|
enum CommandType {
|
||||||
CMD_LOAD_MODEL,
|
CMD_LOAD_MODEL,
|
||||||
CMD_SET_ROTATION,
|
CMD_SET_ROTATION,
|
||||||
@ -75,6 +76,7 @@ private:
|
|||||||
Handle(Font_FontMgr) ftMgr ;
|
Handle(Font_FontMgr) ftMgr ;
|
||||||
int rdWidth=0;
|
int rdWidth=0;
|
||||||
int rdHeight=0;
|
int rdHeight=0;
|
||||||
|
|
||||||
};
|
};
|
||||||
} // namespace NaitveRenderThread
|
} // namespace NaitveRenderThread
|
||||||
#endif //NATIVE_RENDER_THREAD_H
|
#endif //NATIVE_RENDER_THREAD_H
|
||||||
@ -14,13 +14,13 @@ namespace NativeOpenCAX {
|
|||||||
|
|
||||||
V3dOGD::V3dOGD() : graphicDriver(nullptr), displayConnection(nullptr) {}
|
V3dOGD::V3dOGD() : graphicDriver(nullptr), displayConnection(nullptr) {}
|
||||||
V3dOGD::~V3dOGD() {}
|
V3dOGD::~V3dOGD() {}
|
||||||
bool V3dOGD::InitV3dOGD(EGLCore& eglCore) {
|
bool V3dOGD::InitV3dOGD(EGLDisplay eglDisp,EGLContext ctx,EGLConfig eglConfig) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
displayConnection = new Aspect_DisplayConnection();
|
displayConnection = new Aspect_DisplayConnection();
|
||||||
graphicDriver = new OpenGl_GraphicDriver(displayConnection, false);
|
graphicDriver = new OpenGl_GraphicDriver(displayConnection, false);
|
||||||
graphicDriver->ChangeOptions().buffersNoSwap = true;
|
graphicDriver->ChangeOptions().buffersNoSwap = true;
|
||||||
graphicDriver->InitEglContext(eglGetCurrentDisplay(), eglGetCurrentContext(), eglCore.getConfig());
|
graphicDriver->InitEglContext(eglDisp, ctx, eglConfig);
|
||||||
HILOG_INFO(NATIVE_TAG, "InitGraphicDriver Done");
|
HILOG_INFO(NATIVE_TAG, "InitGraphicDriver Done");
|
||||||
return true;
|
return true;
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
|
|||||||
@ -18,7 +18,7 @@ class V3dOGD{
|
|||||||
public:
|
public:
|
||||||
V3dOGD();
|
V3dOGD();
|
||||||
~V3dOGD();
|
~V3dOGD();
|
||||||
bool InitV3dOGD(EGLCore& eglCore);
|
bool InitV3dOGD(EGLDisplay eglDisp,EGLContext ctx,EGLConfig eglConfig);
|
||||||
public:
|
public:
|
||||||
Handle(OpenGl_GraphicDriver) graphicDriver;
|
Handle(OpenGl_GraphicDriver) graphicDriver;
|
||||||
Handle(Aspect_DisplayConnection) displayConnection;
|
Handle(Aspect_DisplayConnection) displayConnection;
|
||||||
|
|||||||
@ -18,10 +18,10 @@ namespace NativeOpenCAX {
|
|||||||
V3dView::V3dView() : view(nullptr) {}
|
V3dView::V3dView() : view(nullptr) {}
|
||||||
V3dView::~V3dView() {}
|
V3dView::~V3dView() {}
|
||||||
|
|
||||||
bool V3dView::InitV3dView(Handle(V3d_Viewer) & viewer,Handle(Aspect_NeutralWindow)& win) {
|
bool V3dView::InitV3dView(Handle(V3d_Viewer) & viewer,EGLContext ctx,Handle(Aspect_NeutralWindow)& win) {
|
||||||
try {
|
try {
|
||||||
view = viewer->CreateView();
|
view = viewer->CreateView();
|
||||||
view->SetWindow(win, (Aspect_RenderingContext)eglGetCurrentContext());
|
view->SetWindow(win, ctx);
|
||||||
HILOG_INFO(NATIVE_TAG, "InitView Done");
|
HILOG_INFO(NATIVE_TAG, "InitView Done");
|
||||||
return true;
|
return true;
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class V3dView{
|
|||||||
public:
|
public:
|
||||||
V3dView();
|
V3dView();
|
||||||
~V3dView();
|
~V3dView();
|
||||||
bool InitV3dView(Handle(V3d_Viewer)& viewer,Handle(Aspect_NeutralWindow)& win);
|
bool InitV3dView(Handle(V3d_Viewer)& viewer,EGLContext ctx,Handle(Aspect_NeutralWindow)& win);
|
||||||
void InitViewOption();
|
void InitViewOption();
|
||||||
void SetClearColor(float r, float g, float b, float a);
|
void SetClearColor(float r, float g, float b, float a);
|
||||||
void MustBeResized();
|
void MustBeResized();
|
||||||
|
|||||||
@ -2,6 +2,6 @@ export interface NativeXOpenCAX {
|
|||||||
setFrameRate(nodeId: string, min: number, max: number, expected: number): void;
|
setFrameRate(nodeId: string, min: number, max: number, expected: number): void;
|
||||||
setNeedSoftKeyboard(nodeId: string, need: boolean): void;
|
setNeedSoftKeyboard(nodeId: string, need: boolean): void;
|
||||||
}
|
}
|
||||||
export function InitNativeNode(xId:string,initType:boolean,nodeContent: any): void;
|
export function InitNativeNode(nId:string,xId:string,initType:boolean,nodeContent: any): void;
|
||||||
export function loadModel(stepFilePath: string): void;
|
export function loadModel(stepFilePath: string): void;
|
||||||
export function switchView(viewName: string): void;
|
export function switchView(viewName: string): void;
|
||||||
@ -5,6 +5,7 @@ import { mwInfo } from '../AppStorageV2Class';
|
|||||||
import {NodeContent} from '@kit.ArkUI';
|
import {NodeContent} from '@kit.ArkUI';
|
||||||
|
|
||||||
export interface XComp{
|
export interface XComp{
|
||||||
|
nId?:string;
|
||||||
xId?:string;
|
xId?:string;
|
||||||
name?:string;
|
name?:string;
|
||||||
icon?:string;
|
icon?:string;
|
||||||
@ -32,16 +33,18 @@ function XNRandomId(): string {
|
|||||||
export function InitXCompNode(name:string){
|
export function InitXCompNode(name:string){
|
||||||
//数组为0表示初始化,Native侧这初始化主上下文,否则Native侧初始化共享上下文
|
//数组为0表示初始化,Native侧这初始化主上下文,否则Native侧初始化共享上下文
|
||||||
let ncInfo:XComp={};
|
let ncInfo:XComp={};
|
||||||
ncInfo.xId=XNRandomId();
|
ncInfo.nId='n_'+XNRandomId();
|
||||||
ncInfo.name=name+XNRandomId();
|
ncInfo.xId='x_'+XNRandomId();
|
||||||
|
ncInfo.name=name+ncInfo.xId;
|
||||||
ncInfo.node=new NodeContent();
|
ncInfo.node=new NodeContent();
|
||||||
if(XComps.length==0)
|
if(XComps.length==0)
|
||||||
{
|
{
|
||||||
NativeOpenCAX.InitNativeNode(ncInfo.xId,true,ncInfo.node);
|
NativeOpenCAX.InitNativeNode(ncInfo.nId,ncInfo.xId,true,ncInfo.node);
|
||||||
}else{
|
}else{
|
||||||
NativeOpenCAX.InitNativeNode(ncInfo.xId,false,ncInfo.node);
|
NativeOpenCAX.InitNativeNode(ncInfo.nId,ncInfo.xId,false,ncInfo.node);
|
||||||
}
|
}
|
||||||
XComps.push(ncInfo);
|
XComps.push(ncInfo);
|
||||||
|
console.log(`ArkUI Node ID: ${ncInfo.nId}`);
|
||||||
console.log(`ArkUI XComp ID: ${ncInfo.xId}`);
|
console.log(`ArkUI XComp ID: ${ncInfo.xId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user