1 /*
2  * Copyright (C) 2022 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 "my_pixel_map.h"
17 #include "media_errors.h"
18 #include "image_log.h"
19 #include "image_napi_utils.h"
20 #include "image_pixel_map_napi.h"
21 
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
24 
25 #undef LOG_TAG
26 #define LOG_TAG "MyPixelMapNapiTest"
27 
28 namespace {
29 constexpr uint32_t TEST_ARG_SUM = 1;
30 }
31 namespace OHOS {
32 namespace Media {
33 static const std::string CLASS_NAME = "MyPixelMap";
34 napi_ref MyPixelMap::sConstructor_ = nullptr;
MyPixelMap()35 MyPixelMap::MyPixelMap():env_(nullptr)
36 {
37 }
38 
~MyPixelMap()39 MyPixelMap::~MyPixelMap()
40 {
41 }
42 
Init(napi_env env,napi_value exports)43 napi_value MyPixelMap::Init(napi_env env, napi_value exports)
44 {
45     napi_property_descriptor props[] = {
46     };
47 
48     napi_property_descriptor static_prop[] = {
49         DECLARE_NAPI_STATIC_FUNCTION("testGetImageInfo", TestGetImageInfo),
50         DECLARE_NAPI_STATIC_FUNCTION("testAccessPixels", TestAccessPixels),
51         DECLARE_NAPI_STATIC_FUNCTION("testUnAccessPixels", TestUnAccessPixels),
52     };
53 
54     napi_value constructor = nullptr;
55 
56     if (napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, IMG_ARRAY_SIZE(props),
57         props, &constructor) != napi_ok) {
58         IMAGE_LOGE("define class fail");
59         return nullptr;
60     }
61 
62     if (napi_create_reference(env, constructor, 1, &sConstructor_) != napi_ok) {
63         IMAGE_LOGE("create reference fail");
64         return nullptr;
65     }
66 
67     if (napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor) != napi_ok) {
68         IMAGE_LOGE("set named property fail");
69         return nullptr;
70     }
71 
72     if (napi_define_properties(env, exports, IMG_ARRAY_SIZE(static_prop), static_prop) != napi_ok) {
73         IMAGE_LOGE("define properties fail");
74         return nullptr;
75     }
76 
77     IMAGE_LOGD("Init success");
78     return exports;
79 }
80 
Constructor(napi_env env,napi_callback_info info)81 napi_value MyPixelMap::Constructor(napi_env env, napi_callback_info info)
82 {
83     IMAGE_LOGD("Constructor IN");
84     napi_value undefineVar = nullptr;
85     napi_get_undefined(env, &undefineVar);
86 
87     napi_status status;
88     napi_value thisVar = nullptr;
89     napi_get_undefined(env, &thisVar);
90 
91     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
92 
93     IMAGE_LOGD("Constructor OUT");
94     return thisVar;
95 }
96 
TestGetImageInfo(napi_env env,napi_callback_info info)97 napi_value MyPixelMap::TestGetImageInfo(napi_env env, napi_callback_info info)
98 {
99     IMAGE_LOGD("TestGetImageInfo IN");
100 
101     napi_value result = nullptr;
102     napi_get_undefined(env, &result);
103 
104     napi_status status;
105     napi_value thisVar = nullptr;
106     napi_value argValue[TEST_ARG_SUM] = {0};
107     size_t argCount = TEST_ARG_SUM;
108 
109     status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
110     if (status != napi_ok) {
111         IMAGE_LOGE("napi_get_cb_info fail");
112     }
113 
114     IMAGE_LOGD("OH_GetImageInfo Test|Begin");
115     OhosPixelMapInfo pixelMapInfo;
116     int32_t res = OH_GetImageInfo(env, argValue[0], &pixelMapInfo);
117     IMAGE_LOGD("OH_GetImageInfo Test|End, res=%{public}d", res);
118     IMAGE_LOGD("OH_GetImageInfo, w=%{public}u, h=%{public}u, r=%{public}u, f=%{public}d",
119         pixelMapInfo.width, pixelMapInfo.height, pixelMapInfo.rowSize, pixelMapInfo.pixelFormat);
120 
121     IMAGE_LOGD("TestGetImageInfo OUT");
122     return result;
123 }
124 
TestAccessPixels(napi_env env,napi_callback_info info)125 napi_value MyPixelMap::TestAccessPixels(napi_env env, napi_callback_info info)
126 {
127     IMAGE_LOGD("TestAccessPixels IN");
128 
129     napi_value result = nullptr;
130     napi_get_undefined(env, &result);
131 
132     napi_status status;
133     napi_value thisVar = nullptr;
134     napi_value argValue[TEST_ARG_SUM] = {0};
135     size_t argCount = TEST_ARG_SUM;
136 
137     status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
138     if (status != napi_ok) {
139         IMAGE_LOGE("napi_get_cb_info fail");
140     }
141 
142     IMAGE_LOGD("OH_AccessPixels Test|Begin");
143     void* addrPtr = nullptr;
144     int32_t res = OH_AccessPixels(env, argValue[0], &addrPtr);
145     IMAGE_LOGD("OH_AccessPixels Test|End, res=%{public}d", res);
146 
147     IMAGE_LOGD("TestAccessPixels OUT");
148     return result;
149 }
150 
TestUnAccessPixels(napi_env env,napi_callback_info info)151 napi_value MyPixelMap::TestUnAccessPixels(napi_env env, napi_callback_info info)
152 {
153     IMAGE_LOGD("TestUnAccessPixels IN");
154 
155     napi_value result = nullptr;
156     napi_get_undefined(env, &result);
157 
158     napi_status status;
159     napi_value thisVar = nullptr;
160     napi_value argValue[TEST_ARG_SUM] = {0};
161     size_t argCount = TEST_ARG_SUM;
162 
163     status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
164     if (status != napi_ok) {
165         IMAGE_LOGE("napi_get_cb_info fail");
166     }
167 
168     IMAGE_LOGD("OH_UnAccessPixels Test|Begin");
169     int32_t res = OH_UnAccessPixels(env, argValue[0]);
170     IMAGE_LOGD("OH_UnAccessPixels Test|End, res=%{public}d", res);
171 
172     IMAGE_LOGD("TestUnAccessPixels OUT");
173     return result;
174 }
175 
176 /*
177  * Function registering all props and functions of ohos.medialibrary module
178  */
Export(napi_env env,napi_value exports)179 static napi_value Export(napi_env env, napi_value exports)
180 {
181     IMAGE_LOGI("MyPixelMap CALL");
182     MyPixelMap::Init(env, exports);
183     return exports;
184 }
185 
186 /*
187  * module define
188  */
189 static napi_module g_module = {
190     .nm_version = 1,
191     .nm_flags = 0,
192     .nm_filename = nullptr,
193     .nm_register_func = Export,
194     .nm_modname = "xtstest.mypixelmap",
195     .nm_priv = ((void*)0),
196     .reserved = {0}
197 };
198 
199 /*
200  * module register
201  */
MyPixelMapRegisterModule(void)202 extern "C" __attribute__((constructor)) void MyPixelMapRegisterModule(void)
203 {
204     napi_module_register(&g_module);
205 }
206 }  // namespace Media
207 }  // namespace OHOS
208