删除无用代码

This commit is contained in:
JackLee 2026-02-25 17:59:14 +08:00
parent 898c2633e0
commit e69e157960
9 changed files with 15 additions and 1340 deletions

View File

@ -30,13 +30,9 @@ add_library(opencax SHARED
# Header # Header
NativeEGLOCCT/EGLConst.h NativeEGLOCCT/EGLConst.h
NativeEGLOCCT/EGLCore.h NativeEGLOCCT/EGLCore.h
NativeEGLOCCT/EGLRender.h
NativeEGLOCCT/NativeRender.h
NativeEGLOCCT/NativeManager.h NativeEGLOCCT/NativeManager.h
# Cpp Src # Cpp Src
NativeEGLOCCT/EGLCore.cpp NativeEGLOCCT/EGLCore.cpp
NativeEGLOCCT/EGLRender.cpp
NativeEGLOCCT/NativeRender.cpp
NativeEGLOCCT/NativeManager.cpp NativeEGLOCCT/NativeManager.cpp
napi_init.cpp napi_init.cpp
) )

View File

@ -1,329 +0,0 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [Start egl_render]
// EGLRender.cpp
#include "EGLRender.h"
#include "EGLConst.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <hilog/log.h>
#include <iostream>
namespace {
void Rotate2d(GLfloat centerX, GLfloat centerY, GLfloat *rotateX, GLfloat *rotateY, GLfloat theta)
{
GLfloat tempX = cos(theta) * (*rotateX - centerX) - sin(theta) * (*rotateY - centerY);
GLfloat tempY = sin(theta) * (*rotateX - centerX) + cos(theta) * (*rotateY - centerY);
*rotateX = tempX + centerX;
*rotateY = tempY + centerY;
}
GLuint LoadShader(GLenum type, const char *shaderSrc)
{
if ((type <= 0) || (shaderSrc == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "glCreateShader type or shaderSrc error");
return PROGRAM_ERROR;
}
GLuint shader = glCreateShader(type);
if (shader == 0) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "glCreateShader unable to load shader");
return PROGRAM_ERROR;
}
// The gl function has no return value.
glShaderSource(shader, 1, &shaderSrc, nullptr);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (compiled != 0) {
return shader;
}
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen <= 1) {
glDeleteShader(shader);
return PROGRAM_ERROR;
}
char *infoLog = (char *)malloc(sizeof(char) * (infoLen + 1));
if (infoLog != nullptr) {
memset(infoLog, 0, infoLen + 1);
glGetShaderInfoLog(shader, infoLen, nullptr, infoLog);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "glCompileShader error = %s", infoLog);
free(infoLog);
infoLog = nullptr;
}
glDeleteShader(shader);
return PROGRAM_ERROR;
}
// 创建program
GLuint CreateProgram(const char *vertexShader, const char *fragShader)
{
if ((vertexShader == nullptr) || (fragShader == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender",
"createProgram: vertexShader or fragShader is null");
return PROGRAM_ERROR;
}
GLuint vertex = LoadShader(GL_VERTEX_SHADER, vertexShader);
if (vertex == PROGRAM_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "createProgram vertex error");
return PROGRAM_ERROR;
}
GLuint fragment = LoadShader(GL_FRAGMENT_SHADER, fragShader);
if (fragment == PROGRAM_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "createProgram fragment error");
return PROGRAM_ERROR;
}
GLuint program = glCreateProgram();
if (program == PROGRAM_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "createProgram program error");
glDeleteShader(vertex);
glDeleteShader(fragment);
return PROGRAM_ERROR;
}
// 该gl函数没有返回值。
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (linked != 0) {
glDeleteShader(vertex);
glDeleteShader(fragment);
return program;
}
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "createProgram linked error");
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = (char *)malloc(sizeof(char) * (infoLen + 1));
memset(infoLog, 0, infoLen + 1);
glGetProgramInfoLog(program, infoLen, nullptr, infoLog);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "glLinkProgram error = %s", infoLog);
free(infoLog);
infoLog = nullptr;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
glDeleteProgram(program);
return PROGRAM_ERROR;
}
} // namespace
bool EGLRender::SetUpEGLContext(void *window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLRender", "EglContextInit execute");
eglWindow_ = (EGLNativeWindowType)(window);
// 初始化display。
eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (eglDisplay_ == EGL_NO_DISPLAY) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "eglGetDisplay: unable to get EGL display");
return false;
}
EGLint majorVersion;
EGLint minorVersion;
if (!eglInitialize(eglDisplay_, &majorVersion, &minorVersion)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender",
"eglInitialize: unable to get initialize EGL display");
return false;
};
// 选择配置。
const EGLint maxConfigSize = 1;
EGLint numConfigs;
if (!eglChooseConfig(eglDisplay_, ATTRIB_LIST, &eglConfig_, maxConfigSize, &numConfigs)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "eglChooseConfig: unable to choose configs");
return false;
};
// 创建环境。
// 创建 Surface。
eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, eglWindow_, NULL);
if (eglSurface_ == nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender",
"eglCreateWindowSurface: unable to create surface");
return false;
}
if (eglSurface_ == nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender",
"eglCreateWindowSurface: unable to create surface");
return false;
}
// 创建上下文。
eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, CONTEXT_ATTRIBS);
if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "eglMakeCurrent failed");
return false;
}
return true;
}
GLint EGLRender::PrepareDraw()
{
if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (eglContext_ == nullptr) ||
(!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "PrepareDraw: param error");
return POSITION_ERROR;
}
// 该gl函数没有返回值。
glViewport(DEFAULT_X_POSITION, DEFAULT_Y_POSITION, width_, height_);
glClearColor(GL_RED_DEFAULT, GL_GREEN_DEFAULT, GL_BLUE_DEFAULT, GL_ALPHA_DEFAULT);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program_);
return glGetAttribLocation(program_, POSITION_NAME);
}
// 绘制五角星
void EGLRender::DrawStar(bool drawColor)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLRender", "Draw");
GLint position = PrepareDraw();
if (position == POSITION_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "Draw get position failed");
return;
}
// 绘制背景
if (!ExecuteDraw(position, BACKGROUND_COLOR, BACKGROUND_RECTANGLE_VERTICES)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "Draw execute draw background failed");
return;
}
// 将其划分为五个四边形,并计算其中一个四边形的顶点
GLfloat rotateX = 0;
GLfloat rotateY = FIFTY_PERCENT * height_;
GLfloat centerX = 0;
// 将角度 54° 和 18° 转换为弧度
GLfloat centerY = -rotateY * (M_PI / 180 * 54) * (M_PI / 180 * 18);
// 将角度 18° 转换为弧度
GLfloat leftX = -rotateY * (M_PI / 180 * 18);
GLfloat leftY = 0;
// 将角度 18° 转换为弧度
GLfloat rightX = rotateY * (M_PI / 180 * 18);
GLfloat rightY = 0;
// 确定绘制四边形的顶点,使用绘制区域的百分比表示
const GLfloat shapeVertices[] = {centerX / width_, centerY / height_, leftX / width_, leftY / height_,
rotateX / width_, rotateY / height_, rightX / width_, rightY / height_};
auto color = drawColor ? DRAW_COLOR : CHANGE_COLOR;
if (!ExecuteDraw(position, color, shapeVertices)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "Draw execute draw shape failed");
return;
}
// 将角度 72° 转换为弧度
GLfloat rad = M_PI / 180 * 72;
// 旋转四次。
// 在头文件EGLConst.h中定义NUM_0的值为0NUM_4的值为4
for (int i = NUM_0; i < NUM_4; ++i) {
// 旋转得其他四个四边形的顶点
Rotate2d(centerX, centerY, &rotateX, &rotateY, rad);
Rotate2d(centerX, centerY, &leftX, &leftY, rad);
Rotate2d(centerX, centerY, &rightX, &rightY, rad);
// 确定绘制四边形的顶点,使用绘制区域的百分比表示
const GLfloat shapeVertices[] = {centerX / width_, centerY / height_, leftX / width_, leftY / height_,
rotateX / width_, rotateY / height_, rightX / width_, rightY / height_};
// 绘制图形
if (!ExecuteDraw(position, color, shapeVertices)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "Draw execute draw shape failed");
return;
}
}
// 将绘制命令提交给GPUGPU执行完成后将渲染结果显示到屏幕
glFlush();
glFinish();
if (!eglSwapBuffers(eglDisplay_, eglSurface_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "Draw FinishDraw failed");
return;
}
}
// [StartExclude egl_render]
void EGLRender::Clear()
{
if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (eglContext_ == nullptr) ||
(!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "PrepareDraw: param error");
return;
}
// The gl function has no return value.
glViewport(DEFAULT_X_POSITION, DEFAULT_Y_POSITION, width_, height_);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glFinish();
eglSwapBuffers(eglDisplay_, eglSurface_);
}
// [EndExclude egl_render]
bool EGLRender::ExecuteDraw(GLint position, const GLfloat *color, const GLfloat shapeVertices[])
{
if ((position > 0) || (color == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLRender", "ExecuteDraw: param error");
return false;
}
// 该gl函数没有返回值。
glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
glEnableVertexAttribArray(position);
glVertexAttrib4fv(1, color);
glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
glDisableVertexAttribArray(position);
return true;
}
void EGLRender::SetEGLWindowSize(int width, int height)
{
width_ = width;
height_ = height;
}
// 释放相关资源
void EGLRender::DestroySurface()
{
if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (!eglDestroySurface(eglDisplay_, eglSurface_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "EGLRender", "Release eglDestroySurface failed");
}
if ((eglDisplay_ == nullptr) || (eglContext_ == nullptr) || (!eglDestroyContext(eglDisplay_, eglContext_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "EGLRender", "Release eglDestroySurface failed");
}
if ((eglDisplay_ == nullptr) || (!eglTerminate(eglDisplay_))) {
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "EGLRender", "Release eglDestroySurface failed");
}
eglDisplay_ = EGL_NO_DISPLAY;
eglSurface_ = EGL_NO_SURFACE;
eglContext_ = EGL_NO_CONTEXT;
}
// [End egl_render]

View File

@ -1,53 +0,0 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVEXCOMPONENT_EGLRENDER_H
#define NATIVEXCOMPONENT_EGLRENDER_H
// [Start egl_render_h]
// EGLRender.h
#include "EGLConst.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglplatform.h>
#include <GLES3/gl3.h>
#include <string>
class EGLRender {
public:
bool SetUpEGLContext(void *window);
void SetEGLWindowSize(int width, int height);
void DrawStar(bool drawColor);
void DestroySurface();
// [StartExclude egl_render_h]
void Clear();
// [EndExclude egl_render_h]
std::string xcomponentId;
EGLNativeWindowType eglWindow_;
EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
EGLSurface eglSurface_ = EGL_NO_SURFACE;
EGLContext eglContext_ = EGL_NO_CONTEXT;
GLuint program_;
int width_ = 0;
int height_ = 0;
private:
GLint PrepareDraw();
bool ExecuteDraw(GLint position, const GLfloat *color, const GLfloat shapeVertices[]);
};
// [End egl_render_h]
#endif // NATIVEXCOMPONENT_EGLRENDER_H

View File

@ -62,39 +62,33 @@ NativeManager::~NativeManager()
} }
pluginManagerMap_.clear(); pluginManagerMap_.clear();
} }
//Surface回调事件
// [Start surface_holder]
void OnSurfaceCreatedNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceCreatedNative(OH_ArkUI_SurfaceHolder *holder)
{ {
auto window = OH_ArkUI_XComponent_GetNativeWindow(holder); // 获取native window auto window = OH_ArkUI_XComponent_GetNativeWindow(holder); // 获取native window
auto render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); auto eglCore = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->SetUpEGLContext(window); // 初始化egl环境 eglCore->EglContextInit(window, 800, 600);
} }
void OnSurfaceChangedNative(OH_ArkUI_SurfaceHolder *holder, uint64_t width, uint64_t height) void OnSurfaceChangedNative(OH_ArkUI_SurfaceHolder *holder, uint64_t width, uint64_t height)
{ {
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->SetEGLWindowSize(width, height); // 设置绘制区域大小 render->UpdateSize(width, height); // 设置绘制区域大小
render->DrawStar(true); // 绘制五角星 render->Draw(NativeManager::hasDraw_); // 绘制五角星
} }
void OnSurfaceDestroyedNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceDestroyedNative(OH_ArkUI_SurfaceHolder *holder)
{ {
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "onBind", "on destroyed"); OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "onBind", "on destroyed");
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->DestroySurface(); // 销毁eglSurface相关资源 render->Release(); // 销毁eglSurface相关资源
} }
void OnSurfaceShowNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceShowNative(OH_ArkUI_SurfaceHolder *holder)
{ {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface show"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface show");
} }
void OnSurfaceHideNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceHideNative(OH_ArkUI_SurfaceHolder *holder)
{ {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface hide"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface hide");
} }
void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t targetTimestamp) void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t targetTimestamp)
{ {
if (!NativeManager::surfaceHolderMap_.count(node)) { if (!NativeManager::surfaceHolderMap_.count(node)) {
@ -107,7 +101,6 @@ void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t t
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "OnFrameCallback count = %{public}ld", count); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "OnFrameCallback count = %{public}ld", count);
} }
} }
void onEvent(ArkUI_NodeEvent *event) void onEvent(ArkUI_NodeEvent *event)
{ {
auto eventType = OH_ArkUI_NodeEvent_GetEventType(event); // 获取组件事件类型 auto eventType = OH_ArkUI_NodeEvent_GetEventType(event); // 获取组件事件类型
@ -115,13 +108,11 @@ void onEvent(ArkUI_NodeEvent *event)
if (eventType == NODE_TOUCH_EVENT) { if (eventType == NODE_TOUCH_EVENT) {
ArkUI_NodeHandle handle = OH_ArkUI_NodeEvent_GetNodeHandle(event); // 获取触发该事件的组件对象 ArkUI_NodeHandle handle = OH_ArkUI_NodeEvent_GetNodeHandle(event); // 获取触发该事件的组件对象
auto holder = NativeManager::surfaceHolderMap_[handle]; auto holder = NativeManager::surfaceHolderMap_[handle];
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->DrawStar(false); // 绘制五角星 render->Draw(NativeManager::hasDraw_); // 绘制五角星
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on touch"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on touch");
} }
} }
// [End surface_holder]
// [EndExclude plugin_manager_cpp]
static std::string value2String(napi_env env, napi_value value) static std::string value2String(napi_env env, napi_value value)
{ {
size_t stringSize = 0; size_t stringSize = 0;
@ -131,46 +122,6 @@ static std::string value2String(napi_env env, napi_value value)
napi_get_value_string_utf8(env, value, &valueString[0], stringSize+1, &stringSize); napi_get_value_string_utf8(env, value, &valueString[0], stringSize+1, &stringSize);
return valueString; return valueString;
} }
// [StartExclude plugin_manager_cpp]
napi_value NativeManager::GetXComponentStatus(napi_env env, napi_callback_info info)
{
napi_value hasDraw;
napi_value hasChangeColor;
napi_status ret = napi_create_int32(env, hasDraw_, &(hasDraw));
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "GetXComponentStatus", "napi_create_int32 hasDraw_ error");
return nullptr;
}
ret = napi_create_int32(env, hasChangeColor_, &(hasChangeColor));
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "GetXComponentStatus", "napi_create_int32 hasChangeColor_ error");
return nullptr;
}
napi_value obj;
ret = napi_create_object(env, &obj);
if (ret != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "GetXComponentStatus", "napi_create_object error");
return nullptr;
}
ret = napi_set_named_property(env, obj, "hasDraw", hasDraw);
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "GetXComponentStatus", "napi_set_named_property hasDraw error");
return nullptr;
}
ret = napi_set_named_property(env, obj, "hasChangeColor", hasChangeColor);
if (ret != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "GetXComponentStatus",
"napi_set_named_property hasChangeColor error");
return nullptr;
}
return obj;
}
// [Start plugin_draw_pattern]
napi_value NativeManager::NapiDrawPattern(napi_env env, napi_callback_info info) napi_value NativeManager::NapiDrawPattern(napi_env env, napi_callback_info info)
{ {
// [StartExclude plugin_draw_pattern] // [StartExclude plugin_draw_pattern]
@ -194,9 +145,7 @@ napi_value NativeManager::NapiDrawPattern(napi_env env, napi_callback_info info)
return nullptr; return nullptr;
} }
// [End plugin_draw_pattern] //XComponent回调事件
// [Start plugin_on_surface_created]
// 定义一个函数OnSurfaceCreatedCB(),封装初始化环境与绘制背景
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
{ {
// [StartExclude plugin_on_surface_created] // [StartExclude plugin_on_surface_created]
@ -217,9 +166,6 @@ void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->OnSurfaceCreated(component, window); pluginManger->OnSurfaceCreated(component, window);
} }
// [End plugin_on_surface_created]
// [Start plugin_on_surface_changed]
// 定义一个函数OnSurfaceChangedCB()
void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window)
{ {
// [StartExclude plugin_on_surface_changed] // [StartExclude plugin_on_surface_changed]
@ -237,9 +183,6 @@ void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window)
// 封装OnSurfaceChanged方法 // 封装OnSurfaceChanged方法
pluginManger->OnSurfaceChanged(component, window); pluginManger->OnSurfaceChanged(component, window);
} }
// [End plugin_on_surface_changed]
// [Start plugin_on_surface_destroyed]
// 定义一个函数OnSurfaceDestroyedCB()将NativeRender类内释放资源的方法Release()封装在其中
void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)
{ {
// [StartExclude plugin_on_surface_destroyed] // [StartExclude plugin_on_surface_destroyed]
@ -256,9 +199,6 @@ void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->OnSurfaceDestroyed(component, window); pluginManger->OnSurfaceDestroyed(component, window);
} }
// [End plugin_on_surface_destroyed]
// [Start plugin_dispatch_touch_event]
// 定义一个函数DispatchTouchEventCB(),响应触摸事件时触发该回调
void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) void DispatchTouchEventCB(OH_NativeXComponent* component, void* window)
{ {
// [StartExclude plugin_dispatch_touch_event] // [StartExclude plugin_dispatch_touch_event]
@ -275,7 +215,6 @@ void DispatchTouchEventCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->DispatchTouchEvent(component, window); pluginManger->DispatchTouchEvent(component, window);
} }
// [End plugin_dispatch_touch_event]
NativeManager::NativeManager() NativeManager::NativeManager()
{ {
eglcore_ = new EGLCore(); eglcore_ = new EGLCore();
@ -284,7 +223,6 @@ NativeManager::NativeManager()
callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
callback_.DispatchTouchEvent = DispatchTouchEventCB; callback_.DispatchTouchEvent = DispatchTouchEventCB;
} }
// [Start plugin_create_native_node]
ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) ArkUI_NodeHandle CreateNodeHandle(const std::string &tag)
{ {
ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER); ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
@ -332,7 +270,6 @@ ArkUI_NodeHandle CreateNodeHandle(const std::string &tag)
nodeAPI->addChild(column, xc); nodeAPI->addChild(column, xc);
return column; return column;
} }
napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info) napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info)
{ {
if ((env == nullptr) || (info == nullptr)) { if ((env == nullptr) || (info == nullptr)) {
@ -381,7 +318,6 @@ napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info
} }
return nullptr; return nullptr;
} }
// [End plugin_create_native_node]
void NativeManager::OnSurfaceCreated(OH_NativeXComponent* component, void* window) void NativeManager::OnSurfaceCreated(OH_NativeXComponent* component, void* window)
{ {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceCreated"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceCreated");
@ -395,12 +331,10 @@ void NativeManager::OnSurfaceCreated(OH_NativeXComponent* component, void* windo
eglcore_->Background(); eglcore_->Background();
} }
} }
void NativeManager::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window) void NativeManager::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window)
{ {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceDestroyed"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceDestroyed");
} }
void NativeManager::DispatchTouchEvent(OH_NativeXComponent* component, void* window) void NativeManager::DispatchTouchEvent(OH_NativeXComponent* component, void* window)
{ {
int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_); int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_);
@ -446,125 +380,6 @@ void NativeManager::OnSurfaceChanged(OH_NativeXComponent* component, void* windo
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native",
"OnSurfaceChanged ret=%{public}d width=%{public}lu, height=%{public}lu", ret, width_, height_); "OnSurfaceChanged ret=%{public}d width=%{public}lu, height=%{public}lu", ret, width_, height_);
} }
napi_value NativeManager::GetContext(napi_env env, napi_callback_info info)
{
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetContext env or info is null");
return nullptr;
}
size_t argCnt = 1;
napi_value args[1] = { nullptr };
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetContext napi_get_cb_info failed");
}
if (argCnt != 1) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
}
napi_valuetype valuetype;
if (napi_typeof(env, args[0], &valuetype) != napi_ok) {
napi_throw_type_error(env, NULL, "napi_typeof failed");
return nullptr;
}
if (valuetype != napi_number) {
napi_throw_type_error(env, NULL, "Wrong type of arguments");
return nullptr;
}
int64_t value;
if (napi_get_value_int64(env, args[0], &value) != napi_ok) {
napi_throw_type_error(env, NULL, "napi_get_value_int64 failed");
return nullptr;
}
napi_value exports;
if (napi_create_object(env, &exports) != napi_ok) {
napi_throw_type_error(env, NULL, "napi_create_object failed");
return nullptr;
}
return exports;
}
void NativeManager::SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent)
{
if (nativeXComponent == nullptr) {
return;
}
nativeXComponentMap_[id] = nativeXComponent;
}
// [EndExclude plugin_manager_cpp]
napi_value NativeManager::BindNode(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
std::string nodeId = value2String(env, args[0]);
ArkUI_NodeHandle handle;
OH_ArkUI_GetNodeHandleFromNapiValue(env, args[1], &handle); // 获取 nodeHandle
OH_ArkUI_SurfaceHolder *holder = OH_ArkUI_SurfaceHolder_Create(handle); // 获取 SurfaceHolder
nodeHandleMap_[nodeId] = handle;
surfaceHolderMap_[handle] = holder;
auto callback = OH_ArkUI_SurfaceCallback_Create(); // 创建 SurfaceCallback
callbackMap_[holder] = callback;
auto render = new EGLRender();
OH_ArkUI_SurfaceHolder_SetUserData(holder, render); // 将render保存在holder中
OH_ArkUI_SurfaceCallback_SetSurfaceCreatedEvent(callback, OnSurfaceCreatedNative); // 注册OnSurfaceCreated回调
OH_ArkUI_SurfaceCallback_SetSurfaceChangedEvent(callback, OnSurfaceChangedNative); // 注册OnSurfaceChanged回调
OH_ArkUI_SurfaceCallback_SetSurfaceDestroyedEvent(callback, OnSurfaceDestroyedNative); // 注册OnSurfaceDestroyed回调
OH_ArkUI_SurfaceCallback_SetSurfaceShowEvent(callback, OnSurfaceShowNative); // 注册OnSurfaceShow回调
OH_ArkUI_SurfaceCallback_SetSurfaceHideEvent(callback, OnSurfaceHideNative); // 注册OnSurfaceHide回调
OH_ArkUI_XComponent_RegisterOnFrameCallback(handle, OnFrameCallbackNative); // 注册OnFrameCallback回调
OH_ArkUI_SurfaceHolder_AddSurfaceCallback(holder, callback); // 注册SurfaceCallback回调
if (!nodeAPI->addNodeEventReceiver(handle, onEvent)) { // 添加事件监听,返回成功码 0
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "addNodeEventReceiver error");
}
if (!nodeAPI->registerNodeEvent(handle, NODE_TOUCH_EVENT, 0, nullptr)) { // 用C接口注册touch事件返回成功码 0
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "registerTouchEvent error");
}
provider_ = OH_ArkUI_AccessibilityProvider_Create(handle); // 创建一个ArkUI_AccessibilityProvider类型的对象
/**
* ArkUI_AccessibilityProvider后
* https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/ui/ndk-accessibility-xcomponent.md
* **/
return nullptr;
}
napi_value NativeManager::UnbindNode(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
std::string nodeId = value2String(env, args[0]);
ArkUI_NodeHandle node;
if (nodeHandleMap_.find(nodeId) == nodeHandleMap_.end()) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "SetNeedSoftKeyboard", "nodeId not exit error");
return nullptr;
}
node = nodeHandleMap_[nodeId];
OH_ArkUI_XComponent_UnregisterOnFrameCallback(node); // 解注册帧回调
OH_ArkUI_AccessibilityProvider_Dispose(provider_); // 销毁 ArkUI_AccessibilityProvider
auto holder = surfaceHolderMap_[node];
if (NativeManager::callbackMap_.count(holder)) {
auto callback = NativeManager::callbackMap_[holder];
OH_ArkUI_SurfaceHolder_RemoveSurfaceCallback(holder, callback); // 移除SurfaceCallback
OH_ArkUI_SurfaceCallback_Dispose(callback); // 销毁surfaceCallback
NativeManager::callbackMap_.erase(holder);
}
auto render = reinterpret_cast<NativeRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
delete render; // 销毁EGLRender对象
OH_ArkUI_SurfaceHolder_Dispose(holder); // 销毁surfaceHolder
nodeAPI->disposeNode(node); // 销毁nodeHandle
nodeHandleMap_.erase(nodeId);
return nullptr;
}
napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info) napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info)
{ {
size_t argc = 4; size_t argc = 4;
@ -585,7 +400,6 @@ napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info)
OH_ArkUI_XComponent_SetExpectedFrameRateRange(node, range); // 设置期望帧率 OH_ArkUI_XComponent_SetExpectedFrameRateRange(node, range); // 设置期望帧率
return nullptr; return nullptr;
} }
napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info info) napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info info)
{ {
size_t argc = 2; size_t argc = 2;
@ -604,67 +418,5 @@ napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info i
OH_ArkUI_XComponent_SetNeedSoftKeyboard(node, needSoftKeyboard); // 设置是否需要软键盘 OH_ArkUI_XComponent_SetNeedSoftKeyboard(node, needSoftKeyboard); // 设置是否需要软键盘
return nullptr; return nullptr;
} }
// [End plugin_manager_cpp]
napi_value NativeManager::Initialize(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
std::string nodeId = value2String(env, args[0]);
ArkUI_NodeHandle node;
if (nodeHandleMap_.find(nodeId) == nodeHandleMap_.end()) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "SetNeedSoftKeyboard", "nodeId not exit error");
return nullptr;
}
node = nodeHandleMap_[nodeId];
bool autoInitialize = 1;
OH_ArkUI_XComponent_SetAutoInitialize(node, autoInitialize);
OH_ArkUI_XComponent_Initialize(node);
bool isInitialized;
OH_ArkUI_XComponent_IsInitialized(node, &isInitialized);
auto holder = surfaceHolderMap_[node];
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->DrawStar(true); // 绘制五角星
return nullptr;
}
napi_value NativeManager::Finalize(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
std::string nodeId = value2String(env, args[0]);
ArkUI_NodeHandle node;
if (nodeHandleMap_.find(nodeId) == nodeHandleMap_.end()) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "SetNeedSoftKeyboard", "nodeId not exit error");
return nullptr;
}
node = nodeHandleMap_[nodeId];
auto holder = surfaceHolderMap_[node];
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->Clear();
OH_ArkUI_XComponent_Finalize(node);
return nullptr;
}
napi_value NativeManager::DrawStar(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
std::string nodeId = value2String(env, args[0]);
ArkUI_NodeHandle node;
if (nodeHandleMap_.find(nodeId) == nodeHandleMap_.end()) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "SetNeedSoftKeyboard", "nodeId not exit error");
return nullptr;
}
node = nodeHandleMap_[nodeId];
auto holder = surfaceHolderMap_[node];
EGLRender* render = reinterpret_cast<EGLRender*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "onBind", "YGB DrawStar w:[%{public}d],h[%{public}d]", render->width_,
render->height_);
render->DrawStar(true); // 绘制五角星
return nullptr;
}
} // namespace NativeXComponentSample } // namespace NativeXComponentSample

