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