summaryrefslogtreecommitdiff
path: root/src/egl/drivers/dri2/platform_tizen.c
blob: 119736f36e4ec1ea0c6889bfb3abf098e8a9e180 (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
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
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2017 Samsung Electronics co., Ltd. All Rights Reserved
 *
 * Based on platform_android, which has
 *
 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
 * Copyright (C) 2010-2011 LunarG Inc.
 * Copyright © 2011 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Gwan-gyeong Mun <kk.moon@samsung.com>
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <xf86drm.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "egl_dri2.h"
#include "egl_dri2_fallbacks.h"
#include "loader.h"

#include <wayland-client.h>

#include <drm_fourcc.h>

int tbm_bufmgr_fd = -1;
static void tizen_free_local_buffers(struct dri2_egl_surface *dri2_surf);

/* createImageFromFds requires fourcc format */
static int get_fourcc(tbm_format format)
{
   switch (format) {
   case TBM_FORMAT_RGB565:   return __DRI_IMAGE_FOURCC_RGB565;
   case TBM_FORMAT_BGRA8888: return __DRI_IMAGE_FOURCC_ARGB8888;
   case TBM_FORMAT_RGBA8888: return __DRI_IMAGE_FOURCC_ABGR8888;
   case TBM_FORMAT_ARGB8888: return __DRI_IMAGE_FOURCC_ARGB8888;
   case TBM_FORMAT_ABGR8888: return __DRI_IMAGE_FOURCC_ABGR8888;
   case TBM_FORMAT_RGBX8888: return __DRI_IMAGE_FOURCC_XBGR8888;
   case TBM_FORMAT_XRGB8888: return __DRI_IMAGE_FOURCC_XRGB8888;
   default:
      _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
   }
   return -1;
}

static int get_fourcc_yuv(tbm_format format)
{
   switch (format) {
   case TBM_FORMAT_NV12:   return __DRI_IMAGE_FOURCC_NV12;
   case TBM_FORMAT_NV21:   return __DRI_IMAGE_FOURCC_NV12;
   case TBM_FORMAT_YUV420: return __DRI_IMAGE_FOURCC_YUV420;
   case TBM_FORMAT_YVU420: return __DRI_IMAGE_FOURCC_YVU420;
   default:
      _eglLog(_EGL_WARNING, "unsupported native yuv buffer format 0x%x", format);
   }
   return -1;
}

static bool is_yuv_format(tbm_format format)
{
   if (get_fourcc_yuv(format) == -1)
     return false;
   else
     return true;
}

static int get_format(tbm_format format)
{
   switch (format) {
   case TBM_FORMAT_RGB565:   return __DRI_IMAGE_FORMAT_RGB565;
   case TBM_FORMAT_BGRA8888: return __DRI_IMAGE_FORMAT_ARGB8888;
   case TBM_FORMAT_RGBA8888: return __DRI_IMAGE_FORMAT_ABGR8888;
   case TBM_FORMAT_ARGB8888: return __DRI_IMAGE_FORMAT_ARGB8888;
   case TBM_FORMAT_ABGR8888: return __DRI_IMAGE_FORMAT_ABGR8888;
   case TBM_FORMAT_RGBX8888: return __DRI_IMAGE_FORMAT_XBGR8888;
   case TBM_FORMAT_XRGB8888: return __DRI_IMAGE_FORMAT_XRGB8888;
   default:
      _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
   }
   return -1;
}

static int get_format_bpp(tbm_format format)
{
   int bpp;

   switch (format) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
   case TBM_FORMAT_BGRA8888:
   case TBM_FORMAT_RGBA8888:
   case TBM_FORMAT_RGBX8888:
   case TBM_FORMAT_ARGB8888:
#pragma GCC diagnostic pop
   case TBM_FORMAT_XRGB8888:
      bpp = 4;
      break;
   case TBM_FORMAT_RGB565:
      bpp = 2;
      break;
   default:
      bpp = 0;
      break;
   }

   return bpp;
}

static int get_pitch(tbm_surface_h tbm_surface)
{
   tbm_surface_info_s surf_info;

   if (tbm_surface_get_info(tbm_surface, &surf_info) != TBM_SURFACE_ERROR_NONE) {
     return 0;
   }

   return surf_info.planes[0].stride;
}

static int
get_native_buffer_fd(tbm_surface_h tbm_surface)
{
   tbm_bo_handle bo_handle;
   bo_handle = tbm_bo_get_handle(tbm_surface_internal_get_bo(tbm_surface, 0),
                                 TBM_DEVICE_3D);

   return (bo_handle.ptr != NULL) ? (int)bo_handle.u32 : -1;
}

static int
get_native_buffer_name(tbm_surface_h tbm_surface)
{
   uint32_t bo_name;

   bo_name = tbm_bo_export(tbm_surface_internal_get_bo(tbm_surface, 0));

   return (bo_name != 0 ) ? (int)bo_name : -1;
}

static EGLBoolean
tizen_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
{
   int width, height;

   dri2_surf->tbm_surface = tpl_surface_dequeue_buffer(dri2_surf->tpl_surface);

   if (!dri2_surf->tbm_surface)
     return EGL_FALSE;

   tbm_surface_internal_ref(dri2_surf->tbm_surface);

   tpl_surface_get_size(dri2_surf->tpl_surface, &width, &height);
   if (dri2_surf->base.Width != width ||
       dri2_surf->base.Height != height) {
      dri2_surf->base.Width = width;
      dri2_surf->base.Height = height;
      tizen_free_local_buffers(dri2_surf);
   }

   /* Record all the buffers created by tpl_surface (tbm_surface_queue)
    *   and update back buffer * for updating buffer's age in swap_buffers.
    */
   EGLBoolean updated = EGL_FALSE;
   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
      if (!dri2_surf->color_buffers[i].tbm_surface) {
         dri2_surf->color_buffers[i].tbm_surface = dri2_surf->tbm_surface;
         dri2_surf->color_buffers[i].age = 0;
      }
      if (dri2_surf->color_buffers[i].tbm_surface == dri2_surf->tbm_surface) {
         dri2_surf->back = &dri2_surf->color_buffers[i];
         updated = EGL_TRUE;
         break;
      }
   }

   if (!updated) {
      /* In case of all the buffers were recreated , reset the color_buffers */
      for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
         dri2_surf->color_buffers[i].tbm_surface = NULL;
         dri2_surf->color_buffers[i].age = 0;
      }
      dri2_surf->color_buffers[0].tbm_surface = dri2_surf->tbm_surface;
      dri2_surf->back = &dri2_surf->color_buffers[0];
   }

   return EGL_TRUE;
}

