1 /*
2 * Copyright (c) 2023 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 "recording/recording_path.h"
17
18 #include "recording/cmd_list_helper.h"
19 #include "utils/log.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
RecordingPath()24 RecordingPath::RecordingPath() noexcept : cmdList_(std::make_shared<PathCmdList>()) {}
25
GetCmdList() const26 std::shared_ptr<PathCmdList> RecordingPath::GetCmdList() const
27 {
28 return cmdList_;
29 }
30
BuildFromSVGString(const std::string & str)31 bool RecordingPath::BuildFromSVGString(const std::string& str)
32 {
33 uint32_t offset = 0;
34 size_t length = str.length();
35 if (str.length() != 0) {
36 const void* data = static_cast<const void*>(str.data());
37 size_t size = length + (8 - length % 8);
38 offset = cmdList_->AddCmdListData({ data, size });
39 }
40
41 cmdList_->AddOp<BuildFromSVGOpItem>(offset, length);
42 return true;
43 }
44
MoveTo(scalar x,scalar y)45 void RecordingPath::MoveTo(scalar x, scalar y)
46 {
47 cmdList_->AddOp<MoveToOpItem>(x, y);
48 }
49
LineTo(scalar x,scalar y)50 void RecordingPath::LineTo(scalar x, scalar y)
51 {
52 cmdList_->AddOp<LineToOpItem>(x, y);
53 }
54
ArcTo(scalar pt1X,scalar pt1Y,scalar pt2X,scalar pt2Y,scalar startAngle,scalar sweepAngle)55 void RecordingPath::ArcTo(scalar pt1X, scalar pt1Y, scalar pt2X, scalar pt2Y, scalar startAngle, scalar sweepAngle)
56 {
57 cmdList_->AddOp<ArcToOpItem>(Point(pt1X, pt1Y), Point(pt2X, pt2Y), startAngle, sweepAngle);
58 }
59
ArcTo(const Point & pt1,const Point & pt2,scalar startAngle,scalar sweepAngle)60 void RecordingPath::ArcTo(const Point& pt1, const Point& pt2, scalar startAngle, scalar sweepAngle)
61 {
62 cmdList_->AddOp<ArcToOpItem>(pt1, pt2, startAngle, sweepAngle);
63 }
64
ArcTo(scalar rx,scalar ry,scalar angle,PathDirection direction,scalar endX,scalar endY)65 void RecordingPath::ArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar endX, scalar endY)
66 {
67 cmdList_->AddOp<ArcToOpItem>(rx, ry, angle, direction, endX, endY);
68 }
69
CubicTo(scalar ctrlPt1X,scalar ctrlPt1Y,scalar ctrlPt2X,scalar ctrlPt2Y,scalar endPtX,scalar endPtY)70 void RecordingPath::CubicTo(scalar ctrlPt1X, scalar ctrlPt1Y, scalar ctrlPt2X, scalar ctrlPt2Y,
71 scalar endPtX, scalar endPtY)
72 {
73 cmdList_->AddOp<CubicToOpItem>(Point(ctrlPt1X, ctrlPt1Y), Point(ctrlPt2X, ctrlPt2Y), Point(endPtX, endPtY));
74 }
75
CubicTo(const Point & ctrlPt1,const Point & ctrlPt2,const Point & endPt)76 void RecordingPath::CubicTo(const Point& ctrlPt1, const Point& ctrlPt2, const Point& endPt)
77 {
78 cmdList_->AddOp<CubicToOpItem>(ctrlPt1, ctrlPt2, endPt);
79 }
80
QuadTo(scalar ctrlPtX,scalar ctrlPtY,scalar endPtX,scalar endPtY)81 void RecordingPath::QuadTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY)
82 {
83 cmdList_->AddOp<QuadToOpItem>(Point(ctrlPtX, ctrlPtY), Point(endPtX, endPtY));
84 }
85
QuadTo(const Point & ctrlPt,const Point endPt)86 void RecordingPath::QuadTo(const Point& ctrlPt, const Point endPt)
87 {
88 cmdList_->AddOp<QuadToOpItem>(ctrlPt, endPt);
89 }
90
AddRect(const Rect & rect,PathDirection dir)91 void RecordingPath::AddRect(const Rect& rect, PathDirection dir)
92 {
93 cmdList_->AddOp<AddRectOpItem>(rect, dir);
94 }
95
AddRect(scalar left,scalar top,scalar right,scalar bottom,PathDirection dir)96 void RecordingPath::AddRect(scalar left, scalar top, scalar right, scalar bottom, PathDirection dir)
97 {
98 cmdList_->AddOp<AddRectOpItem>(Rect(left, top, right, bottom), dir);
99 }
100
AddOval(const Rect & oval,PathDirection dir)101 void RecordingPath::AddOval(const Rect& oval, PathDirection dir)
102 {
103 cmdList_->AddOp<AddOvalOpItem>(oval, dir);
104 }
105
AddArc(const Rect & oval,scalar startAngle,scalar sweepAngle)106 void RecordingPath::AddArc(const Rect& oval, scalar startAngle, scalar sweepAngle)
107 {
108 cmdList_->AddOp<AddArcOpItem>(oval, startAngle, sweepAngle);
109 }
110
AddPoly(const std::vector<Point> & points,int count,bool close)111 void RecordingPath::AddPoly(const std::vector<Point>& points, int count, bool close)
112 {
113 auto pointsInfo = CmdListHelper::AddVectorToCmdList<Point>(*cmdList_, points);
114 cmdList_->AddOp<AddPolyOpItem>(pointsInfo, count, close);
115 }
116
AddCircle(scalar x,scalar y,scalar radius,PathDirection dir)117 void RecordingPath::AddCircle(scalar x, scalar y, scalar radius, PathDirection dir)
118 {
119 cmdList_->AddOp<AddCircleOpItem>(x, y, radius, dir);
120 }
121
AddRoundRect(const Rect & rect,scalar xRadius,scalar yRadius,PathDirection dir)122 void RecordingPath::AddRoundRect(const Rect& rect, scalar xRadius, scalar yRadius, PathDirection dir)
123 {
124 RoundRect roundRect(rect, xRadius, yRadius);
125 AddRoundRect(roundRect, dir);
126 }
127
AddRoundRect(const RoundRect & rrect,PathDirection dir)128 void RecordingPath::AddRoundRect(const RoundRect& rrect, PathDirection dir)
129 {
130 std::vector<Point> radiusXY;
131 radiusXY.push_back(rrect.GetCornerRadius(RoundRect::CornerPos::TOP_LEFT_POS));
132 radiusXY.push_back(rrect.GetCornerRadius(RoundRect::CornerPos::TOP_RIGHT_POS));
133 radiusXY.push_back(rrect.GetCornerRadius(RoundRect::CornerPos::BOTTOM_RIGHT_POS));
134 radiusXY.push_back(rrect.GetCornerRadius(RoundRect::CornerPos::BOTTOM_LEFT_POS));
135 auto radiusXYData = CmdListHelper::AddVectorToCmdList<Point>(*cmdList_, radiusXY);
136
137 cmdList_->AddOp<AddRoundRectOpItem>(radiusXYData, rrect.GetRect(), dir);
138 }
139
AddPath(const Path & src,scalar dx,scalar dy)140 void RecordingPath::AddPath(const Path& src, scalar dx, scalar dy)
141 {
142 auto pathHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, src);
143 cmdList_->AddOp<AddPathOpItem>(pathHandle, dx, dy);
144 }
145
AddPath(const Path & src)146 void RecordingPath::AddPath(const Path& src)
147 {
148 auto pathHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, src);
149 cmdList_->AddOp<AddPathOpItem>(pathHandle);
150 }
151
AddPath(const Path & src,const Matrix & matrix)152 void RecordingPath::AddPath(const Path& src, const Matrix& matrix)
153 {
154 auto pathHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, src);
155 cmdList_->AddOp<AddPathWithMatrixOpItem>(pathHandle, matrix);
156 }
157
ReverseAddPath(const Path & src)158 void RecordingPath::ReverseAddPath(const Path& src)
159 {
160 auto pathHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, src);
161 cmdList_->AddOp<ReverseAddPathOpItem>(pathHandle);
162 }
163
SetFillStyle(PathFillType fillstyle)164 void RecordingPath::SetFillStyle(PathFillType fillstyle)
165 {
166 cmdList_->AddOp<SetFillStyleOpItem>(fillstyle);
167 }
168
BuildFromInterpolate(const Path & src,const Path & ending,scalar weight)169 bool RecordingPath::BuildFromInterpolate(const Path& src, const Path& ending, scalar weight)
170 {
171 auto srcHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, src);
172 auto endingHandle = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, ending);
173 cmdList_->AddOp<BuildFromInterpolateOpItem>(srcHandle, endingHandle, weight);
174
175 return true;
176 }
177
Transform(const Matrix & matrix)178 void RecordingPath::Transform(const Matrix& matrix)
179 {
180 cmdList_->AddOp<TransformOpItem>(matrix);
181 }
182
Offset(scalar dx,scalar dy)183 void RecordingPath::Offset(scalar dx, scalar dy)
184 {
185 cmdList_->AddOp<OffsetOpItem>(dx, dy);
186 }
187
Op(const Path & path1,Path & path2,PathOp op)188 bool RecordingPath::Op(const Path& path1, Path& path2, PathOp op)
189 {
190 auto pathHandle1 = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, path1);
191 auto pathHandle2 = CmdListHelper::AddRecordedToCmdList<RecordingPath>(*cmdList_, path2);
192 cmdList_->AddOp<PathOpWithOpItem>(pathHandle1, pathHandle2, op);
193
194 return true;
195 }
196
Reset()197 void RecordingPath::Reset()
198 {
199 cmdList_->AddOp<ResetOpItem>();
200 }
201
Close()202 void RecordingPath::Close()
203 {
204 cmdList_->AddOp<CloseOpItem>();
205 }
206 } // namespace Drawing
207 } // namespace Rosen
208 } // namespace OHOS
209