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_region.h"
17 #include "draw/path.h"
18 #include "utils/region.h"
19 #include "path_napi/js_path.h"
20 #include "js_common.h"
21 #include "native_value.h"
22 #include "js_drawing_utils.h"
23 
24 namespace OHOS::Rosen {
25 namespace Drawing {
26 thread_local napi_ref JsRegion::constructor_ = nullptr;
27 const std::string CLASS_NAME = "Region";
Init(napi_env env,napi_value exportObj)28 napi_value JsRegion::Init(napi_env env, napi_value exportObj)
29 {
30     napi_property_descriptor properties[] = {
31         DECLARE_NAPI_FUNCTION("isPointContained", JsRegion::IsPointContained),
32         DECLARE_NAPI_FUNCTION("isRegionContained", JsRegion::IsRegionContained),
33         DECLARE_NAPI_FUNCTION("op", JsRegion::Op),
34         DECLARE_NAPI_FUNCTION("quickReject", JsRegion::QuickReject),
35         DECLARE_NAPI_FUNCTION("setRect", JsRegion::SetRect),
36         DECLARE_NAPI_FUNCTION("setPath", JsRegion::SetPath),
37     };
38 
39     napi_value constructor = nullptr;
40     napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
41                                            sizeof(properties) / sizeof(properties[0]), properties, &constructor);
42     if (status != napi_ok) {
43         ROSEN_LOGE("JsRegion::Init Failed to define Region class");
44         return nullptr;
45     }
46 
47     status = napi_create_reference(env, constructor, 1, &constructor_);
48     if (status != napi_ok) {
49         ROSEN_LOGE("JsRegion::Init Failed to create reference of constructor");
50         return nullptr;
51     }
52 
53     status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
54     if (status != napi_ok) {
55         ROSEN_LOGE("JsRegion::Init Failed to set constructor");
56         return nullptr;
57     }
58 
59     return exportObj;
60 }
61 
Constructor(napi_env env,napi_callback_info info)62 napi_value JsRegion::Constructor(napi_env env, napi_callback_info info)
63 {
64     size_t argCount = 0;
65     napi_value jsThis = nullptr;
66     napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
67     if (status != napi_ok) {
68         ROSEN_LOGE("JsRegion::Constructor failed to napi_get_cb_info");
69         return nullptr;
70     }
71     auto region = std::make_shared<Region>();
72     JsRegion* jsRegion = new JsRegion(region);
73     status = napi_wrap(env, jsThis, jsRegion,
74                        JsRegion::Destructor, nullptr, nullptr);
75     if (status != napi_ok) {
76         delete jsRegion;
77         ROSEN_LOGE("JsRegion::Constructor Failed to wrap native instance");
78         return nullptr;
79     }
80 
81     return jsThis;
82 }
83 
Destructor(napi_env env,void * nativeObject,void * finalize)84 void JsRegion::Destructor(napi_env env, void* nativeObject, void* finalize)
85 {
86     (void)finalize;
87     if (nativeObject != nullptr) {
88         JsRegion* napi = reinterpret_cast<JsRegion*>(nativeObject);
89         delete napi;
90     }
91 }
92 
IsPointContained(napi_env env,napi_callback_info info)93 napi_value JsRegion::IsPointContained(napi_env env, napi_callback_info info)
94 {
95     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
96     return (me != nullptr) ? me->OnIsPointContained(env, info) : nullptr;
97 }
98 
OnIsPointContained(napi_env env,napi_callback_info info)99 napi_value JsRegion::OnIsPointContained(napi_env env, napi_callback_info info)
100 {
101     if (m_region == nullptr) {
102         ROSEN_LOGE("JsRegion::OnIsPointContained region is nullptr");
103         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
104     }
105 
106     napi_value argv[ARGC_TWO] = {nullptr};
107     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
108 
109     int32_t x = 0;
110     GET_INT32_PARAM(ARGC_ZERO, x);
111     int32_t y = 0;
112     GET_INT32_PARAM(ARGC_ONE, y);
113 
114     return CreateJsValue(env, m_region->Contains(x, y));
115 }
116 
IsRegionContained(napi_env env,napi_callback_info info)117 napi_value JsRegion::IsRegionContained(napi_env env, napi_callback_info info)
118 {
119     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
120     return (me != nullptr) ? me->OnIsRegionContained(env, info) : nullptr;
121 }
122 
OnIsRegionContained(napi_env env,napi_callback_info info)123 napi_value JsRegion::OnIsRegionContained(napi_env env, napi_callback_info info)
124 {
125     if (m_region == nullptr) {
126         ROSEN_LOGE("JsRegion::OnIsRegionContained region is nullptr");
127         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
128     }
129 
130     napi_value argv[ARGC_ONE] = {nullptr};
131     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
132 
133     JsRegion* other = nullptr;
134     GET_UNWRAP_PARAM(ARGC_ZERO, other);
135     if (other->GetRegion() == nullptr) {
136         ROSEN_LOGE("JsRegion::OnIsRegionContained other is nullptr");
137         return nullptr;
138     }
139 
140     return CreateJsValue(env, m_region->IsRegionContained(*other->GetRegion()));
141 }
142 
Op(napi_env env,napi_callback_info info)143 napi_value JsRegion::Op(napi_env env, napi_callback_info info)
144 {
145     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
146     return (me != nullptr) ? me->OnOp(env, info) : nullptr;
147 }
148 
OnOp(napi_env env,napi_callback_info info)149 napi_value JsRegion::OnOp(napi_env env, napi_callback_info info)
150 {
151     if (m_region == nullptr) {
152         ROSEN_LOGE("JsRegion::OnOp region is nullptr");
153         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
154     }
155 
156     napi_value argv[ARGC_TWO] = {nullptr};
157     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
158 
159     JsRegion* region = nullptr;
160     GET_UNWRAP_PARAM(ARGC_ZERO, region);
161     if (region->GetRegion() == nullptr) {
162         ROSEN_LOGE("JsRegion::OnOp region is nullptr");
163         return nullptr;
164     }
165 
166     int32_t regionOp = 0;
167     GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ONE, regionOp);
168 
169     RegionOp op = static_cast<RegionOp>(regionOp);
170     if (op < RegionOp::DIFFERENCE || op > RegionOp::REPLACE) {
171         ROSEN_LOGE("JsRegion::OnOp regionOp is error param");
172         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
173     }
174 
175     return CreateJsValue(env, m_region->Op(*region->GetRegion(), op));
176 }
177 
QuickReject(napi_env env,napi_callback_info info)178 napi_value JsRegion::QuickReject(napi_env env, napi_callback_info info)
179 {
180     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
181     return (me != nullptr) ? me->OnQuickReject(env, info) : nullptr;
182 }
183 
OnQuickReject(napi_env env,napi_callback_info info)184 napi_value JsRegion::OnQuickReject(napi_env env, napi_callback_info info)
185 {
186     if (m_region == nullptr) {
187         ROSEN_LOGE("JsRegion::OnQuickReject region is nullptr");
188         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
189     }
190 
191     napi_value argv[ARGC_FOUR] = {nullptr};
192     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
193 
194     int32_t left = 0;
195     GET_INT32_PARAM(ARGC_ZERO, left);
196     int32_t top = 0;
197     GET_INT32_PARAM(ARGC_ONE, top);
198     int32_t right = 0;
199     GET_INT32_PARAM(ARGC_TWO, right);
200     int32_t bottom = 0;
201     GET_INT32_PARAM(ARGC_THREE, bottom);
202     Drawing::RectI rectI = Drawing::RectI(left, top, right, bottom);
203 
204     return CreateJsValue(env, m_region->QuickReject(rectI));
205 }
206 
SetRect(napi_env env,napi_callback_info info)207 napi_value JsRegion::SetRect(napi_env env, napi_callback_info info)
208 {
209     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
210     return (me != nullptr) ? me->OnSetRect(env, info) : nullptr;
211 }
212 
OnSetRect(napi_env env,napi_callback_info info)213 napi_value JsRegion::OnSetRect(napi_env env, napi_callback_info info)
214 {
215     if (m_region == nullptr) {
216         ROSEN_LOGE("JsRegion::OnSetRect region is nullptr");
217         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
218     }
219 
220     napi_value argv[ARGC_FOUR] = {nullptr};
221     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
222 
223     int32_t left = 0;
224     GET_INT32_PARAM(ARGC_ZERO, left);
225     int32_t top = 0;
226     GET_INT32_PARAM(ARGC_ONE, top);
227     int32_t right = 0;
228     GET_INT32_PARAM(ARGC_TWO, right);
229     int32_t bottom = 0;
230     GET_INT32_PARAM(ARGC_THREE, bottom);
231     Drawing::RectI rectI = Drawing::RectI(left, top, right, bottom);
232 
233     return CreateJsValue(env, m_region->SetRect(rectI));
234 }
235 
SetPath(napi_env env,napi_callback_info info)236 napi_value JsRegion::SetPath(napi_env env, napi_callback_info info)
237 {
238     JsRegion* me = CheckParamsAndGetThis<JsRegion>(env, info);
239     return (me != nullptr) ? me->OnSetPath(env, info) : nullptr;
240 }
241 
OnSetPath(napi_env env,napi_callback_info info)242 napi_value JsRegion::OnSetPath(napi_env env, napi_callback_info info)
243 {
244     if (m_region == nullptr) {
245         ROSEN_LOGE("JsRegion::OnSetPath region is nullptr");
246         return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
247     }
248 
249     napi_value argv[ARGC_TWO] = {nullptr};
250     CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
251 
252     JsPath* jsPath = nullptr;
253     GET_UNWRAP_PARAM(ARGC_ZERO, jsPath);
254     if (jsPath->GetPath() == nullptr) {
255         ROSEN_LOGE("JsRegion::OnSetPath jsPath is nullptr");
256         return nullptr;
257     }
258     JsRegion* jsClip = nullptr;
259     GET_UNWRAP_PARAM(ARGC_ONE, jsClip);
260     if (jsClip->GetRegion() == nullptr) {
261         ROSEN_LOGE("JsRegion::OnSetPath jsClip is nullptr");
262         return nullptr;
263     }
264 
265     return CreateJsValue(env, m_region->SetPath(*jsPath->GetPath(), *jsClip->GetRegion()));
266 }
267 
GetRegion()268 Region* JsRegion::GetRegion()
269 {
270     return m_region.get();
271 }
272 } // namespace Drawing
273 } // namespace OHOS::Rosen
274