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