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