1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "vector_vraster.h"
#include <climits>
#include <cstring>
#include <memory>
#include "config.h"
#include "vector_freetype_v_ft_raster.h"
#include "vector_freetype_v_ft_stroker.h"
#include "vector_vdebug.h"
#include "vector_vmatrix.h"
#include "vector_vpath.h"
#include "vector_vrle.h"
V_BEGIN_NAMESPACE
template <typename T>
class dyn_array {
public:
explicit dyn_array(size_t size)
: mCapacity(size), mData(std::make_unique<T[]>(mCapacity))
{
}
void reserve(size_t size)
{
if (mCapacity > size) return;
mCapacity = size;
mData = std::make_unique<T[]>(mCapacity);
}
T * data() const { return mData.get(); }
dyn_array &operator=(dyn_array &&) noexcept = delete;
private:
size_t mCapacity{0};
std::unique_ptr<T[]> mData{nullptr};
};
struct FTOutline {
public:
void reset();
void grow(size_t, size_t);
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);
SW_FT_Pos TO_FT_COORD(float x)
{
return SW_FT_Pos(x * 64);
} // to freetype 26.6 coordinate.
SW_FT_Outline ft;
bool closed{false};
SW_FT_Stroker_LineCap ftCap;
SW_FT_Stroker_LineJoin ftJoin;
SW_FT_Fixed ftWidth;
SW_FT_Fixed ftMiterLimit;
dyn_array<SW_FT_Vector> mPointMemory{100};
dyn_array<char> mTagMemory{100};
dyn_array<short> mContourMemory{10};
dyn_array<char> mContourFlagMemory{10};
};
void FTOutline::reset()
{
ft.n_points = ft.n_contours = 0;
ft.flags = 0x0;
}
void FTOutline::grow(size_t points, size_t segments)
{
reset();
mPointMemory.reserve(points + segments);
mTagMemory.reserve(points + segments);
mContourMemory.reserve(segments);
mContourFlagMemory.reserve(segments);
ft.points = mPointMemory.data();
ft.tags = mTagMemory.data();
ft.contours = mContourMemory.data();
ft.contours_flag = mContourFlagMemory.data();
}
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());
size_t 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;
}
}
end();
}
void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
float miterLimit)
{
// map strokeWidth to freetype. It uses as the radius of the pen not the
// diameter
width = width / 2.0f;
// 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
ftMiterLimit = SW_FT_Fixed(miterLimit * (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_FIXED;
break;
}
}
void FTOutline::moveTo(const VPointF &pt)
{
assert(ft.n_points <= SHRT_MAX - 1);
ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
if (ft.n_points) {
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++;
}
void FTOutline::lineTo(const VPointF &pt)
{
assert(ft.n_points <= SHRT_MAX - 1);
ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
}
void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
const VPointF ep)
{
assert(ft.n_points <= SHRT_MAX - 3);
ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
ft.n_points++;
ft.points[ft.n_points].x = TO_FT_COORD(cp2.x());
ft.points[ft.n_points].y = TO_FT_COORD(cp2.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
ft.n_points++;
ft.points[ft.n_points].x = TO_FT_COORD(ep.x());
ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
}
void FTOutline::close()
{
assert(ft.n_points <= SHRT_MAX - 1);
// 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;
} else {
index = 0;
}
// make sure atleast 1 point exists in the segment.
if (ft.n_points == index) {
closed = false;
return;
}
ft.points[ft.n_points].x = ft.points[index].x;
ft.points[ft.n_points].y = ft.points[index].y;
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
}
void FTOutline::end()
{
assert(ft.n_contours <= SHRT_MAX - 1);
if (ft.n_points) {
ft.contours[ft.n_contours] = ft.n_points - 1;
ft.n_contours++;
}
}
static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user)
{
VRle *rle = static_cast<VRle *>(user);
auto *rleSpan = reinterpret_cast<const VRle::Span *>(spans);
rle->addSpan(rleSpan, count);
}
static void bboxCb(int x, int y, int w, int h, void *user)
{
VRle *rle = static_cast<VRle *>(user);
rle->setBoundingRect({x, y, w, h});
}
class SharedRle {
public:
SharedRle() = default;
VRle &unsafe() { return _rle; }
void notify()
{
{
std::lock_guard<std::mutex> lock(_mutex);
_ready = true;
}
_cv.notify_one();
}
void wait()
{
if (!_pending) return;
{
std::unique_lock<std::mutex> lock(_mutex);
while (!_ready) _cv.wait(lock);
}
_pending = false;
}
VRle &get()
{
wait();
return _rle;
}
void reset()
{
wait();
_ready = false;
_pending = true;
}
private:
VRle _rle;
std::mutex _mutex;
std::condition_variable _cv;
bool _ready{true};
bool _pending{false};
};
struct VRleTask {
SharedRle mRle;
VPath mPath;
float mStrokeWidth;
float mMiterLimit;
VRect mClip;
FillRule mFillRule;
CapStyle mCap;
JoinStyle mJoin;
bool mGenerateStroke;
VRle &rle() { return mRle.get(); }
void update(VPath path, FillRule fillRule, const VRect &clip)
{
mRle.reset();
mPath = std::move(path);
mFillRule = fillRule;
mClip = clip;
mGenerateStroke = false;
}
void update(VPath path, CapStyle cap, JoinStyle join, float width,
float miterLimit, const VRect &clip)
{
mRle.reset();
mPath = std::move(path);
mCap = cap;
mJoin = join;
mStrokeWidth = width;
mMiterLimit = miterLimit;
mClip = clip;
mGenerateStroke = true;
}
void render(FTOutline &outRef)
{
SW_FT_Raster_Params params;
mRle.unsafe().reset();
params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
params.gray_spans = &rleGenerationCb;
params.bbox_cb = &bboxCb;
params.user = &mRle.unsafe();
params.source = &outRef.ft;
if (!mClip.empty()) {
params.flags |= SW_FT_RASTER_FLAG_CLIP;
params.clip_box.xMin = mClip.left();
params.clip_box.yMin = mClip.top();
params.clip_box.xMax = mClip.right();
params.clip_box.yMax = mClip.bottom();
}
// compute rle
sw_ft_grays_raster.raster_render(nullptr, ¶ms);
}
void operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
{
if (mPath.points().size() > SHRT_MAX ||
mPath.points().size() + mPath.segments() > SHRT_MAX) {
return;
}
if (mGenerateStroke) { // Stroke Task
outRef.convert(mPath);
outRef.convert(mCap, mJoin, mStrokeWidth, mMiterLimit);
uint points, contors;
SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap,
outRef.ftJoin, outRef.ftMiterLimit);
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);
} else { // Fill Task
outRef.convert(mPath);
int fillRuleFlag = SW_FT_OUTLINE_NONE;
switch (mFillRule) {
case FillRule::EvenOdd:
fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
break;
default:
fillRuleFlag = SW_FT_OUTLINE_NONE;
break;
}
outRef.ft.flags = fillRuleFlag;
}
render(outRef);
mPath = VPath();
mRle.notify();
}
};
using VTask = std::shared_ptr<VRleTask>;
#ifdef LOTTIE_THREAD_SUPPORT
#include <thread>
#include "vector_vtaskqueue.h"
class RleTaskScheduler {
const unsigned _count{std::thread::hardware_concurrency()};
std::vector<std::thread> _threads;
std::vector<TaskQueue<VTask>> _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);
// Task Loop
VTask task;
while (true) {
bool success = false;
for (unsigned n = 0; n != _count * 2; ++n) {
if (_q[(i + n) % _count].try_pop(task)) {
success = true;
break;
}
}
if (!success && !_q[i].pop(task)) break;
(*task)(outlineRef, stroker);
}
// cleanup
SW_FT_Stroker_Done(stroker);
}
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();
for (auto &e : _threads) e.join();
}
void process(VTask task)
{
auto i = _index++;
for (unsigned n = 0; n != _count; ++n) {
if (_q[(i + n) % _count].try_push(std::move(task))) return;
}
if (_count > 0) {
_q[i % _count].push(std::move(task));
}
}
};
#else
class RleTaskScheduler {
public:
FTOutline outlineRef{};
SW_FT_Stroker stroker;
public:
static RleTaskScheduler &instance()
{
static RleTaskScheduler singleton;
return singleton;
}
RleTaskScheduler() { SW_FT_Stroker_New(&stroker); }
~RleTaskScheduler() { SW_FT_Stroker_Done(stroker); }
void process(VTask task) { (*task)(outlineRef, stroker); }
};
#endif
struct VRasterizer::VRasterizerImpl {
VRleTask mTask;
VRle & rle() { return mTask.rle(); }
VRleTask &task() { return mTask; }
};
VRle VRasterizer::rle()
{
if (!d) return VRle();
return d->rle();
}
void VRasterizer::init()
{
if (!d) d = std::make_shared<VRasterizerImpl>();
}
void VRasterizer::updateRequest()
{
VTask taskObj = VTask(d, &d->task());
RleTaskScheduler::instance().process(std::move(taskObj));
}
void VRasterizer::rasterize(VPath path, FillRule fillRule, const VRect &clip)
{
init();
if (path.empty()) {
d->rle().reset();
return;
}
d->task().update(std::move(path), fillRule, clip);
updateRequest();
}
void VRasterizer::rasterize(VPath path, CapStyle cap, JoinStyle join,
float width, float miterLimit, const VRect &clip)
{
init();
if (path.empty() || vIsZero(width)) {
d->rle().reset();
return;
}
d->task().update(std::move(path), cap, join, width, miterLimit, clip);
updateRequest();
}
V_END_NAMESPACE
|