View File

@ -23,11 +23,9 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "EGLCore.h" #include "EGLCore.h"
#include "EGLRender.h"
#include "arkui/native_node.h" #include "arkui/native_node.h"
#include "arkui/native_node_napi.h" #include "arkui/native_node_napi.h"
#include "NativeRender.h"
// [Start plugin_manager_h_part] // [Start plugin_manager_h_part]
// plugin_manager.h // plugin_manager.h
namespace NativeOpenCAX { namespace NativeOpenCAX {
@ -51,41 +49,18 @@ public:
return &NativeManager::pluginManager_; return &NativeManager::pluginManager_;
} }
static napi_value createNativeNode(napi_env env, napi_callback_info info); static napi_value createNativeNode(napi_env env, napi_callback_info info);
static napi_value GetXComponentStatus(napi_env env, napi_callback_info info);
static napi_value NapiDrawPattern(napi_env env, napi_callback_info info); static napi_value NapiDrawPattern(napi_env env, napi_callback_info info);
// [StartExclude plugin_manager_h]
static napi_value GetContext(napi_env env, napi_callback_info info);
// [EndExclude plugin_manager_h_part]
static napi_value BindNode(napi_env env, napi_callback_info info);
static napi_value UnbindNode(napi_env env, napi_callback_info info);
static napi_value SetFrameRate(napi_env env, napi_callback_info info); static napi_value SetFrameRate(napi_env env, napi_callback_info info);
static napi_value SetNeedSoftKeyboard(napi_env env, napi_callback_info info); static napi_value SetNeedSoftKeyboard(napi_env env, napi_callback_info info);
// [StartExclude plugin_manager_h_part]
static napi_value Initialize(napi_env env, napi_callback_info info);
static napi_value Finalize(napi_env env, napi_callback_info info);
static napi_value DrawStar(napi_env env, napi_callback_info info);
// [EndExclude plugin_manager_h]
// CApi XComponent // CApi XComponent
void OnSurfaceChanged(OH_NativeXComponent* component, void* window); void OnSurfaceChanged(OH_NativeXComponent* component, void* window);
void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window);
void DispatchTouchEvent(OH_NativeXComponent* component, void* window); void DispatchTouchEvent(OH_NativeXComponent* component, void* window);
void OnSurfaceCreated(OH_NativeXComponent* component, void* window); void OnSurfaceCreated(OH_NativeXComponent* component, void* window);
// [StartExclude plugin_manager_h]
void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent);
NativeRender* GetRender(std::string& id);
void Export(napi_env env, napi_value exports);
// [EndExclude plugin_manager_h]
private: private:
static NativeManager pluginManager_; static NativeManager pluginManager_;
std::unordered_map<std::string, OH_NativeXComponent*> nativeXComponentMap_; std::unordered_map<std::string, OH_NativeXComponent*> nativeXComponentMap_;
// [StartExclude plugin_manager_h]
std::unordered_map<std::string, NativeRender*> pluginRenderMap_;
// [EndExclude plugin_manager_h]
std::unordered_map<std::string, NativeManager*> pluginManagerMap_; std::unordered_map<std::string, NativeManager*> pluginManagerMap_;
// [EndExclude plugin_manager_h_part]
public: public:
// [StartExclude plugin_manager_h_part] // [StartExclude plugin_manager_h_part]
EGLCore *eglcore_; EGLCore *eglcore_;
@ -94,15 +69,10 @@ public:
OH_NativeXComponent_TouchEvent touchEvent_; OH_NativeXComponent_TouchEvent touchEvent_;
static int32_t hasDraw_; static int32_t hasDraw_;
static int32_t hasChangeColor_; static int32_t hasChangeColor_;
// [EndExclude plugin_manager_h_part]
// [StartExclude plugin_manager_h]
static std::unordered_map<std::string, ArkUI_NodeHandle> nodeHandleMap_; static std::unordered_map<std::string, ArkUI_NodeHandle> nodeHandleMap_;
static std::unordered_map<void *, OH_ArkUI_SurfaceCallback *> callbackMap_; static std::unordered_map<void *, OH_ArkUI_SurfaceCallback *> callbackMap_;
static std::unordered_map<void *, OH_ArkUI_SurfaceHolder *> surfaceHolderMap_; static std::unordered_map<void *, OH_ArkUI_SurfaceHolder *> surfaceHolderMap_;
static ArkUI_AccessibilityProvider *provider_; static ArkUI_AccessibilityProvider *provider_;
// [EndExclude plugin_manager_h]
}; };
// [End plugin_manager_h] }
} // namespace NativeXComponentSample
// [End plugin_manager_h_part]
#endif // NATIVE_XCOMPONENT_PLUGIN_MANAGER_H #endif // NATIVE_XCOMPONENT_PLUGIN_MANAGER_H

