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 <cstdint>
18 #include <hilog/log.h>
19 #include <multimedia/player_framework/native_avcodec_base.h>
20 #include <native_image/native_image.h>
21 #include <native_window/external_window.h>
22 #include <native_buffer/native_buffer.h>
23 #include "nativewindow.h"
24 #include "ace/xcomponent/native_interface_xcomponent.h"
25 #include <string>
26
27 #define SUCCESS 0
28 #define FAIL (-1)
29
30 #define CONSTANT_2147483647 2147483647
31 #define CONSTANT_2147483648 2147483648
32 #define CONSTANT_NEG_2147483647 (-2147483647)
33 #define CONSTANT_NEG_2147483648 (-2147483648)
34 #define CONSTANT_NEG_9223372036854775807 (9223372036854775807)
35 #define CONSTANT_NEG_9223372036854775808 (-9223372036854775808)
36
37
38 #define CONSTANT_0 0
39 #define CONSTANT_1 1
40 #define CONSTANT_2 2
41 #define CONSTANT_3 3
42 #define CONSTANT_4 4
43 #define CONSTANT_5 5
44 #define CONSTANT_6 6
45 #define CONSTANT_7 7
46 #define CONSTANT_8 8
47 #define CONSTANT_60 60
48 #define CONSTANT_2999 2999
49 #define CONSTANT_3000 3000
50 #define CONSTANT_3001 3001
51 #define CONSTANT_998899 998899
52 #define CONSTANT_100 100
53 #define CONSTANT_1000 1000
54 #define CONSTANT_10000 10000
55 #define CONSTANT_100000 100000
56 #define CONSTANT_1000000 1000000
57 #define CONSTANT_10000000 10000000
58 #define CONSTANT_100000000 100000000
59 #define CONSTANT_1000000000 1000000000
60 #define CONSTANT_1999999 1999999
61 #define CONSTANT_99999999 99999999
62 #define CONSTANT_99999999999999999 99999999999999999
63 #define CONSTANT_999999999999999999 999999999999999999
64 #define CONSTANT_40001000 40001000
65 #define CONSTANT_50002000 50002000
66 #define CONSTANT_50102000 50102000
67 #define CONSTANT_41211000 41211000
68 #define CONSTANT_40601000 40601000
69 #define CONSTANT_41210000 41210000
70 #define CONSTANT_500 500
71
72
testNativeWindowCreateNativeWindow(napi_env env,napi_callback_info info)73 napi_value testNativeWindowCreateNativeWindow(napi_env env, napi_callback_info info)
74 {
75 napi_value result = nullptr;
76 OHNativeWindow *nativeWindow = nullptr;
77 nativeWindow = OH_NativeWindow_CreateNativeWindow(nullptr);
78 if (nativeWindow == nullptr) {
79 napi_create_int32(env, SUCCESS, &result);
80 return result;
81 }
82 napi_create_int32(env, FAIL, &result);
83 return result;
84 }
85
testNativeWindowNativeWindowSetSetScalingMode(napi_env env,napi_callback_info info)86 napi_value testNativeWindowNativeWindowSetSetScalingMode(napi_env env, napi_callback_info info)
87 {
88 napi_value result = nullptr;
89 InitNativeWindow* initNative = new InitNativeWindow();
90 OHNativeWindow *nativeWindow = nullptr;
91 nativeWindow = initNative->returnNativeWindow();
92 if (nativeWindow == nullptr) {
93 napi_create_int32(env, CONSTANT_998899, &result);
94 return result;
95 }
96 uint32_t sequence = 1000;
97 int32_t flag1 = OH_NativeWindow_NativeWindowSetScalingMode(nullptr, sequence, OH_SCALING_MODE_FREEZE);
98 int32_t flag2 =
99 OH_NativeWindow_NativeWindowSetScalingMode(nativeWindow, sequence, static_cast<OHScalingMode>(CONSTANT_100));
100 int32_t flag3 = OH_NativeWindow_NativeWindowSetScalingMode(nativeWindow, 0, OH_SCALING_MODE_FREEZE);
101 if (flag1 != CONSTANT_40001000 && flag2 != CONSTANT_40001000 && flag3 != CONSTANT_40001000) {
102 napi_create_int32(env, CONSTANT_1, &result);
103 return result;
104 }
105 delete initNative;
106 napi_create_int32(env, SUCCESS, &result);
107 return result;
108 }
109
testNativeWindowNativeWindowSetMetaData(napi_env env,napi_callback_info info)110 napi_value testNativeWindowNativeWindowSetMetaData(napi_env env, napi_callback_info info)
111 {
112 napi_value result = nullptr;
113 InitNativeWindow* initNative = new InitNativeWindow();
114 OHNativeWindow *nativeWindow = nullptr;
115 nativeWindow = initNative->returnNativeWindow();
116 if (nativeWindow == nullptr) {
117 napi_create_int32(env, CONSTANT_998899, &result);
118 return result;
119 }
120 uint32_t sequence = 1000;
121 int32_t size = 0;
122 const OHHDRMetaData metaData[] = {{OH_METAKEY_RED_PRIMARY_X, 0}};
123 int32_t flag1 = OH_NativeWindow_NativeWindowSetMetaData(nullptr, sequence, sizeof(metaData), metaData);
124 int32_t flag2 = OH_NativeWindow_NativeWindowSetMetaData(nativeWindow, sequence, sizeof(metaData), nullptr);
125 int32_t flag3 = OH_NativeWindow_NativeWindowSetMetaData(nativeWindow, 0, sizeof(metaData), metaData);
126 if (flag1 != CONSTANT_40001000 && flag2 != CONSTANT_40001000 && flag3 != CONSTANT_40001000) {
127 napi_create_int32(env, CONSTANT_1, &result);
128 return result;
129 }
130 delete initNative;
131 napi_create_int32(env, SUCCESS, &result);
132 return result;
133 }
134
testNativeWindowNativeWindowSetMetaDataSet(napi_env env,napi_callback_info info)135 napi_value testNativeWindowNativeWindowSetMetaDataSet(napi_env env, napi_callback_info info)
136 {
137 napi_value result = nullptr;
138 InitNativeWindow* initNative = new InitNativeWindow();
139 OHNativeWindow *nativeWindow = nullptr;
140 nativeWindow = initNative->returnNativeWindow();
141 if (nativeWindow == nullptr) {
142 napi_create_int32(env, CONSTANT_998899, &result);
143 return result;
144 }
145 uint32_t sequence = 1000;
146 int32_t size = 0;
147 OHHDRMetadataKey key = OHHDRMetadataKey::OH_METAKEY_HDR10_PLUS;
148 const uint8_t metaData[] = {0};
149 int32_t flag1 = OH_NativeWindow_NativeWindowSetMetaDataSet(nullptr, sequence, key, sizeof(metaData), metaData);
150 int32_t flag2 = OH_NativeWindow_NativeWindowSetMetaDataSet(nativeWindow, sequence, key, sizeof(metaData), nullptr);
151 int32_t flag3 = OH_NativeWindow_NativeWindowSetMetaDataSet(
152 nativeWindow, sequence, static_cast<OHHDRMetadataKey>(CONSTANT_100), sizeof(metaData), metaData);
153 if (flag1 != CONSTANT_40001000 && flag2 != CONSTANT_40001000 && flag3 != CONSTANT_40001000) {
154 napi_create_int32(env, CONSTANT_1000, &result);
155 return result;
156 }
157 delete initNative;
158 napi_create_int32(env, SUCCESS, &result);
159 return result;
160 }
161
AllocOHExtDataHandle(uint32_t reserveInts)162 static OHExtDataHandle *AllocOHExtDataHandle(uint32_t reserveInts)
163 {
164 size_t handleSize = sizeof(OHExtDataHandle) + (sizeof(int32_t) * reserveInts);
165 OHExtDataHandle *handle = static_cast<OHExtDataHandle *>(malloc(handleSize));
166 if (handle == nullptr) {
167 return nullptr;
168 }
169 handle->fd = -1;
170 handle->reserveInts = reserveInts;
171 for (uint32_t i = 0; i < reserveInts; i++) {
172 handle->reserve[i] = -1;
173 }
174 return handle;
175 }
176
testNativeWindowNativeWindowSetTunnelHandle(napi_env env,napi_callback_info info)177 napi_value testNativeWindowNativeWindowSetTunnelHandle(napi_env env, napi_callback_info info)
178 {
179 napi_value result = nullptr;
180 InitNativeWindow* initNative = new InitNativeWindow();
181 OHNativeWindow *nativeWindow = nullptr;
182 nativeWindow = initNative->returnNativeWindow();
183 if (nativeWindow == nullptr) {
184 napi_create_int32(env, CONSTANT_998899, &result);
185 return result;
186 }
187 uint32_t reserveInts = 1;
188 OHExtDataHandle *handle = AllocOHExtDataHandle(reserveInts);
189 int32_t flag1 = OH_NativeWindow_NativeWindowSetTunnelHandle(nullptr, handle);
190 int32_t flag2 = OH_NativeWindow_NativeWindowSetTunnelHandle(nativeWindow, nullptr);
191 if (flag1 != CONSTANT_40001000 && flag2 != CONSTANT_40001000) {
192 napi_create_int32(env, CONSTANT_1000, &result);
193 return result;
194 }
195 int32_t flag3 = OH_NativeWindow_NativeWindowSetTunnelHandle(nativeWindow, handle);
196 if (flag3 != SUCCESS) {
197 napi_create_int32(env, CONSTANT_10000, &result);
198 return result;
199 }
200 delete initNative;
201 napi_create_int32(env, SUCCESS, &result);
202 return result;
203 }
204
testNativeWindowCleanCacheBeforeBufferRequested(napi_env env,napi_callback_info info)205 napi_value testNativeWindowCleanCacheBeforeBufferRequested(napi_env env, napi_callback_info info)
206 {
207 napi_value result = nullptr;
208
209 // 1. create native window
210 InitNativeWindow* initNative = new InitNativeWindow();
211 OHNativeWindow *nativeWindow = nullptr;
212 nativeWindow = initNative->returnNativeWindow();
213 if (nativeWindow == nullptr) {
214 napi_create_int32(env, CONSTANT_998899, &result);
215 return result;
216 }
217
218 // 2. cleancache 2 times
219 int32_t flag = OH_NativeWindow_CleanCache(nativeWindow);
220 flag = OH_NativeWindow_CleanCache(nativeWindow);
221 napi_create_int32(env, flag, &result);
222 if (flag != CONSTANT_41211000) {
223 napi_create_int32(env, FAIL, &result);
224 return result;
225 }
226
227 delete initNative;
228 napi_create_int32(env, SUCCESS, &result);
229 return result;
230 }
231
testNativeWindowCleanCacheWhenBufferRequested(napi_env env,napi_callback_info info)232 napi_value testNativeWindowCleanCacheWhenBufferRequested(napi_env env, napi_callback_info info)
233 {
234 napi_value result = nullptr;
235
236 // 1. create native window
237 InitNativeWindow* initNative = new InitNativeWindow();
238 OHNativeWindow *nativeWindow = nullptr;
239 nativeWindow = initNative->returnNativeWindow();
240 if (nativeWindow == nullptr) {
241 napi_create_int32(env, CONSTANT_998899, &result);
242 return result;
243 }
244
245 // 2. invoke OH_NativeWindow_NativeWindowRequestBuffer
246 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
247 int fenceFd = -1;
248 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
249 if (ret != 0) {
250 napi_create_int32(env, FAIL, &result);
251 return result;
252 }
253
254 // 3. invoke 2 times cleancache
255 int32_t flag = OH_NativeWindow_CleanCache(nativeWindow);
256 flag = OH_NativeWindow_CleanCache(nativeWindow);
257 if (flag != 0) {
258 napi_create_int32(env, FAIL, &result);
259 return result;
260 }
261
262 // 4. invoke OH_NativeWindow_NativeWindowFlushBuffer
263 Region::Rect rect{
264 .x = 0x100,
265 .y = 0x100,
266 .w = 0x100,
267 .h = 0x100,
268 };
269 Region region{.rects = &rect};
270 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, region);
271 if (ret != CONSTANT_40001000) {
272 napi_create_int32(env, ret, &result);
273 return result;
274 }
275
276 delete initNative;
277 napi_create_int32(env, SUCCESS, &result);
278 return result;
279 }
280
testNativeWindowCleanCacheWhenBufferFlushed(napi_env env,napi_callback_info info)281 napi_value testNativeWindowCleanCacheWhenBufferFlushed(napi_env env, napi_callback_info info)
282 {
283 napi_value result = nullptr;
284
285 // 1. create native window
286 InitNativeWindow* initNative = new InitNativeWindow();
287 OHNativeWindow *nativeWindow = nullptr;
288 nativeWindow = initNative->returnNativeWindow();
289 if (nativeWindow == nullptr) {
290 napi_create_int32(env, CONSTANT_998899, &result);
291 return result;
292 }
293
294 // 2. invoke OH_NativeWindow_NativeWindowRequestBuffer
295 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
296 int fenceFd = -1;
297 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
298 if (ret != 0) {
299 napi_create_int32(env, FAIL, &result);
300 return result;
301 }
302
303 // 3. invoke OH_NativeWindow_NativeWindowFlushBuffer
304 Region::Rect rect{
305 .x = 0x100,
306 .y = 0x100,
307 .w = 0x100,
308 .h = 0x100,
309 };
310 Region region{.rects = &rect};
311 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, region);
312 if (ret != 0) {
313 napi_create_int32(env, FAIL, &result);
314 return result;
315 }
316
317 // 4. invoke 2 times cleancache
318 int32_t flag = OH_NativeWindow_CleanCache(nativeWindow);
319 flag = OH_NativeWindow_CleanCache(nativeWindow);
320 if (flag != 0) {
321 napi_create_int32(env, FAIL, &result);
322 return result;
323 }
324
325 // 5. invoke OH_NativeImage_AcquireNativeWindowBuffer
326 OH_NativeImage* nativeImage = nullptr;
327 nativeImage = initNative->returnNativeImage();
328 if (nativeImage == nullptr) {
329 napi_create_int32(env, CONSTANT_998899, &result);
330 return result;
331 }
332 ret = OH_NativeImage_AcquireNativeWindowBuffer(nativeImage, &nativeWindowBuffer, &fenceFd);
333 if (ret != CONSTANT_40601000) {
334 napi_create_int32(env, ret, &result);
335 return result;
336 }
337
338 delete initNative;
339 napi_create_int32(env, SUCCESS, &result);
340 return result;
341 }
342
testNativeWindowCleanCacheWhenBufferAcquired(napi_env env,napi_callback_info info)343 napi_value testNativeWindowCleanCacheWhenBufferAcquired(napi_env env, napi_callback_info info)
344 {
345 napi_value result = nullptr;
346
347 // 1. create native window
348 InitNativeWindow* initNative = new InitNativeWindow();
349 OHNativeWindow *nativeWindow = nullptr;
350 nativeWindow = initNative->returnNativeWindow();
351 if (nativeWindow == nullptr) {
352 napi_create_int32(env, CONSTANT_998899, &result);
353 return result;
354 }
355
356 OH_NativeImage* nativeImage = nullptr;
357 nativeImage = initNative->returnNativeImage();
358 if (nativeImage == nullptr) {
359 napi_create_int32(env, CONSTANT_998899, &result);
360 return result;
361 }
362 RoundWindowBuffer* windowBuffer = new RoundWindowBuffer(nativeImage, nativeWindow);
363
364 int32_t ret = windowBuffer->ProduceBuffer();
365 if (ret != 0) {
366 napi_create_int32(env, FAIL, &result);
367 return result;
368 }
369
370 // AcquireBuffer
371 ret = windowBuffer->AcquireBuffer();
372 if (ret != 0) {
373 napi_create_int32(env, FAIL, &result);
374 return result;
375 }
376
377 // 5. invoke 2 times cleancache
378 int32_t flag = OH_NativeWindow_CleanCache(nativeWindow);
379 flag = OH_NativeWindow_CleanCache(nativeWindow);
380 if (flag != 0) {
381 napi_create_int32(env, FAIL, &result);
382 return result;
383 }
384
385 // 6. release buffer
386 ret = windowBuffer->ReleaseBuffer();
387 if (ret != CONSTANT_41210000) {
388 napi_create_int32(env, FAIL, &result);
389 return result;
390 }
391
392 delete windowBuffer;
393 delete initNative;
394 napi_create_int32(env, SUCCESS, &result);
395 return result;
396 }
397
testNativeWindowCleanCacheWhenBufferReleased(napi_env env,napi_callback_info info)398 napi_value testNativeWindowCleanCacheWhenBufferReleased(napi_env env, napi_callback_info info)
399 {
400 napi_value result = nullptr;
401
402 // 1. create native window
403 InitNativeWindow* initNative = new InitNativeWindow();
404 OHNativeWindow *nativeWindow = nullptr;
405 nativeWindow = initNative->returnNativeWindow();
406 if (nativeWindow == nullptr) {
407 napi_create_int32(env, CONSTANT_998899, &result);
408 return result;
409 }
410
411 OH_NativeImage* nativeImage = nullptr;
412 nativeImage = initNative->returnNativeImage();
413 RoundWindowBuffer* windowBuffer = new RoundWindowBuffer(nativeImage, nativeWindow);
414
415 int32_t ret = windowBuffer->ProduceBuffer();
416 if (ret != 0) {
417 napi_create_int32(env, FAIL, &result);
418 return result;
419 }
420
421 ret = windowBuffer->ConsumerBuffer();
422 if (ret != 0) {
423 napi_create_int32(env, FAIL, &result);
424 return result;
425 }
426
427 // invoke 2 times cleancache
428 int32_t flag = OH_NativeWindow_CleanCache(nativeWindow);
429 flag = OH_NativeWindow_CleanCache(nativeWindow);
430 if (flag != 0) {
431 napi_create_int32(env, FAIL, &result);
432 return result;
433 }
434
435 delete windowBuffer;
436 delete initNative;
437 napi_create_int32(env, SUCCESS, &result);
438 return result;
439 }
440
testNativeWindowCleanCacheLoopWhenBufferRequested(napi_env env,napi_callback_info info)441 napi_value testNativeWindowCleanCacheLoopWhenBufferRequested(napi_env env, napi_callback_info info)
442 {
443 napi_value result = nullptr;
444
445 // 1. create native window
446 InitNativeWindow* initNative = new InitNativeWindow();
447 OHNativeWindow *nativeWindow = nullptr;
448 nativeWindow = initNative->returnNativeWindow();
449 if (nativeWindow == nullptr) {
450 napi_create_int32(env, CONSTANT_998899, &result);
451 return result;
452 }
453
454 // 2. RequestBuffer
455 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
456 int fenceFd = -1;
457 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
458 if (ret != 0) {
459 napi_create_int32(env, FAIL, &result);
460 return result;
461 }
462
463 int32_t flag = 0;
464 for (int i = 0; i < CONSTANT_10000; i++) {
465 flag = OH_NativeWindow_CleanCache(nativeWindow);
466 if (flag != 0) {
467 napi_create_int32(env, FAIL, &result);
468 return result;
469 }
470 }
471
472 delete initNative;
473 napi_create_int32(env, SUCCESS, &result);
474 return result;
475 }
476
testNativeWindowCleanCacheLoopWhenBufferFlushed(napi_env env,napi_callback_info info)477 napi_value testNativeWindowCleanCacheLoopWhenBufferFlushed(napi_env env, napi_callback_info info)
478 {
479 napi_value result = nullptr;
480
481 // 1. create native window
482 InitNativeWindow* initNative = new InitNativeWindow();
483 OHNativeWindow *nativeWindow = nullptr;
484 nativeWindow = initNative->returnNativeWindow();
485 if (nativeWindow == nullptr) {
486 napi_create_int32(env, CONSTANT_998899, &result);
487 return result;
488 }
489
490 // 2. RequestBuffer
491 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
492 int fenceFd = -1;
493 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
494 if (ret != 0) {
495 napi_create_int32(env, FAIL, &result);
496 return result;
497 }
498
499 // 3. FlushBuffer
500 Region::Rect rect{
501 .x = 0x50,
502 .y = 0x50,
503 .w = 0x50,
504 .h = 0x50,
505 };
506 Region region{.rects = &rect};
507 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, region);
508 if (ret != 0) {
509 napi_create_int32(env, FAIL, &result);
510 return result;
511 }
512
513 int32_t flag = 0;
514 for (int i = 0; i < CONSTANT_10000; i++) {
515 flag = OH_NativeWindow_CleanCache(nativeWindow);
516 if (flag != 0) {
517 napi_create_int32(env, FAIL, &result);
518 return result;
519 }
520 }
521
522 delete initNative;
523 napi_create_int32(env, SUCCESS, &result);
524 return result;
525 }
526