summaryrefslogtreecommitdiff
path: root/src/egl/drivers/dri/egldri.c
blob: cab0be2bd1820ce48c7864b39c9b28d07540e58e (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
/**
 * Generic EGL driver for DRI.
 *
 * This file contains all the code needed to interface DRI-based drivers
 * with libEGL.
 *
 * There's a lot of dependencies on fbdev and the /sys/ filesystem.
 */


#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <linux/fb.h>
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>

#include "egldriver.h"
#include "egldisplay.h"
#include "eglcontext.h"
#include "eglconfig.h"
#include "eglsurface.h"
#include "eglscreen.h"
#include "eglglobals.h"
#include "egllog.h"
#include "eglmode.h"

#include "egldri.h"

const char *sysfs = "/sys/class";
#define None 0
static const int empty_attribute_list[1] = { None };


/**
 * The bootstrap function.
 * Return a new driDriver object and plug in API functions.
 * This function, in turn, loads a specific DRI driver (ex: r200_dri.so).
 */
_EGLDriver *
_eglMain(_EGLDisplay *dpy)
{
   int length;
   char path[NAME_MAX];
   struct dirent *dirent;
#if 1
   FILE *file;
#endif
   DIR *dir;
   _EGLDriver *driver = NULL;;

   snprintf(path, sizeof(path), "%s/drm", sysfs);
   if (!(dir = opendir(path))) {
      _eglLog(_EGL_WARNING, "%s DRM devices not found.", path);
      return EGL_FALSE;
   }
   while ((dirent = readdir(dir))) {

      if (strncmp(&dirent->d_name[0], "card", 4) != 0)
         continue;
      if (strcmp(&dirent->d_name[4], &dpy->Name[1]) != 0)
         continue;

      snprintf(path, sizeof(path), "%s/drm/card%s/dri_library_name", sysfs, &dpy->Name[1]);
      _eglLog(_EGL_INFO, "Opening %s", path);
#if 1
      file = fopen(path, "r");
      if (!file) {
         _eglLog(_EGL_WARNING, "Failed to open %s", path);
         return NULL;
      }
      fgets(path, sizeof(path), file);
      fclose(file);
#else
      strcpy(path, "r200\n");
#endif
      if ((length = strlen(path)) > 0)
         path[length - 1] = '\0';  /* remove the trailing newline from sysfs */
      strncat(path, "_dri", sizeof(path));

      driver = _eglOpenDriver(dpy, path);

      break;
   }
   closedir(dir);

   return driver;
}


/**
 * Called by eglCreateContext via drv->API.CreateContext().
 */
static EGLContext
_eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
                     EGLContext share_list, const EGLint *attrib_list)
{
   driDisplay *disp = Lookup_driDisplay(dpy);
   driContext *c, *share;
   void *sharePriv;
   _EGLConfig *conf;
   __GLcontextModes visMode;

   c = (driContext *) calloc(1, sizeof(*c));
   if (!c)
      return EGL_NO_CONTEXT;

   if (!_eglInitContext(drv, dpy, &c->Base, config, attrib_list)) {
      free(c);
      return EGL_NO_CONTEXT;
   }

   if (share_list != EGL_NO_CONTEXT) {
      _EGLContext *shareCtx = _eglLookupContext(share_list);
      if (!shareCtx) {
         _eglError(EGL_BAD_CONTEXT, "eglCreateContext(share_list)");
         return EGL_FALSE;
      }
   }
   share = Lookup_driContext(share_list);
   if (share)
      sharePriv = share->driContext.private;
   else
      sharePriv = NULL;

   conf = _eglLookupConfig(drv, dpy, config);
   assert(conf);
   _eglConfigToContextModesRec(conf, &visMode);

   c->driContext.private = disp->driScreen.createNewContext(disp, &visMode,
           GLX_WINDOW_BIT, sharePriv, &c->driContext);
   if (!c->driContext.private) {
      free(c);
      return EGL_FALSE;
   }

   /* generate handle and insert into hash table */
   _eglSaveContext(&c->Base);

   return c->Base.Handle;
}


static EGLBoolean
_eglDRIMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw,
                   EGLSurface read, EGLContext context)
{
   driDisplay *disp = Lookup_driDisplay(dpy);
   driContext *ctx = Lookup_driContext(context);
   EGLBoolean b;

   b = _eglMakeCurrent(drv, dpy, draw, read, context);
   if (!b)
      return EGL_FALSE;

   if (ctx) {
      ctx->driContext.bindContext(disp, 0, read, draw, &ctx->driContext);
   }
   else {
      /* what's this??? */
      /*      _mesa_make_current( NULL, NULL, NULL );*/
   }
   return EGL_TRUE;
}