static EGLBoolean
tizen_window_enqueue_buffer_with_damage(_EGLDisplay *disp,
                                        struct dri2_egl_surface *dri2_surf,
                                        const EGLint *rects,
                                        EGLint n_rects)
{
   tpl_result_t ret;
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);

   /* To avoid blocking other EGL calls, release the display mutex before
    * we enter tizen_window_enqueue_buffer() and re-acquire the mutex upon
    * return.
    */
   mtx_unlock(&disp->Mutex);

   if (n_rects < 1 || rects == NULL) {
     /* if there is no damage, call the normal API tpl_surface_enqueue_buffer */
     ret = tpl_surface_enqueue_buffer(dri2_surf->tpl_surface,
                                      dri2_surf->tbm_surface);
   } else {
     /* if there are rectangles of damage region,
        call the API tpl_surface_enqueue_buffer_with_damage() */
     ret = tpl_surface_enqueue_buffer_with_damage(dri2_surf->tpl_surface,
                                                  dri2_surf->tbm_surface,
                                                  n_rects, rects);
   }

   if (ret != TPL_ERROR_NONE) {
      _eglLog(_EGL_DEBUG, "%s : %d :tpl_surface_enqueue fail", __func__, __LINE__);
   }

   tbm_surface_internal_unref(dri2_surf->tbm_surface);
   dri2_surf->tbm_surface = NULL;
   dri2_surf->back = NULL;

   mtx_lock(&disp->Mutex);

   if (dri2_surf->dri_image_back) {
      dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
      dri2_surf->dri_image_back = NULL;
   }

   return EGL_TRUE;
}

static EGLBoolean
tizen_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
{
   return tizen_window_enqueue_buffer_with_damage(disp, dri2_surf, NULL, 0);
}

static void
tizen_window_cancel_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
{
   tizen_window_enqueue_buffer(disp, dri2_surf);
}

static __DRIbuffer *
tizen_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
                         unsigned int att, unsigned int format)
{
   struct dri2_egl_display *dri2_dpy =
      dri2_egl_display(dri2_surf->base.Resource.Display);

   if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
      return NULL;

   if (!dri2_surf->local_buffers[att]) {
      dri2_surf->local_buffers[att] =
         dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
               dri2_surf->base.Width, dri2_surf->base.Height);
   }

   return dri2_surf->local_buffers[att];
}

static void
tizen_free_local_buffers(struct dri2_egl_surface *dri2_surf)
{
   struct dri2_egl_display *dri2_dpy =
      dri2_egl_display(dri2_surf->base.Resource.Display);
   int i;

   for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
      if (dri2_surf->local_buffers[i]) {
         dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
               dri2_surf->local_buffers[i]);
         dri2_surf->local_buffers[i] = NULL;
      }
   }
}

static _EGLSurface *
tizen_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
                     _EGLConfig *conf, void *native_window,
                     const EGLint *attrib_list)
{
   __DRIcreateNewDrawableFunc createNewDrawable;
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
   struct dri2_egl_surface *dri2_surf;
   const __DRIconfig *config;
   tpl_surface_type_t tpl_surf_type = TPL_SURFACE_ERROR;
   tpl_result_t ret = TPL_ERROR_INVALID_PARAMETER;

   dri2_surf = calloc(1, sizeof *dri2_surf);
   if (!dri2_surf) {
      _eglError(EGL_BAD_ALLOC, "tizen_create_surface");
      return NULL;
   }

   if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
      goto cleanup_surface;

   config = dri2_get_dri_config(dri2_conf, type,
                                dri2_surf->base.GLColorspace);
   if (!config)
      goto cleanup_surface;

   if (type == EGL_WINDOW_BIT) {
      unsigned int alpha, depth;

      if (!native_window) {
         _eglError(EGL_BAD_NATIVE_WINDOW, "tizen_create_surface needs vaild native window");
         goto cleanup_surface;
      }
      dri2_surf->native_win = native_window;

      dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_DEPTH_SIZE, &depth);
      dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &alpha);

      ret = tpl_display_get_native_window_info(dri2_dpy->tpl_display,
                                               (tpl_handle_t)native_window,
                                               &dri2_surf->base.Width,
                                               &dri2_surf->base.Height,
                                               &dri2_surf->tbm_format,
                                               depth, alpha);

      if (ret != TPL_ERROR_NONE || dri2_surf->tbm_format == 0) {
        _eglError(EGL_BAD_NATIVE_WINDOW, "tizen_create_surface fails on tpl_display_get_native_window_info()");
        goto cleanup_surface;
      }

      tpl_surf_type = TPL_SURFACE_TYPE_WINDOW;
   } else if (type == EGL_PIXMAP_BIT) {

      if (!native_window) {
         _eglError(EGL_BAD_NATIVE_PIXMAP, "tizen_create_surface needs valid native pixmap");
         goto cleanup_surface;
      }
      ret = tpl_display_get_native_pixmap_info(dri2_dpy->tpl_display,
                                               (tpl_handle_t)native_window,
                                               &dri2_surf->base.Width,
                                               &dri2_surf->base.Height,
                                               &dri2_surf->tbm_format);

      if (ret != TPL_ERROR_NONE || dri2_surf->tbm_format == 0) {
        _eglError(EGL_BAD_NATIVE_PIXMAP, "tizen_create_surface fails on tpl_display_get_native_pixmap_info");
        goto cleanup_surface;
      }

      tpl_surf_type = TPL_SURFACE_TYPE_PIXMAP;
   } else {
      _eglError(EGL_BAD_NATIVE_WINDOW, "tizen_create_surface does not support PBuffer");
      goto cleanup_surface;
   }

   dri2_surf->tpl_surface = tpl_surface_create(dri2_dpy->tpl_display,
                                               (tpl_handle_t)native_window,
                                               tpl_surf_type,
                                               dri2_surf->tbm_format);
   if (!dri2_surf->tpl_surface)
      goto cleanup_surface;

   if (dri2_dpy->dri2) {
      createNewDrawable = dri2_dpy->dri2->createNewDrawable;
   } else {
      createNewDrawable = dri2_dpy->swrast->createNewDrawable;
   }

   dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
                                                  dri2_surf);
    if (dri2_surf->dri_drawable == NULL) {
      _eglError(EGL_BAD_ALLOC, "createNewDrawable");
       goto cleanup_tpl_surface;
    }

   return &dri2_surf->base;

