remove sneaked inserting unicodes, zero-width no-break space.
[platform/core/uifw/lottie-player.git] / src / vector / vraster.cpp
index 3c17908..ff5133b 100644 (file)
@@ -1,25 +1,42 @@
-#include"vraster.h"
-#include"v_ft_raster.h"
-#include"v_ft_stroker.h"
-#include"vpath.h"
-#include"vmatrix.h"
-#include<cstring>
-#include"vdebug.h"
-#include"vtaskqueue.h"
-#include<thread>
+/*
+ * 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"
+#include "v_ft_stroker.h"
+#include "vdebug.h"
+#include "vmatrix.h"
+#include "vpath.h"
+#include "vtaskqueue.h"
+#include "vrle.h"
 
 V_BEGIN_NAMESPACE
 
-struct FTOutline
-{
+struct FTOutline {
 public:
-    FTOutline():mMemory(nullptr){}
-    ~FTOutline(){delete[] mMemory;}
-    void releaseMemory() {
-        if (mMemory) delete [] mMemory;
-        mMemory = nullptr;
-        mPointSize = 0;
-        mSegmentSize = 0;
+    ~FTOutline()
+    {
+        if (mPointSize) delete[] ft.points;
+        if (mTagSize) delete[] ft.tags;
+        if (mSegmentSize) {
+            delete[] ft.contours;
+            delete[] ft.contours_flag;
+        }
     }
     void reset();
     void grow(int, int);
@@ -31,16 +48,15 @@ public:
     void close();
     void end();
     void transform(const VMatrix &m);
-    SW_FT_Outline            ft;
-    SW_FT_Vector            *mMemory{nullptr};
-    int                      mPointSize{0};
-    int                      mSegmentSize{0};
-    bool                     closed{false};
-    SW_FT_Stroker_LineCap    ftCap;
-    SW_FT_Stroker_LineJoin   ftJoin;
-    SW_FT_Fixed              ftWidth;
-    SW_FT_Fixed              ftMeterLimit;
-    SW_FT_Bool               ftClosed;
+    SW_FT_Outline          ft;
+    int                    mPointSize{0};
+    int                    mSegmentSize{0};
+    int                    mTagSize{0};
+    bool                   closed{false};
+    SW_FT_Stroker_LineCap  ftCap;
+    SW_FT_Stroker_LineJoin ftJoin;
+    SW_FT_Fixed            ftWidth;
+    SW_FT_Fixed            ftMeterLimit;
 };
 
 void FTOutline::reset()
@@ -52,41 +68,44 @@ void FTOutline::reset()
 void FTOutline::grow(int points, int segments)
 {
     reset();
-    if (mPointSize >= points && mSegmentSize >= segments)
-        return;
 
-    // release old memory
-    releaseMemory();
+    int point_size = (points + segments);
+    int segment_size = (sizeof(short) * segments);
+    int tag_size = (sizeof(char) * (points + segments));
 
-    //update book keeping
-    mPointSize = points;
-    mSegmentSize = segments;
+    if (point_size > mPointSize) {
+        if (mPointSize) delete [] ft.points;
+        ft.points = new SW_FT_Vector[point_size];
+        mPointSize = point_size;
+    }
 
-    int point_size = (points + segments);
-    int contour_size = ((sizeof(short) * segments) / sizeof(SW_FT_Vector)) + 1;
-    int tag_size = ((sizeof(char) * (points + segments)) / sizeof(SW_FT_Vector)) + 1;
-
-    /*
-     * Optimization, instead of allocating 3 different buffer
-     * allocate one big buffer and divide the buffer into 3 different
-     * segment.
-     */
-    mMemory = new SW_FT_Vector[point_size + contour_size + tag_size];
-    ft.points = reinterpret_cast<SW_FT_Vector *>(mMemory);
-    ft.tags = reinterpret_cast<char *>(mMemory + point_size);
-    ft.contours = reinterpret_cast<short *>(mMemory + point_size + tag_size);
+    if (segment_size > mSegmentSize) {
+        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;
+    }
+
+    if (tag_size > mTagSize) {
+        if (mTagSize) delete [] ft.tags;
+        ft.tags = new char[tag_size];
+        mTagSize = tag_size;
+    }
 }
 
 void FTOutline::convert(const VPath &path)
 {
     const std::vector<VPath::Element> &elements = path.elements();
-    const std::vector<VPointF> &points = path.points();
+    const std::vector<VPointF> &       points = path.points();
 
     grow(points.size(), path.segments());
 
     int index = 0;
-    for(auto element : elements) {
-        switch (element){
+    for (auto element : elements) {
+        switch (element) {
         case VPath::Element::MoveTo:
             moveTo(points[index]);
             index++;
@@ -96,8 +115,8 @@ void FTOutline::convert(const VPath &path)
             index++;
             break;
         case VPath::Element::CubicTo:
-            cubicTo(points[index], points[index+1], points[index+2]);
-            index = index+3;
+            cubicTo(points[index], points[index + 1], points[index + 2]);
+            index = index + 3;
             break;
         case VPath::Element::Close:
             close();
@@ -109,48 +128,44 @@ void FTOutline::convert(const VPath &path)
     end();
 }
 
-void FTOutline::convert(CapStyle cap, JoinStyle join,
-                        float width, float meterLimit)
+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;
+    // map strokeWidth to freetype. It uses as the radius of the pen not the
+    // diameter
+    width = width / 2.0;
     // convert to freetype co-ordinate
-    //IMP: stroker takes radius in 26.6 co-ordinate
-    ftWidth = SW_FT_Fixed(width * (1<<6));
-    //IMP: stroker takes meterlimit in 16.16 co-ordinate
-    ftMeterLimit = SW_FT_Fixed(meterLimit * (1<<16));
+    // IMP: stroker takes radius in 26.6 co-ordinate
+    ftWidth = SW_FT_Fixed(width * (1 << 6));
+    // IMP: stroker takes meterlimit in 16.16 co-ordinate
+    ftMeterLimit = SW_FT_Fixed(meterLimit * (1 << 16));
 
     // map to freetype capstyle
-    switch (cap)
-      {
-         case CapStyle::Square:
-           ftCap = SW_FT_STROKER_LINECAP_SQUARE;
-           break;
-         case CapStyle::Round:
-           ftCap = SW_FT_STROKER_LINECAP_ROUND;
-           break;
-         default:
-           ftCap = SW_FT_STROKER_LINECAP_BUTT;
-           break;
-      }
-    switch (join)
-      {
-         case JoinStyle::Bevel:
-           ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
-           break;
-         case JoinStyle::Round:
-           ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
-           break;
-         default:
-           ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
-           break;
-      }
+    switch (cap) {
+    case CapStyle::Square:
+        ftCap = SW_FT_STROKER_LINECAP_SQUARE;
+        break;
+    case CapStyle::Round:
+        ftCap = SW_FT_STROKER_LINECAP_ROUND;
+        break;
+    default:
+        ftCap = SW_FT_STROKER_LINECAP_BUTT;
+        break;
+    }
+    switch (join) {
+    case JoinStyle::Bevel:
+        ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
+        break;
+    case JoinStyle::Round:
+        ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
+        break;
+    default:
+        ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
+        break;
+    }
 }
 
-
-#define TO_FT_COORD(x) ((x) * 64) // to freetype 26.6 coordinate.
+#define TO_FT_COORD(x) ((x)*64)  // to freetype 26.6 coordinate.
 
 void FTOutline::moveTo(const VPointF &pt)
 {
@@ -161,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)
@@ -171,10 +189,10 @@ 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, const VPointF ep)
+void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
+                        const VPointF ep)
 {
     ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
     ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
@@ -190,10 +208,12 @@ void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2, const VPointF ep
     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;
@@ -211,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()
@@ -222,66 +241,80 @@ void FTOutline::end()
     }
 }
 