View File

@ -1,575 +0,0 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdint>
#include <hilog/log.h>
#include <js_native_api.h>
#include <js_native_api_types.h>
#include <string>
#include "common.h"
#include "NativeManager.h"
#include "NativeRender.h"
#define EXPECTED_FRAME_RATE 30
namespace NativeOpenCAX {
namespace {
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB");
if ((component == nullptr) || (window == nullptr)) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB: component or window is null");
return;
}
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB: Unable to get XComponent id");
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
uint64_t width;
uint64_t height;
int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) {
if (render->eglCore_->EglContextInit(window, width, height)) {
render->eglCore_->Background();
}
}
}
void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceChangedCB");
if ((component == nullptr) || (window == nullptr)) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceChangedCB: component or window is null");
return;
}
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceChangedCB: Unable to get XComponent id");
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnSurfaceChanged(component, window);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "surface changed");
}
}
void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB");
if ((component == nullptr) || (window == nullptr)) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB: component or window is null");
return;
}
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB: Unable to get XComponent id");
return;
}
std::string id(idStr);
NativeRender::Release(id);
}
void DispatchTouchEventCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB");
if ((component == nullptr) || (window == nullptr)) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB: component or window is null");
return;
}
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB: Unable to get XComponent id");
return;
}
std::string id(idStr);
NativeRender* render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnTouchEvent(component, window);
}
}
void DispatchMouseEventCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "DispatchMouseEventCB");
int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
OH_NativeXComponent_ExtraMouseEventInfo* mouseEventInfo = NULL;
ret = OH_NativeXComponent_GetExtraMouseEventInfo(component, &mouseEventInfo);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
uint64_t mouseKey;
ret = OH_NativeXComponent_GetMouseEventModifierKeyStates(mouseEventInfo, &mouseKey);
if (ret == OH_NATIVEXCOMPONENT_RESULT_FAILED || ret == OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER) {
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnMouseEvent(component, window);
}
}
void DispatchHoverEventCB(OH_NativeXComponent* component, bool isHover)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "DispatchHoverEventCB");
int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnHoverEvent(component, isHover);
}
}
void OnFocusEventCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnFocusEventCB");
int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnFocusEvent(component, window);
}
}
void OnBlurEventCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnBlurEventCB");
int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnBlurEvent(component, window);
}
}
void OnKeyEventCB(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnKeyEventCB");
int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
return;
}
std::string id(idStr);
auto render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->OnKeyEvent(component, window);
}
}
} // namespace
std::unordered_map<std::string, NativeRender*> NativeRender::instance_;
int32_t NativeRender::hasDraw_ = 0;
int32_t NativeRender::hasChangeColor_ = 0;
NativeRender::NativeRender(std::string& id)
{
this->id_ = id;
this->eglCore_ = new EGLCore();
}
NativeRender* NativeRender::GetInstance(std::string& id)
{
if (instance_.find(id) == instance_.end()) {
NativeRender* instance = new NativeRender(id);
instance_[id] = instance;
return instance;
} else {
return instance_[id];
}
}
void NativeRender::Export(napi_env env, napi_value exports)
{
if ((env == nullptr) || (exports == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "Export: env or exports is null");
return;
}
napi_property_descriptor desc[] = {
{"drawPatternX", nullptr, NativeRender::NapiDrawPattern, nullptr, nullptr,
nullptr, napi_default, nullptr},
{"getStatusX", nullptr, NativeRender::TestGetXComponentStatus, nullptr, nullptr,
nullptr, napi_default, nullptr}};
if (napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "Export: napi_define_properties failed");
}
}
// NAPI registration method type napi_callback. If no value is returned, nullptr is returned.
napi_value NativeRender::NapiDrawPattern(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern");
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern: env or info is null");
return nullptr;
}
napi_value thisArg;
if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern: napi_get_cb_info fail");
return nullptr;
}
napi_value exportInstance;
if (napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern: napi_get_named_property fail");
return nullptr;
}
OH_NativeXComponent* nativeXComponent = nullptr;
if (napi_unwrap(env, exportInstance, reinterpret_cast<void**>(&nativeXComponent)) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern: napi_unwrap fail");
return nullptr;
}
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "NapiDrawPattern: Unable to get XComponent id");
return nullptr;
}
std::string id(idStr);
NativeRender* render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->eglCore_->Draw(hasDraw_);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "render->eglCore_->Draw() executed");
}
return nullptr;
}
void NativeRender::Release(std::string& id)
{
NativeRender* render = NativeRender::GetInstance(id);
if (render != nullptr) {
render->eglCore_->Release();
delete render->eglCore_;
render->eglCore_ = nullptr;
instance_.erase(instance_.find(id));
}
}
void NativeRender::OnSurfaceChanged(OH_NativeXComponent* component, void* window)
{
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceChanged: Unable to get XComponent id");
return;
}
std::string id(idStr);
NativeRender* render = NativeRender::GetInstance(id);
double offsetX;
double offsetY;
OH_NativeXComponent_GetXComponentOffset(component, window, &offsetX, &offsetY);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OH_NativeXComponent_GetXComponentOffset",
"offsetX = %{public}lf, offsetY = %{public}lf", offsetX, offsetY);
uint64_t width;
uint64_t height;
OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
if (render != nullptr) {
render->eglCore_->UpdateSize(width, height);
}
}
void SampleCallback(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targettimestamp)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleCallback", "SampleCallback");
}
void SampleInputeventCallback(OH_NativeXComponent *component, ArkUI_UIInputEvent *event, ArkUI_UIInputEvent_Type type)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleInputeventCallback", "SampleInputeventCallback");
}
HitTestMode SampleInterceptCallback(OH_NativeXComponent *component, ArkUI_UIInputEvent *event)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleInterceptCallback", "SampleInterceptCallback");
return HTM_DEFAULT;
}
void SampleSurfaceShowCallback(OH_NativeXComponent *component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleSurfaceShowCallback", "SampleSurfaceShowCallback");
}
bool SampleCallbackWithResult(OH_NativeXComponent *component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleCallbackWithResult", "SampleCallbackWithResult");
return false;
}
void SampleAnalyzer(ArkUI_NodeHandle node, ArkUI_XComponent_ImageAnalyzerState statusCode, void* userData)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "SampleAnalyzer", "SampleAnalyzer");
}
void MyContentHandler(ArkUI_NodeContentEvent* event)
{
// 处理内容变化
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "MyContentHandler", "MyContentHandler");
}
void NativeRender::OnTouchEvent(OH_NativeXComponent* component, void* window)
{
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB: Unable to get XComponent id");
return;
}
OH_NativeXComponent_TouchEvent_SourceTool sourceTool = OH_NATIVEXCOMPONENT_SOURCETOOL_UNKNOWN;
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OnTouchEvent",
"touch info: OH_NATIVEXCOMPONENT_SOURCETOOL_UNKNOWN = %{public}d", sourceTool);
OH_NativeXComponent_TouchEvent touchEvent;
OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent);
std::string id(idStr);
NativeRender* render = NativeRender::GetInstance(id);
if (render != nullptr && touchEvent.type == OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP) {
render->eglCore_->ChangeColor(hasChangeColor_);
}
float tiltX = 0.0f;
float tiltY = 0.0f;
OH_NativeXComponent_TouchPointToolType toolType =
OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN;
OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType);
OH_NativeXComponent_GetTouchPointTiltX(component, 0, &tiltX);
OH_NativeXComponent_GetTouchPointTiltY(component, 0, &tiltY);
float windowX = 0;
float windowY = 0;
float displayX = 0;
float displayY = 0;
OH_NativeXComponent_GetTouchPointWindowX(component, 0, &windowX);
OH_NativeXComponent_GetTouchPointWindowY(component, 0, &windowY);
OH_NativeXComponent_GetTouchPointDisplayX(component, 0, &displayX);
OH_NativeXComponent_GetTouchPointDisplayY(component, 0, &displayY);
int32_t pointId = 0;
OH_NativeXComponent_EventSourceType sourceType;
OH_NativeXComponent_GetTouchEventSourceType(component, pointId, &sourceType);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OnTouchEvent",
"touch info: toolType = %{public}d, tiltX = %{public}lf, tiltY = %{public}lf", toolType, tiltX, tiltY);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OnTouchEvent",
"touch info: max_touch_points_number = %{public}d", OH_MAX_TOUCH_POINTS_NUMBER);
OnTouchEventPartTwo(component, window);
ArkUI_NodeHandle node; // 此处需要绑定node
void* userData; // 此处需要绑定userData
OH_ArkUI_XComponent_StartImageAnalyzer(node, userData, SampleAnalyzer);
OH_ArkUI_XComponent_StopImageAnalyzer(node);
// ArkUI_NodeContentCallback 的具体使用请搜索 OH_ArkUI_NodeContent_RegisterCallback
// ArkUI_NodeContentCallback callback1 = MyContentHandler;
return;
}
void NativeRender::OnTouchEventPartTwo(OH_NativeXComponent* component, void* window)
{
int32_t min = EXPECTED_FRAME_RATE;
int32_t max = EXPECTED_FRAME_RATE;
int32_t expected = EXPECTED_FRAME_RATE;
OH_NativeXComponent_ExpectedRateRange range = {.min = min, .max = max, .expected = expected};
OH_NativeXComponent_SetExpectedFrameRateRange(component, &range);
bool needSoftKeyboard = 0;
OH_NativeXComponent_SetNeedSoftKeyboard(component, needSoftKeyboard);
OH_NativeXComponent_RegisterOnFrameCallback(component, SampleCallback);
OH_NativeXComponent_UnregisterOnFrameCallback(component);
ArkUI_UIInputEvent_Type type = ARKUI_UIINPUTEVENT_TYPE_UNKNOWN;
OH_NativeXComponent_RegisterUIInputEventCallback(component, SampleInputeventCallback, type);
ArkUI_AccessibilityProvider* sampleHandle; // 此处需要绑定handle
OH_NativeXComponent_GetNativeAccessibilityProvider(component, &sampleHandle);
OH_NativeXComponent_RegisterOnTouchInterceptCallback(component, SampleInterceptCallback);
OH_NativeXComponent_RegisterSurfaceShowCallback(component, SampleSurfaceShowCallback);
OH_NativeXComponent_RegisterSurfaceHideCallback(component, SampleSurfaceShowCallback);
OH_NativeXComponent_RegisterKeyEventCallbackWithResult(component, SampleCallbackWithResult);
OH_NativeXComponent_RegisterKeyEventCallbackWithResult(component, SampleCallbackWithResult);
return;
}
void NativeRender::RegisterCallback(OH_NativeXComponent* nativeXComponent)
{
renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB;
renderCallback_.OnSurfaceChanged = OnSurfaceChangedCB;
renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
renderCallback_.DispatchTouchEvent = DispatchTouchEventCB;
OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_);
mouseCallback_.DispatchMouseEvent = DispatchMouseEventCB;
mouseCallback_.DispatchHoverEvent = DispatchHoverEventCB;
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback_);
OH_NativeXComponent_RegisterFocusEventCallback(nativeXComponent, OnFocusEventCB);
OH_NativeXComponent_RegisterKeyEventCallback(nativeXComponent, OnKeyEventCB);
OH_NativeXComponent_RegisterBlurEventCallback(nativeXComponent, OnBlurEventCB);
}
void NativeRender::OnMouseEvent(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "OnMouseEvent");
OH_NativeXComponent_MouseEvent mouseEvent;
int32_t ret = OH_NativeXComponent_GetMouseEvent(component, window, &mouseEvent);
if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender",
"MouseEvent Info: x = %{public}f, y = %{public}f, action = %{public}d, button = %{public}d", mouseEvent.x,
mouseEvent.y, mouseEvent.action, mouseEvent.button);
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "GetMouseEvent error");
}
}
void NativeRender::OnHoverEvent(OH_NativeXComponent* component, bool isHover)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "OnHoverEvent isHover_ = %{public}d", isHover);
}
void NativeRender::OnFocusEvent(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "OnFocusEvent");
}
void NativeRender::OnBlurEvent(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "OnBlurEvent");
}
void NativeRender::OnKeyEvent(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender", "OnKeyEvent");
OH_NativeXComponent_KeyEvent* keyEvent = nullptr;
if (OH_NativeXComponent_GetKeyEvent(component, &keyEvent) >= 0) {
OH_NativeXComponent_KeyAction action;
OH_NativeXComponent_GetKeyEventAction(keyEvent, &action);
OH_NativeXComponent_KeyCode code;
OH_NativeXComponent_GetKeyEventCode(keyEvent, &code);
OH_NativeXComponent_EventSourceType sourceType;
OH_NativeXComponent_GetKeyEventSourceType(keyEvent, &sourceType);
int64_t deviceId;
OH_NativeXComponent_GetKeyEventDeviceId(keyEvent, &deviceId);
int64_t timeStamp;
OH_NativeXComponent_GetKeyEventTimestamp(keyEvent, &timeStamp);
uint64_t keys;
OH_NativeXComponent_GetKeyEventModifierKeyStates(keyEvent, &keys);
bool isNumLockOn;
OH_NativeXComponent_GetKeyEventNumLockState(keyEvent, &isNumLockOn);
bool isCapsLockOn;
OH_NativeXComponent_GetKeyEventCapsLockState(keyEvent, &isCapsLockOn);
bool isScrollLockOn;
OH_NativeXComponent_GetKeyEventScrollLockState(keyEvent, &isScrollLockOn);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeRender",
"KeyEvent Info: action=%{public}d, code=%{public}d, sourceType=%{public}d, deviceId=%{public}ld, "
"timeStamp=%{public}ld",
action, code, sourceType, deviceId, timeStamp);
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRender", "GetKeyEvent error");
}
}
napi_value NativeRender::TestGetXComponentStatus(napi_env env, napi_callback_info info)
{
napi_value hasDraw;
napi_value hasChangeColor;
napi_status ret = napi_create_int32(env, hasDraw_, &(hasDraw));
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TestGetXComponentStatus", "napi_create_int32 hasDraw_ error");
return nullptr;
}
ret = napi_create_int32(env, hasChangeColor_, &(hasChangeColor));
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TestGetXComponentStatus", "napi_create_int32 hasChangeColor_ error");
return nullptr;
}
napi_value obj;
ret = napi_create_object(env, &obj);
if (ret != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TestGetXComponentStatus", "napi_create_object error");
return nullptr;
}
ret = napi_set_named_property(env, obj, "hasDraw", hasDraw);
if (ret != napi_ok) {
OH_LOG_Print(
LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TestGetXComponentStatus", "napi_set_named_property hasDraw error");
return nullptr;
}
ret = napi_set_named_property(env, obj, "hasChangeColor", hasChangeColor);
if (ret != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TestGetXComponentStatus",
"napi_set_named_property hasChangeColor error");
return nullptr;
}
return obj;
}
} // namespace NativeXComponentSample