cleanup_tpl_surface:
   tpl_object_unreference((tpl_object_t *)dri2_surf->tpl_surface);
cleanup_surface:
   free(dri2_surf);

   return NULL;
}

static _EGLSurface *
tizen_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
                            _EGLConfig *conf, void *native_window,
                            const EGLint *attrib_list)
{
   return tizen_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
                               native_window, attrib_list);
}

static _EGLSurface *
tizen_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
                            _EGLConfig *conf, void *native_pixmap,
                            const EGLint *attrib_list)
{
   return tizen_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
                               native_pixmap, attrib_list);
}

static _EGLSurface *
tizen_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
                             _EGLConfig *conf, const EGLint *attrib_list)
{
   return tizen_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
                               NULL, attrib_list);
}

static EGLBoolean
tizen_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);

   tizen_free_local_buffers(dri2_surf);

   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
      if (dri2_surf->tbm_surface)
         tizen_window_cancel_buffer(disp, dri2_surf);
   }

   if (dri2_surf->dri_image_back) {
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
      dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
      dri2_surf->dri_image_back = NULL;
   }

   if (dri2_surf->dri_image_front) {
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
      dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
      dri2_surf->dri_image_front = NULL;
   }

   dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);

   tpl_object_unreference((tpl_object_t *)dri2_surf->tpl_surface);

   free(dri2_surf);

   return EGL_TRUE;
}

static int
update_buffers(struct dri2_egl_surface *dri2_surf)
{
   if (dri2_surf->base.Type != EGL_WINDOW_BIT)
      return 0;

   /* try to dequeue the next back buffer */
   if (!dri2_surf->tbm_surface && !tizen_window_dequeue_buffer(dri2_surf)) {
      _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
      return -1;
   }

   return 0;
}

static int
get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
{
   struct dri2_egl_display *dri2_dpy =
      dri2_egl_display(dri2_surf->base.Resource.Display);

   if (dri2_surf->dri_image_front)
      return 0;

   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
      /* According current EGL spec, front buffer rendering
       * for window surface is not supported now.
       * and mesa doesn't have the implementation of this case.
       * Add warning message, but not treat it as error.
       */
      _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
   } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
      dri2_surf->dri_image_front =
          dri2_dpy->image->createImage(dri2_dpy->dri_screen,
                                       dri2_surf->base.Width,
                                       dri2_surf->base.Height,
                                       format,
                                       0,
                                       dri2_surf);
      if (!dri2_surf->dri_image_front) {
         _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
         return -1;
      }
   }

   return 0;
}

static int
get_back_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
{
   struct dri2_egl_display *dri2_dpy =
      dri2_egl_display(dri2_surf->base.Resource.Display);
   int fourcc, pitch;
   int offset = 0, fd;
   tbm_surface_info_s surf_info;

   if (dri2_surf->dri_image_back)
      return 0;

   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
      if (!dri2_surf->tbm_surface) {
         _eglLog(_EGL_WARNING, "Could not get native buffer");
         return -1;
      }

      fd = get_native_buffer_fd(dri2_surf->tbm_surface);
      if (fd < 0) {
         _eglLog(_EGL_WARNING, "Could not get native buffer FD");
         return -1;
      }

      if (tbm_surface_get_info(dri2_surf->tbm_surface, &surf_info) != TBM_SURFACE_ERROR_NONE) {
         _eglLog(_EGL_WARNING, "Could not get pitch");
         return -1;
      }

      pitch = surf_info.planes[0].stride;
      fourcc = get_fourcc(dri2_surf->tbm_format);

      if (fourcc == -1 || pitch == 0) {
         _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
                 fourcc, pitch);
         return -1;
      }

      dri2_surf->base.Width = surf_info.width;
      dri2_surf->base.Height = surf_info.height;

      dri2_surf->dri_image_back =
         dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
                                             dri2_surf->base.Width,
                                             dri2_surf->base.Height,
                                             fourcc,
                                             &fd,
                                             1,
                                             &pitch,
                                             &offset,
                                             dri2_surf);

      if (!dri2_surf->dri_image_back) {
         _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
         return -1;
      }
   } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
      /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
       * the spec states that they have a back buffer but no front buffer, in
       * contrast to pixmaps, which have a front buffer but no back buffer.
       *
       * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
       * from the spec, following the precedent of Mesa's EGL X11 platform. The
       * X11 platform correctly assigns pbuffers to single-buffered configs, but
       * assigns the pbuffer a front buffer instead of a back buffer.
       *
       * Pbuffers in the X11 platform mostly work today, so let's just copy its
       * behavior instead of trying to fix (and hence potentially breaking) the
       * world.
       */
      _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
   }

   return 0;
}

/* Some drivers will pass multiple bits in buffer_mask.
 * For such case, will go through all the bits, and
 * will not return error when unsupported buffer is requested, only
 * return error when the allocation for supported buffer failed.
 */
static int
tizen_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
                        uint32_t *stamp, void *loaderPrivate,
                        uint32_t buffer_mask, struct __DRIimageList *images)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;

   images->image_mask = 0;
   images->front = NULL;
   images->back = NULL;

   if (update_buffers(dri2_surf) < 0)
      return 0;

   if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
      if (get_front_bo(dri2_surf, format) < 0)
         return 0;

      if (dri2_surf->dri_image_front) {
         images->front = dri2_surf->dri_image_front;
         images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
      }
   }

   if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
      if (get_back_bo(dri2_surf, format) < 0)
         return 0;

      if (dri2_surf->dri_image_back) {
         images->back = dri2_surf->dri_image_back;
         images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
      }
   }

   return 1;
}

