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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
|
/*
* Copyright © 2012 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <assert.h>
#include <linux/input.h>
#include "gl-renderer.h"
#include <EGL/eglext.h>
#include "weston-egl-ext.h"
struct gl_shader {
GLuint program;
GLuint vertex_shader, fragment_shader;
GLint proj_uniform;
GLint tex_uniforms[3];
GLint alpha_uniform;
GLint color_uniform;
};
struct gl_output_state {
EGLSurface egl_surface;
int current_buffer;
pixman_region32_t buffer_damage[2];
};
struct gl_surface_state {
GLfloat color[4];
struct gl_shader *shader;
GLuint textures[3];
int num_textures;
pixman_region32_t texture_damage;
EGLImageKHR images[3];
GLenum target;
int num_images;
struct weston_buffer_reference buffer_ref;
int pitch; /* in pixels */
};
struct gl_renderer {
struct weston_renderer base;
int fragment_shader_debug;
EGLDisplay egl_display;
EGLContext egl_context;
EGLConfig egl_config;
struct {
int32_t top, bottom, left, right;
GLuint texture;
int32_t width, height;
} border;
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
PFNEGLCREATEIMAGEKHRPROC create_image;
PFNEGLDESTROYIMAGEKHRPROC destroy_image;
int has_unpack_subimage;
PFNEGLBINDWAYLANDDISPLAYWL bind_display;
PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
int has_bind_display;
int has_egl_image_external;
struct gl_shader texture_shader_rgba;
struct gl_shader texture_shader_rgbx;
struct gl_shader texture_shader_egl_external;
struct gl_shader texture_shader_y_uv;
struct gl_shader texture_shader_y_u_v;
struct gl_shader texture_shader_y_xuxv;
struct gl_shader invert_color_shader;
struct gl_shader solid_shader;
struct gl_shader *current_shader;
};
static inline struct gl_output_state *
get_output_state(struct weston_output *output)
{
return (struct gl_output_state *)output->renderer_state;
}
static inline struct gl_surface_state *
get_surface_state(struct weston_surface *surface)
{
return (struct gl_surface_state *)surface->renderer_state;
}
static inline struct gl_renderer *
get_renderer(struct weston_compositor *ec)
{
return (struct gl_renderer *)ec->renderer;
}
static const char *
egl_error_string(EGLint code)
{
#define MYERRCODE(x) case x: return #x;
switch (code) {
MYERRCODE(EGL_SUCCESS)
MYERRCODE(EGL_NOT_INITIALIZED)
MYERRCODE(EGL_BAD_ACCESS)
MYERRCODE(EGL_BAD_ALLOC)
MYERRCODE(EGL_BAD_ATTRIBUTE)
MYERRCODE(EGL_BAD_CONTEXT)
MYERRCODE(EGL_BAD_CONFIG)
MYERRCODE(EGL_BAD_CURRENT_SURFACE)
MYERRCODE(EGL_BAD_DISPLAY)
MYERRCODE(EGL_BAD_SURFACE)
MYERRCODE(EGL_BAD_MATCH)
MYERRCODE(EGL_BAD_PARAMETER)
MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
MYERRCODE(EGL_BAD_NATIVE_WINDOW)
MYERRCODE(EGL_CONTEXT_LOST)
default:
return "unknown";
}
#undef MYERRCODE
}
WL_EXPORT void
gl_renderer_print_egl_error_state(void)
{
EGLint code;
code = eglGetError();
weston_log("EGL error state: %s (0x%04lx)\n",
egl_error_string(code), (long)code);
}
struct polygon8 {
GLfloat x[8];
GLfloat y[8];
int n;
};
struct clip_context {
struct {
GLfloat x;
GLfloat y;
} prev;
struct {
GLfloat x1, y1;
GLfloat x2, y2;
} clip;
struct {
GLfloat *x;
GLfloat *y;
} vertices;
};
static GLfloat
float_difference(GLfloat a, GLfloat b)
{
/* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
static const GLfloat max_diff = 4.0f * FLT_MIN;
static const GLfloat max_rel_diff = 4.0e-5;
GLfloat diff = a - b;
GLfloat adiff = fabsf(diff);
if (adiff <= max_diff)
return 0.0f;
a = fabsf(a);
b = fabsf(b);
if (adiff <= (a > b ? a : b) * max_rel_diff)
return 0.0f;
return diff;
}
/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
* Compute the y coordinate of the intersection.
*/
static GLfloat
clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
GLfloat x_arg)
{
GLfloat a;
GLfloat diff = float_difference(p1x, p2x);
/* Practically vertical line segment, yet the end points have already
* been determined to be on different sides of the line. Therefore
* the line segment is part of the line and intersects everywhere.
* Return the end point, so we use the whole line segment.
*/
if (diff == 0.0f)
return p2y;
a = (x_arg - p2x) / diff;
return p2y + (p1y - p2y) * a;
}
/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
* Compute the x coordinate of the intersection.
*/
static GLfloat
clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
GLfloat y_arg)
{
GLfloat a;
GLfloat diff = float_difference(p1y, p2y);
/* Practically horizontal line segment, yet the end points have already
* been determined to be on different sides of the line. Therefore
* the line segment is part of the line and intersects everywhere.
* Return the end point, so we use the whole line segment.
*/
if (diff == 0.0f)
return p2x;
a = (y_arg - p2y) / diff;
return p2x + (p1x - p2x) * a;
}
enum path_transition {
PATH_TRANSITION_OUT_TO_OUT = 0,
PATH_TRANSITION_OUT_TO_IN = 1,
PATH_TRANSITION_IN_TO_OUT = 2,
PATH_TRANSITION_IN_TO_IN = 3,
};
static void
clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
{
*ctx->vertices.x++ = x;
*ctx->vertices.y++ = y;
}
static enum path_transition
path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
{
return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
}
static enum path_transition
path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
{
return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
}
static enum path_transition
path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
{
return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
}
static enum path_transition
path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
{
return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
}
static void
clip_polygon_leftright(struct clip_context *ctx,
enum path_transition transition,
GLfloat x, GLfloat y, GLfloat clip_x)
{
GLfloat yi;
switch (transition) {
case PATH_TRANSITION_IN_TO_IN:
clip_append_vertex(ctx, x, y);
break;
case PATH_TRANSITION_IN_TO_OUT:
yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
clip_append_vertex(ctx, clip_x, yi);
break;
case PATH_TRANSITION_OUT_TO_IN:
yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
clip_append_vertex(ctx, clip_x, yi);
clip_append_vertex(ctx, x, y);
break;
case PATH_TRANSITION_OUT_TO_OUT:
/* nothing */
break;
default:
assert(0 && "bad enum path_transition");
}
ctx->prev.x = x;
ctx->prev.y = y;
}
static void
clip_polygon_topbottom(struct clip_context *ctx,
enum path_transition transition,
GLfloat x, GLfloat y, GLfloat clip_y)
{
GLfloat xi;
switch (transition) {
case PATH_TRANSITION_IN_TO_IN:
clip_append_vertex(ctx, x, y);
break;
case PATH_TRANSITION_IN_TO_OUT:
xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
clip_append_vertex(ctx, xi, clip_y);
break;
case PATH_TRANSITION_OUT_TO_IN:
xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
clip_append_vertex(ctx, xi, clip_y);
clip_append_vertex(ctx, x, y);
break;
case PATH_TRANSITION_OUT_TO_OUT:
/* nothing */
break;
default:
assert(0 && "bad enum path_transition");
}
ctx->prev.x = x;
ctx->prev.y = y;
}
static void
clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
GLfloat *dst_x, GLfloat *dst_y)
{
ctx->prev.x = src->x[src->n - 1];
ctx->prev.y = src->y[src->n - 1];
ctx->vertices.x = dst_x;
ctx->vertices.y = dst_y;
}
static int
clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
GLfloat *dst_x, GLfloat *dst_y)
{
enum path_transition trans;
int i;
clip_context_prepare(ctx, src, dst_x, dst_y);
for (i = 0; i < src->n; i++) {
trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
ctx->clip.x1);
}
return ctx->vertices.x - dst_x;
}
static int
clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
GLfloat *dst_x, GLfloat *dst_y)
{
enum path_transition trans;
int i;
clip_context_prepare(ctx, src, dst_x, dst_y);
for (i = 0; i < src->n; i++) {
trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
ctx->clip.x2);
}
return ctx->vertices.x - dst_x;
}
static int
clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
GLfloat *dst_x, GLfloat *dst_y)
{
enum path_transition trans;
int i;
clip_context_prepare(ctx, src, dst_x, dst_y);
for (i = 0; i < src->n; i++) {
trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
ctx->clip.y1);
}
return ctx->vertices.x - dst_x;
}
static int
clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
GLfloat *dst_x, GLfloat *dst_y)
{
enum path_transition trans;
int i;
clip_context_prepare(ctx, src, dst_x, dst_y);
for (i = 0; i < src->n; i++) {
trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
ctx->clip.y2);
}
return ctx->vertices.x - dst_x;
}
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) > (b)) ? (b) : (a))
#define clip(x, a, b) min(max(x, a), b)
/*
* Compute the boundary vertices of the intersection of the global coordinate
* aligned rectangle 'rect', and an arbitrary quadrilateral produced from
* 'surf_rect' when transformed from surface coordinates into global coordinates.
* The vertices are written to 'ex' and 'ey', and the return value is the
* number of vertices. Vertices are produced in clockwise winding order.
* Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
* polygon area.
*/
static int
calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
{
struct polygon8 polygon;
struct clip_context ctx;
int i, n;
GLfloat min_x, max_x, min_y, max_y;
struct polygon8 surf = {
{ surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
{ surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
4
};
ctx.clip.x1 = rect->x1;
ctx.clip.y1 = rect->y1;
ctx.clip.x2 = rect->x2;
ctx.clip.y2 = rect->y2;
/* transform surface to screen space: */
for (i = 0; i < surf.n; i++)
weston_surface_to_global_float(es, surf.x[i], surf.y[i],
&surf.x[i], &surf.y[i]);
/* find bounding box: */
min_x = max_x = surf.x[0];
min_y = max_y = surf.y[0];
for (i = 1; i < surf.n; i++) {
min_x = min(min_x, surf.x[i]);
max_x = max(max_x, surf.x[i]);
min_y = min(min_y, surf.y[i]);
max_y = max(max_y, surf.y[i]);
}
/* First, simple bounding box check to discard early transformed
* surface rects that do not intersect with the clip region:
*/
if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
(min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
return 0;
/* Simple case, bounding box edges are parallel to surface edges,
* there will be only four edges. We just need to clip the surface
* vertices to the clip rect bounds:
*/
if (!es->transform.enabled) {
for (i = 0; i < surf.n; i++) {
ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
}
return surf.n;
}
/* Transformed case: use a general polygon clipping algorithm to
* clip the surface rectangle with each side of 'rect'.
* The algorithm is Sutherland-Hodgman, as explained in
* http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
* but without looking at any of that code.
*/
polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
/* Get rid of duplicate vertices */
ex[0] = surf.x[0];
ey[0] = surf.y[0];
n = 1;
for (i = 1; i < surf.n; i++) {
if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
float_difference(ey[n - 1], surf.y[i]) == 0.0f)
continue;
ex[n] = surf.x[i];
ey[n] = surf.y[i];
n++;
}
if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
float_difference(ey[n - 1], surf.y[0]) == 0.0f)
n--;
if (n < 3)
return 0;
return n;
}
static int
texture_region(struct weston_surface *es, pixman_region32_t *region,
pixman_region32_t *surf_region)
{
struct gl_surface_state *gs = get_surface_state(es);
struct weston_compositor *ec = es->compositor;
GLfloat *v, inv_width, inv_height;
unsigned int *vtxcnt, nvtx = 0;
pixman_box32_t *rects, *surf_rects;
int i, j, k, nrects, nsurf;
rects = pixman_region32_rectangles(region, &nrects);
surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
/* worst case we can have 8 vertices per rect (ie. clipped into
* an octagon):
*/
v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
inv_width = 1.0 / gs->pitch;
switch (es->buffer_transform) {
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
inv_height = 1.0 / es->geometry.width;
break;
default:
inv_height = 1.0 / es->geometry.height;
}
for (i = 0; i < nrects; i++) {
pixman_box32_t *rect = &rects[i];
for (j = 0; j < nsurf; j++) {
pixman_box32_t *surf_rect = &surf_rects[j];
GLfloat sx, sy, bx, by;
GLfloat ex[8], ey[8]; /* edge points in screen space */
int n;
/* The transformed surface, after clipping to the clip region,
* can have as many as eight sides, emitted as a triangle-fan.
* The first vertex in the triangle fan can be chosen arbitrarily,
* since the area is guaranteed to be convex.
*
* If a corner of the transformed surface falls outside of the
* clip region, instead of emitting one vertex for the corner
* of the surface, up to two are emitted for two corresponding
* intersection point(s) between the surface and the clip region.
*
* To do this, we first calculate the (up to eight) points that
* form the intersection of the clip rect and the transformed
* surface.
*/
n = calculate_edges(es, rect, surf_rect, ex, ey);
if (n < 3)
continue;
/* emit edge points: */
for (k = 0; k < n; k++) {
weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
/* position: */
*(v++) = ex[k];
*(v++) = ey[k];
/* texcoord: */
weston_surface_to_buffer_float(es, sx, sy,
&bx, &by);
*(v++) = bx * inv_width;
*(v++) = by * inv_height;
}
vtxcnt[nvtx++] = n;
}
}
return nvtx;
}
static void
triangle_fan_debug(struct weston_surface *surface, int first, int count)
{
struct weston_compositor *compositor = surface->compositor;
struct gl_renderer *gr = get_renderer(compositor);
int i;
GLushort *buffer;
GLushort *index;
int nelems;
static int color_idx = 0;
static const GLfloat color[][4] = {
{ 1.0, 0.0, 0.0, 1.0 },
{ 0.0, 1.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0, 1.0 },
{ 1.0, 1.0, 1.0, 1.0 },
};
nelems = (count - 1 + count - 2) * 2;
buffer = malloc(sizeof(GLushort) * nelems);
index = buffer;
for (i = 1; i < count; i++) {
*index++ = first;
*index++ = first + i;
}
for (i = 2; i < count; i++) {
*index++ = first + i - 1;
*index++ = first + i;
}
glUseProgram(gr->solid_shader.program);
glUniform4fv(gr->solid_shader.color_uniform, 1,
color[color_idx++ % ARRAY_LENGTH(color)]);
glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
glUseProgram(gr->current_shader->program);
free(buffer);
}
static void
repaint_region(struct weston_surface *es, pixman_region32_t *region,
pixman_region32_t *surf_region)
{
struct weston_compositor *ec = es->compositor;
GLfloat *v;
unsigned int *vtxcnt;
int i, first, nfans;
/* The final region to be painted is the intersection of
* 'region' and 'surf_region'. However, 'region' is in the global
* coordinates, and 'surf_region' is in the surface-local
* coordinates. texture_region() will iterate over all pairs of
* rectangles from both regions, compute the intersection
* polygon for each pair, and store it as a triangle fan if
* it has a non-zero area (at least 3 vertices, actually).
*/
nfans = texture_region(es, region, surf_region);
v = ec->vertices.data;
vtxcnt = ec->vtxcnt.data;
/* position: */
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
glEnableVertexAttribArray(0);
/* texcoord: */
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
glEnableVertexAttribArray(1);
for (i = 0, first = 0; i < nfans; i++) {
glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
if (ec->fan_debug)
triangle_fan_debug(es, first, vtxcnt[i]);
first += vtxcnt[i];
}
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
ec->vertices.size = 0;
ec->vtxcnt.size = 0;
}
static int
use_output(struct weston_output *output)
{
static int errored;
struct gl_output_state *go = get_output_state(output);
struct gl_renderer *gr = get_renderer(output->compositor);
EGLBoolean ret;
ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
go->egl_surface, gr->egl_context);
if (ret == EGL_FALSE) {
if (errored)
return -1;
errored = 1;
weston_log("Failed to make EGL context current.\n");
gl_renderer_print_egl_error_state();
return -1;
}
return 0;
}
static void
use_shader(struct gl_renderer *gr,
struct gl_shader *shader)
{
if (gr->current_shader == shader)
return;
glUseProgram(shader->program);
gr->current_shader = shader;
}
static void
shader_uniforms(struct gl_shader *shader,
struct weston_surface *surface,
struct weston_output *output)
{
int i;
struct gl_surface_state *gs = get_surface_state(surface);
glUniformMatrix4fv(shader->proj_uniform,
1, GL_FALSE, output->matrix.d);
glUniform4fv(shader->color_uniform, 1, gs->color);
glUniform1f(shader->alpha_uniform, surface->alpha);
for (i = 0; i < gs->num_textures; i++)
glUniform1i(shader->tex_uniforms[i], i);
}
static void
draw_surface(struct weston_surface *es, struct weston_output *output,
pixman_region32_t *damage) /* in global coordinates */
{
struct weston_compositor *ec = es->compositor;
struct gl_renderer *gr = get_renderer(ec);
struct gl_surface_state *gs = get_surface_state(es);
struct gl_output_state *go = get_output_state(output);
/* repaint bounding region in global coordinates: */
pixman_region32_t repaint;
/* non-opaque region in surface coordinates: */
pixman_region32_t surface_blend;
pixman_region32_t *buffer_damage;
GLint filter;
int i;
pixman_region32_init(&repaint);
pixman_region32_intersect(&repaint,
&es->transform.boundingbox, damage);
pixman_region32_subtract(&repaint, &repaint, &es->clip);
if (!pixman_region32_not_empty(&repaint))
goto out;
buffer_damage = &go->buffer_damage[go->current_buffer];
pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
if (ec->fan_debug) {
use_shader(gr, &gr->solid_shader);
shader_uniforms(&gr->solid_shader, es, output);
}
use_shader(gr, gs->shader);
shader_uniforms(gs->shader, es, output);
if (es->transform.enabled || output->zoom.active)
filter = GL_LINEAR;
else
filter = GL_NEAREST;
for (i = 0; i < gs->num_textures; i++) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(gs->target, gs->textures[i]);
glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
}
/* blended region is whole surface minus opaque region: */
pixman_region32_init_rect(&surface_blend, 0, 0,
es->geometry.width, es->geometry.height);
pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
if (pixman_region32_not_empty(&es->opaque)) {
if (gs->shader == &gr->texture_shader_rgba) {
/* Special case for RGBA textures with possibly
* bad data in alpha channel: use the shader
* that forces texture alpha = 1.0.
* Xwayland surfaces need this.
*/
use_shader(gr, &gr->texture_shader_rgbx);
shader_uniforms(&gr->texture_shader_rgbx, es, output);
}
if (es->alpha < 1.0)
glEnable(GL_BLEND);
else
glDisable(GL_BLEND);
repaint_region(es, &repaint, &es->opaque);
}
if (pixman_region32_not_empty(&surface_blend)) {
use_shader(gr, gs->shader);
glEnable(GL_BLEND);
repaint_region(es, &repaint, &surface_blend);
}
pixman_region32_fini(&surface_blend);
out:
pixman_region32_fini(&repaint);
}
static void
repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
{
struct weston_compositor *compositor = output->compositor;
struct weston_surface *surface;
wl_list_for_each_reverse(surface, &compositor->surface_list, link)
if (surface->plane == &compositor->primary_plane)
draw_surface(surface, output, damage);
}
static int
texture_border(struct weston_output *output)
{
struct weston_compositor *ec = output->compositor;
struct gl_renderer *gr = get_renderer(ec);
GLfloat *d;
unsigned int *p;
int i, j, k, n;
GLfloat x[4], y[4], u[4], v[4];
x[0] = -gr->border.left;
x[1] = 0;
x[2] = output->current->width;
x[3] = output->current->width + gr->border.right;
y[0] = -gr->border.top;
y[1] = 0;
y[2] = output->current->height;
y[3] = output->current->height + gr->border.bottom;
u[0] = 0.0;
u[1] = (GLfloat) gr->border.left / gr->border.width;
u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
u[3] = 1.0;
v[0] = 0.0;
v[1] = (GLfloat) gr->border.top / gr->border.height;
v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
v[3] = 1.0;
n = 8;
d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
k = 0;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
if (i == 1 && j == 1)
continue;
d[ 0] = x[i];
d[ 1] = y[j];
d[ 2] = u[i];
d[ 3] = v[j];
d[ 4] = x[i];
d[ 5] = y[j + 1];
d[ 6] = u[i];
d[ 7] = v[j + 1];
d[ 8] = x[i + 1];
d[ 9] = y[j];
d[10] = u[i + 1];
d[11] = v[j];
d[12] = x[i + 1];
d[13] = y[j + 1];
d[14] = u[i + 1];
d[15] = v[j + 1];
p[0] = k + 0;
p[1] = k + 1;
p[2] = k + 2;
p[3] = k + 2;
p[4] = k + 1;
p[5] = k + 3;
d += 16;
p += 6;
k += 4;
}
return k / 4;
}
static void
draw_border(struct weston_output *output)
{
struct weston_compositor *ec = output->compositor;
struct gl_renderer *gr = get_renderer(ec);
struct gl_shader *shader = &gr->texture_shader_rgba;
GLfloat *v;
int n;
glDisable(GL_BLEND);
use_shader(gr, shader);
glUniformMatrix4fv(shader->proj_uniform,
1, GL_FALSE, output->matrix.d);
glUniform1i(shader->tex_uniforms[0], 0);
glUniform1f(shader->alpha_uniform, 1);
n = texture_border(output);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gr->border.texture);
v = ec->vertices.data;
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, n * 6,
GL_UNSIGNED_INT, ec->indices.data);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
ec->vertices.size = 0;
ec->indices.size = 0;
}
static void
gl_renderer_repaint_output(struct weston_output *output,
pixman_region32_t *output_damage)
{
struct gl_output_state *go = get_output_state(output);
struct weston_compositor *compositor = output->compositor;
struct gl_renderer *gr = get_renderer(compositor);
EGLBoolean ret;
static int errored;
int32_t width, height, i;
width = output->current->width +
output->border.left + output->border.right;
height = output->current->height +
output->border.top + output->border.bottom;
glViewport(0, 0, width, height);
if (use_output(output) < 0)
return;
/* if debugging, redraw everything outside the damage to clean up
* debug lines from the previous draw on this buffer:
*/
if (compositor->fan_debug) {
pixman_region32_t undamaged;
pixman_region32_init(&undamaged);
pixman_region32_subtract(&undamaged, &output->region,
output_damage);
compositor->fan_debug = 0;
repaint_surfaces(output, &undamaged);
compositor->fan_debug = 1;
pixman_region32_fini(&undamaged);
}
for (i = 0; i < 2; i++)
pixman_region32_union(&go->buffer_damage[i],
&go->buffer_damage[i],
output_damage);
pixman_region32_union(output_damage, output_damage,
&go->buffer_damage[go->current_buffer]);
repaint_surfaces(output, output_damage);
if (gr->border.texture)
draw_border(output);
pixman_region32_copy(&output->previous_damage, output_damage);
wl_signal_emit(&output->frame_signal, output);
ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
if (ret == EGL_FALSE && !errored) {
errored = 1;
weston_log("Failed in eglSwapBuffers.\n");
gl_renderer_print_egl_error_state();
}
go->current_buffer ^= 1;
}
static int
gl_renderer_read_pixels(struct weston_output *output,
pixman_format_code_t format, void *pixels,
uint32_t x, uint32_t y,
uint32_t width, uint32_t height)
{
GLenum gl_format;
switch (format) {
case PIXMAN_a8r8g8b8:
gl_format = GL_BGRA_EXT;
break;
case PIXMAN_a8b8g8r8:
gl_format = GL_RGBA;
break;
default:
return -1;
}
if (use_output(output) < 0)
return -1;
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, width, height, gl_format,
GL_UNSIGNED_BYTE, pixels);
return 0;
}
static void
gl_renderer_flush_damage(struct weston_surface *surface)
{
struct gl_renderer *gr = get_renderer(surface->compositor);
struct gl_surface_state *gs = get_surface_state(surface);
struct wl_buffer *buffer = gs->buffer_ref.buffer;
#ifdef GL_UNPACK_ROW_LENGTH
pixman_box32_t *rectangles;
void *data;
int i, n;
#endif
pixman_region32_union(&gs->texture_damage,
&gs->texture_damage, &surface->damage);
if (!buffer)
return;
/* Avoid upload, if the texture won't be used this time.
* We still accumulate the damage in texture_damage, and
* hold the reference to the buffer, in case the surface
* migrates back to the primary plane.
*/
if (surface->plane != &surface->compositor->primary_plane)
return;
if (!pixman_region32_not_empty(&gs->texture_damage))
goto done;
glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
if (!gr->has_unpack_subimage) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
gs->pitch, buffer->height, 0,
GL_BGRA_EXT, GL_UNSIGNED_BYTE,
wl_shm_buffer_get_data(buffer));
goto done;
}
#ifdef GL_UNPACK_ROW_LENGTH
/* Mesa does not define GL_EXT_unpack_subimage */
glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
data = wl_shm_buffer_get_data(buffer);
rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
for (i = 0; i < n; i++) {
pixman_box32_t r;
r = weston_surface_to_buffer_rect(surface, rectangles[i]);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
r.x2 - r.x1, r.y2 - r.y1,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
}
#endif
done:
pixman_region32_fini(&gs->texture_damage);
pixman_region32_init(&gs->texture_damage);
weston_buffer_reference(&gs->buffer_ref, NULL);
}
static void
ensure_textures(struct gl_surface_state *gs, int num_textures)
{
int i;
if (num_textures <= gs->num_textures)
return;
for (i = gs->num_textures; i < num_textures; i++) {
glGenTextures(1, &gs->textures[i]);
glBindTexture(gs->target, gs->textures[i]);
glTexParameteri(gs->target,
GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(gs->target,
GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
gs->num_textures = num_textures;
glBindTexture(gs->target, 0);
}
static void
gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
{
struct weston_compositor *ec = es->compositor;
struct gl_renderer *gr = get_renderer(ec);
struct gl_surface_state *gs = get_surface_state(es);
EGLint attribs[3], format;
int i, num_planes;
weston_buffer_reference(&gs->buffer_ref, buffer);
if (!buffer) {
for (i = 0; i < gs->num_images; i++) {
gr->destroy_image(gr->egl_display, gs->images[i]);
gs->images[i] = NULL;
}
gs->num_images = 0;
glDeleteTextures(gs->num_textures, gs->textures);
gs->num_textures = 0;
return;
}
if (wl_buffer_is_shm(buffer)) {
gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
gs->target = GL_TEXTURE_2D;
ensure_textures(gs, 1);
glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
gs->pitch, buffer->height, 0,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
gs->shader = &gr->texture_shader_rgbx;
else
gs->shader = &gr->texture_shader_rgba;
} else if (gr->query_buffer(gr->egl_display, buffer,
EGL_TEXTURE_FORMAT, &format)) {
for (i = 0; i < gs->num_images; i++)
gr->destroy_image(gr->egl_display, gs->images[i]);
gs->num_images = 0;
gs->target = GL_TEXTURE_2D;
switch (format) {
case EGL_TEXTURE_RGB:
case EGL_TEXTURE_RGBA:
default:
num_planes = 1;
gs->shader = &gr->texture_shader_rgba;
break;
case EGL_TEXTURE_EXTERNAL_WL:
num_planes = 1;
gs->target = GL_TEXTURE_EXTERNAL_OES;
gs->shader = &gr->texture_shader_egl_external;
break;
case EGL_TEXTURE_Y_UV_WL:
num_planes = 2;
gs->shader = &gr->texture_shader_y_uv;
break;
case EGL_TEXTURE_Y_U_V_WL:
num_planes = 3;
gs->shader = &gr->texture_shader_y_u_v;
break;
case EGL_TEXTURE_Y_XUXV_WL:
num_planes = 2;
gs->shader = &gr->texture_shader_y_xuxv;
break;
}
ensure_textures(gs, num_planes);
for (i = 0; i < num_planes; i++) {
attribs[0] = EGL_WAYLAND_PLANE_WL;
attribs[1] = i;
attribs[2] = EGL_NONE;
gs->images[i] = gr->create_image(gr->egl_display,
NULL,
EGL_WAYLAND_BUFFER_WL,
buffer, attribs);
if (!gs->images[i]) {
weston_log("failed to create img for plane %d\n", i);
continue;
}
gs->num_images++;
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(gs->target, gs->textures[i]);
gr->image_target_texture_2d(gs->target,
gs->images[i]);
}
gs->pitch = buffer->width;
} else {
weston_log("unhandled buffer type!\n");
weston_buffer_reference(&gs->buffer_ref, NULL);
}
}
static void
gl_renderer_surface_set_color(struct weston_surface *surface,
float red, float green, float blue, float alpha)
{
struct gl_surface_state *gs = get_surface_state(surface);
struct gl_renderer *gr = get_renderer(surface->compositor);
gs->color[0] = red;
gs->color[1] = green;
gs->color[2] = blue;
gs->color[3] = alpha;
gs->shader = &gr->solid_shader;
}
static int
gl_renderer_create_surface(struct weston_surface *surface)
{
struct gl_surface_state *gs;
gs = calloc(1, sizeof *gs);
if (!gs)
return -1;
/* A buffer is never attached to solid color surfaces, yet
* they still go through texcoord computations. Do not divide
* by zero there.
*/
gs->pitch = 1;
pixman_region32_init(&gs->texture_damage);
surface->renderer_state = gs;
return 0;
}
static void
gl_renderer_destroy_surface(struct weston_surface *surface)
{
struct gl_surface_state *gs = get_surface_state(surface);
struct gl_renderer *gr = get_renderer(surface->compositor);
int i;
glDeleteTextures(gs->num_textures, gs->textures);
for (i = 0; i < gs->num_images; i++)
gr->destroy_image(gr->egl_display, gs->images[i]);
weston_buffer_reference(&gs->buffer_ref, NULL);
pixman_region32_fini(&gs->texture_damage);
free(gs);
}
static const char vertex_shader[] =
"uniform mat4 proj;\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"varying vec2 v_texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = proj * vec4(position, 0.0, 1.0);\n"
" v_texcoord = texcoord;\n"
"}\n";
/* Declare common fragment shader uniforms */
#define FRAGMENT_CONVERT_YUV \
" y *= alpha;\n" \
" u *= alpha;\n" \
" v *= alpha;\n" \
" gl_FragColor.r = y + 1.59602678 * v;\n" \
" gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
" gl_FragColor.b = y + 2.01723214 * u;\n" \
" gl_FragColor.a = alpha;\n"
static const char fragment_debug[] =
" gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
static const char fragment_brace[] =
"}\n";
static const char texture_fragment_shader_rgba[] =
"precision mediump float;\n"
"varying vec2 v_texcoord;\n"
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"void main()\n"
"{\n"
" gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
;
static const char texture_fragment_shader_rgbx[] =
"precision mediump float;\n"
"varying vec2 v_texcoord;\n"
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"void main()\n"
"{\n"
" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
" gl_FragColor.a = alpha;\n"
;
static const char texture_fragment_shader_egl_external[] =
"#extension GL_OES_EGL_image_external : require\n"
"precision mediump float;\n"
"varying vec2 v_texcoord;\n"
"uniform samplerExternalOES tex;\n"
"uniform float alpha;\n"
"void main()\n"
"{\n"
" gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
;
static const char texture_fragment_shader_y_uv[] =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"uniform sampler2D tex1;\n"
"varying vec2 v_texcoord;\n"
"uniform float alpha;\n"
"void main() {\n"
" float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
" float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
" float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
FRAGMENT_CONVERT_YUV
;
static const char texture_fragment_shader_y_u_v[] =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"uniform sampler2D tex1;\n"
"uniform sampler2D tex2;\n"
"varying vec2 v_texcoord;\n"
"uniform float alpha;\n"
"void main() {\n"
" float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
" float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
" float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
FRAGMENT_CONVERT_YUV
;
static const char texture_fragment_shader_y_xuxv[] =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"uniform sampler2D tex1;\n"
"varying vec2 v_texcoord;\n"
"uniform float alpha;\n"
"void main() {\n"
" float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
" float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
" float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
FRAGMENT_CONVERT_YUV
;
static const char solid_fragment_shader[] =
"precision mediump float;\n"
"uniform vec4 color;\n"
"uniform float alpha;\n"
"void main()\n"
"{\n"
" gl_FragColor = alpha * color\n;"
;
static int
compile_shader(GLenum type, int count, const char **sources)
{
GLuint s;
char msg[512];
GLint status;
s = glCreateShader(type);
glShaderSource(s, count, sources, NULL);
glCompileShader(s);
glGetShaderiv(s, GL_COMPILE_STATUS, &status);
if (!status) {
glGetShaderInfoLog(s, sizeof msg, NULL, msg);
weston_log("shader info: %s\n", msg);
return GL_NONE;
}
return s;
}
static int
shader_init(struct gl_shader *shader, struct weston_compositor *ec,
const char *vertex_source, const char *fragment_source)
{
char msg[512];
GLint status;
int count;
const char *sources[3];
struct gl_renderer *renderer = get_renderer(ec);
shader->vertex_shader =
compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
if (renderer->fragment_shader_debug) {
sources[0] = fragment_source;
sources[1] = fragment_debug;
sources[2] = fragment_brace;
count = 3;
} else {
sources[0] = fragment_source;
sources[1] = fragment_brace;
count = 2;
}
shader->fragment_shader =
compile_shader(GL_FRAGMENT_SHADER, count, sources);
shader->program = glCreateProgram();
glAttachShader(shader->program, shader->vertex_shader);
glAttachShader(shader->program, shader->fragment_shader);
glBindAttribLocation(shader->program, 0, "position");
glBindAttribLocation(shader->program, 1, "texcoord");
glLinkProgram(shader->program);
glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
if (!status) {
glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
weston_log("link info: %s\n", msg);
return -1;
}
shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
shader->color_uniform = glGetUniformLocation(shader->program, "color");
return 0;
}
static void
shader_release(struct gl_shader *shader)
{
glDeleteShader(shader->vertex_shader);
glDeleteShader(shader->fragment_shader);
glDeleteProgram(shader->program);
shader->vertex_shader = 0;
shader->fragment_shader = 0;
shader->program = 0;
}
static void
log_extensions(const char *name, const char *extensions)
{
const char *p, *end;
int l;
int len;
l = weston_log("%s:", name);
p = extensions;
while (*p) {
end = strchrnul(p, ' ');
len = end - p;
if (l + len > 78)
l = weston_log_continue("\n" STAMP_SPACE "%.*s",
len, p);
else
l += weston_log_continue(" %.*s", len, p);
for (p = end; isspace(*p); p++)
;
}
weston_log_continue("\n");
}
static void
log_egl_gl_info(EGLDisplay egldpy)
{
const char *str;
str = eglQueryString(egldpy, EGL_VERSION);
weston_log("EGL version: %s\n", str ? str : "(null)");
str = eglQueryString(egldpy, EGL_VENDOR);
weston_log("EGL vendor: %s\n", str ? str : "(null)");
str = eglQueryString(egldpy, EGL_CLIENT_APIS);
weston_log("EGL client APIs: %s\n", str ? str : "(null)");
str = eglQueryString(egldpy, EGL_EXTENSIONS);
log_extensions("EGL extensions", str ? str : "(null)");
str = (char *)glGetString(GL_VERSION);
weston_log("GL version: %s\n", str ? str : "(null)");
str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
weston_log("GLSL version: %s\n", str ? str : "(null)");
str = (char *)glGetString(GL_VENDOR);
weston_log("GL vendor: %s\n", str ? str : "(null)");
str = (char *)glGetString(GL_RENDERER);
weston_log("GL renderer: %s\n", str ? str : "(null)");
str = (char *)glGetString(GL_EXTENSIONS);
log_extensions("GL extensions", str ? str : "(null)");
}
static void
log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
{
EGLint r, g, b, a;
weston_log("Chosen EGL config details:\n");
weston_log_continue(STAMP_SPACE "RGBA bits");
if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
weston_log_continue(": %d %d %d %d\n", r, g, b, a);
else
weston_log_continue(" unknown\n");
weston_log_continue(STAMP_SPACE "swap interval range");
if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
weston_log_continue(": %d - %d\n", a, b);
else
weston_log_continue(" unknown\n");
}
static void
output_apply_border(struct weston_output *output, struct gl_renderer *gr)
{
output->border.top = gr->border.top;
output->border.bottom = gr->border.bottom;
output->border.left = gr->border.left;
output->border.right = gr->border.right;
}
WL_EXPORT void
gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
int32_t *edges)
{
struct gl_renderer *gr = get_renderer(ec);
struct weston_output *output;
gr->border.left = edges[0];
gr->border.right = edges[1];
gr->border.top = edges[2];
gr->border.bottom = edges[3];
gr->border.width = width;
gr->border.height = height;
glGenTextures(1, &gr->border.texture);
glBindTexture(GL_TEXTURE_2D, gr->border.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
width,
height,
0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
data);
wl_list_for_each(output, &ec->output_list, link)
output_apply_border(output, gr);
}
static int
gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
WL_EXPORT int
gl_renderer_output_create(struct weston_output *output,
EGLNativeWindowType window)
{
struct weston_compositor *ec = output->compositor;
struct gl_renderer *gr = get_renderer(ec);
struct gl_output_state *go = calloc(1, sizeof *go);
int i;
if (!go)
return -1;
go->egl_surface =
eglCreateWindowSurface(gr->egl_display,
gr->egl_config,
window, NULL);
if (go->egl_surface == EGL_NO_SURFACE) {
weston_log("failed to create egl surface\n");
free(go);
return -1;
}
if (gr->egl_context == NULL)
if (gl_renderer_setup(ec, go->egl_surface) < 0) {
free(go);
return -1;
}
go->current_buffer = 0;
for (i = 0; i < 2; i++)
pixman_region32_init(&go->buffer_damage[i]);
output->renderer_state = go;
output_apply_border(output, gr);
return 0;
}
WL_EXPORT void
gl_renderer_output_destroy(struct weston_output *output)
{
struct gl_renderer *gr = get_renderer(output->compositor);
struct gl_output_state *go = get_output_state(output);
int i;
for (i = 0; i < 2; i++)
pixman_region32_fini(&go->buffer_damage[i]);
eglDestroySurface(gr->egl_display, go->egl_surface);
free(go);
}
WL_EXPORT EGLSurface
gl_renderer_output_surface(struct weston_output *output)
{
return get_output_state(output)->egl_surface;
}
static void
gl_renderer_destroy(struct weston_compositor *ec)
{
struct gl_renderer *gr = get_renderer(ec);
if (gr->has_bind_display)
gr->unbind_display(gr->egl_display, ec->wl_display);
/* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
eglMakeCurrent(gr->egl_display,
EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
eglTerminate(gr->egl_display);
eglReleaseThread();
}
static int
egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
const EGLint *visual_id)
{
EGLint count = 0;
EGLint matched = 0;
EGLConfig *configs;
int i;
if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
return -1;
configs = calloc(count, sizeof *configs);
if (!configs)
return -1;
if (!eglChooseConfig(gr->egl_display, attribs, configs,
count, &matched))
goto out;
for (i = 0; i < matched; ++i) {
EGLint id;
if (visual_id) {
if (!eglGetConfigAttrib(gr->egl_display,
configs[i], EGL_NATIVE_VISUAL_ID,
&id))
continue;
if (id != *visual_id)
continue;
}
gr->egl_config = configs[i];
free(configs);
return 0;
}
out:
free(configs);
return -1;
}
WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 1,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
WL_EXPORT int
gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
const EGLint *attribs, const EGLint *visual_id)
{
struct gl_renderer *gr;
EGLint major, minor;
gr = calloc(1, sizeof *gr);
if (gr == NULL)
return -1;
gr->base.read_pixels = gl_renderer_read_pixels;
gr->base.repaint_output = gl_renderer_repaint_output;
gr->base.flush_damage = gl_renderer_flush_damage;
gr->base.attach = gl_renderer_attach;
gr->base.create_surface = gl_renderer_create_surface;
gr->base.surface_set_color = gl_renderer_surface_set_color;
gr->base.destroy_surface = gl_renderer_destroy_surface;
gr->base.destroy = gl_renderer_destroy;
gr->egl_display = eglGetDisplay(display);
if (gr->egl_display == EGL_NO_DISPLAY) {
weston_log("failed to create display\n");
goto err_egl;
}
if (!eglInitialize(gr->egl_display, &major, &minor)) {
weston_log("failed to initialize display\n");
goto err_egl;
}
if (egl_choose_config(gr, attribs, visual_id) < 0) {
weston_log("failed to choose EGL config\n");
goto err_egl;
}
ec->renderer = &gr->base;
return 0;
err_egl:
gl_renderer_print_egl_error_state();
free(gr);
return -1;
}
WL_EXPORT EGLDisplay
gl_renderer_display(struct weston_compositor *ec)
{
return get_renderer(ec)->egl_display;
}
static int
compile_shaders(struct weston_compositor *ec)
{
struct gl_renderer *gr = get_renderer(ec);
if (shader_init(&gr->texture_shader_rgba, ec,
vertex_shader, texture_fragment_shader_rgba) < 0)
return -1;
if (shader_init(&gr->texture_shader_rgbx, ec,
vertex_shader, texture_fragment_shader_rgbx) < 0)
return -1;
if (gr->has_egl_image_external &&
shader_init(&gr->texture_shader_egl_external, ec,
vertex_shader, texture_fragment_shader_egl_external) < 0)
return -1;
if (shader_init(&gr->texture_shader_y_uv, ec,
vertex_shader, texture_fragment_shader_y_uv) < 0)
return -1;
if (shader_init(&gr->texture_shader_y_u_v, ec,
vertex_shader, texture_fragment_shader_y_u_v) < 0)
return -1;
if (shader_init(&gr->texture_shader_y_xuxv, ec,
vertex_shader, texture_fragment_shader_y_xuxv) < 0)
return -1;
if (shader_init(&gr->solid_shader, ec,
vertex_shader, solid_fragment_shader) < 0)
return -1;
return 0;
}
static void
fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
void *data)
{
struct weston_compositor *ec = data;
struct gl_renderer *gr = get_renderer(ec);
struct weston_output *output;
gr->fragment_shader_debug ^= 1;
shader_release(&gr->texture_shader_rgba);
shader_release(&gr->texture_shader_rgbx);
shader_release(&gr->texture_shader_egl_external);
shader_release(&gr->texture_shader_y_uv);
shader_release(&gr->texture_shader_y_u_v);
shader_release(&gr->texture_shader_y_xuxv);
shader_release(&gr->solid_shader);
compile_shaders(ec);
/* Force use_shader() to call glUseProgram(), since we need to use
* the recompiled version of the shader. */
gr->current_shader = NULL;
wl_list_for_each(output, &ec->output_list, link)
weston_output_damage(output);
}
static int
gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
{
struct gl_renderer *gr = get_renderer(ec);
const char *extensions;
EGLBoolean ret;
static const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
weston_log("failed to bind EGL_OPENGL_ES_API\n");
gl_renderer_print_egl_error_state();
return -1;
}
log_egl_config_info(gr->egl_display, gr->egl_config);
gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
EGL_NO_CONTEXT, context_attribs);
if (gr->egl_context == NULL) {
weston_log("failed to create context\n");
gl_renderer_print_egl_error_state();
return -1;
}
ret = eglMakeCurrent(gr->egl_display, egl_surface,
egl_surface, gr->egl_context);
if (ret == EGL_FALSE) {
weston_log("Failed to make EGL context current.\n");
gl_renderer_print_egl_error_state();
return -1;
}
log_egl_gl_info(gr->egl_display);
gr->image_target_texture_2d =
(void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
gr->bind_display =
(void *) eglGetProcAddress("eglBindWaylandDisplayWL");
gr->unbind_display =
(void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
gr->query_buffer =
(void *) eglGetProcAddress("eglQueryWaylandBufferWL");
extensions = (const char *) glGetString(GL_EXTENSIONS);
if (!extensions) {
weston_log("Retrieving GL extension string failed.\n");
return -1;
}
if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
return -1;
}
if (strstr(extensions, "GL_EXT_read_format_bgra"))
ec->read_format = PIXMAN_a8r8g8b8;
else
ec->read_format = PIXMAN_a8b8g8r8;
if (strstr(extensions, "GL_EXT_unpack_subimage"))
gr->has_unpack_subimage = 1;
if (strstr(extensions, "GL_OES_EGL_image_external"))
gr->has_egl_image_external = 1;
extensions =
(const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
if (!extensions) {
weston_log("Retrieving EGL extension string failed.\n");
return -1;
}
if (strstr(extensions, "EGL_WL_bind_wayland_display"))
gr->has_bind_display = 1;
if (gr->has_bind_display) {
ret = gr->bind_display(gr->egl_display, ec->wl_display);
if (!ret)
gr->has_bind_display = 0;
}
glActiveTexture(GL_TEXTURE0);
if (compile_shaders(ec))
return -1;
weston_compositor_add_debug_binding(ec, KEY_S,
fragment_debug_binding, ec);
weston_log("GL ES 2 renderer features:\n");
weston_log_continue(STAMP_SPACE "read-back format: %s\n",
ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
gr->has_unpack_subimage ? "yes" : "no");
weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
gr->has_bind_display ? "yes" : "no");
return 0;
}
|