summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapiutils_egl.c
blob: dc5dfcb3927efe697f43aa78448a009ee690cdf8 (plain)
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
/*
 * gstvaapiutils_egl.c - EGL utilities
 *
 * Copyright (C) 2014 Intel Corporation
 *   Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 * 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
 */

#include "sysdeps.h"
#include "gstvaapiutils_egl.h"

typedef struct egl_message_s EglMessage;
struct egl_message_s
{
  EglObject base;
  EglContextRunFunc func;
  gpointer args;
};

static void
egl_message_finalize (EglMessage * msg)
{
}

/* ------------------------------------------------------------------------- */
// Utility functions

typedef struct gl_version_info_s GlVersionInfo;
struct gl_version_info_s
{
  guint gles_version;
  guint gl_api_bit;
  guint gl_api;
  const gchar *gl_api_name;
};

static const GlVersionInfo gl_version_info[] = {
  {0, EGL_OPENGL_BIT, EGL_OPENGL_API, "OpenGL"},
  {1, EGL_OPENGL_ES_BIT, EGL_OPENGL_ES_API, "OpenGL_ES"},
  {2, EGL_OPENGL_ES2_BIT, EGL_OPENGL_ES_API, "OpenGL_ES2"},
  {3, EGL_OPENGL_ES3_BIT_KHR, EGL_OPENGL_ES_API, "OpenGL_ES3"},
  {0,}
};

static const GlVersionInfo *
gl_version_info_lookup (guint gles_version)
{
  const GlVersionInfo *vinfo;

  for (vinfo = gl_version_info; vinfo->gl_api_bit != 0; vinfo++) {
    if (vinfo->gles_version == gles_version)
      return vinfo;
  }
  return NULL;
}

static const GlVersionInfo *
gl_version_info_lookup_by_api (guint api)
{
  const GlVersionInfo *vinfo;

  for (vinfo = gl_version_info; vinfo->gl_api_bit != 0; vinfo++) {
    if (api & vinfo->gl_api_bit)
      return vinfo;
  }
  return NULL;
}

static const GlVersionInfo *
gl_version_info_lookup_by_api_name (const gchar * name)
{
  const GlVersionInfo *vinfo;

  for (vinfo = gl_version_info; vinfo->gl_api_bit != 0; vinfo++) {
    if (g_strcmp0 (vinfo->gl_api_name, name) == 0)
      return vinfo;
  }
  return NULL;
}

static gboolean
g_strv_match_string (gchar ** extensions_list, const gchar * name)
{
  if (extensions_list) {
    for (; *extensions_list != NULL; extensions_list++) {
      if (g_strcmp0 (*extensions_list, name) == 0)
        return TRUE;
    }
  }
  return FALSE;
}

static gboolean
egl_find_attrib_value (const EGLint * attribs, EGLint type, EGLint * value_ptr)
{
  while (attribs[0] != EGL_NONE) {
    if (attribs[0] == type) {
      if (value_ptr)
        *value_ptr = attribs[1];
      return TRUE;
    }
    attribs += 2;
  }
  return FALSE;
}

/* ------------------------------------------------------------------------- */
// Basic objects

#define EGL_OBJECT(object) ((EglObject *)(object))
#define EGL_OBJECT_CLASS(klass) ((EglObjectClass *)(klass))

#define EGL_OBJECT_DEFINE_CLASS_WITH_CODE(TN, t_n, code)        \
static void                                                     \
G_PASTE(t_n,_finalize) (TN * object);                           \
                                                                \
static inline const EglObjectClass *                            \
G_PASTE(t_n,_class) (void)                                      \
{                                                               \
  static G_PASTE(TN,Class) g_class;                             \
  static gsize g_class_init = FALSE;                            \
                                                                \
  if (g_once_init_enter (&g_class_init)) {                      \
    GstVaapiMiniObjectClass *const object_class =               \
        GST_VAAPI_MINI_OBJECT_CLASS (&g_class);                 \
    code;                                                       \
    object_class->size = sizeof (TN);                           \
    object_class->finalize = (GDestroyNotify)                   \
      G_PASTE(t_n,_finalize);                                   \
    g_once_init_leave (&g_class_init, TRUE);                    \
  }                                                             \
  return EGL_OBJECT_CLASS (&g_class);                           \
}

#define EGL_OBJECT_DEFINE_CLASS(TN, t_n) \
  EGL_OBJECT_DEFINE_CLASS_WITH_CODE (TN, t_n, /**/)

static inline gpointer
egl_object_new (const EglObjectClass * klass)
{
  return gst_vaapi_mini_object_new (GST_VAAPI_MINI_OBJECT_CLASS (klass));
}

static inline gpointer
egl_object_new0 (const EglObjectClass * klass)
{
  return gst_vaapi_mini_object_new0 (GST_VAAPI_MINI_OBJECT_CLASS (klass));
}

typedef struct egl_object_class_s EglMessageClass;
typedef struct egl_object_class_s EglVTableClass;
typedef struct egl_object_class_s EglDisplayClass;
typedef struct egl_object_class_s EglConfigClass;
typedef struct egl_object_class_s EglContextClass;
typedef struct egl_object_class_s EglSurfaceClass;
typedef struct egl_object_class_s EglProgramClass;
typedef struct egl_object_class_s EglWindowClass;

EGL_OBJECT_DEFINE_CLASS (EglMessage, egl_message);
EGL_OBJECT_DEFINE_CLASS (EglVTable, egl_vtable);
EGL_OBJECT_DEFINE_CLASS (EglDisplay, egl_display);
EGL_OBJECT_DEFINE_CLASS (EglConfig, egl_config);
EGL_OBJECT_DEFINE_CLASS (EglContext, egl_context);
EGL_OBJECT_DEFINE_CLASS (EglSurface, egl_surface);
EGL_OBJECT_DEFINE_CLASS (EglProgram, egl_program);
EGL_OBJECT_DEFINE_CLASS (EglWindow, egl_window);