static EGLint
tizen_query_buffer_age(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surface)
{
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);

   if (update_buffers(dri2_surf) < 0) {
      _eglError(EGL_BAD_ALLOC, "tizen_query_buffer_age");
      return 0;
   }

   return dri2_surf->back->age;
}

static EGLBoolean
tizen_swap_buffers_with_damage(_EGLDriver *drv, _EGLDisplay *disp,
                               _EGLSurface *draw, const EGLint *rects,
                               EGLint n_rects)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);

   if (dri2_surf->base.Type != EGL_WINDOW_BIT)
      return EGL_TRUE;

   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
      if (dri2_surf->color_buffers[i].age > 0)
         dri2_surf->color_buffers[i].age++;
   }

   /* Make sure we have a back buffer in case we're swapping without
    * ever rendering. */
   if (get_back_bo(dri2_surf, 0) < 0) {
      _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
      return EGL_FALSE;
   }

   dri2_surf->back->age = 1;

   if (dri2_surf->tbm_surface)
      tizen_window_enqueue_buffer_with_damage(disp, dri2_surf, rects, n_rects);

   if (dri2_dpy->swrast) {
      dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
   } else {
      dri2_flush_drawable_for_swapbuffers(disp, draw);
      dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
   }

   return EGL_TRUE;
}

static EGLBoolean
tizen_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
{
   return tizen_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
}

static _EGLImage *
tizen_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
                                     tbm_surface_h tbm_surface)

{
   tbm_surface_info_s surf_info;
   tbm_fd bo_fd[TBM_SURF_PLANE_MAX];
   tbm_bo bo[TBM_SURF_PLANE_MAX];
   int num_planes;
   int i;
   int fourcc;
   size_t offsets[3] = {0, 0, 0};
   size_t pitches[3] = {0, 0, 0};
   int fds[3] = {-1, -1, -1};

   if (tbm_surface_get_info(tbm_surface, &surf_info) != TBM_SURFACE_ERROR_NONE) {
     _eglLog(_EGL_WARNING, "Could not get tbm_surface_info");
     return NULL;
   }

   num_planes = surf_info.num_planes;
   for (i = 0; i < num_planes; i++) {
      tbm_bo_handle bo_handle;
      int bo_idx = tbm_surface_internal_get_plane_bo_idx(tbm_surface, i);
      bo[i] = tbm_surface_internal_get_bo (tbm_surface, bo_idx);
      if (bo[i] == NULL) {
        _eglLog(_EGL_WARNING, "Could not get tbm_surface_internal_bo");
        return NULL;
      }
      bo_handle = tbm_bo_get_handle(bo[i], TBM_DEVICE_3D);
      bo_fd[i] = bo_handle.u32;
   }

   fourcc = get_fourcc_yuv(tbm_surface_get_format(tbm_surface));
   if (fourcc == -1) {
     _eglLog(_EGL_WARNING, "Unsupported native yuv format");
     return NULL;
   }

   switch (fourcc) {
   case __DRI_IMAGE_FOURCC_NV12:
      fds[0] = bo_fd[0];
      fds[1] = bo_fd[1];
      offsets[0] = surf_info.planes[0].offset;
      offsets[1] = surf_info.planes[1].offset;
      pitches[0] = surf_info.planes[0].stride;
      pitches[1] = surf_info.planes[1].stride;
      break;
   case __DRI_IMAGE_FOURCC_YUV420:
      fds[0] = bo_fd[0];
      fds[1] = bo_fd[1];
      fds[2] = bo_fd[2];
      offsets[0] = surf_info.planes[0].offset;
      offsets[1] = surf_info.planes[1].offset;
      offsets[2] = surf_info.planes[2].offset;
      pitches[0] = surf_info.planes[0].stride;
      pitches[1] = surf_info.planes[1].stride;
      pitches[2] = surf_info.planes[2].stride;
      break;
   case __DRI_IMAGE_FOURCC_YVU420:
      fds[0] = bo_fd[0];
      fds[1] = bo_fd[2];
      fds[2] = bo_fd[1];
      offsets[0] = surf_info.planes[0].offset;
      offsets[1] = surf_info.planes[2].offset;
      offsets[2] = surf_info.planes[1].offset;
      pitches[0] = surf_info.planes[0].stride;
      pitches[1] = surf_info.planes[2].stride;
      pitches[2] = surf_info.planes[1].stride;
      break;
    default:
      _eglLog(_EGL_WARNING, "Unsupported native yuv format");
      return NULL;
   }

   if (num_planes == 2) {
      const EGLint attr_list_2plane[] = {
         EGL_WIDTH, surf_info.width,
         EGL_HEIGHT, surf_info.height,
         EGL_LINUX_DRM_FOURCC_EXT, fourcc,
         EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
         EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
         EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
         EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
         EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
         EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
         EGL_NONE, 0
      };
      return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
   } else if (num_planes == 3) {
      const EGLint attr_list_3plane[] = {
         EGL_WIDTH, surf_info.width,
         EGL_HEIGHT, surf_info.height,
         EGL_LINUX_DRM_FOURCC_EXT, fourcc,
         EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
         EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
         EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
         EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
         EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
         EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
         EGL_DMA_BUF_PLANE2_FD_EXT, fds[2],
         EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
         EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
         EGL_NONE, 0
      };
      return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
   } else {
      _eglLog(_EGL_WARNING, "Unsupported yuv planes");
      return NULL;
   }
}

