-#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() = 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;
};
+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());
-#define TO_FT_COORD(x) ((x) * 64) // to freetype 26.6 coordinate.
+ 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);
}
-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;
- FTOutline *outline;
- SW_FT_Stroker_LineCap cap;
- SW_FT_Stroker_LineJoin join;
- int width;
- int meterLimit;
- SW_FT_Bool closed;
-};
-
-static VRle generateRleAsync(RleTask *task);
+ VRle * rle = (VRle *)user;
+ rle->setBoundingRect({x, y, w, h});
+}
-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};
+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 run(unsigned i) {
- while (true) {
- RleTask *task = nullptr;
+void RleTask::render(FTOutline &outRef)
+{
+ SW_FT_Raster_Params params;
- for (unsigned n = 0; n != _count * 32; ++n) {
- if (_q[(i + n) % _count].try_pop(task)) break;
- }
- if (!task && !_q[i].pop(task)) break;
+ 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;
- VRle rle = generateRleAsync(task);
- task->sender.set_value(std::move(rle));
- delete task->outline;
- delete task;
- }
- }
+ if (!clip.empty()) {
+ params.flags |= SW_FT_RASTER_FLAG_CLIP;
-public:
- RleTaskScheduler() {
- for (unsigned n = 0; n != _count; ++n) {
- _threads.emplace_back([&, n] { run(n); });
- }
+ 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);
+}
- ~RleTaskScheduler() {
- for (auto& e : _q)
- e.done();
+VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
+{
+ rle.reset();
+ if (stroke) { // Stroke Task
+ outRef.convert(path);
+ outRef.convert(cap, join, width, meterLimit);
- for (auto& e : _threads)
- e.join();
- }
+ uint points, contors;
- std::future<VRle> async(RleTask *task) {
- auto receiver = std::move(task->receiver);
- auto i = _index++;
+ 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);
- for (unsigned n = 0; n != _count; ++n) {
- if (_q[(i + n) % _count].try_push(task)) return std::move(receiver);
- }
+ outRef.grow(points, contors);
- _q[i % _count].push(task);
+ SW_FT_Stroker_Export(stroker, &outRef.ft);
- return std::move(receiver);
+ } 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;
}
- std::future<VRle> strokeRle(FTOutline *outline,
- SW_FT_Stroker_LineCap cap,
- SW_FT_Stroker_LineJoin join,
- int width,
- int meterLimit,
- SW_FT_Bool closed) {
- RleTask *task = new RleTask();
- task->stroke = true;
- task->outline = outline;
- task->cap = cap;
- task->join = join;
- task->width = width;
- task->meterLimit = meterLimit;
- task->closed = closed;
- return async(task);
- }
+ render(outRef);
- std::future<VRle> fillRle(FTOutline *outline) {
- RleTask *task = new RleTask();
- task->stroke = false;
- task->outline = outline;
- return async(task);
- }
-};
+ path = VPath();
-static RleTaskScheduler raster_scheduler;
+ return std::move(rle);
+}
-static VRle generateRleAsync(RleTask *task)
-{
- if (task->stroke) {
- // for stroke generation
+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};
+
+ void run(unsigned i)
+ {
+ /*
+ * initalize per thread objects.
+ */
+ FTOutline outlineRef;
SW_FT_Stroker stroker;
SW_FT_Stroker_New(&stroker);
- uint points,contors;
- SW_FT_Outline strokeOutline = { 0, 0, nullptr, nullptr, nullptr, SW_FT_OUTLINE_NONE };
+ // Task Loop
+ RleTask task;
+ while (true) {
+ bool success = false;
- SW_FT_Stroker_Set(stroker, task->width, task->cap, task->join, task->meterLimit);
- SW_FT_Stroker_ParseOutline(stroker, &task->outline->ft, !task->closed);
- SW_FT_Stroker_GetCounts(stroker,&points, &contors);
+ for (unsigned n = 0; n != _count * 32; ++n) {
+ if (_q[(i + n) % _count].try_pop(task)) {
+ success = true;
+ break;
+ }
+ }
- 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));
+ if (!success && !_q[i].pop(task)) break;
- SW_FT_Stroker_Export(stroker, &strokeOutline);
+ task.mRlePromise->set_value((task)(outlineRef, stroker));
+ }
+ // cleanup
SW_FT_Stroker_Done(stroker);
+ }
- VRle rle;
- 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 = &strokeOutline;
-
- sw_ft_grays_raster.raster_render(nullptr, ¶ms);
-
- // cleanup the outline data.
- free(strokeOutline.points);
- free(strokeOutline.tags);
- free(strokeOutline.contours);
-
- return rle;
- } else {
- // fill generation
- VRle rle;
- 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 = &task->outline->ft;
-
- sw_ft_grays_raster.raster_render(nullptr, ¶ms);
-
- return rle;
+ RleTaskScheduler()
+ {
+ for (unsigned n = 0; n != _count; ++n) {
+ _threads.emplace_back([&, n] { run(n); });
+ }
+ }
+public:
+ static RleTaskScheduler& instance()
+ {
+ static RleTaskScheduler singleton;
+ return singleton;
}
-}
-VRaster::VRaster()
-{
-}
+ ~RleTaskScheduler()
+ {
+ for (auto &e : _q) e.done();
-VRaster::~VRaster()
-{
-}
+ for (auto &e : _threads) e.join();
+ }
-void VRaster::deleteFTOutline(FTOutline *outline)
-{
- delete outline;
-}
+ void async(RleTask &&task)
+ {
+ auto i = _index++;
-FTOutline *VRaster::toFTOutline(const VPath &path)
-{
- if (path.isEmpty())
- return nullptr;
+ for (unsigned n = 0; n != _count; ++n) {
+ if (_q[(i + n) % _count].try_push(std::move(task))) return;
+ }
- const std::vector<VPath::Element> &elements = path.elements();
- const std::vector<VPointF> &points = path.points();
+ _q[i % _count].push(std::move(task));
+ }
- FTOutline *outline = new FTOutline(points.size(), path.segments());
+ 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));
+ }
- 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;
- }
+ 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));
}
- outline->end();
- return outline;
-}
+};
-std::future<VRle>
-VRaster::generateFillInfo(FTOutline *outline, FillRule fillRule)
+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;
}
-
- outline->ft.flags = fillRuleFlag;
-
- return std::move(raster_scheduler.fillRle(outline));
+ return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
}
-std::future<VRle>
-VRaster::generateStrokeInfo(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 ftclose = (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 std::move(raster_scheduler.strokeRle(outline, ftCap, ftJoin,
- ftWidth, ftMeterLimit, ftclose));
+ 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