-
Notifications
You must be signed in to change notification settings - Fork 5
/
ovgosd.c
2818 lines (2279 loc) · 65 KB
/
ovgosd.c
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
/*
* rpihddevice - VDR HD output device for Raspberry Pi
* Copyright (C) 2014, 2015, 2016 Thomas Reufer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vector>
#include <queue>
#include <algorithm>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <VG/openvg.h>
#include <VG/vgu.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include "ovgosd.h"
#include "display.h"
#include "omxdevice.h"
#include "setup.h"
#include "tools.h"
/* ------------------------------------------------------------------------- */
// glyphs containing kerning cache, based on VDR's implementation
class cOvgGlyph : public cListObject
{
public:
cOvgGlyph(uint charCode, VGfloat advanceX, VGfloat advanceY) :
m_charCode(charCode), m_advanceX(advanceX), m_advanceY(advanceY) { }
virtual ~cOvgGlyph() { }
uint CharCode(void) { return m_charCode; }
VGfloat AdvanceX(void) { return m_advanceX; }
VGfloat AdvanceY(void) { return m_advanceY; }
bool GetKerningCache(uint prevSym, VGfloat &kerning)
{
for (int i = m_kerningCache.Size(); --i > 0; )
if (m_kerningCache[i].m_prevSym == prevSym)
{
kerning = m_kerningCache[i].m_kerning;
return true;
}
return false;
}
void SetKerningCache(uint prevSym, VGfloat kerning)
{
m_kerningCache.Append(tKerning(prevSym, kerning));
}
private:
struct tKerning
{
public:
tKerning(uint prevSym, VGfloat kerning = 0.0f)
{
m_prevSym = prevSym;
m_kerning = kerning;
}
uint m_prevSym;
VGfloat m_kerning;
};
uint m_charCode;
VGfloat m_advanceX;
VGfloat m_advanceY;
cVector<tKerning> m_kerningCache;
};
/* ------------------------------------------------------------------------- */
#define CHAR_HEIGHT (1 << 14)
class cOvgFont : public cListObject
{
public:
static cOvgFont *Get(const char *name)
{
Init();
cOvgFont *font;
for (font = s_fonts.First(); font; font = s_fonts.Next(font))
if (!strcmp(font->Name(), name))
return font;
font = 0;
bool retry = true;
while (!font)
{
font = new cOvgFont(s_ftLib, name);
if (vgGetError() == VG_OUT_OF_MEMORY_ERROR)
{
delete font;
font = 0;
s_fonts.Clear();
if (!retry)
{
ELOG("[OpenVG] out of memory - failed to load font!");
font = new cOvgFont();
break;
}
retry = false;
}
}
s_fonts.Add(font);
return font;
}
static void CleanUp(void)
{
s_fonts.Clear();
if (FT_Done_FreeType(s_ftLib))
ELOG("failed to deinitialize FreeType library!");
s_ftLib = 0;
}
cOvgGlyph* Glyph(uint charCode) const
{
cOvgGlyph *glyph = 0;
for (glyph = m_glyphs.First(); glyph; glyph = m_glyphs.Next(glyph))
if (glyph->CharCode() == charCode)
return glyph;
glyph = ConvertChar(charCode);
if (glyph)
m_glyphs.Add(glyph);
return glyph;
}
VGfloat Kerning(cOvgGlyph *glyph, uint prevSym) const
{
VGfloat kerning = 0.0f;
if (glyph && prevSym)
{
if (!glyph->GetKerningCache(prevSym, kerning))
{
FT_Vector delta;
FT_UInt cur = FT_Get_Char_Index(m_face, glyph->CharCode());
FT_UInt prev = FT_Get_Char_Index(m_face, prevSym);
FT_Get_Kerning(m_face, prev, cur, FT_KERNING_DEFAULT, &delta);
kerning = (VGfloat)delta.x / CHAR_HEIGHT;
glyph->SetKerningCache(prevSym, kerning);
}
}
return kerning;
}
VGfloat Height(void) { return m_height; }
VGfloat Descender(void) { return m_descender; }
VGFont Font(void) { return m_font; }
const char* Name(void) { return *m_name; }
private:
cOvgFont(void) :
m_font(VG_INVALID_HANDLE),
m_name(""),
m_height(0.0f),
m_descender(0.0f),
m_face(0)
{ }
cOvgFont(FT_Library lib, const char *name) :
m_name(name)
{
ILOG("loading %s ...", *m_name);
if (FT_New_Face(lib, name, 0, &m_face))
ELOG("failed to open %s!", name);
m_font = vgCreateFont(m_face->num_glyphs);
if (m_font == VG_INVALID_HANDLE)
{
ELOG("failed to allocate new OVG font!");
return;
}
FT_Set_Char_Size(m_face, 0, CHAR_HEIGHT, 0, 0);
m_height = (VGfloat)(m_face->size->metrics.height) / CHAR_HEIGHT;
m_descender = (VGfloat)(abs(m_face->size->metrics.descender)) /
CHAR_HEIGHT;
#if 0
FT_UInt glyphIndex;
FT_ULong ch = FT_Get_First_Char(m_face, &glyphIndex);
while (ch != 0)
{
if (FT_Load_Glyph(m_face, glyphIndex, FT_LOAD_DEFAULT))
break;
FT_Outline *ot = &m_face->glyph->outline;
VGPath path = ConvertOutline(ot);
VGfloat origin[] = { 0.0f, 0.0f };
VGfloat esc[] = {
(VGfloat)(m_face->glyph->advance.x) / CHAR_HEIGHT,
(VGfloat)(m_face->glyph->advance.y) / CHAR_HEIGHT
};
vgSetGlyphToPath(m_font, ch, path, VG_FALSE, origin, esc);
m_glyphs.Add(new cOvgGlyph(ch, esc[0], esc[1]));
if (path != VG_INVALID_HANDLE)
vgDestroyPath(path);
ch = FT_Get_Next_Char(m_face, ch, &glyphIndex);
}
#endif
}
~cOvgFont()
{
vgDestroyFont(m_font);
FT_Done_Face(m_face);
}
static void Init(void)
{
if (!s_ftLib && FT_Init_FreeType(&s_ftLib))
ELOG("failed to initialize FreeType library!");
}
cOvgGlyph *ConvertChar(uint charCode) const
{
FT_UInt glyphIndex = FT_Get_Char_Index(m_face, charCode);
if (FT_Load_Glyph(m_face, glyphIndex, FT_LOAD_DEFAULT))
return 0;
FT_Outline *ot = &m_face->glyph->outline;
VGPath path = ConvertOutline(ot);
VGfloat origin[] = { 0.0f, 0.0f };
VGfloat esc[] = {
(VGfloat)(m_face->glyph->advance.x) / CHAR_HEIGHT,
(VGfloat)(m_face->glyph->advance.y) / CHAR_HEIGHT
};
vgSetGlyphToPath(m_font, charCode, path, VG_FALSE, origin, esc);
if (path != VG_INVALID_HANDLE)
vgDestroyPath(path);
return new cOvgGlyph(charCode, esc[0], esc[1]);
}
// convert freetype outline to OpenVG path,
// based on Raspberry Pi's vgfont library
VGPath ConvertOutline(FT_Outline *outline) const
{
if (outline->n_contours == 0)
return VG_INVALID_HANDLE;
std::vector<VGubyte> segments;
std::vector<VGshort> coord;
segments.reserve(256);
coord.reserve(1024);
FT_Vector *points = outline->points;
const char *tags = outline->tags;
const short* contour = outline->contours;
short nCont = outline->n_contours;
for (short point = 0; nCont != 0; contour++, nCont--)
{
short nextContour = *contour + 1;
bool firstTag = true;
VGubyte segment = VG_MOVE_TO;
char lastTag = 0;
short firstPoint = point;
for (; point < nextContour; point++)
{
char tag = tags[point];
FT_Vector fpoint = points[point];
if (firstTag)
firstTag = false;
else if (tag & 0x1)
{
if (lastTag & 0x1)
segment = VG_LINE_TO;
else if (lastTag & 0x2)
segment = VG_CUBIC_TO;
else
segment = VG_QUAD_TO;
}
else
{
if (!(tag & 0x2) && !(lastTag & 0x1))
{
segment = VG_QUAD_TO;
int coord_size = coord.size();
VGshort x = (coord[coord_size-2] + fpoint.x) >> 1;
VGshort y = (coord[coord_size-1] + fpoint.y) >> 1;
coord.emplace_back(x);
coord.emplace_back(y);
}
else
goto skip_segment;
}
segments.emplace_back(segment);
skip_segment:
lastTag = tag;
coord.emplace_back(fpoint.x);
coord.emplace_back(fpoint.y);
}
if (!(lastTag & 0x1))
{
segments.emplace_back(lastTag & 0x2
? VG_CUBIC_TO
: VG_QUAD_TO);
coord.emplace_back(points[firstPoint].x);
coord.emplace_back(points[firstPoint].y);
}
segments.emplace_back(VG_CLOSE_PATH);
}
VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD,
VG_PATH_DATATYPE_S_16, 1.0f / (VGfloat)CHAR_HEIGHT, 0.0f,
segments.size(), coord.size(), VG_PATH_CAPABILITY_APPEND_TO);
if (path != VG_INVALID_HANDLE)
vgAppendPathData(path, segments.size(), &segments[0], &coord[0]);
return path;
}
VGFont m_font;
cString m_name;
VGfloat m_height;
VGfloat m_descender;
mutable cList<cOvgGlyph> m_glyphs;
FT_Face m_face;
static FT_Library s_ftLib;
static cList<cOvgFont> s_fonts;
};
FT_Library cOvgFont::s_ftLib = 0;
cList<cOvgFont> cOvgFont::s_fonts;
/* ------------------------------------------------------------------------- */
class cOvgString
{
public:
cOvgString(const unsigned int *symbols, cOvgFont *font) :
m_width(0.0f), m_height(font->Height()), m_descender(font->Descender()),
m_font(font)
{
uint prevSym = 0;
for (int i = 0; symbols[i]; i++)
if (cOvgGlyph *g = font->Glyph(symbols[i]))
{
VGfloat kerning = 0.0f;
if (prevSym)
{
kerning = m_font->Kerning(g, prevSym);
m_kerning.emplace_back(kerning);
}
m_width += g->AdvanceX() + kerning;
m_glyphIds.emplace_back(symbols[i]);
prevSym = symbols[i];
}
m_kerning.emplace_back(0.0f);
}
~cOvgString() { }
VGFont Font(void) { return m_font->Font(); }
VGint Length(void) { return m_glyphIds.size(); }
VGfloat Width(void) { return m_width; }
VGfloat Height(void) { return m_height; }
VGfloat Descender(void) { return m_descender; }
const VGuint *GlyphIds(void) { return &m_glyphIds[0]; }
const VGfloat *Kerning(void) { return &m_kerning[0]; }
private:
std::vector<VGuint> m_glyphIds;
std::vector<VGfloat> m_kerning;
VGfloat m_width;
VGfloat m_height;
VGfloat m_descender;
cOvgFont *m_font;
};
/* ------------------------------------------------------------------------- */
class cOvgPaintBox
{
public:
static void Draw(VGPath path)
{
vgDrawPath(path, VG_FILL_PATH);
}
static void Draw(cOvgString *string)
{
vgDrawGlyphs(string->Font(), string->Length(), string->GlyphIds(),
string->Kerning(), NULL, VG_FILL_PATH, VG_TRUE);
}
static VGPath Rect(void)
{
if (!s_initialized)
SetUp();
return s_rect;
}
static VGPath Ellipse(int quadrants)
{
if (!s_initialized)
SetUp();
return s_ellipse[(quadrants < -4 || quadrants > 8) ? 4 : quadrants + 4];
}
static VGPath Slope(int type)
{
if (!s_initialized)
SetUp();
return s_slope[(type < 0 || type > 7) ? 0 : type];
}
static void CleanUp(void)
{
vgDestroyPaint(s_paint);
vgDestroyPath(s_rect);
for (int i = 0; i < 8; i++)
vgDestroyPath(s_slope[i]);
for (int i = 0; i < 13; i++)
vgDestroyPath(s_ellipse[i]);
s_initialized = false;
}
static void SetColor(tColor color)
{
if (!s_initialized)
SetUp();
vgSetParameteri(s_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
vgSetColor(s_paint, (color << 8) + (color >> 24));
vgSetPaint(s_paint, VG_FILL_PATH);
}
static void SetAlpha(int alpha)
{
if (!s_initialized)
SetUp();
alpha = constrain(alpha, ALPHA_TRANSPARENT, ALPHA_OPAQUE);
VGfloat values[] = {
1.0f, 1.0f, 1.0f, alpha / 255.0f, 0.0f, 0.0f, 0.0f, 0.0f };
vgSetfv(VG_COLOR_TRANSFORM_VALUES, 8, values);
vgSeti(VG_COLOR_TRANSFORM, alpha == ALPHA_OPAQUE ? VG_FALSE : VG_TRUE);
}
static void SetPattern(VGImage image = VG_INVALID_HANDLE)
{
if (!s_initialized)
SetUp();
vgPaintPattern(s_paint, image);
if (image == VG_INVALID_HANDLE)
return;
vgSetParameteri(s_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_PATTERN);
vgSetParameteri(s_paint, VG_PAINT_PATTERN_TILING_MODE, VG_TILE_REPEAT);
vgSetPaint(s_paint, VG_FILL_PATH);
}
static void SetScissoring(int x = 0, int y = 0, int w = 0, int h = 0)
{
VGint cropArea[4] = { x, y, w, h };
vgSetiv(VG_SCISSOR_RECTS, 4, cropArea);
vgSeti(VG_SCISSORING, w && h ? VG_TRUE : VG_FALSE);
}
private:
static void SetUp(void)
{
// paint
s_paint = vgCreatePaint();
vgSetPaint(s_paint, VG_FILL_PATH);
// rectangle
s_rect = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
vguRect(s_rect, 0.0f, 0.0f, 1.0f, 1.0f);
// ellipses
for (int i = 0; i < 13; i++)
s_ellipse[i] = vgCreatePath(VG_PATH_FORMAT_STANDARD,
VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
vguArc(s_ellipse[0], 0.0f, 1.0f, 2.0f, 2.0f, 270, 90, VGU_ARC_OPEN);
vguArc(s_ellipse[1], 1.0f, 1.0f, 2.0f, 2.0f, 180, 90, VGU_ARC_OPEN);
vguArc(s_ellipse[2], 1.0f, 0.0f, 2.0f, 2.0f, 90, 90, VGU_ARC_OPEN);
vguArc(s_ellipse[3], 0.0f, 0.0f, 2.0f, 2.0f, 0, 90, VGU_ARC_OPEN);
// close path via corner opposed of center of arc for inverted arcs
VGubyte cornerSeg[] = { VG_LINE_TO_ABS, VG_CLOSE_PATH };
VGfloat cornerData[][2] = {
{ 1.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 1.0f }, { 1.0f, 1.0f }
};
for (int i = 0; i < 4; i++)
vgAppendPathData(s_ellipse[i], 2, cornerSeg, cornerData[i]);
vguEllipse(s_ellipse[4], 0.5f, 0.5f, 1.0f, 1.0f);
vguArc(s_ellipse[5], 0.0f, 0.0f, 2.0f, 2.0f, 0, 90, VGU_ARC_PIE);
vguArc(s_ellipse[6], 1.0f, 0.0f, 2.0f, 2.0f, 90, 90, VGU_ARC_PIE);
vguArc(s_ellipse[7], 1.0f, 1.0f, 2.0f, 2.0f, 180, 90, VGU_ARC_PIE);
vguArc(s_ellipse[8], 0.0f, 1.0f, 2.0f, 2.0f, 270, 90, VGU_ARC_PIE);
vguArc(s_ellipse[9], 0.0f, 0.5f, 2.0f, 1.0f, 270, 180, VGU_ARC_PIE);
vguArc(s_ellipse[10], 0.5f, 0.0f, 1.0f, 2.0f, 0, 180, VGU_ARC_PIE);
vguArc(s_ellipse[11], 1.0f, 0.5f, 2.0f, 1.0f, 90, 180, VGU_ARC_PIE);
vguArc(s_ellipse[12], 0.5f, 1.0f, 1.0f, 2.0f, 180, 180, VGU_ARC_PIE);
// slopes
VGubyte slopeSeg[] = {
VG_MOVE_TO_ABS, VG_LINE_TO_ABS, VG_CUBIC_TO_ABS, VG_CLOSE_PATH
};
// gradient of the slope: VDR uses 0.5 but 0.6 looks nicer...
const VGfloat s = 0.6f;
VGfloat slopeData[] = {
1.0f, 0.0f, 1.0f, 1.0f, 1.0f - s, 1.0f, s, 0.0f, 0.0f, 0.0f
};
VGfloat slopeScale[][2] = {
{ -1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, -1.0f },
{ -1.0f, 1.0f }, { 1.0f, -1.0f }, {-1.0f, -1.0f },
{ 1.0f, 1.0f }
};
VGfloat slopeTrans[][2] = {
{ -1.0f, -1.0f }, { -1.0f, 0.0f }, { 0.0f, -1.0f },
{ -1.0f, -1.0f }, { 0.0f, 0.0f }, { -1.0f, 0.0f },
{ 0.0f, -1.0f }
};
VGfloat slopeRot[] = { 0.0f, 0.0f, 0.0f, 90.0f, 90.0f, 90.0f, 90.0f };
VGfloat backupMatrix[9];
vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
vgGetMatrix(backupMatrix);
for (int i = 0; i < 8; i++)
{
s_slope[i] = vgCreatePath(VG_PATH_FORMAT_STANDARD,
VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
if (!i)
// draw the basic form...
vgAppendPathData(s_slope[0], 4, slopeSeg, slopeData);
else
{
// .. and translate the variants
vgLoadIdentity();
vgRotate(slopeRot[i - 1]);
vgScale(slopeScale[i - 1][0], slopeScale[i - 1][1]);
vgTranslate(slopeTrans[i - 1][0], slopeTrans[i - 1][1]);
vgTransformPath(s_slope[i], s_slope[0]);
}
}
vgLoadMatrix(backupMatrix);
s_initialized = true;
}
cOvgPaintBox();
~cOvgPaintBox();
static VGPaint s_paint;
static VGPath s_rect;
static VGPath s_ellipse[13];
static VGPath s_slope[8];
static bool s_initialized;
};
VGPaint cOvgPaintBox::s_paint = VG_INVALID_HANDLE;
VGPath cOvgPaintBox::s_rect = VG_INVALID_HANDLE;
VGPath cOvgPaintBox::s_ellipse[13] = { VG_INVALID_HANDLE };
VGPath cOvgPaintBox::s_slope[8] = { VG_INVALID_HANDLE };
bool cOvgPaintBox::s_initialized = false;
/* ------------------------------------------------------------------------- */
class cEgl
{
public:
EGLDisplay display;
EGLContext context;
EGLConfig config;
EGLint nConfig;
EGLSurface surface;
EGLSurface currentSurface;
EGL_DISPMANX_WINDOW_T window;
static const char* errStr(EGLint error)
{
return error == EGL_SUCCESS ? "success" :
error == EGL_NOT_INITIALIZED ? "not initialized" :
error == EGL_BAD_ACCESS ? "bad access" :
error == EGL_BAD_ALLOC ? "bad alloc" :
error == EGL_BAD_ATTRIBUTE ? "bad attribute" :
error == EGL_BAD_CONTEXT ? "bad context" :
error == EGL_BAD_CONFIG ? "bad config" :
error == EGL_BAD_CURRENT_SURFACE ? "bad current surface" :
error == EGL_BAD_DISPLAY ? "bad display" :
error == EGL_BAD_SURFACE ? "bad surface" :
error == EGL_BAD_MATCH ? "bad match" :
error == EGL_BAD_PARAMETER ? "bad parameter" :
error == EGL_BAD_NATIVE_PIXMAP ? "bad native pixmap" :
error == EGL_BAD_NATIVE_WINDOW ? "bad native window" :
error == EGL_CONTEXT_LOST ? "context lost" :
"unknown error";
}
};
/* ------------------------------------------------------------------------- */
struct tOvgImageRef
{
VGImage image;
bool used;
};
/* ------------------------------------------------------------------------- */
class cOvgSavedRegion
{
public:
cOvgSavedRegion() : image(VG_INVALID_HANDLE), rect(cRect()) { }
VGImage image;
cRect rect;
};
/* ------------------------------------------------------------------------- */
class cOvgRenderTarget
{
public:
cOvgRenderTarget(int _width = 0, int _height = 0) :
surface(EGL_NO_SURFACE),
image(VG_INVALID_HANDLE),
width(_width),
height(_height),
initialized(false) { }
virtual ~cOvgRenderTarget() { }
static bool MakeDefault(cEgl *egl)
{
if (eglMakeCurrent(egl->display, egl->surface, egl->surface,
egl->context) == EGL_FALSE)
{
ELOG("[EGL] failed to connect context to surface: %s!",
cEgl::errStr(eglGetError()));
return false;
}
egl->currentSurface = egl->surface;
return true;
}
virtual bool MakeCurrent(cEgl *egl)
{
// if this is a window surface, check for an update after OSD reset
if (image == VG_INVALID_HANDLE && surface != egl->surface)
{
surface = egl->surface;
width = egl->window.width;
height = egl->window.height;
}
if (egl->currentSurface == surface)
return true;
if (eglMakeCurrent(egl->display, surface, surface, egl->context) ==
EGL_FALSE)
{
ELOG("[EGL] failed to connect context to surface: %s!",
cEgl::errStr(eglGetError()));
return false;
}
egl->currentSurface = surface;
return true;
}
EGLSurface surface;
VGImage image;
int width;
int height;
bool initialized;
private:
cOvgRenderTarget(const cOvgRenderTarget&);
cOvgRenderTarget& operator= (const cOvgRenderTarget&);
};
/* ------------------------------------------------------------------------- */
class cOvgCmd
{
public:
cOvgCmd(cOvgRenderTarget *target) : m_target(target) { }
virtual ~cOvgCmd() { }
virtual bool Execute(cEgl *egl) = 0;
virtual const char* Description(void) = 0;
#ifdef DEBUG_OVGSTAT
virtual bool IsFlush(void) { return false; };
#endif
protected:
cOvgRenderTarget *m_target;
private:
cOvgCmd(const cOvgCmd&);
cOvgCmd& operator= (const cOvgCmd&);
};
class cOvgCmdFlush : public cOvgCmd
{
public:
cOvgCmdFlush(cOvgRenderTarget *target) :
cOvgCmd(target) { }
virtual const char* Description(void) { return "Flush"; }
#ifdef DEBUG_OVGSTAT
virtual bool IsFlush(void) { return true; };
#endif
virtual bool Execute(cEgl *egl)
{
if (!m_target->MakeCurrent(egl))
return false;
eglSwapBuffers(egl->display, m_target->surface);
return true;
}
};
class cOvgCmdReset : public cOvgCmd
{
public:
cOvgCmdReset(bool cleanup = false) :
cOvgCmd(0), m_cleanup(cleanup) { }
virtual const char* Description(void) { return "Reset"; }
virtual bool Execute(cEgl *egl)
{
if (m_cleanup)
{
cOvgFont::CleanUp();
cOvgPaintBox::CleanUp();
}
return false;
}
private:
bool m_cleanup;
};
class cOvgCmdCreatePixelBuffer : public cOvgCmd
{
public:
cOvgCmdCreatePixelBuffer(cOvgRenderTarget *target) : cOvgCmd(target) { }
virtual const char* Description(void) { return "CreatePixelBuffer"; }
virtual bool Execute(cEgl *egl)
{
m_target->image = vgCreateImage(VG_sARGB_8888, m_target->width,
m_target->height, VG_IMAGE_QUALITY_BETTER);
if (m_target->image == VG_INVALID_HANDLE)
ELOG("[OpenVG] failed to allocate %dpx x %dpx pixel buffer!",
m_target->width, m_target->height);
else
{
m_target->surface = eglCreatePbufferFromClientBuffer(egl->display,
EGL_OPENVG_IMAGE, (EGLClientBuffer)m_target->image,
egl->config, NULL);
if (m_target->surface == EGL_NO_SURFACE)
{
ELOG("[EGL] failed to create pixel buffer surface: %s!",
cEgl::errStr(eglGetError()));
vgDestroyImage(m_target->image);
m_target->image = VG_INVALID_HANDLE;
}
else
{
if (eglSurfaceAttrib(egl->display, m_target->surface,
EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED) == EGL_FALSE)
{
ELOG("[EGL] failed to set surface attributes!");
eglDestroySurface(egl->display, m_target->surface);
vgDestroyImage(m_target->image);
m_target->image = VG_INVALID_HANDLE;
}
}
}
m_target->initialized = true;
return true;
}
};
class cOvgCmdDestroySurface : public cOvgCmd
{
public:
cOvgCmdDestroySurface(cOvgRenderTarget *&target)
: cOvgCmd(target), m_targetRef(target) {}
virtual const char* Description(void) { return "DestroySurface"; }
virtual bool Execute(cEgl *egl)
{
bool ok = cOvgRenderTarget::MakeDefault(egl);
// only destroy pixel buffer surfaces
if (ok && m_target->image != VG_INVALID_HANDLE)
{
if (eglDestroySurface(egl->display, m_target->surface) == EGL_FALSE)
ELOG("[EGL] failed to destroy surface: %s!",
cEgl::errStr(eglGetError()));
vgDestroyImage(m_target->image);
}
delete m_target;
m_targetRef = NULL;
return ok;
}
private:
cOvgRenderTarget *&m_targetRef;
};
class cOvgCmdClear : public cOvgCmd
{
public:
cOvgCmdClear(cOvgRenderTarget *target, tColor color = clrTransparent) :
cOvgCmd(target), m_color(color) { }
virtual const char* Description(void) { return "Clear"; }
virtual bool Execute(cEgl *egl)
{
if (!m_target->MakeCurrent(egl))
return false;
VGfloat color[4] = {
(m_color >> 16 & 0xff) / 255.0f,
(m_color >> 8 & 0xff) / 255.0f,
(m_color & 0xff) / 255.0f,
(m_color >> 24 & 0xff) / 255.0f
};
vgSetfv(VG_CLEAR_COLOR, 4, color);
vgClear(0, 0, m_target->width, m_target->height);
return true;
}
private:
tColor m_color;
};
class cOvgCmdSaveRegion : public cOvgCmd
{
public:
cOvgCmdSaveRegion(cOvgRenderTarget *target, cOvgSavedRegion *savedRegion,
int x, int y, int w, int h) : cOvgCmd(target),
m_x(x), m_y(y), m_w(w), m_h(h), m_savedRegion(savedRegion)
{ }
virtual const char* Description(void) { return "SaveRegion"; }
virtual bool Execute(cEgl *egl)
{
if (!m_target->MakeCurrent(egl))
return false;
if (m_savedRegion->image != VG_INVALID_HANDLE)
vgDestroyImage(m_savedRegion->image);
if (m_w && m_h)
{
m_savedRegion->image = vgCreateImage(VG_sARGB_8888,
m_w, m_h, VG_IMAGE_QUALITY_BETTER);
if (m_savedRegion->image == VG_INVALID_HANDLE)
{
ELOG("failed to allocate image!");
return false;
}
m_savedRegion->rect.Set(m_x, m_y, m_w, m_h);
vgGetPixels(m_savedRegion->image, 0, 0, m_savedRegion->rect.X(),
m_target->height - m_savedRegion->rect.Bottom() - 1,
m_savedRegion->rect.Width(), m_savedRegion->rect.Height());
}
return true;
}
private:
int m_x;
int m_y;
int m_w;
int m_h;
cOvgSavedRegion *m_savedRegion;
};
class cOvgCmdRestoreRegion : public cOvgCmd
{
public:
cOvgCmdRestoreRegion(cOvgRenderTarget *target, cOvgSavedRegion *savedRegion)
: cOvgCmd(target), m_savedRegion(savedRegion) { }
virtual const char* Description(void) { return "RestoreRegion"; }
virtual bool Execute(cEgl *egl)
{
if (!m_target->MakeCurrent(egl))
return false;
if (m_savedRegion && m_savedRegion->image != VG_INVALID_HANDLE)
vgSetPixels(m_savedRegion->rect.X(),
m_target->height - m_savedRegion->rect.Bottom() - 1,
m_savedRegion->image, 0, 0, m_savedRegion->rect.Width(),
m_savedRegion->rect.Height());
return true;
}
private:
cOvgSavedRegion *m_savedRegion;
};
class cOvgCmdDropRegion : public cOvgCmd