static _EGLImage *
tizen_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
                                 tbm_surface_h tbm_surface , int fd)
{
   unsigned int pitch;
   tbm_surface_info_s surf_info;
   unsigned int flags = tbm_bo_get_flags(tbm_surface_internal_get_bo(tbm_surface, 0));

   if (is_yuv_format(tbm_surface_get_format(tbm_surface)))
      return tizen_create_image_from_prime_fd_yuv(disp, ctx, tbm_surface);

   const int fourcc = get_fourcc(tbm_surface_get_format(tbm_surface));
   if (fourcc == -1) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }

   if (tbm_surface_get_info(tbm_surface, &surf_info) != TBM_SURFACE_ERROR_NONE) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }
   pitch = surf_info.planes[0].stride;

   if (pitch == 0) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }

   /*const EGLint attr_list[] = {
      EGL_WIDTH, surf_info.width,
      EGL_HEIGHT, surf_info.height,
      EGL_LINUX_DRM_FOURCC_EXT, fourcc,
      EGL_DMA_BUF_PLANE0_FD_EXT, fd,
      EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
      EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
      EGL_NONE, 0
   };*/

   //TODO: get modifier from vendor driver/TBM
   EGLint attr_list[50];
   int atti = 0;

   attr_list[atti++] = EGL_WIDTH;
   attr_list[atti++] = surf_info.width;
   attr_list[atti++] = EGL_HEIGHT;
   attr_list[atti++] = surf_info.height;
   attr_list[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
   attr_list[atti++] = fourcc;
   attr_list[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
   attr_list[atti++] = fd;
   attr_list[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
   attr_list[atti++] = pitch;
   attr_list[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
   attr_list[atti++] = 0;

   if (flags == TBM_BO_TILED) {
		attr_list[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
		attr_list[atti++] = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED & 0xFFFFFFFF;
		attr_list[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
		attr_list[atti++] = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED >> 32;
   }
   attr_list[atti++] = EGL_NONE;
   attr_list[atti++] = 0;


   return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
}

static _EGLImage *
tizen_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
                             tbm_surface_h tbm_surface)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   struct dri2_egl_image *dri2_img;
   int name;
   int format;
   unsigned int pitch;
   tbm_surface_info_s surf_info;

   name = get_native_buffer_name(tbm_surface);
   if (!name) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }

   format = get_format(tbm_surface_get_format(tbm_surface));
   if (format == -1)
       return NULL;

   if (tbm_surface_get_info(tbm_surface, &surf_info) != TBM_SURFACE_ERROR_NONE) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }
   pitch = surf_info.planes[0].stride;

   if (pitch == 0) {
      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
      return NULL;
   }

   dri2_img = calloc(1, sizeof(*dri2_img));
   if (!dri2_img) {
      _eglError(EGL_BAD_ALLOC, "tizen_create_image_mesa_drm");
      return NULL;
   }

   _eglInitImage(&dri2_img->base, disp);

   dri2_img->dri_image =
      dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
                                           surf_info.width,
                                           surf_info.height,
                                           format,
                                           name,
                                           pitch,
                                           dri2_img);
   if (!dri2_img->dri_image) {
      free(dri2_img);
      _eglError(EGL_BAD_ALLOC, "tizen_create_image_mesa_drm");
      return NULL;
   }

   return &dri2_img->base;
}

static EGLBoolean
tizen_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
                    EGLint attribute, EGLint *value)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
   int width = 0, height = 0;

   if (tpl_display_get_native_window_info(dri2_dpy->tpl_display,
                                          dri2_surf->native_win,
                                          &width, &height, NULL, 0, 0) != TPL_ERROR_NONE)
     return EGL_FALSE;

   switch (attribute) {
      case EGL_WIDTH:
         if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->native_win) {
            *value = width;
            return EGL_TRUE;
         }
         break;
      case EGL_HEIGHT:
         if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->native_win) {
            *value = height;
            return EGL_TRUE;
         }
         break;
      default:
         break;
   }
   return _eglQuerySurface(drv, dpy, surf, attribute, value);
}

static _EGLImage *
dri2_create_image_tizen_native_buffer(_EGLDisplay *disp,
                                      _EGLContext *ctx,
                                      tbm_surface_h tbm_surface)
{
   int fd;

   fd = get_native_buffer_fd(tbm_surface);
   if (fd >= 0)
      return tizen_create_image_from_prime_fd(disp, ctx, tbm_surface, fd);

   return tizen_create_image_from_name(disp, ctx, tbm_surface);
}

static _EGLImage *
dri2_create_image_tizen_wl_buffer(_EGLDisplay *disp,
                                  _EGLContext *ctx,
                                  tpl_handle_t native_pixmap)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   int fd;
   tbm_surface_h tbm_surface = NULL;

   tbm_surface = tpl_display_get_buffer_from_native_pixmap(dri2_dpy->tpl_display,
                                                           (tpl_handle_t)native_pixmap);
   if (!tbm_surface)
     return NULL;

   fd = get_native_buffer_fd(tbm_surface);
   if (fd >= 0)
      return tizen_create_image_from_prime_fd(disp, ctx, tbm_surface, fd);

   return tizen_create_image_from_name(disp, ctx, tbm_surface);
}

static _EGLImage *
tizen_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
                       _EGLContext *ctx, EGLenum target,
                       EGLClientBuffer buffer, const EGLint *attr_list)
{
   switch (target) {
   case EGL_NATIVE_SURFACE_TIZEN:
      return dri2_create_image_tizen_native_buffer(disp, ctx,
                                                   (tbm_surface_h)buffer);
   case EGL_WAYLAND_BUFFER_WL:
      return dri2_create_image_tizen_wl_buffer(disp, ctx, (tpl_handle_t)buffer);
   default:
      return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
   }
}

static void
tizen_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
{
}