/* ------------------------------------------------------------------------- */
// Desktop OpenGL and OpenGL|ES dispatcher (vtable)

static GMutex gl_vtables_lock;
static EglVTable *gl_vtables[4];

#if (USE_GLES_VERSION_MASK & (1U << 0))
static const gchar *gl_library_names[] = {
  "libGL.la",
  "libGL.so.1",
  NULL
};
#endif

#if (USE_GLES_VERSION_MASK & (1U << 1))
static const gchar *gles1_library_names[] = {
  "libGLESv1_CM.la",
  "libGLESv1_CM.so.1",
  NULL
};
#endif

#if (USE_GLES_VERSION_MASK & (1U << 2))
static const gchar *gles2_library_names[] = {
  "libGLESv2.la",
  "libGLESv2.so.2",
  NULL
};
#endif

static const gchar **gl_library_names_group[] = {
#if (USE_GLES_VERSION_MASK & (1U << 0))
  gl_library_names,
#endif
  NULL
};

static const gchar **gles1_library_names_group[] = {
#if (USE_GLES_VERSION_MASK & (1U << 1))
  gles1_library_names,
#endif
  NULL
};

static const gchar **gles2_library_names_group[] = {
#if (USE_GLES_VERSION_MASK & (1U << 2))
  gles2_library_names,
#endif
  NULL
};

static const gchar **gles3_library_names_group[] = {
#if (USE_GLES_VERSION_MASK & (1U << 3))
  gles2_library_names,
#endif
  NULL
};

static const gchar ***
egl_vtable_get_library_names_group (guint gles_version)
{
  const gchar ***library_names_group;

  switch (gles_version) {
    case 0:
      library_names_group = gl_library_names_group;
      break;
    case 1:
      library_names_group = gles1_library_names_group;
      break;
    case 2:
      library_names_group = gles2_library_names_group;
      break;
    case 3:
      library_names_group = gles3_library_names_group;
      break;
    default:
      library_names_group = NULL;
      break;
  }
  return library_names_group;
}

static gboolean
egl_vtable_check_extension (EglVTable * vtable, EGLDisplay display,
    gboolean is_egl, const gchar * group_name, guint * group_ptr)
{
  gchar ***extensions_list;
  const gchar *extensions;

  g_return_val_if_fail (group_name != NULL, FALSE);
  g_return_val_if_fail (group_ptr != NULL, FALSE);

  if (*group_ptr > 0)
    return TRUE;

  GST_DEBUG ("check for %s extension %s", is_egl ? "EGL" : "GL", group_name);

  if (is_egl) {
    if (!vtable->egl_extensions) {
      extensions = eglQueryString (display, EGL_EXTENSIONS);
      if (!extensions)
        return FALSE;
      GST_DEBUG ("EGL extensions: %s", extensions);
      vtable->egl_extensions = g_strsplit (extensions, " ", 0);
    }
    extensions_list = &vtable->egl_extensions;
  } else {
    if (!vtable->gl_extensions) {
      extensions = (const gchar *) vtable->glGetString (GL_EXTENSIONS);
      if (!extensions)
        return FALSE;
      GST_DEBUG ("GL extensions: %s", extensions);
      vtable->gl_extensions = g_strsplit (extensions, " ", 0);
    }
    extensions_list = &vtable->gl_extensions;
  }
  if (!g_strv_match_string (*extensions_list, group_name))
    return FALSE;

  GST_LOG ("  found %s extension %s", is_egl ? "EGL" : "GL", group_name);
  (*group_ptr)++;
  return TRUE;
}

static gboolean
egl_vtable_load_symbol (EglVTable * vtable, EGLDisplay display, gboolean is_egl,
    const gchar * symbol_name, gpointer * symbol_ptr,
    const gchar * group_name, guint * group_ptr)
{
  void (*symbol) (void);

  if (group_ptr && !*group_ptr) {
    if (!egl_vtable_check_extension (vtable, display, is_egl, group_name,
            group_ptr))
      return FALSE;
  }

  if (is_egl) {
    symbol = eglGetProcAddress (symbol_name);
  } else {
    if (!g_module_symbol (vtable->base.handle.p, symbol_name,
            (gpointer *) & symbol))
      return FALSE;
  }
  if (!symbol)
    return FALSE;

  GST_LOG ("  found symbol %s", symbol_name);
  if (symbol_ptr)
    *symbol_ptr = symbol;
  if (group_ptr)
    (*group_ptr)++;
  return TRUE;
}

static gboolean
egl_vtable_load_egl_symbols (EglVTable * vtable, EGLDisplay display)
{
  guint n = 0;

#define EGL_DEFINE_EXTENSION(NAME) do {                         \
      egl_vtable_check_extension (vtable, display, TRUE,        \
          "EGL_" G_STRINGIFY (NAME),                            \
          &vtable->GL_PROTO_GEN_CONCAT(has_EGL_,NAME));         \
    } while (0);

#define EGL_PROTO_BEGIN(NAME, TYPE, EXTENSION) do {             \
      n += egl_vtable_load_symbol (vtable, display, TRUE,       \
          G_STRINGIFY(GL_PROTO_GEN_CONCAT(egl,NAME)),           \
          (gpointer *) &vtable->GL_PROTO_GEN_CONCAT(egl,NAME),  \
          "EGL_" G_STRINGIFY(EXTENSION),                        \
          &vtable->GL_PROTO_GEN_CONCAT(has_EGL_,EXTENSION));    \
    } while (0);

