1 /*
2 * Copyright (c) 2024-2024 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 "camera_napi_param_parser.h"
17 #include "camera_napi_security_utils.h"
18 #include "capture_session.h"
19 #include "mode/light_painting_session_napi.h"
20
21 namespace OHOS {
22 namespace CameraStandard {
23 using namespace std;
24
25 thread_local napi_ref LightPaintingSessionNapi::sConstructor_ = nullptr;
26
LightPaintingSessionNapi()27 LightPaintingSessionNapi::LightPaintingSessionNapi() : env_(nullptr), wrapper_(nullptr)
28 {
29 }
~LightPaintingSessionNapi()30 LightPaintingSessionNapi::~LightPaintingSessionNapi()
31 {
32 MEDIA_INFO_LOG("~LightPaintingSessionNapi is called");
33 CHECK_EXECUTE(wrapper_ != nullptr, napi_delete_reference(env_, wrapper_));
34 if (lightPaintingSession_) {
35 lightPaintingSession_ = nullptr;
36 }
37 }
LightPaintingSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)38 void LightPaintingSessionNapi::LightPaintingSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
39 {
40 MEDIA_INFO_LOG("LightPaintingSessionNapiDestructor is called");
41 LightPaintingSessionNapi* cameraObj = reinterpret_cast<LightPaintingSessionNapi*>(nativeObject);
42 if (cameraObj != nullptr) {
43 delete cameraObj;
44 }
45 }
Init(napi_env env,napi_value exports)46 napi_value LightPaintingSessionNapi::Init(napi_env env, napi_value exports)
47 {
48 MEDIA_DEBUG_LOG("Init is called");
49 napi_status status;
50 napi_value ctorObj;
51 std::vector<napi_property_descriptor> light_painting_props = {
52 DECLARE_NAPI_FUNCTION("getSupportedLightPaintingTypes", GetSupportedLightPaintings),
53 DECLARE_NAPI_FUNCTION("getLightPaintingType", GetLightPainting),
54 DECLARE_NAPI_FUNCTION("setLightPaintingType", SetLightPainting),
55 };
56 std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props,
57 color_effect_props, focus_props, manual_focus_props, zoom_props, flash_props, light_painting_props};
58 std::vector<napi_property_descriptor> light_painting_session_props =
59 CameraNapiUtils::GetPropertyDescriptor(descriptors);
60 status = napi_define_class(env, LIGHT_PAINTING_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
61 LightPaintingSessionNapiConstructor, nullptr,
62 light_painting_session_props.size(),
63 light_painting_session_props.data(), &ctorObj);
64 if (status == napi_ok) {
65 int32_t refCount = 1;
66 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
67 if (status == napi_ok) {
68 status = napi_set_named_property(env, exports, LIGHT_PAINTING_SESSION_NAPI_CLASS_NAME, ctorObj);
69 CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
70 }
71 }
72 MEDIA_ERR_LOG("Init call Failed!");
73 return nullptr;
74 }
75
CreateCameraSession(napi_env env)76 napi_value LightPaintingSessionNapi::CreateCameraSession(napi_env env)
77 {
78 MEDIA_INFO_LOG("CreateCameraSession is called");
79 CAMERA_SYNC_TRACE;
80 napi_status status;
81 napi_value result = nullptr;
82 napi_value constructor;
83 status = napi_get_reference_value(env, sConstructor_, &constructor);
84 if (status == napi_ok) {
85 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::LIGHT_PAINTING);
86 if (sCameraSession_ == nullptr) {
87 MEDIA_ERR_LOG("Failed to create light painting session instance");
88 napi_get_undefined(env, &result);
89 return result;
90 }
91 status = napi_new_instance(env, constructor, 0, nullptr, &result);
92 sCameraSession_ = nullptr;
93 if (status == napi_ok && result != nullptr) {
94 MEDIA_DEBUG_LOG("success to create light painting session napi instance");
95 return result;
96 } else {
97 MEDIA_ERR_LOG("Failed to create light painting session napi instance");
98 }
99 } else {
100 MEDIA_ERR_LOG("Failed to create napi reference value instance");
101 }
102 MEDIA_ERR_LOG("Failed to create light painting session napi instance last");
103 napi_get_undefined(env, &result);
104 return result;
105 }
106
LightPaintingSessionNapiConstructor(napi_env env,napi_callback_info info)107 napi_value LightPaintingSessionNapi::LightPaintingSessionNapiConstructor(napi_env env, napi_callback_info info)
108 {
109 MEDIA_INFO_LOG("LightPaintingSessionNapiConstructor is called");
110 napi_status status;
111 napi_value result = nullptr;
112 napi_value thisVar = nullptr;
113
114 napi_get_undefined(env, &result);
115 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
116
117 if (status == napi_ok && thisVar != nullptr) {
118 std::unique_ptr<LightPaintingSessionNapi> obj = std::make_unique<LightPaintingSessionNapi>();
119 obj->env_ = env;
120 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
121 obj->lightPaintingSession_ = static_cast<LightPaintingSession*>(sCameraSession_.GetRefPtr());
122 obj->cameraSession_ = obj->lightPaintingSession_;
123 CHECK_ERROR_RETURN_RET_LOG(obj->lightPaintingSession_ == nullptr, result, "lightPaintingSession_ is null");
124 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
125 LightPaintingSessionNapi::LightPaintingSessionNapiDestructor, nullptr, nullptr);
126 if (status == napi_ok) {
127 obj.release();
128 return thisVar;
129 } else {
130 MEDIA_ERR_LOG("LightPaintingSessionNapi Failure wrapping js to native napi");
131 }
132 }
133 MEDIA_ERR_LOG("LightPaintingSessionNapi call Failed!");
134 return result;
135 }
136
GetSupportedLightPaintings(napi_env env,napi_callback_info info)137 napi_value LightPaintingSessionNapi::GetSupportedLightPaintings(napi_env env, napi_callback_info info)
138 {
139 CHECK_ERROR_RETURN_RET_LOG(!CameraNapiSecurity::CheckSystemApp(env),
140 nullptr, "SystemApi GetSupportedLightPaintings is called!");
141 MEDIA_INFO_LOG("GetSupportedLightPaintings is called");
142 napi_status status;
143 auto result = CameraNapiUtils::GetUndefinedValue(env);
144
145 LightPaintingSessionNapi* lightPaintingSessionNapi = nullptr;
146 CameraNapiParamParser jsParamParser(env, info, lightPaintingSessionNapi);
147 if (lightPaintingSessionNapi->lightPaintingSession_ == nullptr) {
148 MEDIA_ERR_LOG("GetSupportedLightPaintings get native object fail");
149 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
150 return result;
151 }
152 status = napi_create_array(env, &result);
153 std::vector<LightPaintingType> lightPaintingTypes;
154 int32_t retCode = lightPaintingSessionNapi->lightPaintingSession_->GetSupportedLightPaintings(lightPaintingTypes);
155 CHECK_ERROR_RETURN_RET_LOG(!CameraNapiUtils::CheckError(env, retCode), nullptr,
156 "GetSupportedLightPaintings fail %{public}d", retCode);
157 if (!lightPaintingTypes.empty() && status == napi_ok) {
158 for (size_t i = 0; i < lightPaintingTypes.size(); i++) {
159 int lightPaintingType = lightPaintingTypes[i];
160 napi_value value;
161 napi_create_int32(env, lightPaintingType, &value);
162 napi_set_element(env, result, i, value);
163 }
164 }
165 return result;
166 }
167
GetLightPainting(napi_env env,napi_callback_info info)168 napi_value LightPaintingSessionNapi::GetLightPainting(napi_env env, napi_callback_info info)
169 {
170 CHECK_ERROR_RETURN_RET_LOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
171 "SystemApi GetSupportedLightPaintings is called!");
172 MEDIA_INFO_LOG("GetLightPainting is called");
173 napi_status status;
174 auto result = CameraNapiUtils::GetUndefinedValue(env);
175
176 LightPaintingSessionNapi* lightPaintingSessionNapi = nullptr;
177 CameraNapiParamParser jsParamParser(env, info, lightPaintingSessionNapi);
178 if (lightPaintingSessionNapi->lightPaintingSession_ == nullptr) {
179 MEDIA_ERR_LOG("GetSupportedLightPaintings get native object fail");
180 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
181 return result;
182 }
183 LightPaintingType lightPainting;
184 int32_t retCode = lightPaintingSessionNapi->lightPaintingSession_->GetLightPainting(lightPainting);
185 CHECK_ERROR_PRINT_LOG(!CameraNapiUtils::CheckError(env, retCode), "GetLightPainting fail %{public}d", retCode);
186 status = napi_create_int32(env, lightPainting, &result);
187 CHECK_ERROR_PRINT_LOG(status != napi_ok, "Failed to get GetLightPainting!, errorCode : %{public}d", status);
188 return result;
189 }
190
SetLightPainting(napi_env env,napi_callback_info info)191 napi_value LightPaintingSessionNapi::SetLightPainting(napi_env env, napi_callback_info info)
192 {
193 CHECK_ERROR_RETURN_RET_LOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
194 "SystemApi GetSupportedLightPaintings is called!");
195 MEDIA_INFO_LOG("SetLightPainting is called");
196 auto result = CameraNapiUtils::GetUndefinedValue(env);
197
198 LightPaintingSessionNapi* lightPaintingSessionNapi = nullptr;
199 int32_t lightPaintingType;
200 CameraNapiParamParser jsParamParser(env, info, lightPaintingSessionNapi, lightPaintingType);
201 CHECK_ERROR_RETURN_RET_LOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
202 result, "SetLightPainting parse parameter occur error");
203 if (lightPaintingSessionNapi->lightPaintingSession_ == nullptr) {
204 MEDIA_ERR_LOG("SetLightPainting get native object fail");
205 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
206 return result;
207 }
208 lightPaintingSessionNapi->lightPaintingSession_->LockForControl();
209 MEDIA_INFO_LOG("SetLightPainting is called native");
210 int32_t retCode = lightPaintingSessionNapi->
211 lightPaintingSession_->SetLightPainting(static_cast<LightPaintingType>(lightPaintingType));
212 lightPaintingSessionNapi->lightPaintingSession_->UnlockForControl();
213 CHECK_ERROR_PRINT_LOG(!CameraNapiUtils::CheckError(env, retCode), "SetLightPainting fail %{public}d", retCode);
214 return result;
215 }
216
TriggerLighting(napi_env env,napi_callback_info info)217 napi_value LightPaintingSessionNapi::TriggerLighting(napi_env env, napi_callback_info info)
218 {
219 CHECK_ERROR_RETURN_RET_LOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
220 "SystemApi GetSupportedLightPaintings is called!");
221 MEDIA_INFO_LOG("TriggerLighting is called");
222 auto result = CameraNapiUtils::GetUndefinedValue(env);
223
224 LightPaintingSessionNapi* lightPaintingSessionNapi = nullptr;
225 CameraNapiParamParser jsParamParser(env, info, lightPaintingSessionNapi);
226 CHECK_ERROR_RETURN_RET_LOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
227 result, "TriggerLighting parse parameter occur error");
228 if (lightPaintingSessionNapi->lightPaintingSession_ == nullptr) {
229 MEDIA_ERR_LOG("TriggerLighting get native object fail");
230 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
231 return result;
232 }
233 lightPaintingSessionNapi->lightPaintingSession_->LockForControl();
234 int32_t retCode = lightPaintingSessionNapi->lightPaintingSession_->TriggerLighting();
235 lightPaintingSessionNapi->lightPaintingSession_->UnlockForControl();
236 CHECK_ERROR_PRINT_LOG(!CameraNapiUtils::CheckError(env, retCode), "TriggerLighting fail %{public}d", retCode);
237 return result;
238 }
239 } // namespace CameraStandard
240 } // namespace OHOS