static int
tizen_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
                                    unsigned int *attachments, int count)
{
   int num_buffers = 0, i;

   /* fill dri2_surf->buffers */
   for (i = 0; i < count * 2; i += 2) {
      __DRIbuffer *buf, *local;

      assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
      buf = &dri2_surf->buffers[num_buffers];

      switch (attachments[i]) {
      case __DRI_BUFFER_BACK_LEFT:
         if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
            buf->attachment = attachments[i];
            buf->name = get_native_buffer_name(dri2_surf->tbm_surface);
            buf->cpp = get_format_bpp(tbm_surface_get_format(dri2_surf->tbm_surface));
            buf->pitch = get_pitch(dri2_surf->tbm_surface);
            buf->flags = 0;

            if (buf->name)
               num_buffers++;

            break;
         }
         /* fall through for pbuffers */
      case __DRI_BUFFER_DEPTH:
      case __DRI_BUFFER_STENCIL:
      case __DRI_BUFFER_ACCUM:
      case __DRI_BUFFER_DEPTH_STENCIL:
      case __DRI_BUFFER_HIZ:
         local = tizen_alloc_local_buffer(dri2_surf, attachments[i], attachments[i + 1]);

         if (local) {
            *buf = *local;
            num_buffers++;
         }
         break;
      case __DRI_BUFFER_FRONT_LEFT:
      case __DRI_BUFFER_FRONT_RIGHT:
      case __DRI_BUFFER_FAKE_FRONT_LEFT:
      case __DRI_BUFFER_FAKE_FRONT_RIGHT:
      case __DRI_BUFFER_BACK_RIGHT:
      default:
         /* no front or right buffers */
         break;
      }
   }

   return num_buffers;
}

static __DRIbuffer *
tizen_get_buffers_with_format(__DRIdrawable * driDrawable,
                              int *width, int *height,
                              unsigned int *attachments, int count,
                              int *out_count, void *loaderPrivate)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;

   if (update_buffers(dri2_surf) < 0)
      return NULL;

   dri2_surf->buffer_count =
      tizen_get_buffers_parse_attachments(dri2_surf, attachments, count);

   if (width)
      *width = dri2_surf->base.Width;
   if (height)
      *height = dri2_surf->base.Height;

   *out_count = dri2_surf->buffer_count;

   return dri2_surf->buffers;
}

static int
tizen_swrast_get_stride_for_format(tbm_format format, int w)
{
   switch (format) {
   case TBM_FORMAT_RGB565:
      return 2 * w;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
   case TBM_FORMAT_BGRA8888:
   case TBM_FORMAT_RGBA8888:
#pragma GCC diagnostic pop
   case TBM_FORMAT_RGBX8888:
   default:
      return 4 * w;
   }
}

static void
tizen_swrast_get_drawable_info(__DRIdrawable * draw,
                               int *x, int *y, int *w, int *h,
                               void *loaderPrivate)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;

   if (!dri2_surf->tbm_surface) {
      if (update_buffers(dri2_surf) < 0)
         return;
   }

   *x = 0;
   *y = 0;
   *w = dri2_surf->base.Width;
   *h = dri2_surf->base.Height;
}

static void
tizen_swrast_get_image(__DRIdrawable * read,
                       int x, int y, int w, int h,
                       char *data, void *loaderPrivate)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;
   tbm_surface_info_s surf_info;
   int ret = TBM_SURFACE_ERROR_NONE;
   int internal_stride, stride, i;

   if (!dri2_surf->tbm_surface) {
      if (update_buffers(dri2_surf) < 0)
         return;
   }

   ret = tbm_surface_map(dri2_surf->tbm_surface, TBM_SURF_OPTION_READ, &surf_info);

   if (ret != TBM_SURFACE_ERROR_NONE) {
      _eglLog(_EGL_WARNING, "Could not tbm_surface_map");
      return;
   }

   internal_stride = surf_info.planes[0].stride;
   stride = w * 4;

   for (i = 0; i < h; i++) {
      memcpy(data + i * stride,
             surf_info.planes[0].ptr + (x + i) * internal_stride + y, stride);
   }

   tbm_surface_unmap(dri2_surf->tbm_surface);
}

static void
tizen_swrast_put_image2(__DRIdrawable * draw, int op,
                        int x, int y, int w, int h, int stride,
                        char *data, void *loaderPrivate)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;
   tbm_surface_info_s surf_info;
   int ret = TBM_SURFACE_ERROR_NONE;
   int internal_stride, i;

   if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
       op != __DRI_SWRAST_IMAGE_OP_SWAP)
      return;

   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
      if (!dri2_surf->tbm_surface) {
        if (update_buffers(dri2_surf) < 0) {
           _eglLog(_EGL_WARNING, "Could not get native buffer");
           return;
        }
      }

      ret = tbm_surface_map(dri2_surf->tbm_surface, TBM_SURF_OPTION_WRITE, &surf_info);
      if (ret != TBM_SURFACE_ERROR_NONE) {
         _eglLog(_EGL_WARNING, "Could not tbm_surface_map");
         return;
      }

      internal_stride = surf_info.planes[0].stride;

      for (i = 0; i < h; i++) {
         memcpy(surf_info.planes[0].ptr + (x + i) * internal_stride + y,
                data + i * stride, stride);
      }

      tbm_surface_unmap(dri2_surf->tbm_surface);
   }
}

static void
tizen_swrast_put_image(__DRIdrawable * draw, int op,
                         int x, int y, int w, int h,
                         char *data, void *loaderPrivate)
{
   struct dri2_egl_surface *dri2_surf = loaderPrivate;
   int stride;

   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
      if (!dri2_surf->tbm_surface) {
        if (update_buffers(dri2_surf) < 0) {
           _eglLog(_EGL_WARNING, "Could not get native buffer");
           return;
        }
      }

      stride = tizen_swrast_get_stride_for_format(dri2_surf->tbm_format, w);
      tizen_swrast_put_image2(draw, op, x, y, w, h, stride, data, loaderPrivate);
   }
}

static EGLBoolean
tizen_add_configs(_EGLDriver *drv, _EGLDisplay *dpy)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
   int count, i;

   for (i = 0, count = 0; dri2_dpy->driver_configs[i]; i++) {
      struct dri2_egl_config *dri2_cfg;
      unsigned int red, blue, green, alpha, depth;
      int surface_type = 0;
      tpl_bool_t is_slow;
      EGLint attr_list[] = {
         EGL_NATIVE_VISUAL_ID, 0,
         EGL_NONE,
      };
      tpl_result_t res;

      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
                                      __DRI_ATTRIB_RED_SIZE, &red);
      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
                                      __DRI_ATTRIB_GREEN_SIZE, &green);
      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
                                      __DRI_ATTRIB_BLUE_SIZE, &blue);
      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
                                      __DRI_ATTRIB_ALPHA_SIZE, &alpha);
      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
                                      __DRI_ATTRIB_DEPTH_SIZE, &depth);

      res = tpl_display_query_config(dri2_dpy->tpl_display, TPL_SURFACE_TYPE_WINDOW,
                                     red, green, blue, alpha, depth,
                                     &attr_list[1], &is_slow);
      if (res == TPL_ERROR_NONE)
        surface_type |= EGL_WINDOW_BIT;

      res = tpl_display_query_config(dri2_dpy->tpl_display, TPL_SURFACE_TYPE_PIXMAP,
                                     red, green, blue, alpha, depth,
                                     &attr_list[1], &is_slow);
      if (res == TPL_ERROR_NONE)
        surface_type |= EGL_PIXMAP_BIT;

      if (!surface_type)
        continue;

      dri2_cfg = dri2_add_config(dpy, dri2_dpy->driver_configs[i], count + 1,
                                 surface_type, &attr_list[0], NULL);
      if (dri2_cfg)
        count++;
   }

   return (count != 0);
}