-struct SpanInfo
-{
-  VRle::Span *spans;
-  int          size;
+struct SpanInfo {
+    VRle::Span *spans;
+    int         size;
 };
 
-static void
-rleGenerationCb( int count, const SW_FT_Span*  spans,void *user)
+static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user)
 {
-   VRle *rle = (VRle *) user;
-   VRle::Span *rleSpan = (VRle::Span *)spans;
-   rle->addSpan(rleSpan, count);
+    VRle *      rle = (VRle *)user;
+    VRle::Span *rleSpan = (VRle::Span *)spans;
+    rle->addSpan(rleSpan, count);
 }
 
-struct RleTask
+static void bboxCb(int x, int y, int w, int h, void *user)
 {
-    RleTask() {
-        receiver = sender.get_future();
-    }
-    std::promise<VRle>       sender;
-    std::future<VRle>        receiver;
-    bool                     stroke;
-    VPath                    path;
-    FillRule                 fillRule;
-    CapStyle                 cap;
-    JoinStyle                join;
-    float                    width;
-    float                    meterLimit;
-    VRle operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
+    VRle *      rle = (VRle *)user;
+    rle->setBoundingRect({x, y, w, h});
+}
+
+struct RleTask {
+    RleShare           mRlePromise;
+    VPath              path;
+    VRle               rle;
+    float              width;
+    float              meterLimit;
+    VRect              clip;
+    FillRule           fillRule;
+    CapStyle           cap;
+    JoinStyle          join;
+    bool               stroke;
+    VRle               operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
+    void               render(FTOutline &outRef);
 };
 
-VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
+void RleTask::render(FTOutline &outRef)
 {
-    if (stroke) { //Stroke Task
-        outRef.convert(path);
-        outRef.convert(cap, join, width, meterLimit);
+    SW_FT_Raster_Params params;
 
-        uint points,contors;
+    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;
 
-        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_GetCounts(stroker,&points, &contors);
+    if (!clip.empty()) {
+        params.flags |= SW_FT_RASTER_FLAG_CLIP;
 
-        FTOutline strokeOutline;
-        strokeOutline.grow(points, contors);
+        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();
+    if (stroke) {  // Stroke Task
+        outRef.convert(path);
+        outRef.convert(cap, join, width, meterLimit);
 
-        SW_FT_Stroker_Export(stroker, &strokeOutline.ft);
+        uint points, contors;
 
-        VRle rle;
-        SW_FT_Raster_Params params;
+        SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin,
+                          outRef.ftMeterLimit);
+        SW_FT_Stroker_ParseOutline(stroker, &outRef.ft);
+        SW_FT_Stroker_GetCounts(stroker, &points, &contors);
 
-        params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
-        params.gray_spans = &rleGenerationCb;
-        params.user = &rle;
-        params.source = &strokeOutline;
+        outRef.grow(points, contors);
 
-        sw_ft_grays_raster.raster_render(nullptr, &params);
+        SW_FT_Stroker_Export(stroker, &outRef.ft);
 
-        return rle;
-    } else { //Fill Task
+    } else {  // Fill Task
         outRef.convert(path);
         int fillRuleFlag = SW_FT_OUTLINE_NONE;
         switch (fillRule) {
@@ -292,134 +325,133 @@ VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
             fillRuleFlag = SW_FT_OUTLINE_NONE;
             break;
         }
-        outRef.ft.flags =  fillRuleFlag;
-        VRle rle;
-        SW_FT_Raster_Params params;
+        outRef.ft.flags = fillRuleFlag;
+    }
 
-        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);
 
-        sw_ft_grays_raster.raster_render(nullptr, &params);
-        return rle;
-    }
+    path = VPath();
+
+    return std::move(rle);
 }
 
 class RleTaskScheduler {
-    const unsigned _count{std::thread::hardware_concurrency()};
-    std::vector<std::thread> _threads;
+    const unsigned                  _count{std::thread::hardware_concurrency()};
+    std::vector<std::thread>        _threads;
     std::vector<TaskQueue<RleTask>> _q{_count};
-    std::atomic<unsigned> _index{0};
+    std::atomic<unsigned>           _index{0};
 
-    void run(unsigned i) {
+    void run(unsigned i)
+    {
         /*
          * initalize  per thread objects.
          */
-        FTOutline  outlineRef;
+        FTOutline     outlineRef;
         SW_FT_Stroker stroker;
         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
+        // cleanup
         SW_FT_Stroker_Done(stroker);
     }
 
-public:
-    RleTaskScheduler() {
+    RleTaskScheduler()
+    {
         for (unsigned n = 0; n != _count; ++n) {
             _threads.emplace_back([&, n] { run(n); });
         }
     }
+public:
+    static RleTaskScheduler& instance()
+    {
+         static RleTaskScheduler singleton;
+         return singleton;
+    }
 
-    ~RleTaskScheduler() {
-        for (auto& e : _q)
-            e.done();
+    ~RleTaskScheduler()
+    {
+        for (auto &e : _q) e.done();
 
-        for (auto& e : _threads)
-            e.join();
+        for (auto &e : _threads) e.join();
     }
 
-    std::future<VRle> async(RleTask *task) {
-        auto receiver = std::move(task->receiver);
+    void async(RleTask &&task)
+    {
         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(const VPath &path,
-                                CapStyle cap,
-                                JoinStyle join,
-                                float width,
-                                float meterLimit) {
-        RleTask *task = new RleTask();
-        task->stroke = true;
-        task->path = path;
-        task->cap = cap;
-        task->join = join;
-        task->width = width;
-        task->meterLimit = meterLimit;
-        return async(task);
+    void strokeRle(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
+                                float width, float meterLimit, const VRect &clip)
+    {
+        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(const VPath &path, FillRule fillRule) {
-        RleTask *task = new RleTask();
-        task->path = path;
-        task->fillRule = fillRule;
-        task->stroke = false;
-        return async(task);
+    void fillRle(RleShare &promise, VPath &&path, VRle &&rle, FillRule fillRule, const VRect &clip)
+    {
+        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;
-
-VRaster::VRaster()
-{
-}
-
-VRaster::~VRaster()
+void VRaster::generateFillInfo(RleShare &promise, VPath &&path, VRle &&rle,
+                                            FillRule fillRule, const VRect &clip)
 {
-}
-
-std::future<VRle>
-VRaster::generateFillInfo(const VPath &path, FillRule fillRule)
-{
-    if (path.isEmpty()) {
-        std::promise<VRle> promise;
-        promise.set_value(VRle());
-        return promise.get_future();
+    if (path.empty()) {
+        promise->set_value(VRle());
+        return;
     }
-    return raster_scheduler.fillRle(path, fillRule);
+    return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
 }
 
-std::future<VRle>
-VRaster::generateStrokeInfo(const VPath &path, 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.isEmpty()) {
-        std::promise<VRle> promise;
-        promise.set_value(VRle());
-        return promise.get_future();
+    if (path.empty()) {
+        promise->set_value(VRle());
+        return;
     }
-    return raster_scheduler.strokeRle(path, cap, join, width, meterLimit);
+    return RleTaskScheduler::instance().strokeRle(promise, std::move(path), std::move(rle), cap, join, width, meterLimit, clip);
 }
 
 V_END_NAMESPACE