2 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright © 2008 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Soft-
7 * ware"), to deal in the Software without restriction, including without
8 * limitation the rights to use, copy, modify, merge, publish, distribute,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, provided that the above copyright
11 * notice(s) and this permission notice appear in all copies of the Soft-
12 * ware and that both the above copyright notice(s) and this permission
13 * notice appear in supporting documentation.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
18 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
19 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
20 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
23 * MANCE OF THIS SOFTWARE.
25 * Except as contained in this notice, the name of a copyright holder shall
26 * not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization of
28 * the copyright holder.
31 * Kevin E. Martin <kevin@precisioninsight.com>
32 * Brian Paul <brian@precisioninsight.com>
33 * Kristian Høgsberg (krh@redhat.com)
36 #ifdef GLX_DIRECT_RENDERING
41 #include "glxclient.h"
42 #include "glcontextmodes.h"
43 #include "dri_common.h"
52 _X_HIDDEN
void InfoMessageF(const char *f
, ...)
57 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
58 fprintf(stderr
, "libGL: ");
60 vfprintf(stderr
, f
, args
);
66 * Print error to stderr, unless LIBGL_DEBUG=="quiet".
68 _X_HIDDEN
void ErrorMessageF(const char *f
, ...)
73 if ((env
= getenv("LIBGL_DEBUG")) && !strstr(env
, "quiet")) {
74 fprintf(stderr
, "libGL error: ");
76 vfprintf(stderr
, f
, args
);
81 #ifndef DEFAULT_DRIVER_DIR
82 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
83 #define DEFAULT_DRIVER_DIR "/usr/local/lib/dri"
87 * Try to \c dlopen the named driver.
89 * This function adds the "_dri.so" suffix to the driver name and searches the
90 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
91 * order to find the driver.
93 * \param driverName - a name like "tdfx", "i810", "mga", etc.
96 * A handle from \c dlopen, or \c NULL if driver file not found.
98 _X_HIDDEN
void *driOpenDriver(const char *driverName
)
100 void *glhandle
, *handle
;
101 const char *libPaths
, *p
, *next
;
102 char realDriverName
[200];
105 /* Attempt to make sure libGL symbols will be visible to the driver */
106 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
109 if (geteuid() == getuid()) {
110 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
111 libPaths
= getenv("LIBGL_DRIVERS_PATH");
113 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
115 if (libPaths
== NULL
)
116 libPaths
= DEFAULT_DRIVER_DIR
;
119 for (p
= libPaths
; *p
; p
= next
) {
120 next
= strchr(p
, ':');
130 snprintf(realDriverName
, sizeof realDriverName
,
131 "%.*s/tls/%s_dri.so", len
, p
, driverName
);
132 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
133 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
136 if ( handle
== NULL
) {
137 snprintf(realDriverName
, sizeof realDriverName
,
138 "%.*s/%s_dri.so", len
, p
, driverName
);
139 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
140 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
143 if ( handle
!= NULL
)
146 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
150 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
158 _X_HIDDEN
const __DRIsystemTimeExtension systemTimeExtension
= {
159 { __DRI_SYSTEM_TIME
, __DRI_SYSTEM_TIME_VERSION
},
164 #define __ATTRIB(attrib, field) \
165 { attrib, offsetof(__GLcontextModes, field) }
167 static const struct { unsigned int attrib
, offset
; } attribMap
[] = {
168 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
169 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
170 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
171 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
172 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
173 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
174 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
175 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
176 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
177 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
178 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
179 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
180 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
181 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
182 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
183 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
184 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
186 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
187 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentIndex
),
188 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
189 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
190 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
191 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
192 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
193 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
194 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
195 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
197 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
198 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
199 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
200 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
201 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
203 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
205 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
206 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
207 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
, bindToMipmapTexture
),
208 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
211 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
214 scalarEqual(__GLcontextModes
*mode
, unsigned int attrib
, unsigned int value
)
216 unsigned int glxValue
;
219 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
220 if (attribMap
[i
].attrib
== attrib
) {
221 glxValue
= *(unsigned int *) ((char *) mode
+ attribMap
[i
].offset
);
222 return glxValue
== GLX_DONT_CARE
|| glxValue
== value
;
225 return GL_TRUE
; /* Is a non-existing attribute equal to value? */
229 driConfigEqual(const __DRIcoreExtension
*core
,
230 __GLcontextModes
*modes
, const __DRIconfig
*driConfig
)
232 unsigned int attrib
, value
, glxValue
;
236 while (core
->indexConfigAttrib(driConfig
, i
++, &attrib
, &value
)) {
238 case __DRI_ATTRIB_RENDER_TYPE
:
240 if (value
& __DRI_ATTRIB_RGBA_BIT
) {
241 glxValue
|= GLX_RGBA_BIT
;
242 } else if (value
& __DRI_ATTRIB_COLOR_INDEX_BIT
) {
243 glxValue
|= GLX_COLOR_INDEX_BIT
;
245 if (glxValue
!= modes
->renderType
)
249 case __DRI_ATTRIB_CONFIG_CAVEAT
:
250 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
251 glxValue
= GLX_NON_CONFORMANT_CONFIG
;
252 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
253 glxValue
= GLX_SLOW_CONFIG
;
256 if (glxValue
!= modes
->visualRating
)
260 case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
:
262 if (value
& __DRI_ATTRIB_TEXTURE_1D_BIT
)
263 glxValue
|= GLX_TEXTURE_1D_BIT_EXT
;
264 if (value
& __DRI_ATTRIB_TEXTURE_2D_BIT
)
265 glxValue
|= GLX_TEXTURE_2D_BIT_EXT
;
266 if (value
& __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
)
267 glxValue
|= GLX_TEXTURE_RECTANGLE_BIT_EXT
;
268 if (modes
->bindToTextureTargets
!= GLX_DONT_CARE
&&
269 glxValue
!= modes
->bindToTextureTargets
)
274 if (!scalarEqual(modes
, attrib
, value
))
282 static __GLcontextModes
*
283 createDriMode(const __DRIcoreExtension
*core
,
284 __GLcontextModes
*modes
, const __DRIconfig
**driConfigs
)
286 __GLXDRIconfigPrivate
*config
;
289 for (i
= 0; driConfigs
[i
]; i
++) {
290 if (driConfigEqual(core
, modes
, driConfigs
[i
]))
294 if (driConfigs
[i
] == NULL
)
297 config
= Xmalloc(sizeof *config
);
301 config
->modes
= *modes
;
302 config
->driConfig
= driConfigs
[i
];
304 return &config
->modes
;
307 _X_HIDDEN __GLcontextModes
*
308 driConvertConfigs(const __DRIcoreExtension
*core
,
309 __GLcontextModes
*modes
, const __DRIconfig
**configs
)
311 __GLcontextModes head
, *tail
, *m
;
315 for (m
= modes
; m
; m
= m
->next
) {
316 tail
->next
= createDriMode(core
, m
, configs
);
317 if (tail
->next
== NULL
) {
318 /* no matching dri config for m */
326 _gl_context_modes_destroy(modes
);
332 driBindExtensions(__GLXscreenConfigs
*psc
)
334 const __DRIextension
**extensions
;
337 extensions
= psc
->core
->getExtensions(psc
->__driScreen
);
339 for (i
= 0; extensions
[i
]; i
++) {
340 #ifdef __DRI_COPY_SUB_BUFFER
341 if (strcmp(extensions
[i
]->name
, __DRI_COPY_SUB_BUFFER
) == 0) {
342 psc
->copySubBuffer
= (__DRIcopySubBufferExtension
*) extensions
[i
];
343 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer_bit");
347 #ifdef __DRI_SWAP_CONTROL
348 if (strcmp(extensions
[i
]->name
, __DRI_SWAP_CONTROL
) == 0) {
349 psc
->swapControl
= (__DRIswapControlExtension
*) extensions
[i
];
350 __glXEnableDirectExtension(psc
, "GLX_SGI_swap_control");
351 __glXEnableDirectExtension(psc
, "GLX_MESA_swap_control");
355 #ifdef __DRI_ALLOCATE
356 if (strcmp(extensions
[i
]->name
, __DRI_ALLOCATE
) == 0) {
357 psc
->allocate
= (__DRIallocateExtension
*) extensions
[i
];
358 __glXEnableDirectExtension(psc
, "GLX_MESA_allocate_memory");
362 #ifdef __DRI_FRAME_TRACKING
363 if (strcmp(extensions
[i
]->name
, __DRI_FRAME_TRACKING
) == 0) {
364 psc
->frameTracking
= (__DRIframeTrackingExtension
*) extensions
[i
];
365 __glXEnableDirectExtension(psc
, "GLX_MESA_swap_frame_usage");
369 #ifdef __DRI_MEDIA_STREAM_COUNTER
370 if (strcmp(extensions
[i
]->name
, __DRI_MEDIA_STREAM_COUNTER
) == 0) {
371 psc
->msc
= (__DRImediaStreamCounterExtension
*) extensions
[i
];
372 __glXEnableDirectExtension(psc
, "GLX_SGI_video_sync");
376 #ifdef __DRI_SWAP_BUFFER_COUNTER
377 /* No driver supports this at this time and the extension is
378 * not defined in dri_interface.h. Will enable
379 * GLX_OML_sync_control if implemented. */
382 #ifdef __DRI_READ_DRAWABLE
383 if (strcmp(extensions
[i
]->name
, __DRI_READ_DRAWABLE
) == 0) {
384 __glXEnableDirectExtension(psc
, "GLX_SGI_make_current_read");
388 #ifdef __DRI_TEX_BUFFER
389 if (strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0) {
390 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
391 __glXEnableDirectExtension(psc
, "GLX_EXT_texture_from_pixmap");
395 /* Ignore unknown extensions */
399 #endif /* GLX_DIRECT_RENDERING */