-
Notifications
You must be signed in to change notification settings - Fork 7
/
imottie_renderer.cpp
8099 lines (7457 loc) · 250 KB
/
imottie_renderer.cpp
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Thanks for Samsung Electronics for amazing rlottie library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "freetype/v_ft_raster.h"
#include "freetype/v_ft_stroker.h"
#include "rapidjson/document.h"
#include "rapidjson/stream.h"
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STBI_NO_GIF
#define STBI_NO_PIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "imlottie_impl.h"
#include <fstream>
#include <mutex>
#include <condition_variable>
namespace imlottie {
std::shared_ptr<Animation> animationLoad(const char *path) {
return Animation::loadFromFile(path, false
/*not use cache*/
);
}
uint16_t animationTotalFrame(const std::shared_ptr<Animation> &anim) {
return anim->totalFrame();
}
double animationDuration(const std::shared_ptr<Animation> &anim) {
return anim->duration();
}
void animationRenderSync (const std::shared_ptr<Animation> &anim, int nextFrameIndex, uint32_t *data, int width, int height, int row_pitch) {
Surface surface(data, width, height, row_pitch);
// rasterize frame to nextFrame.data, imlottie::Surface is temporary
// structure which not save any data
anim->renderSync(nextFrameIndex, surface);
}
} // ImGui
namespace imlottie {
enum class Operation {Add, Xor};
struct VRleHelper {
size_t alloc {0};
size_t size {0};
VRle::Span *spans {nullptr};
};
static void rleIntersectWithRle(VRleHelper *, int, int, VRleHelper *, VRleHelper *);
static void rleIntersectWithRect(const VRect &, VRleHelper *, VRleHelper *);
static void rleOpGeneric(VRleHelper *, VRleHelper *, VRleHelper *, Operation op);
static void rleSubstractWithRle(VRleHelper *, VRleHelper *, VRleHelper *);
static inline uchar divBy255(int x) { return (x + (x >> 8) + 0x80) >> 8; }
inline static void copyArrayToVector(const VRle::Span *span, size_t count, std::vector<VRle::Span> &v) {
// make sure enough memory available
if (v.capacity() < v.size() + count) v.reserve(v.size() + count);
std::copy(span, span + count, std::back_inserter(v));
}
void VRle::VRleData::addSpan(const VRle::Span *span, size_t count) {
copyArrayToVector (span, count, mSpans);
mBboxDirty = true;
}
VRect VRle::VRleData::bbox() const {
updateBbox();
return mBbox;
}
void VRle::VRleData::setBbox(const VRect &bbox) const {
mBboxDirty = false;
mBbox = bbox;
}
void VRle::VRleData::reset() {
mSpans.clear();
mBbox = VRect();
mOffset = VPoint();
mBboxDirty = false;
}
void VRle::VRleData::clone(const VRle::VRleData &o) {
*this = o;
}
void VRle::VRleData::translate(const VPoint &p) {
// take care of last offset if applied
mOffset = p - mOffset;
int x = mOffset.x();
int y = mOffset.y();
for (auto &i : mSpans) {
i.x = i.x + x;
i.y = i.y + y;
}
updateBbox();
mBbox.translate(mOffset.x(), mOffset.y());
}
void VRle::VRleData::addRect(const VRect &rect) {
int x = rect.left();
int y = rect.top();
int width = rect.width();
int height = rect.height();
mSpans.reserve(size_t(height));
VRle::Span span;
for (int i = 0; i < height; i++) {
span.x = x;
span.y = y + i;
span.len = width;
span.coverage = 255;
mSpans.push_back(span);
}
updateBbox();
}
void VRle::VRleData::updateBbox() const {
if (!mBboxDirty) return;
mBboxDirty = false;
int l = std::numeric_limits<int>::max();
const VRle::Span *span = mSpans.data();
mBbox = VRect();
size_t sz = mSpans.size();
if (sz) {
int t = span[0].y;
int b = span[sz - 1].y;
int r = 0;
for (size_t i = 0; i < sz; i++) {
if (span[i].x < l) l = span[i].x;
if (span[i].x + span[i].len > r) r = span[i].x + span[i].len;
}
mBbox = VRect(l, t, r - l, b - t + 1);
}
}
void VRle::VRleData::invert() {
for (auto &i : mSpans) {
i.coverage = 255 - i.coverage;
}
}
void VRle::VRleData::operator*=(uchar alpha) {
for (auto &i : mSpans) {
i.coverage = divBy255(i.coverage * alpha);
}
}
void VRle::VRleData::opIntersect(const VRect &r, VRle::VRleSpanCb cb,
void *userData) const {
if (empty()) return;
if (r.contains(bbox())) {
cb(mSpans.size(), mSpans.data(), userData);
return;
}
VRect clip = r;
VRleHelper tresult, tmp_obj;
std::array<VRle::Span, 256> array;
// setup the tresult object
tresult.size = array.size();
tresult.alloc = array.size();
tresult.spans = array.data();
// setup tmp object
tmp_obj.size = mSpans.size();
tmp_obj.spans = const_cast<VRle::Span *>(mSpans.data());
// run till all the spans are processed
while (tmp_obj.size) {
rleIntersectWithRect(clip, &tmp_obj, &tresult);
if (tresult.size) {
cb(tresult.size, tresult.spans, userData);
}
tresult.size = 0;
}
}
// res = a - b;
void VRle::VRleData::opSubstract(const VRle::VRleData &a,
const VRle::VRleData &b) {
// if two rle are disjoint
if (!a.bbox().intersects(b.bbox())) {
mSpans = a.mSpans;
} else {
VRle::Span * aPtr = const_cast<VRle::Span *>(a.mSpans.data());
const VRle::Span *aEnd = a.mSpans.data() + a.mSpans.size();
VRle::Span * bPtr = const_cast<VRle::Span *>(b.mSpans.data());
const VRle::Span *bEnd = b.mSpans.data() + b.mSpans.size();
// 1. forward till both y intersect
while ((aPtr != aEnd) && (aPtr->y < bPtr->y)) aPtr++;
size_t sizeA = size_t(aPtr - a.mSpans.data());
if (sizeA) copyArrayToVector(a.mSpans.data(), sizeA, mSpans);
// 2. forward b till it intersect with a.
while ((bPtr != bEnd) && (bPtr->y < aPtr->y)) bPtr++;
size_t sizeB = size_t(bPtr - b.mSpans.data());
// 2. calculate the intersect region
VRleHelper tresult, aObj, bObj;
std::array<VRle::Span, 256> array;
// setup the tresult object
tresult.size = array.size();
tresult.alloc = array.size();
tresult.spans = array.data();
// setup a object
aObj.size = a.mSpans.size() - sizeA;
aObj.spans = aPtr;
// setup b object
bObj.size = b.mSpans.size() - sizeB;
bObj.spans = bPtr;
// run till all the spans are processed
while (aObj.size && bObj.size) {
rleSubstractWithRle(&aObj, &bObj, &tresult);
if (tresult.size) {
copyArrayToVector(tresult.spans, tresult.size, mSpans);
}
tresult.size = 0;
}
// 3. copy the rest of a
if (aObj.size) copyArrayToVector(aObj.spans, aObj.size, mSpans);
}
mBboxDirty = true;
}
void VRle::VRleData::opGeneric(const VRle::VRleData &a, const VRle::VRleData &b,
OpCode code) {
// This routine assumes, obj1(span_y) < obj2(span_y).
// reserve some space for the result vector.
mSpans.reserve(a.mSpans.size() + b.mSpans.size());
// if two rle are disjoint
if (!a.bbox().intersects(b.bbox())) {
if (a.mSpans[0].y < b.mSpans[0].y) {
copyArrayToVector(a.mSpans.data(), a.mSpans.size(), mSpans);
copyArrayToVector(b.mSpans.data(), b.mSpans.size(), mSpans);
} else {
copyArrayToVector(b.mSpans.data(), b.mSpans.size(), mSpans);
copyArrayToVector(a.mSpans.data(), a.mSpans.size(), mSpans);
}
} else {
VRle::Span * aPtr = const_cast<VRle::Span *>(a.mSpans.data());
const VRle::Span *aEnd = a.mSpans.data() + a.mSpans.size();
VRle::Span * bPtr = const_cast<VRle::Span *>(b.mSpans.data());
const VRle::Span *bEnd = b.mSpans.data() + b.mSpans.size();
// 1. forward a till it intersects with b
while ((aPtr != aEnd) && (aPtr->y < bPtr->y)) aPtr++;
size_t sizeA = size_t(aPtr - a.mSpans.data());
if (sizeA) copyArrayToVector(a.mSpans.data(), sizeA, mSpans);
// 2. forward b till it intersects with a
while ((bPtr != bEnd) && (bPtr->y < aPtr->y)) bPtr++;
size_t sizeB = size_t(bPtr - b.mSpans.data());
if (sizeB) copyArrayToVector(b.mSpans.data(), sizeB, mSpans);
// 3. calculate the intersect region
VRleHelper tresult, aObj, bObj;
std::array<VRle::Span, 256> array;
// setup the tresult object
tresult.size = array.size();
tresult.alloc = array.size();
tresult.spans = array.data();
// setup a object
aObj.size = a.mSpans.size() - sizeA;
aObj.spans = aPtr;
// setup b object
bObj.size = b.mSpans.size() - sizeB;
bObj.spans = bPtr;
Operation op = Operation::Add;
switch (code) {
case OpCode::Add:
op = Operation::Add;
break;
case OpCode::Xor:
op = Operation::Xor;
break;
}
// run till all the spans are processed
while (aObj.size && bObj.size) {
rleOpGeneric(&aObj, &bObj, &tresult, op);
if (tresult.size) {
copyArrayToVector(tresult.spans, tresult.size, mSpans);
}
tresult.size = 0;
}
// 3. copy the rest
if (bObj.size) copyArrayToVector(bObj.spans, bObj.size, mSpans);
if (aObj.size) copyArrayToVector(aObj.spans, aObj.size, mSpans);
}
mBboxDirty = true;
}
static void rle_cb(size_t count, const VRle::Span *spans, void *userData) {
auto vector = static_cast<std::vector<VRle::Span> *>(userData);
copyArrayToVector(spans, count, *vector);
}
void opIntersectHelper(const VRle::VRleData &obj1, const VRle::VRleData &obj2,
VRle::VRleSpanCb cb, void *userData) {
VRleHelper result, source, clip;
std::array<VRle::Span, 256> array;
// setup the tresult object
result.size = array.size();
result.alloc = array.size();
result.spans = array.data();
// setup tmp object
source.size = obj1.mSpans.size();
source.spans = const_cast<VRle::Span *>(obj1.mSpans.data());
// setup tmp clip object
clip.size = obj2.mSpans.size();
clip.spans = const_cast<VRle::Span *>(obj2.mSpans.data());
// run till all the spans are processed
while (source.size) {
rleIntersectWithRle(&clip, 0, 0, &source, &result);
if (result.size) {
cb(result.size, result.spans, userData);
}
result.size = 0;
}
}
void VRle::VRleData::opIntersect(const VRle::VRleData &obj1,
const VRle::VRleData &obj2) {
opIntersectHelper(obj1, obj2, rle_cb, &mSpans);
updateBbox();
}
#define VMIN(a, b) ((a) < (b) ? (a) : (b))
#define VMAX(a, b) ((a) > (b) ? (a) : (b))
/*
* This function will clip a rle list with another rle object
* tmp_clip : The rle list that will be use to clip the rle
* tmp_obj : holds the list of spans that has to be clipped
* result : will hold the result after the processing
* NOTE: if the algorithm runs out of the result buffer list
* it will stop and update the tmp_obj with the span list
* that are yet to be processed as well as the tpm_clip object
* with the unprocessed clip spans.
*/
static void rleIntersectWithRle(VRleHelper *tmp_clip, int clip_offset_x,
int clip_offset_y, VRleHelper *tmp_obj,
VRleHelper *result) {
VRle::Span *out = result->spans;
size_t available = result->alloc;
VRle::Span *spans = tmp_obj->spans;
VRle::Span *end = tmp_obj->spans + tmp_obj->size;
VRle::Span *clipSpans = tmp_clip->spans;
VRle::Span *clipEnd = tmp_clip->spans + tmp_clip->size;
int sx1, sx2, cx1, cx2, x, len;
while (available && spans < end) {
if (clipSpans >= clipEnd) {
spans = end;
break;
}
if ((clipSpans->y + clip_offset_y) > spans->y) {
++spans;
continue;
}
if (spans->y != (clipSpans->y + clip_offset_y)) {
++clipSpans;
continue;
}
// assert(spans->y == (clipSpans->y + clip_offset_y));
sx1 = spans->x;
sx2 = sx1 + spans->len;
cx1 = (clipSpans->x + clip_offset_x);
cx2 = cx1 + clipSpans->len;
if (cx1 < sx1 && cx2 < sx1) {
++clipSpans;
continue;
} else if (sx1 < cx1 && sx2 < cx1) {
++spans;
continue;
}
x = std::max(sx1, cx1);
len = std::min(sx2, cx2) - x;
if (len) {
out->x = std::max(sx1, cx1);
out->len = (std::min(sx2, cx2) - out->x);
out->y = spans->y;
out->coverage = divBy255(spans->coverage * clipSpans->coverage);
++out;
--available;
}
if (sx2 < cx2) {
++spans;
} else {
++clipSpans;
}
}
// update the span list that yet to be processed
tmp_obj->spans = spans;
tmp_obj->size = end - spans;
// update the clip list that yet to be processed
tmp_clip->spans = clipSpans;
tmp_clip->size = clipEnd - clipSpans;
// update the result
result->size = result->alloc - available;
}
/*
* This function will clip a rle list with a given rect
* clip : The clip rect that will be use to clip the rle
* tmp_obj : holds the list of spans that has to be clipped
* result : will hold the result after the processing
* NOTE: if the algorithm runs out of the result buffer list
* it will stop and update the tmp_obj with the span list
* that are yet to be processed
*/
static void rleIntersectWithRect(const VRect &clip, VRleHelper *tmp_obj,
VRleHelper *result) {
VRle::Span *out = result->spans;
size_t available = result->alloc;
VRle::Span *spans = tmp_obj->spans;
VRle::Span *end = tmp_obj->spans + tmp_obj->size;
short minx, miny, maxx, maxy;
minx = clip.left();
miny = clip.top();
maxx = clip.right() - 1;
maxy = clip.bottom() - 1;
while (available && spans < end) {
if (spans->y > maxy) {
spans = end;
// update spans so that we can breakout
break;
}
if (spans->y < miny || spans->x > maxx ||
spans->x + spans->len <= minx) {
++spans;
continue;
}
if (spans->x < minx) {
out->len = VMIN(spans->len - (minx - spans->x), maxx - minx + 1);
out->x = minx;
} else {
out->x = spans->x;
out->len = VMIN(spans->len, (maxx - spans->x + 1));
}
if (out->len != 0) {
out->y = spans->y;
out->coverage = spans->coverage;
++out;
--available;
}
++spans;
}
// update the span list that yet to be processed
tmp_obj->spans = spans;
tmp_obj->size = end - spans;
// update the result
result->size = result->alloc - available;
}
void blitXor(VRle::Span *spans, int count, uchar *buffer, int offsetX) {
while (count--) {
int x = spans->x + offsetX;
int l = spans->len;
uchar *ptr = buffer + x;
while (l--) {
int da = *ptr;
*ptr = divBy255((255 - spans->coverage) * (da) +
spans->coverage * (255 - da));
ptr++;
}
spans++;
}
}
void blitDestinationOut(VRle::Span *spans, int count, uchar *buffer,
int offsetX) {
while (count--) {
int x = spans->x + offsetX;
int l = spans->len;
uchar *ptr = buffer + x;
while (l--) {
*ptr = divBy255((255 - spans->coverage) * (*ptr));
ptr++;
}
spans++;
}
}
void blitSrcOver(VRle::Span *spans, int count, uchar *buffer, int offsetX) {
while (count--) {
int x = spans->x + offsetX;
int l = spans->len;
uchar *ptr = buffer + x;
while (l--) {
*ptr = spans->coverage + divBy255((255 - spans->coverage) * (*ptr));
ptr++;
}
spans++;
}
}
void blit(VRle::Span *spans, int count, uchar *buffer, int offsetX) {
while (count--) {
int x = spans->x + offsetX;
int l = spans->len;
uchar *ptr = buffer + x;
while (l--) {
*ptr = std::max(spans->coverage, *ptr);
ptr++;
}
spans++;
}
}
size_t bufferToRle(uchar *buffer, int size, int offsetX, int y, VRle::Span *out) {
size_t count = 0;
uchar value = buffer[0];
int curIndex = 0;
size = offsetX < 0 ? size + offsetX : size;
for (int i = 0; i < size; i++) {
uchar curValue = buffer[0];
if (value != curValue) {
if (value) {
out->y = y;
out->x = offsetX + curIndex;
out->len = i - curIndex;
out->coverage = value;
out++;
count++;
}
curIndex = i;
value = curValue;
}
buffer++;
}
if (value) {
out->y = y;
out->x = offsetX + curIndex;
out->len = size - curIndex;
out->coverage = value;
count++;
}
return count;
}
static void rleOpGeneric(VRleHelper *a, VRleHelper *b, VRleHelper *result,
Operation op) {
std::array<VRle::Span, 256> temp;
VRle::Span * out = result->spans;
size_t available = result->alloc;
VRle::Span * aPtr = a->spans;
VRle::Span * aEnd = a->spans + a->size;
VRle::Span * bPtr = b->spans;
VRle::Span * bEnd = b->spans + b->size;
while (available && aPtr < aEnd && bPtr < bEnd) {
if (aPtr->y < bPtr->y) {
*out++ = *aPtr++;
available--;
} else if (bPtr->y < aPtr->y) {
*out++ = *bPtr++;
available--;
} else {
// same y
VRle::Span *aStart = aPtr;
VRle::Span *bStart = bPtr;
int y = aPtr->y;
while (aPtr < aEnd && aPtr->y == y) aPtr++;
while (bPtr < bEnd && bPtr->y == y) bPtr++;
int aLength = (aPtr - 1)->x + (aPtr - 1)->len;
int bLength = (bPtr - 1)->x + (bPtr - 1)->len;
int offset = std::min(aStart->x, bStart->x);
std::array<uchar, 1024> array = { {
0
}
}
;
blit(aStart, (aPtr - aStart), array.data(), -offset);
if (op == Operation::Add)
blitSrcOver(bStart, (bPtr - bStart), array.data(), -offset); else if (op == Operation::Xor)
blitXor(bStart, (bPtr - bStart), array.data(), -offset);
VRle::Span *tResult = temp.data();
size_t size = bufferToRle(array.data(), std::max(aLength, bLength),
offset, y, tResult);
if (available >= size) {
while (size--) {
*out++ = *tResult++;
available--;
}
} else {
aPtr = aStart;
bPtr = bStart;
break;
}
}
}
// update the span list that yet to be processed
a->spans = aPtr;
a->size = aEnd - aPtr;
// update the clip list that yet to be processed
b->spans = bPtr;
b->size = bEnd - bPtr;
// update the result
result->size = result->alloc - available;
}
static void rleSubstractWithRle(VRleHelper *a, VRleHelper *b,
VRleHelper *result) {
std::array<VRle::Span, 256> temp;
VRle::Span * out = result->spans;
size_t available = result->alloc;
VRle::Span * aPtr = a->spans;
VRle::Span * aEnd = a->spans + a->size;
VRle::Span * bPtr = b->spans;
VRle::Span * bEnd = b->spans + b->size;
while (available && aPtr < aEnd && bPtr < bEnd) {
if (aPtr->y < bPtr->y) {
*out++ = *aPtr++;
available--;
} else if (bPtr->y < aPtr->y) {
bPtr++;
} else {
// same y
VRle::Span *aStart = aPtr;
VRle::Span *bStart = bPtr;
int y = aPtr->y;
while (aPtr < aEnd && aPtr->y == y) aPtr++;
while (bPtr < bEnd && bPtr->y == y) bPtr++;
int aLength = (aPtr - 1)->x + (aPtr - 1)->len;
int bLength = (bPtr - 1)->x + (bPtr - 1)->len;
int offset = std::min(aStart->x, bStart->x);
std::array<uchar, 1024> array = { {
0
}
}
;
blit(aStart, (aPtr - aStart), array.data(), -offset);
blitDestinationOut(bStart, (bPtr - bStart), array.data(), -offset);
VRle::Span *tResult = temp.data();
size_t size = bufferToRle(array.data(), std::max(aLength, bLength),
offset, y, tResult);
if (available >= size) {
while (size--) {
*out++ = *tResult++;
available--;
}
} else {
aPtr = aStart;
bPtr = bStart;
break;
}
}
}
// update the span list that yet to be processed
a->spans = aPtr;
a->size = size_t(aEnd - aPtr);
// update the clip list that yet to be processed
b->spans = bPtr;
b->size = size_t(bEnd - bPtr);
// update the result
result->size = result->alloc - available;
}
VRle VRle::toRle(const VRect &rect) {
if (rect.empty()) return VRle();
VRle result;
result.d.write().addRect(rect);
return result;
}
/*
* this api makes use of thread_local temporary
* buffer to avoid creating intermediate temporary rle buffer
* the scratch buffer object will grow its size on demand
* so that future call won't need any more memory allocation.
* this function is thread safe as it uses thread_local variable
* which is unique per thread.
*/
static thread_local VRle::VRleData Scratch_Object;
void VRle::operator&=(const VRle &o) {
if (empty()) return;
if (o.empty()) {
reset();
return;
}
Scratch_Object.reset();
Scratch_Object.opIntersect(d.read(), o.d.read());
d.write() = Scratch_Object;
}
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;