告警清理

Signed-off-by: limingkang <limingkang1@huawei.com>
Change-Id: Idc52e88615322e6dfd3a98a90f3a51554821a565
This commit is contained in:
limingkang 2024-07-18 18:13:30 +08:00
parent 5706af7723
commit 38d3bfa5f0
11 changed files with 99 additions and 107 deletions

View File

@ -2,6 +2,7 @@
cmake_minimum_required(VERSION 3.4.1) cmake_minimum_required(VERSION 3.4.1)
project(XComponent) project(XComponent)
set(CMAKE_CXX_STANDARD 17)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
add_definitions(-DOHOS_PLATFORM) add_definitions(-DOHOS_PLATFORM)

View File

@ -23,25 +23,25 @@
#include "common.h" #include "common.h"
namespace OHOS { namespace OHOS {
PluginManager PluginManager::m_pluginManager; PluginManager PluginManager::pluginmanager_;
PluginManager::~PluginManager() PluginManager::~PluginManager()
{ {
for (auto iter = m_nativeXComponentMap.begin(); iter != m_nativeXComponentMap.end(); ++iter) { for (auto iter = nativeXComponentMap_.begin(); iter != nativeXComponentMap_.end(); ++iter) {
if (iter->second != nullptr) { if (iter->second != nullptr) {
delete iter->second; delete iter->second;
iter->second = nullptr; iter->second = nullptr;
} }
} }
m_nativeXComponentMap.clear(); nativeXComponentMap_.clear();
for (auto iter = m_pluginRenderMap.begin(); iter != m_pluginRenderMap.end(); ++iter) { for (auto iter = pluginrendermap_.begin(); iter != pluginrendermap_.end(); ++iter) {
if (iter->second != nullptr) { if (iter->second != nullptr) {
delete iter->second; delete iter->second;
iter->second = nullptr; iter->second = nullptr;
} }
} }
m_pluginRenderMap.clear(); pluginrendermap_.clear();
} }
void PluginManager::Export(napi_env env, napi_value exports) void PluginManager::Export(napi_env env, napi_value exports)
@ -76,7 +76,7 @@ void PluginManager::Export(napi_env env, napi_value exports)
if ((context != nullptr) && (nativeXComponent != nullptr)) { if ((context != nullptr) && (nativeXComponent != nullptr)) {
context->SetNativeXComponent(id, nativeXComponent); context->SetNativeXComponent(id, nativeXComponent);
auto render = context->GetRender(id); auto render = context->GetRender(id);
OH_NativeXComponent_RegisterCallback(nativeXComponent, &PluginRender::m_callback); OH_NativeXComponent_RegisterCallback(nativeXComponent, &PluginRender::callback_);
if (render != nullptr) { if (render != nullptr) {
render->Export(env, exports); render->Export(env, exports);
} }
@ -89,27 +89,21 @@ void PluginManager::SetNativeXComponent(std::string &id, OH_NativeXComponent *na
return; return;
} }
if (m_nativeXComponentMap.find(id) == m_nativeXComponentMap.end()) { auto [iter, inserted] = nativeXComponentMap_.try_emplace(id, nativeXComponent);
m_nativeXComponentMap[id] = nativeXComponent; if (!inserted && iter->second != nativeXComponent) {
return; delete iter->second;
} iter->second = nativeXComponent;
if (m_nativeXComponentMap[id] != nativeXComponent) {
OH_NativeXComponent *tmp = m_nativeXComponentMap[id];
delete tmp;
tmp = nullptr;
m_nativeXComponentMap[id] = nativeXComponent;
} }
} }
PluginRender *PluginManager::GetRender(std::string &id) PluginRender *PluginManager::GetRender(std::string &id)
{ {
if (m_pluginRenderMap.find(id) == m_pluginRenderMap.end()) { if (pluginrendermap_.find(id) == pluginrendermap_.end()) {
PluginRender *instance = PluginRender::GetInstance(id); PluginRender *instance = PluginRender::GetInstance(id);
m_pluginRenderMap[id] = instance; pluginrendermap_[id] = instance;
return instance; return instance;
} }
return m_pluginRenderMap[id]; return pluginrendermap_[id];
} }
} // namespace OHOS } // namespace OHOS

View File

