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