-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnapi_init_10.cpp
More file actions
148 lines (120 loc) · 4.85 KB
/
Copy pathnapi_init_10.cpp
File metadata and controls
148 lines (120 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <napi/native_api.h>
#include <string>
#include <hilog/log.h>
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <native_window/external_window.h>
#include <native_vsync/native_vsync.h>
#include <unordered_map>
#include <VulkanTriangle.h>
VulkanTriangle VULKAN_TRIANGLE;
#define LOG_PRINT_DOMAIN 0x1
#define VSYNC_NAME "VulkanTriangleVSync"
OH_NativeVSync *NATIVE_VSYNC = nullptr;
void OnVsync(long long timestamp, void *data)
{
OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "VulkanTriangle", "OnVsync %{public}lld.", timestamp);
// TODO: Rendering Loop
VULKAN_TRIANGLE.Draw(timestamp * 0.001);
OH_NativeVSync_RequestFrame(NATIVE_VSYNC, &OnVsync, nullptr);
}
void OnSurfaceCreated(OH_NativeXComponent *component, void *window)
{
OHNativeWindow *native_window = static_cast<OHNativeWindow *>(window);
VULKAN_TRIANGLE.CreateSurface(native_window);
// TODO: Create VkSurface
{
NATIVE_VSYNC = OH_NativeVSync_Create(VSYNC_NAME, strlen(VSYNC_NAME));
OH_NativeVSync_RequestFrame(NATIVE_VSYNC, &OnVsync, nullptr);
long long period = 0;
OH_NativeVSync_GetPeriod(NATIVE_VSYNC, &period);
}
}
void OnSurfaceChanged(OH_NativeXComponent *component, void *window)
{
OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window);
// TODO: Recreate VkSurface and update state
}
void OnSurfaceDestroyed(OH_NativeXComponent *component, void *window)
{
OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window);
{
OH_NativeVSync_Destroy(NATIVE_VSYNC);
NATIVE_VSYNC = nullptr;
}
// TODO: Destroy VkSurface
}
void DispatchTouchEvent(OH_NativeXComponent *component, void *window)
{
OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window);
}
void OnNativeXComponentInit(OH_NativeXComponent *nativeXComponent)
{
uint64_t id_size = OH_XCOMPONENT_ID_LEN_MAX + 1;
char id[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};
if (OH_NativeXComponent_GetXComponentId(nativeXComponent, id, &id_size) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Init", "OH_NativeXComponent_GetXComponentId(nativeXComponent, id, id_size) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS");
return;
}
OH_NativeXComponent_Callback callback;
callback.OnSurfaceCreated = OnSurfaceCreated; // surface创建成功后触发,开发者可以从中获取native window的句柄
callback.OnSurfaceChanged = OnSurfaceChanged; // surface发生变化后触发,开发者可以从中获取native window的句柄以及XComponent的变更信息
callback.OnSurfaceDestroyed = OnSurfaceDestroyed; // surface销毁时触发,开发者可以在此释放资源
callback.DispatchTouchEvent = DispatchTouchEvent; // XComponent的touch事件回调接口,开发者可以从中获得此次touch事件的信息
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback);
}
void OnVulkanInit()
{
// TODO: Init Vulkan
}
extern "C"
{
static napi_value Init(napi_env env, napi_value exports)
{
static bool is_napi_define_properties = false;
if (!is_napi_define_properties)
{
napi_property_descriptor desc[] = {
//{"myAdd", nullptr, MyAdd, nullptr, nullptr, nullptr, napi_default, nullptr}
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
OnVulkanInit();
is_napi_define_properties = true;
return exports;
}
{
napi_status status;
napi_value xcomponent_object = nullptr;
status = napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &xcomponent_object);
if (status != napi_ok)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Init", "napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &xcomponent_object)");
return nullptr;
}
OH_NativeXComponent *native_xcomponent = nullptr;
status = napi_unwrap(env, xcomponent_object, reinterpret_cast<void **>(&native_xcomponent));
if (status != napi_ok)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Init", "napi_unwrap(env, xcomponent_object, reinterpret_cast<void **>(&native_xcomponent))");
return nullptr;
}
{
OnNativeXComponentInit(native_xcomponent);
}
}
return exports;
}
}
static napi_module VULKAN_TRIANGLE_MODULE = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "VulkanTriangle",
.nm_priv = ((void *)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterVulkanTriangleModule()
{
napi_module_register(&VULKAN_TRIANGLE_MODULE);
}