@ -30,7 +30,7 @@ public:
static PluginManager *GetInstance() static PluginManager *GetInstance()
{ {
return &PluginManager::m_pluginManager; return &PluginManager::pluginmanager_;
} }
void SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent); void SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent);
@ -38,10 +38,10 @@ public:
void Export(napi_env env, napi_value exports); void Export(napi_env env, napi_value exports);
private: private:
static PluginManager m_pluginManager; static PluginManager pluginmanager_;
std::unordered_map<std::string, OH_NativeXComponent *> m_nativeXComponentMap; std::unordered_map<std::string, OH_NativeXComponent *> nativeXComponentMap_;
std::unordered_map<std::string, PluginRender *> m_pluginRenderMap; std::unordered_map<std::string, PluginRender *> pluginrendermap_;
}; };
} // namespace OHOS } // namespace OHOS
#endif // PLUGIN_MANAGER_H #endif // PLUGIN_MANAGER_H

View File

@ -32,23 +32,21 @@ bool EGLCore::EglContextInit(void *window, int width, int height)
return false; return false;
} }
m_width = width; width_ = width;
m_height = height; height_ = height;
if (m_width > 0) { widthpercent_ = FIFTY_PERCENT * height_ / width_;
m_widthPercent = FIFTY_PERCENT * m_height / m_width; eglwindow_ = static_cast<EGLNativeWindowType>(window);
}
m_eglWindow = static_cast<EGLNativeWindowType>(window);
// Init display. // Init display.
m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); egldisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (m_eglDisplay == EGL_NO_DISPLAY) { if (egldisplay_ == EGL_NO_DISPLAY) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglGetDisplay: unable to get EGL display"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglGetDisplay: unable to get EGL display");
return false; return false;
} }
EGLint majorVersion; EGLint majorVersion;
EGLint minorVersion; EGLint minorVersion;
if (!eglInitialize(m_eglDisplay, &majorVersion, &minorVersion)) { if (!eglInitialize(egldisplay_, &majorVersion, &minorVersion)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore",
"eglInitialize: unable to get initialize EGL display"); "eglInitialize: unable to get initialize EGL display");
return false; return false;
@ -57,7 +55,7 @@ bool EGLCore::EglContextInit(void *window, int width, int height)
// Select configuration. // Select configuration.
const EGLint maxConfigSize = 1; const EGLint maxConfigSize = 1;
EGLint numConfigs; EGLint numConfigs;
if (!eglChooseConfig(m_eglDisplay, ATTRIB_LIST, &m_eglConfig, maxConfigSize, &numConfigs)) { if (!eglChooseConfig(egldisplay_, ATTRIB_LIST, &eglconfig_, maxConfigSize, &numConfigs)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglChooseConfig: unable to choose configs"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglChooseConfig: unable to choose configs");
return false; return false;
} }
@ -67,27 +65,27 @@ bool EGLCore::EglContextInit(void *window, int width, int height)
bool EGLCore::CreateEnvironment() bool EGLCore::CreateEnvironment()
{ {
if (m_eglWindow == nullptr) { if (eglwindow_ == nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "m_eglWindow is null"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglwindow_ is null");
return false; return false;
} }
m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig, m_eglWindow, nullptr); eglsurface_ = eglCreateWindowSurface(egldisplay_, eglconfig_, eglwindow_, nullptr);
if (m_eglSurface == nullptr) { if (eglsurface_ == nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore",
"eglCreateWindowSurface: unable to create WindowSurface"); "eglCreateWindowSurface: unable to create WindowSurface");
return false; return false;
} }
// Create context. // Create context.
m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, EGL_NO_CONTEXT, CONTEXT_ATTRIBS); eglcontext_ = eglCreateContext(egldisplay_, eglconfig_, EGL_NO_CONTEXT, CONTEXT_ATTRIBS);
if (!eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext)) { if (!eglMakeCurrent(egldisplay_, eglsurface_, eglsurface_, eglcontext_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglMakeCurrent failed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglMakeCurrent failed");
return false; return false;
} }
// Create program. // Create program.
m_program = CreateProgram(VERTEX_SHADER, FRAGMENT_SHADER); program_ = CreateProgram(VERTEX_SHADER, FRAGMENT_SHADER);
if (m_program == PROGRAM_ERROR) { if (program_ == PROGRAM_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "CreateProgram: unable to create program"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "CreateProgram: unable to create program");
return false; return false;
} }
@ -138,7 +136,7 @@ GLuint EGLCore::CreateProgram(const char *vertexShader, const char *fragShader)
GLint infoLen = 0; GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) { if (infoLen > 1) {
char *infoLog = (char *)malloc(sizeof(char) * (infoLen + 1)); char *infoLog = static_cast<char*>(malloc(sizeof(char) * (infoLen + 1)));
std::fill(infoLog, infoLog + infoLen, 0); std::fill(infoLog, infoLog + infoLen, 0);
glGetProgramInfoLog(program, infoLen, nullptr, infoLog); glGetProgramInfoLog(program, infoLen, nullptr, infoLog);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glLinkProgram error = %s", infoLog); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glLinkProgram error = %s", infoLog);
@ -181,7 +179,7 @@ GLuint EGLCore::LoadShader(GLenum type, const char *shaderSrc)
return PROGRAM_ERROR; return PROGRAM_ERROR;
} }
char *infoLog = (char *)malloc(sizeof(char) * (infoLen + 1)); char *infoLog = static_cast<char*>(malloc(sizeof(char) * (infoLen + 1)));
if (infoLog != nullptr) { if (infoLog != nullptr) {
std::fill(infoLog, infoLog + infoLen, 0); std::fill(infoLog, infoLog + infoLen, 0);
glGetShaderInfoLog(shader, infoLen, nullptr, infoLog); glGetShaderInfoLog(shader, infoLen, nullptr, infoLog);
@ -195,7 +193,7 @@ GLuint EGLCore::LoadShader(GLenum type, const char *shaderSrc)
void EGLCore::Draw() void EGLCore::Draw()
{ {
m_flag = false; flag_ = false;
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "Draw"); OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "Draw");
GLint position = PrepareDraw(); GLint position = PrepareDraw();
if (position == POSITION_ERROR) { if (position == POSITION_ERROR) {
@ -210,10 +208,10 @@ void EGLCore::Draw()
} }
const GLfloat rectangleVertices[] = { const GLfloat rectangleVertices[] = {
-m_widthPercent, FIFTY_PERCENT, -widthpercent_, FIFTY_PERCENT,
m_widthPercent, FIFTY_PERCENT, widthpercent_, FIFTY_PERCENT,
m_widthPercent, -FIFTY_PERCENT, widthpercent_, -FIFTY_PERCENT,
-m_widthPercent, -FIFTY_PERCENT -widthpercent_, -FIFTY_PERCENT
}; };
if (!ExecuteDraw(position, DRAW_COLOR, rectangleVertices, sizeof(rectangleVertices))) { if (!ExecuteDraw(position, DRAW_COLOR, rectangleVertices, sizeof(rectangleVertices))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw rectangle failed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw rectangle failed");
@ -225,24 +223,24 @@ void EGLCore::Draw()
return; return;
} }
m_flag = true; flag_ = true;
} }
GLint EGLCore::PrepareDraw() GLint EGLCore::PrepareDraw()
{ {
if ((m_eglDisplay == nullptr) || (m_eglSurface == nullptr) || (m_eglContext == nullptr) || if ((egldisplay_ == nullptr) || (eglsurface_ == nullptr) || (eglcontext_ == nullptr) ||
(!eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext))) { (!eglMakeCurrent(egldisplay_, eglsurface_, eglsurface_, eglcontext_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "PrepareDraw: param error"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "PrepareDraw: param error");
return POSITION_ERROR; return POSITION_ERROR;
} }
// The gl function has no return value. // The gl function has no return value.
glViewport(0, 0, m_width, m_height); glViewport(0, 0, width_, height_);
glClearColor(0, 255, 0, 1); //255 is color value, 1 is transparency glClearColor(0, 255, 0, 1); //255 is color value, 1 is transparency
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_program); glUseProgram(program_);
return glGetAttribLocation(m_program, "a_position"); return glGetAttribLocation(program_, "a_position");
} }
bool EGLCore::ExecuteDraw(GLint position, const GLfloat *color, const GLfloat rectangleVertices[], bool EGLCore::ExecuteDraw(GLint position, const GLfloat *color, const GLfloat rectangleVertices[],
@ -268,20 +266,20 @@ bool EGLCore::FinishDraw()
// The gl function has no return value. // The gl function has no return value.
glFlush(); glFlush();
glFinish(); glFinish();
return eglSwapBuffers(m_eglDisplay, m_eglSurface); return eglSwapBuffers(egldisplay_, eglsurface_);
} }
void EGLCore::Release() void EGLCore::Release()
{ {
if ((m_eglDisplay == nullptr) || (m_eglSurface == nullptr) || (!eglDestroySurface(m_eglDisplay, m_eglSurface))) { if ((egldisplay_ == nullptr) || (eglsurface_ == nullptr) || (!eglDestroySurface(egldisplay_, eglsurface_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroySurface failed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroySurface failed");
} }
if ((m_eglDisplay == nullptr) || (m_eglContext == nullptr) || (!eglDestroyContext(m_eglDisplay, m_eglContext))) { if ((egldisplay_ == nullptr) || (eglcontext_ == nullptr) || (!eglDestroyContext(egldisplay_, eglcontext_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroyContext failed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroyContext failed");
} }
if ((m_eglDisplay == nullptr) || (!eglTerminate(m_eglDisplay))) { if ((egldisplay_ == nullptr) || (!eglTerminate(egldisplay_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglTerminate failed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglTerminate failed");
} }
} }

View File

@ -33,20 +33,21 @@ private:
GLuint LoadShader(GLenum type, const char *shaderSrc); GLuint LoadShader(GLenum type, const char *shaderSrc);
GLuint CreateProgram(const char *vertexShader, const char *fragShader); GLuint CreateProgram(const char *vertexShader, const char *fragShader);
GLint PrepareDraw(); GLint PrepareDraw();
bool ExecuteDraw(GLint position, const GLfloat *color, const GLfloat rectangleVertices[], unsigned long vertSize); static bool ExecuteDraw(GLint position, const GLfloat *color, const GLfloat rectangleVertices[],
unsigned long vertSize);
bool FinishDraw(); bool FinishDraw();
private: private:
EGLNativeWindowType m_eglWindow; EGLNativeWindowType eglwindow_;
EGLDisplay m_eglDisplay = EGL_NO_DISPLAY; EGLDisplay egldisplay_ = EGL_NO_DISPLAY;
EGLConfig m_eglConfig = EGL_NO_CONFIG_KHR; EGLConfig eglconfig_ = EGL_NO_CONFIG_KHR;
EGLSurface m_eglSurface = EGL_NO_SURFACE; EGLSurface eglsurface_ = EGL_NO_SURFACE;
EGLContext m_eglContext = EGL_NO_CONTEXT; EGLContext eglcontext_ = EGL_NO_CONTEXT;
GLuint m_program; GLuint program_ = 0;
bool m_flag = false; bool flag_ = false;
int m_width; int width_ = 0;
int m_height; int height_ = 0;
GLfloat m_widthPercent; GLfloat widthpercent_;
}; };
} // namespace OHOS } // namespace OHOS
#endif // EGL_CORE_H #endif // EGL_CORE_H

View File

@ -23,8 +23,8 @@
#include "common.h" #include "common.h"
namespace OHOS { namespace OHOS {
std::unordered_map<std::string, PluginRender *> PluginRender::m_instance; std::unordered_map<std::string, PluginRender *> PluginRender::instance_;
OH_NativeXComponent_Callback PluginRender::m_callback; OH_NativeXComponent_Callback PluginRender::callback_;
void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
{ {
@ -49,7 +49,7 @@ void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
uint64_t height; uint64_t height;
int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height); int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) { if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) {
render->m_eglCore->EglContextInit(window, width, height); render->eglcore_->EglContextInit(window, width, height);
} }
} }
@ -74,23 +74,23 @@ void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
PluginRender::Release(id); PluginRender::Release(id);
} }
PluginRender::PluginRender(std::string &id) PluginRender::PluginRender(const std::string &id)
{ {
this->m_id = id; this->id_ = id;
this->m_eglCore = new EGLCore(); this->eglcore_ = new EGLCore();
OH_NativeXComponent_Callback *renderCallback = &PluginRender::m_callback; OH_NativeXComponent_Callback *renderCallback = &PluginRender::callback_;
renderCallback->OnSurfaceCreated = OnSurfaceCreatedCB; renderCallback->OnSurfaceCreated = OnSurfaceCreatedCB;
renderCallback->OnSurfaceDestroyed = OnSurfaceDestroyedCB; renderCallback->OnSurfaceDestroyed = OnSurfaceDestroyedCB;
} }
PluginRender *PluginRender::GetInstance(std::string &id) PluginRender *PluginRender::GetInstance(std::string &id)
{ {
if (m_instance.find(id) == m_instance.end()) { if (instance_.find(id) == instance_.end()) {
PluginRender *instance = new PluginRender(id); PluginRender *instance = new PluginRender(id);
m_instance[id] = instance; instance_[id] = instance;
return instance; return instance;
} else { } else {
return m_instance[id]; return instance_[id];
} }
} }
@ -133,8 +133,8 @@ napi_value PluginRender::NapiDrawRectangle(napi_env env, napi_callback_info info
std::string id(idStr); std::string id(idStr);
PluginRender *render = PluginRender::GetInstance(id); PluginRender *render = PluginRender::GetInstance(id);
if (render) { if (render) {
render->m_eglCore->Draw(); render->eglcore_->Draw();
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "PluginRender", "render->m_eglCore->Draw() executed"); OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "PluginRender", "render->eglcore_->Draw() executed");
} }
return nullptr; return nullptr;
} }
@ -158,12 +158,12 @@ void PluginRender::Release(std::string &id)
{ {
PluginRender *render = PluginRender::GetInstance(id); PluginRender *render = PluginRender::GetInstance(id);
if (render != nullptr) { if (render != nullptr) {
render->m_eglCore->Release(); render->eglcore_->Release();
delete render->m_eglCore; delete render->eglcore_;
render->m_eglCore = nullptr; render->eglcore_ = nullptr;
delete render; delete render;
render = nullptr; render = nullptr;
m_instance.erase(m_instance.find(id)); instance_.erase(instance_.find(id));
} }
} }
} // namespace OHOS } // namespace OHOS