static EGLSurface
_eglDRICreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
                            const EGLint *attrib_list)
{
   driSurface *surf;

   surf = (driSurface *) calloc(1, sizeof(*surf));
   if (!surf) {
      return EGL_NO_SURFACE;
   }

   if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_PBUFFER_BIT,
                        config, attrib_list)) {
      free(surf);
      return EGL_NO_SURFACE;
   }

   /* create software-based pbuffer */
   {
#if 0
      GLcontext *ctx = NULL; /* this _should_ be OK */
#endif
      GLvisual visMode;
      _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
      assert(conf); /* bad config should be caught earlier */
      _eglConfigToContextModesRec(conf, &visMode);

#if 0
      surf->mesa_framebuffer = _mesa_create_framebuffer(&visMode);
      _mesa_add_soft_renderbuffers(surf->mesa_framebuffer,
                                   GL_TRUE, /* color bufs */
                                   visMode.haveDepthBuffer,
                                   visMode.haveStencilBuffer,
                                   visMode.haveAccumBuffer,
                                   GL_FALSE, /* alpha */
                                   GL_FALSE /* aux */ );

      /* set pbuffer/framebuffer size */
      _mesa_resize_framebuffer(ctx, surf->mesa_framebuffer,
                               surf->Base.Width, surf->Base.Height);
#endif
   }

   _eglSaveSurface(&surf->Base);

   return surf->Base.Handle;
}


static EGLBoolean
_eglDRIDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
{
   driDisplay *disp = Lookup_driDisplay(dpy);
   driSurface *fs = Lookup_driSurface(surface);

   _eglRemoveSurface(&fs->Base);

   fs->drawable.destroyDrawable(disp, fs->drawable.private);

   if (fs->Base.IsBound) {
      fs->Base.DeletePending = EGL_TRUE;
   }
   else {
      free(fs);
   }
   return EGL_TRUE;
}


static EGLBoolean
_eglDRIDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
{
   driDisplay *disp = Lookup_driDisplay(dpy);
   driContext *fc = Lookup_driContext(context);

   _eglRemoveContext(&fc->Base);

   fc->driContext.destroyContext(disp, 0, fc->driContext.private);

   if (fc->Base.IsBound) {
      fc->Base.DeletePending = EGL_TRUE;
   }
   else {
      free(fc);
   }
   return EGL_TRUE;
}


/**
 * Create a drawing surface which can be directly displayed on a screen.
 */
static EGLSurface
_eglDRICreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
                               const EGLint *attrib_list)
{
   _EGLConfig *config = _eglLookupConfig(drv, dpy, cfg);
   driDisplay *disp = Lookup_driDisplay(dpy);
   driSurface *surface;
   GLvisual visMode;

   surface = (driSurface *) calloc(1, sizeof(*surface));
   if (!surface) {
      return EGL_NO_SURFACE;
   }

   /* init base class, do error checking, etc. */
   if (!_eglInitSurface(drv, dpy, &surface->Base, EGL_SCREEN_BIT_MESA,
                        cfg, attrib_list)) {
      free(surface);
      return EGL_NO_SURFACE;
   }

   _eglSaveSurface(&surface->Base);


   /*
    * XXX this is where we should allocate video memory for the surface!
    */


   /* convert EGLConfig to GLvisual */
   _eglConfigToContextModesRec(config, &visMode);

   /* Create a new DRI drawable */
   if (!disp->driScreen.createNewDrawable(disp, &visMode, surface->Base.Handle,
                                          &surface->drawable, GLX_WINDOW_BIT,
                                          empty_attribute_list)) {
      _eglRemoveSurface(&surface->Base);
      free(surface);
      return EGL_NO_SURFACE;
   }

   return surface->Base.Handle;
}


/**
 * Set the fbdev colormap to a simple linear ramp.
 */
