lottie/vector: move pixman code to separate pixman folder.
[platform/core/uifw/lottie-player.git] / src / vector / vpath.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef VPATH_H
18 #define VPATH_H
19 #include <vector>
20 #include "vcowptr.h"
21 #include "vmatrix.h"
22 #include "vpoint.h"
23 #include "vrect.h"
24
25 V_BEGIN_NAMESPACE
26
27 struct VPathData;
28 class VPath {
29 public:
30     enum class Direction { CCW, CW };
31
32     enum class Element : uchar { MoveTo, LineTo, CubicTo, Close };
33     bool  empty() const;
34     bool  null() const;
35     void  moveTo(const VPointF &p);
36     void  moveTo(float x, float y);
37     void  lineTo(const VPointF &p);
38     void  lineTo(float x, float y);
39     void  cubicTo(const VPointF &c1, const VPointF &c2, const VPointF &e);
40     void  cubicTo(float c1x, float c1y, float c2x, float c2y, float ex,
41                   float ey);
42     void  arcTo(const VRectF &rect, float startAngle, float sweepLength,
43                 bool forceMoveTo);
44     void  close();
45     void  reset();
46     void  reserve(int pts, int elms);
47     int   segments() const;
48     void  addCircle(float cx, float cy, float radius,
49                     VPath::Direction dir = Direction::CW);
50     void  addOval(const VRectF &rect, VPath::Direction dir = Direction::CW);
51     void  addRoundRect(const VRectF &rect, float rx, float ry,
52                        VPath::Direction dir = Direction::CW);
53     void  addRoundRect(const VRectF &rect, float roundness,
54                        VPath::Direction dir = Direction::CW);
55     void  addRect(const VRectF &rect, VPath::Direction dir = Direction::CW);
56     void  addPolystar(float points, float innerRadius, float outerRadius,
57                       float innerRoundness, float outerRoundness,
58                       float startAngle, float cx, float cy,
59                       VPath::Direction dir = Direction::CW);
60     void  addPolygon(float points, float radius, float roundness,
61                      float startAngle, float cx, float cy,
62                      VPath::Direction dir = Direction::CW);
63     void addPath(const VPath &path);
64     void  transform(const VMatrix &m);
65     float length() const;
66     const std::vector<VPath::Element> &elements() const;
67     const std::vector<VPointF> &       points() const;
68     void  clone(const VPath &srcPath);
69     bool unique() const { return d.unique();}
70     int refCount() const { return d.refCount();}
71
72 private:
73     struct VPathData {
74         bool  empty() const { return m_elements.empty(); }
75         bool  null() const { return empty() && !m_elements.capacity();}
76         void  moveTo(float x, float y);
77         void  lineTo(float x, float y);
78         void  cubicTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey);
79         void  close();
80         void  reset();
81         void  reserve(int, int);
82         void  checkNewSegment();
83         int   segments() const;
84         void  transform(const VMatrix &m);
85         float length() const;
86         void  addRoundRect(const VRectF &, float, float, VPath::Direction);
87         void  addRoundRect(const VRectF &, float, VPath::Direction);
88         void  addRect(const VRectF &, VPath::Direction);
89         void  arcTo(const VRectF &, float, float, bool);
90         void  addCircle(float, float, float, VPath::Direction);
91         void  addOval(const VRectF &, VPath::Direction);
92         void  addPolystar(float points, float innerRadius, float outerRadius,
93                           float innerRoundness, float outerRoundness,
94                           float startAngle, float cx, float cy,
95                           VPath::Direction dir = Direction::CW);
96         void  addPolygon(float points, float radius, float roundness,
97                          float startAngle, float cx, float cy,
98                          VPath::Direction dir = Direction::CW);
99         void  addPath(const VPathData &path);
100         const std::vector<VPath::Element> &elements() const
101         {
102             return m_elements;
103         }
104         const std::vector<VPointF> &points() const { return m_points; }
105         std::vector<VPointF>        m_points;
106         std::vector<VPath::Element> m_elements;
107         int                         m_segments;
108         VPointF                     mStartPoint;
109         mutable float               mLength{0};
110         mutable bool                mLengthDirty{true};
111         bool                        mNewSegment;
112     };
113
114     vcow_ptr<VPathData> d;
115 };
116
117 inline bool VPath::empty() const
118 {
119     return d->empty();
120 }
121
122 /*
123  * path is empty as well as null(no memory for data allocated yet).
124  */
125 inline bool VPath::null() const
126 {
127     return d->null();
128 }
129
130 inline void VPath::moveTo(const VPointF &p)
131 {
132     d.write().moveTo(p.x(), p.y());
133 }
134
135 inline void VPath::lineTo(const VPointF &p)
136 {
137     d.write().lineTo(p.x(), p.y());
138 }
139
140 inline void VPath::close()
141 {
142     d.write().close();
143 }
144
145 inline void VPath::reset()
146 {
147     d.write().reset();
148 }
149
150 inline void VPath::reserve(int pts, int elms)
151 {
152     d.write().reserve(pts, elms);
153 }
154
155 inline int VPath::segments() const
156 {
157     return d->segments();
158 }
159
160 inline float VPath::length() const
161 {
162     return d->length();
163 }
164
165 inline void VPath::cubicTo(const VPointF &c1, const VPointF &c2,
166                            const VPointF &e)
167 {
168     d.write().cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
169 }
170
171 inline void VPath::lineTo(float x, float y)
172 {
173     d.write().lineTo(x, y);
174 }
175
176 inline void VPath::moveTo(float x, float y)
177 {
178     d.write().moveTo(x, y);
179 }
180
181 inline void VPath::cubicTo(float c1x, float c1y, float c2x, float c2y, float ex,
182                            float ey)
183 {
184     d.write().cubicTo(c1x, c1y, c2x, c2y, ex, ey);
185 }
186
187 inline void VPath::transform(const VMatrix &m)
188 {
189     d.write().transform(m);
190 }
191
192 inline void VPath::arcTo(const VRectF &rect, float startAngle,
193                          float sweepLength, bool forceMoveTo)
194 {
195     d.write().arcTo(rect, startAngle, sweepLength, forceMoveTo);
196 }
197
198 inline void VPath::addRect(const VRectF &rect, VPath::Direction dir)
199 {
200     d.write().addRect(rect, dir);
201 }
202
203 inline void VPath::addRoundRect(const VRectF &rect, float rx, float ry,
204                                 VPath::Direction dir)
205 {
206     d.write().addRoundRect(rect, rx, ry, dir);
207 }
208
209 inline void VPath::addRoundRect(const VRectF &rect, float roundness,
210                                 VPath::Direction dir)
211 {
212     d.write().addRoundRect(rect, roundness, dir);
213 }
214
215 inline void VPath::addCircle(float cx, float cy, float radius,
216                              VPath::Direction dir)
217 {
218     d.write().addCircle(cx, cy, radius, dir);
219 }
220
221 inline void VPath::addOval(const VRectF &rect, VPath::Direction dir)
222 {
223     d.write().addOval(rect, dir);
224 }
225
226 inline void VPath::addPolystar(float points, float innerRadius,
227                                float outerRadius, float innerRoundness,
228                                float outerRoundness, float startAngle, float cx,
229                                float cy, VPath::Direction dir)
230 {
231     d.write().addPolystar(points, innerRadius, outerRadius, innerRoundness,
232                           outerRoundness, startAngle, cx, cy, dir);
233 }
234
235 inline void VPath::addPolygon(float points, float radius, float roundness,
236                               float startAngle, float cx, float cy,
237                               VPath::Direction dir)
238 {
239     d.write().addPolygon(points, radius, roundness, startAngle, cx, cy, dir);
240 }
241
242 inline void VPath::addPath(const VPath &path)
243 {
244     if (path.empty()) return;
245
246     if (null()) {
247         *this = path;
248     } else {
249         d.write().addPath(path.d.read());
250     }
251 }
252
253 inline const std::vector<VPath::Element> &VPath::elements() const
254 {
255     return d->elements();
256 }
257
258 inline const std::vector<VPointF> &VPath::points() const
259 {
260     return d->points();
261 }
262
263 inline void VPath::clone(const VPath &srcPath)
264 {
265    reset();
266    addPath(srcPath);
267 }
268
269 V_END_NAMESPACE
270
271 #endif  // VPATH_H