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 if (wrapper_ != nullptr) {
31 napi_delete_reference(env_, wrapper_);
32 }
33 if (panoramaSession_) {
34 panoramaSession_ = nullptr;
35 }
36 }
37
PanoramaSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)38 void PanoramaSessionNapi::PanoramaSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
39 {
40 MEDIA_DEBUG_LOG("PanoramaSessionNapiDestructor is called");
41 PanoramaSessionNapi* cameraObj = reinterpret_cast<PanoramaSessionNapi*>(nativeObject);
42 if (cameraObj != nullptr) {
43 delete cameraObj;
44 }
45 }
46
Init(napi_env env,napi_value exports)47 napi_value PanoramaSessionNapi::Init(napi_env env, napi_value exports)
48 {
49 MEDIA_DEBUG_LOG("Init is called");
50 napi_status status;
51 napi_value ctorObj;
52
53 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, focus_props,
54 auto_exposure_props, color_effect_props, auto_wb_props, manual_wb_props };
55
56 std::vector<napi_property_descriptor> panorama_session_props =
57 CameraNapiUtils::GetPropertyDescriptor(descriptors);
58
59 status =
60 napi_define_class(env, PANORAMA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, PanoramaSessionNapiConstructor,
61 nullptr, panorama_session_props.size(), panorama_session_props.data(), &ctorObj);
62 if (status == napi_ok) {
63 int32_t refCount = 1;
64 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
65 if (status == napi_ok) {
66 status = napi_set_named_property(env, exports, PANORAMA_SESSION_NAPI_CLASS_NAME, ctorObj);
67 if (status == napi_ok) {
68 return exports;
69 }
70 }
71 }
72 MEDIA_ERR_LOG("Init call Failed!");
73 return nullptr;
74 }
75
CreateCameraSession(napi_env env)76 napi_value PanoramaSessionNapi::CreateCameraSession(napi_env env)
77 {
78 MEDIA_DEBUG_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::PANORAMA_PHOTO);
86 if (sCameraSession_ == nullptr) {
87 MEDIA_ERR_LOG("Failed to create Photo 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 Photo session napi instance");
95 return result;
96 } else {
97 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
98 }
99 }
100 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
101 napi_get_undefined(env, &result);
102 return result;
103 }
104
PanoramaSessionNapiConstructor(napi_env env,napi_callback_info info)105 napi_value PanoramaSessionNapi::PanoramaSessionNapiConstructor(napi_env env, napi_callback_info info)
106 {
107 MEDIA_DEBUG_LOG("PanoramaSessionNapiConstructor is called");
108 napi_status status;
109 napi_value result = nullptr;
110 napi_value thisVar = nullptr;
111
112 napi_get_undefined(env, &result);
113 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
114
115 if (status == napi_ok && thisVar != nullptr) {
116 std::unique_ptr<PanoramaSessionNapi> obj = std::make_unique<PanoramaSessionNapi>();
117 obj->env_ = env;
118 if (sCameraSession_ == nullptr) {
119 MEDIA_ERR_LOG("sCameraSession_ is null");
120 return result;
121 }
122 obj->panoramaSession_ = static_cast<PanoramaSession*>(sCameraSession_.GetRefPtr());
123 obj->cameraSession_ = obj->panoramaSession_;
124 if (obj->panoramaSession_ == nullptr) {
125 MEDIA_ERR_LOG("panoramaSession_ is null");
126 return result;
127 }
128 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
129 PanoramaSessionNapi::PanoramaSessionNapiDestructor, nullptr, nullptr);
130 if (status == napi_ok) {
131 obj.release();
132 return thisVar;
133 } else {
134 MEDIA_ERR_LOG("PanoramaSessionNapi Failure wrapping js to native napi");
135 }
136 }
137 MEDIA_ERR_LOG("PanoramaSessionNapi call Failed!");
138 return result;
139 }
140
141 } // CameraStandard
142 } // OHOS
143