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 "mode/macro_photo_session_napi.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21
22 thread_local napi_ref MacroPhotoSessionNapi::sConstructor_ = nullptr;
23
MacroPhotoSessionNapi()24 MacroPhotoSessionNapi::MacroPhotoSessionNapi() : env_(nullptr) {}
25
~MacroPhotoSessionNapi()26 MacroPhotoSessionNapi::~MacroPhotoSessionNapi()
27 {
28 MEDIA_DEBUG_LOG("~MacroPhotoSessionNapi is called");
29 }
30
MacroPhotoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)31 void MacroPhotoSessionNapi::MacroPhotoSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
32 {
33 MEDIA_DEBUG_LOG("MacroPhotoSessionNapiDestructor is called");
34 MacroPhotoSessionNapi* cameraObj = reinterpret_cast<MacroPhotoSessionNapi*>(nativeObject);
35 if (cameraObj != nullptr) {
36 delete cameraObj;
37 }
38 }
39
Init(napi_env env,napi_value exports)40 napi_value MacroPhotoSessionNapi::Init(napi_env env, napi_value exports)
41 {
42 MEDIA_DEBUG_LOG("Init is called");
43 napi_status status;
44 napi_value ctorObj;
45 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, flash_props,
46 auto_exposure_props, focus_props, zoom_props, depth_fusion_props,
47 color_effect_props, features_props, manual_focus_props, color_management_props };
48
49 std::vector<napi_property_descriptor> macro_photo_session_props =
50 CameraNapiUtils::GetPropertyDescriptor(descriptors);
51
52 status =
53 napi_define_class(env, MACRO_PHOTO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, MacroPhotoSessionNapiConstructor,
54 nullptr, macro_photo_session_props.size(), macro_photo_session_props.data(), &ctorObj);
55 if (status == napi_ok) {
56 int32_t refCount = 1;
57 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
58 if (status == napi_ok) {
59 status = napi_set_named_property(env, exports, MACRO_PHOTO_SESSION_NAPI_CLASS_NAME, ctorObj);
60 CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
61 }
62 }
63 MEDIA_ERR_LOG("Init call Failed!");
64 return nullptr;
65 }
66
CreateCameraSession(napi_env env)67 napi_value MacroPhotoSessionNapi::CreateCameraSession(napi_env env)
68 {
69 MEDIA_DEBUG_LOG("CreateCameraSession is called");
70 CAMERA_SYNC_TRACE;
71 napi_status status;
72 napi_value result = nullptr;
73 napi_value constructor;
74 status = napi_get_reference_value(env, sConstructor_, &constructor);
75 if (status == napi_ok) {
76 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::CAPTURE_MACRO);
77 if (sCameraSession_ == nullptr) {
78 MEDIA_ERR_LOG("Failed to create Photo session instance");
79 napi_get_undefined(env, &result);
80 return result;
81 }
82 status = napi_new_instance(env, constructor, 0, nullptr, &result);
83 sCameraSession_ = nullptr;
84 if (status == napi_ok && result != nullptr) {
85 MEDIA_DEBUG_LOG("success to create Photo session napi instance");
86 return result;
87 } else {
88 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
89 }
90 }
91 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
92 napi_get_undefined(env, &result);
93 return result;
94 }
95
MacroPhotoSessionNapiConstructor(napi_env env,napi_callback_info info)96 napi_value MacroPhotoSessionNapi::MacroPhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
97 {
98 MEDIA_DEBUG_LOG("MacroPhotoSessionNapiConstructor is called");
99 napi_status status;
100 napi_value result = nullptr;
101 napi_value thisVar = nullptr;
102
103 napi_get_undefined(env, &result);
104 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
105
106 if (status == napi_ok && thisVar != nullptr) {
107 std::unique_ptr<MacroPhotoSessionNapi> obj = std::make_unique<MacroPhotoSessionNapi>();
108 obj->env_ = env;
109 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
110 obj->macroPhotoSession_ = static_cast<MacroPhotoSession*>(sCameraSession_.GetRefPtr());
111 obj->cameraSession_ = obj->macroPhotoSession_;
112 CHECK_ERROR_RETURN_RET_LOG(obj->macroPhotoSession_ == nullptr, result, "macroPhotoSession_ is null");
113 status = napi_wrap(
114 env, thisVar, reinterpret_cast<void*>(obj.get()), MacroPhotoSessionNapiDestructor, nullptr, nullptr);
115 if (status == napi_ok) {
116 obj.release();
117 return thisVar;
118 } else {
119 MEDIA_ERR_LOG("MacroPhotoSessionNapiConstructor Failure wrapping js to native napi");
120 }
121 }
122 MEDIA_ERR_LOG("MacroPhotoSessionNapiConstructor call Failed!");
123 return result;
124 }
125 } // namespace CameraStandard
126 } // namespace OHOS
127