static void
_eglDRILoadColormap(driScreen *scrn)
{
   char path[ NAME_MAX ];
   char *buffer;
   int i, fd;

   /* cmap attribute uses 256 lines of 16 bytes.
    * Allocate one extra char for the \0 added by sprintf()
    */
   if ( !( buffer = malloc( 256 * 16 + 1 ) ) ) {
      _eglLog(_EGL_WARNING, "Out of memory in _eglDRILoadColormap");
      return;
   }

   /* cmap attribute uses 256 lines of 16 bytes */
   for ( i = 0; i < 256; i++ ) {
      int c = (i << 8) | i; /* expand to 16-bit value */
      sprintf(&buffer[i * 16], "%02x%c%04x%04x%04x\n", i, ' ', c, c, c);
   }

   snprintf(path, sizeof(path), "%s/graphics/%s/color_map", sysfs, scrn->fb);
   if ( !( fd = open( path, O_RDWR ) ) ) {
      _eglLog(_EGL_WARNING, "Unable to open %s to set colormap", path);
      return;
   }
   write( fd, buffer, 256 * 16 );
   close( fd );

   free( buffer );
}


/**
 * Show the given surface on the named screen.
 * If surface is EGL_NO_SURFACE, disable the screen's output.
 * Called via eglShowSurfaceMESA().
 */
EGLBoolean
_eglDRIShowScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
                             EGLScreenMESA screen,
                             EGLSurface surface, EGLModeMESA m)
{
   driDisplay *display = Lookup_driDisplay(dpy);
   driScreen *scrn = Lookup_driScreen(dpy, screen);
   driSurface *surf = Lookup_driSurface(surface);
   _EGLMode *mode = _eglLookupMode(dpy, m);
   FILE *file;
   char fname[NAME_MAX], buffer[1000];
   int temp;

   _eglLog(_EGL_DEBUG, "Enter _eglDRIShowScreenSurface");

   /* This will check that surface, screen, and mode are valid.
    * Also, it checks that the surface is large enough for the mode, etc.
    */
   if (!_eglShowScreenSurfaceMESA(drv, dpy, screen, surface, m))
      return EGL_FALSE;

   assert(surface == EGL_NO_SURFACE || surf);
   assert(m == EGL_NO_MODE_MESA || mode);
   assert(scrn);

   /*
    * Blank/unblank screen depending on if m == EGL_NO_MODE_MESA
    */
   snprintf(fname, sizeof(fname), "%s/graphics/%s/blank", sysfs, scrn->fb);
   file = fopen(fname, "r+");
   if (!file) {
      _eglLog(_EGL_WARNING, "kernel patch?? chown all fb sysfs attrib to allow"
              " write - %s\n", fname);
      return EGL_FALSE;
   }
   snprintf(buffer, sizeof(buffer), "%d",
            (m == EGL_NO_MODE_MESA ? VESA_POWERDOWN : VESA_VSYNC_SUSPEND));
   fputs(buffer, file);
   fclose(file);

   if (m == EGL_NO_MODE_MESA) {
      /* all done! */
      return EGL_TRUE;
   }

   _eglLog(_EGL_INFO, "Setting display mode to %d x %d, %d bpp",
           mode->Width, mode->Height, display->bpp);

   /*
    * Set the display mode
    */
   snprintf(fname, sizeof(fname), "%s/graphics/%s/mode", sysfs, scrn->fb);
   file = fopen(fname, "r+");
   if (!file) {
      _eglLog(_EGL_WARNING, "Failed to open %s to set mode", fname);
      return EGL_FALSE;
   }
   /* note: nothing happens without the \n! */
   snprintf(buffer, sizeof(buffer), "%s\n", mode->Name);
   fputs(buffer, file);
   fclose(file);
   _eglLog(_EGL_INFO, "Set mode to %s in %s", mode->Name, fname);

   /*
    * Set display bpp
    */
   snprintf(fname, sizeof(fname), "%s/graphics/%s/bits_per_pixel",
            sysfs, scrn->fb);
   file = fopen(fname, "r+");
   if (!file) {
      _eglLog(_EGL_WARNING, "Failed to open %s to set bpp", fname);
      return EGL_FALSE;
   }
   display->bpp = GET_CONFIG_ATTRIB(surf->Base.Config, EGL_BUFFER_SIZE);
   display->cpp = display->bpp / 8;
   snprintf(buffer, sizeof(buffer), "%d", display->bpp);
   fputs(buffer, file);
   fclose(file);

   /*
    * Unblank display
    */
   snprintf(fname, sizeof(fname), "%s/graphics/%s/blank", sysfs, scrn->fb);
   file = fopen(fname, "r+");
   if (!file) {
      _eglLog(_EGL_WARNING, "Failed to open %s", fname);
      return EGL_FALSE;
   }
   snprintf(buffer, sizeof(buffer), "%d", VESA_NO_BLANKING);
   fputs(buffer, file);
   fclose(file);

   /*
    * Set fbdev buffer virtual size to surface's size.
    */
   snprintf(fname, sizeof(fname), "%s/graphics/%s/virtual_size", sysfs, scrn->fb);
   file = fopen(fname, "r+");
   snprintf(buffer, sizeof(buffer), "%d,%d", surf->Base.Width, surf->Base.Height);
   fputs(buffer, file);
   rewind(file);
   fgets(buffer, sizeof(buffer), file);
   sscanf(buffer, "%d,%d", &display->virtualWidth, &display->virtualHeight);
   fclose(file);

   /*
    * round up pitch as needed
    */
   temp = display->virtualWidth;
   switch (display->bpp / 8) {
   case 1: temp = (display->virtualWidth + 127) & ~127; break;
   case 2: temp = (display->virtualWidth +  31) &  ~31; break;
   case 3:
   case 4: temp = (display->virtualWidth +  15) &  ~15; break;
   default:
      _eglLog(_EGL_WARNING, "Bad display->bpp = %d in _eglDRIShowScreenSurface");
   }
   display->virtualWidth = temp;

   /*
    * sanity check
    */
   if (surf->Base.Width < display->virtualWidth ||
       surf->Base.Height < display->virtualHeight) {
      /* this case _should_ have been caught at the top of this function */
      _eglLog(_EGL_WARNING, "too small of surface in _eglDRIShowScreenSurfaceMESA "
              "%d x %d < %d x %d", 
              surf->Base.Width,
              surf->Base.Height,
              display->virtualWidth,
              display->virtualHeight);
      /*
      return EGL_FALSE;
      */
   }

   /* This used to be done in the _eglDRICreateScreens routine. */
   _eglDRILoadColormap(scrn);

   return EGL_TRUE;
}


