Put samsung copyright header to all our own source code.
[platform/core/uifw/lottie-player.git] / src / vector / vraster.cpp
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 #include "vraster.h"
18 #include <cstring>
19 #include <thread>
20 #include "v_ft_raster.h"
21 #include "v_ft_stroker.h"
22 #include "vdebug.h"
23 #include "vmatrix.h"
24 #include "vpath.h"
25 #include "vtaskqueue.h"
26 #include "vrle.h"
27
28 V_BEGIN_NAMESPACE
29
30 struct FTOutline {
31 public:
32     ~FTOutline()
33     {
34         if (mPointSize) delete[] ft.points;
35         if (mTagSize) delete[] ft.tags;
36         if (mSegmentSize) {
37             delete[] ft.contours;
38             delete[] ft.contours_flag;
39         }
40     }
41     void reset();
42     void grow(int, int);
43     void convert(const VPath &path);
44     void convert(CapStyle, JoinStyle, float, float);
45     void moveTo(const VPointF &pt);
46     void lineTo(const VPointF &pt);
47     void cubicTo(const VPointF &ctr1, const VPointF &ctr2, const VPointF end);
48     void close();
49     void end();
50     void transform(const VMatrix &m);
51     SW_FT_Outline          ft;
52     int                    mPointSize{0};
53     int                    mSegmentSize{0};
54     int                    mTagSize{0};
55     bool                   closed{false};
56     SW_FT_Stroker_LineCap  ftCap;
57     SW_FT_Stroker_LineJoin ftJoin;
58     SW_FT_Fixed            ftWidth;
59     SW_FT_Fixed            ftMeterLimit;
60 };
61
62 void FTOutline::reset()
63 {
64     ft.n_points = ft.n_contours = 0;
65     ft.flags = 0x0;
66 }
67
68 void FTOutline::grow(int points, int segments)
69 {
70     reset();
71
72     int point_size = (points + segments);
73     int segment_size = (sizeof(short) * segments);
74     int tag_size = (sizeof(char) * (points + segments));
75
76     if (point_size > mPointSize) {
77         if (mPointSize) delete [] ft.points;
78         ft.points = new SW_FT_Vector[point_size];
79         mPointSize = point_size;
80     }
81
82     if (segment_size > mSegmentSize) {
83         if (mSegmentSize) {
84             delete [] ft.contours;
85             delete [] ft.contours_flag;
86         }
87         ft.contours = new short[segment_size];
88         ft.contours_flag = new char[segment_size];
89         mSegmentSize = segment_size;
90     }
91
92     if (tag_size > mTagSize) {
93         if (mTagSize) delete [] ft.tags;
94         ft.tags = new char[tag_size];
95         mTagSize = tag_size;
96     }
97 }
98
99 void FTOutline::convert(const VPath &path)
100 {
101     const std::vector<VPath::Element> &elements = path.elements();
102     const std::vector<VPointF> &       points = path.points();
103
104     grow(points.size(), path.segments());
105
106     int index = 0;
107     for (auto element : elements) {
108         switch (element) {
109         case VPath::Element::MoveTo:
110             moveTo(points[index]);
111             index++;
112             break;
113         case VPath::Element::LineTo:
114             lineTo(points[index]);
115             index++;
116             break;
117         case VPath::Element::CubicTo:
118             cubicTo(points[index], points[index + 1], points[index + 2]);
119             index = index + 3;
120             break;
121         case VPath::Element::Close:
122             close();
123             break;
124         default:
125             break;
126         }
127     }
128     end();
129 }
130
131 void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
132                         float meterLimit)
133 {
134     // map strokeWidth to freetype. It uses as the radius of the pen not the
135     // diameter
136     width = width / 2.0;
137     // convert to freetype co-ordinate
138     // IMP: stroker takes radius in 26.6 co-ordinate
139     ftWidth = SW_FT_Fixed(width * (1 << 6));
140     // IMP: stroker takes meterlimit in 16.16 co-ordinate
141     ftMeterLimit = SW_FT_Fixed(meterLimit * (1 << 16));
142
143     // map to freetype capstyle
144     switch (cap) {
145     case CapStyle::Square:
146         ftCap = SW_FT_STROKER_LINECAP_SQUARE;
147         break;
148     case CapStyle::Round:
149         ftCap = SW_FT_STROKER_LINECAP_ROUND;
150         break;
151     default:
152         ftCap = SW_FT_STROKER_LINECAP_BUTT;
153         break;
154     }
155     switch (join) {
156     case JoinStyle::Bevel:
157         ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
158         break;
159     case JoinStyle::Round:
160         ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
161         break;
162     default:
163         ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
164         break;
165     }
166 }
167
168 #define TO_FT_COORD(x) ((x)*64)  // to freetype 26.6 coordinate.
169
170 void FTOutline::moveTo(const VPointF &pt)
171 {
172     ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
173     ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
174     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
175     if (ft.n_points) {
176         ft.contours[ft.n_contours] = ft.n_points - 1;
177         ft.n_contours++;
178     }
179     // mark the current contour as open
180     // will be updated if ther is a close tag at the end.
181     ft.contours_flag[ft.n_contours] = 1;
182
183     ft.n_points++;
184 }
185
186 void FTOutline::lineTo(const VPointF &pt)
187 {
188     ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
189     ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
190     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
191     ft.n_points++;
192 }
193
194 void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
195                         const VPointF ep)
196 {
197     ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
198     ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
199     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
200     ft.n_points++;
201
202     ft.points[ft.n_points].x = TO_FT_COORD(cp2.x());
203     ft.points[ft.n_points].y = TO_FT_COORD(cp2.y());
204     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
205     ft.n_points++;
206
207     ft.points[ft.n_points].x = TO_FT_COORD(ep.x());
208     ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
209     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
210     ft.n_points++;
211 }
212 void FTOutline::close()
213 {
214     // mark the contour as a close path.
215     ft.contours_flag[ft.n_contours] = 0;
216
217     int index;
218     if (ft.n_contours) {
219         index = ft.contours[ft.n_contours - 1] + 1;
220     } else {
221         index = 0;
222     }
223
224     // make sure atleast 1 point exists in the segment.
225     if (ft.n_points == index) {
226         closed = false;
227         return;
228     }
229
230     ft.points[ft.n_points].x = ft.points[index].x;
231     ft.points[ft.n_points].y = ft.points[index].y;
232     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
233     ft.n_points++;
234 }
235
236 void FTOutline::end()
237 {
238     if (ft.n_points) {
239         ft.contours[ft.n_contours] = ft.n_points - 1;
240         ft.n_contours++;
241     }
242 }
243
244 struct SpanInfo {
245     VRle::Span *spans;
246     int         size;
247 };
248
249 static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user)
250 {
251     VRle *      rle = (VRle *)user;
252     VRle::Span *rleSpan = (VRle::Span *)spans;
253     rle->addSpan(rleSpan, count);
254 }
255
256 static void bboxCb(int x, int y, int w, int h, void *user)
257 {
258     VRle *      rle = (VRle *)user;
259     rle->setBoundingRect({x, y, w, h});
260 }
261
262 struct RleTask {
263     RleShare           mRlePromise;
264     VPath              path;
265     VRle               rle;
266     float              width;
267     float              meterLimit;
268     VRect              clip;
269     FillRule           fillRule;
270     CapStyle           cap;
271     JoinStyle          join;
272     bool               stroke;
273     VRle               operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
274     void               render(FTOutline &outRef);
275 };
276
277 void RleTask::render(FTOutline &outRef)
278 {
279     SW_FT_Raster_Params params;
280
281     params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
282     params.gray_spans = &rleGenerationCb;
283     params.bbox_cb = &bboxCb;
284     params.user = &rle;
285     params.source = &outRef.ft;
286
287     if (!clip.empty()) {
288         params.flags |= SW_FT_RASTER_FLAG_CLIP;
289
290         params.clip_box.xMin =  clip.left();
291         params.clip_box.yMin =  clip.top();
292         params.clip_box.xMax =  clip.right();
293         params.clip_box.yMax =  clip.bottom();
294     }
295     // compute rle
296     sw_ft_grays_raster.raster_render(nullptr, &params);
297 }
298
299 VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
300 {
301     rle.reset();
302     if (stroke) {  // Stroke Task
303         outRef.convert(path);
304         outRef.convert(cap, join, width, meterLimit);
305
306         uint points, contors;
307
308         SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin,
309                           outRef.ftMeterLimit);
310         SW_FT_Stroker_ParseOutline(stroker, &outRef.ft);
311         SW_FT_Stroker_GetCounts(stroker, &points, &contors);
312
313         outRef.grow(points, contors);
314
315         SW_FT_Stroker_Export(stroker, &outRef.ft);
316
317     } else {  // Fill Task
318         outRef.convert(path);
319         int fillRuleFlag = SW_FT_OUTLINE_NONE;
320         switch (fillRule) {
321         case FillRule::EvenOdd:
322             fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
323             break;
324         default:
325             fillRuleFlag = SW_FT_OUTLINE_NONE;
326             break;
327         }
328         outRef.ft.flags = fillRuleFlag;
329     }
330
331     render(outRef);
332
333     path = VPath();
334
335     return std::move(rle);
336 }
337
338 class RleTaskScheduler {
339     const unsigned                  _count{std::thread::hardware_concurrency()};
340     std::vector<std::thread>        _threads;
341     std::vector<TaskQueue<RleTask>> _q{_count};
342     std::atomic<unsigned>           _index{0};
343
344     void run(unsigned i)
345     {
346         /*
347          * initalize  per thread objects.
348          */
349         FTOutline     outlineRef;
350         SW_FT_Stroker stroker;
351         SW_FT_Stroker_New(&stroker);
352
353         // Task Loop
354         RleTask task;
355         while (true) {
356             bool success = false;
357
358             for (unsigned n = 0; n != _count * 32; ++n) {
359                 if (_q[(i + n) % _count].try_pop(task)) {
360                     success = true;
361                     break;
362                 }
363             }
364
365             if (!success && !_q[i].pop(task)) break;
366
367             task.mRlePromise->set_value((task)(outlineRef, stroker));
368         }
369
370         // cleanup
371         SW_FT_Stroker_Done(stroker);
372     }
373
374     RleTaskScheduler()
375     {
376         for (unsigned n = 0; n != _count; ++n) {
377             _threads.emplace_back([&, n] { run(n); });
378         }
379     }
380 public:
381     static RleTaskScheduler& instance()
382     {
383          static RleTaskScheduler singleton;
384          return singleton;
385     }
386
387     ~RleTaskScheduler()
388     {
389         for (auto &e : _q) e.done();
390
391         for (auto &e : _threads) e.join();
392     }
393
394     void async(RleTask &&task)
395     {
396         auto i = _index++;
397
398         for (unsigned n = 0; n != _count; ++n) {
399             if (_q[(i + n) % _count].try_push(std::move(task))) return;
400         }
401
402         _q[i % _count].push(std::move(task));
403     }
404
405     void strokeRle(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
406                                 float width, float meterLimit, const VRect &clip)
407     {
408         RleTask task;
409         task.stroke = true;
410         task.path = std::move(path);
411         task.rle = std::move(rle);
412         task.cap = cap;
413         task.join = join;
414         task.width = width;
415         task.meterLimit = meterLimit;
416         task.clip = clip;
417         task.mRlePromise = promise;
418
419         async(std::move(task));
420     }
421
422     void fillRle(RleShare &promise, VPath &&path, VRle &&rle, FillRule fillRule, const VRect &clip)
423     {
424         RleTask task;
425         task.path = std::move(path);
426         task.rle = std::move(rle);
427         task.fillRule = fillRule;
428         task.clip = clip;
429         task.stroke = false;
430         task.mRlePromise = promise;
431
432         async(std::move(task));
433     }
434 };
435
436 void VRaster::generateFillInfo(RleShare &promise, VPath &&path, VRle &&rle,
437                                             FillRule fillRule, const VRect &clip)
438 {
439     if (path.empty()) {
440         promise->set_value(VRle());
441         return;
442     }
443     return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
444 }
445
446 void VRaster::generateStrokeInfo(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap,
447                                  JoinStyle join, float width,
448                                  float meterLimit, const VRect &clip)
449 {
450     if (path.empty()) {
451         promise->set_value(VRle());
452         return;
453     }
454     return RleTaskScheduler::instance().strokeRle(promise, std::move(path), std::move(rle), cap, join, width, meterLimit, clip);
455 }
456
457 V_END_NAMESPACE