-#include"vraster.h"
-#include"v_ft_raster.h"
-#include"v_ft_stroker.h"
-#include"vpath.h"
-#include"vmatrix.h"
-#include<cstring>
-#include"vdebug.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"
+#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() = delete;
- FTOutline(int points, int segments)
+ ~FTOutline()
{
- ft.points = new SW_FT_Vector[points + segments];
- ft.tags = new char[points + segments];
- ft.contours = new short[segments];
- ft.n_points = ft.n_contours = 0;
- ft.flags = 0x0;
+ 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);
+ void convert(const VPath &path);
+ void convert(CapStyle, JoinStyle, float, float);
void moveTo(const VPointF &pt);
void lineTo(const VPointF &pt);
void cubicTo(const VPointF &ctr1, const VPointF &ctr2, const VPointF end);
void close();
void end();
void transform(const VMatrix &m);
- ~FTOutline()
- {
- delete[] ft.points;
- delete[] ft.tags;
- delete[] ft.contours;
- }
- SW_FT_Outline ft;
- bool closed;
+ 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;
};
-#define TO_FT_COORD(x) ((x) * 64) // to freetype 26.6 coordinate.
+void FTOutline::reset()
+{
+ ft.n_points = ft.n_contours = 0;
+ ft.flags = 0x0;
+}
+
+void FTOutline::grow(int points, int segments)
+{
+ reset();
+
+ int point_size = (points + segments);
+ int segment_size = (sizeof(short) * segments);
+ int tag_size = (sizeof(char) * (points + segments));
+
+ if (point_size > mPointSize) {
+ if (mPointSize) delete [] ft.points;
+ ft.points = new SW_FT_Vector[point_size];
+ mPointSize = point_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();
+
+ grow(points.size(), path.segments());
+
+ int index = 0;
+ for (auto element : elements) {
+ switch (element) {
+ case VPath::Element::MoveTo:
+ moveTo(points[index]);
+ index++;
+ break;
+ case VPath::Element::LineTo:
+ lineTo(points[index]);
+ index++;
+ break;
+ case VPath::Element::CubicTo:
+ cubicTo(points[index], points[index + 1], points[index + 2]);
+ index = index + 3;
+ break;
+ case VPath::Element::Close:
+ close();
+ break;
+ default:
+ break;
+ }
+ }
+ end();
+}
-void FTOutline::transform(const VMatrix &m)
+void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
+ float meterLimit)
{
- VPointF pt;
- if (m.isIdentity()) return;
- for (auto i = 0; i < ft.n_points; i++) {
- pt = m.map(VPointF(ft.points[i].x/64.0, ft.points[i].y/64.0));
- ft.points[i].x = TO_FT_COORD(pt.x());
- ft.points[i].y = TO_FT_COORD(pt.y());
+ // 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));
+
+ // 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;
}
}
+#define TO_FT_COORD(x) ((x)*64) // to freetype 26.6 coordinate.
+
void FTOutline::moveTo(const VPointF &pt)
{
ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
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)
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());
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;
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()
}
}
-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);
}
-VRle generateFillInfoAsync(const SW_FT_Outline *outline)
+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 {
+ 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);
+};
+
+void RleTask::render(FTOutline &outRef)
{
- VRle rle;
SW_FT_Raster_Params params;
- params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
+ 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 = outline;
+ params.source = &outRef.ft;
- sw_ft_grays_raster.raster_render(nullptr, ¶ms);
+ if (!clip.empty()) {
+ params.flags |= SW_FT_RASTER_FLAG_CLIP;
- return rle;
+ 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, ¶ms);
}
-VRle generateStrokeInfoAsync(const SW_FT_Outline *outline, SW_FT_Stroker_LineCap cap,
- SW_FT_Stroker_LineJoin join,
- int width, int meterLimit,
- SW_FT_Bool closed)
+VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
{
- SW_FT_Stroker stroker;
- SW_FT_Stroker_New(&stroker);
+ rle.reset();
+ if (stroke) { // Stroke Task
+ outRef.convert(path);
+ outRef.convert(cap, join, width, meterLimit);
- uint points,contors;
- SW_FT_Outline strokeOutline = { 0, 0, nullptr, nullptr, nullptr, SW_FT_OUTLINE_NONE };
+ uint points, contors;
- SW_FT_Stroker_Set(stroker, width, cap, join, meterLimit);
- SW_FT_Stroker_ParseOutline(stroker, outline, !closed);
- SW_FT_Stroker_GetCounts(stroker,&points, &contors);
+ 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);
- strokeOutline.points = (SW_FT_Vector *) calloc(points, sizeof(SW_FT_Vector));
- strokeOutline.tags = (char *) calloc(points, sizeof(char));
- strokeOutline.contours = (short *) calloc(contors, sizeof(short));
+ outRef.grow(points, contors);
- SW_FT_Stroker_Export(stroker, &strokeOutline);
+ SW_FT_Stroker_Export(stroker, &outRef.ft);
- SW_FT_Stroker_Done(stroker);
+ } else { // Fill Task
+ outRef.convert(path);
+ int fillRuleFlag = SW_FT_OUTLINE_NONE;
+ switch (fillRule) {
+ case FillRule::EvenOdd:
+ fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
+ break;
+ default:
+ fillRuleFlag = SW_FT_OUTLINE_NONE;
+ break;
+ }
+ outRef.ft.flags = fillRuleFlag;
+ }
- VRle rle = generateFillInfoAsync(&strokeOutline);
+ render(outRef);
- // cleanup the outline data.
- free(strokeOutline.points);
- free(strokeOutline.tags);
- free(strokeOutline.contours);
+ path = VPath();
- return rle;
+ return std::move(rle);
}
+class RleTaskScheduler {
+ const unsigned _count{std::thread::hardware_concurrency()};
+ std::vector<std::thread> _threads;
+ std::vector<TaskQueue<RleTask>> _q{_count};
+ std::atomic<unsigned> _index{0};
-VRaster::VRaster()
-{
-}
+ void run(unsigned i)
+ {
+ /*
+ * initalize per thread objects.
+ */
+ FTOutline outlineRef;
+ SW_FT_Stroker stroker;
+ SW_FT_Stroker_New(&stroker);
+
+ // Task Loop
+ RleTask task;
+ while (true) {
+ bool success = false;
+
+ for (unsigned n = 0; n != _count * 32; ++n) {
+ if (_q[(i + n) % _count].try_pop(task)) {
+ success = true;
+ break;
+ }
+ }
+
+ if (!success && !_q[i].pop(task)) break;
+
+ task.mRlePromise->set_value((task)(outlineRef, stroker));
+ }
-VRaster::~VRaster()
-{
-}
+ // cleanup
+ SW_FT_Stroker_Done(stroker);
+ }
-void VRaster::deleteFTOutline(FTOutline *outline)
-{
- delete outline;
-}
+ RleTaskScheduler()
+ {
+ for (unsigned n = 0; n != _count; ++n) {
+ _threads.emplace_back([&, n] { run(n); });
+ }
+ }
+public:
+ static RleTaskScheduler& instance()
+ {
+ static RleTaskScheduler singleton;
+ return singleton;
+ }
-FTOutline *VRaster::toFTOutline(const VPath &path)
-{
- if (path.isEmpty())
- return nullptr;
+ ~RleTaskScheduler()
+ {
+ for (auto &e : _q) e.done();
- const std::vector<VPath::Element> &elements = path.elements();
- const std::vector<VPointF> &points = path.points();
+ for (auto &e : _threads) e.join();
+ }
- FTOutline *outline = new FTOutline(points.size(), path.segments());
+ void async(RleTask &&task)
+ {
+ auto i = _index++;
- int index = 0;
- for(auto element : elements) {
- switch (element){
- case VPath::Element::MoveTo:
- outline->moveTo(points[index]);
- index++;
- break;
- case VPath::Element::LineTo:
- outline->lineTo(points[index]);
- index++;
- break;
- case VPath::Element::CubicTo:
- outline->cubicTo(points[index], points[index+1], points[index+2]);
- index = index+3;
- break;
- case VPath::Element::Close:
- outline->close();
- break;
- default:
- break;
+ for (unsigned n = 0; n != _count; ++n) {
+ if (_q[(i + n) % _count].try_push(std::move(task))) return;
}
+
+ _q[i % _count].push(std::move(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));
}
- outline->end();
- return outline;
-}
-VRle VRaster::generateFillInfo(const FTOutline *outline, FillRule fillRule)
+ 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));
+ }
+};
+
+void VRaster::generateFillInfo(RleShare &promise, VPath &&path, VRle &&rle,
+ FillRule fillRule, const VRect &clip)
{
- int fillRuleFlag = SW_FT_OUTLINE_NONE;
- switch (fillRule) {
- case FillRule::EvenOdd:
- fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
- break;
- default:
- fillRuleFlag = SW_FT_OUTLINE_NONE;
- break;
+ if (path.empty()) {
+ promise->set_value(VRle());
+ return;
}
- FTOutline *outlineRef = const_cast<FTOutline *>(outline);
- outlineRef->ft.flags = fillRuleFlag;
- return generateFillInfoAsync(&outlineRef->ft);
+ return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
}
-VRle VRaster::generateStrokeInfo(const FTOutline *outline, 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)
{
- SW_FT_Stroker_LineCap ftCap;
- SW_FT_Stroker_LineJoin ftJoin;
- int ftWidth;
- int ftMeterLimit;
- SW_FT_Bool ftbool = (SW_FT_Bool) outline->closed;
-
- // map strokeWidth to freetype. It uses as the radius of the pen not the diameter
- width = width/2.0;
- // convert to freetype co-ordinate
- ftWidth = int(width * 64);
- ftMeterLimit = int(meterLimit * 64);
-
- // 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;
- }
-
- return generateStrokeInfoAsync(&outline->ft, ftCap, ftJoin,
- ftWidth, ftMeterLimit, ftbool);
+ if (path.empty()) {
+ promise->set_value(VRle());
+ return;
+ }
+ return RleTaskScheduler::instance().strokeRle(promise, std::move(path), std::move(rle), cap, join, width, meterLimit, clip);
}
V_END_NAMESPACE