/**
 * Called by eglSwapBuffers via the drv->API.SwapBuffers() pointer.
 *
 * If the backbuffer is on a videocard, this is extraordinarily slow!
 */
static EGLBoolean
_eglDRISwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
{
   driSurface *drawable = Lookup_driSurface(draw);

   /* this does error checking */
   if (!_eglSwapBuffers(drv, dpy, draw))
      return EGL_FALSE;

   drawable->drawable.swapBuffers(NULL, drawable->drawable.private);

   return EGL_TRUE;
}


EGLBoolean
_eglDRIGetDisplayInfo(driDisplay *dpy)
{
   char path[ NAME_MAX ];
   FILE *file;
   int i, rc;
   drmSetVersion sv;
   drm_magic_t magic;

   snprintf( path, sizeof( path ), "%s/graphics/fb%d/device/device", sysfs, dpy->minor );
   file = fopen( path, "r" );
   if (!file) {
      _eglLog(_EGL_WARNING, "Unable to open %s", path);
      return EGL_FALSE;
   }
   fgets( path, sizeof( path ), file );
   sscanf( path, "%x", &dpy->chipset );
   fclose( file );

   sprintf(path, DRM_DEV_NAME, DRM_DIR_NAME, dpy->minor);
   if ( ( dpy->drmFD = open(path, O_RDWR, 0) ) < 0 ) {
      _eglLog(_EGL_WARNING, "drmOpen failed.");
      return EGL_FALSE;
   }

   /* Set the interface version, asking for 1.2 */
   sv.drm_di_major = 1;
   sv.drm_di_minor = 2;
   sv.drm_dd_major = -1;
   if ((rc = drmSetInterfaceVersion(dpy->drmFD, &sv)))
      return EGL_FALSE;

   /* self authorize */
   if (drmGetMagic(dpy->drmFD, &magic))
      return EGL_FALSE;
   if (drmAuthMagic(dpy->drmFD, magic))
      return EGL_FALSE;

   /* Map framebuffer and SAREA */
   for (i = 0; ; i++) {
      drm_handle_t handle, offset;
      drmSize size;
      drmMapType type;
      drmMapFlags flags;
      int mtrr;

      if (drmGetMap(dpy->drmFD, i, &offset, &size, &type, &flags,
                    &handle, &mtrr))
         break;

      if (type == DRM_FRAME_BUFFER) {
         rc = drmMap( dpy->drmFD, offset, size, (drmAddressPtr) &dpy->pFB);
         if (rc < 0) {
            _eglLog(_EGL_WARNING, "drmMap DRM_FAME_BUFFER failed");
            return EGL_FALSE;
         }
         dpy->fbSize = size;
         _eglLog(_EGL_INFO, "Found framebuffer size: %d", dpy->fbSize);
      }
      else if (type == DRM_SHM) {
         rc = drmMap(dpy->drmFD, offset, size, (drmAddressPtr) &dpy->pSAREA);
         if (rc < 0 ) {
            _eglLog(_EGL_WARNING, "drmMap DRM_SHM failed.");
            return EGL_FALSE;
         }
         dpy->SAREASize = SAREA_MAX;
         _eglLog(_EGL_DEBUG, "mapped SAREA 0x%08lx to %p, size %d",
                 (unsigned long) offset, dpy->pSAREA, dpy->SAREASize );
      }
   }

   if (!dpy->pFB) {
      _eglLog(_EGL_WARNING, "failed to map framebuffer");
      return EGL_FALSE;
   }

   if (!dpy->pSAREA) {
      /* if this happens, make sure you're using the most recent DRM modules */
      _eglLog(_EGL_WARNING, "failed to map SAREA");
      return EGL_FALSE;
   }

   memset( dpy->pSAREA, 0, dpy->SAREASize );

   return EGL_TRUE;
}


 /* Return the DRI per screen structure */
