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