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 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
41 #include "glxclient.h"
42 #include "dri_common.h"
53 dri_message(int level
, const char *f
, ...)
56 int threshold
= _LOADER_WARNING
;
57 const char *libgl_debug
;
59 libgl_debug
= getenv("LIBGL_DEBUG");
61 if (strstr(libgl_debug
, "quiet"))
62 threshold
= _LOADER_FATAL
;
63 else if (strstr(libgl_debug
, "verbose"))
64 threshold
= _LOADER_DEBUG
;
67 /* Note that the _LOADER_* levels are lower numbers for more severe. */
68 if (level
<= threshold
) {
69 fprintf(stderr
, "libGL%s: ", level
<= _LOADER_WARNING
? " error" : "");
71 vfprintf(stderr
, f
, args
);
77 #define GL_LIB_NAME "libGL.so.1"
80 #ifndef DEFAULT_DRIVER_DIR
81 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
82 #define DEFAULT_DRIVER_DIR "/usr/local/lib/dri"
86 * Try to \c dlopen the named driver.
88 * This function adds the "_dri.so" suffix to the driver name and searches the
89 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
90 * order to find the driver.
92 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
95 * A handle from \c dlopen, or \c NULL if driver file not found.
98 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(GL_LIB_NAME
, 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
, ':');
131 snprintf(realDriverName
, sizeof realDriverName
,
132 "%.*s/tls/%s_dri.so", len
, p
, driverName
);
133 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
134 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
137 if (handle
== NULL
) {
138 snprintf(realDriverName
, sizeof realDriverName
,
139 "%.*s/%s_dri.so", len
, p
, driverName
);
140 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
141 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
147 InfoMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
151 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
159 _X_HIDDEN
const __DRIextension
**
160 driGetDriverExtensions(void *handle
, const char *driver_name
)
162 const __DRIextension
**extensions
= NULL
;
163 const __DRIextension
**(*get_extensions
)(void);
164 char *get_extensions_name
;
166 if (asprintf(&get_extensions_name
, "%s_%s",
167 __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) != -1) {
168 get_extensions
= dlsym(handle
, get_extensions_name
);
169 if (get_extensions
) {
170 free(get_extensions_name
);
171 return get_extensions();
173 InfoMessageF("driver does not expose %s(): %s\n",
174 get_extensions_name
, dlerror());
175 free(get_extensions_name
);
179 extensions
= dlsym(handle
, __DRI_DRIVER_EXTENSIONS
);
180 if (extensions
== NULL
) {
181 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
189 __driGetMSCRate(__DRIdrawable
*draw
,
190 int32_t * numerator
, int32_t * denominator
,
193 __GLXDRIdrawable
*glxDraw
= loaderPrivate
;
195 return __glxGetMscRate(glxDraw
->psc
, numerator
, denominator
);
198 _X_HIDDEN
const __DRIsystemTimeExtension systemTimeExtension
= {
199 .base
= {__DRI_SYSTEM_TIME
, 1 },
201 .getUST
= __glXGetUST
,
202 .getMSCRate
= __driGetMSCRate
205 #define __ATTRIB(attrib, field) \
206 { attrib, offsetof(struct glx_config, field) }
210 unsigned int attrib
, offset
;
212 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
213 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
214 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
215 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
216 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
217 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
218 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
219 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
220 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
221 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
222 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
223 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
224 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
225 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
226 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
227 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
228 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
230 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
231 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentIndex
),
232 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
233 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
234 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
235 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
236 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
237 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
238 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
239 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
241 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
242 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
243 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
244 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
245 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
247 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
249 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
250 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
251 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
,
252 bindToMipmapTexture
),
253 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
254 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
, sRGBCapable
)
258 scalarEqual(struct glx_config
*mode
, unsigned int attrib
, unsigned int value
)
260 unsigned glxValue
, i
;
262 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
263 if (attribMap
[i
].attrib
== attrib
) {
264 glxValue
= *(unsigned int *) ((char *) mode
+ attribMap
[i
].offset
);
265 return glxValue
== GLX_DONT_CARE
|| glxValue
== value
;
268 return GL_TRUE
; /* Is a non-existing attribute equal to value? */
272 driConfigEqual(const __DRIcoreExtension
*core
,
273 struct glx_config
*config
, const __DRIconfig
*driConfig
)
275 unsigned int attrib
, value
, glxValue
;
279 while (core
->indexConfigAttrib(driConfig
, i
++, &attrib
, &value
)) {
281 case __DRI_ATTRIB_RENDER_TYPE
:
283 if (value
& __DRI_ATTRIB_RGBA_BIT
) {
284 glxValue
|= GLX_RGBA_BIT
;
286 if (value
& __DRI_ATTRIB_COLOR_INDEX_BIT
) {
287 glxValue
|= GLX_COLOR_INDEX_BIT
;
289 if (value
& __DRI_ATTRIB_FLOAT_BIT
) {
290 glxValue
|= GLX_RGBA_FLOAT_BIT_ARB
;
292 if (value
& __DRI_ATTRIB_UNSIGNED_FLOAT_BIT
) {
293 glxValue
|= GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
;
295 if (glxValue
!= config
->renderType
)
299 case __DRI_ATTRIB_CONFIG_CAVEAT
:
300 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
301 glxValue
= GLX_NON_CONFORMANT_CONFIG
;
302 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
303 glxValue
= GLX_SLOW_CONFIG
;
306 if (glxValue
!= config
->visualRating
)
310 case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
:
312 if (value
& __DRI_ATTRIB_TEXTURE_1D_BIT
)
313 glxValue
|= GLX_TEXTURE_1D_BIT_EXT
;
314 if (value
& __DRI_ATTRIB_TEXTURE_2D_BIT
)
315 glxValue
|= GLX_TEXTURE_2D_BIT_EXT
;
316 if (value
& __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
)
317 glxValue
|= GLX_TEXTURE_RECTANGLE_BIT_EXT
;
318 if (config
->bindToTextureTargets
!= GLX_DONT_CARE
&&
319 glxValue
!= config
->bindToTextureTargets
)
324 if (!scalarEqual(config
, attrib
, value
))
332 static struct glx_config
*
333 createDriMode(const __DRIcoreExtension
* core
,
334 struct glx_config
*config
, const __DRIconfig
**driConfigs
)
336 __GLXDRIconfigPrivate
*driConfig
;
339 for (i
= 0; driConfigs
[i
]; i
++) {
340 if (driConfigEqual(core
, config
, driConfigs
[i
]))
344 if (driConfigs
[i
] == NULL
)
347 driConfig
= malloc(sizeof *driConfig
);
348 if (driConfig
== NULL
)
351 driConfig
->base
= *config
;
352 driConfig
->driConfig
= driConfigs
[i
];
354 return &driConfig
->base
;
357 _X_HIDDEN
struct glx_config
*
358 driConvertConfigs(const __DRIcoreExtension
* core
,
359 struct glx_config
*configs
, const __DRIconfig
**driConfigs
)
361 struct glx_config head
, *tail
, *m
;
365 for (m
= configs
; m
; m
= m
->next
) {
366 tail
->next
= createDriMode(core
, m
, driConfigs
);
367 if (tail
->next
== NULL
) {
368 /* no matching dri config for m */
380 driDestroyConfigs(const __DRIconfig
**configs
)
384 for (i
= 0; configs
[i
]; i
++)
385 free((__DRIconfig
*) configs
[i
]);
389 _X_HIDDEN __GLXDRIdrawable
*
390 driFetchDrawable(struct glx_context
*gc
, GLXDrawable glxDrawable
)
392 struct glx_display
*const priv
= __glXInitialize(gc
->psc
->dpy
);
393 __GLXDRIdrawable
*pdraw
;
394 struct glx_screen
*psc
;
399 if (glxDrawable
== None
)
402 psc
= priv
->screens
[gc
->screen
];
403 if (priv
->drawHash
== NULL
)
406 if (__glxHashLookup(priv
->drawHash
, glxDrawable
, (void *) &pdraw
) == 0) {
411 pdraw
= psc
->driScreen
->createDrawable(psc
, glxDrawable
,
412 glxDrawable
, gc
->config
);
415 ErrorMessageF("failed to create drawable\n");
419 if (__glxHashInsert(priv
->drawHash
, glxDrawable
, pdraw
)) {
420 (*pdraw
->destroyDrawable
) (pdraw
);
429 driReleaseDrawables(struct glx_context
*gc
)
431 const struct glx_display
*priv
= gc
->psc
->display
;
432 __GLXDRIdrawable
*pdraw
;
437 if (__glxHashLookup(priv
->drawHash
,
438 gc
->currentDrawable
, (void *) &pdraw
) == 0) {
439 if (pdraw
->drawable
== pdraw
->xDrawable
) {
441 if (pdraw
->refcount
== 0) {
442 (*pdraw
->destroyDrawable
)(pdraw
);
443 __glxHashDelete(priv
->drawHash
, gc
->currentDrawable
);
448 if (__glxHashLookup(priv
->drawHash
,
449 gc
->currentReadable
, (void *) &pdraw
) == 0) {
450 if (pdraw
->drawable
== pdraw
->xDrawable
) {
452 if (pdraw
->refcount
== 0) {
453 (*pdraw
->destroyDrawable
)(pdraw
);
454 __glxHashDelete(priv
->drawHash
, gc
->currentReadable
);
459 gc
->currentDrawable
= None
;
460 gc
->currentReadable
= None
;
465 dri2_convert_glx_attribs(unsigned num_attribs
, const uint32_t *attribs
,
466 unsigned *major_ver
, unsigned *minor_ver
,
467 uint32_t *render_type
, uint32_t *flags
, unsigned *api
,
468 int *reset
, unsigned *error
)
471 bool got_profile
= false;
476 *render_type
= GLX_RGBA_TYPE
;
477 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
479 *api
= __DRI_API_OPENGL
;
481 if (num_attribs
== 0) {
485 /* This is actually an internal error, but what the heck.
487 if (attribs
== NULL
) {
488 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
492 for (i
= 0; i
< num_attribs
; i
++) {
493 switch (attribs
[i
* 2]) {
494 case GLX_CONTEXT_MAJOR_VERSION_ARB
:
495 *major_ver
= attribs
[i
* 2 + 1];
497 case GLX_CONTEXT_MINOR_VERSION_ARB
:
498 *minor_ver
= attribs
[i
* 2 + 1];
500 case GLX_CONTEXT_FLAGS_ARB
:
501 *flags
= attribs
[i
* 2 + 1];
503 case GLX_CONTEXT_PROFILE_MASK_ARB
:
504 profile
= attribs
[i
* 2 + 1];
507 case GLX_RENDER_TYPE
:
508 *render_type
= attribs
[i
* 2 + 1];
510 case GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB
:
511 switch (attribs
[i
* 2 + 1]) {
512 case GLX_NO_RESET_NOTIFICATION_ARB
:
513 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
515 case GLX_LOSE_CONTEXT_ON_RESET_ARB
:
516 *reset
= __DRI_CTX_RESET_LOSE_CONTEXT
;
519 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
524 /* If an unknown attribute is received, fail.
526 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
532 if (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
533 *api
= __DRI_API_OPENGL_CORE
;
536 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
537 /* There are no profiles before OpenGL 3.2. The
538 * GLX_ARB_create_context_profile spec says:
540 * "If the requested OpenGL version is less than 3.2,
541 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
542 * of the context is determined solely by the requested version."
544 *api
= (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
545 ? __DRI_API_OPENGL_CORE
: __DRI_API_OPENGL
;
547 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
548 *api
= __DRI_API_OPENGL
;
550 case GLX_CONTEXT_ES2_PROFILE_BIT_EXT
:
551 *api
= __DRI_API_GLES2
;
554 *error
= __DRI_CTX_ERROR_BAD_API
;
559 /* Unknown flag value.
561 if (*flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
562 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)) {
563 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
567 /* There are no forward-compatible contexts before OpenGL 3.0. The
568 * GLX_ARB_create_context spec says:
570 * "Forward-compatible contexts are defined only for OpenGL versions
573 if (*major_ver
< 3 && (*flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
574 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
578 if (*major_ver
>= 3 && *render_type
== GLX_COLOR_INDEX_TYPE
) {
579 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
583 /* The GLX_EXT_create_context_es2_profile spec says:
585 * "... If the version requested is 2.0, and the
586 * GLX_CONTEXT_ES2_PROFILE_BIT_EXT bit is set in the
587 * GLX_CONTEXT_PROFILE_MASK_ARB attribute (see below), then the context
588 * returned will implement OpenGL ES 2.0. This is the only way in which
589 * an implementation may request an OpenGL ES 2.0 context."
591 if (*api
== __DRI_API_GLES2
&& (*major_ver
!= 2 || *minor_ver
!= 0)) {
592 *error
= __DRI_CTX_ERROR_BAD_API
;
596 *error
= __DRI_CTX_ERROR_SUCCESS
;
600 #endif /* GLX_DIRECT_RENDERING */