#include "egl_vtable.h"

  vtable->num_egl_symbols = n;
  return TRUE;
}

static gboolean
egl_vtable_load_gl_symbols (EglVTable * vtable, EGLDisplay display)
{
  guint n = 0;

  vtable->has_GL_CORE_1_0 = 1;
  vtable->has_GL_CORE_1_1 = 1;
  vtable->has_GL_CORE_1_3 = 1;
  vtable->has_GL_CORE_2_0 = 1;

#define GL_DEFINE_EXTENSION(NAME) do {                          \
      egl_vtable_check_extension (vtable, display, FALSE,       \
          "GL_" G_STRINGIFY (NAME),                             \
          &vtable->GL_PROTO_GEN_CONCAT(has_GL_,NAME));          \
    } while (0);

#define GL_PROTO_BEGIN(NAME, TYPE, EXTENSION) do {              \
      n += egl_vtable_load_symbol (vtable, display, FALSE,      \
          G_STRINGIFY(GL_PROTO_GEN_CONCAT(gl,NAME)),            \
          (gpointer *) &vtable->GL_PROTO_GEN_CONCAT(gl,NAME),   \
          "GL_" G_STRINGIFY(EXTENSION),                         \
          &vtable->GL_PROTO_GEN_CONCAT(has_GL_,EXTENSION));     \
    } while (0);

#include "egl_vtable.h"

  --vtable->has_GL_CORE_1_0;
  --vtable->has_GL_CORE_1_1;
  --vtable->has_GL_CORE_1_3;
  --vtable->has_GL_CORE_2_0;

  vtable->num_gl_symbols = n;
  return TRUE;
}

