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