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