static __DRIscreen *
__eglFindDRIScreen(__DRInativeDisplay *ndpy, int scrn)
{
   driDisplay *disp = (driDisplay *)ndpy;
   return &disp->driScreen;
}


static GLboolean
__eglCreateContextWithConfig(__DRInativeDisplay* ndpy, int screen,
                             int configID, void* context,
                             drm_context_t * hHWContext)
{
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;

    pDRIScreen = __eglFindDRIScreen(ndpy, screen);
    if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        return GL_FALSE;
    }
    psp = (__DRIscreenPrivate *) pDRIScreen->private;
    if (psp->fd) {
        if (drmCreateContext(psp->fd, hHWContext)) {
            _eglLog(_EGL_WARNING, "drmCreateContext failed.");
            return GL_FALSE;
        }
        *(void**)context = (void*) *hHWContext;
    }
#if 0
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;

    pDRIScreen = __glXFindDRIScreen(dpy, screen);
    if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        return GL_FALSE;
    }

    psp = (__DRIscreenPrivate *) pDRIScreen->private;

    if (psp->fd) {
        if (drmCreateContext(psp->fd, hHWContext)) {
            _eglLog(_EGL_WARNING, "drmCreateContext failed.");
            return GL_FALSE;
        }
        *(void**)contextID = (void*) *hHWContext;
    }
#endif
    return GL_TRUE;
}


static GLboolean
__eglDestroyContext( __DRInativeDisplay * ndpy, int screen,  __DRIid context )
{
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;

    pDRIScreen = __eglFindDRIScreen(ndpy, screen);
    if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        return GL_FALSE;
    }
    psp = (__DRIscreenPrivate *) pDRIScreen->private;
    if (psp->fd)
      drmDestroyContext(psp->fd, context);

   return GL_TRUE;
}


static GLboolean
__eglCreateDrawable(__DRInativeDisplay * ndpy, int screen,
                    __DRIid drawable, drm_drawable_t * hHWDrawable)
{
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;

    pDRIScreen = __eglFindDRIScreen(ndpy, screen);
    if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        return GL_FALSE;
    }
    psp = (__DRIscreenPrivate *) pDRIScreen->private;
    if (psp->fd) {
        if (drmCreateDrawable(psp->fd, hHWDrawable)) {
            _eglLog(_EGL_WARNING, "drmCreateDrawable failed.");
            return GL_FALSE;
        }
    }
    return GL_TRUE;
}


static GLboolean
__eglDestroyDrawable( __DRInativeDisplay * ndpy, int screen, __DRIid drawable )
{
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;

    pDRIScreen = __eglFindDRIScreen(ndpy, screen);
    if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        return GL_FALSE;
    }
    psp = (__DRIscreenPrivate *) pDRIScreen->private;
    if (psp->fd)
      drmDestroyDrawable(psp->fd, drawable);

   return GL_TRUE;
}