View File

@ -1,65 +0,0 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVE_XCOMPONENT_PLUGIN_RENDER_H
#define NATIVE_XCOMPONENT_PLUGIN_RENDER_H
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <napi/native_api.h>
#include <string>
#include <unordered_map>
#include "EGLCore.h"
namespace NativeOpenCAX {
class NativeRender {
public:
explicit NativeRender(std::string& id);
~NativeRender()
{
if (eglCore_ != nullptr) {
eglCore_->Release();
delete eglCore_;
eglCore_ = nullptr;
}
}
static NativeRender* GetInstance(std::string& id);
static void Release(std::string& id);
static napi_value NapiDrawPattern(napi_env env, napi_callback_info info);
static napi_value TestGetXComponentStatus(napi_env env, napi_callback_info info);
void Export(napi_env env, napi_value exports);
void OnSurfaceChanged(OH_NativeXComponent* component, void* window);
void OnTouchEvent(OH_NativeXComponent* component, void* window);
void OnTouchEventPartTwo(OH_NativeXComponent* component, void* window);
void OnMouseEvent(OH_NativeXComponent* component, void* window);
void OnHoverEvent(OH_NativeXComponent* component, bool isHover);
void OnFocusEvent(OH_NativeXComponent* component, void* window);
void OnBlurEvent(OH_NativeXComponent* component, void* window);
void OnKeyEvent(OH_NativeXComponent* component, void* window);
void RegisterCallback(OH_NativeXComponent* nativeXComponent);
void ShowSample(OH_NativeXComponent* component);
public:
static std::unordered_map<std::string, NativeRender*> instance_;
EGLCore* eglCore_;
std::string id_;
static int32_t hasDraw_;
static int32_t hasChangeColor_;
private:
OH_NativeXComponent_Callback renderCallback_;
OH_NativeXComponent_MouseEvent_Callback mouseCallback_;
};
} // namespace NativeXComponentSample
#endif // NATIVE_XCOMPONENT_PLUGIN_RENDER_H

