1 /*
2 * Copyright (c) 2021-2022 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 "bridge/declarative_frontend/jsview/canvas/js_canvas_path.h"
17
18 #include "bridge/declarative_frontend/jsview/canvas/js_rendering_context.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20
21 namespace OHOS::Ace::Framework {
22 constexpr size_t GCTHRESHOLD = 50;
23
JSCanvasPath()24 JSCanvasPath::JSCanvasPath()
25 {
26 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) {
27 isJudgeSpecialValue_ = true;
28 }
29 }
30
JsPath2DSetTransform(const JSCallbackInfo & info)31 void JSCanvasPath::JsPath2DSetTransform(const JSCallbackInfo& info)
32 {
33 double scaleX = 0.0;
34 double skewX = 0.0;
35 double skewY = 0.0;
36 double scaleY = 0.0;
37 double translateX = 0.0;
38 double translateY = 0.0;
39 if (info.GetDoubleArg(0, scaleX, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
40 info.GetDoubleArg(1, skewX, isJudgeSpecialValue_) && // Index1: the 2nd arg.
41 info.GetDoubleArg(2, skewY, isJudgeSpecialValue_) && // Index2: the 3rd arg.
42 info.GetDoubleArg(3, scaleY, isJudgeSpecialValue_) && // Index3: the 4th arg.
43 info.GetDoubleArg(4, translateX, isJudgeSpecialValue_) && // Index4: the 5th arg.
44 info.GetDoubleArg(5, translateY, isJudgeSpecialValue_)) { // Index5: the 6th arg.
45 double density = GetDensity();
46 path2d_->SetTransform(scaleX, skewX, skewY, scaleY, translateX * density, translateY * density);
47 SetPathSize(info);
48 }
49 }
50
51 // moveTo(x: number, y: number): void
JsPath2DMoveTo(const JSCallbackInfo & info)52 void JSCanvasPath::JsPath2DMoveTo(const JSCallbackInfo& info)
53 {
54 double x = 0.0;
55 double y = 0.0;
56 if (info.GetDoubleArg(0, x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
57 info.GetDoubleArg(1, y, isJudgeSpecialValue_)) { // Index1: the 2nd arg.
58 double density = GetDensity();
59 path2d_->MoveTo(x * density, y * density);
60 SetPathSize(info);
61 }
62 }
63
64 // lineTo(x: number, y: number): void
JsPath2DLineTo(const JSCallbackInfo & info)65 void JSCanvasPath::JsPath2DLineTo(const JSCallbackInfo& info)
66 {
67 double x = 0.0;
68 double y = 0.0;
69 if (info.GetDoubleArg(0, x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
70 info.GetDoubleArg(1, y, isJudgeSpecialValue_)) { // Index1: the 2nd arg.
71 double density = GetDensity();
72 path2d_->LineTo(x * density, y * density);
73 SetPathSize(info);
74 }
75 }
76
77 // arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void
JsPath2DArc(const JSCallbackInfo & info)78 void JSCanvasPath::JsPath2DArc(const JSCallbackInfo& info)
79 {
80 double x = 0.0;
81 double y = 0.0;
82 double radius = 0.0;
83 double startAngle = 0.0;
84 double endAngle = 0.0;
85 if (info.GetDoubleArg(0, x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
86 info.GetDoubleArg(1, y, isJudgeSpecialValue_) && // Index1: the 2nd arg.
87 info.GetDoubleArg(2, radius, isJudgeSpecialValue_) && // Index2: the 3rd arg.
88 info.GetDoubleArg(3, startAngle, isJudgeSpecialValue_) && // Index3: the 4th arg.
89 info.GetDoubleArg(4, endAngle, isJudgeSpecialValue_)) { // Index4: the 5th arg.
90 bool anticlockwise = false;
91 info.GetBooleanArg(5, anticlockwise);
92 double density = GetDensity();
93 path2d_->Arc(x * density, y * density, radius * density, startAngle, endAngle, anticlockwise);
94 SetPathSize(info);
95 }
96 }
97
98 // arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void
JsPath2DArcTo(const JSCallbackInfo & info)99 void JSCanvasPath::JsPath2DArcTo(const JSCallbackInfo& info)
100 {
101 double x1 = 0.0;
102 double y1 = 0.0;
103 double x2 = 0.0;
104 double y2 = 0.0;
105 double radius = 0.0;
106 if (info.GetDoubleArg(0, x1, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
107 info.GetDoubleArg(1, y1, isJudgeSpecialValue_) && // Index1: the 2nd arg.
108 info.GetDoubleArg(2, x2, isJudgeSpecialValue_) && // Index2: the 3rd arg.
109 info.GetDoubleArg(3, y2, isJudgeSpecialValue_) && // Index3: the 4th arg.
110 info.GetDoubleArg(4, radius, isJudgeSpecialValue_)) { // Index4: the 5th arg.
111 double density = GetDensity();
112 path2d_->ArcTo(x1 * density, y1 * density, x2 * density, y2 * density, radius * density);
113 SetPathSize(info);
114 }
115 }
116
117 // quadraticCurveTo(cpx: number, cpy: number, x: number ,y: number): void
JsPath2DQuadraticCurveTo(const JSCallbackInfo & info)118 void JSCanvasPath::JsPath2DQuadraticCurveTo(const JSCallbackInfo& info)
119 {
120 double cpx = 0.0;
121 double cpy = 0.0;
122 double x = 0.0;
123 double y = 0.0;
124 if (info.GetDoubleArg(0, cpx, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
125 info.GetDoubleArg(1, cpy, isJudgeSpecialValue_) && // Index1: the 2nd arg.
126 info.GetDoubleArg(2, x, isJudgeSpecialValue_) && // Index2: the 3rd arg.
127 info.GetDoubleArg(3, y, isJudgeSpecialValue_)) { // Index3: the 4th arg.
128 double density = GetDensity();
129 path2d_->QuadraticCurveTo(cpx * density, cpy * density, x * density, y * density);
130 SetPathSize(info);
131 }
132 }
133
134 // bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void
JsPath2DBezierCurveTo(const JSCallbackInfo & info)135 void JSCanvasPath::JsPath2DBezierCurveTo(const JSCallbackInfo& info)
136 {
137 double cp1x = 0.0;
138 double cp1y = 0.0;
139 double cp2x = 0.0;
140 double cp2y = 0.0;
141 double x = 0.0;
142 double y = 0.0;
143 if (info.GetDoubleArg(0, cp1x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
144 info.GetDoubleArg(1, cp1y, isJudgeSpecialValue_) && // Index1: the 2nd arg.
145 info.GetDoubleArg(2, cp2x, isJudgeSpecialValue_) && // Index2: the 3rd arg.
146 info.GetDoubleArg(3, cp2y, isJudgeSpecialValue_) && // Index3: the 4th arg.
147 info.GetDoubleArg(4, x, isJudgeSpecialValue_) && // Index4: the 5th arg.
148 info.GetDoubleArg(5, y, isJudgeSpecialValue_)) { // Index5: the 6th arg.
149 double density = GetDensity();
150 path2d_->BezierCurveTo(
151 cp1x * density, cp1y * density, cp2x * density, cp2y * density, x * density, y * density);
152 SetPathSize(info);
153 }
154 }
155
156 // ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number,
157 // endAngle: number, counterclockwise?: boolean): void
JsPath2DEllipse(const JSCallbackInfo & info)158 void JSCanvasPath::JsPath2DEllipse(const JSCallbackInfo& info)
159 {
160 double x = 0.0;
161 double y = 0.0;
162 double radiusX = 0.0;
163 double radiusY = 0.0;
164 double rotation = 0.0;
165 double startAngle = 0.0;
166 double endAngle = 0.0;
167 if (info.GetDoubleArg(0, x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
168 info.GetDoubleArg(1, y, isJudgeSpecialValue_) && // Index1: the 2nd arg.
169 info.GetDoubleArg(2, radiusX, isJudgeSpecialValue_) && // Index2: the 3rd arg.
170 info.GetDoubleArg(3, radiusY, isJudgeSpecialValue_) && // Index3: the 4th arg.
171 info.GetDoubleArg(4, rotation, isJudgeSpecialValue_) && // Index4: the 5th arg.
172 info.GetDoubleArg(5, startAngle, isJudgeSpecialValue_) && // Index5: the 6th arg.
173 info.GetDoubleArg(6, endAngle, isJudgeSpecialValue_)) { // Index6: the 7th arg.
174 bool anticlockwise = false;
175 info.GetBooleanArg(7, anticlockwise);
176 double density = GetDensity();
177 path2d_->Ellipse(x * density, y * density, radiusX * density, radiusY * density, rotation,
178 startAngle, endAngle, anticlockwise);
179 SetPathSize(info);
180 }
181 }
182
183 // rect(x: number, y: number, w: number, h: number): void
JsPath2DRect(const JSCallbackInfo & info)184 void JSCanvasPath::JsPath2DRect(const JSCallbackInfo& info)
185 {
186 double x = 0.0;
187 double y = 0.0;
188 double width = 0.0;
189 double height = 0.0;
190 if (info.GetDoubleArg(0, x, isJudgeSpecialValue_) && // Indexd0: the 1st arg.
191 info.GetDoubleArg(1, y, isJudgeSpecialValue_) && // Index1: the 2nd arg.
192 info.GetDoubleArg(2, width, isJudgeSpecialValue_) && // Index2: the 3rd arg.
193 info.GetDoubleArg(3, height, isJudgeSpecialValue_)) { // Index3: the 4th arg.
194 double density = GetDensity();
195 path2d_->Rect(x * density, y * density, width * density, height * density);
196 SetPathSize(info);
197 }
198 }
199
200 // closePath(): void
JsPath2DClosePath(const JSCallbackInfo & info)201 void JSCanvasPath::JsPath2DClosePath(const JSCallbackInfo& info)
202 {
203 path2d_->ClosePath();
204 SetPathSize(info);
205 }
206
SetPathSize(const JSCallbackInfo & info)207 void JSCanvasPath::SetPathSize(const JSCallbackInfo& info)
208 {
209 CHECK_NULL_VOID(path2d_);
210 const std::vector<std::pair<PathCmd, PathArgs>> caches = path2d_->GetCaches();
211 size_t pathSize = caches.size();
212 if (pathSize - lastPathSize_ > GCTHRESHOLD) {
213 EcmaVM* vm = info.GetVm();
214 CHECK_NULL_VOID(vm);
215 panda::CopyableGlobal<ObjectRef> pathCmdObj = panda::CopyableGlobal<ObjectRef>(pathCmdObj_);
216 if (!pathCmdObj.IsEmpty()) {
217 pathCmdObj->SetNativePointerField(
218 vm, 0, nullptr, &JSCanvasPath::DestructorInterceptor, nullptr,
219 (pathSize - lastPathSize_) * sizeof(std::pair<PathCmd, PathArgs>));
220 lastPathSize_ = pathSize;
221 }
222 }
223 }
224 } // namespace OHOS::Ace::Framework
225