static gboolean
egl_vtable_try_load_library (EglVTable * vtable, const gchar * name)
{
  if (vtable->base.handle.p)
    g_module_close (vtable->base.handle.p);
  vtable->base.handle.p = g_module_open (name,
      G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
  if (!vtable->base.handle.p)
    return FALSE;

  GST_DEBUG ("loaded backend: %s", g_module_name (vtable->base.handle.p));
  return TRUE;
}

static gboolean
egl_vtable_find_library (EglVTable * vtable)
{
  const gchar ***library_names_ptr =
      egl_vtable_get_library_names_group (vtable->gles_version);

  if (!library_names_ptr)
    return FALSE;

  for (; *library_names_ptr != NULL; library_names_ptr++) {
    const gchar **library_name_ptr = *library_names_ptr;
    for (; *library_name_ptr != NULL; library_name_ptr++) {
      if (egl_vtable_try_load_library (vtable, *library_name_ptr))
        return TRUE;
    }
  }
  return FALSE;
}

static gboolean
egl_vtable_init (EglVTable * vtable, EGLDisplay display, guint gles_version)
{
  GST_DEBUG ("initialize for OpenGL|ES API version %d", gles_version);

  vtable->gles_version = gles_version;
  if (!egl_vtable_find_library (vtable))
    return FALSE;
  if (!egl_vtable_load_egl_symbols (vtable, display))
    return FALSE;
  return TRUE;
}

static void
egl_vtable_finalize (EglVTable * vtable)
{
  g_strfreev (vtable->egl_extensions);
  g_strfreev (vtable->gl_extensions);
  if (vtable->base.handle.p)
    g_module_close (vtable->base.handle.p);

  if (vtable->base.is_wrapped) {
    g_mutex_lock (&gl_vtables_lock);
    gl_vtables[vtable->gles_version] = NULL;
    g_mutex_unlock (&gl_vtables_lock);
  }
}

static EglVTable *
egl_vtable_new (EglDisplay * display, guint gles_version)
{
  EglVTable *vtable;

  g_return_val_if_fail (display != NULL, NULL);

  vtable = egl_object_new0 (egl_vtable_class ());
  if (!vtable
      || !egl_vtable_init (vtable, display->base.handle.p, gles_version))
    goto error;
  return vtable;

error:
  egl_object_replace (&vtable, NULL);
  return NULL;
}

static EglVTable *
egl_vtable_new_cached (EglDisplay * display, guint gles_version)
{
  EglVTable *vtable, **vtable_ptr;

  g_return_val_if_fail (gles_version < G_N_ELEMENTS (gl_vtables), NULL);

  vtable_ptr = &gl_vtables[gles_version];

  g_mutex_lock (&gl_vtables_lock);
  vtable = *vtable_ptr;
  if (vtable)
    egl_object_ref (vtable);
  else {
    vtable = egl_vtable_new (display, gles_version);
    if (vtable) {
      vtable->base.is_wrapped = TRUE;
      *vtable_ptr = vtable;
    }
  }
  g_mutex_unlock (&gl_vtables_lock);
  return vtable;
}

/* ------------------------------------------------------------------------- */
// EGL Display

static gboolean
egl_display_run (EglDisplay * display, EglContextRunFunc func, gpointer args)
{
  EglMessage *msg;

  if (display->gl_thread == g_thread_self ()) {
    func (args);
    return TRUE;
  }

  msg = egl_object_new0 (egl_message_class ());
  if (!msg)
    return FALSE;

  msg->base.is_valid = TRUE;
  msg->func = func;
  msg->args = args;
  g_async_queue_push (display->gl_queue, egl_object_ref (msg));

  g_mutex_lock (&display->mutex);
  while (msg->base.is_valid)
    g_cond_wait (&display->gl_thread_ready, &display->mutex);
  g_mutex_unlock (&display->mutex);
  egl_object_unref (msg);
  return TRUE;
}

static gpointer
egl_display_thread (gpointer data)
{
  EglDisplay *const display = data;
  EGLDisplay gl_display = display->base.handle.p;
  EGLint major_version, minor_version;
  gchar **gl_apis, **gl_api;

  if (!display->base.is_wrapped) {
    gl_display = display->base.handle.p = eglGetDisplay (gl_display);
    if (!gl_display)
      goto error;
    if (!eglInitialize (gl_display, &major_version, &minor_version))
      goto error;
  }

  display->gl_vendor_string =
      g_strdup (eglQueryString (gl_display, EGL_VENDOR));
  display->gl_version_string =
      g_strdup (eglQueryString (gl_display, EGL_VERSION));
  display->gl_apis_string =
      g_strdup (eglQueryString (gl_display, EGL_CLIENT_APIS));

  GST_INFO ("EGL vendor: %s", display->gl_vendor_string);
  GST_INFO ("EGL version: %s", display->gl_version_string);
  GST_INFO ("EGL client APIs: %s", display->gl_apis_string);

  gl_apis = g_strsplit (display->gl_apis_string, " ", 0);
  if (!gl_apis)
    goto error;
  for (gl_api = gl_apis; *gl_api != NULL; gl_api++) {
    const GlVersionInfo *const vinfo =
        gl_version_info_lookup_by_api_name (*gl_api);

    if (vinfo)
      display->gl_apis |= vinfo->gl_api_bit;
  }
  g_strfreev (gl_apis);
  if (!display->gl_apis)
    goto error;

  display->base.is_valid = TRUE;
  g_cond_broadcast (&display->gl_thread_ready);

  while (!display->gl_thread_cancel) {
    EglMessage *const msg =
        g_async_queue_timeout_pop (display->gl_queue, 100000);

    if (msg) {
      if (msg->base.is_valid) {
        msg->func (msg->args);
        msg->base.is_valid = FALSE;
        g_cond_broadcast (&display->gl_thread_ready);
      }
      egl_object_unref (msg);
    }
  }

done:
  if (gl_display != EGL_NO_DISPLAY && !display->base.is_wrapped)
    eglTerminate (gl_display);
  display->base.handle.p = NULL;
  g_cond_broadcast (&display->gl_thread_ready);
  return NULL;

error:
  display->base.is_valid = FALSE;
  goto done;
}

static gboolean
egl_display_init (EglDisplay * display)
{
  display->gl_queue =
      g_async_queue_new_full ((GDestroyNotify) gst_vaapi_mini_object_unref);
  if (!display->gl_queue)
    return FALSE;

  g_mutex_init (&display->mutex);
  g_cond_init (&display->gl_thread_ready);
  display->gl_thread = g_thread_try_new ("OpenGL Thread", egl_display_thread,
      display, NULL);
  if (!display->gl_thread)
    return FALSE;

  g_mutex_lock (&display->mutex);
  g_cond_wait (&display->gl_thread_ready, &display->mutex);
  g_mutex_unlock (&display->mutex);
  return display->base.is_valid;
}

static void
egl_display_finalize (EglDisplay * display)
{
  display->gl_thread_cancel = TRUE;
  g_thread_join (display->gl_thread);
  g_cond_clear (&display->gl_thread_ready);
  g_mutex_clear (&display->mutex);
  g_async_queue_unref (display->gl_queue);

  g_free (display->gl_vendor_string);
  g_free (display->gl_version_string);
  g_free (display->gl_apis_string);
}

static EglDisplay *
egl_display_new_full (gpointer handle, gboolean is_wrapped)
{
  EglDisplay *display;

  display = egl_object_new0 (egl_display_class ());
  if (!display)
    return NULL;

  display->base.handle.p = handle;
  display->base.is_wrapped = is_wrapped;
  if (!egl_display_init (display))
    goto error;
  return display;

error:
  egl_object_unref (display);
  return NULL;
}

EglDisplay *
egl_display_new (gpointer native_display)
{
  g_return_val_if_fail (native_display != NULL, NULL);

  return egl_display_new_full (native_display, FALSE);
}

EglDisplay *
egl_display_new_wrapped (EGLDisplay gl_display)
{
  g_return_val_if_fail (gl_display != EGL_NO_DISPLAY, NULL);

  return egl_display_new_full (gl_display, TRUE);
}

/* ------------------------------------------------------------------------- */
// EGL Config

static gboolean
egl_config_init (EglConfig * config, EglDisplay * display,
    const EGLint * attribs)
{
  EGLDisplay const gl_display = display->base.handle.p;
  const GlVersionInfo *vinfo;
  EGLConfig gl_config;
  EGLint v, gl_apis, num_configs;

  egl_object_replace (&config->display, display);

  if (!eglChooseConfig (gl_display, attribs, &gl_config, 1, &num_configs))
    return FALSE;
  if (num_configs != 1)
    return FALSE;
  config->base.handle.p = gl_config;

  if (!eglGetConfigAttrib (gl_display, gl_config, EGL_CONFIG_ID, &v))
    return FALSE;
  config->config_id = v;

  if (!eglGetConfigAttrib (gl_display, gl_config, EGL_NATIVE_VISUAL_ID, &v))
    return FALSE;
  config->visual_id = v;

  if (!eglGetConfigAttrib (gl_display, gl_config, EGL_RENDERABLE_TYPE, &v))
    return FALSE;
  if (!egl_find_attrib_value (attribs, EGL_RENDERABLE_TYPE, &gl_apis))
    return FALSE;
  vinfo = gl_version_info_lookup_by_api (v & gl_apis);
  if (!vinfo)
    return FALSE;
  config->gles_version = vinfo->gles_version;
  config->gl_api = vinfo->gles_version > 0 ? EGL_OPENGL_ES_API : EGL_OPENGL_API;
  return TRUE;
}

static void
egl_config_finalize (EglConfig * config)
{
  egl_object_replace (&config->display, NULL);
}

EglConfig *
egl_config_new (EglDisplay * display, guint gles_version, GstVideoFormat format)
{
  EGLint attribs[2 * 6 + 1], *attrib = attribs;
  const GstVideoFormatInfo *finfo;
  const GlVersionInfo *vinfo;

  g_return_val_if_fail (display != NULL, NULL);

  finfo = gst_video_format_get_info (format);
  if (!finfo || !GST_VIDEO_FORMAT_INFO_IS_RGB (finfo))
    return NULL;

  vinfo = gl_version_info_lookup (gles_version);
  if (!vinfo)
    return NULL;

  *attrib++ = EGL_COLOR_BUFFER_TYPE;
  *attrib++ = EGL_RGB_BUFFER;
  *attrib++ = EGL_RED_SIZE;
  *attrib++ = GST_VIDEO_FORMAT_INFO_DEPTH (finfo, GST_VIDEO_COMP_R);
  *attrib++ = EGL_GREEN_SIZE;
  *attrib++ = GST_VIDEO_FORMAT_INFO_DEPTH (finfo, GST_VIDEO_COMP_G);
  *attrib++ = EGL_BLUE_SIZE;
  *attrib++ = GST_VIDEO_FORMAT_INFO_DEPTH (finfo, GST_VIDEO_COMP_B);
  *attrib++ = EGL_ALPHA_SIZE;
  *attrib++ = GST_VIDEO_FORMAT_INFO_DEPTH (finfo, GST_VIDEO_COMP_A);
  *attrib++ = EGL_RENDERABLE_TYPE;
  *attrib++ = vinfo->gl_api_bit;
  *attrib++ = EGL_NONE;
  g_assert (attrib - attribs <= G_N_ELEMENTS (attribs));

  return egl_config_new_with_attribs (display, attribs);
}

EglConfig *
egl_config_new_with_attribs (EglDisplay * display, const EGLint * attribs)
{
  EglConfig *config;

  g_return_val_if_fail (display != NULL, NULL);
  g_return_val_if_fail (attribs != NULL, NULL);

  config = egl_object_new0 (egl_config_class ());
  if (!config || !egl_config_init (config, display, attribs))
    goto error;
  return config;

error:
  egl_object_replace (&config, NULL);
  return NULL;
}

static EglConfig *
egl_config_new_from_gl_context (EglDisplay * display, EGLContext gl_context)
{
  EGLDisplay const gl_display = display->base.handle.p;
  EGLint attribs[3 * 2 + 1], *attrib = attribs;
  EGLint config_id, api, v;
  guint gles_version;
  const GlVersionInfo *vinfo;

  if (!eglQueryContext (gl_display, gl_context, EGL_CONFIG_ID, &config_id))
    return NULL;
  if (!eglQueryContext (gl_display, gl_context, EGL_CONTEXT_CLIENT_TYPE, &api))
    return NULL;
  if (!eglQueryContext (gl_display, gl_context, EGL_CONTEXT_CLIENT_VERSION, &v))
    return NULL;

  if (api == EGL_OPENGL_API)
    gles_version = 0;
  else if (api == EGL_OPENGL_ES_API)
    gles_version = v;
  else {
    GST_ERROR ("unsupported EGL client API (%d)", api);
    return NULL;
  }

  vinfo = gl_version_info_lookup (gles_version);
  if (!vinfo)
    return NULL;

  *attrib++ = EGL_COLOR_BUFFER_TYPE;
  *attrib++ = EGL_RGB_BUFFER;
  *attrib++ = EGL_CONFIG_ID;
  *attrib++ = config_id;
  *attrib++ = EGL_RENDERABLE_TYPE;
  *attrib++ = vinfo->gl_api_bit;
  *attrib++ = EGL_NONE;
  g_assert (attrib - attribs <= G_N_ELEMENTS (attribs));

  return egl_config_new_with_attribs (display, attribs);
}

/* ------------------------------------------------------------------------- */
// EGL Surface

static void
egl_surface_finalize (EglSurface * surface)
{
  if (surface->base.handle.p != EGL_NO_SURFACE && !surface->base.is_wrapped)
    eglDestroySurface (surface->display->base.handle.p, surface->base.handle.p);
  egl_object_replace (&surface->display, NULL);
}

static EglSurface *
egl_surface_new_wrapped (EglDisplay * display, EGLSurface gl_surface)
{
  EglSurface *surface;

  g_return_val_if_fail (display != NULL, NULL);

  surface = egl_object_new (egl_surface_class ());
  if (!surface)
    return NULL;

  surface->base.is_wrapped = TRUE;
  surface->base.handle.p = gl_surface;
  surface->display = egl_object_ref (display);
  return surface;
}

/* ------------------------------------------------------------------------- */
// EGL Context

static void egl_context_state_get_current (EglContextState * cs);

static gboolean
egl_context_state_set_current (EglContextState * new_cs,
    EglContextState * old_cs);

static gboolean
ensure_vtable (EglContext * ctx)
{
  if (!ctx->vtable) {
    ctx->vtable = egl_vtable_new_cached (ctx->display,
        ctx->config ? ctx->config->gles_version : 0);
    if (!ctx->vtable)
      return FALSE;
  }
  return TRUE;
}

static gboolean
ensure_context (EglContext * ctx, EGLContext gl_parent_context)
{
  EGLDisplay *const gl_display = ctx->display->base.handle.p;
  EGLContext gl_context = ctx->base.handle.p;
  EGLint gles_attribs[3], *attribs = NULL;

  if (!gl_context) {
    if (ctx->config->gles_version >= 2) {
      attribs = gles_attribs;
      attribs[0] = EGL_CONTEXT_CLIENT_VERSION;
      attribs[1] = ctx->config->gles_version;
      attribs[2] = EGL_NONE;
    }

    gl_context = eglCreateContext (gl_display, ctx->config->base.handle.p,
        gl_parent_context, attribs);
    if (!gl_context)
      goto error_create_context;
    ctx->base.handle.p = gl_context;
  }
  return TRUE;

  /* ERRORS */
error_create_context:
  GST_ERROR ("failed to create EGL context");
  return FALSE;
}

static inline gboolean
ensure_gl_is_surfaceless (EglContext * ctx)
{
  return ctx->vtable->has_EGL_KHR_surfaceless_context ||
      (ctx->read_surface && ctx->draw_surface);
}

static gboolean
ensure_gl_scene (EglContext * ctx)
{
  EglVTable *vtable;

  if (!ensure_gl_is_surfaceless (ctx))
    return FALSE;

  if (ctx->base.is_valid)
    return TRUE;

  vtable = egl_context_get_vtable (ctx, TRUE);
  if (!vtable)
    return FALSE;

  vtable->glClearColor (0.0, 0.0, 0.0, 1.0);
  if (ctx->config && ctx->config->gles_version == 0)
    vtable->glEnable (GL_TEXTURE_2D);
  vtable->glDisable (GL_BLEND);
  vtable->glDisable (GL_DEPTH_TEST);

  ctx->base.is_valid = TRUE;
  return TRUE;
}

/**
 * egl_context_state_get_current:
 * @cs: return location to the current #EglContextState
 *
 * Retrieves the current EGL context, display and surface to pack into
 * the #EglContextState struct.
 */
static void
egl_context_state_get_current (EglContextState * cs)
{
  cs->display = eglGetCurrentDisplay ();
  cs->context = eglGetCurrentContext ();
  if (cs->context) {
    cs->read_surface = eglGetCurrentSurface (EGL_READ);
    cs->draw_surface = eglGetCurrentSurface (EGL_DRAW);
  } else {
    cs->read_surface = EGL_NO_SURFACE;
    cs->draw_surface = EGL_NO_SURFACE;
  }
}

/**
 * egl_context_state_set_current:
 * @new_cs: the requested new #EglContextState
 * @old_cs: return location to the context that was previously current
 *
 * Makes the @new_cs EGL context the current EGL rendering context of
 * the calling thread, replacing the previously current context if
 * there was one.
 *
 * If @old_cs is non %NULL, the previously current EGL context and
 * surface are recorded.
 *
 * Return value: %TRUE on success
 */
static gboolean
egl_context_state_set_current (EglContextState * new_cs,
    EglContextState * old_cs)
{
  /* If display is NULL, this could be that new_cs was retrieved from
     egl_context_state_get_current() with none set previously. If that case,
     the other fields are also NULL and we don't return an error */
  if (!new_cs->display)
    return !new_cs->context && !new_cs->read_surface && !new_cs->draw_surface;

  if (old_cs) {
    if (old_cs == new_cs)
      return TRUE;
    egl_context_state_get_current (old_cs);
    if (old_cs->display == new_cs->display &&
        old_cs->context == new_cs->context &&
        old_cs->read_surface == new_cs->read_surface &&
        old_cs->draw_surface == new_cs->draw_surface)
      return TRUE;
  }
  return eglMakeCurrent (new_cs->display, new_cs->draw_surface,
      new_cs->read_surface, new_cs->context);
}

static gboolean
egl_context_init (EglContext * ctx, EglDisplay * display, EglConfig * config,
    EGLContext gl_parent_context)
{
  egl_object_replace (&ctx->display, display);
  egl_object_replace (&ctx->config, config);

  if (config)
    eglBindAPI (config->gl_api);

  if (!ensure_vtable (ctx))
    return FALSE;
  if (!ensure_context (ctx, gl_parent_context))
    return FALSE;
  return TRUE;
}

static void
egl_context_finalize (EglContext * ctx)
{
  if (ctx->base.handle.p && !ctx->base.is_wrapped)
    eglDestroyContext (ctx->display->base.handle.p, ctx->base.handle.p);
  egl_object_replace (&ctx->read_surface, NULL);
  egl_object_replace (&ctx->draw_surface, NULL);
  egl_object_replace (&ctx->config, NULL);
  egl_object_replace (&ctx->display, NULL);
  egl_object_replace (&ctx->vtable, NULL);
}

typedef struct
{
  EglDisplay *display;
  EglConfig *config;
  EGLContext gl_parent_context;
  EglContext *context;          /* result */
} CreateContextArgs;

static void
do_egl_context_new (CreateContextArgs * args)
{
  EglContext *ctx;

  ctx = egl_object_new0 (egl_context_class ());
  if (!ctx || !egl_context_init (ctx, args->display, args->config,
          args->gl_parent_context))
    goto error;
  args->context = ctx;
  return;

error:
  egl_object_replace (&ctx, NULL);
  args->context = NULL;
}

EglContext *
egl_context_new (EglDisplay * display, EglConfig * config, EglContext * parent)
{
  CreateContextArgs args;

  g_return_val_if_fail (display != NULL, NULL);
  g_return_val_if_fail (config != NULL, NULL);

  args.display = display;
  args.config = config;
  args.gl_parent_context = parent ? parent->base.handle.p : EGL_NO_CONTEXT;
  if (!egl_display_run (display, (EglContextRunFunc) do_egl_context_new, &args))
    return NULL;
  return args.context;
}

EglContext *
egl_context_new_wrapped (EglDisplay * display, EGLContext gl_context)
{
  CreateContextArgs args;
  EglConfig *config;
  gboolean success;

  g_return_val_if_fail (display != NULL, NULL);
  g_return_val_if_fail (gl_context != EGL_NO_CONTEXT, NULL);

  config = egl_config_new_from_gl_context (display, gl_context);
  if (!config)
    return NULL;

  args.display = display;
  args.config = config;
  args.gl_parent_context = gl_context;
  success = egl_display_run (display, (EglContextRunFunc) do_egl_context_new,
      &args);
  egl_object_unref (config);
  if (!success)
    return NULL;
  return args.context;
}

EglVTable *
egl_context_get_vtable (EglContext * ctx, gboolean need_gl_symbols)
{
  g_return_val_if_fail (ctx != NULL, NULL);
  g_return_val_if_fail (ctx->display->gl_thread == g_thread_self (), NULL);

  if (!ensure_vtable (ctx))
    return NULL;

  if (need_gl_symbols && !(ctx->vtable->num_gl_symbols > 0 ||
          egl_vtable_load_gl_symbols (ctx->vtable,
              ctx->display->base.handle.p)))
    return NULL;
  return ctx->vtable;
}

static void
egl_context_set_surface (EglContext * ctx, EglSurface * surface)
{
  g_return_if_fail (ctx != NULL);
  g_return_if_fail (surface != NULL);

  egl_object_replace (&ctx->read_surface, surface);
  egl_object_replace (&ctx->draw_surface, surface);
}

gboolean
egl_context_set_current (EglContext * ctx, gboolean activate,
    EglContextState * old_cs)
{
  EglContextState cs, *new_cs;

  g_return_val_if_fail (ctx != NULL, FALSE);
  g_return_val_if_fail (ctx->display->gl_thread == g_thread_self (), FALSE);

  if (activate) {
    new_cs = &cs;
    new_cs->display = ctx->display->base.handle.p;
    new_cs->context = ctx->base.handle.p;
    new_cs->draw_surface = ctx->draw_surface ?
        ctx->draw_surface->base.handle.p : EGL_NO_SURFACE;
    new_cs->read_surface = ctx->read_surface ?
        ctx->read_surface->base.handle.p : EGL_NO_SURFACE;
  } else if (old_cs) {
    new_cs = old_cs;
    old_cs = NULL;
  } else {
    new_cs = &cs;
    new_cs->display = ctx->display->base.handle.p;
    new_cs->context = EGL_NO_CONTEXT;
    new_cs->draw_surface = EGL_NO_SURFACE;
    new_cs->read_surface = EGL_NO_SURFACE;
    old_cs = NULL;
  }

  if (!egl_context_state_set_current (new_cs, old_cs))
    return FALSE;
  if (activate && !ensure_gl_scene (ctx))
    return FALSE;
  return TRUE;
}

gboolean
egl_context_run (EglContext * ctx, EglContextRunFunc func, gpointer args)
{
  g_return_val_if_fail (ctx != NULL, FALSE);
  g_return_val_if_fail (func != NULL, FALSE);

  return egl_display_run (ctx->display, func, args);
}

/* ------------------------------------------------------------------------- */
// EGL Program

static GLuint
egl_compile_shader (EglContext * ctx, GLenum type, const char *source)
{
  EglVTable *const vtable = egl_context_get_vtable (ctx, TRUE);
  GLuint shader;
  GLint status;
  char log[BUFSIZ];
  GLsizei log_length;

  shader = vtable->glCreateShader (type);
  vtable->glShaderSource (shader, 1, &source, NULL);
  vtable->glCompileShader (shader);
  vtable->glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
  if (!status) {
    GST_ERROR ("failed to compile %s shader",
        type == GL_FRAGMENT_SHADER ? "fragment" :
        type == GL_VERTEX_SHADER ? "vertex" : "<unknown>");

    vtable->glGetShaderInfoLog (shader, sizeof (log), &log_length, log);
    GST_ERROR ("info log: %s", log);
    return 0;
  }
  return shader;
}

static void
egl_program_finalize (EglProgram * program)
{
  EglVTable *const vtable = program->vtable;

  if (program->base.handle.u)
    vtable->glDeleteProgram (program->base.handle.u);
  if (program->frag_shader)
    vtable->glDeleteShader (program->frag_shader);
  if (program->vert_shader)
    vtable->glDeleteShader (program->vert_shader);
  egl_object_replace (&program->vtable, NULL);
}

static gboolean
egl_program_init (EglProgram * program, EglContext * ctx,
    const gchar * frag_shader_text, const gchar * vert_shader_text)
{
  EglVTable *const vtable = egl_context_get_vtable (ctx, TRUE);
  GLuint prog_id;
  char msg[BUFSIZ];
  GLsizei msglen;
  GLint status;

  if (ctx->config->gles_version == 1)
    goto error_unsupported_gles_version;

  program->vtable = egl_object_ref (vtable);

  program->frag_shader =
      egl_compile_shader (ctx, GL_FRAGMENT_SHADER, frag_shader_text);
  if (!program->frag_shader)
    return FALSE;

  program->vert_shader =
      egl_compile_shader (ctx, GL_VERTEX_SHADER, vert_shader_text);
  if (!program->vert_shader)
    return FALSE;

  prog_id = vtable->glCreateProgram ();
  if (!prog_id)
    return FALSE;
  program->base.handle.u = prog_id;

  vtable->glAttachShader (prog_id, program->frag_shader);
  vtable->glAttachShader (prog_id, program->vert_shader);
  vtable->glBindAttribLocation (prog_id, 0, "position");
  vtable->glBindAttribLocation (prog_id, 1, "texcoord");
  vtable->glLinkProgram (prog_id);

  vtable->glGetProgramiv (prog_id, GL_LINK_STATUS, &status);
  if (!status)
    goto error_link_program;
  return TRUE;

  /* ERRORS */
error_unsupported_gles_version:
  GST_ERROR ("unsupported shader with OpenGL|ES version 1");
  return FALSE;
error_link_program:
  vtable->glGetProgramInfoLog (prog_id, sizeof (msg), &msglen, msg);
  GST_ERROR ("failed to link program: %s", msg);
  return FALSE;
}

EglProgram *
egl_program_new (EglContext * ctx, const gchar * frag_shader_text,
    const gchar * vert_shader_text)
{
  EglProgram *program;

  g_return_val_if_fail (ctx != NULL, NULL);
  g_return_val_if_fail (frag_shader_text != NULL, NULL);
  g_return_val_if_fail (vert_shader_text != NULL, NULL);

  program = egl_object_new0 (egl_program_class ());
  if (!program
      || !egl_program_init (program, ctx, frag_shader_text, vert_shader_text))
    goto error;
  return program;

error:
  egl_object_replace (&program, NULL);
  return NULL;
}

/* ------------------------------------------------------------------------- */
// EGL Window

static gboolean
egl_window_init (EglWindow * window, EglContext * ctx, gpointer native_window)
{
  EGLSurface gl_surface;

  window->context = egl_context_new (ctx->display, ctx->config, ctx);
  if (!window->context)
    return FALSE;
  ctx = window->context;

  gl_surface = eglCreateWindowSurface (ctx->display->base.handle.p,
      ctx->config->base.handle.p, (EGLNativeWindowType) native_window, NULL);
  if (!gl_surface)
    return FALSE;

  window->surface = egl_surface_new_wrapped (ctx->display, gl_surface);
  if (!window->surface)
    goto error_create_surface;
  window->base.handle.p = gl_surface;
  window->base.is_wrapped = FALSE;

  egl_context_set_surface (ctx, window->surface);
  return TRUE;

  /* ERRORS */
error_create_surface:
  GST_ERROR ("failed to create EGL wrapper surface");
  eglDestroySurface (ctx->display->base.handle.p, gl_surface);
  return FALSE;
}

static void
egl_window_finalize (EglWindow * window)
{
  if (window->context && window->base.handle.p)
    eglDestroySurface (window->context->display->base.handle.p,
        window->base.handle.p);

  egl_object_replace (&window->surface, NULL);
  egl_object_replace (&window->context, NULL);
}

EglWindow *
egl_window_new (EglContext * ctx, gpointer native_window)
{
  EglWindow *window;

  g_return_val_if_fail (ctx != NULL, NULL);
  g_return_val_if_fail (native_window != NULL, NULL);

  window = egl_object_new0 (egl_window_class ());
  if (!window || !egl_window_init (window, ctx, native_window))
    goto error;
  return window;

error:
  egl_object_replace (&window, NULL);
  return NULL;
}

/* ------------------------------------------------------------------------- */
// Misc utility functions

void
egl_matrix_set_identity (gfloat m[16])
{
#define MAT(m,r,c) (m)[(c) * 4 + (r)]
  MAT (m, 0, 0) = 1.0;
  MAT (m, 0, 1) = 0.0;
  MAT (m, 0, 2) = 0.0;
  MAT (m, 0, 3) = 0.0;
  MAT (m, 1, 0) = 0.0;
  MAT (m, 1, 1) = 1.0;
  MAT (m, 1, 2) = 0.0;
  MAT (m, 1, 3) = 0.0;
  MAT (m, 2, 0) = 0.0;
  MAT (m, 2, 1) = 0.0;
  MAT (m, 2, 2) = 1.0;
  MAT (m, 2, 3) = 0.0;
  MAT (m, 3, 0) = 0.0;
  MAT (m, 3, 1) = 0.0;
  MAT (m, 3, 2) = 0.0;
  MAT (m, 3, 3) = 1.0;
#undef MAT
}

/**
 * egl_create_texture:
 * @ctx: the parent #EglContext object
 * @target: the target to which the texture is bound
 * @format: the format of the pixel data
 * @width: the requested width, in pixels
 * @height: the requested height, in pixels
 *
 * Creates a texture with the specified dimensions and @format. The
 * internal format will be automatically derived from @format.
 *
 * Return value: the newly created texture name
 */
guint
egl_create_texture (EglContext * ctx, guint target, guint format,
    guint width, guint height)
{
  EglVTable *const vtable = egl_context_get_vtable (ctx, TRUE);
  guint internal_format, texture, bytes_per_component;

  internal_format = format;
  switch (format) {
    case GL_LUMINANCE:
      bytes_per_component = 1;
      break;
    case GL_LUMINANCE_ALPHA:
      bytes_per_component = 2;
      break;
    case GL_RGBA:
    case GL_BGRA_EXT:
      internal_format = GL_RGBA;
      bytes_per_component = 4;
      break;
    default:
      bytes_per_component = 0;
      break;
  }
  g_assert (bytes_per_component > 0);

  vtable->glGenTextures (1, &texture);
  vtable->glBindTexture (target, texture);

  if (width > 0 && height > 0)
    vtable->glTexImage2D (target, 0, internal_format, width, height, 0,
        format, GL_UNSIGNED_BYTE, NULL);

  vtable->glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  vtable->glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  vtable->glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  vtable->glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  vtable->glPixelStorei (GL_UNPACK_ALIGNMENT, bytes_per_component);

  return texture;
}

/**
 * egl_destroy_texture:
 * @ctx: the parent #EglContext object
 * @texture: the texture name to delete
 *
 * Destroys the supplied @texture name.
 */
void
egl_destroy_texture (EglContext * ctx, guint texture)
{
  EglVTable *const vtable = egl_context_get_vtable (ctx, TRUE);

  vtable->glDeleteTextures (1, &texture);
}