• 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 "image_source_test.h"
17 #include "node_api.h"
18 #include "image_pixel_map_napi.h"
19 #include <cstdlib>
20 #include "hilog/log.h"
21 
22 namespace {
23     constexpr size_t SIZE_ZERO = 0;
24     constexpr size_t SIZE_ONE = 1;
25     constexpr size_t SIZE_TWO = 2;
26     constexpr size_t SIZE_THREE = 3;
27     constexpr size_t DEFAULT_STRING_SIZE = 64;
28     constexpr uint32_t ARGS_FIRST = 0;
29     constexpr uint32_t ARGS_SECOND = 1;
30     constexpr uint32_t ARGS_THIRD = 2;
31     constexpr uint32_t INVALID_FRAME_COUNT = 0;
32     constexpr int8_t INT8_FALSE = 0;
33     constexpr int8_t INT8_TRUE = 1;
34     constexpr int32_t DEFAULT_INDEX = 0;
35 }
36 
37 namespace OHOS {
38 namespace Media {
39 
40 const unsigned int LOG_ID = 0xD002B05;
41 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_ID, "ImageSourceNDKTest"};
42 #define MY_HILOG(op, fmt, args...) \
43     do {                                  \
44         op(LABEL, "{%{public}s:%{public}d} " fmt, __FUNCTION__, __LINE__, ##args);  \
45     } while (0)
46 #define DEBUG_LOG(fmt, ...) MY_HILOG(OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__)
47 #define DEBUG_PTR(p) (((p) == nullptr)?"nullptr":"not nullptr")
48 
49 #define STATIC_FUNCTION(n, f) { (n), nullptr, (f), nullptr, nullptr, nullptr, napi_static, nullptr }
Init(napi_env env,napi_value exports)50 napi_value ImageSourceNDKTest::Init(napi_env env, napi_value exports)
51 {
52     napi_property_descriptor props[] = {
53         STATIC_FUNCTION("create", Create),
54         STATIC_FUNCTION("createIncremental", CreateIncremental),
55         STATIC_FUNCTION("initNative", InitNative),
56         STATIC_FUNCTION("createPixelMap", CreatePixelMap),
57         STATIC_FUNCTION("createPixelMapList", CreatePixelMapList),
58         STATIC_FUNCTION("getDelayTime", GetDelayTime),
59         STATIC_FUNCTION("getFrameCount", GetFrameCount),
60         STATIC_FUNCTION("getSupportedFormats", GetSupportedFormats),
61         STATIC_FUNCTION("getImageInfo", GetImageInfo),
62         STATIC_FUNCTION("getImageProperty", GetImageProperty),
63         STATIC_FUNCTION("modifyImageProperty", ModifyImageProperty),
64         STATIC_FUNCTION("updateData", UpdateData),
65         STATIC_FUNCTION("release", Release),
66     };
67     napi_define_properties(env, exports, sizeof(props) / sizeof(props[ARGS_FIRST]), props);
68     return exports;
69 }
70 
GetBoolProperty(napi_env env,napi_value root,const char * utf8name,int8_t * res)71 static bool GetBoolProperty(napi_env env, napi_value root, const char* utf8name, int8_t* res)
72 {
73     napi_value property = nullptr;
74     auto status = napi_get_named_property(env, root, utf8name, &property);
75     if (status != napi_ok || property == nullptr) {
76         DEBUG_LOG("Get property error %{public}s", utf8name);
77         return false;
78     }
79     bool tmp = false;
80     status = napi_get_value_bool(env, property, &tmp);
81     *res = tmp ? INT8_TRUE : INT8_FALSE;
82     return (status == napi_ok);
83 }
84 
GetInt32Property(napi_env env,napi_value root,const char * utf8name,int32_t * res)85 static bool GetInt32Property(napi_env env, napi_value root, const char* utf8name, int32_t* res)
86 {
87     napi_value property = nullptr;
88     auto status = napi_get_named_property(env, root, utf8name, &property);
89     if (status != napi_ok || property == nullptr) {
90         DEBUG_LOG("Get property error %{public}s", utf8name);
91         return false;
92     }
93     return (napi_get_value_int32(env, property, res) == napi_ok);
94 }
95 
GetUint32Property(napi_env env,napi_value root,const char * utf8name,uint32_t * res)96 static bool GetUint32Property(napi_env env, napi_value root, const char* utf8name, uint32_t* res)
97 {
98     napi_value property = nullptr;
99     auto status = napi_get_named_property(env, root, utf8name, &property);
100     if (status != napi_ok || property == nullptr) {
101         DEBUG_LOG("Get property error %{public}s", utf8name);
102         return false;
103     }
104     return (napi_get_value_uint32(env, property, res) == napi_ok);
105 }
106 
GetArrayBufferProperty(napi_env env,napi_value root,const char * utf8name,uint8_t ** buffer,size_t * bufferSize)107 static bool GetArrayBufferProperty(napi_env env, napi_value root, const char* utf8name,
108     uint8_t** buffer, size_t* bufferSize)
109 {
110     napi_value property = nullptr;
111     auto status = napi_get_named_property(env, root, utf8name, &property);
112     if (status != napi_ok || property == nullptr) {
113         DEBUG_LOG("Get property error %{public}s", utf8name);
114         return false;
115     }
116     void* tmp;
117     status = napi_get_arraybuffer_info(env, property, &tmp, bufferSize);
118     *buffer = static_cast<uint8_t*>(tmp);
119     return (status == napi_ok);
120 }
121 
GetStringValue(napi_env env,napi_value value,char ** buffer,size_t * bufferSize)122 static bool GetStringValue(napi_env env, napi_value value, char** buffer, size_t *bufferSize)
123 {
124     if (napi_ok != napi_get_value_string_utf8(env, value, nullptr, SIZE_ZERO, bufferSize)
125         && *bufferSize == SIZE_ZERO) {
126         DEBUG_LOG("Get napi string length error");
127         return false;
128     }
129     *buffer = (char*)malloc((*bufferSize) + 1);
130     if (napi_ok != napi_get_value_string_utf8(env, value, *buffer, (*bufferSize) + 1, bufferSize)) {
131         DEBUG_LOG("Get napi string error");
132         return false;
133     }
134     return (*bufferSize > SIZE_ZERO);
135 }
136 
setInt32NamedProperty(napi_env env,napi_value object,const char * utf8name,uint32_t value)137 static void setInt32NamedProperty(napi_env env, napi_value object, const char* utf8name, uint32_t value)
138 {
139     napi_value tmp;
140     napi_create_int32(env, value, &tmp);
141     napi_set_named_property(env, object, utf8name, tmp);
142 }
143 
checkType(napi_env env,napi_value arg,napi_valuetype type)144 static bool checkType(napi_env env, napi_value arg, napi_valuetype type)
145 {
146     napi_valuetype argType = napi_undefined;
147     napi_typeof(env, arg, &argType);
148     return (type == argType);
149 }
checkArgs(const napi_value * argValue,size_t argCount,size_t want)150 static bool checkArgs(const napi_value* argValue, size_t argCount, size_t want)
151 {
152     if (argCount < want) {
153         DEBUG_LOG("argCount %{public}zu < want %{public}zu", argCount, want);
154         return false;
155     }
156     for (size_t i = SIZE_ZERO; i < want; i++) {
157         if (argValue[i] == nullptr) {
158             DEBUG_LOG("argValue[%{public}zu] is nullptr", i);
159             return false;
160         }
161     }
162     return true;
163 }
parseImageSource(napi_env env,napi_value arg,struct OhosImageSource & src)164 static bool parseImageSource(napi_env env, napi_value arg, struct OhosImageSource &src)
165 {
166     if (env == nullptr || arg == nullptr) {
167         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
168         return false;
169     }
170     bool isArrayBuffer = false;
171     if (checkType(env, arg, napi_string)) {
172         DEBUG_LOG("Uri arg In");
173         if (!GetStringValue(env, arg, &src.uri, &src.uriSize)) {
174             DEBUG_LOG("Uri arg failed");
175             return false;
176         }
177         DEBUG_LOG("Uri arg %{public}s", src.uri);
178         return true;
179     } else if (checkType(env, arg, napi_number)) {
180         DEBUG_LOG("Fd arg In");
181         if (napi_ok != napi_get_value_int32(env, arg, &(src.fd))) {
182             DEBUG_LOG("Fd arg failed");
183             return false;
184         }
185         return true;
186     } else if (napi_is_arraybuffer(env, arg, &isArrayBuffer) == napi_ok && isArrayBuffer) {
187         DEBUG_LOG("Buffer arg In");
188         void* buf = nullptr;
189         if (napi_ok != napi_get_arraybuffer_info(env, arg, &buf, &(src.bufferSize)) || buf == nullptr ||
190             src.bufferSize == SIZE_ZERO) {
191             DEBUG_LOG("Fd arg failed");
192             return false;
193         }
194         src.buffer = static_cast<uint8_t*>(buf);
195         return true;
196     }
197     DEBUG_LOG("Invaild arg type");
198     return false;
199 }
OhosImageSourceRelease(struct OhosImageSource & src)200 static void OhosImageSourceRelease(struct OhosImageSource &src)
201 {
202     if (src.uri != nullptr) {
203         free(src.uri);
204         src.uri = nullptr;
205     }
206 }
parseImageSourceOpt(napi_env env,napi_value arg,struct OhosImageSourceOps & src)207 static bool parseImageSourceOpt(napi_env env, napi_value arg, struct OhosImageSourceOps &src)
208 {
209     if (env == nullptr || arg == nullptr) {
210         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
211         return false;
212     }
213     // Optional parameters, no need check error.
214     GetInt32Property(env, arg, "density", &(src.density));
215     GetInt32Property(env, arg, "pixelFormat", &(src.pixelFormat));
216     napi_value nSize = nullptr;
217     auto status = napi_get_named_property(env, arg, "size", &nSize);
218     if (status == napi_ok && nSize != nullptr) {
219         GetInt32Property(env, nSize, "width", &(src.size.width));
220         GetInt32Property(env, nSize, "height", &(src.size.height));
221     }
222     return true;
223 }
parseImageDecodingOps(napi_env env,napi_value arg,struct OhosImageDecodingOps & ops)224 static bool parseImageDecodingOps(napi_env env, napi_value arg, struct OhosImageDecodingOps &ops)
225 {
226     if (env == nullptr || arg == nullptr) {
227         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
228         return false;
229     }
230 
231     // Optional parameters, no need check error.
232     GetBoolProperty(env, arg, "editable", &(ops.editable));
233     GetInt32Property(env, arg, "pixelFormat", &(ops.pixelFormat));
234     GetInt32Property(env, arg, "fitDensity", &(ops.fitDensity));
235     GetUint32Property(env, arg, "index", &(ops.index));
236     GetUint32Property(env, arg, "sampleSize", &(ops.sampleSize));
237     GetUint32Property(env, arg, "rotate", &(ops.rotate));
238     napi_value nSize = nullptr;
239     auto status = napi_get_named_property(env, arg, "size", &nSize);
240     if (status == napi_ok && nSize != nullptr) {
241         GetInt32Property(env, nSize, "width", &(ops.size.width));
242         GetInt32Property(env, nSize, "height", &(ops.size.height));
243     }
244     napi_value nRegion = nullptr;
245     status = napi_get_named_property(env, arg, "region", &nRegion);
246     if (status == napi_ok && nRegion != nullptr) {
247         GetInt32Property(env, nRegion, "x", &(ops.region.x));
248         GetInt32Property(env, nRegion, "y", &(ops.region.y));
249         GetInt32Property(env, nRegion, "width", &(ops.region.width));
250         GetInt32Property(env, nRegion, "height", &(ops.region.height));
251     }
252     return true;
253 }
createUndefine(napi_env env)254 static napi_value createUndefine(napi_env env)
255 {
256     napi_value udfVal = nullptr;
257     napi_get_undefined(env, &udfVal);
258     return udfVal;
259 }
createResultValue(napi_env env,int32_t resCode,napi_value res=nullptr)260 static napi_value createResultValue(napi_env env, int32_t resCode, napi_value res = nullptr)
261 {
262     napi_value result = nullptr;
263     // JS result like
264     // {code: <error code>, result: <result value>}
265     napi_value nRes = nullptr;
266     napi_create_int32(env, resCode, &nRes);
267     napi_create_object(env, &result);
268     napi_set_named_property(env, result, "code", nRes);
269     if (res != nullptr) {
270         napi_set_named_property(env, result, "result", res);
271     }
272     return result;
273 }
Create(napi_env env,napi_callback_info info)274 napi_value ImageSourceNDKTest::Create(napi_env env, napi_callback_info info)
275 {
276     napi_value thisVar = nullptr;
277     napi_value argValue[SIZE_TWO] = {0};
278     size_t argCount = SIZE_TWO;
279     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
280         !checkArgs(argValue, argCount, SIZE_TWO)) {
281         return createUndefine(env);
282     }
283     struct OhosImageSource src;
284     if (!parseImageSource(env, argValue[ARGS_FIRST], src)) {
285         DEBUG_LOG("parseImageSource failed!!!");
286         OhosImageSourceRelease(src);
287         return createUndefine(env);
288     }
289     struct OhosImageSourceOps ops;
290     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
291         DEBUG_LOG("parseImageSourceOpt failed!!!");
292         OhosImageSourceRelease(src);
293         return createUndefine(env);
294     }
295     napi_value imageSource = nullptr;
296     int32_t res = OH_ImageSource_Create(env, &src, &ops, &imageSource);
297     OhosImageSourceRelease(src);
298     return createResultValue(env, res, imageSource);
299 }
300 
CreateIncremental(napi_env env,napi_callback_info info)301 napi_value ImageSourceNDKTest::CreateIncremental(napi_env env, napi_callback_info info)
302 {
303     napi_value thisVar = nullptr;
304     napi_value argValue[SIZE_TWO] = {0};
305     size_t argCount = SIZE_TWO;
306     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
307         !checkArgs(argValue, argCount, SIZE_TWO)) {
308         return createUndefine(env);
309     }
310     // No source check. There is no source input.
311     struct OhosImageSource src;
312     parseImageSource(env, argValue[ARGS_FIRST], src);
313     struct OhosImageSourceOps ops;
314     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
315         DEBUG_LOG("parseImageSourceOpt failed!!!");
316         OhosImageSourceRelease(src);
317         return createUndefine(env);
318     }
319     napi_value imageSource = nullptr;
320     int32_t res = OH_ImageSource_CreateIncremental(env, &src, &ops, &imageSource);
321     if (res != OHOS_IMAGE_RESULT_SUCCESS || imageSource == nullptr) {
322         OhosImageSourceRelease(src);
323         return createUndefine(env);
324     }
325     OhosImageSourceRelease(src);
326     return createResultValue(env, res, imageSource);
327 }
328 
getNativeImageSource(napi_env env,napi_callback_info info,napi_value * argValue,size_t & argCount)329 static ImageSourceNative* getNativeImageSource(napi_env env, napi_callback_info info,
330     napi_value* argValue, size_t &argCount)
331 {
332     napi_value thisVar = nullptr;
333     if (argValue == nullptr || argCount == SIZE_ZERO) {
334         DEBUG_LOG("Invaild input!");
335         return nullptr;
336     }
337     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok) {
338         return nullptr;
339     }
340     return OH_ImageSource_InitNative(env, argValue[ARGS_FIRST]);
341 }
342 
InitNative(napi_env env,napi_callback_info info)343 napi_value ImageSourceNDKTest::InitNative(napi_env env, napi_callback_info info)
344 {
345     napi_value argValue[SIZE_ONE] = {0};
346     size_t argCount = SIZE_ONE;
347 
348     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
349     if (native == nullptr) {
350         return createUndefine(env);
351     }
352     return createResultValue(env, OHOS_IMAGE_RESULT_SUCCESS);
353 }
354 
CreatePixelMap(napi_env env,napi_callback_info info)355 napi_value ImageSourceNDKTest::CreatePixelMap(napi_env env, napi_callback_info info)
356 {
357     napi_value argValue[SIZE_TWO] = {0};
358     size_t argCount = SIZE_TWO;
359 
360     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
361     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
362         DEBUG_LOG("argValue check failed");
363         return createUndefine(env);
364     }
365     struct OhosImageDecodingOps ops;
366     if (!parseImageDecodingOps(env, argValue[ARGS_SECOND], ops)) {
367         DEBUG_LOG("parseImageDecodingOps failed");
368         return createUndefine(env);
369     }
370     napi_value pixelmap = nullptr;
371     int32_t res = OH_ImageSource_CreatePixelMap(native, &ops, &pixelmap);
372     return createResultValue(env, res, pixelmap);
373 }
374 
CreatePixelMapList(napi_env env,napi_callback_info info)375 napi_value ImageSourceNDKTest::CreatePixelMapList(napi_env env, napi_callback_info info)
376 {
377     napi_value argValue[SIZE_TWO] = {0};
378     size_t argCount = SIZE_TWO;
379 
380     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
381     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
382         DEBUG_LOG("argValue check failed");
383         return createUndefine(env);
384     }
385     struct OhosImageDecodingOps ops;
386     if (!parseImageDecodingOps(env, argValue[ARGS_SECOND], ops)) {
387         DEBUG_LOG("parseImageDecodingOps failed");
388         return createUndefine(env);
389     }
390     napi_value pixelmapList = nullptr;
391     int32_t res = OH_ImageSource_CreatePixelMapList(native, &ops, &pixelmapList);
392     return createResultValue(env, res, pixelmapList);
393 }
394 
ReleaseDelayTimeList(struct OhosImageSourceDelayTimeList & timeList)395 static void ReleaseDelayTimeList(struct OhosImageSourceDelayTimeList &timeList)
396 {
397     if (timeList.delayTimeList != nullptr) {
398         timeList.size = SIZE_ZERO;
399         free(timeList.delayTimeList);
400         timeList.delayTimeList = nullptr;
401     }
402 }
403 
GetDelayTime(napi_env env,napi_callback_info info)404 napi_value ImageSourceNDKTest::GetDelayTime(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
405 {
406     napi_value argValue[SIZE_ONE] = {0};
407     size_t argCount = SIZE_ONE;
408 
409     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
410     if (native == nullptr) {
411         DEBUG_LOG("argValue check failed");
412         return createUndefine(env);
413     }
414 
415     struct OhosImageSourceDelayTimeList timeList;
416     int32_t res = OH_ImageSource_GetDelayTime(native, &timeList);
417     if (timeList.size == SIZE_ZERO || res != OHOS_IMAGE_RESULT_SUCCESS) {
418         DEBUG_LOG("Delay time list get failed");
419         return createUndefine(env);
420     }
421     timeList.delayTimeList = (int32_t*)malloc(sizeof(int32_t) * timeList.size);
422     res = OH_ImageSource_GetDelayTime(native, &timeList);
423 
424     napi_value result;
425     napi_create_array(env, &result);
426     for (size_t i = SIZE_ZERO; i < timeList.size; i++) {
427         napi_value nDelayTime = nullptr;
428         napi_create_int32(env, timeList.delayTimeList[i], &nDelayTime);
429         napi_set_element(env, result, i, nDelayTime);
430     }
431     ReleaseDelayTimeList(timeList);
432     return createResultValue(env, res, result);
433 }
434 
GetFrameCount(napi_env env,napi_callback_info info)435 napi_value ImageSourceNDKTest::GetFrameCount(napi_env env, napi_callback_info info)
436 {
437     napi_value argValue[SIZE_ONE] = {0};
438     size_t argCount = SIZE_ONE;
439 
440     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
441     if (native == nullptr) {
442         DEBUG_LOG("argValue check failed");
443         return createUndefine(env);
444     }
445     uint32_t frameCount = INVALID_FRAME_COUNT;
446     int32_t res = OH_ImageSource_GetFrameCount(native, &frameCount);
447 
448     napi_value nFrameCount = nullptr;
449     napi_create_int32(env, frameCount, &nFrameCount);
450     return createResultValue(env, res, nFrameCount);
451 }
ReleaseSupportedFormatList(struct OhosImageSourceSupportedFormatList & formatList)452 static void ReleaseSupportedFormatList(struct OhosImageSourceSupportedFormatList &formatList)
453 {
454     if (formatList.supportedFormatList == nullptr) {
455         return;
456     }
457     for (size_t i = SIZE_ZERO; i < formatList.size; i++) {
458         auto format = formatList.supportedFormatList[i];
459         if (format != nullptr) {
460             if (format->format != nullptr) {
461                 free(format->format);
462                 format->format = nullptr;
463             }
464             free(format);
465         }
466     }
467     free(formatList.supportedFormatList);
468     formatList.supportedFormatList = nullptr;
469 }
GetSupportedFormats(napi_env env,napi_callback_info info)470 napi_value ImageSourceNDKTest::GetSupportedFormats(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
471 {
472     struct OhosImageSourceSupportedFormatList formatList;
473     int32_t res = OH_ImageSource_GetSupportedFormats(&formatList);
474     if (formatList.size == SIZE_ZERO || res != OHOS_IMAGE_RESULT_SUCCESS) {
475         DEBUG_LOG("Supported format list get failed");
476         return createUndefine(env);
477     }
478     formatList.supportedFormatList = (struct OhosImageSourceSupportedFormat**)malloc(
479         sizeof(struct OhosImageSourceSupportedFormat*) * formatList.size);
480     for (size_t i = 0; i < formatList.size; i++) {
481         formatList.supportedFormatList[i] = (struct OhosImageSourceSupportedFormat*)malloc(
482             sizeof(struct OhosImageSourceSupportedFormat));
483         formatList.supportedFormatList[i]->format = (char*)malloc(DEFAULT_STRING_SIZE);
484         formatList.supportedFormatList[i]->size = DEFAULT_STRING_SIZE;
485     }
486 
487     res = OH_ImageSource_GetSupportedFormats(&formatList);
488     napi_value result;
489     napi_create_array(env, &result);
490     for (size_t i = SIZE_ZERO; i < formatList.size; i++) {
491         napi_value nFormat = nullptr;
492         auto format = formatList.supportedFormatList[i];
493         napi_create_string_utf8(env, format->format, format->size, &nFormat);
494         napi_set_element(env, result, i, nFormat);
495     }
496     ReleaseSupportedFormatList(formatList);
497     return createResultValue(env, res, result);
498 }
499 
createImageInfoNVal(napi_env env,const struct OhosImageSourceInfo & imageInfo)500 static napi_value createImageInfoNVal(napi_env env, const struct OhosImageSourceInfo &imageInfo)
501 {
502     napi_value result = nullptr;
503     napi_create_object(env, &result);
504     setInt32NamedProperty(env, result, "pixelFormat", imageInfo.pixelFormat);
505     setInt32NamedProperty(env, result, "colorSpace", imageInfo.colorSpace);
506     setInt32NamedProperty(env, result, "alphaType", imageInfo.alphaType);
507     setInt32NamedProperty(env, result, "density", imageInfo.density);
508     napi_value nSize = nullptr;
509     napi_create_object(env, &nSize);
510     setInt32NamedProperty(env, nSize, "width", imageInfo.size.width);
511     setInt32NamedProperty(env, nSize, "height", imageInfo.size.height);
512     napi_set_named_property(env, result, "size", nSize);
513     return result;
514 }
515 
GetImageInfo(napi_env env,napi_callback_info info)516 napi_value ImageSourceNDKTest::GetImageInfo(napi_env env, napi_callback_info info)
517 {
518     napi_value argValue[SIZE_TWO] = {0};
519     size_t argCount = SIZE_TWO;
520     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
521     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
522         DEBUG_LOG("argValue check failed");
523         return createUndefine(env);
524     }
525     int32_t index = DEFAULT_INDEX;
526     napi_get_value_int32(env, argValue[ARGS_SECOND], &index);
527     struct OhosImageSourceInfo imageInfo;
528     int32_t res = OH_ImageSource_GetImageInfo(native, index, &imageInfo);
529     napi_value nImageInfo = createImageInfoNVal(env, imageInfo);
530     return createResultValue(env, res, nImageInfo);
531 }
532 
GetImageProperty(napi_env env,napi_callback_info info)533 napi_value ImageSourceNDKTest::GetImageProperty(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
534 {
535     napi_value argValue[SIZE_TWO] = {0};
536     size_t argCount = SIZE_TWO;
537     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
538     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
539         DEBUG_LOG("argValue check failed");
540         return createUndefine(env);
541     }
542     struct OhosImageSourceProperty key;
543     if (!GetStringValue(env, argValue[ARGS_SECOND], &key.value, &key.size) ||
544         key.value == nullptr || key.size == SIZE_ZERO) {
545         DEBUG_LOG("Get key failed");
546         return createUndefine(env);
547     }
548     struct OhosImageSourceProperty val;
549     int32_t res = OH_ImageSource_GetImageProperty(native, &key, &val);
550     if (val.size == SIZE_ZERO) {
551         DEBUG_LOG("Get val size failed");
552         return createResultValue(env, res, createUndefine(env));
553     }
554     val.value = (char*)malloc(val.size);
555 
556     res = OH_ImageSource_GetImageProperty(native, &key, &val);
557     napi_value nValue = nullptr;
558     if (val.value != nullptr && val.size != SIZE_ZERO) {
559         napi_create_string_utf8(env, val.value, val.size, &nValue);
560     } else {
561         DEBUG_LOG("Get val is empty");
562     }
563     if (val.value != nullptr) {
564         free(val.value);
565     }
566     return createResultValue(env, res, nValue);
567 }
568 
ModifyImageProperty(napi_env env,napi_callback_info info)569 napi_value ImageSourceNDKTest::ModifyImageProperty(napi_env env, napi_callback_info info)
570 {
571     napi_value argValue[SIZE_THREE] = {0};
572     size_t argCount = SIZE_THREE;
573     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
574     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_THREE)) {
575         DEBUG_LOG("argValue check failed");
576         return createUndefine(env);
577     }
578     struct OhosImageSourceProperty key;
579     if (!GetStringValue(env, argValue[ARGS_SECOND], &key.value, &key.size) ||
580         key.value == nullptr || key.size == SIZE_ZERO) {
581         DEBUG_LOG("Get key failed");
582         return createUndefine(env);
583     }
584     struct OhosImageSourceProperty val;
585     if (!GetStringValue(env, argValue[ARGS_THIRD], &val.value, &val.size) ||
586         val.value == nullptr || val.size == SIZE_ZERO) {
587         DEBUG_LOG("Get val failed");
588         return createUndefine(env);
589     }
590     int32_t res = OH_ImageSource_ModifyImageProperty(native, &key, &val);
591     return createResultValue(env, res);
592 }
parseImageSourceUpdateData(napi_env env,napi_value arg,struct OhosImageSourceUpdateData & data)593 static bool parseImageSourceUpdateData(napi_env env, napi_value arg, struct OhosImageSourceUpdateData &data)
594 {
595     if (env == nullptr || arg == nullptr) {
596         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
597         return false;
598     }
599     GetArrayBufferProperty(env, arg, "buffer", &(data.buffer), &(data.bufferSize));
600     GetUint32Property(env, arg, "offset", &(data.offset));
601     GetUint32Property(env, arg, "updateLength", &(data.updateLength));
602     GetBoolProperty(env, arg, "isCompleted", &(data.isCompleted));
603     return true;
604 }
UpdateData(napi_env env,napi_callback_info info)605 napi_value ImageSourceNDKTest::UpdateData(napi_env env, napi_callback_info info)
606 {
607     napi_value argValue[SIZE_TWO] = {0};
608     size_t argCount = SIZE_TWO;
609     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
610     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
611         DEBUG_LOG("argValue check failed");
612         return createUndefine(env);
613     }
614     struct OhosImageSourceUpdateData data;
615     parseImageSourceUpdateData(env, argValue[ARGS_SECOND], data);
616     int32_t res = OH_ImageSource_UpdateData(native, &data);
617     return createResultValue(env, res);
618 }
619 
Release(napi_env env,napi_callback_info info)620 napi_value ImageSourceNDKTest::Release(napi_env env, napi_callback_info info)
621 {
622     napi_value argValue[SIZE_ONE] = {0};
623     size_t argCount = SIZE_ONE;
624     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
625     if (native == nullptr) {
626         DEBUG_LOG("argValue check failed");
627         return createUndefine(env);
628     }
629     int32_t res = OH_ImageSource_Release(native);
630     return createResultValue(env, res);
631 }
632 
633 EXTERN_C_START
ModuleRegister(napi_env env,napi_value exports)634 static napi_value ModuleRegister(napi_env env, napi_value exports)
635 {
636     ImageSourceNDKTest::Init(env, exports);
637     return exports;
638 }
639 
640 static napi_module demoModule = {
641     .nm_version =1,
642     .nm_flags = 0,
643     .nm_filename = nullptr,
644     .nm_register_func = ModuleRegister,
645     .nm_modname = "ImageSourceNDKTest",
646     .nm_priv = nullptr,
647     .reserved = { 0 },
648 };
649 
RegisterModule(void)650 __attribute__((constructor)) void RegisterModule(void)
651 {
652     napi_module_register(&demoModule);
653 }
654 EXTERN_C_END
655 }
656 }