1 /*
2 * Copyright (c) 2021-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 "input/camera_size_napi.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23
24 thread_local napi_ref CameraSizeNapi::sConstructor_ = nullptr;
25 thread_local Size* CameraSizeNapi::sCameraPicSize_ = nullptr;
26
CameraSizeNapi()27 CameraSizeNapi::CameraSizeNapi() : env_(nullptr), wrapper_(nullptr)
28 {
29 }
30
~CameraSizeNapi()31 CameraSizeNapi::~CameraSizeNapi()
32 {
33 MEDIA_DEBUG_LOG("~CameraSizeNapi is called");
34 if (wrapper_ != nullptr) {
35 napi_delete_reference(env_, wrapper_);
36 }
37 if (cameraPicSize_) {
38 cameraPicSize_ = nullptr;
39 }
40 }
41
CameraSizeNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)42 void CameraSizeNapi::CameraSizeNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
43 {
44 MEDIA_DEBUG_LOG("CameraSizeNapiDestructor is called");
45 CameraSizeNapi* cameraSizeNapi = reinterpret_cast<CameraSizeNapi*>(nativeObject);
46 if (cameraSizeNapi != nullptr) {
47 delete cameraSizeNapi;
48 }
49 }
50
Init(napi_env env,napi_value exports)51 napi_value CameraSizeNapi::Init(napi_env env, napi_value exports)
52 {
53 MEDIA_DEBUG_LOG("Init is called");
54 napi_status status;
55 napi_value ctorObj;
56 int32_t refCount = 1;
57
58 napi_property_descriptor camera_size_props[] = {
59 DECLARE_NAPI_GETTER("width", GetCameraSizeWidth),
60 DECLARE_NAPI_GETTER("height", GetCameraSizeHeight)
61 };
62
63 status = napi_define_class(env, CAMERA_SIZE_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
64 CameraSizeNapiConstructor, nullptr,
65 sizeof(camera_size_props) / sizeof(camera_size_props[PARAM0]),
66 camera_size_props, &ctorObj);
67 if (status == napi_ok) {
68 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
69 if (status == napi_ok) {
70 status = napi_set_named_property(env, exports, CAMERA_SIZE_NAPI_CLASS_NAME, ctorObj);
71 if (status == napi_ok) {
72 return exports;
73 }
74 }
75 }
76 MEDIA_ERR_LOG("Init call Failed!");
77 return nullptr;
78 }
79
80 // Constructor callback
CameraSizeNapiConstructor(napi_env env,napi_callback_info info)81 napi_value CameraSizeNapi::CameraSizeNapiConstructor(napi_env env, napi_callback_info info)
82 {
83 MEDIA_DEBUG_LOG("CameraSizeNapiConstructor is called");
84 napi_status status;
85 napi_value result = nullptr;
86 napi_value thisVar = nullptr;
87
88 napi_get_undefined(env, &result);
89 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
90
91 if (status == napi_ok && thisVar != nullptr) {
92 std::unique_ptr<CameraSizeNapi> obj = std::make_unique<CameraSizeNapi>();
93 obj->env_ = env;
94 obj->cameraPicSize_= sCameraPicSize_;
95 MEDIA_INFO_LOG("CameraSizeNapiConstructor "
96 "size.width = %{public}d, size.height = %{public}d",
97 obj->cameraPicSize_->width, obj->cameraPicSize_->height);
98 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
99 CameraSizeNapi::CameraSizeNapiDestructor, nullptr, nullptr);
100 if (status == napi_ok) {
101 obj.release();
102 return thisVar;
103 } else {
104 MEDIA_ERR_LOG("Failure wrapping js to native napi");
105 }
106 }
107 MEDIA_ERR_LOG("CameraSizeNapiConstructor call Failed!");
108 return result;
109 }
110
CreateCameraSize(napi_env env,Size & cameraPicSize)111 napi_value CameraSizeNapi::CreateCameraSize(napi_env env, Size &cameraPicSize)
112 {
113 MEDIA_DEBUG_LOG("CreateCameraSize is called");
114 napi_status status;
115 napi_value result = nullptr;
116 napi_value constructor;
117
118 status = napi_get_reference_value(env, sConstructor_, &constructor);
119 if (status == napi_ok) {
120 std::unique_ptr<Size> sizePtr = std::make_unique<Size>();
121 sizePtr->width = cameraPicSize.width;
122 sizePtr->height = cameraPicSize.height;
123 MEDIA_INFO_LOG("CreateCameraSize cameraPicSize size.width = %{public}d, size.height = %{public}d",
124 sizePtr->width, sizePtr->height);
125 sCameraPicSize_ = reinterpret_cast<Size*>(sizePtr.get());
126 MEDIA_INFO_LOG("CreateCameraSize sCameraPicSize_ size.width = %{public}d, size.height = %{public}d",
127 sCameraPicSize_->width, sCameraPicSize_->height);
128 status = napi_new_instance(env, constructor, 0, nullptr, &result);
129 if (status == napi_ok && result != nullptr) {
130 return result;
131 } else {
132 MEDIA_ERR_LOG("Failed to create Camera obj instance");
133 }
134 }
135 MEDIA_ERR_LOG("CreateCameraSize call Failed!");
136 napi_get_undefined(env, &result);
137 return result;
138 }
139
GetCameraSizeWidth(napi_env env,napi_callback_info info)140 napi_value CameraSizeNapi::GetCameraSizeWidth(napi_env env, napi_callback_info info)
141 {
142 MEDIA_DEBUG_LOG("GetCameraSizeWidth is called");
143 napi_status status;
144 napi_value jsResult = nullptr;
145 napi_value undefinedResult = nullptr;
146 CameraSizeNapi* obj = nullptr;
147 uint32_t cameraSizeWidth = 0;
148 napi_value thisVar = nullptr;
149
150 napi_get_undefined(env, &undefinedResult);
151 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
152
153 if (status != napi_ok || thisVar == nullptr) {
154 MEDIA_ERR_LOG("Invalid arguments!");
155 return undefinedResult;
156 }
157
158 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
159 if ((status == napi_ok) && (obj != nullptr)) {
160 cameraSizeWidth = obj->cameraPicSize_->width;
161 MEDIA_INFO_LOG("CreateCameraSize GetCameraSizeWidth "
162 "size.width = %{public}d, size.height = %{public}d",
163 obj->cameraPicSize_->width, obj->cameraPicSize_->height);
164 status = napi_create_uint32(env, cameraSizeWidth, &jsResult);
165 if (status == napi_ok) {
166 return jsResult;
167 } else {
168 MEDIA_ERR_LOG("Failed to get CameraSizeWidth!, errorCode : %{public}d", status);
169 }
170 }
171 MEDIA_ERR_LOG("GetCameraSizeWidth call Failed!");
172 return undefinedResult;
173 }
174
GetCameraSizeHeight(napi_env env,napi_callback_info info)175 napi_value CameraSizeNapi::GetCameraSizeHeight(napi_env env, napi_callback_info info)
176 {
177 MEDIA_DEBUG_LOG("GetCameraSizeHeight is called");
178 napi_status status;
179 napi_value jsResult = nullptr;
180 napi_value undefinedResult = nullptr;
181 CameraSizeNapi* obj = nullptr;
182 uint32_t cameraSizeHeight = 0;
183 napi_value thisVar = nullptr;
184
185 napi_get_undefined(env, &undefinedResult);
186 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
187
188 if (status != napi_ok || thisVar == nullptr) {
189 MEDIA_ERR_LOG("Invalid arguments!");
190 return undefinedResult;
191 }
192
193 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
194 if ((status == napi_ok) && (obj != nullptr)) {
195 cameraSizeHeight = obj->cameraPicSize_->height;
196 MEDIA_INFO_LOG("GetCameraSizeWidth size.width = %{public}d, size.height = %{public}d",
197 obj->cameraPicSize_->width, obj->cameraPicSize_->height);
198 status = napi_create_uint32(env, cameraSizeHeight, &jsResult);
199 if (status == napi_ok) {
200 return jsResult;
201 } else {
202 MEDIA_ERR_LOG("Failed to get CameraSizeHeight!, errorCode : %{public}d", status);
203 }
204 }
205 MEDIA_ERR_LOG("GetCameraSizeHeight call Failed!");
206 return undefinedResult;
207 }
208 } // namespace CameraStandard
209 } // namespace OHOS
210