View File

@ -26,26 +26,26 @@
namespace OHOS { namespace OHOS {
class PluginRender { class PluginRender {
public: public:
explicit PluginRender(std::string &id); explicit PluginRender(const std::string &id);
~PluginRender() ~PluginRender()
{ {
if (m_eglCore != nullptr) { if (eglcore_ != nullptr) {
m_eglCore->Release(); eglcore_->Release();
delete m_eglCore; delete eglcore_;
m_eglCore = nullptr; eglcore_ = nullptr;
} }
} }
static PluginRender *GetInstance(std::string &id); static PluginRender *GetInstance(std::string &id);
static void Release(std::string &id); static void Release(std::string &id);
static napi_value NapiDrawRectangle(napi_env env, napi_callback_info info); static napi_value NapiDrawRectangle(napi_env env, napi_callback_info info);
void Export(napi_env env, napi_value exports); static void Export(napi_env env, napi_value exports);
public: public:
static std::unordered_map<std::string, PluginRender *> m_instance; static std::unordered_map<std::string, PluginRender *> instance_;
static OH_NativeXComponent_Callback m_callback; static OH_NativeXComponent_Callback callback_;
EGLCore *m_eglCore; EGLCore *eglcore_ = nullptr;
std::string m_id; std::string id_ = "";
}; };
} // namespace OHOS } // namespace OHOS
#endif // PLUGIN_RENDER_H #endif // PLUGIN_RENDER_H

View File

@ -2,6 +2,7 @@
cmake_minimum_required(VERSION 3.4.1) cmake_minimum_required(VERSION 3.4.1)
project(drawing_test) project(drawing_test)
set(CMAKE_CXX_STANDARD 17)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${NATIVERENDER_ROOT_PATH} include_directories(${NATIVERENDER_ROOT_PATH}

View File

@ -46,16 +46,10 @@ void PluginManager::SetNativeXComponent(std::string &id, OH_NativeXComponent *na
return; return;
} }
if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) { auto [iter, inserted] = nativeXComponentMap_.try_emplace(id, nativeXComponent);
nativeXComponentMap_[id] = nativeXComponent; if (!inserted && iter->second != nativeXComponent) {
return; delete iter->second;
} iter->second = nativeXComponent;
if (nativeXComponentMap_[id] != nativeXComponent) {
OH_NativeXComponent *tmp = nativeXComponentMap_[id];
delete tmp;
tmp = nullptr;
nativeXComponentMap_[id] = nativeXComponent;
} }
} }

View File

@ -301,6 +301,9 @@ static void OnSurfaceCreatedCB(OH_NativeXComponent *component,
} }
std::string id(idStr); std::string id(idStr);
auto render = SampleBitMap::GetInstance(id); auto render = SampleBitMap::GetInstance(id);
if (render == nullptr) {
return;
}
OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window); OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window);
render->SetNativeWindow(nativeWindow); render->SetNativeWindow(nativeWindow);

View File

@ -52,11 +52,11 @@ public:
void DisPlay(); void DisPlay();
void ConstructPath(); void ConstructPath();
void SetPenAndBrush(); void SetPenAndBrush();
void Export(napi_env env, napi_value exports); static void Export(napi_env env, napi_value exports);
void RegisterCallback(OH_NativeXComponent *nativeXComponent); void RegisterCallback(OH_NativeXComponent *nativeXComponent);
void Destroy(); void Destroy();
static SampleBitMap *GetInstance(std::string &id); static SampleBitMap *GetInstance(std::string &id);
std::string id_; std::string id_ = "";
private: private:
OH_NativeXComponent_Callback renderCallback_; OH_NativeXComponent_Callback renderCallback_;