static EGLBoolean tizen_set_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy,
                                          _EGLSurface *surf, EGLint interval)
{
   tpl_result_t res = TPL_ERROR_NONE;
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);

   res = tpl_surface_set_post_interval(dri2_surf->tpl_surface, interval);

   if (res != TPL_ERROR_NONE)
     return EGL_FALSE;

   return EGL_TRUE;
}

static struct dri2_egl_display_vtbl tizen_display_vtbl = {
   .authenticate = NULL,
   .create_window_surface = tizen_create_window_surface,
   .create_pixmap_surface = tizen_create_pixmap_surface,
   .create_pbuffer_surface = tizen_create_pbuffer_surface,
   .destroy_surface = tizen_destroy_surface,
   .create_image = tizen_create_image_khr,
   .swap_interval = tizen_set_swap_interval,
   .swap_buffers = tizen_swap_buffers,
   .swap_buffers_with_damage = tizen_swap_buffers_with_damage,
   .swap_buffers_region = dri2_fallback_swap_buffers_region,
   .post_sub_buffer = dri2_fallback_post_sub_buffer,
   .copy_buffers = dri2_fallback_copy_buffers,
   .query_buffer_age = tizen_query_buffer_age,
   .query_surface = tizen_query_surface,
   .create_wayland_buffer_from_image = NULL,
   .get_sync_values = dri2_fallback_get_sync_values,
   .get_dri_drawable = dri2_surface_get_dri_drawable,
};

static const __DRIdri2LoaderExtension tizen_dri2_loader_extension = {
   .base = { __DRI_DRI2_LOADER, 3 },

   .getBuffers           = NULL,
   .getBuffersWithFormat = tizen_get_buffers_with_format,
   .flushFrontBuffer     = tizen_flush_front_buffer,
};

static const __DRIimageLoaderExtension tizen_image_loader_extension = {
   .base = { __DRI_IMAGE_LOADER, 1 },

   .getBuffers          = tizen_image_get_buffers,
   .flushFrontBuffer    = tizen_flush_front_buffer,
};

static const __DRIswrastLoaderExtension tizen_swrast_loader_extension = {
   .base = { __DRI_SWRAST_LOADER, 2 },

   .getDrawableInfo = tizen_swrast_get_drawable_info,
   .putImage        = tizen_swrast_put_image,
   .getImage        = tizen_swrast_get_image,
   .putImage2       = tizen_swrast_put_image2,
};

static const __DRIextension *tizen_dri2_loader_extensions[] = {
   &tizen_dri2_loader_extension.base,
   &image_lookup_extension.base,
   &use_invalidate.base,
   NULL,
};

static const __DRIextension *tizen_image_loader_extensions[] = {
   &tizen_image_loader_extension.base,
   &image_lookup_extension.base,
   &use_invalidate.base,
   NULL,
};

static const __DRIextension *tizen_swrast_loader_extensions[] = {
   &tizen_swrast_loader_extension.base,
   NULL,
};

static EGLBoolean
tizen_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
                              struct wl_display *wl_dpy)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);

   (void) drv;
   (void) wl_dpy;

   if (!dri2_dpy->tpl_display)
     return EGL_FALSE;

   if (!tpl_display_get_native_handle(dri2_dpy->tpl_display))
     return EGL_FALSE;

   return EGL_TRUE;
}

static EGLBoolean
tizen_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
                                struct wl_display *wl_dpy)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);

   (void) drv;
   (void) wl_dpy;

   if (!dri2_dpy->tpl_display)
     return EGL_FALSE;

   if (!tpl_display_get_native_handle(dri2_dpy->tpl_display))
     return EGL_FALSE;

   return EGL_TRUE;
}

static EGLBoolean
tizen_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
                              struct wl_resource *buffer_resource,
                              EGLint attribute, EGLint *value)
{
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
   tbm_format tbm_format = 0;
   int width = 0, height = 0;
   tpl_result_t res;

   if (!dri2_dpy->tpl_display)
     return EGL_FALSE;

   if (!tpl_display_get_native_handle(dri2_dpy->tpl_display))
     return EGL_FALSE;

   res = tpl_display_get_native_pixmap_info(dri2_dpy->tpl_display,
                                            (tpl_handle_t)buffer_resource,
                                            &width, &height, &tbm_format);
   if (res != TPL_ERROR_NONE)
     return EGL_FALSE;

   switch (attribute) {
   case EGL_TEXTURE_FORMAT:
      switch (tbm_format) {
      case TBM_FORMAT_ARGB8888:
         *value = EGL_TEXTURE_RGBA;
         return EGL_TRUE;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
      case TBM_FORMAT_XRGB8888:
      case TBM_FORMAT_RGB565:
#pragma GCC diagnostic pop
         *value = EGL_TEXTURE_RGB;
         return EGL_TRUE;
      }
   case EGL_WIDTH:
      *value = width;
      return EGL_TRUE;
   case EGL_HEIGHT:
      *value = height;
      return EGL_TRUE;
   }

   return EGL_FALSE;
}