static GLboolean
__eglGetDrawableInfo(__DRInativeDisplay * ndpy, int screen, __DRIid drawable,
                     unsigned int* index, unsigned int* stamp,
                     int* X, int* Y, int* W, int* H,
                     int* numClipRects, drm_clip_rect_t ** pClipRects,
                     int* backX, int* backY,
                     int* numBackClipRects, drm_clip_rect_t ** pBackClipRects )
{
    __DRIscreen *pDRIScreen;
    __DRIscreenPrivate *psp;
   driSurface *surf = Lookup_driSurface(drawable);

   pDRIScreen = __eglFindDRIScreen(ndpy, screen);

   if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
       return GL_FALSE;
   }
   psp = (__DRIscreenPrivate *) pDRIScreen->private;
   *X = 0;
   *Y = 0;
   *W = surf->Base.Width;
   *H = surf->Base.Height;

   *backX = 0;
   *backY = 0;
   *numBackClipRects = 0;
   *pBackClipRects = NULL;

   *numClipRects = 1;
   *pClipRects = malloc(sizeof(**pClipRects));
   **pClipRects = (drm_clip_rect_t){0, 0, surf->Base.Width, surf->Base.Height};

   psp->pSAREA->drawableTable[0].stamp = 1;
   *stamp = 1;
#if 0
    GLXDrawable drawable = (GLXDrawable) draw;
    drm_clip_rect_t * cliprect;
    Display* display = (Display*)dpy;
    __DRIcontextPrivate *pcp = (__DRIcontextPrivate *)CurrentContext->driContext.private;
    if (drawable == 0) {
        return GL_FALSE;
    }

    cliprect = (drm_clip_rect_t*) _mesa_malloc(sizeof(drm_clip_rect_t));
    cliprect->x1 = drawable->x;
    cliprect->y1 = drawable->y;
    cliprect->x2 = drawable->x + drawable->w;
    cliprect->y2 = drawable->y + drawable->h;
    
    /* the drawable index is by client id */
    *index = display->clientID;

    *stamp = pcp->driScreenPriv->pSAREA->drawableTable[display->clientID].stamp;
    *x = drawable->x;
    *y = drawable->y;
    *width = drawable->w;
    *height = drawable->h;
    *numClipRects = 1;
    *pClipRects = cliprect;
    
    *backX = drawable->x;
    *backY = drawable->y;
    *numBackClipRects = 0;
    *pBackClipRects = 0;
#endif
   return GL_TRUE;
}


/**
 * Implement \c __DRIinterfaceMethods::getProcAddress.
 */
static __DRIfuncPtr
get_proc_address(const char * proc_name)
{
   return NULL;
}


/**
 * Destroy a linked list of \c __GLcontextModes structures created by
 * \c _gl_context_modes_create.
 * 
 * \param modes  Linked list of structures to be destroyed.  All structres
 *               in the list will be freed.
 */
static void
__egl_context_modes_destroy(__GLcontextModes *modes)
{
   while ( modes != NULL ) {
      __GLcontextModes * const next = modes->next;

      free( modes );
      modes = next;
   }
}


/**
 * Allocate a linked list of \c __GLcontextModes structures.  The fields of
 * each structure will be initialized to "reasonable" default values.  In
 * most cases this is the default value defined by table 3.4 of the GLX
 * 1.3 specification.  This means that most values are either initialized to
 * zero or \c GLX_DONT_CARE (which is -1).  As support for additional
 * extensions is added, the new values will be initialized to appropriate
 * values from the extension specification.
 * 
 * \param count         Number of structures to allocate.
 * \param minimum_size  Minimum size of a structure to allocate.  This allows
 *                      for differences in the version of the
 *                      \c __GLcontextModes stucture used in libGL and in a
 *                      DRI-based driver.
 * \returns A pointer to the first element in a linked list of \c count
 *          stuctures on success, or \c NULL on failure.
 * 
 * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
 *          The fundamental assumption is that if the \c minimum_size
 *          specified by the driver and the size of the \c __GLcontextModes
 *          structure in libGL is the same, then the meaning of each byte in
 *          the structure is the same in both places.  \b Be \b careful!
 *          Basically this means that fields have to be added in libGL and
 *          then propagated to drivers.  Drivers should \b never arbitrarilly
 *          extend the \c __GLcontextModes data-structure.
 */
