Patch removes _SOLO definition needed for mesa-solo. mesa-solo
[mesa.git] / src / mesa / drivers / dri / common / utils.c
1 /*
2 * (C) Copyright IBM Corporation 2002, 2004
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 *
24 * Authors:
25 * Ian Romanick <idr@us.ibm.com>
26 */
27 /* $XFree86:$ */
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include "mtypes.h"
32 #include "extensions.h"
33 #include "utils.h"
34
35 #if !defined( DRI_NEW_INTERFACE_ONLY )
36 #include "xf86dri.h" /* For XF86DRIQueryVersion prototype. */
37 #endif
38
39 #if defined(USE_X86_ASM)
40 #include "x86/common_x86_asm.h"
41 #endif
42
43 unsigned
44 driParseDebugString( const char * debug,
45 const struct dri_debug_control * control )
46 {
47 unsigned flag;
48
49
50 flag = 0;
51 if ( debug != NULL ) {
52 while( control->string != NULL ) {
53 if ( !strcmp( debug, "all" ) ||
54 strstr( debug, control->string ) != NULL ) {
55 flag |= control->flag;
56 }
57
58 control++;
59 }
60 }
61
62 return flag;
63 }
64
65
66
67
68 unsigned
69 driGetRendererString( char * buffer, const char * hardware_name,
70 const char * driver_date, GLuint agp_mode )
71 {
72 #ifdef USE_X86_ASM
73 char * x86_str = "";
74 char * mmx_str = "";
75 char * tdnow_str = "";
76 char * sse_str = "";
77 #endif
78 unsigned offset;
79
80
81 offset = sprintf( buffer, "Mesa DRI %s %s", hardware_name, driver_date );
82
83 /* Append any AGP-specific information.
84 */
85 switch ( agp_mode ) {
86 case 1:
87 case 2:
88 case 4:
89 case 8:
90 offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
91 break;
92
93 default:
94 break;
95 }
96
97 /* Append any CPU-specific information.
98 */
99 #ifdef USE_X86_ASM
100 if ( _mesa_x86_cpu_features ) {
101 x86_str = " x86";
102 }
103 # ifdef USE_MMX_ASM
104 if ( cpu_has_mmx ) {
105 mmx_str = (cpu_has_mmxext) ? "/MMX+" : "/MMX";
106 }
107 # endif
108 # ifdef USE_3DNOW_ASM
109 if ( cpu_has_3dnow ) {
110 tdnow_str = (cpu_has_3dnowext) ? "/3DNow!+" : "/3DNow!";
111 }
112 # endif
113 # ifdef USE_SSE_ASM
114 if ( cpu_has_xmm ) {
115 sse_str = (cpu_has_xmm2) ? "/SSE2" : "/SSE";
116 }
117 # endif
118
119 offset += sprintf( & buffer[ offset ], "%s%s%s%s",
120 x86_str, mmx_str, tdnow_str, sse_str );
121
122 #elif defined(USE_SPARC_ASM)
123
124 offset += sprintf( & buffer[ offset ], " Sparc" );
125
126 #endif
127
128 return offset;
129 }
130
131
132
133
134 void driInitExtensions( GLcontext * ctx,
135 const char * const extensions_to_enable[],
136 GLboolean enable_imaging )
137 {
138 unsigned i;
139
140 if ( enable_imaging ) {
141 _mesa_enable_imaging_extensions( ctx );
142 }
143
144 for ( i = 0 ; extensions_to_enable[i] != NULL ; i++ ) {
145 _mesa_enable_extension( ctx, extensions_to_enable[i] );
146 }
147 }
148
149
150
151
152 #ifndef DRI_NEW_INTERFACE_ONLY
153 /**
154 * Utility function used by drivers to test the verions of other components.
155 *
156 * \deprecated
157 * All drivers using the new interface should use \c driCheckDriDdxVersions2
158 * instead. This function is implemented using a call that is not available
159 * to drivers using the new interface. Furthermore, the information gained
160 * by this call (the DRI and DDX version information) is already provided to
161 * the driver via the new interface.
162 */
163 GLboolean
164 driCheckDriDdxDrmVersions(__DRIscreenPrivate *sPriv,
165 const char * driver_name,
166 int dri_major, int dri_minor,
167 int ddx_major, int ddx_minor,
168 int drm_major, int drm_minor)
169 {
170 static const char format[] = "%s DRI driver expected %s version %d.%d.x "
171 "but got version %d.%d.%d";
172 int major, minor, patch;
173
174 /* Check the DRI version */
175 if (XF86DRIQueryVersion(sPriv->display, &major, &minor, &patch)) {
176 if (major != dri_major || minor < dri_minor) {
177 __driUtilMessage(format, driver_name, "DRI", dri_major, dri_minor,
178 major, minor, patch);
179 return GL_FALSE;
180 }
181 }
182
183 /* Check that the DDX driver version is compatible */
184 if (sPriv->ddxMajor != ddx_major || sPriv->ddxMinor < ddx_minor) {
185 __driUtilMessage(format, driver_name, "DDX", ddx_major, ddx_minor,
186 sPriv->ddxMajor, sPriv->ddxMinor, sPriv->ddxPatch);
187 return GL_FALSE;
188 }
189
190 /* Check that the DRM driver version is compatible */
191 if (sPriv->drmMajor != drm_major || sPriv->drmMinor < drm_minor) {
192 __driUtilMessage(format, driver_name, "DRM", drm_major, drm_minor,
193 sPriv->drmMajor, sPriv->drmMinor, sPriv->drmPatch);
194 return GL_FALSE;
195 }
196
197 return GL_TRUE;
198 }
199 #endif /* DRI_NEW_INTERFACE_ONLY */
200
201 /**
202 * Utility function used by drivers to test the verions of other components.
203 *
204 * If one of the version requirements is not met, a message is logged using
205 * \c __driUtilMessage.
206 *
207 * \param driver_name Name of the driver. Used in error messages.
208 * \param driActual Actual DRI version supplied __driCreateNewScreen.
209 * \param driExpected Minimum DRI version required by the driver.
210 * \param ddxActual Actual DDX version supplied __driCreateNewScreen.
211 * \param ddxExpected Minimum DDX version required by the driver.
212 * \param drmActual Actual DRM version supplied __driCreateNewScreen.
213 * \param drmExpected Minimum DRM version required by the driver.
214 *
215 * \returns \c GL_TRUE if all version requirements are met. Otherwise,
216 * \c GL_FALSE is returned.
217 *
218 * \sa __driCreateNewScreen, driCheckDriDdxDrmVersions, __driUtilMessage
219 */
220 GLboolean
221 driCheckDriDdxDrmVersions2(const char * driver_name,
222 const __DRIversion * driActual,
223 const __DRIversion * driExpected,
224 const __DRIversion * ddxActual,
225 const __DRIversion * ddxExpected,
226 const __DRIversion * drmActual,
227 const __DRIversion * drmExpected)
228 {
229 static const char format[] = "%s DRI driver expected %s version %d.%d.x "
230 "but got version %d.%d.%d";
231
232
233 /* Check the DRI version */
234 if ( (driActual->major != driExpected->major)
235 || (driActual->minor < driExpected->minor) ) {
236 __driUtilMessage(format, driver_name, "DRI",
237 driExpected->major, driExpected->minor,
238 driActual->major, driActual->minor, driActual->patch);
239 return GL_FALSE;
240 }
241
242 /* Check that the DDX driver version is compatible */
243 if ( (ddxActual->major != ddxExpected->major)
244 || (ddxActual->minor < ddxExpected->minor) ) {
245 __driUtilMessage(format, driver_name, "DDX",
246 ddxExpected->major, ddxExpected->minor,
247 ddxActual->major, ddxActual->minor, ddxActual->patch);
248 return GL_FALSE;
249 }
250
251 /* Check that the DRM driver version is compatible */
252 if ( (drmActual->major != drmExpected->major)
253 || (drmActual->minor < drmExpected->minor) ) {
254 __driUtilMessage(format, driver_name, "DRM",
255 drmExpected->major, drmExpected->minor,
256 drmActual->major, drmActual->minor, drmActual->patch);
257 return GL_FALSE;
258 }
259
260 return GL_TRUE;
261 }
262
263
264 GLboolean driClipRectToFramebuffer( const GLframebuffer *buffer,
265 GLint *x, GLint *y,
266 GLsizei *width, GLsizei *height )
267 {
268 /* left clipping */
269 if (*x < buffer->_Xmin) {
270 *width -= (buffer->_Xmin - *x);
271 *x = buffer->_Xmin;
272 }
273
274 /* right clipping */
275 if (*x + *width > buffer->_Xmax)
276 *width -= (*x + *width - buffer->_Xmax - 1);
277
278 if (*width <= 0)
279 return GL_FALSE;
280
281 /* bottom clipping */
282 if (*y < buffer->_Ymin) {
283 *height -= (buffer->_Ymin - *y);
284 *y = buffer->_Ymin;
285 }
286
287 /* top clipping */
288 if (*y + *height > buffer->_Ymax)
289 *height -= (*y + *height - buffer->_Ymax - 1);
290
291 if (*height <= 0)
292 return GL_FALSE;
293
294 return GL_TRUE;
295 }
296
297
298
299 /**
300 * Creates a set of \c __GLcontextModes that a driver will expose.
301 *
302 * A set of \c __GLcontextModes will be created based on the supplied
303 * parameters. The number of modes processed will be 2 *
304 * \c num_depth_stencil_bits * \c num_db_modes.
305 *
306 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
307 * \c db_modes, and \c visType into each \c __GLcontextModes element.
308 * However, the meanings of \c fb_format and \c fb_type require further
309 * explanation. The \c fb_format specifies which color components are in
310 * each pixel and what the default order is. For example, \c GL_RGB specifies
311 * that red, green, blue are available and red is in the "most significant"
312 * position and blue is in the "least significant". The \c fb_type specifies
313 * the bit sizes of each component and the actual ordering. For example, if
314 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
315 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
316 * the red value.
317 *
318 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
319 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
320 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
321 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
322 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
323 * still uses 32-bits.
324 *
325 * If in doubt, look at the tables used in the function.
326 *
327 * \param ptr_to_modes Pointer to a pointer to a linked list of
328 * \c __GLcontextModes. Upon completion, a pointer to
329 * the next element to be process will be stored here.
330 * If the function fails and returns \c GL_FALSE, this
331 * value will be unmodified, but some elements in the
332 * linked list may be modified.
333 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
334 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
335 * \param fb_type Type of the pixels in the framebuffer. Currently only
336 * \c GL_UNSIGNED_SHORT_5_6_5,
337 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
338 * \c GL_UNSIGNED_INT_8_8_8_8, and
339 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
340 * \param depth_bits Array of depth buffer sizes to be exposed.
341 * \param stencil_bits Array of stencil buffer sizes to be exposed.
342 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
343 * \c stencil_bits.
344 * \param db_modes Array of buffer swap modes. If an element has a
345 * value of \c GLX_NONE, then it represents a
346 * single-buffered mode. Other valid values are
347 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
348 * \c GLX_SWAP_UNDEFINED_OML. See the
349 * GLX_OML_swap_method extension spec for more details.
350 * \param num_db_modes Number of entries in \c db_modes.
351 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
352 * \c GLX_DIRECT_COLOR.
353 *
354 * \returns
355 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
356 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
357 * \c fb_type).
358 *
359 * \todo
360 * There is currently no way to support packed RGB modes (i.e., modes with
361 * exactly 3 bytes per pixel) or floating-point modes. This could probably
362 * be done by creating some new, private enums with clever names likes
363 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
364 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
365 */
366 GLboolean
367 driFillInModes( __GLcontextModes ** ptr_to_modes,
368 GLenum fb_format, GLenum fb_type,
369 const uint8_t * depth_bits, const uint8_t * stencil_bits,
370 unsigned num_depth_stencil_bits,
371 const GLenum * db_modes, unsigned num_db_modes,
372 int visType )
373 {
374 static const uint8_t bits_table[3][4] = {
375 /* R G B A */
376 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
377 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
378 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
379 };
380
381 /* The following arrays are all indexed by the fb_type masked with 0x07.
382 * Given the four supported fb_type values, this results in valid array
383 * indices of 3, 4, 5, and 7.
384 */
385 static const uint32_t masks_table_rgb[8][4] = {
386 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
387 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
388 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
389 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
390 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
391 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000 }, /* 8_8_8_8 */
392 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
393 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 } /* 8_8_8_8_REV */
394 };
395
396 static const uint32_t masks_table_rgba[8][4] = {
397 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
398 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
399 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
400 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
401 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
402 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF }, /* 8_8_8_8 */
403 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
404 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 }, /* 8_8_8_8_REV */
405 };
406
407 static const uint32_t masks_table_bgr[8][4] = {
408 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
409 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
410 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
411 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
412 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
413 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 }, /* 8_8_8_8 */
414 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
415 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }, /* 8_8_8_8_REV */
416 };
417
418 static const uint32_t masks_table_bgra[8][4] = {
419 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
420 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
421 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
422 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
423 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
424 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF }, /* 8_8_8_8 */
425 { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
426 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }, /* 8_8_8_8_REV */
427 };
428
429 static const uint8_t bytes_per_pixel[8] = {
430 0, 0, 0, 2, 2, 4, 0, 4
431 };
432
433 const uint8_t * bits;
434 const uint32_t * masks;
435 const int index = fb_type & 0x07;
436 __GLcontextModes * modes = *ptr_to_modes;
437 unsigned i;
438 unsigned j;
439 unsigned k;
440
441
442 if ( bytes_per_pixel[ index ] == 0 ) {
443 fprintf( stderr, "[%s:%u] Framebuffer type 0x%04x has 0 bytes per pixel.\n",
444 __func__, __LINE__, fb_type );
445 return GL_FALSE;
446 }
447
448
449 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
450 * the _REV versions.
451 *
452 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
453 */
454
455 switch ( fb_format ) {
456 case GL_RGB:
457 bits = (bytes_per_pixel[ index ] == 2)
458 ? bits_table[0] : bits_table[1];
459 masks = masks_table_rgb[ index ];
460 break;
461
462 case GL_RGBA:
463 bits = (bytes_per_pixel[ index ] == 2)
464 ? bits_table[0] : bits_table[2];
465 masks = masks_table_rgba[ index ];
466 break;
467
468 case GL_BGR:
469 bits = (bytes_per_pixel[ index ] == 2)
470 ? bits_table[0] : bits_table[1];
471 masks = masks_table_bgr[ index ];
472 break;
473
474 case GL_BGRA:
475 bits = (bytes_per_pixel[ index ] == 2)
476 ? bits_table[0] : bits_table[2];
477 masks = masks_table_bgra[ index ];
478 break;
479
480 default:
481 fprintf( stderr, "[%s:%u] Framebuffer format 0x%04x is not GL_RGB, GL_RGBA, GL_BGR, or GL_BGRA.\n",
482 __func__, __LINE__, fb_format );
483 return GL_FALSE;
484 }
485
486
487 for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
488 for ( i = 0 ; i < num_db_modes ; i++ ) {
489 for ( j = 0 ; j < 2 ; j++ ) {
490
491 modes->redBits = bits[0];
492 modes->greenBits = bits[1];
493 modes->blueBits = bits[2];
494 modes->alphaBits = bits[3];
495 modes->redMask = masks[0];
496 modes->greenMask = masks[1];
497 modes->blueMask = masks[2];
498 modes->alphaMask = masks[3];
499 modes->rgbBits = modes->redBits + modes->greenBits
500 + modes->blueBits + modes->alphaBits;
501
502 modes->accumRedBits = 16 * j;
503 modes->accumGreenBits = 16 * j;
504 modes->accumBlueBits = 16 * j;
505 modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
506 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
507
508 modes->stencilBits = stencil_bits[k];
509 modes->depthBits = depth_bits[k];
510
511 modes->visualType = visType;
512 modes->renderType = GLX_RGBA_BIT;
513 modes->drawableType = GLX_WINDOW_BIT;
514 modes->rgbMode = GL_TRUE;
515
516 if ( db_modes[i] == GLX_NONE ) {
517 modes->doubleBufferMode = GL_FALSE;
518 }
519 else {
520 modes->doubleBufferMode = GL_TRUE;
521 modes->swapMethod = db_modes[i];
522 }
523
524 modes = modes->next;
525 }
526 }
527 }
528
529 *ptr_to_modes = modes;
530 return GL_TRUE;
531 }