EGLBoolean
dri2_initialize_tizen(_EGLDriver *drv, _EGLDisplay *disp)
{
   struct dri2_egl_display *dri2_dpy;
   tpl_display_t *tpl_display = NULL;
   const char *err;
   char *tbm_bufmgr_device_name = NULL;
   int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);

   loader_set_logger(_eglLog);

   dri2_dpy = calloc(1, sizeof *dri2_dpy);
   if (!dri2_dpy)
      return _eglError(EGL_BAD_ALLOC, "eglInitialize");

   disp->DriverData = (void *) dri2_dpy;
   if (disp->PlatformDisplay == NULL) {
      disp->PlatformDisplay = wl_display_connect(NULL);
      if (disp->PlatformDisplay == NULL) {
         err = "DRI2: failed to get native display";
         goto cleanup_display;
      }
      dri2_dpy->own_device = 1;
   }

   /* TPL_BACKEND_UNKNOWN type make checking runtime type check */
   tpl_display = tpl_display_create(TPL_BACKEND_UNKNOWN, disp->PlatformDisplay);
   if (!tpl_display) {
      err = "DRI2: failed to create tpl_display";
      goto cleanup_display;
   }
   dri2_dpy->tpl_display = tpl_display;

   /* Get tbm_bufmgr's fd */
   tbm_bufmgr_fd = tbm_drm_helper_get_fd();

   if (tbm_bufmgr_fd == -1) {
      err = "DRI2: failed to get tbm_bufmgr fd";
      goto cleanup_device;
   }

   if (hw_accel) {
      int fd = -1;

      if (drmGetNodeTypeFromFd(tbm_bufmgr_fd) == DRM_NODE_RENDER) {

         tbm_bufmgr_device_name = loader_get_device_name_for_fd(tbm_bufmgr_fd);
         if (tbm_bufmgr_device_name == NULL) {
            err = "DRI2: failed to get tbm_bufmgr device name";
            goto cleanup_device;
         }
         fd = loader_open_device(tbm_bufmgr_device_name);

      } else {
         if (!tbm_drm_helper_get_auth_info(&fd, NULL, NULL)) {

            /* FIXME: tbm_drm_helper_get_auth_info() does not support the case of
             *        display server for now. this code is fallback routine for
             *        that Enlightenment Server fails on tbm_drm_helper_get_auth_info.
             *        When tbm_drm_helper_get_auth_info() supports display server
             *        case, then remove below routine.
             */
#if 1
            tbm_bufmgr_device_name = loader_get_device_name_for_fd(tbm_bufmgr_fd);
            if (tbm_bufmgr_device_name == NULL) {
               err = "DRI2: failed to get tbm_bufmgr device name";
               goto cleanup_device;
            }
            fd = loader_open_device(tbm_bufmgr_device_name);
#else
            err = "DRI2: failed to get fd from tbm_drm_helper_get_auth_info()";
            goto cleanup_device;
#endif
         }
      }

      if (fd == -1) {
         err = "DRI2: failed to get fd";
         goto cleanup_device;
      }
      dri2_dpy->fd = fd;

      dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
      if (dri2_dpy->driver_name == NULL) {
         err = "DRI2: failed to get driver name";
         goto cleanup_device;
      }
      if (!dri2_load_driver(disp)) {
         err = "DRI2: failed to load driver";
         goto cleanup_driver_name;
      }

      dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
      /* render nodes cannot use Gem names, and thus do not support
       * the __DRI_DRI2_LOADER extension */

      if (!dri2_dpy->is_render_node)
        dri2_dpy->loader_extensions = tizen_dri2_loader_extensions;
      else
        dri2_dpy->loader_extensions = tizen_image_loader_extensions;

   } else {
      dri2_dpy->fd = tbm_bufmgr_fd;
      dri2_dpy->driver_name = strdup("swrast");
      if (!dri2_load_driver_swrast(disp)) {
         err = "DRI2: failed to load swrast driver";
         goto cleanup_device;
      }
      dri2_dpy->loader_extensions = tizen_swrast_loader_extensions;
   }

   if (!dri2_create_screen(disp)) {
      err = "DRI2: failed to create screen";
      goto cleanup_driver;
   }

   dri2_setup_screen(disp);

   if (!dri2_setup_extensions(disp)) {
      err = "DRI2: failed to setup extensions";
      goto cleanup_screen;
   }

   if (!tizen_add_configs(drv, disp)) {
      err = "DRI2: failed to add configs";
      goto cleanup_screen;
   }

   /* Fill vtbl last to prevent accidentally calling virtual function during
    * initialization.
    */
   dri2_dpy->vtbl = &tizen_display_vtbl;

   disp->Extensions.EXT_buffer_age = EGL_FALSE;
   disp->Extensions.EXT_swap_buffers_with_damage = EGL_FALSE;
   disp->Extensions.TIZEN_image_native_surface = EGL_TRUE;
   disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
   disp->Extensions.WL_create_wayland_buffer_from_image = EGL_FALSE;

   drv->API.BindWaylandDisplayWL = tizen_bind_wayland_display_wl;
   drv->API.UnbindWaylandDisplayWL = tizen_unbind_wayland_display_wl;
   drv->API.QueryWaylandBufferWL = tizen_query_wayland_buffer_wl;

   return EGL_TRUE;

cleanup_screen:
   dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
cleanup_driver:
   dlclose(dri2_dpy->driver);
cleanup_driver_name:
   if (hw_accel)
     free(dri2_dpy->driver_name);
cleanup_device:
   if (tbm_bufmgr_device_name)
     free(tbm_bufmgr_device_name);
   tpl_object_unreference((tpl_object_t *)tpl_display);
cleanup_display:
   if (dri2_dpy->own_device) {
      wl_display_disconnect(disp->PlatformDisplay);
      disp->PlatformDisplay = NULL;
   }
   free(dri2_dpy);
   disp->DriverData = NULL;

   return _eglError(EGL_NOT_INITIALIZED, err);
}

void
dri2_teardown_tizen(_EGLDisplay *disp)
{
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);

    if (dri2_dpy->tpl_display)
        tpl_object_unreference((tpl_object_t *)(dri2_dpy->tpl_display));

    if (tbm_bufmgr_fd > 0)
        close(tbm_bufmgr_fd);

    if (dri2_dpy->own_device)
        wl_display_disconnect(disp->PlatformDisplay);
}