static __GLcontextModes *
__egl_context_modes_create(unsigned count, size_t minimum_size)
{
   const size_t size = (minimum_size > sizeof( __GLcontextModes ))
       ? minimum_size : sizeof( __GLcontextModes );
   __GLcontextModes * base = NULL;
   __GLcontextModes ** next;
   unsigned   i;

   next = & base;
   for ( i = 0 ; i < count ; i++ ) {
      *next = (__GLcontextModes *) malloc( size );
      if ( *next == NULL ) {
	 __egl_context_modes_destroy( base );
	 base = NULL;
	 break;
      }

      (void) memset( *next, 0, size );
      (*next)->visualID = GLX_DONT_CARE;
      (*next)->visualType = GLX_DONT_CARE;
      (*next)->visualRating = GLX_NONE;
      (*next)->transparentPixel = GLX_NONE;
      (*next)->transparentRed = GLX_DONT_CARE;
      (*next)->transparentGreen = GLX_DONT_CARE;
      (*next)->transparentBlue = GLX_DONT_CARE;
      (*next)->transparentAlpha = GLX_DONT_CARE;
      (*next)->transparentIndex = GLX_DONT_CARE;
      (*next)->xRenderable = GLX_DONT_CARE;
      (*next)->fbconfigID = GLX_DONT_CARE;
      (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;

      next = & ((*next)->next);
   }

   return base;
}


static GLboolean
__eglWindowExists(__DRInativeDisplay *dpy, __DRIid draw)
{
    return EGL_TRUE;
}


/**
 * Get the unadjusted system time (UST).  Currently, the UST is measured in
 * microseconds since Epoc.  The actual resolution of the UST may vary from
 * system to system, and the units may vary from release to release.
 * Drivers should not call this function directly.  They should instead use
 * \c glXGetProcAddress to obtain a pointer to the function.
 *
 * \param ust Location to store the 64-bit UST
 * \returns Zero on success or a negative errno value on failure.
 * 
 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
 *
 * \since Internal API version 20030317.
 */
static int
__eglGetUST(int64_t *ust)
{
    struct timeval  tv;
    
    if ( ust == NULL ) {
	return -EFAULT;
    }

    if ( gettimeofday( & tv, NULL ) == 0 ) {
	ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
	return 0;
    }
    else {
	return -errno;
    }
}

/**
 * Determine the refresh rate of the specified drawable and display.
 * 
 * \param dpy          Display whose refresh rate is to be determined.
 * \param drawable     Drawable whose refresh rate is to be determined.
 * \param numerator    Numerator of the refresh rate.
 * \param demoninator  Denominator of the refresh rate.
 * \return  If the refresh rate for the specified display and drawable could
 *          be calculated, True is returned.  Otherwise False is returned.
 * 
 * \note This function is implemented entirely client-side.  A lot of other
 *       functionality is required to export GLX_OML_sync_control, so on
 *       XFree86 this function can be called for direct-rendering contexts
 *       when GLX_OML_sync_control appears in the client extension string.
 */
static GLboolean
__eglGetMSCRate(__DRInativeDisplay * dpy, __DRIid drawable,
                int32_t * numerator, int32_t * denominator)
{
   return EGL_TRUE;
}


/**
 * Table of functions exported by the loader to the driver.
 */
static const __DRIinterfaceMethods interface_methods = {
    get_proc_address,

    __egl_context_modes_create,
    __egl_context_modes_destroy,

    __eglFindDRIScreen,
    __eglWindowExists,

    __eglCreateContextWithConfig,
    __eglDestroyContext,

    __eglCreateDrawable,
    __eglDestroyDrawable,
    __eglGetDrawableInfo,

    __eglGetUST,
    __eglGetMSCRate,
};


static int
__glXGetInternalVersion(void)
{
    return 20050725;
}

static const char createNewScreenName[] = "__driCreateNewScreen_20050727";


/**
 * Do per-display initialization.
 */
EGLBoolean
_eglDRICreateDisplay(driDisplay *dpy, __DRIframebuffer *framebuffer)
{
   PFNCREATENEWSCREENFUNC createNewScreen;
    int api_ver = __glXGetInternalVersion();
   __DRIversion ddx_version;
   __DRIversion dri_version;
   __DRIversion drm_version;
   drmVersionPtr version;

   version = drmGetVersion( dpy->drmFD );
   if ( version ) {
      drm_version.major = version->version_major;
      drm_version.minor = version->version_minor;
      drm_version.patch = version->version_patchlevel;
      drmFreeVersion( version );
   }
   else {
      drm_version.major = -1;
      drm_version.minor = -1;
      drm_version.patch = -1;
   }

   /*
    * Get device name (like "tdfx") and the ddx version numbers.
    * We'll check the version in each DRI driver's "createScreen"
    * function.
    */
   ddx_version.major = 4;
   ddx_version.minor = 0;
   ddx_version.patch = 0;

   /*
    * Get the DRI X extension version.
    */
   dri_version.major = 4;
   dri_version.minor = 0;
   dri_version.patch = 0;

   createNewScreen = ( PFNCREATENEWSCREENFUNC ) dlsym( dpy->Base.Driver->LibHandle, createNewScreenName );
   if ( !createNewScreen ) {
      _eglLog(_EGL_WARNING, "Couldn't find %s function in the driver.",
              createNewScreenName );
      return EGL_FALSE;
   }

   dpy->driScreen.private = createNewScreen( dpy, 0, &dpy->driScreen, NULL,
                            &ddx_version, &dri_version,
                            &drm_version, framebuffer,
                            dpy->pSAREA, dpy->drmFD,
                            api_ver,
                            & interface_methods,
                            NULL);
   if (!dpy->driScreen.private)
      return EGL_FALSE;

   DRM_UNLOCK( dpy->drmFD, dpy->pSAREA, dpy->serverContext );

   return EGL_TRUE;
}


/**
 * Create all the EGL screens for the given display.
 */
EGLBoolean
_eglDRICreateScreens(driDisplay *dpy)
{
   const int numScreens = 1;  /* XXX fix this someday */
   int i;

   for (i = 0; i < numScreens; i++) {
      char path[ NAME_MAX ];
      FILE *file;
      driScreen *s;

      /* Create a screen */
      if ( !( s = ( driScreen * ) calloc( 1, sizeof( *s ) ) ) )
         return EGL_FALSE;

      snprintf( s->fb, NAME_MAX, "fb%d", dpy->minor );
      _eglInitScreen( &s->Base );

      _eglAddScreen( &dpy->Base, &s->Base );

      /* Create the screen's mode list */
      snprintf( path, sizeof( path ), "%s/graphics/%s/modes", sysfs, s->fb );
      file = fopen( path, "r" );
      while ( fgets( path, sizeof( path ), file ) ) {
         unsigned int x, y, r;
         char c;
         path[ strlen( path ) - 1 ] = '\0';  /* strip off \n from sysfs */
         sscanf( path, "%c:%ux%u-%u", &c, &x, &y, &r );
         _eglAddNewMode( &s->Base, x, y, r * 1000, path );
      }
      fclose( file );

      /*
       * NOTE: we used to set the colormap here, but that didn't work reliably.
       * Some entries near the start of the table would get corrupted by later
       * mode changes.
       */
   }

   return EGL_TRUE;
}


EGLBoolean
_eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy,
                  EGLint *major, EGLint *minor)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   driDisplay *display;

   assert(disp);

   /* Create new driDisplay object to replace the _EGLDisplay that was
    * previously created.
    */
   display = calloc(1, sizeof(*display));
   display->Base = *disp;
   _eglHashInsert(_eglGlobal.Displays, disp->Handle, display);
   free(disp);

   *major = 1;
   *minor = 0;

   sscanf(&disp->Name[1], "%d", &display->minor);

   drv->Initialized = EGL_TRUE;
   return EGL_TRUE;
}


