81429c167e36b9f167edd151bae88e6f349e69aa
[mesa.git] / src / mesa / drivers / dri / common / utils.c
1 /*
2 * (c) Copyright IBM Corporation 2002
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(USE_X86_ASM)
36 #include "x86/common_x86_asm.h"
37 #endif
38
39 unsigned
40 driParseDebugString( const char * debug,
41 const struct dri_debug_control * control )
42 {
43 unsigned flag;
44
45
46 flag = 0;
47 if ( debug != NULL ) {
48 while( control->string != NULL ) {
49 if ( strstr( debug, control->string ) != NULL ) {
50 flag |= control->flag;
51 }
52
53 control++;
54 }
55 }
56
57 return flag;
58 }
59
60
61
62
63 unsigned
64 driGetRendererString( char * buffer, const char * hardware_name,
65 const char * driver_date, GLuint agp_mode )
66 {
67 #ifdef USE_X86_ASM
68 char * x86_str = "";
69 char * mmx_str = "";
70 char * tdnow_str = "";
71 char * sse_str = "";
72 #endif
73 unsigned offset;
74
75
76 offset = sprintf( buffer, "Mesa DRI %s %s", hardware_name, driver_date );
77
78 /* Append any AGP-specific information.
79 */
80 switch ( agp_mode ) {
81 case 1:
82 case 2:
83 case 4:
84 case 8:
85 offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
86 break;
87
88 default:
89 break;
90 }
91
92 /* Append any CPU-specific information.
93 */
94 #ifdef USE_X86_ASM
95 if ( _mesa_x86_cpu_features ) {
96 x86_str = " x86";
97 }
98 # ifdef USE_MMX_ASM
99 if ( cpu_has_mmx ) {
100 mmx_str = (cpu_has_mmxext) ? "/MMX+" : "/MMX";
101 }
102 # endif
103 # ifdef USE_3DNOW_ASM
104 if ( cpu_has_3dnow ) {
105 tdnow_str = (cpu_has_3dnowext) ? "/3DNow!+" : "/3DNow!";
106 }
107 # endif
108 # ifdef USE_SSE_ASM
109 if ( cpu_has_xmm ) {
110 sse_str = (cpu_has_xmm2) ? "/SSE2" : "/SSE";
111 }
112 # endif
113
114 offset += sprintf( & buffer[ offset ], "%s%s%s%s",
115 x86_str, mmx_str, tdnow_str, sse_str );
116
117 #elif defined(USE_SPARC_ASM)
118
119 offset += sprintf( & buffer[ offset ], " Sparc" );
120
121 #endif
122
123 return offset;
124 }
125
126
127
128
129 void driInitExtensions( GLcontext * ctx,
130 const char * const extensions_to_enable[],
131 GLboolean enable_imaging )
132 {
133 unsigned i;
134
135 if ( enable_imaging ) {
136 _mesa_enable_imaging_extensions( ctx );
137 }
138
139 for ( i = 0 ; extensions_to_enable[i] != NULL ; i++ ) {
140 _mesa_enable_extension( ctx, extensions_to_enable[i] );
141 }
142 }
143
144
145
146
147 GLboolean
148 driCheckDriDdxDrmVersions(__DRIscreenPrivate *sPriv,
149 const char * driver_name,
150 int dri_major, int dri_minor,
151 int ddx_major, int ddx_minor,
152 int drm_major, int drm_minor)
153 {
154 static const char format[] = "%s DRI driver expected %s version %d.%d.x "
155 "but got version %d.%d.%d";
156 int major, minor, patch;
157
158 #ifndef _SOLO
159 /* Check the DRI version */
160 if (XF86DRIQueryVersion(sPriv->display, &major, &minor, &patch)) {
161 if (major != dri_major || minor < dri_minor) {
162 __driUtilMessage(format, "DRI", driver_name, dri_major, dri_minor,
163 major, minor, patch);
164 return GL_FALSE;
165 }
166 }
167
168 /* Check that the DDX driver version is compatible */
169 if (sPriv->ddxMajor != ddx_major || sPriv->ddxMinor < ddx_minor) {
170 __driUtilMessage(format, "DDX", driver_name, ddx_major, ddx_minor,
171 sPriv->ddxMajor, sPriv->ddxMinor, sPriv->ddxPatch);
172 return GL_FALSE;
173 }
174 #else
175 (void)major;(void)minor;(void)patch;
176 #endif
177
178 /* Check that the DRM driver version is compatible */
179 if (sPriv->drmMajor != drm_major || sPriv->drmMinor < drm_minor) {
180 __driUtilMessage(format, "DRM", driver_name, drm_major, drm_minor,
181 sPriv->drmMajor, sPriv->drmMinor, sPriv->drmPatch);
182 return GL_FALSE;
183 }
184
185 return GL_TRUE;
186 }
187
188 GLboolean driClipRectToFramebuffer( const GLframebuffer *buffer,
189 GLint *x, GLint *y,
190 GLsizei *width, GLsizei *height )
191 {
192 /* left clipping */
193 if (*x < buffer->_Xmin) {
194 *width -= (buffer->_Xmin - *x);
195 *x = buffer->_Xmin;
196 }
197
198 /* right clipping */
199 if (*x + *width > buffer->_Xmax)
200 *width -= (*x + *width - buffer->_Xmax - 1);
201
202 if (*width <= 0)
203 return GL_FALSE;
204
205 /* bottom clipping */
206 if (*y < buffer->_Ymin) {
207 *height -= (buffer->_Ymin - *y);
208 *y = buffer->_Ymin;
209 }
210
211 /* top clipping */
212 if (*y + *height > buffer->_Ymax)
213 *height -= (*y + *height - buffer->_Ymax - 1);
214
215 if (*height <= 0)
216 return GL_FALSE;
217
218 return GL_TRUE;
219 }