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 * Print informational message to stderr if LIBGL_DEBUG is set to
81 InfoMessageF(const char *f
, ...)
86 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
87 fprintf(stderr
, "libGL: ");
89 vfprintf(stderr
, f
, args
);
95 * Print error message to stderr if LIBGL_DEBUG is set to anything but
96 * "quiet", (do nothing if LIBGL_DEBUG is unset).
99 ErrorMessageF(const char *f
, ...)
104 if ((env
= getenv("LIBGL_DEBUG")) && !strstr(env
, "quiet")) {
105 fprintf(stderr
, "libGL error: ");
107 vfprintf(stderr
, f
, args
);
113 * Print error message unless LIBGL_DEBUG is set to "quiet".
115 * The distinction between CriticalErrorMessageF and ErrorMessageF is
116 * that critcial errors will be printed by default, (even when
117 * LIBGL_DEBUG is unset).
120 CriticalErrorMessageF(const char *f
, ...)
125 if (!(env
= getenv("LIBGL_DEBUG")) || !strstr(env
, "quiet")) {
126 fprintf(stderr
, "libGL error: ");
128 vfprintf(stderr
, f
, args
);
131 if (!env
|| !strstr(env
, "verbose"))
132 fprintf(stderr
, "libGL error: Try again with LIBGL_DEBUG=verbose for more details.\n");
136 #ifndef DEFAULT_DRIVER_DIR
137 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
138 #define DEFAULT_DRIVER_DIR "/usr/local/lib/dri"
142 * Try to \c dlopen the named driver.
144 * This function adds the "_dri.so" suffix to the driver name and searches the
145 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
146 * order to find the driver.
148 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
151 * A handle from \c dlopen, or \c NULL if driver file not found.
154 driOpenDriver(const char *driverName
)
156 void *glhandle
, *handle
;
157 const char *libPaths
, *p
, *next
;
158 char realDriverName
[200];
161 /* Attempt to make sure libGL symbols will be visible to the driver */
162 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
165 if (geteuid() == getuid()) {
166 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
167 libPaths
= getenv("LIBGL_DRIVERS_PATH");
169 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
171 if (libPaths
== NULL
)
172 libPaths
= DEFAULT_DRIVER_DIR
;
175 for (p
= libPaths
; *p
; p
= next
) {
176 next
= strchr(p
, ':');
187 snprintf(realDriverName
, sizeof realDriverName
,
188 "%.*s/tls/%s_dri.so", len
, p
, driverName
);
189 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
190 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
193 if (handle
== NULL
) {
194 snprintf(realDriverName
, sizeof realDriverName
,
195 "%.*s/%s_dri.so", len
, p
, driverName
);
196 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
197 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
203 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
207 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
215 _X_HIDDEN
const __DRIextension
**
216 driGetDriverExtensions(void *handle
, const char *driver_name
)
218 const __DRIextension
**extensions
= NULL
;
219 const __DRIextension
**(*get_extensions
)(void);
220 char *get_extensions_name
;
222 if (asprintf(&get_extensions_name
, "%s_%s",
223 __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) != -1) {
224 get_extensions
= dlsym(handle
, get_extensions_name
);
225 if (get_extensions
) {
226 free(get_extensions_name
);
227 return get_extensions();
229 InfoMessageF("driver does not expose %s(): %s\n",
230 get_extensions_name
, dlerror());
231 free(get_extensions_name
);
235 extensions
= dlsym(handle
, __DRI_DRIVER_EXTENSIONS
);
236 if (extensions
== NULL
) {
237 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
245 __driGetMSCRate(__DRIdrawable
*draw
,
246 int32_t * numerator
, int32_t * denominator
,
249 __GLXDRIdrawable
*glxDraw
= loaderPrivate
;
251 return __glxGetMscRate(glxDraw
->psc
, numerator
, denominator
);
254 _X_HIDDEN
const __DRIsystemTimeExtension systemTimeExtension
= {
255 {__DRI_SYSTEM_TIME
, __DRI_SYSTEM_TIME_VERSION
},
260 #define __ATTRIB(attrib, field) \
261 { attrib, offsetof(struct glx_config, field) }
265 unsigned int attrib
, offset
;
267 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
268 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
269 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
270 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
271 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
272 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
273 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
274 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
275 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
276 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
277 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
278 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
279 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
280 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
281 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
282 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
283 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
285 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
286 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentIndex
),
287 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
288 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
289 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
290 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
291 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
292 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
293 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
294 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
296 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
297 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
298 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
299 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
300 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
302 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
304 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
305 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
306 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
,
307 bindToMipmapTexture
),
308 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
309 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
, sRGBCapable
)
313 scalarEqual(struct glx_config
*mode
, unsigned int attrib
, unsigned int value
)
315 unsigned int glxValue
;
318 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
319 if (attribMap
[i
].attrib
== attrib
) {
320 glxValue
= *(unsigned int *) ((char *) mode
+ attribMap
[i
].offset
);
321 return glxValue
== GLX_DONT_CARE
|| glxValue
== value
;
324 return GL_TRUE
; /* Is a non-existing attribute equal to value? */
328 driConfigEqual(const __DRIcoreExtension
*core
,
329 struct glx_config
*config
, const __DRIconfig
*driConfig
)
331 unsigned int attrib
, value
, glxValue
;
335 while (core
->indexConfigAttrib(driConfig
, i
++, &attrib
, &value
)) {
337 case __DRI_ATTRIB_RENDER_TYPE
:
339 if (value
& __DRI_ATTRIB_RGBA_BIT
) {
340 glxValue
|= GLX_RGBA_BIT
;
342 if (value
& __DRI_ATTRIB_COLOR_INDEX_BIT
) {
343 glxValue
|= GLX_COLOR_INDEX_BIT
;
345 if (value
& __DRI_ATTRIB_FLOAT_BIT
) {
346 glxValue
|= GLX_RGBA_FLOAT_BIT_ARB
;
348 if (value
& __DRI_ATTRIB_UNSIGNED_FLOAT_BIT
) {
349 glxValue
|= GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
;
351 if (glxValue
!= config
->renderType
)
355 case __DRI_ATTRIB_CONFIG_CAVEAT
:
356 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
357 glxValue
= GLX_NON_CONFORMANT_CONFIG
;
358 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
359 glxValue
= GLX_SLOW_CONFIG
;
362 if (glxValue
!= config
->visualRating
)
366 case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
:
368 if (value
& __DRI_ATTRIB_TEXTURE_1D_BIT
)
369 glxValue
|= GLX_TEXTURE_1D_BIT_EXT
;
370 if (value
& __DRI_ATTRIB_TEXTURE_2D_BIT
)
371 glxValue
|= GLX_TEXTURE_2D_BIT_EXT
;
372 if (value
& __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
)
373 glxValue
|= GLX_TEXTURE_RECTANGLE_BIT_EXT
;
374 if (config
->bindToTextureTargets
!= GLX_DONT_CARE
&&
375 glxValue
!= config
->bindToTextureTargets
)
380 if (!scalarEqual(config
, attrib
, value
))
388 static struct glx_config
*
389 createDriMode(const __DRIcoreExtension
* core
,
390 struct glx_config
*config
, const __DRIconfig
**driConfigs
)
392 __GLXDRIconfigPrivate
*driConfig
;
395 for (i
= 0; driConfigs
[i
]; i
++) {
396 if (driConfigEqual(core
, config
, driConfigs
[i
]))
400 if (driConfigs
[i
] == NULL
)
403 driConfig
= malloc(sizeof *driConfig
);
404 if (driConfig
== NULL
)
407 driConfig
->base
= *config
;
408 driConfig
->driConfig
= driConfigs
[i
];
410 return &driConfig
->base
;
413 _X_HIDDEN
struct glx_config
*
414 driConvertConfigs(const __DRIcoreExtension
* core
,
415 struct glx_config
*configs
, const __DRIconfig
**driConfigs
)
417 struct glx_config head
, *tail
, *m
;
421 for (m
= configs
; m
; m
= m
->next
) {
422 tail
->next
= createDriMode(core
, m
, driConfigs
);
423 if (tail
->next
== NULL
) {
424 /* no matching dri config for m */
436 driDestroyConfigs(const __DRIconfig
**configs
)
440 for (i
= 0; configs
[i
]; i
++)
441 free((__DRIconfig
*) configs
[i
]);
445 _X_HIDDEN __GLXDRIdrawable
*
446 driFetchDrawable(struct glx_context
*gc
, GLXDrawable glxDrawable
)
448 struct glx_display
*const priv
= __glXInitialize(gc
->psc
->dpy
);
449 __GLXDRIdrawable
*pdraw
;
450 struct glx_screen
*psc
;
455 psc
= priv
->screens
[gc
->screen
];
456 if (priv
->drawHash
== NULL
)
459 if (__glxHashLookup(priv
->drawHash
, glxDrawable
, (void *) &pdraw
) == 0) {
464 pdraw
= psc
->driScreen
->createDrawable(psc
, glxDrawable
,
465 glxDrawable
, gc
->config
);
468 ErrorMessageF("failed to create drawable\n");
472 if (__glxHashInsert(priv
->drawHash
, glxDrawable
, pdraw
)) {
473 (*pdraw
->destroyDrawable
) (pdraw
);
482 driReleaseDrawables(struct glx_context
*gc
)
484 const struct glx_display
*priv
= gc
->psc
->display
;
485 __GLXDRIdrawable
*pdraw
;
490 if (__glxHashLookup(priv
->drawHash
,
491 gc
->currentDrawable
, (void *) &pdraw
) == 0) {
492 if (pdraw
->drawable
== pdraw
->xDrawable
) {
494 if (pdraw
->refcount
== 0) {
495 (*pdraw
->destroyDrawable
)(pdraw
);
496 __glxHashDelete(priv
->drawHash
, gc
->currentDrawable
);
501 if (__glxHashLookup(priv
->drawHash
,
502 gc
->currentReadable
, (void *) &pdraw
) == 0) {
503 if (pdraw
->drawable
== pdraw
->xDrawable
) {
505 if (pdraw
->refcount
== 0) {
506 (*pdraw
->destroyDrawable
)(pdraw
);
507 __glxHashDelete(priv
->drawHash
, gc
->currentReadable
);
512 gc
->currentDrawable
= None
;
513 gc
->currentReadable
= None
;
518 dri2_convert_glx_attribs(unsigned num_attribs
, const uint32_t *attribs
,
519 unsigned *major_ver
, unsigned *minor_ver
,
520 uint32_t *render_type
, uint32_t *flags
, unsigned *api
,
521 int *reset
, unsigned *error
)
524 bool got_profile
= false;
529 *render_type
= GLX_RGBA_TYPE
;
530 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
532 *api
= __DRI_API_OPENGL
;
534 if (num_attribs
== 0) {
538 /* This is actually an internal error, but what the heck.
540 if (attribs
== NULL
) {
541 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
545 for (i
= 0; i
< num_attribs
; i
++) {
546 switch (attribs
[i
* 2]) {
547 case GLX_CONTEXT_MAJOR_VERSION_ARB
:
548 *major_ver
= attribs
[i
* 2 + 1];
550 case GLX_CONTEXT_MINOR_VERSION_ARB
:
551 *minor_ver
= attribs
[i
* 2 + 1];
553 case GLX_CONTEXT_FLAGS_ARB
:
554 *flags
= attribs
[i
* 2 + 1];
556 case GLX_CONTEXT_PROFILE_MASK_ARB
:
557 profile
= attribs
[i
* 2 + 1];
560 case GLX_RENDER_TYPE
:
561 *render_type
= attribs
[i
* 2 + 1];
563 case GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB
:
564 switch (attribs
[i
* 2 + 1]) {
565 case GLX_NO_RESET_NOTIFICATION_ARB
:
566 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
568 case GLX_LOSE_CONTEXT_ON_RESET_ARB
:
569 *reset
= __DRI_CTX_RESET_LOSE_CONTEXT
;
572 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
577 /* If an unknown attribute is received, fail.
579 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
585 if (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
586 *api
= __DRI_API_OPENGL_CORE
;
589 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
590 /* There are no profiles before OpenGL 3.2. The
591 * GLX_ARB_create_context_profile spec says:
593 * "If the requested OpenGL version is less than 3.2,
594 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
595 * of the context is determined solely by the requested version."
597 *api
= (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
598 ? __DRI_API_OPENGL_CORE
: __DRI_API_OPENGL
;
600 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
601 *api
= __DRI_API_OPENGL
;
603 case GLX_CONTEXT_ES2_PROFILE_BIT_EXT
:
604 *api
= __DRI_API_GLES2
;
607 *error
= __DRI_CTX_ERROR_BAD_API
;
612 /* Unknown flag value.
614 if (*flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
615 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)) {
616 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
620 /* There are no forward-compatible contexts before OpenGL 3.0. The
621 * GLX_ARB_create_context spec says:
623 * "Forward-compatible contexts are defined only for OpenGL versions
626 if (*major_ver
< 3 && (*flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
627 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
631 if (*major_ver
>= 3 && *render_type
== GLX_COLOR_INDEX_TYPE
) {
632 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
636 /* The GLX_EXT_create_context_es2_profile spec says:
638 * "... If the version requested is 2.0, and the
639 * GLX_CONTEXT_ES2_PROFILE_BIT_EXT bit is set in the
640 * GLX_CONTEXT_PROFILE_MASK_ARB attribute (see below), then the context
641 * returned will implement OpenGL ES 2.0. This is the only way in which
642 * an implementation may request an OpenGL ES 2.0 context."
644 if (*api
== __DRI_API_GLES2
&& (*major_ver
!= 2 || *minor_ver
!= 0)) {
645 *error
= __DRI_CTX_ERROR_BAD_API
;
649 *error
= __DRI_CTX_ERROR_SUCCESS
;
653 #endif /* GLX_DIRECT_RENDERING */