remove sneaked inserting unicodes, zero-width no-break space.
[platform/core/uifw/lottie-player.git] / src / vector / vraster.cpp
index b5e58f4..ff5133b 100644 (file)
@@ -1,4 +1,20 @@
-#include "vraster.h"
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "vraster.h"
 #include <cstring>
 #include <thread>
 #include "v_ft_raster.h"
@@ -17,7 +33,10 @@ public:
     {
         if (mPointSize) delete[] ft.points;
         if (mTagSize) delete[] ft.tags;
-        if (mSegmentSize) delete[] ft.contours;
+        if (mSegmentSize) {
+            delete[] ft.contours;
+            delete[] ft.contours_flag;
+        }
     }
     void reset();
     void grow(int, int);
@@ -38,7 +57,6 @@ public:
     SW_FT_Stroker_LineJoin ftJoin;
     SW_FT_Fixed            ftWidth;
     SW_FT_Fixed            ftMeterLimit;
-    SW_FT_Bool             ftClosed;
 };
 
 void FTOutline::reset()
@@ -62,8 +80,12 @@ void FTOutline::grow(int points, int segments)
     }
 
     if (segment_size > mSegmentSize) {
-        if (mSegmentSize) delete [] ft.contours;
+        if (mSegmentSize) {
+            delete [] ft.contours;
+            delete [] ft.contours_flag;
+        }
         ft.contours = new short[segment_size];
+        ft.contours_flag = new char[segment_size];
         mSegmentSize = segment_size;
     }
 
@@ -109,8 +131,6 @@ void FTOutline::convert(const VPath &path)
 void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
                         float meterLimit)
 {
-    ftClosed = (SW_FT_Bool)closed;
-
     // map strokeWidth to freetype. It uses as the radius of the pen not the
     // diameter
     width = width / 2.0;
@@ -156,8 +176,11 @@ void FTOutline::moveTo(const VPointF &pt)
         ft.contours[ft.n_contours] = ft.n_points - 1;
         ft.n_contours++;
     }
+    // mark the current contour as open
+    // will be updated if ther is a close tag at the end.
+    ft.contours_flag[ft.n_contours] = 1;
+
     ft.n_points++;
-    closed = false;
 }
 
 void FTOutline::lineTo(const VPointF &pt)
@@ -166,7 +189,6 @@ void FTOutline::lineTo(const VPointF &pt)
     ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
     ft.n_points++;
-    closed = false;
 }
 
 void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
@@ -186,10 +208,12 @@ void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
     ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
     ft.n_points++;
-    closed = false;
 }
 void FTOutline::close()
 {
+    // mark the contour as a close path.
+    ft.contours_flag[ft.n_contours] = 0;
+
     int index;
     if (ft.n_contours) {
         index = ft.contours[ft.n_contours - 1] + 1;
@@ -207,7 +231,6 @@ void FTOutline::close()
     ft.points[ft.n_points].y = ft.points[index].y;
     ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
     ft.n_points++;
-    closed = true;
 }
 
 void FTOutline::end()
@@ -230,21 +253,49 @@ static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user)
     rle->addSpan(rleSpan, count);
 }
 
+static void bboxCb(int x, int y, int w, int h, void *user)
+{
+    VRle *      rle = (VRle *)user;
+    rle->setBoundingRect({x, y, w, h});
+}
+
 struct RleTask {
-    RleTask() { receiver = sender.get_future(); }
-    std::promise<VRle> sender;
-    std::future<VRle>  receiver;
-    bool               stroke;
+    RleShare           mRlePromise;
     VPath              path;
     VRle               rle;
+    float              width;
+    float              meterLimit;
+    VRect              clip;
     FillRule           fillRule;
     CapStyle           cap;
     JoinStyle          join;
-    float              width;
-    float              meterLimit;
+    bool               stroke;
     VRle               operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
+    void               render(FTOutline &outRef);
 };
 
+void RleTask::render(FTOutline &outRef)
+{
+    SW_FT_Raster_Params params;
+
+    params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
+    params.gray_spans = &rleGenerationCb;
+    params.bbox_cb = &bboxCb;
+    params.user = &rle;
+    params.source = &outRef.ft;
+
+    if (!clip.empty()) {
+        params.flags |= SW_FT_RASTER_FLAG_CLIP;
+
+        params.clip_box.xMin =  clip.left();
+        params.clip_box.yMin =  clip.top();
+        params.clip_box.xMax =  clip.right();
+        params.clip_box.yMax =  clip.bottom();
+    }
+    // compute rle
+    sw_ft_grays_raster.raster_render(nullptr, &params);
+}
+
 VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
 {
     rle.reset();
@@ -256,22 +307,13 @@ VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
 
         SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin,
                           outRef.ftMeterLimit);
-        SW_FT_Stroker_ParseOutline(stroker, &outRef.ft, !outRef.ftClosed);
+        SW_FT_Stroker_ParseOutline(stroker, &outRef.ft);
         SW_FT_Stroker_GetCounts(stroker, &points, &contors);
 
         outRef.grow(points, contors);
 
         SW_FT_Stroker_Export(stroker, &outRef.ft);
 
-        SW_FT_Raster_Params params;
-
-        params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
-        params.gray_spans = &rleGenerationCb;
-        params.user = &rle;
-        params.source = &outRef;
-
-        sw_ft_grays_raster.raster_render(nullptr, &params);
-
     } else {  // Fill Task
         outRef.convert(path);
         int fillRuleFlag = SW_FT_OUTLINE_NONE;
@@ -284,15 +326,12 @@ VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
             break;
         }
         outRef.ft.flags = fillRuleFlag;
