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
|
/**************************************************************************
Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
All Rights Reserved.
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, sub license, 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 NON-INFRINGEMENT.
IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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:
* Kevin E. Martin <kevin@precisioninsight.com>
* Brian Paul <brian@precisioninsight.com>
*
*/
#ifdef GLX_DIRECT_RENDERING
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xdamage.h>
#include "glheader.h"
#include "glxclient.h"
#include "glcontextmodes.h"
#include "xf86dri.h"
#include "sarea.h"
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "xf86drm.h"
#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif
#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0
#endif
typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
typedef struct __GLXDRIconfigPrivateRec __GLXDRIconfigPrivate;
struct __GLXDRIdisplayPrivateRec {
__GLXDRIdisplay base;
/*
** XFree86-DRI version information
*/
int driMajor;
int driMinor;
int driPatch;
};
struct __GLXDRIcontextPrivateRec {
__GLXDRIcontext base;
__DRIcontext *driContext;
XID hwContextID;
__GLXscreenConfigs *psc;
};
struct __GLXDRIconfigPrivateRec {
__GLcontextModes modes;
const __DRIconfig *driConfig;
};
#ifndef DEFAULT_DRIVER_DIR
/* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
#define DEFAULT_DRIVER_DIR "/usr/X11R6/lib/modules/dri"
#endif
static void InfoMessageF(const char *f, ...)
{
va_list args;
const char *env;
if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) {
fprintf(stderr, "libGL: ");
va_start(args, f);
vfprintf(stderr, f, args);
va_end(args);
}
}
extern void ErrorMessageF(const char *f, ...);
/**
* Print error to stderr, unless LIBGL_DEBUG=="quiet".
*/
_X_HIDDEN void ErrorMessageF(const char *f, ...)
{
va_list args;
const char *env;
if ((env = getenv("LIBGL_DEBUG")) && !strstr(env, "quiet")) {
fprintf(stderr, "libGL error: ");
va_start(args, f);
vfprintf(stderr, f, args);
va_end(args);
}
}
extern void *driOpenDriver(const char *driverName);
/**
* Try to \c dlopen the named driver.
*
* This function adds the "_dri.so" suffix to the driver name and searches the
* directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
* order to find the driver.
*
* \param driverName - a name like "tdfx", "i810", "mga", etc.
*
* \returns
* A handle from \c dlopen, or \c NULL if driver file not found.
*/
_X_HIDDEN void *driOpenDriver(const char *driverName)
{
void *glhandle, *handle;
const char *libPaths, *p, *next;
char realDriverName[200];
int len;
/* Attempt to make sure libGL symbols will be visible to the driver */
glhandle = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);
libPaths = NULL;
if (geteuid() == getuid()) {
/* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
libPaths = getenv("LIBGL_DRIVERS_PATH");
if (!libPaths)
libPaths = getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
}
if (libPaths == NULL)
libPaths = DEFAULT_DRIVER_DIR;
handle = NULL;
for (p = libPaths; *p; p = next) {
next = strchr(p, ':');
if (next == NULL) {
len = strlen(p);
next = p + len;
} else {
len = next - p;
next++;
}
#ifdef GLX_USE_TLS
snprintf(realDriverName, sizeof realDriverName,
"%.*s/tls/%s_dri.so", len, p, driverName);
InfoMessageF("OpenDriver: trying %s\n", realDriverName);
handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
#endif
if ( handle == NULL ) {
snprintf(realDriverName, sizeof realDriverName,
"%.*s/%s_dri.so", len, p, driverName);
InfoMessageF("OpenDriver: trying %s\n", realDriverName);
handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
}
if ( handle != NULL )
break;
else
ErrorMessageF("dlopen %s failed (%s)\n", realDriverName, dlerror());
}
if (!handle)
ErrorMessageF("unable to load driver: %s_dri.so\n", driverName);
if (glhandle)
dlclose(glhandle);
return handle;
}
/*
* Given a display pointer and screen number, determine the name of
* the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
* Return True for success, False for failure.
*/
static Bool GetDriverName(Display *dpy, int scrNum, char **driverName)
{
int directCapable;
Bool b;
int driverMajor, driverMinor, driverPatch;
*driverName = NULL;
if (!XF86DRIQueryDirectRenderingCapable(dpy, scrNum, &directCapable)) {
ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
return False;
}
if (!directCapable) {
ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
return False;
}
b = XF86DRIGetClientDriverName(dpy, scrNum, &driverMajor, &driverMinor,
&driverPatch, driverName);
if (!b) {
ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum);
return False;
}
InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
driverMajor, driverMinor, driverPatch, *driverName, scrNum);
return True;
}
/*
* Given a display pointer and screen number, return a __DRIdriver handle.
* Return NULL if anything goes wrong.
*/
static void *driGetDriver(Display *dpy, int scrNum)
{
char *driverName;
void *ret;
if (GetDriverName(dpy, scrNum, &driverName)) {
ret = driOpenDriver(driverName);
if (driverName)
Xfree(driverName);
return ret;
}
return NULL;
}
/*
* Exported function for querying the DRI driver for a given screen.
*
* The returned char pointer points to a static array that will be
* overwritten by subsequent calls.
*/
PUBLIC const char *glXGetScreenDriver (Display *dpy, int scrNum) {
static char ret[32];
char *driverName;
if (GetDriverName(dpy, scrNum, &driverName)) {
int len;
if (!driverName)
return NULL;
len = strlen (driverName);
if (len >= 31)
return NULL;
memcpy (ret, driverName, len+1);
Xfree(driverName);
return ret;
}
return NULL;
}
/*
* Exported function for obtaining a driver's option list (UTF-8 encoded XML).
*
* The returned char pointer points directly into the driver. Therefore
* it should be treated as a constant.
*
* If the driver was not found or does not support configuration NULL is
* returned.
*
* Note: The driver remains opened after this function returns.
*/
PUBLIC const char *glXGetDriverConfig (const char *driverName)
{
void *handle = driOpenDriver (driverName);
if (handle)
return dlsym (handle, "__driConfigOptions");
else
return NULL;
}
extern void
driFilterModes(__GLcontextModes ** server_modes,
const __GLcontextModes * driver_modes);
_X_HIDDEN void
driFilterModes(__GLcontextModes ** server_modes,
const __GLcontextModes * driver_modes)
{
__GLcontextModes * m;
__GLcontextModes ** prev_next;
const __GLcontextModes * check;
if (driver_modes == NULL) {
fprintf(stderr, "libGL warning: 3D driver returned no fbconfigs.\n");
return;
}
/* For each mode in server_modes, check to see if a matching mode exists
* in driver_modes. If not, then the mode is not available.
*/
prev_next = server_modes;
for ( m = *prev_next ; m != NULL ; m = *prev_next ) {
GLboolean do_delete = GL_TRUE;
for ( check = driver_modes ; check != NULL ; check = check->next ) {
if ( _gl_context_modes_are_same( m, check ) ) {
do_delete = GL_FALSE;
break;
}
}
/* The 3D has to support all the modes that match the GLX visuals
* sent from the X server.
*/
if ( do_delete && (m->visualID != 0) ) {
do_delete = GL_FALSE;
/* don't warn for this visual (Novell #247471 / X.Org #6689) */
if (m->visualRating != GLX_NON_CONFORMANT_CONFIG) {
fprintf(stderr, "libGL warning: 3D driver claims to not "
"support visual 0x%02x\n", m->visualID);
}
}
if ( do_delete ) {
*prev_next = m->next;
m->next = NULL;
_gl_context_modes_destroy( m );
}
else {
prev_next = & m->next;
}
}
}
#ifdef XDAMAGE_1_1_INTERFACE
static GLboolean has_damage_post(Display *dpy)
{
static GLboolean inited = GL_FALSE;
static GLboolean has_damage;
if (!inited) {
int major, minor;
if (XDamageQueryVersion(dpy, &major, &minor) &&
major == 1 && minor >= 1)
{
has_damage = GL_TRUE;
} else {
has_damage = GL_FALSE;
}
inited = GL_TRUE;
}
return has_damage;
}
static void __glXReportDamage(__DRIdrawable *driDraw,
int x, int y,
drm_clip_rect_t *rects, int num_rects,
GLboolean front_buffer,
void *loaderPrivate)
{
XRectangle *xrects;
XserverRegion region;
int i;
int x_off, y_off;
__GLXDRIdrawable *glxDraw = loaderPrivate;
__GLXscreenConfigs *psc = glxDraw->psc;
Display *dpy = psc->dpy;
Drawable drawable;
if (!has_damage_post(dpy))
return;
if (front_buffer) {
x_off = x;
y_off = y;
drawable = RootWindow(dpy, psc->scr);
} else{
x_off = 0;
y_off = 0;
drawable = glxDraw->xDrawable;
}
xrects = malloc(sizeof(XRectangle) * num_rects);
if (xrects == NULL)
return;
for (i = 0; i < num_rects; i++) {
xrects[i].x = rects[i].x1 + x_off;
xrects[i].y = rects[i].y1 + y_off;
xrects[i].width = rects[i].x2 - rects[i].x1;
xrects[i].height = rects[i].y2 - rects[i].y1;
}
region = XFixesCreateRegion(dpy, xrects, num_rects);
free(xrects);
XDamageAdd(dpy, drawable, region);
XFixesDestroyRegion(dpy, region);
}
static const __DRIdamageExtension damageExtension = {
{ __DRI_DAMAGE, __DRI_DAMAGE_VERSION },
__glXReportDamage,
};
#endif
static GLboolean
__glXDRIGetDrawableInfo(__DRIdrawable *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,
void *loaderPrivate)
{
__GLXDRIdrawable *glxDraw = loaderPrivate;
__GLXscreenConfigs *psc = glxDraw->psc;
Display *dpy = psc->dpy;
return XF86DRIGetDrawableInfo(dpy, psc->scr, glxDraw->drawable,
index, stamp, X, Y, W, H,
numClipRects, pClipRects,
backX, backY,
numBackClipRects, pBackClipRects);
}
_X_HIDDEN const __DRIsystemTimeExtension systemTimeExtension = {
{ __DRI_SYSTEM_TIME, __DRI_SYSTEM_TIME_VERSION },
__glXGetUST,
__driGetMscRateOML,
};
static const __DRIgetDrawableInfoExtension getDrawableInfoExtension = {
{ __DRI_GET_DRAWABLE_INFO, __DRI_GET_DRAWABLE_INFO_VERSION },
__glXDRIGetDrawableInfo
};
static const __DRIextension *loader_extensions[] = {
&systemTimeExtension.base,
&getDrawableInfoExtension.base,
#ifdef XDAMAGE_1_1_INTERFACE
&damageExtension.base,
#endif
NULL
};
#define __ATTRIB(attrib, field) \
{ attrib, offsetof(__GLcontextModes, field) }
static const struct { unsigned int attrib, offset; } attribMap[] = {
__ATTRIB(__DRI_ATTRIB_BUFFER_SIZE, rgbBits),
__ATTRIB(__DRI_ATTRIB_LEVEL, level),
__ATTRIB(__DRI_ATTRIB_RED_SIZE, redBits),
__ATTRIB(__DRI_ATTRIB_GREEN_SIZE, greenBits),
__ATTRIB(__DRI_ATTRIB_BLUE_SIZE, blueBits),
__ATTRIB(__DRI_ATTRIB_ALPHA_SIZE, alphaBits),
__ATTRIB(__DRI_ATTRIB_DEPTH_SIZE, depthBits),
__ATTRIB(__DRI_ATTRIB_STENCIL_SIZE, stencilBits),
__ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits),
__ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits),
__ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits),
__ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits),
__ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS, sampleBuffers),
__ATTRIB(__DRI_ATTRIB_SAMPLES, samples),
__ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode),
__ATTRIB(__DRI_ATTRIB_STEREO, stereoMode),
__ATTRIB(__DRI_ATTRIB_AUX_BUFFERS, numAuxBuffers),
#if 0
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE, transparentPixel),
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE, transparentIndex),
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE, transparentRed),
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE, transparentGreen),
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE, transparentBlue),
__ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
__ATTRIB(__DRI_ATTRIB_RED_MASK, redMask),
__ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
__ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
__ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
#endif
__ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
__ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
__ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
__ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
__ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
#if 0
__ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
#endif
__ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
__ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
__ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE, bindToMipmapTexture),
__ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
};
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
static int
scalarEqual(__GLcontextModes *mode, unsigned int attrib, unsigned int value)
{
unsigned int glxValue;
int i;
for (i = 0; i < ARRAY_SIZE(attribMap); i++)
if (attribMap[i].attrib == attrib) {
glxValue = *(unsigned int *) ((char *) mode + attribMap[i].offset);
return glxValue == GLX_DONT_CARE || glxValue == value;
}
return GL_TRUE; /* Is a non-existing attribute equal to value? */
}
static int
driConfigEqual(const __DRIcoreExtension *core,
__GLcontextModes *modes, const __DRIconfig *driConfig)
{
unsigned int attrib, value, glxValue;
int i;
i = 0;
while (core->indexConfigAttrib(driConfig, i++, &attrib, &value)) {
switch (attrib) {
case __DRI_ATTRIB_RENDER_TYPE:
glxValue = 0;
if (value & __DRI_ATTRIB_RGBA_BIT) {
glxValue |= GLX_RGBA_BIT;
} else if (value & __DRI_ATTRIB_COLOR_INDEX_BIT) {
glxValue |= GLX_COLOR_INDEX_BIT;
}
if (glxValue != modes->renderType)
return GL_FALSE;
break;
case __DRI_ATTRIB_CONFIG_CAVEAT:
if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
glxValue = GLX_NON_CONFORMANT_CONFIG;
else if (value & __DRI_ATTRIB_SLOW_BIT)
glxValue = GLX_SLOW_CONFIG;
else
glxValue = GLX_NONE;
if (glxValue != modes->visualRating)
return GL_FALSE;
break;
case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS:
glxValue = 0;
if (value & __DRI_ATTRIB_TEXTURE_1D_BIT)
glxValue |= GLX_TEXTURE_1D_BIT_EXT;
if (value & __DRI_ATTRIB_TEXTURE_2D_BIT)
glxValue |= GLX_TEXTURE_2D_BIT_EXT;
if (value & __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT)
glxValue |= GLX_TEXTURE_RECTANGLE_BIT_EXT;
if (modes->bindToTextureTargets != GLX_DONT_CARE &&
glxValue != modes->bindToTextureTargets)
return GL_FALSE;
break;
default:
if (!scalarEqual(modes, attrib, value))
return GL_FALSE;
}
}
return GL_TRUE;
}
static __GLcontextModes *
createDriMode(const __DRIcoreExtension *core,
__GLcontextModes *modes, const __DRIconfig **driConfigs)
{
__GLXDRIconfigPrivate *config;
int i;
for (i = 0; driConfigs[i]; i++) {
if (driConfigEqual(core, modes, driConfigs[i]))
break;
}
if (driConfigs[i] == NULL)
return NULL;
config = Xmalloc(sizeof *config);
if (config == NULL)
return NULL;
config->modes = *modes;
config->driConfig = driConfigs[i];
return &config->modes;
}
extern __GLcontextModes *
driConvertConfigs(const __DRIcoreExtension *core,
__GLcontextModes *modes, const __DRIconfig **configs);
_X_HIDDEN __GLcontextModes *
driConvertConfigs(const __DRIcoreExtension *core,
__GLcontextModes *modes, const __DRIconfig **configs)
{
__GLcontextModes head, *tail, *m;
tail = &head;
head.next = NULL;
for (m = modes; m; m = m->next) {
tail->next = createDriMode(core, m, configs);
if (tail->next == NULL) {
/* no matching dri config for m */
continue;
}
tail = tail->next;
}
_gl_context_modes_destroy(modes);
return head.next;
}
/**
* Perform the required libGL-side initialization and call the client-side
* driver's \c __driCreateNewScreen function.
*
* \param dpy Display pointer.
* \param scrn Screen number on the display.
* \param psc DRI screen information.
* \param driDpy DRI display information.
* \param createNewScreen Pointer to the client-side driver's
* \c __driCreateNewScreen function.
* \returns A pointer to the \c __DRIscreenPrivate structure returned by
* the client-side driver on success, or \c NULL on failure.
*
* \todo This function needs to be modified to remove context-modes from the
* list stored in the \c __GLXscreenConfigsRec to match the list
* returned by the client-side driver.
*/
static void *
CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
__GLXDRIdisplayPrivate * driDpy)
{
void *psp = NULL;
#ifndef GLX_USE_APPLEGL
drm_handle_t hSAREA;
drmAddress pSAREA = MAP_FAILED;
char *BusID;
__DRIversion ddx_version;
__DRIversion dri_version;
__DRIversion drm_version;
__DRIframebuffer framebuffer;
int fd = -1;
int status;
const char * err_msg;
const char * err_extra;
const __DRIconfig **driver_configs;
dri_version.major = driDpy->driMajor;
dri_version.minor = driDpy->driMinor;
dri_version.patch = driDpy->driPatch;
err_msg = "XF86DRIOpenConnection";
err_extra = NULL;
framebuffer.base = MAP_FAILED;
framebuffer.dev_priv = NULL;
if (XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
int newlyopened;
fd = drmOpenOnce(NULL,BusID, &newlyopened);
Xfree(BusID); /* No longer needed */
err_msg = "open DRM";
err_extra = strerror( -fd );
if (fd >= 0) {
drm_magic_t magic;
err_msg = "drmGetMagic";
err_extra = NULL;
if (!drmGetMagic(fd, &magic)) {
drmVersionPtr version = drmGetVersion(fd);
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;
}
err_msg = "XF86DRIAuthConnection";
if (!newlyopened || XF86DRIAuthConnection(dpy, scrn, magic)) {
char *driverName;
/*
* Get device name (like "tdfx") and the ddx version
* numbers. We'll check the version in each DRI driver's
* "createNewScreen" function.
*/
err_msg = "XF86DRIGetClientDriverName";
if (XF86DRIGetClientDriverName(dpy, scrn,
&ddx_version.major,
&ddx_version.minor,
&ddx_version.patch,
&driverName)) {
drm_handle_t hFB;
int junk;
/* No longer needed. */
Xfree( driverName );
/*
* Get device-specific info. pDevPriv will point to a struct
* (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
* that has information about the screen size, depth, pitch,
* ancilliary buffers, DRM mmap handles, etc.
*/
err_msg = "XF86DRIGetDeviceInfo";
if (XF86DRIGetDeviceInfo(dpy, scrn,
&hFB,
&junk,
&framebuffer.size,
&framebuffer.stride,
&framebuffer.dev_priv_size,
&framebuffer.dev_priv)) {
framebuffer.width = DisplayWidth(dpy, scrn);
framebuffer.height = DisplayHeight(dpy, scrn);
/*
* Map the framebuffer region.
*/
status = drmMap(fd, hFB, framebuffer.size,
(drmAddressPtr)&framebuffer.base);
err_msg = "drmMap of framebuffer";
err_extra = strerror( -status );
if ( status == 0 ) {
/*
* Map the SAREA region. Further mmap regions
* may be setup in each DRI driver's
* "createNewScreen" function.
*/
status = drmMap(fd, hSAREA, SAREA_MAX,
&pSAREA);
err_msg = "drmMap of sarea";
err_extra = strerror( -status );
if ( status == 0 ) {
err_msg = "InitDriver";
err_extra = NULL;
psp = (*psc->legacy->createNewScreen)(scrn,
& ddx_version,
& dri_version,
& drm_version,
& framebuffer,
pSAREA,
fd,
loader_extensions,
& driver_configs,
psc);
if (psp) {
psc->configs =
driConvertConfigs(psc->core,
psc->configs,
driver_configs);
psc->visuals =
driConvertConfigs(psc->core,
psc->visuals,
driver_configs);
}
}
}
}
}
}
}
}
}
if ( psp == NULL ) {
if ( pSAREA != MAP_FAILED ) {
(void)drmUnmap(pSAREA, SAREA_MAX);
}
if ( framebuffer.base != MAP_FAILED ) {
(void)drmUnmap((drmAddress)framebuffer.base, framebuffer.size);
}
if ( framebuffer.dev_priv != NULL ) {
Xfree(framebuffer.dev_priv);
}
if ( fd >= 0 ) {
(void)drmCloseOnce(fd);
}
(void)XF86DRICloseConnection(dpy, scrn);
if ( err_extra != NULL ) {
fprintf(stderr, "libGL error: %s failed (%s)\n", err_msg,
err_extra);
}
else {
fprintf(stderr, "libGL error: %s failed\n", err_msg );
}
fprintf(stderr, "libGL error: reverting to (slow) indirect rendering\n");
}
#endif /* !GLX_USE_APPLEGL */
return psp;
}
static void driDestroyContext(__GLXDRIcontext *context,
__GLXscreenConfigs *psc, Display *dpy)
{
__GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
(*psc->core->destroyContext)(pcp->driContext);
XF86DRIDestroyContext(psc->dpy, psc->scr, pcp->hwContextID);
}
static Bool driBindContext(__GLXDRIcontext *context,
__GLXDRIdrawable *draw, __GLXDRIdrawable *read)
{
__GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
const __DRIcoreExtension *core = pcp->psc->core;
return (*core->bindContext)(pcp->driContext,
draw->driDrawable,
read->driDrawable);
}
static void driUnbindContext(__GLXDRIcontext *context)
{
__GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
const __DRIcoreExtension *core = pcp->psc->core;
(*core->unbindContext)(pcp->driContext);
}
static __GLXDRIcontext *driCreateContext(__GLXscreenConfigs *psc,
const __GLcontextModes *mode,
GLXContext gc,
GLXContext shareList, int renderType)
{
__GLXDRIcontextPrivate *pcp, *pcp_shared;
drm_context_t hwContext;
__DRIcontext *shared = NULL;
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
if (psc && psc->driScreen) {
if (shareList) {
pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
shared = pcp_shared->driContext;
}
pcp = Xmalloc(sizeof *pcp);
if (pcp == NULL)
return NULL;
pcp->psc = psc;
if (!XF86DRICreateContextWithConfig(psc->dpy, psc->scr,
mode->visualID,
&pcp->hwContextID, &hwContext)) {
Xfree(pcp);
return NULL;
}
pcp->driContext =
(*psc->legacy->createNewContext)(psc->__driScreen,
config->driConfig,
renderType,
shared,
hwContext,
pcp);
if (pcp->driContext == NULL) {
XF86DRIDestroyContext(psc->dpy, psc->scr, pcp->hwContextID);
Xfree(pcp);
return NULL;
}
pcp->base.destroyContext = driDestroyContext;
pcp->base.bindContext = driBindContext;
pcp->base.unbindContext = driUnbindContext;
return &pcp->base;
}
return NULL;
}
static void driDestroyDrawable(__GLXDRIdrawable *pdraw)
{
__GLXscreenConfigs *psc = pdraw->psc;
(*psc->core->destroyDrawable)(pdraw->driDrawable);
XF86DRIDestroyDrawable(psc->dpy, psc->scr, pdraw->drawable);
Xfree(pdraw);
}
static __GLXDRIdrawable *driCreateDrawable(__GLXscreenConfigs *psc,
XID xDrawable,
GLXDrawable drawable,
const __GLcontextModes *modes)
{
__GLXDRIdrawable *pdraw;
drm_drawable_t hwDrawable;
void *empty_attribute_list = NULL;
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
/* Old dri can't handle GLX 1.3+ drawable constructors. */
if (xDrawable != drawable)
return NULL;
pdraw = Xmalloc(sizeof(*pdraw));
if (!pdraw)
return NULL;
pdraw->drawable = drawable;
pdraw->psc = psc;
if (!XF86DRICreateDrawable(psc->dpy, psc->scr, drawable, &hwDrawable))
return NULL;
/* Create a new drawable */
pdraw->driDrawable =
(*psc->legacy->createNewDrawable)(psc->__driScreen,
config->driConfig,
hwDrawable,
GLX_WINDOW_BIT,
empty_attribute_list,
pdraw);
if (!pdraw->driDrawable) {
XF86DRIDestroyDrawable(psc->dpy, psc->scr, drawable);
Xfree(pdraw);
return NULL;
}
pdraw->destroyDrawable = driDestroyDrawable;
return pdraw;
}
static void driDestroyScreen(__GLXscreenConfigs *psc)
{
/* Free the direct rendering per screen data */
if (psc->__driScreen)
(*psc->core->destroyScreen)(psc->__driScreen);
psc->__driScreen = NULL;
if (psc->driver)
dlclose(psc->driver);
}
void
driBindExtensions(__GLXscreenConfigs *psc);
static __GLXDRIscreen *driCreateScreen(__GLXscreenConfigs *psc, int screen,
__GLXdisplayPrivate *priv)
{
__GLXDRIdisplayPrivate *pdp;
__GLXDRIscreen *psp;
const __DRIextension **extensions;
int i;
psp = Xmalloc(sizeof *psp);
if (psp == NULL)
return NULL;
/* Initialize per screen dynamic client GLX extensions */
psc->ext_list_first_time = GL_TRUE;
psc->driver = driGetDriver(priv->dpy, screen);
if (psc->driver == NULL) {
Xfree(psp);
return NULL;
}
extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
if (extensions == NULL) {
ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
Xfree(psp);
return NULL;
}
for (i = 0; extensions[i]; i++) {
if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
psc->core = (__DRIcoreExtension *) extensions[i];
if (strcmp(extensions[i]->name, __DRI_LEGACY) == 0)
psc->legacy = (__DRIlegacyExtension *) extensions[i];
}
if (psc->core == NULL || psc->legacy == NULL) {
Xfree(psp);
return NULL;
}
pdp = (__GLXDRIdisplayPrivate *) priv->driDisplay;
psc->__driScreen =
CallCreateNewScreen(psc->dpy, screen, psc, pdp);
if (psc->__driScreen == NULL) {
dlclose(psc->driver);
Xfree(psp);
return NULL;
}
driBindExtensions(psc);
psp->destroyScreen = driDestroyScreen;
psp->createContext = driCreateContext;
psp->createDrawable = driCreateDrawable;
return psp;
}
/* Called from __glXFreeDisplayPrivate.
*/
static void driDestroyDisplay(__GLXDRIdisplay *dpy)
{
Xfree(dpy);
}
/*
* Allocate, initialize and return a __DRIdisplayPrivate object.
* This is called from __glXInitialize() when we are given a new
* display pointer.
*/
_X_HIDDEN __GLXDRIdisplay *driCreateDisplay(Display *dpy)
{
__GLXDRIdisplayPrivate *pdpyp;
int eventBase, errorBase;
int major, minor, patch;
if (!XF86DRIQueryExtension(dpy, &eventBase, &errorBase)) {
return NULL;
}
if (!XF86DRIQueryVersion(dpy, &major, &minor, &patch)) {
return NULL;
}
pdpyp = Xmalloc(sizeof *pdpyp);
if (!pdpyp) {
return NULL;
}
pdpyp->driMajor = major;
pdpyp->driMinor = minor;
pdpyp->driPatch = patch;
pdpyp->base.destroyDisplay = driDestroyDisplay;
pdpyp->base.createScreen = driCreateScreen;
return &pdpyp->base;
}
#endif /* GLX_DIRECT_RENDERING */
|