1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include <ctime>
18 #include <native_vsync/native_vsync.h>
19
20 #define SUCCESS 0
21 #define FAIL (-1)
22 #define PARAM_0 0
23 #define TIMEOUT_FIVE 10
24
25 static bool g_flag = false;
OnVSync(long long timestamp,void * data)26 static void OnVSync(long long timestamp, void *data) { g_flag = true; }
27
OHNativeVSyncCreate(napi_env env,napi_callback_info info)28 static napi_value OHNativeVSyncCreate(napi_env env, napi_callback_info info)
29 {
30 napi_value result = nullptr;
31 char name[] = "test";
32 OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
33 if (nativeVSync != nullptr) {
34 napi_create_int32(env, SUCCESS, &result);
35 OH_NativeVSync_Destroy(nativeVSync);
36 } else {
37 napi_create_int32(env, FAIL, &result);
38 }
39 return result;
40 }
41
OHNativeVSyncCreateFOne(napi_env env,napi_callback_info info)42 static napi_value OHNativeVSyncCreateFOne(napi_env env, napi_callback_info info)
43 {
44 napi_value result = nullptr;
45 unsigned int length = 0;
46 OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(nullptr, length);
47 if (nativeVSync != nullptr) {
48 napi_create_int32(env, FAIL, &result);
49 } else {
50 napi_create_int32(env, SUCCESS, &result);
51 }
52 return result;
53 }
54
OHNativeVSyncRequestFrame(napi_env env,napi_callback_info info)55 static napi_value OHNativeVSyncRequestFrame(napi_env env, napi_callback_info info)
56 {
57 napi_value result = nullptr;
58 char name[] = "test";
59 OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
60 OH_NativeVSync_FrameCallback callback = OnVSync;
61 int ret = OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
62 if (ret == SUCCESS) {
63 napi_create_int32(env, SUCCESS, &result);
64 } else {
65 napi_create_int32(env, FAIL, &result);
66 }
67 if (nativeVSync) {
68 OH_NativeVSync_Destroy(nativeVSync);
69 }
70 return result;
71 }
72
OHNativeVSyncRequestFrameFOne(napi_env env,napi_callback_info info)73 static napi_value OHNativeVSyncRequestFrameFOne(napi_env env, napi_callback_info info)
74 {
75 napi_value result = nullptr;
76 int ret = OH_NativeVSync_RequestFrame(nullptr, nullptr, nullptr);
77 if (ret != SUCCESS) {
78 napi_create_int32(env, SUCCESS, &result);
79 } else {
80 napi_create_int32(env, FAIL, &result);
81 }
82 return result;
83 }
84
OHNativeVSyncRequestFrameFTwo(napi_env env,napi_callback_info info)85 static napi_value OHNativeVSyncRequestFrameFTwo(napi_env env, napi_callback_info info)
86 {
87 napi_value result = nullptr;
88 char name[] = "test";
89 OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
90 int ret = OH_NativeVSync_RequestFrame(nativeVSync, nullptr, nullptr);
91 if (ret != SUCCESS) {
92 napi_create_int32(env, SUCCESS, &result);
93 } else {
94 napi_create_int32(env, FAIL, &result);
95 }
96 if (nativeVSync) {
97 OH_NativeVSync_Destroy(nativeVSync);
98 }
99 return result;
100 }
101
OHNativeVSyncGetPeriod(napi_env env,napi_callback_info info)102 static napi_value OHNativeVSyncGetPeriod(napi_env env, napi_callback_info info)
103 {
104 napi_value result = nullptr;
105 char name[] = "test";
106 OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
107 OH_NativeVSync_FrameCallback callback = OnVSync;
108 OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
109 time_t startTime = time(PARAM_0);
110 double diffTime = 0;
111 while (!g_flag && diffTime < TIMEOUT_FIVE) {
112 time_t curTime = time(PARAM_0);
113 diffTime = difftime(curTime, startTime);
114 }
115 long long period = 0;
116 int ret = OH_NativeVSync_GetPeriod(nativeVSync, &period);
117 if (ret == SUCCESS) {
118 napi_create_int32(env, SUCCESS, &result);
119 } else {
120 napi_create_int32(env, FAIL, &result);
121 }
122 if (nativeVSync) {
123 OH_NativeVSync_Destroy(nativeVSync);
124 }
125 return result;
126 }
127
OHNativeVSyncGetPeriodFOne(napi_env env,napi_callback_info info)128 static napi_value OHNativeVSyncGetPeriodFOne(napi_env env, napi_callback_info info)
129 {
130 napi_value result = nullptr;
131 long long period = 0;
132 int ret = OH_NativeVSync_GetPeriod(nullptr, &period);
133 if (ret != SUCCESS) {
134 napi_create_int32(env, SUCCESS, &result);
135 } else {
136 napi_create_int32(env, FAIL, &result);
137 }
138 return result;
139 }
140
141 EXTERN_C_START
Init(napi_env env,napi_value exports)142 static napi_value Init(napi_env env, napi_value exports)
143 {
144 napi_property_descriptor desc[] = {
145 {"oHNativeVSyncCreate", nullptr, OHNativeVSyncCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
146 {"oHNativeVSyncCreateFOne", nullptr, OHNativeVSyncCreateFOne, nullptr, nullptr, nullptr, napi_default, nullptr},
147 {"oHNativeVSyncRequestFrame", nullptr, OHNativeVSyncRequestFrame, nullptr, nullptr, nullptr, napi_default,
148 nullptr},
149 {"oHNativeVSyncRequestFrameFOne", nullptr, OHNativeVSyncRequestFrameFOne, nullptr, nullptr, nullptr,
150 napi_default, nullptr},
151 {"oHNativeVSyncRequestFrameFTwo", nullptr, OHNativeVSyncRequestFrameFTwo, nullptr, nullptr, nullptr,
152 napi_default, nullptr},
153 {"oHNativeVSyncGetPeriod", nullptr, OHNativeVSyncGetPeriod, nullptr, nullptr, nullptr, napi_default, nullptr},
154 {"oHNativeVSyncGetPeriodFOne", nullptr, OHNativeVSyncGetPeriodFOne, nullptr, nullptr, nullptr, napi_default,
155 nullptr},
156 };
157 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
158 return exports;
159 }
160 EXTERN_C_END
161
162 static napi_module demoModule = {
163 .nm_version = 1,
164 .nm_flags = 0,
165 .nm_filename = nullptr,
166 .nm_register_func = Init,
167 .nm_modname = "nativevsync",
168 .nm_priv = ((void *)0),
169 .reserved = {0},
170 };
171
RegisterModule(void)172 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); };
173