static EGLBoolean
_eglDRITerminate(_EGLDriver *drv, EGLDisplay dpy)
{
   driDisplay *display = Lookup_driDisplay(dpy);
   _eglCleanupDisplay(&display->Base);/*rename that function*/
   free(display);
   free(drv);
   return EGL_TRUE;
}


/**
 * Plug in the DRI-specific functions into the driver's dispatch table.
 * Also, enable some EGL extensions.
 */
void
_eglDRIInitDriverFallbacks(_EGLDriver *drv)
{
   _eglInitDriverFallbacks(drv);

   drv->API.Initialize = _eglDRIInitialize;
   drv->API.Terminate = _eglDRITerminate;
   drv->API.CreateContext = _eglDRICreateContext;
   drv->API.MakeCurrent = _eglDRIMakeCurrent;
   drv->API.CreatePbufferSurface = _eglDRICreatePbufferSurface;
   drv->API.DestroySurface = _eglDRIDestroySurface;
   drv->API.DestroyContext = _eglDRIDestroyContext;
   drv->API.CreateScreenSurfaceMESA = _eglDRICreateScreenSurfaceMESA;
   drv->API.ShowScreenSurfaceMESA = _eglDRIShowScreenSurfaceMESA;
   drv->API.SwapBuffers = _eglDRISwapBuffers;

   /* enable supported extensions */
   drv->Extensions.MESA_screen_surface = EGL_TRUE;
   drv->Extensions.MESA_copy_context = EGL_TRUE;
}