• 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/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             CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
58         }
59     }
60     MEDIA_ERR_LOG("Init call Failed!");
61     return nullptr;
62 }
63 
CreateCameraSession(napi_env env)64 napi_value HighResPhotoSessionNapi::CreateCameraSession(napi_env env)
65 {
66     MEDIA_DEBUG_LOG("CreateCameraSession is called");
67     CAMERA_SYNC_TRACE;
68     napi_status status;
69     napi_value result = nullptr;
70     napi_value constructor;
71     status = napi_get_reference_value(env, sConstructor_, &constructor);
72     if (status == napi_ok) {
73         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::HIGH_RES_PHOTO);
74         if (sCameraSession_ == nullptr) {
75             MEDIA_ERR_LOG("Failed to create High res photo session instance");
76             napi_get_undefined(env, &result);
77             return result;
78         }
79         status = napi_new_instance(env, constructor, 0, nullptr, &result);
80         sCameraSession_ = nullptr;
81         if (status == napi_ok && result != nullptr) {
82             MEDIA_DEBUG_LOG("success to create High res photo session napi instance");
83             return result;
84         } else {
85             MEDIA_ERR_LOG("Failed to create High res photo session napi instance");
86         }
87     } else {
88         MEDIA_ERR_LOG("Failed to create napi reference value instance");
89     }
90     MEDIA_ERR_LOG("Failed to create High res photo session napi instance last");
91     napi_get_undefined(env, &result);
92     return result;
93 }
94 
HighResPhotoSessionNapiConstructor(napi_env env,napi_callback_info info)95 napi_value HighResPhotoSessionNapi::HighResPhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
96 {
97     MEDIA_DEBUG_LOG("HighResPhotoSessionNapiConstructor is called");
98     napi_status status;
99     napi_value result = nullptr;
100     napi_value thisVar = nullptr;
101 
102     napi_get_undefined(env, &result);
103     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
104 
105     if (status == napi_ok && thisVar != nullptr) {
106         std::unique_ptr<HighResPhotoSessionNapi> obj = std::make_unique<HighResPhotoSessionNapi>();
107         obj->env_ = env;
108         CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
109         obj->highResPhotoSession_ = static_cast<HighResPhotoSession*>(sCameraSession_.GetRefPtr());
110         obj->cameraSession_ = obj->highResPhotoSession_;
111         CHECK_ERROR_RETURN_RET_LOG(obj->highResPhotoSession_ == nullptr, result, "highResPhotoSession_ is null");
112         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
113             HighResPhotoSessionNapi::HighResPhotoSessionNapiDestructor, nullptr, nullptr);
114         if (status == napi_ok) {
115             obj.release();
116             return thisVar;
117         } else {
118             MEDIA_ERR_LOG("HighResPhotoSessionNapi Failure wrapping js to native napi");
119         }
120     }
121     MEDIA_ERR_LOG("HighResPhotoSessionNapi call Failed!");
122     return result;
123 }
124 } // namespace CameraStandard
125 } // namespace OHOS