442eb6b9ecf54b1ce5c5d095d2813086417aa884
[mesa.git] / src / mesa / drivers / dri / r128 / r128_screen.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r128/r128_screen.c,v 1.9 2003/03/26 20:43:49 tsi Exp $ */
2 /**************************************************************************
3
4 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
5 Cedar Park, Texas.
6 All Rights Reserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 on the rights to use, copy, modify, merge, publish, distribute, sub
12 license, and/or sell copies of the Software, and to permit persons to whom
13 the Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice (including the next
16 paragraph) shall be included in all copies or substantial portions of the
17 Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Gareth Hughes <gareth@valinux.com>
32 * Kevin E. Martin <martin@valinux.com>
33 *
34 */
35
36 #include "r128_dri.h"
37
38 #include "r128_context.h"
39 #include "r128_ioctl.h"
40 #include "r128_span.h"
41 #include "r128_tris.h"
42
43 #include "context.h"
44 #include "imports.h"
45 #include "framebuffer.h"
46 #include "renderbuffer.h"
47
48 #include "utils.h"
49 #include "vblank.h"
50
51 #include "GL/internal/dri_interface.h"
52
53 /* R128 configuration
54 */
55 #include "xmlpool.h"
56
57 PUBLIC const char __driConfigOptions[] =
58 DRI_CONF_BEGIN
59 DRI_CONF_SECTION_PERFORMANCE
60 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
61 DRI_CONF_SECTION_END
62 DRI_CONF_SECTION_QUALITY
63 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
64 DRI_CONF_SECTION_END
65 DRI_CONF_SECTION_DEBUG
66 DRI_CONF_NO_RAST(false)
67 #if ENABLE_PERF_BOXES
68 DRI_CONF_PERFORMANCE_BOXES(false)
69 #endif
70 DRI_CONF_SECTION_END
71 DRI_CONF_END;
72 #if ENABLE_PERF_BOXES
73 static const GLuint __driNConfigOptions = 4;
74 #else
75 static const GLuint __driNConfigOptions = 3;
76 #endif
77
78 extern const struct dri_extension card_extensions[];
79
80 #if 1
81 /* Including xf86PciInfo.h introduces a bunch of errors...
82 */
83 #define PCI_CHIP_RAGE128LE 0x4C45
84 #define PCI_CHIP_RAGE128LF 0x4C46
85 #define PCI_CHIP_RAGE128PF 0x5046
86 #define PCI_CHIP_RAGE128PR 0x5052
87 #define PCI_CHIP_RAGE128RE 0x5245
88 #define PCI_CHIP_RAGE128RF 0x5246
89 #define PCI_CHIP_RAGE128RK 0x524B
90 #define PCI_CHIP_RAGE128RL 0x524C
91 #endif
92
93
94 /* Create the device specific screen private data struct.
95 */
96 static r128ScreenPtr
97 r128CreateScreen( __DRIscreenPrivate *sPriv )
98 {
99 r128ScreenPtr r128Screen;
100 R128DRIPtr r128DRIPriv = (R128DRIPtr)sPriv->pDevPriv;
101 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
102 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
103 void * const psc = sPriv->psc->screenConfigs;
104
105 if (sPriv->devPrivSize != sizeof(R128DRIRec)) {
106 fprintf(stderr,"\nERROR! sizeof(R128DRIRec) does not match passed size from device driver\n");
107 return GL_FALSE;
108 }
109
110 /* Allocate the private area */
111 r128Screen = (r128ScreenPtr) CALLOC( sizeof(*r128Screen) );
112 if ( !r128Screen ) return NULL;
113
114 /* parse information in __driConfigOptions */
115 driParseOptionInfo (&r128Screen->optionCache,
116 __driConfigOptions, __driNConfigOptions);
117
118 /* This is first since which regions we map depends on whether or
119 * not we are using a PCI card.
120 */
121 r128Screen->IsPCI = r128DRIPriv->IsPCI;
122 r128Screen->sarea_priv_offset = r128DRIPriv->sarea_priv_offset;
123
124 if (sPriv->drmMinor >= 3) {
125 drm_r128_getparam_t gp;
126 int ret;
127
128 gp.param = R128_PARAM_IRQ_NR;
129 gp.value = &r128Screen->irq;
130
131 ret = drmCommandWriteRead( sPriv->fd, DRM_R128_GETPARAM,
132 &gp, sizeof(gp));
133 if (ret) {
134 fprintf(stderr, "drmR128GetParam (R128_PARAM_IRQ_NR): %d\n", ret);
135 FREE( r128Screen );
136 return NULL;
137 }
138 }
139
140 r128Screen->mmio.handle = r128DRIPriv->registerHandle;
141 r128Screen->mmio.size = r128DRIPriv->registerSize;
142 if ( drmMap( sPriv->fd,
143 r128Screen->mmio.handle,
144 r128Screen->mmio.size,
145 (drmAddressPtr)&r128Screen->mmio.map ) ) {
146 FREE( r128Screen );
147 return NULL;
148 }
149
150 r128Screen->buffers = drmMapBufs( sPriv->fd );
151 if ( !r128Screen->buffers ) {
152 drmUnmap( (drmAddress)r128Screen->mmio.map, r128Screen->mmio.size );
153 FREE( r128Screen );
154 return NULL;
155 }
156
157 if ( !r128Screen->IsPCI ) {
158 r128Screen->agpTextures.handle = r128DRIPriv->agpTexHandle;
159 r128Screen->agpTextures.size = r128DRIPriv->agpTexMapSize;
160 if ( drmMap( sPriv->fd,
161 r128Screen->agpTextures.handle,
162 r128Screen->agpTextures.size,
163 (drmAddressPtr)&r128Screen->agpTextures.map ) ) {
164 drmUnmapBufs( r128Screen->buffers );
165 drmUnmap( (drmAddress)r128Screen->mmio.map, r128Screen->mmio.size );
166 FREE( r128Screen );
167 return NULL;
168 }
169 }
170
171 switch ( r128DRIPriv->deviceID ) {
172 case PCI_CHIP_RAGE128RE:
173 case PCI_CHIP_RAGE128RF:
174 case PCI_CHIP_RAGE128RK:
175 case PCI_CHIP_RAGE128RL:
176 r128Screen->chipset = R128_CARD_TYPE_R128;
177 break;
178 case PCI_CHIP_RAGE128PF:
179 r128Screen->chipset = R128_CARD_TYPE_R128_PRO;
180 break;
181 case PCI_CHIP_RAGE128LE:
182 case PCI_CHIP_RAGE128LF:
183 r128Screen->chipset = R128_CARD_TYPE_R128_MOBILITY;
184 break;
185 default:
186 r128Screen->chipset = R128_CARD_TYPE_R128;
187 break;
188 }
189
190 r128Screen->cpp = r128DRIPriv->bpp / 8;
191 r128Screen->AGPMode = r128DRIPriv->AGPMode;
192
193 r128Screen->frontOffset = r128DRIPriv->frontOffset;
194 r128Screen->frontPitch = r128DRIPriv->frontPitch;
195 r128Screen->backOffset = r128DRIPriv->backOffset;
196 r128Screen->backPitch = r128DRIPriv->backPitch;
197 r128Screen->depthOffset = r128DRIPriv->depthOffset;
198 r128Screen->depthPitch = r128DRIPriv->depthPitch;
199 r128Screen->spanOffset = r128DRIPriv->spanOffset;
200
201 r128Screen->texOffset[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureOffset;
202 r128Screen->texSize[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureSize;
203 r128Screen->logTexGranularity[R128_LOCAL_TEX_HEAP] = r128DRIPriv->log2TexGran;
204
205 if ( r128Screen->IsPCI ) {
206 r128Screen->numTexHeaps = R128_NR_TEX_HEAPS - 1;
207 r128Screen->texOffset[R128_AGP_TEX_HEAP] = 0;
208 r128Screen->texSize[R128_AGP_TEX_HEAP] = 0;
209 r128Screen->logTexGranularity[R128_AGP_TEX_HEAP] = 0;
210 } else {
211 r128Screen->numTexHeaps = R128_NR_TEX_HEAPS;
212 r128Screen->texOffset[R128_AGP_TEX_HEAP] =
213 r128DRIPriv->agpTexOffset + R128_AGP_TEX_OFFSET;
214 r128Screen->texSize[R128_AGP_TEX_HEAP] = r128DRIPriv->agpTexMapSize;
215 r128Screen->logTexGranularity[R128_AGP_TEX_HEAP] =
216 r128DRIPriv->log2AGPTexGran;
217 }
218
219 r128Screen->driScreen = sPriv;
220
221 if ( glx_enable_extension != NULL ) {
222 if ( r128Screen->irq != 0 ) {
223 (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
224 (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
225 (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
226 }
227
228 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
229 }
230
231 return r128Screen;
232 }
233
234 /* Destroy the device specific screen private data struct.
235 */
236 static void
237 r128DestroyScreen( __DRIscreenPrivate *sPriv )
238 {
239 r128ScreenPtr r128Screen = (r128ScreenPtr)sPriv->private;
240
241 if ( !r128Screen )
242 return;
243
244 if ( !r128Screen->IsPCI ) {
245 drmUnmap( (drmAddress)r128Screen->agpTextures.map,
246 r128Screen->agpTextures.size );
247 }
248 drmUnmapBufs( r128Screen->buffers );
249 drmUnmap( (drmAddress)r128Screen->mmio.map, r128Screen->mmio.size );
250
251 /* free all option information */
252 driDestroyOptionInfo (&r128Screen->optionCache);
253
254 FREE( r128Screen );
255 sPriv->private = NULL;
256 }
257
258
259 /* Create and initialize the Mesa and driver specific pixmap buffer
260 * data.
261 */
262 static GLboolean
263 r128CreateBuffer( __DRIscreenPrivate *driScrnPriv,
264 __DRIdrawablePrivate *driDrawPriv,
265 const __GLcontextModes *mesaVis,
266 GLboolean isPixmap )
267 {
268 r128ScreenPtr screen = (r128ScreenPtr) driScrnPriv->private;
269
270 if (isPixmap) {
271 return GL_FALSE; /* not implemented */
272 }
273 else {
274 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
275
276 {
277 driRenderbuffer *frontRb
278 = driNewRenderbuffer(GL_RGBA, screen->cpp,
279 screen->frontOffset, screen->frontPitch);
280 r128SetSpanFunctions(frontRb, mesaVis);
281 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
282 }
283
284 if (mesaVis->doubleBufferMode) {
285 driRenderbuffer *backRb
286 = driNewRenderbuffer(GL_RGBA, screen->cpp,
287 screen->backOffset, screen->backPitch);
288 r128SetSpanFunctions(backRb, mesaVis);
289 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
290 }
291
292 if (mesaVis->depthBits == 16) {
293 driRenderbuffer *depthRb
294 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, screen->cpp,
295 screen->depthOffset, screen->depthPitch);
296 r128SetSpanFunctions(depthRb, mesaVis);
297 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
298 }
299 else if (mesaVis->depthBits == 24) {
300 driRenderbuffer *depthRb
301 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, screen->cpp,
302 screen->depthOffset, screen->depthPitch);
303 r128SetSpanFunctions(depthRb, mesaVis);
304 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
305 }
306
307 _mesa_add_soft_renderbuffers(fb,
308 GL_FALSE, /* color */
309 GL_FALSE, /* depth */
310 mesaVis->stencilBits > 0,
311 mesaVis->accumRedBits > 0,
312 GL_FALSE, /* alpha */
313 GL_FALSE /* aux */);
314 driDrawPriv->driverPrivate = (void *) fb;
315
316 return (driDrawPriv->driverPrivate != NULL);
317 }
318 }
319
320
321 static void
322 r128DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
323 {
324 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
325 }
326
327
328 /* Copy the back color buffer to the front color buffer */
329 static void
330 r128SwapBuffers(__DRIdrawablePrivate *dPriv)
331 {
332 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
333 r128ContextPtr rmesa;
334 GLcontext *ctx;
335 rmesa = (r128ContextPtr) dPriv->driContextPriv->driverPrivate;
336 ctx = rmesa->glCtx;
337 if (ctx->Visual.doubleBufferMode) {
338 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
339 if ( rmesa->doPageFlip ) {
340 r128PageFlip( dPriv );
341 }
342 else {
343 r128CopyBuffer( dPriv );
344 }
345 }
346 }
347 else {
348 /* XXX this shouldn't be an error but we can't handle it for now */
349 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
350 }
351 }
352
353
354 /* Initialize the driver specific screen private data.
355 */
356 static GLboolean
357 r128InitDriver( __DRIscreenPrivate *sPriv )
358 {
359 sPriv->private = (void *) r128CreateScreen( sPriv );
360
361 if ( !sPriv->private ) {
362 r128DestroyScreen( sPriv );
363 return GL_FALSE;
364 }
365
366 return GL_TRUE;
367 }
368
369
370 static struct __DriverAPIRec r128API = {
371 .InitDriver = r128InitDriver,
372 .DestroyScreen = r128DestroyScreen,
373 .CreateContext = r128CreateContext,
374 .DestroyContext = r128DestroyContext,
375 .CreateBuffer = r128CreateBuffer,
376 .DestroyBuffer = r128DestroyBuffer,
377 .SwapBuffers = r128SwapBuffers,
378 .MakeCurrent = r128MakeCurrent,
379 .UnbindContext = r128UnbindContext,
380 .GetSwapInfo = NULL,
381 .GetMSC = driGetMSC32,
382 .WaitForMSC = driWaitForMSC32,
383 .WaitForSBC = NULL,
384 .SwapBuffersMSC = NULL
385
386 };
387
388
389 static __GLcontextModes *
390 r128FillInModes( unsigned pixel_bits, unsigned depth_bits,
391 unsigned stencil_bits, GLboolean have_back_buffer )
392 {
393 __GLcontextModes * modes;
394 __GLcontextModes * m;
395 unsigned num_modes;
396 unsigned depth_buffer_factor;
397 unsigned back_buffer_factor;
398 GLenum fb_format;
399 GLenum fb_type;
400
401 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
402 * enough to add support. Basically, if a context is created with an
403 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
404 * will never be used.
405 */
406 static const GLenum back_buffer_modes[] = {
407 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
408 };
409
410 u_int8_t depth_bits_array[2];
411 u_int8_t stencil_bits_array[2];
412
413
414 depth_bits_array[0] = depth_bits;
415 depth_bits_array[1] = depth_bits;
416
417 /* Just like with the accumulation buffer, always provide some modes
418 * with a stencil buffer. It will be a sw fallback, but some apps won't
419 * care about that.
420 */
421 stencil_bits_array[0] = 0;
422 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
423
424 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
425 back_buffer_factor = (have_back_buffer) ? 2 : 1;
426
427 num_modes = depth_buffer_factor * back_buffer_factor * 4;
428
429 if ( pixel_bits == 16 ) {
430 fb_format = GL_RGB;
431 fb_type = GL_UNSIGNED_SHORT_5_6_5;
432 }
433 else {
434 fb_format = GL_BGR;
435 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
436 }
437
438 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
439 m = modes;
440 if ( ! driFillInModes( & m, fb_format, fb_type,
441 depth_bits_array, stencil_bits_array, depth_buffer_factor,
442 back_buffer_modes, back_buffer_factor,
443 GLX_TRUE_COLOR ) ) {
444 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
445 __func__, __LINE__ );
446 return NULL;
447 }
448
449 if ( ! driFillInModes( & m, fb_format, fb_type,
450 depth_bits_array, stencil_bits_array, depth_buffer_factor,
451 back_buffer_modes, back_buffer_factor,
452 GLX_DIRECT_COLOR ) ) {
453 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
454 __func__, __LINE__ );
455 return NULL;
456 }
457
458 /* Mark the visual as slow if there are "fake" stencil bits.
459 */
460 for ( m = modes ; m != NULL ; m = m->next ) {
461 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
462 m->visualRating = GLX_SLOW_CONFIG;
463 }
464 }
465
466 return modes;
467 }
468
469
470 /**
471 * This is the bootstrap function for the driver. libGL supplies all of the
472 * requisite information about the system, and the driver initializes itself.
473 * This routine also fills in the linked list pointed to by \c driver_modes
474 * with the \c __GLcontextModes that the driver can support for windows or
475 * pbuffers.
476 *
477 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
478 * failure.
479 */
480 PUBLIC
481 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
482 const __GLcontextModes * modes,
483 const __DRIversion * ddx_version,
484 const __DRIversion * dri_version,
485 const __DRIversion * drm_version,
486 const __DRIframebuffer * frame_buffer,
487 drmAddress pSAREA, int fd,
488 int internal_api_version,
489 const __DRIinterfaceMethods * interface,
490 __GLcontextModes ** driver_modes )
491
492 {
493 __DRIscreenPrivate *psp;
494 static const __DRIversion ddx_expected = { 4, 0, 0 };
495 static const __DRIversion dri_expected = { 4, 0, 0 };
496 static const __DRIversion drm_expected = { 2, 2, 0 };
497
498
499 dri_interface = interface;
500
501 if ( ! driCheckDriDdxDrmVersions2( "Rage128",
502 dri_version, & dri_expected,
503 ddx_version, & ddx_expected,
504 drm_version, & drm_expected ) ) {
505 return NULL;
506 }
507
508 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
509 ddx_version, dri_version, drm_version,
510 frame_buffer, pSAREA, fd,
511 internal_api_version, &r128API);
512 if ( psp != NULL ) {
513 R128DRIPtr dri_priv = (R128DRIPtr) psp->pDevPriv;
514 *driver_modes = r128FillInModes( dri_priv->bpp,
515 (dri_priv->bpp == 16) ? 16 : 24,
516 (dri_priv->bpp == 16) ? 0 : 8,
517 (dri_priv->backOffset != dri_priv->depthOffset) );
518
519 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
520 * enable the extensions. It just makes sure that all the dispatch offsets for all
521 * the extensions that *might* be enables are known. This is needed because the
522 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
523 * enable the extensions until we have a context pointer.
524 *
525 * Hello chicken. Hello egg. How are you two today?
526 */
527 driInitExtensions( NULL, card_extensions, GL_FALSE );
528 }
529
530 return (void *) psp;
531 }