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"
52 * Print informational message to stderr if LIBGL_DEBUG is set to
56 InfoMessageF(const char *f
, ...)
61 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
62 fprintf(stderr
, "libGL: ");
64 vfprintf(stderr
, f
, args
);
70 * Print error message to stderr if LIBGL_DEBUG is set to anything but
71 * "quiet", (do nothing if LIBGL_DEBUG is unset).
74 ErrorMessageF(const char *f
, ...)
79 if ((env
= getenv("LIBGL_DEBUG")) && !strstr(env
, "quiet")) {
80 fprintf(stderr
, "libGL error: ");
82 vfprintf(stderr
, f
, args
);
88 * Print error message unless LIBGL_DEBUG is set to "quiet".
90 * The distinction between CriticalErrorMessageF and ErrorMessageF is
91 * that critcial errors will be printed by default, (even when
92 * LIBGL_DEBUG is unset).
95 CriticalErrorMessageF(const char *f
, ...)
100 if (!(env
= getenv("LIBGL_DEBUG")) || !strstr(env
, "quiet")) {
101 fprintf(stderr
, "libGL error: ");
103 vfprintf(stderr
, f
, args
);
106 if (!env
|| !strstr(env
, "verbose"))
107 fprintf(stderr
, "libGL error: Try again with LIBGL_DEBUG=verbose for more details.\n");
111 #ifndef DEFAULT_DRIVER_DIR
112 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
113 #define DEFAULT_DRIVER_DIR "/usr/local/lib/dri"
117 * Try to \c dlopen the named driver.
119 * This function adds the "_dri.so" suffix to the driver name and searches the
120 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
121 * order to find the driver.
123 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
126 * A handle from \c dlopen, or \c NULL if driver file not found.
129 driOpenDriver(const char *driverName
)
131 void *glhandle
, *handle
;
132 const char *libPaths
, *p
, *next
;
133 char realDriverName
[200];
136 /* Attempt to make sure libGL symbols will be visible to the driver */
137 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
140 if (geteuid() == getuid()) {
141 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
142 libPaths
= getenv("LIBGL_DRIVERS_PATH");
144 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
146 if (libPaths
== NULL
)
147 libPaths
= DEFAULT_DRIVER_DIR
;
150 for (p
= libPaths
; *p
; p
= next
) {
151 next
= strchr(p
, ':');
162 snprintf(realDriverName
, sizeof realDriverName
,
163 "%.*s/tls/%s_dri.so", len
, p
, driverName
);
164 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
165 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
168 if (handle
== NULL
) {
169 snprintf(realDriverName
, sizeof realDriverName
,
170 "%.*s/%s_dri.so", len
, p
, driverName
);
171 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
172 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
178 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
182 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
191 __driGetMSCRate(__DRIdrawable
*draw
,
192 int32_t * numerator
, int32_t * denominator
,
195 __GLXDRIdrawable
*glxDraw
= loaderPrivate
;
197 return __glxGetMscRate(glxDraw
, numerator
, denominator
);
200 _X_HIDDEN
const __DRIsystemTimeExtension systemTimeExtension
= {
201 {__DRI_SYSTEM_TIME
, __DRI_SYSTEM_TIME_VERSION
},
206 #define __ATTRIB(attrib, field) \
207 { attrib, offsetof(struct glx_config, field) }
211 unsigned int attrib
, offset
;
213 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
214 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
215 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
216 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
217 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
218 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
219 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
220 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
221 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
222 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
223 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
224 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
225 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
226 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
227 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
228 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
229 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
231 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
232 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentIndex
),
233 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
234 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
235 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
236 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
237 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
238 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
239 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
240 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
242 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
243 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
244 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
245 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
246 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
248 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
250 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
251 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
252 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
,
253 bindToMipmapTexture
),
254 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
255 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
, sRGBCapable
)
259 scalarEqual(struct glx_config
*mode
, unsigned int attrib
, unsigned int value
)
261 unsigned int glxValue
;
264 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
265 if (attribMap
[i
].attrib
== attrib
) {
266 glxValue
= *(unsigned int *) ((char *) mode
+ attribMap
[i
].offset
);
267 return glxValue
== GLX_DONT_CARE
|| glxValue
== value
;
270 return GL_TRUE
; /* Is a non-existing attribute equal to value? */
274 driConfigEqual(const __DRIcoreExtension
*core
,
275 struct glx_config
*config
, const __DRIconfig
*driConfig
)
277 unsigned int attrib
, value
, glxValue
;
281 while (core
->indexConfigAttrib(driConfig
, i
++, &attrib
, &value
)) {
283 case __DRI_ATTRIB_RENDER_TYPE
:
285 if (value
& __DRI_ATTRIB_RGBA_BIT
) {
286 glxValue
|= GLX_RGBA_BIT
;
288 if (value
& __DRI_ATTRIB_COLOR_INDEX_BIT
) {
289 glxValue
|= GLX_COLOR_INDEX_BIT
;
291 if (value
& __DRI_ATTRIB_FLOAT_BIT
) {
292 glxValue
|= GLX_RGBA_FLOAT_BIT_ARB
;
294 if (value
& __DRI_ATTRIB_UNSIGNED_FLOAT_BIT
) {
295 glxValue
|= GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
;
297 if (glxValue
!= config
->renderType
)
301 case __DRI_ATTRIB_CONFIG_CAVEAT
:
302 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
303 glxValue
= GLX_NON_CONFORMANT_CONFIG
;
304 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
305 glxValue
= GLX_SLOW_CONFIG
;
308 if (glxValue
!= config
->visualRating
)
312 case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
:
314 if (value
& __DRI_ATTRIB_TEXTURE_1D_BIT
)
315 glxValue
|= GLX_TEXTURE_1D_BIT_EXT
;
316 if (value
& __DRI_ATTRIB_TEXTURE_2D_BIT
)
317 glxValue
|= GLX_TEXTURE_2D_BIT_EXT
;
318 if (value
& __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
)
319 glxValue
|= GLX_TEXTURE_RECTANGLE_BIT_EXT
;
320 if (config
->bindToTextureTargets
!= GLX_DONT_CARE
&&
321 glxValue
!= config
->bindToTextureTargets
)
326 if (!scalarEqual(config
, attrib
, value
))
334 static struct glx_config
*
335 createDriMode(const __DRIcoreExtension
* core
,
336 struct glx_config
*config
, const __DRIconfig
**driConfigs
)
338 __GLXDRIconfigPrivate
*driConfig
;
341 for (i
= 0; driConfigs
[i
]; i
++) {
342 if (driConfigEqual(core
, config
, driConfigs
[i
]))
346 if (driConfigs
[i
] == NULL
)
349 driConfig
= malloc(sizeof *driConfig
);
350 if (driConfig
== NULL
)
353 driConfig
->base
= *config
;
354 driConfig
->driConfig
= driConfigs
[i
];
356 return &driConfig
->base
;
359 _X_HIDDEN
struct glx_config
*
360 driConvertConfigs(const __DRIcoreExtension
* core
,
361 struct glx_config
*configs
, const __DRIconfig
**driConfigs
)
363 struct glx_config head
, *tail
, *m
;
367 for (m
= configs
; m
; m
= m
->next
) {
368 tail
->next
= createDriMode(core
, m
, driConfigs
);
369 if (tail
->next
== NULL
) {
370 /* no matching dri config for m */
382 driDestroyConfigs(const __DRIconfig
**configs
)
386 for (i
= 0; configs
[i
]; i
++)
387 free((__DRIconfig
*) configs
[i
]);
391 _X_HIDDEN __GLXDRIdrawable
*
392 driFetchDrawable(struct glx_context
*gc
, GLXDrawable glxDrawable
)
394 struct glx_display
*const priv
= __glXInitialize(gc
->psc
->dpy
);
395 __GLXDRIdrawable
*pdraw
;
396 struct glx_screen
*psc
;
401 psc
= priv
->screens
[gc
->screen
];
402 if (priv
->drawHash
== NULL
)
405 if (__glxHashLookup(priv
->drawHash
, glxDrawable
, (void *) &pdraw
) == 0) {
410 pdraw
= psc
->driScreen
->createDrawable(psc
, glxDrawable
,
411 glxDrawable
, gc
->config
);
414 ErrorMessageF("failed to create drawable\n");
418 if (__glxHashInsert(priv
->drawHash
, glxDrawable
, pdraw
)) {
419 (*pdraw
->destroyDrawable
) (pdraw
);
428 driReleaseDrawables(struct glx_context
*gc
)
430 const struct glx_display
*priv
= gc
->psc
->display
;
431 __GLXDRIdrawable
*pdraw
;
436 if (__glxHashLookup(priv
->drawHash
,
437 gc
->currentDrawable
, (void *) &pdraw
) == 0) {
438 if (pdraw
->drawable
== pdraw
->xDrawable
) {
440 if (pdraw
->refcount
== 0) {
441 (*pdraw
->destroyDrawable
)(pdraw
);
442 __glxHashDelete(priv
->drawHash
, gc
->currentDrawable
);
447 if (__glxHashLookup(priv
->drawHash
,
448 gc
->currentReadable
, (void *) &pdraw
) == 0) {
449 if (pdraw
->drawable
== pdraw
->xDrawable
) {
451 if (pdraw
->refcount
== 0) {
452 (*pdraw
->destroyDrawable
)(pdraw
);
453 __glxHashDelete(priv
->drawHash
, gc
->currentReadable
);
458 gc
->currentDrawable
= None
;
459 gc
->currentReadable
= None
;
464 dri2_convert_glx_attribs(unsigned num_attribs
, const uint32_t *attribs
,
465 unsigned *major_ver
, unsigned *minor_ver
,
466 uint32_t *render_type
, uint32_t *flags
, unsigned *api
,
467 int *reset
, unsigned *error
)
470 bool got_profile
= false;
475 *render_type
= GLX_RGBA_TYPE
;
476 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
478 *api
= __DRI_API_OPENGL
;
480 if (num_attribs
== 0) {
484 /* This is actually an internal error, but what the heck.
486 if (attribs
== NULL
) {
487 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
491 for (i
= 0; i
< num_attribs
; i
++) {
492 switch (attribs
[i
* 2]) {
493 case GLX_CONTEXT_MAJOR_VERSION_ARB
:
494 *major_ver
= attribs
[i
* 2 + 1];
496 case GLX_CONTEXT_MINOR_VERSION_ARB
:
497 *minor_ver
= attribs
[i
* 2 + 1];
499 case GLX_CONTEXT_FLAGS_ARB
:
500 *flags
= attribs
[i
* 2 + 1];
502 case GLX_CONTEXT_PROFILE_MASK_ARB
:
503 profile
= attribs
[i
* 2 + 1];
506 case GLX_RENDER_TYPE
:
507 *render_type
= attribs
[i
* 2 + 1];
509 case GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB
:
510 switch (attribs
[i
* 2 + 1]) {
511 case GLX_NO_RESET_NOTIFICATION_ARB
:
512 *reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
514 case GLX_LOSE_CONTEXT_ON_RESET_ARB
:
515 *reset
= __DRI_CTX_RESET_LOSE_CONTEXT
;
518 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
523 /* If an unknown attribute is received, fail.
525 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
531 if (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
532 *api
= __DRI_API_OPENGL_CORE
;
535 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
536 /* There are no profiles before OpenGL 3.2. The
537 * GLX_ARB_create_context_profile spec says:
539 * "If the requested OpenGL version is less than 3.2,
540 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
541 * of the context is determined solely by the requested version."
543 *api
= (*major_ver
> 3 || (*major_ver
== 3 && *minor_ver
>= 2))
544 ? __DRI_API_OPENGL_CORE
: __DRI_API_OPENGL
;
546 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
547 *api
= __DRI_API_OPENGL
;
549 case GLX_CONTEXT_ES2_PROFILE_BIT_EXT
:
550 *api
= __DRI_API_GLES2
;
553 *error
= __DRI_CTX_ERROR_BAD_API
;
558 /* Unknown flag value.
560 if (*flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
561 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)) {
562 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
566 /* There are no forward-compatible contexts before OpenGL 3.0. The
567 * GLX_ARB_create_context spec says:
569 * "Forward-compatible contexts are defined only for OpenGL versions
572 if (*major_ver
< 3 && (*flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
573 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
577 if (*major_ver
>= 3 && *render_type
== GLX_COLOR_INDEX_TYPE
) {
578 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
582 /* The GLX_EXT_create_context_es2_profile spec says:
584 * "... If the version requested is 2.0, and the
585 * GLX_CONTEXT_ES2_PROFILE_BIT_EXT bit is set in the
586 * GLX_CONTEXT_PROFILE_MASK_ARB attribute (see below), then the context
587 * returned will implement OpenGL ES 2.0. This is the only way in which
588 * an implementation may request an OpenGL ES 2.0 context."
590 if (*api
== __DRI_API_GLES2
&& (*major_ver
!= 2 || *minor_ver
!= 0)) {
591 *error
= __DRI_CTX_ERROR_BAD_API
;
595 *error
= __DRI_CTX_ERROR_SUCCESS
;
599 #endif /* GLX_DIRECT_RENDERING */