• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "js_sampling_options.h"
17 #include "js_drawing_utils.h"
18 #include "native_value.h"
19 
20 namespace OHOS::Rosen {
21 namespace Drawing {
22 thread_local napi_ref JsSamplingOptions::constructor_ = nullptr;
23 const std::string CLASS_NAME = "SamplingOptions";
Init(napi_env env,napi_value exportObj)24 napi_value JsSamplingOptions::Init(napi_env env, napi_value exportObj)
25 {
26     napi_property_descriptor properties[] = {
27     };
28 
29     napi_value constructor = nullptr;
30     napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
31                                            sizeof(properties) / sizeof(properties[0]), properties, &constructor);
32     if (status != napi_ok) {
33         ROSEN_LOGE("JsSamplingOptions::Init failed to define SamplingOptions class");
34         return nullptr;
35     }
36 
37     status = napi_create_reference(env, constructor, 1, &constructor_);
38     if (status != napi_ok) {
39         ROSEN_LOGE("JsSamplingOptions::Init failed to create reference of constructor");
40         return nullptr;
41     }
42 
43     status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
44     if (status != napi_ok) {
45         ROSEN_LOGE("JsSamplingOptions::Init failed to set constructor");
46         return nullptr;
47     }
48 
49     return exportObj;
50 }
51 
Constructor(napi_env env,napi_callback_info info)52 napi_value JsSamplingOptions::Constructor(napi_env env, napi_callback_info info)
53 {
54     size_t argCount = ARGC_ONE;
55     napi_value argv[ARGC_ONE] = {nullptr};
56     napi_value jsThis = nullptr;
57     napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
58     if (status != napi_ok) {
59         ROSEN_LOGE("JsSamplingOptions::Constructor failed to napi_get_cb_info");
60         return nullptr;
61     }
62     JsSamplingOptions* jsSamplingOptions = nullptr;
63     std::shared_ptr<SamplingOptions> samplingOptions = nullptr;
64     if (argCount == ARGC_ZERO) {
65         samplingOptions = std::make_shared<SamplingOptions>();
66         jsSamplingOptions = new JsSamplingOptions(samplingOptions);
67     } else {
68         int32_t jsFilterMode = 0;
69         GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ZERO, jsFilterMode);
70         samplingOptions = std::make_shared<SamplingOptions>(FilterMode(jsFilterMode));
71         jsSamplingOptions = new JsSamplingOptions(samplingOptions);
72     }
73 
74     status = napi_wrap(env, jsThis, jsSamplingOptions,
75                        JsSamplingOptions::Destructor, nullptr, nullptr);
76     if (status != napi_ok) {
77         delete jsSamplingOptions;
78         ROSEN_LOGE("JsSamplingOptions::Constructor failed to wrap native instance");
79         return nullptr;
80     }
81 
82     return jsThis;
83 }
84 
Destructor(napi_env env,void * nativeObject,void * finalize)85 void JsSamplingOptions::Destructor(napi_env env, void* nativeObject, void* finalize)
86 {
87     (void)finalize;
88     if (nativeObject != nullptr) {
89         JsSamplingOptions* napi = reinterpret_cast<JsSamplingOptions*>(nativeObject);
90         delete napi;
91     }
92 }
93 
~JsSamplingOptions()94 JsSamplingOptions::~JsSamplingOptions()
95 {
96     m_samplingOptions = nullptr;
97 }
98 
GetSamplingOptions()99 std::shared_ptr<SamplingOptions> JsSamplingOptions::GetSamplingOptions()
100 {
101     return m_samplingOptions;
102 }
103 
104 } // namespace Drawing
105 } // namespace OHOS::Rosen
106