-        SW_FT_Raster_Params params;
+    }
 
-        params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
-        params.gray_spans = &rleGenerationCb;
-        params.user = &rle;
-        params.source = &outRef.ft;
+    render(outRef);
+
+    path = VPath();
 
-        sw_ft_grays_raster.raster_render(nullptr, &params);
-    }
     return std::move(rle);
 }
 
@@ -312,29 +351,38 @@ class RleTaskScheduler {
         SW_FT_Stroker_New(&stroker);
 
         // Task Loop
+        RleTask task;
         while (true) {
-            RleTask *task = nullptr;
+            bool success = false;
 
             for (unsigned n = 0; n != _count * 32; ++n) {
-                if (_q[(i + n) % _count].try_pop(task)) break;
+                if (_q[(i + n) % _count].try_pop(task)) {
+                    success = true;
+                    break;
+                }
             }
-            if (!task && !_q[i].pop(task)) break;
 
-            task->sender.set_value((*task)(outlineRef, stroker));
-            delete task;
+            if (!success && !_q[i].pop(task)) break;
+
+            task.mRlePromise->set_value((task)(outlineRef, stroker));
         }
 
         // cleanup
         SW_FT_Stroker_Done(stroker);
     }
 
-public:
     RleTaskScheduler()
     {
         for (unsigned n = 0; n != _count; ++n) {
             _threads.emplace_back([&, n] { run(n); });
         }
     }
+public:
+    static RleTaskScheduler& instance()
+    {
+         static RleTaskScheduler singleton;
+         return singleton;
+    }
 
     ~RleTaskScheduler()
     {
@@ -343,68 +391,67 @@ public:
         for (auto &e : _threads) e.join();
     }
 
-    std::future<VRle> async(RleTask *task)
+    void async(RleTask &&task)
     {
-        auto receiver = std::move(task->receiver);
         auto i = _index++;
 
         for (unsigned n = 0; n != _count; ++n) {
-            if (_q[(i + n) % _count].try_push(task)) return receiver;
+            if (_q[(i + n) % _count].try_push(std::move(task))) return;
         }
 
-        _q[i % _count].push(task);
-
-        return receiver;
+        _q[i % _count].push(std::move(task));
     }
 
-    std::future<VRle> strokeRle(VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
-                                float width, float meterLimit)
+    void strokeRle(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
+                                float width, float meterLimit, const VRect &clip)
     {
-        RleTask *task = new RleTask();
-        task->stroke = true;
-        task->path = std::move(path);
-        task->rle = std::move(rle);
-        task->cap = cap;
-        task->join = join;
-        task->width = width;
-        task->meterLimit = meterLimit;
-        return async(task);
+        RleTask task;
+        task.stroke = true;
+        task.path = std::move(path);
+        task.rle = std::move(rle);
+        task.cap = cap;
+        task.join = join;
+        task.width = width;
+        task.meterLimit = meterLimit;
+        task.clip = clip;
+        task.mRlePromise = promise;
+
+        async(std::move(task));
     }
 
-    std::future<VRle> fillRle(VPath &&path, VRle &&rle, FillRule fillRule)
+    void fillRle(RleShare &promise, VPath &&path, VRle &&rle, FillRule fillRule, const VRect &clip)
     {
-        RleTask *task = new RleTask();
-        task->path = std::move(path);
-        task->rle = std::move(rle);
-        task->fillRule = fillRule;
-        task->stroke = false;
-        return async(task);
+        RleTask task;
+        task.path = std::move(path);
+        task.rle = std::move(rle);
+        task.fillRule = fillRule;
+        task.clip = clip;
+        task.stroke = false;
+        task.mRlePromise = promise;
+
+        async(std::move(task));
     }
 };
 
-static RleTaskScheduler raster_scheduler;
-
-std::future<VRle> VRaster::generateFillInfo(VPath &&path, VRle &&rle,
-                                            FillRule     fillRule)
+void VRaster::generateFillInfo(RleShare &promise, VPath &&path, VRle &&rle,
+                                            FillRule fillRule, const VRect &clip)
 {
     if (path.empty()) {
-        std::promise<VRle> promise;
-        promise.set_value(VRle());
-        return promise.get_future();
+        promise->set_value(VRle());
+        return;
     }
-    return raster_scheduler.fillRle(std::move(path), std::move(rle), fillRule);
+    return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
 }
 
-std::future<VRle> VRaster::generateStrokeInfo(VPath &&path, VRle &&rle, CapStyle cap,
-                                              JoinStyle join, float width,
-                                              float meterLimit)
+void VRaster::generateStrokeInfo(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap,
+                                 JoinStyle join, float width,
+                                 float meterLimit, const VRect &clip)
 {
     if (path.empty()) {
-        std::promise<VRle> promise;
-        promise.set_value(VRle());
-        return promise.get_future();
+        promise->set_value(VRle());
+        return;
     }
-    return raster_scheduler.strokeRle(std::move(path), std::move(rle), cap, join, width, meterLimit);
+    return RleTaskScheduler::instance().strokeRle(promise, std::move(path), std::move(rle), cap, join, width, meterLimit, clip);
 }
 
 V_END_NAMESPACE