2 * (C) Copyright IBM Corporation 2002, 2004
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Utility functions for DRI drivers.
29 * \author Ian Romanick <idr@us.ibm.com>
35 #include "extensions.h"
38 #if !defined( DRI_NEW_INTERFACE_ONLY )
39 #include "xf86dri.h" /* For XF86DRIQueryVersion prototype. */
42 #if defined(USE_X86_ASM)
43 #include "x86/common_x86_asm.h"
46 #if defined(USE_PPC_ASM)
47 #include "ppc/common_ppc_features.h"
51 driParseDebugString( const char * debug
,
52 const struct dri_debug_control
* control
)
58 if ( debug
!= NULL
) {
59 while( control
->string
!= NULL
) {
60 if ( !strcmp( debug
, "all" ) ||
61 strstr( debug
, control
->string
) != NULL
) {
62 flag
|= control
->flag
;
75 * Create the \c GL_RENDERER string for DRI drivers.
77 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
79 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
81 * Using the supplied chip name, driver data, and AGP speed, this function
84 * \param buffer Buffer to hold the \c GL_RENDERER string.
85 * \param hardware_name Name of the hardware.
86 * \param driver_date Driver date.
87 * \param agp_mode AGP mode (speed).
90 * The length of the string stored in \c buffer. This does \b not include
91 * the terminating \c NUL character.
94 driGetRendererString( char * buffer
, const char * hardware_name
,
95 const char * driver_date
, GLuint agp_mode
)
98 const char * cpu
[MAX_INFO
];
104 offset
= sprintf( buffer
, "Mesa DRI %s %s", hardware_name
, driver_date
);
106 /* Append any AGP-specific information.
108 switch ( agp_mode
) {
113 offset
+= sprintf( & buffer
[ offset
], " AGP %ux", agp_mode
);
120 /* Append any CPU-specific information.
123 if ( _mesa_x86_cpu_features
) {
129 cpu
[next
] = (cpu_has_mmxext
) ? "/MMX+" : "/MMX";
133 # ifdef USE_3DNOW_ASM
134 if ( cpu_has_3dnow
) {
135 cpu
[next
] = (cpu_has_3dnowext
) ? "/3DNow!+" : "/3DNow!";
141 cpu
[next
] = (cpu_has_xmm2
) ? "/SSE2" : "/SSE";
146 #elif defined(USE_SPARC_ASM)
151 #elif defined(USE_PPC_ASM)
152 if ( _mesa_ppc_cpu_features
) {
153 cpu
[next
] = (cpu_has_64
) ? " PowerPC 64" : " PowerPC";
159 cpu
[next
] = "/Altivec";
164 if ( ! cpu_has_fpu
) {
165 cpu
[next
] = "/No FPU";
170 for ( i
= 0 ; i
< next
; i
++ ) {
171 const size_t len
= strlen( cpu
[i
] );
173 strncpy( & buffer
[ offset
], cpu
[i
], len
);
183 void driInitExtensions( GLcontext
* ctx
,
184 const char * const extensions_to_enable
[],
185 GLboolean enable_imaging
)
189 if ( enable_imaging
) {
190 _mesa_enable_imaging_extensions( ctx
);
193 for ( i
= 0 ; extensions_to_enable
[i
] != NULL
; i
++ ) {
194 _mesa_enable_extension( ctx
, extensions_to_enable
[i
] );
201 #ifndef DRI_NEW_INTERFACE_ONLY
203 * Utility function used by drivers to test the verions of other components.
206 * All drivers using the new interface should use \c driCheckDriDdxVersions2
207 * instead. This function is implemented using a call that is not available
208 * to drivers using the new interface. Furthermore, the information gained
209 * by this call (the DRI and DDX version information) is already provided to
210 * the driver via the new interface.
213 driCheckDriDdxDrmVersions(__DRIscreenPrivate
*sPriv
,
214 const char * driver_name
,
215 int dri_major
, int dri_minor
,
216 int ddx_major
, int ddx_minor
,
217 int drm_major
, int drm_minor
)
219 static const char format
[] = "%s DRI driver expected %s version %d.%d.x "
220 "but got version %d.%d.%d";
221 int major
, minor
, patch
;
223 /* Check the DRI version */
224 if (XF86DRIQueryVersion(sPriv
->display
, &major
, &minor
, &patch
)) {
225 if (major
!= dri_major
|| minor
< dri_minor
) {
226 __driUtilMessage(format
, driver_name
, "DRI", dri_major
, dri_minor
,
227 major
, minor
, patch
);
232 /* Check that the DDX driver version is compatible */
233 if (sPriv
->ddxMajor
!= ddx_major
|| sPriv
->ddxMinor
< ddx_minor
) {
234 __driUtilMessage(format
, driver_name
, "DDX", ddx_major
, ddx_minor
,
235 sPriv
->ddxMajor
, sPriv
->ddxMinor
, sPriv
->ddxPatch
);
239 /* Check that the DRM driver version is compatible */
240 if (sPriv
->drmMajor
!= drm_major
|| sPriv
->drmMinor
< drm_minor
) {
241 __driUtilMessage(format
, driver_name
, "DRM", drm_major
, drm_minor
,
242 sPriv
->drmMajor
, sPriv
->drmMinor
, sPriv
->drmPatch
);
248 #endif /* DRI_NEW_INTERFACE_ONLY */
251 * Utility function used by drivers to test the verions of other components.
253 * If one of the version requirements is not met, a message is logged using
254 * \c __driUtilMessage.
256 * \param driver_name Name of the driver. Used in error messages.
257 * \param driActual Actual DRI version supplied __driCreateNewScreen.
258 * \param driExpected Minimum DRI version required by the driver.
259 * \param ddxActual Actual DDX version supplied __driCreateNewScreen.
260 * \param ddxExpected Minimum DDX minor and range of DDX major version required by the driver.
261 * \param drmActual Actual DRM version supplied __driCreateNewScreen.
262 * \param drmExpected Minimum DRM version required by the driver.
264 * \returns \c GL_TRUE if all version requirements are met. Otherwise,
265 * \c GL_FALSE is returned.
267 * \sa __driCreateNewScreen, driCheckDriDdxDrmVersions, __driUtilMessage
270 driCheckDriDdxDrmVersions3(const char * driver_name
,
271 const __DRIversion
* driActual
,
272 const __DRIversion
* driExpected
,
273 const __DRIversion
* ddxActual
,
274 const __DRIutilversion2
* ddxExpected
,
275 const __DRIversion
* drmActual
,
276 const __DRIversion
* drmExpected
)
278 static const char format
[] = "%s DRI driver expected %s version %d.%d.x "
279 "but got version %d.%d.%d";
280 static const char format2
[] = "%s DRI driver expected %s version %d-%d.%d.x "
281 "but got version %d.%d.%d";
284 /* Check the DRI version */
285 if ( (driActual
->major
!= driExpected
->major
)
286 || (driActual
->minor
< driExpected
->minor
) ) {
287 __driUtilMessage(format
, driver_name
, "DRI",
288 driExpected
->major
, driExpected
->minor
,
289 driActual
->major
, driActual
->minor
, driActual
->patch
);
293 /* Check that the DDX driver version is compatible */
294 if ( (ddxActual
->major
< ddxExpected
->major_min
)
295 || (ddxActual
->major
> ddxExpected
->major_max
)
296 || (ddxActual
->minor
< ddxExpected
->minor
) ) {
297 __driUtilMessage(format2
, driver_name
, "DDX",
298 ddxExpected
->major_min
, ddxExpected
->major_max
, ddxExpected
->minor
,
299 ddxActual
->major
, ddxActual
->minor
, ddxActual
->patch
);
303 /* Check that the DRM driver version is compatible */
304 if ( (drmActual
->major
!= drmExpected
->major
)
305 || (drmActual
->minor
< drmExpected
->minor
) ) {
306 __driUtilMessage(format
, driver_name
, "DRM",
307 drmExpected
->major
, drmExpected
->minor
,
308 drmActual
->major
, drmActual
->minor
, drmActual
->patch
);
316 driCheckDriDdxDrmVersions2(const char * driver_name
,
317 const __DRIversion
* driActual
,
318 const __DRIversion
* driExpected
,
319 const __DRIversion
* ddxActual
,
320 const __DRIversion
* ddxExpected
,
321 const __DRIversion
* drmActual
,
322 const __DRIversion
* drmExpected
)
324 __DRIutilversion2 ddx_expected
;
325 ddx_expected
.major_min
= ddxExpected
->major
;
326 ddx_expected
.major_max
= ddxExpected
->major
;
327 ddx_expected
.minor
= ddxExpected
->minor
;
328 ddx_expected
.patch
= ddxExpected
->patch
;
329 return driCheckDriDdxDrmVersions3(driver_name
, driActual
,
330 driExpected
, ddxActual
, & ddx_expected
,
331 drmActual
, drmExpected
);
336 GLboolean
driClipRectToFramebuffer( const GLframebuffer
*buffer
,
338 GLsizei
*width
, GLsizei
*height
)
341 if (*x
< buffer
->_Xmin
) {
342 *width
-= (buffer
->_Xmin
- *x
);
347 if (*x
+ *width
> buffer
->_Xmax
)
348 *width
-= (*x
+ *width
- buffer
->_Xmax
- 1);
353 /* bottom clipping */
354 if (*y
< buffer
->_Ymin
) {
355 *height
-= (buffer
->_Ymin
- *y
);
360 if (*y
+ *height
> buffer
->_Ymax
)
361 *height
-= (*y
+ *height
- buffer
->_Ymax
- 1);
372 * Creates a set of \c __GLcontextModes that a driver will expose.
374 * A set of \c __GLcontextModes will be created based on the supplied
375 * parameters. The number of modes processed will be 2 *
376 * \c num_depth_stencil_bits * \c num_db_modes.
378 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
379 * \c db_modes, and \c visType into each \c __GLcontextModes element.
380 * However, the meanings of \c fb_format and \c fb_type require further
381 * explanation. The \c fb_format specifies which color components are in
382 * each pixel and what the default order is. For example, \c GL_RGB specifies
383 * that red, green, blue are available and red is in the "most significant"
384 * position and blue is in the "least significant". The \c fb_type specifies
385 * the bit sizes of each component and the actual ordering. For example, if
386 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
387 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
390 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
391 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
392 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
393 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
394 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
395 * still uses 32-bits.
397 * If in doubt, look at the tables used in the function.
399 * \param ptr_to_modes Pointer to a pointer to a linked list of
400 * \c __GLcontextModes. Upon completion, a pointer to
401 * the next element to be process will be stored here.
402 * If the function fails and returns \c GL_FALSE, this
403 * value will be unmodified, but some elements in the
404 * linked list may be modified.
405 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
406 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
407 * \param fb_type Type of the pixels in the framebuffer. Currently only
408 * \c GL_UNSIGNED_SHORT_5_6_5,
409 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
410 * \c GL_UNSIGNED_INT_8_8_8_8, and
411 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
412 * \param depth_bits Array of depth buffer sizes to be exposed.
413 * \param stencil_bits Array of stencil buffer sizes to be exposed.
414 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
416 * \param db_modes Array of buffer swap modes. If an element has a
417 * value of \c GLX_NONE, then it represents a
418 * single-buffered mode. Other valid values are
419 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
420 * \c GLX_SWAP_UNDEFINED_OML. See the
421 * GLX_OML_swap_method extension spec for more details.
422 * \param num_db_modes Number of entries in \c db_modes.
423 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
424 * \c GLX_DIRECT_COLOR.
427 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
428 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
432 * There is currently no way to support packed RGB modes (i.e., modes with
433 * exactly 3 bytes per pixel) or floating-point modes. This could probably
434 * be done by creating some new, private enums with clever names likes
435 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
436 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
439 driFillInModes( __GLcontextModes
** ptr_to_modes
,
440 GLenum fb_format
, GLenum fb_type
,
441 const u_int8_t
* depth_bits
, const u_int8_t
* stencil_bits
,
442 unsigned num_depth_stencil_bits
,
443 const GLenum
* db_modes
, unsigned num_db_modes
,
446 static const u_int8_t bits_table
[3][4] = {
448 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
449 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
450 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
453 /* The following arrays are all indexed by the fb_type masked with 0x07.
454 * Given the four supported fb_type values, this results in valid array
455 * indices of 3, 4, 5, and 7.
457 static const u_int32_t masks_table_rgb
[8][4] = {
458 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
459 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
460 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
461 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
462 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
463 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000 }, /* 8_8_8_8 */
464 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
465 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 } /* 8_8_8_8_REV */
468 static const u_int32_t masks_table_rgba
[8][4] = {
469 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
470 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
471 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
472 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
473 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
474 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF }, /* 8_8_8_8 */
475 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
476 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 }, /* 8_8_8_8_REV */
479 static const u_int32_t masks_table_bgr
[8][4] = {
480 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
481 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
482 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
483 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
484 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
485 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 }, /* 8_8_8_8 */
486 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
487 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }, /* 8_8_8_8_REV */
490 static const u_int32_t masks_table_bgra
[8][4] = {
491 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
492 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
493 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
494 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
495 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
496 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF }, /* 8_8_8_8 */
497 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
498 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }, /* 8_8_8_8_REV */
501 static const u_int8_t bytes_per_pixel
[8] = {
502 0, 0, 0, 2, 2, 4, 0, 4
505 const u_int8_t
* bits
;
506 const u_int32_t
* masks
;
507 const int index
= fb_type
& 0x07;
508 __GLcontextModes
* modes
= *ptr_to_modes
;
514 if ( bytes_per_pixel
[ index
] == 0 ) {
515 fprintf( stderr
, "[%s:%u] Framebuffer type 0x%04x has 0 bytes per pixel.\n",
516 __FUNCTION__
, __LINE__
, fb_type
);
521 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
524 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
527 switch ( fb_format
) {
529 bits
= (bytes_per_pixel
[ index
] == 2)
530 ? bits_table
[0] : bits_table
[1];
531 masks
= masks_table_rgb
[ index
];
535 bits
= (bytes_per_pixel
[ index
] == 2)
536 ? bits_table
[0] : bits_table
[2];
537 masks
= masks_table_rgba
[ index
];
541 bits
= (bytes_per_pixel
[ index
] == 2)
542 ? bits_table
[0] : bits_table
[1];
543 masks
= masks_table_bgr
[ index
];
547 bits
= (bytes_per_pixel
[ index
] == 2)
548 ? bits_table
[0] : bits_table
[2];
549 masks
= masks_table_bgra
[ index
];
553 fprintf( stderr
, "[%s:%u] Framebuffer format 0x%04x is not GL_RGB, GL_RGBA, GL_BGR, or GL_BGRA.\n",
554 __FUNCTION__
, __LINE__
, fb_format
);
559 for ( k
= 0 ; k
< num_depth_stencil_bits
; k
++ ) {
560 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
561 for ( j
= 0 ; j
< 2 ; j
++ ) {
563 modes
->redBits
= bits
[0];
564 modes
->greenBits
= bits
[1];
565 modes
->blueBits
= bits
[2];
566 modes
->alphaBits
= bits
[3];
567 modes
->redMask
= masks
[0];
568 modes
->greenMask
= masks
[1];
569 modes
->blueMask
= masks
[2];
570 modes
->alphaMask
= masks
[3];
571 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
572 + modes
->blueBits
+ modes
->alphaBits
;
574 modes
->accumRedBits
= 16 * j
;
575 modes
->accumGreenBits
= 16 * j
;
576 modes
->accumBlueBits
= 16 * j
;
577 modes
->accumAlphaBits
= (masks
[3] != 0) ? 16 * j
: 0;
578 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
580 modes
->stencilBits
= stencil_bits
[k
];
581 modes
->depthBits
= depth_bits
[k
];
583 modes
->visualType
= visType
;
584 modes
->renderType
= GLX_RGBA_BIT
;
585 modes
->drawableType
= GLX_WINDOW_BIT
;
586 modes
->rgbMode
= GL_TRUE
;
588 if ( db_modes
[i
] == GLX_NONE
) {
589 modes
->doubleBufferMode
= GL_FALSE
;
592 modes
->doubleBufferMode
= GL_TRUE
;
593 modes
->swapMethod
= db_modes
[i
];
601 *ptr_to_modes
= modes
;