4 #include "v_ft_raster.h"
5 #include "v_ft_stroker.h"
9 #include "vtaskqueue.h"
18 if (mPointSize) delete[] ft.points;
19 if (mTagSize) delete[] ft.tags;
20 if (mSegmentSize) delete[] ft.contours;
24 void convert(const VPath &path);
25 void convert(CapStyle, JoinStyle, float, float);
26 void moveTo(const VPointF &pt);
27 void lineTo(const VPointF &pt);
28 void cubicTo(const VPointF &ctr1, const VPointF &ctr2, const VPointF end);
31 void transform(const VMatrix &m);
37 SW_FT_Stroker_LineCap ftCap;
38 SW_FT_Stroker_LineJoin ftJoin;
40 SW_FT_Fixed ftMeterLimit;
44 void FTOutline::reset()
46 ft.n_points = ft.n_contours = 0;
50 void FTOutline::grow(int points, int segments)
54 int point_size = (points + segments);
55 int segment_size = (sizeof(short) * segments);
56 int tag_size = (sizeof(char) * (points + segments));
58 if (point_size > mPointSize) {
59 if (mPointSize) delete [] ft.points;
60 ft.points = new SW_FT_Vector[point_size];
61 mPointSize = point_size;
64 if (segment_size > mSegmentSize) {
65 if (mSegmentSize) delete [] ft.contours;
66 ft.contours = new short[segment_size];
67 mSegmentSize = segment_size;
70 if (tag_size > mTagSize) {
71 if (mTagSize) delete [] ft.tags;
72 ft.tags = new char[tag_size];
77 void FTOutline::convert(const VPath &path)
79 const std::vector<VPath::Element> &elements = path.elements();
80 const std::vector<VPointF> & points = path.points();
82 grow(points.size(), path.segments());
85 for (auto element : elements) {
87 case VPath::Element::MoveTo:
88 moveTo(points[index]);
91 case VPath::Element::LineTo:
92 lineTo(points[index]);
95 case VPath::Element::CubicTo:
96 cubicTo(points[index], points[index + 1], points[index + 2]);
99 case VPath::Element::Close:
109 void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
112 ftClosed = (SW_FT_Bool)closed;
114 // map strokeWidth to freetype. It uses as the radius of the pen not the
117 // convert to freetype co-ordinate
118 // IMP: stroker takes radius in 26.6 co-ordinate
119 ftWidth = SW_FT_Fixed(width * (1 << 6));
120 // IMP: stroker takes meterlimit in 16.16 co-ordinate
121 ftMeterLimit = SW_FT_Fixed(meterLimit * (1 << 16));
123 // map to freetype capstyle
125 case CapStyle::Square:
126 ftCap = SW_FT_STROKER_LINECAP_SQUARE;
128 case CapStyle::Round:
129 ftCap = SW_FT_STROKER_LINECAP_ROUND;
132 ftCap = SW_FT_STROKER_LINECAP_BUTT;
136 case JoinStyle::Bevel:
137 ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
139 case JoinStyle::Round:
140 ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
143 ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
148 #define TO_FT_COORD(x) ((x)*64) // to freetype 26.6 coordinate.
150 void FTOutline::moveTo(const VPointF &pt)
152 ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
153 ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
154 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
156 ft.contours[ft.n_contours] = ft.n_points - 1;
163 void FTOutline::lineTo(const VPointF &pt)
165 ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
166 ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
167 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
172 void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
175 ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
176 ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
177 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
180 ft.points[ft.n_points].x = TO_FT_COORD(cp2.x());
181 ft.points[ft.n_points].y = TO_FT_COORD(cp2.y());
182 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
185 ft.points[ft.n_points].x = TO_FT_COORD(ep.x());
186 ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
187 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
191 void FTOutline::close()
195 index = ft.contours[ft.n_contours - 1] + 1;
200 // make sure atleast 1 point exists in the segment.
201 if (ft.n_points == index) {
206 ft.points[ft.n_points].x = ft.points[index].x;
207 ft.points[ft.n_points].y = ft.points[index].y;
208 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
213 void FTOutline::end()
216 ft.contours[ft.n_contours] = ft.n_points - 1;
226 static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user)
228 VRle * rle = (VRle *)user;
229 VRle::Span *rleSpan = (VRle::Span *)spans;
230 rle->addSpan(rleSpan, count);
234 RleTask() { receiver = sender.get_future(); }
235 std::promise<VRle> sender;
236 std::future<VRle> receiver;
245 VRle operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
248 VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
251 if (stroke) { // Stroke Task
252 outRef.convert(path);
253 outRef.convert(cap, join, width, meterLimit);
255 uint points, contors;
257 SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin,
258 outRef.ftMeterLimit);
259 SW_FT_Stroker_ParseOutline(stroker, &outRef.ft, !outRef.ftClosed);
260 SW_FT_Stroker_GetCounts(stroker, &points, &contors);
262 outRef.grow(points, contors);
264 SW_FT_Stroker_Export(stroker, &outRef.ft);
266 SW_FT_Raster_Params params;
268 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
269 params.gray_spans = &rleGenerationCb;
271 params.source = &outRef;
273 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
275 } else { // Fill Task
276 outRef.convert(path);
277 int fillRuleFlag = SW_FT_OUTLINE_NONE;
279 case FillRule::EvenOdd:
280 fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
283 fillRuleFlag = SW_FT_OUTLINE_NONE;
286 outRef.ft.flags = fillRuleFlag;
287 SW_FT_Raster_Params params;
289 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
290 params.gray_spans = &rleGenerationCb;
292 params.source = &outRef.ft;
294 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
296 return std::move(rle);
299 class RleTaskScheduler {
300 const unsigned _count{std::thread::hardware_concurrency()};
301 std::vector<std::thread> _threads;
302 std::vector<TaskQueue<RleTask>> _q{_count};
303 std::atomic<unsigned> _index{0};
308 * initalize per thread objects.
310 FTOutline outlineRef;
311 SW_FT_Stroker stroker;
312 SW_FT_Stroker_New(&stroker);
316 RleTask *task = nullptr;
318 for (unsigned n = 0; n != _count * 32; ++n) {
319 if (_q[(i + n) % _count].try_pop(task)) break;
321 if (!task && !_q[i].pop(task)) break;
323 task->sender.set_value((*task)(outlineRef, stroker));
328 SW_FT_Stroker_Done(stroker);
334 for (unsigned n = 0; n != _count; ++n) {
335 _threads.emplace_back([&, n] { run(n); });
341 for (auto &e : _q) e.done();
343 for (auto &e : _threads) e.join();
346 std::future<VRle> async(RleTask *task)
348 auto receiver = std::move(task->receiver);
351 for (unsigned n = 0; n != _count; ++n) {
352 if (_q[(i + n) % _count].try_push(task)) return receiver;
355 _q[i % _count].push(task);
360 std::future<VRle> strokeRle(VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
361 float width, float meterLimit)
363 RleTask *task = new RleTask();
365 task->path = std::move(path);
366 task->rle = std::move(rle);
370 task->meterLimit = meterLimit;
374 std::future<VRle> fillRle(VPath &&path, VRle &&rle, FillRule fillRule)
376 RleTask *task = new RleTask();
377 task->path = std::move(path);
378 task->rle = std::move(rle);
379 task->fillRule = fillRule;
380 task->stroke = false;
385 static RleTaskScheduler raster_scheduler;
387 std::future<VRle> VRaster::generateFillInfo(VPath &&path, VRle &&rle,
390 if (path.isEmpty()) {
391 std::promise<VRle> promise;
392 promise.set_value(VRle());
393 return promise.get_future();
395 return raster_scheduler.fillRle(std::move(path), std::move(rle), fillRule);
398 std::future<VRle> VRaster::generateStrokeInfo(VPath &&path, VRle &&rle, CapStyle cap,
399 JoinStyle join, float width,
402 if (path.isEmpty()) {
403 std::promise<VRle> promise;
404 promise.set_value(VRle());
405 return promise.get_future();
407 return raster_scheduler.strokeRle(std::move(path), std::move(rle), cap, join, width, meterLimit);