• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <stdexcept>
18 #include "napi/native_api.h"
19 #include <bits/alltypes.h>
20 #include <ctime>
21 #include <native_vsync/native_vsync.h>
22 #include <stdexcept>
23 #include <cstdio>
24 #include <zconf.h>
25 #include <native_image/native_image.h>
26 #include <native_window/external_window.h>
27 #include <thread>
28 
29 #define SUCCESS 0
30 #define FAIL (-1)
31 #define PARAM_0 0
32 #define NUMBER_2 2
33 #define NUMBER_3 3
34 #define TIMEOUT_FIVE 10
35 #define NUMBER_254 254
36 #define NUMBER_255 255
37 #define NUMBER_256 256
38 #define NUMBER_500 500
39 #define NUMBER_40001000 40001000
40 #define ARR_NUMBER_0 0
41 #define ARR_NUMBER_1 1
42 #define ARR_NUMBER_2 2
43 #define ARR_NUMBER_3 3
44 #define ARR_NUMBER_4 4
45 
46 static bool g_flag = false;
OnVSync(long long timestamp,void * data)47 static void OnVSync(long long timestamp, void *data) { g_flag = true; }
48 static OH_NativeVSync *g_nativeVsync = nullptr;
49 
OHNativeVSyncCreate(napi_env env,napi_callback_info info)50 static napi_value OHNativeVSyncCreate(napi_env env, napi_callback_info info)
51 {
52     napi_value result = nullptr;
53     char name[] = "test";
54     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
55     if (nativeVSync != nullptr) {
56         napi_create_int32(env, SUCCESS, &result);
57         OH_NativeVSync_Destroy(nativeVSync);
58     } else {
59         napi_create_int32(env, FAIL, &result);
60     }
61     return result;
62 }
63 
OHNativeVSyncCreateFOne(napi_env env,napi_callback_info info)64 static napi_value OHNativeVSyncCreateFOne(napi_env env, napi_callback_info info)
65 {
66     napi_value result = nullptr;
67     unsigned int length = 0;
68     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(nullptr, length);
69     if (nativeVSync != nullptr) {
70         napi_create_int32(env, FAIL, &result);
71     } else {
72         napi_create_int32(env, SUCCESS, &result);
73     }
74     return result;
75 }
76 
OHNativeVSyncRequestFrame(napi_env env,napi_callback_info info)77 static napi_value OHNativeVSyncRequestFrame(napi_env env, napi_callback_info info)
78 {
79     napi_value result = nullptr;
80     char name[] = "test";
81     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
82     OH_NativeVSync_FrameCallback callback = OnVSync;
83     int ret = OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
84     if (ret == SUCCESS) {
85         napi_create_int32(env, SUCCESS, &result);
86     } else {
87         napi_create_int32(env, FAIL, &result);
88     }
89     if (nativeVSync) {
90         OH_NativeVSync_Destroy(nativeVSync);
91     }
92     return result;
93 }
94 
OHNativeVSyncRequestFrameFOne(napi_env env,napi_callback_info info)95 static napi_value OHNativeVSyncRequestFrameFOne(napi_env env, napi_callback_info info)
96 {
97     napi_value result = nullptr;
98     int ret = OH_NativeVSync_RequestFrame(nullptr, nullptr, nullptr);
99     if (ret != SUCCESS) {
100         napi_create_int32(env, SUCCESS, &result);
101     } else {
102         napi_create_int32(env, FAIL, &result);
103     }
104     return result;
105 }
106 
OHNativeVSyncRequestFrameFTwo(napi_env env,napi_callback_info info)107 static napi_value OHNativeVSyncRequestFrameFTwo(napi_env env, napi_callback_info info)
108 {
109     napi_value result = nullptr;
110     char name[] = "test";
111     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
112     int ret = OH_NativeVSync_RequestFrame(nativeVSync, nullptr, nullptr);
113     if (ret != SUCCESS) {
114         napi_create_int32(env, SUCCESS, &result);
115     } else {
116         napi_create_int32(env, FAIL, &result);
117     }
118     if (nativeVSync) {
119         OH_NativeVSync_Destroy(nativeVSync);
120     }
121     return result;
122 }
123 
OHNativeVSyncGetPeriod(napi_env env,napi_callback_info info)124 static napi_value OHNativeVSyncGetPeriod(napi_env env, napi_callback_info info)
125 {
126     napi_value result = nullptr;
127     char name[] = "test";
128     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, sizeof(name));
129     OH_NativeVSync_FrameCallback callback = OnVSync;
130     OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
131     time_t startTime = time(PARAM_0);
132     double diffTime = 0;
133     while (!g_flag && diffTime < TIMEOUT_FIVE) {
134         time_t curTime = time(PARAM_0);
135         diffTime = difftime(curTime, startTime);
136     }
137     long long period;
138     int ret = OH_NativeVSync_GetPeriod(nativeVSync, &period);
139     if (ret == SUCCESS) {
140         napi_create_int32(env, SUCCESS, &result);
141     } else {
142         napi_create_int32(env, FAIL, &result);
143     }
144     if (nativeVSync) {
145         OH_NativeVSync_Destroy(nativeVSync);
146     }
147     return result;
148 }
149 
OHNativeVSyncGetPeriodFOne(napi_env env,napi_callback_info info)150 static napi_value OHNativeVSyncGetPeriodFOne(napi_env env, napi_callback_info info)
151 {
152     napi_value result = nullptr;
153     long long period;
154     int ret = OH_NativeVSync_GetPeriod(nullptr, &period);
155     if (ret != SUCCESS) {
156         napi_create_int32(env, SUCCESS, &result);
157     } else {
158         napi_create_int32(env, FAIL, &result);
159     }
160     return result;
161 }
162 
OHNativeVSyncCreateNull(napi_env env,napi_callback_info info)163 static napi_value OHNativeVSyncCreateNull(napi_env env, napi_callback_info info)
164 {
165     napi_value result = nullptr;
166     unsigned int length = 0;
167     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(nullptr, length);
168     if (nativeVSync == nullptr) {
169         napi_create_int32(env, FAIL, &result);
170     } else {
171         napi_create_int32(env, SUCCESS, &result);
172     }
173     return result;
174 }
175 
OHNativeVSyncCreateNotEq(napi_env env,napi_callback_info info)176 static napi_value OHNativeVSyncCreateNotEq(napi_env env, napi_callback_info info)
177 {
178     napi_value result = nullptr;
179     char name[] = "testCase";
180     unsigned int length = NUMBER_2;
181     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
182     if (nativeVSync == nullptr) {
183         napi_create_int32(env, FAIL, &result);
184     } else {
185         napi_create_int32(env, SUCCESS, &result);
186     }
187     OH_NativeVSync_Destroy(nativeVSync);
188     return result;
189 }
190 
OHNativeVSyncCreateNormal(napi_env env,napi_callback_info info)191 static napi_value OHNativeVSyncCreateNormal(napi_env env, napi_callback_info info)
192 {
193     napi_value result = nullptr;
194     char name[] = "testCase";
195     unsigned int length = strlen(name);
196     OH_NativeVSync *nativeVSync1 = OH_NativeVSync_Create(name, length);
197     napi_create_array_with_length(env, NUMBER_2, &result);
198     napi_value result1 = nullptr;
199     napi_value result2 = nullptr;
200     if (nativeVSync1 == nullptr) {
201         napi_create_int32(env, FAIL, &result1);
202     } else {
203         napi_create_int32(env, SUCCESS, &result1);
204     }
205     napi_set_element(env, result, 0, result1);
206 
207     OH_NativeVSync *nativeVSync2 = OH_NativeVSync_Create(name, length);
208     if (nativeVSync2 == nullptr) {
209         napi_create_int32(env, FAIL, &result2);
210     } else {
211         napi_create_int32(env, SUCCESS, &result2);
212     }
213 
214     napi_set_element(env, result, 1, result2);
215     OH_NativeVSync_Destroy(nativeVSync1);
216     OH_NativeVSync_Destroy(nativeVSync2);
217     return result;
218 }
219 
OHNativeVSyncCreateAbnormal(napi_env env,napi_callback_info info)220 static napi_value OHNativeVSyncCreateAbnormal(napi_env env, napi_callback_info info)
221 {
222     napi_value result = nullptr;
223     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(NULL, 0);
224     if (nativeVSync == nullptr) {
225         napi_create_int32(env, SUCCESS, &result);
226     } else {
227         napi_create_int32(env, FAIL, &result);
228         return result;
229     }
230     nativeVSync = OH_NativeVSync_Create(0, 0);
231     if (nativeVSync == nullptr) {
232         napi_create_int32(env, SUCCESS, &result);
233     } else {
234         napi_create_int32(env, FAIL, &result);
235         return result;
236     }
237     OH_NativeVSync_Destroy(nativeVSync);
238     return result;
239 }
240 
MyFrameCallback(long long timestamp,void * data)241 void MyFrameCallback(long long timestamp, void *data)
242 {
243     int *myData = static_cast<int *>(data);
244     return;
245 }
246 
OHNativeVSyncCreateDifLenth(napi_env env,napi_callback_info info)247 static napi_value OHNativeVSyncCreateDifLenth(napi_env env, napi_callback_info info)
248 {
249     napi_value result = nullptr;
250     std::vector<std::string> myArray = {
251         "0",
252         "a",
253         "你",
254         "!@#¥%^&*()_+",
255         " ",
256         "  ",
257         "这里有好多好多字符qazwsxedcrfvtgbyhnujqazwsxedcrfvtgbyhnujqazwsxedcrfvtgbyhnujqazwsxedcrfvtgbyhnuj",
258         "ab_cdefghijklmnopqrstuvwxyz",
259         "abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde"};
260     napi_create_array_with_length(env, myArray.size(), &result);
261 
262     for (uint32_t index = 0; index < myArray.size(); index++) {
263         unsigned int len = strlen(myArray[index].c_str());
264         OH_NativeVSync *nativeVSync = nullptr;
265         napi_value resultIndex = nullptr;
266         nativeVSync = OH_NativeVSync_Create(myArray[index].c_str(), len);
267         if (nativeVSync == nullptr) {
268             napi_create_int32(env, FAIL+index, &result);
269             OH_NativeVSync_Destroy(nativeVSync);
270             return result;
271         } else {
272             napi_create_int32(env, SUCCESS, &result);
273             OH_NativeVSync_Destroy(nativeVSync);
274             continue;
275         }
276     }
277     return result;
278 }
279 
OHNativeVSyncCreateMuch(napi_env env,napi_callback_info info)280 static napi_value OHNativeVSyncCreateMuch(napi_env env, napi_callback_info info)
281 {
282     napi_value result = nullptr;
283     char name[] = "testcase";
284     unsigned int length = strlen(name);
285     OH_NativeVSync *nativeVSyncArr[NUMBER_500];
286     int success = 0;
287     for (uint32_t i = 0; i < NUMBER_500; i++) {
288         nativeVSyncArr[i] = OH_NativeVSync_Create(name, length);
289         if (nativeVSyncArr[i] != nullptr) {
290             success = success + 1;
291             continue;
292         }
293     }
294     if (success == NUMBER_255 || success == NUMBER_254) {
295         for (uint32_t i = 0; i < NUMBER_500; i++) {
296             OH_NativeVSync_Destroy(nativeVSyncArr[i]);
297         }
298         napi_create_int32(env, SUCCESS, &result);
299         return result;
300     } else {
301         for (uint32_t i = 0; i < NUMBER_500; i++) {
302             OH_NativeVSync_Destroy(nativeVSyncArr[i]);
303         }
304         napi_create_int32(env, FAIL, &result);
305         return result;
306     }
307 }
308 
OHNativeVSyncGetPeriodNullptr(napi_env env,napi_callback_info info)309 static napi_value OHNativeVSyncGetPeriodNullptr(napi_env env, napi_callback_info info)
310 {
311     napi_value result = nullptr;
312     long long period;
313     int res = OH_NativeVSync_GetPeriod(nullptr, &period);
314     napi_create_int32(env, res, &result);
315     return result;
316 }
317 
OHNativeVSyncRequestFrameNullptr(napi_env env,napi_callback_info info)318 static napi_value OHNativeVSyncRequestFrameNullptr(napi_env env, napi_callback_info info)
319 {
320     napi_value result = nullptr;
321     char name[] = "testcase";
322     unsigned int length = strlen(name);
323     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
324     napi_create_array_with_length(env, NUMBER_3, &result);
325     int param = 0;
326     int res = OH_NativeVSync_RequestFrame(nullptr, MyFrameCallback, &param);
327     if (res != NUMBER_40001000) {
328         napi_create_int32(env, FAIL, &result);
329         return result;
330     }
331     res = OH_NativeVSync_RequestFrame(nativeVSync, nullptr, &param);
332     if (res != NUMBER_40001000) {
333         napi_create_int32(env, FAIL, &result);
334         return result;
335     }
336     res = OH_NativeVSync_RequestFrame(nativeVSync, MyFrameCallback, nullptr);
337     if (res != 0) {
338         napi_create_int32(env, FAIL, &result);
339         return result;
340     }
341     napi_create_int32(env, SUCCESS, &result);
342     OH_NativeVSync_Destroy(nativeVSync);
343     return result;
344 }
345 
OHNativeVSyncRequestFrameNormal(napi_env env,napi_callback_info info)346 static napi_value OHNativeVSyncRequestFrameNormal(napi_env env, napi_callback_info info)
347 {
348     napi_value result = nullptr;
349     char name[] = "testcase";
350     unsigned int length = strlen(name);
351     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
352     int *param = nullptr;
353     int res = OH_NativeVSync_RequestFrame(nativeVSync, MyFrameCallback, param);
354     napi_create_int32(env, res, &result);
355     OH_NativeVSync_Destroy(nativeVSync);
356     return result;
357 }
358 
OHNativeVSyncRequestFrameParamErr(napi_env env,napi_callback_info info)359 static napi_value OHNativeVSyncRequestFrameParamErr(napi_env env, napi_callback_info info)
360 {
361     napi_value result = nullptr;
362     char name[] = "testcase";
363     unsigned int length = strlen(name);
364     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
365     char param[] = "test";
366     int res = OH_NativeVSync_RequestFrame(nativeVSync, MyFrameCallback, &param);
367     napi_create_int32(env, res, &result);
368     OH_NativeVSync_Destroy(nativeVSync);
369     return result;
370 }
371 
OHNativeVSyncCreateForAssociatedWindowNormal(napi_env env,napi_callback_info info)372 static napi_value OHNativeVSyncCreateForAssociatedWindowNormal(napi_env env, napi_callback_info info)
373 {
374     napi_value result = nullptr;
375     OHNativeWindow *nativeWindow = nullptr;
376     OH_NativeImage *image = OH_ConsumerSurface_Create();
377     nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
378     uint64_t surfaceId;
379     OH_NativeWindow_GetSurfaceId(nativeWindow, &surfaceId);
380     char name[] = "test";
381     unsigned int length = strlen(name);
382     OH_NativeVSync *nativeVSync = nullptr;
383     nativeVSync = OH_NativeVSync_Create_ForAssociatedWindow(surfaceId, name, length);
384     if (nativeVSync == nullptr) {
385         napi_create_int32(env, FAIL, &result);
386         OH_NativeVSync_Destroy(nativeVSync);
387         return result;
388     } else {
389         napi_create_int32(env, SUCCESS, &result);
390         OH_NativeVSync_Destroy(nativeVSync);
391     }
392     return result;
393 }
394 
OHNativeVSyncCreateForAssociatedWindowAbNormal01(napi_env env,napi_callback_info info)395 static napi_value OHNativeVSyncCreateForAssociatedWindowAbNormal01(napi_env env, napi_callback_info info)
396 {
397     napi_value result = nullptr;
398     napi_value result1 = nullptr;
399     napi_value result2 = nullptr;
400     napi_value result3 = nullptr;
401     napi_create_array_with_length(env, NUMBER_3, &result);
402     OHNativeWindow *nativeWindow = nullptr;
403     OH_NativeImage *image = OH_ConsumerSurface_Create();
404     nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
405     uint64_t surfaceId;
406     OH_NativeWindow_GetSurfaceId(nativeWindow, &surfaceId);
407     char name[] = "test";
408     unsigned int length = strlen(name);
409     OH_NativeVSync *nativeVSync1 = OH_NativeVSync_Create_ForAssociatedWindow(surfaceId, nullptr, length);
410     if (nativeVSync1 == nullptr) {
411         napi_create_int32(env, FAIL, &result1);
412     } else {
413         napi_create_int32(env, SUCCESS, &result1);
414     }
415     napi_set_element(env, result, ARR_NUMBER_0, result1);
416     OH_NativeVSync *nativeVSync2 = OH_NativeVSync_Create_ForAssociatedWindow(surfaceId, name, 0);
417     if (nativeVSync2 == nullptr) {
418         napi_create_int32(env, FAIL, &result2);
419     } else {
420         napi_create_int32(env, SUCCESS, &result2);
421     }
422     napi_set_element(env, result, ARR_NUMBER_1, result2);
423     OH_NativeVSync *nativeVSync3 = OH_NativeVSync_Create_ForAssociatedWindow(0, name, length);
424     if (nativeVSync3 == nullptr) {
425         napi_create_int32(env, FAIL, &result3);
426     } else {
427         napi_create_int32(env, SUCCESS, &result3);
428     }
429     napi_set_element(env, result, ARR_NUMBER_2, result3);
430     OH_NativeVSync_Destroy(nativeVSync1);
431     OH_NativeVSync_Destroy(nativeVSync2);
432     OH_NativeVSync_Destroy(nativeVSync3);
433     OH_NativeImage_Destroy(&image);
434     OH_NativeWindow_DestroyNativeWindow(nativeWindow);
435     return result;
436 }
437 
OHNativeVSyncCreateForAssociatedWindowAbNormal02(napi_env env,napi_callback_info info)438 static napi_value OHNativeVSyncCreateForAssociatedWindowAbNormal02(napi_env env, napi_callback_info info)
439 {
440     napi_value result = nullptr;
441     napi_value result1 = nullptr;
442     napi_value result2 = nullptr;
443     napi_value result3 = nullptr;
444     napi_create_array_with_length(env, NUMBER_3, &result);
445     OHNativeWindow *nativeWindow = nullptr;
446     OH_NativeImage *image = OH_ConsumerSurface_Create();
447     nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
448     uint64_t surfaceId;
449     OH_NativeWindow_GetSurfaceId(nativeWindow, &surfaceId);
450     char name[] = "test";
451     unsigned int length = strlen(name);
452     uint64_t max = 184467440737095516;
453     OH_NativeVSync *nativeVSync1 = OH_NativeVSync_Create_ForAssociatedWindow(max, name, length);
454     if (nativeVSync1 == nullptr) {
455         napi_create_int32(env, FAIL, &result1);
456     } else {
457         napi_create_int32(env, SUCCESS, &result1);
458     }
459     napi_set_element(env, result, ARR_NUMBER_0, result1);
460     OH_NativeVSync *nativeVSync2 = OH_NativeVSync_Create_ForAssociatedWindow(surfaceId, "", length);
461     if (nativeVSync2 == nullptr) {
462         napi_create_int32(env, FAIL, &result2);
463     } else {
464         napi_create_int32(env, SUCCESS, &result2);
465     }
466     napi_set_element(env, result, ARR_NUMBER_1, result2);
467     OH_NativeVSync *nativeVSync3 = OH_NativeVSync_Create_ForAssociatedWindow(surfaceId, name, sizeof(name));
468     if (nativeVSync3 == nullptr) {
469         napi_create_int32(env, FAIL, &result3);
470     } else {
471         napi_create_int32(env, SUCCESS, &result3);
472     }
473     napi_set_element(env, result, ARR_NUMBER_2, result3);
474     OH_NativeVSync_Destroy(nativeVSync1);
475     OH_NativeVSync_Destroy(nativeVSync2);
476     OH_NativeVSync_Destroy(nativeVSync3);
477     OH_NativeImage_Destroy(&image);
478     OH_NativeWindow_DestroyNativeWindow(nativeWindow);
479     return result;
480 }
481 
OHNativeVSyncCreateForAssociatedWindowAbNormal03(napi_env env,napi_callback_info info)482 static napi_value OHNativeVSyncCreateForAssociatedWindowAbNormal03(napi_env env, napi_callback_info info)
483 {
484     napi_value result = nullptr;
485     uint64_t windowID = 9999;
486     char name[] = "test";
487     OH_NativeVSync *nativeVSyncArr[NUMBER_500];
488     int success = 0;
489     for (uint32_t i = 0; i < NUMBER_500; i++) {
490         nativeVSyncArr[i] = OH_NativeVSync_Create_ForAssociatedWindow(windowID, name, sizeof(name));
491         if (nativeVSyncArr[i] != nullptr) {
492             success = success + 1;
493             continue;
494         }
495     }
496     if (success == NUMBER_255 || success == NUMBER_254) {
497         for (uint32_t i = 0; i < NUMBER_500; i++) {
498             OH_NativeVSync_Destroy(nativeVSyncArr[i]);
499         }
500         napi_create_int32(env, SUCCESS, &result);
501         return result;
502     } else {
503         for (uint32_t i = 0; i < NUMBER_500; i++) {
504             OH_NativeVSync_Destroy(nativeVSyncArr[i]);
505         }
506         napi_create_int32(env, FAIL, &result);
507         return result;
508     }
509 }
510 
OHNativeVSyncDVSyncSwitch(napi_env env,napi_callback_info info)511 static napi_value OHNativeVSyncDVSyncSwitch(napi_env env, napi_callback_info info)
512 {
513     napi_value result = nullptr;
514     char name[] = "testcase_switch";
515     unsigned int length = strlen(name);
516     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
517     char param[] = "test";
518     OH_NativeVSync_RequestFrame(nativeVSync, MyFrameCallback, &param);
519     if ((OH_NativeVSync_DVSyncSwitch(nativeVSync, true) == 0) ||
520         (OH_NativeVSync_DVSyncSwitch(nativeVSync, false) == 0)) {
521         napi_create_int32(env, SUCCESS, &result);
522     } else {
523         napi_create_int32(env, FAIL, &result);
524     }
525     OH_NativeVSync_Destroy(nativeVSync);
526     return result;
527 }
528 
OHNativeVSyncDVSyncSwitchNullptr(napi_env env,napi_callback_info info)529 static napi_value OHNativeVSyncDVSyncSwitchNullptr(napi_env env, napi_callback_info info)
530 {
531     napi_value result = nullptr;
532     char name[] = "testcase_switch";
533     unsigned int length = strlen(name);
534     OH_NativeVSync *nativeVSync = OH_NativeVSync_Create(name, length);
535     char param[] = "test";
536     OH_NativeVSync_RequestFrame(nativeVSync, MyFrameCallback, &param);
537     if ((OH_NativeVSync_DVSyncSwitch(nullptr, true) != 0) ||
538         (OH_NativeVSync_DVSyncSwitch(nullptr, false) != 0)) {
539         napi_create_int32(env, SUCCESS, &result);
540     } else {
541         napi_create_int32(env, FAIL, &result);
542     }
543     OH_NativeVSync_Destroy(nativeVSync);
544     return result;
545 }
546 
OHNativeVSyncRequestFrameWithMultiCallbackNormal(napi_env env,napi_callback_info info)547 static napi_value OHNativeVSyncRequestFrameWithMultiCallbackNormal(napi_env env, napi_callback_info info)
548 {
549     napi_value result = nullptr;
550     napi_value result1 = nullptr;
551     napi_value result2 = nullptr;
552     napi_value result3 = nullptr;
553     napi_create_array_with_length(env, NUMBER_3, &result);
554     char name[] = "test";
555     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
556     OH_NativeVSync_FrameCallback callback = OnVSync;
557     auto *data = new std::string("hello");
558     OH_NativeVSync_RequestFrame(native_vsync, callback, data);
559     int ret = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, data);
560     napi_create_int32(env, ret, &result1);
561     napi_set_element(env, result, ARR_NUMBER_0, result1);
562     OH_NativeVSync_RequestFrame(native_vsync, callback, nullptr);
563     int ret1 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, data);
564     napi_create_int32(env, ret1, &result2);
565     napi_set_element(env, result, ARR_NUMBER_1, result2);
566     int num = 5;
567     for (int i = 0; i < num; i++) {
568         int ret3 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, data + i);
569         std::this_thread::sleep_for(std::chrono::milliseconds(1));
570         napi_create_int32(env, ret3, &result3);
571     }
572     napi_set_element(env, result, ARR_NUMBER_2, result3);
573     OH_NativeVSync_Destroy(native_vsync);
574     return result;
575 }
576 
OHNativeVSyncRequestFrameWithMultiCallbackNormal01(napi_env env,napi_callback_info info)577 static napi_value OHNativeVSyncRequestFrameWithMultiCallbackNormal01(napi_env env, napi_callback_info info)
578 {
579     napi_value result = nullptr;
580     napi_value result1 = nullptr;
581     napi_value result2 = nullptr;
582     napi_create_array_with_length(env, NUMBER_2, &result);
583     char name[] = "test";
584     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
585     OH_NativeVSync_FrameCallback callback = OnVSync;
586     auto *data = new std::string("hello");
587     int num1 = 100;
588     int num2 = 1000;
589     OH_NativeVSync_RequestFrame(native_vsync, callback, data);
590     for (int i = 0; i < num1; i++) {
591         int ret100 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, data);
592         std::this_thread::sleep_for(std::chrono::milliseconds(1));
593         napi_create_int32(env, ret100, &result1);
594     }
595     napi_set_element(env, result, ARR_NUMBER_0, result1);
596     for (int i = 0; i < num2; i++) {
597         int ret1000 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, data);
598         std::this_thread::sleep_for(std::chrono::milliseconds(1));
599         napi_create_int32(env, ret1000, &result2);
600     }
601     napi_set_element(env, result, ARR_NUMBER_1, result2);
602     OH_NativeVSync_Destroy(native_vsync);
603     return result;
604 }
605 
OHNativeVSyncRequestFrameWithMultiCallbackAbNormal(napi_env env,napi_callback_info info)606 static napi_value OHNativeVSyncRequestFrameWithMultiCallbackAbNormal(napi_env env, napi_callback_info info)
607 {
608     napi_value result = nullptr;
609     napi_value result1 = nullptr;
610     napi_value result2 = nullptr;
611     napi_value result3 = nullptr;
612     napi_create_array_with_length(env, NUMBER_3, &result);
613     char name[] = "test";
614     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
615     OH_NativeVSync_FrameCallback callback = OnVSync;
616     auto *data = new std::string("hello");
617     int ret1 = OH_NativeVSync_RequestFrameWithMultiCallback(nullptr, callback, data);
618     napi_create_int32(env, ret1, &result1);
619     napi_set_element(env, result, ARR_NUMBER_0, result1);
620     int ret2 = OH_NativeVSync_RequestFrameWithMultiCallback(ARR_NUMBER_0, callback, data);
621     napi_create_int32(env, ret2, &result2);
622     napi_set_element(env, result, ARR_NUMBER_1, result2);
623     int ret3 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, nullptr, data);
624     napi_create_int32(env, ret3, &result3);
625     napi_set_element(env, result, ARR_NUMBER_2, result3);
626     OH_NativeVSync_Destroy(native_vsync);
627     return result;
628 }
629 
OHNativeVSyncRequestFrameWithMultiCallbackAbNormal01(napi_env env,napi_callback_info info)630 static napi_value OHNativeVSyncRequestFrameWithMultiCallbackAbNormal01(napi_env env, napi_callback_info info)
631 {
632     napi_value result = nullptr;
633     napi_value result1 = nullptr;
634     napi_value result2 = nullptr;
635     napi_value result3 = nullptr;
636     napi_create_array_with_length(env, NUMBER_3, &result);
637     char name[] = "test";
638     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
639     OH_NativeVSync_FrameCallback callback = OnVSync;
640     auto *data = new std::string("hello");
641     int ret1 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, ARR_NUMBER_0, data);
642     napi_create_int32(env, ret1, &result1);
643     napi_set_element(env, result, ARR_NUMBER_0, result1);
644     int ret2 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, nullptr);
645     napi_create_int32(env, ret2, &result2);
646     napi_set_element(env, result, ARR_NUMBER_1, result2);
647     int ret3 = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, ARR_NUMBER_0);
648     napi_create_int32(env, ret3, &result3);
649     napi_set_element(env, result, ARR_NUMBER_2, result3);
650     OH_NativeVSync_Destroy(native_vsync);
651     return result;
652 }
653 
OHNativeVSyncSetExpectedFrameRateRange001(napi_env env,napi_callback_info info)654 static napi_value OHNativeVSyncSetExpectedFrameRateRange001(napi_env env, napi_callback_info info)
655 {
656     napi_value result = nullptr;
657     if (OH_NativeVSync_SetExpectedFrameRateRange(nullptr, nullptr) != 0) {
658         napi_create_int32(env, SUCCESS, &result);
659     } else {
660         napi_create_int32(env, FAIL, &result);
661     }
662     return result;
663 }
664 
OHNativeVSyncSetExpectedFrameRateRange002(napi_env env,napi_callback_info info)665 static napi_value OHNativeVSyncSetExpectedFrameRateRange002(napi_env env, napi_callback_info info)
666 {
667     napi_value result = nullptr;
668     OH_NativeVSync_ExpectedRateRange range = {60, 120, 120};
669     if (OH_NativeVSync_SetExpectedFrameRateRange(g_nativeVsync, &range) != 0) {
670         napi_create_int32(env, SUCCESS, &result);
671     } else {
672         napi_create_int32(env, FAIL, &result);
673     }
674     return result;
675 }
676 
OHNativeVSyncSetExpectedFrameRateRange003(napi_env env,napi_callback_info info)677 static napi_value OHNativeVSyncSetExpectedFrameRateRange003(napi_env env, napi_callback_info info)
678 {
679     napi_value result = nullptr;
680     napi_create_array_with_length(env, ARR_NUMBER_4, &result);
681     napi_value result1 = nullptr;
682     napi_value result2 = nullptr;
683     napi_value result3 = nullptr;
684     napi_value result4 = nullptr;
685 
686     OH_NativeVSync_ExpectedRateRange range = {0, 160, 0};
687     if (OH_NativeVSync_SetExpectedFrameRateRange(g_nativeVsync, &range) != 0) {
688         napi_create_int32(env, SUCCESS, &result1);
689     } else {
690         napi_create_int32(env, FAIL, &result1);
691     }
692     napi_set_element(env, result, ARR_NUMBER_0, result1);
693 
694     range = {-60, 120, 120};
695     if (OH_NativeVSync_SetExpectedFrameRateRange(g_nativeVsync, &range) != 0) {
696         napi_create_int32(env, SUCCESS, &result2);
697     } else {
698         napi_create_int32(env, FAIL, &result2);
699     }
700     napi_set_element(env, result, ARR_NUMBER_1, result2);
701 
702     range = {60, 30, 30};
703     if (OH_NativeVSync_SetExpectedFrameRateRange(g_nativeVsync, &range) != 0) {
704         napi_create_int32(env, SUCCESS, &result3);
705     } else {
706         napi_create_int32(env, FAIL, &result3);
707     }
708     napi_set_element(env, result, ARR_NUMBER_2, result3);
709 
710     range = {0, 60, 120};
711     if (OH_NativeVSync_SetExpectedFrameRateRange(g_nativeVsync, &range) != 0) {
712         napi_create_int32(env, SUCCESS, &result4);
713     } else {
714         napi_create_int32(env, FAIL, &result4);
715     }
716     napi_set_element(env, result, ARR_NUMBER_3, result4);
717     return result;
718 }
719 
vsyncInit(napi_env env,napi_value exports)720 static napi_value vsyncInit(napi_env env, napi_value exports)
721 {
722     napi_property_descriptor desc[] = {
723         {"oHNativeVSyncRequestFrameWithMultiCallbackNormal", nullptr, OHNativeVSyncRequestFrameWithMultiCallbackNormal,
724          nullptr, nullptr, nullptr, napi_default, nullptr},
725         {"oHNativeVSyncRequestFrameWithMultiCallbackNormal01", nullptr,
726          OHNativeVSyncRequestFrameWithMultiCallbackNormal01, nullptr, nullptr, nullptr, napi_default, nullptr},
727         {"oHNativeVSyncRequestFrameWithMultiCallbackAbNormal", nullptr,
728          OHNativeVSyncRequestFrameWithMultiCallbackAbNormal, nullptr, nullptr, nullptr, napi_default, nullptr},
729         {"oHNativeVSyncRequestFrameWithMultiCallbackAbNormal01", nullptr,
730          OHNativeVSyncRequestFrameWithMultiCallbackAbNormal01, nullptr, nullptr, nullptr, napi_default, nullptr},
731         {"oHNativeVSyncSetExpectedFrameRateRange001", nullptr,
732          OHNativeVSyncSetExpectedFrameRateRange001, nullptr, nullptr, nullptr, napi_default, nullptr},
733         {"oHNativeVSyncSetExpectedFrameRateRange002", nullptr,
734          OHNativeVSyncSetExpectedFrameRateRange002, nullptr, nullptr, nullptr, napi_default, nullptr},
735         {"oHNativeVSyncSetExpectedFrameRateRange003", nullptr,
736          OHNativeVSyncSetExpectedFrameRateRange003, nullptr, nullptr, nullptr, napi_default, nullptr},
737     };
738     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
739     return exports;
740 }
741 
742 EXTERN_C_START
Init(napi_env env,napi_value exports)743 static napi_value Init(napi_env env, napi_value exports)
744 {
745     vsyncInit(env, exports);
746     napi_property_descriptor desc[] = {
747         {"oHNativeVSyncCreate", nullptr, OHNativeVSyncCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
748         {"oHNativeVSyncCreateFOne", nullptr, OHNativeVSyncCreateFOne, nullptr, nullptr, nullptr, napi_default, nullptr},
749         {"oHNativeVSyncRequestFrame", nullptr, OHNativeVSyncRequestFrame, nullptr, nullptr, nullptr, napi_default,
750          nullptr},
751         {"oHNativeVSyncRequestFrameFOne", nullptr, OHNativeVSyncRequestFrameFOne, nullptr, nullptr, nullptr,
752          napi_default, nullptr},
753         {"oHNativeVSyncRequestFrameFTwo", nullptr, OHNativeVSyncRequestFrameFTwo, nullptr, nullptr, nullptr,
754          napi_default, nullptr},
755         {"oHNativeVSyncGetPeriod", nullptr, OHNativeVSyncGetPeriod, nullptr, nullptr, nullptr, napi_default, nullptr},
756         {"oHNativeVSyncGetPeriodFOne", nullptr, OHNativeVSyncGetPeriodFOne, nullptr, nullptr, nullptr, napi_default,
757          nullptr},
758         {"oHNativeVSyncCreateNull", nullptr, OHNativeVSyncCreateNull, nullptr, nullptr, nullptr, napi_default, nullptr},
759         {"oHNativeVSyncCreateNotEq", nullptr, OHNativeVSyncCreateNotEq, nullptr, nullptr, nullptr, napi_default,
760          nullptr},
761         {"oHNativeVSyncCreateNormal", nullptr, OHNativeVSyncCreateNormal, nullptr, nullptr, nullptr, napi_default,
762          nullptr},
763         {"oHNativeVSyncCreateAbnormal", nullptr, OHNativeVSyncCreateAbnormal, nullptr, nullptr, nullptr, napi_default,
764          nullptr},
765         {"oHNativeVSyncCreateDifLenth", nullptr, OHNativeVSyncCreateDifLenth, nullptr, nullptr, nullptr, napi_default,
766          nullptr},
767         {"oHNativeVSyncCreateMuch", nullptr, OHNativeVSyncCreateMuch, nullptr, nullptr, nullptr, napi_default, nullptr},
768         {"oHNativeVSyncGetPeriodNullptr", nullptr, OHNativeVSyncGetPeriodNullptr, nullptr, nullptr, nullptr,
769          napi_default, nullptr},
770         {"oHNativeVSyncRequestFrameNullptr", nullptr, OHNativeVSyncRequestFrameNullptr, nullptr, nullptr, nullptr,
771          napi_default, nullptr},
772         {"oHNativeVSyncRequestFrameNormal", nullptr, OHNativeVSyncRequestFrameNormal, nullptr, nullptr, nullptr,
773          napi_default, nullptr},
774         {"oHNativeVSyncRequestFrameParamErr", nullptr, OHNativeVSyncRequestFrameParamErr, nullptr, nullptr, nullptr,
775          napi_default, nullptr},
776         {"oHNativeVSyncCreateForAssociatedWindowNormal", nullptr, OHNativeVSyncCreateForAssociatedWindowNormal, nullptr,
777          nullptr, nullptr, napi_default, nullptr},
778         {"oHNativeVSyncCreateForAssociatedWindowAbNormal01", nullptr, OHNativeVSyncCreateForAssociatedWindowAbNormal01,
779          nullptr, nullptr, nullptr, napi_default, nullptr},
780         {"oHNativeVSyncCreateForAssociatedWindowAbNormal02", nullptr, OHNativeVSyncCreateForAssociatedWindowAbNormal02,
781          nullptr, nullptr, nullptr, napi_default, nullptr},
782         {"oHNativeVSyncCreateForAssociatedWindowAbNormal03", nullptr, OHNativeVSyncCreateForAssociatedWindowAbNormal03,
783          nullptr, nullptr, nullptr, napi_default, nullptr},
784         {"oHNativeVSyncDVSyncSwitch", nullptr, OHNativeVSyncDVSyncSwitch,
785          nullptr, nullptr, nullptr, napi_default, nullptr},
786         {"oHNativeVSyncDVSyncSwitchNullptr", nullptr, OHNativeVSyncDVSyncSwitchNullptr,
787          nullptr, nullptr, nullptr, napi_default, nullptr},
788     };
789     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
790     return exports;
791 }
792 EXTERN_C_END
793 
794 static napi_module demoModule = {
795     .nm_version = 1,
796     .nm_flags = 0,
797     .nm_filename = nullptr,
798     .nm_register_func = Init,
799     .nm_modname = "nativevsync",
800     .nm_priv = ((void *)0),
801     .reserved = {0},
802 };
803 
RegisterModule(void)804 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); };
805