View File

@ -39,25 +39,10 @@ static napi_value Init(napi_env env, napi_value exports) {
} }
napi_property_descriptor desc[] = { napi_property_descriptor desc[] = {
// [StartExclude napi_init_part] // [StartExclude napi_init_part]
{"createNativeNode", nullptr, NativeManager::createNativeNode, nullptr, nullptr, nullptr, {"createNativeNode", nullptr, NativeManager::createNativeNode, nullptr, nullptr, nullptr,napi_default, nullptr },
napi_default, nullptr }, {"drawPattern", nullptr, NativeManager::NapiDrawPattern, nullptr, nullptr,nullptr, napi_default, nullptr},
{"getStatus", nullptr, NativeManager::GetXComponentStatus, nullptr, nullptr,
nullptr, napi_default, nullptr},
{"drawPattern", nullptr, NativeManager::NapiDrawPattern, nullptr, nullptr,
nullptr, napi_default, nullptr},
// [StartExclude napi_init]
{"getContext", nullptr, NativeManager::GetContext, nullptr, nullptr, nullptr,
napi_default, nullptr },
// [EndExclude napi_init_part]
{"bindNode", nullptr, NativeManager::BindNode, nullptr, nullptr, nullptr, napi_default, nullptr},
{"unbindNode", nullptr, NativeManager::UnbindNode, nullptr, nullptr, nullptr, napi_default, nullptr},
{"setFrameRate", nullptr, NativeManager::SetFrameRate, nullptr, nullptr, nullptr, napi_default, nullptr}, {"setFrameRate", nullptr, NativeManager::SetFrameRate, nullptr, nullptr, nullptr, napi_default, nullptr},
{"setNeedSoftKeyboard", nullptr, NativeManager::SetNeedSoftKeyboard, nullptr, nullptr, nullptr, napi_default, {"setNeedSoftKeyboard", nullptr, NativeManager::SetNeedSoftKeyboard, nullptr, nullptr, nullptr, napi_default,nullptr},
nullptr},
// [StartExclude napi_init_part]
{"initialize", nullptr, NativeManager::Initialize, nullptr, nullptr, nullptr, napi_default, nullptr},
{"finalize", nullptr, NativeManager::Finalize, nullptr, nullptr, nullptr, napi_default, nullptr},
{"drawStar", nullptr, NativeManager::DrawStar, nullptr, nullptr, nullptr, napi_default, nullptr},
{"nativeLoadTest", nullptr, NativeLoadSoTest, nullptr, nullptr, nullptr, napi_default, nullptr}, {"nativeLoadTest", nullptr, NativeLoadSoTest, nullptr, nullptr, nullptr, napi_default, nullptr},
}; };
napi_status status = napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); napi_status status = napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);

View File

@ -19,12 +19,6 @@ type XComponentContextStatus = {
hasChangeColor: boolean hasChangeColor: boolean
}; };
export const createNativeNode: (content: NodeContent, tag: string) => void; export const createNativeNode: (content: NodeContent, tag: string) => void;
export const getStatus: () => XComponentContextStatus;
export const drawPattern: () => void; export const drawPattern: () => void;
export const bindNode: (id: string, node: object) => void;
export const unbindNode: (id: string) => void;
export const setFrameRate: (id: string, min: number, max: number, expected: number) => void; export const setFrameRate: (id: string, min: number, max: number, expected: number) => void;
export const setNeedSoftKeyboard: (id: string, needSoftKeyborad: boolean) => void; export const setNeedSoftKeyboard: (id: string, needSoftKeyborad: boolean) => void;
export const initialize: (id: string) => void;
export const finalize: (id: string) => void;
export const drawStar: (id: string) => void;