4e9e216e7dbd4acb7c83a8c9790947e68e2be3ff
[mesa.git] / src / mesa / drivers / dri / mach64 / mach64_screen.c
1 /* $XFree86$ */ /* -*- mode: c; c-basic-offset: 3 -*- */
2 /*
3 * Copyright 2000 Gareth Hughes
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * GARETH HUGHES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * Authors:
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * Jos�Fonseca <j_r_fonseca@yahoo.co.uk>
30 */
31
32 #include "mach64_context.h"
33 #include "mach64_ioctl.h"
34 #include "mach64_tris.h"
35 #include "mach64_vb.h"
36 #include "mach64_span.h"
37
38 #include "context.h"
39 #include "imports.h"
40 #include "framebuffer.h"
41 #include "renderbuffer.h"
42
43 #include "utils.h"
44 #include "vblank.h"
45
46 #include "GL/internal/dri_interface.h"
47
48 /* Mach64 configuration
49 */
50 #include "xmlpool.h"
51
52 PUBLIC const char __driConfigOptions[] =
53 DRI_CONF_BEGIN
54 DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
56 DRI_CONF_SECTION_END
57 DRI_CONF_SECTION_DEBUG
58 DRI_CONF_NO_RAST(false)
59 #if ENABLE_PERF_BOXES
60 DRI_CONF_PERFORMANCE_BOXES(false)
61 #endif
62 DRI_CONF_SECTION_END
63 DRI_CONF_END;
64 #if ENABLE_PERF_BOXES
65 static const GLuint __driNConfigOptions = 3;
66 #else
67 static const GLuint __driNConfigOptions = 2;
68 #endif
69
70 extern const struct dri_extension card_extensions[];
71
72 static __GLcontextModes * fill_in_modes( __GLcontextModes * modes,
73 unsigned pixel_bits,
74 unsigned depth_bits,
75 unsigned stencil_bits,
76 const GLenum * db_modes,
77 unsigned num_db_modes,
78 int visType )
79 {
80 static const u_int8_t bits[2][4] = {
81 { 5, 6, 5, 0 },
82 { 8, 8, 8, 0 }
83 };
84
85 static const u_int32_t masks[2][4] = {
86 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
87 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }
88 };
89
90 unsigned i;
91 unsigned j;
92 const unsigned index = ((pixel_bits + 15) / 16) - 1;
93
94 for ( i = 0 ; i < num_db_modes ; i++ ) {
95 for ( j = 0 ; j < 2 ; j++ ) {
96
97 modes->redBits = bits[index][0];
98 modes->greenBits = bits[index][1];
99 modes->blueBits = bits[index][2];
100 modes->alphaBits = bits[index][3];
101 modes->redMask = masks[index][0];
102 modes->greenMask = masks[index][1];
103 modes->blueMask = masks[index][2];
104 modes->alphaMask = masks[index][3];
105 modes->rgbBits = modes->redBits + modes->greenBits
106 + modes->blueBits + modes->alphaBits;
107
108 modes->accumRedBits = 16 * j;
109 modes->accumGreenBits = 16 * j;
110 modes->accumBlueBits = 16 * j;
111 modes->accumAlphaBits = 0;
112 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
113 modes->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
114 modes->stencilBits = stencil_bits;
115 modes->depthBits = depth_bits;
116
117 modes->visualType = visType;
118 modes->renderType = GLX_RGBA_BIT;
119 modes->rgbMode = GL_TRUE;
120
121 if ( db_modes[i] == GLX_NONE ) {
122
123 modes->doubleBufferMode = GL_FALSE;
124 }
125 else {
126 modes->doubleBufferMode = GL_TRUE;
127 modes->swapMethod = db_modes[i];
128 }
129
130 modes = modes->next;
131 }
132 }
133
134 return modes;
135 }
136
137
138 static __GLcontextModes *
139 mach64FillInModes( unsigned pixel_bits, unsigned depth_bits,
140 unsigned stencil_bits, GLboolean have_back_buffer )
141 {
142 __GLcontextModes * modes;
143 __GLcontextModes * m;
144 unsigned num_modes;
145 unsigned depth_buffer_factor;
146 unsigned back_buffer_factor;
147 unsigned i;
148
149 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
150 * enough to add support. Basically, if a context is created with an
151 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
152 * will never be used.
153 */
154 static const GLenum back_buffer_modes[] = {
155 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
156 };
157
158 int depth_buffer_modes[2][2];
159
160
161 depth_buffer_modes[0][0] = depth_bits;
162 depth_buffer_modes[1][0] = depth_bits;
163
164 /* Just like with the accumulation buffer, always provide some modes
165 * with a stencil buffer. It will be a sw fallback, but some apps won't
166 * care about that.
167 */
168 depth_buffer_modes[0][1] = 0;
169 depth_buffer_modes[1][1] = (stencil_bits == 0) ? 8 : stencil_bits;
170
171 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
172 back_buffer_factor = (have_back_buffer) ? 2 : 1;
173
174 num_modes = depth_buffer_factor * back_buffer_factor * 4;
175
176 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
177 m = modes;
178 for ( i = 0 ; i < depth_buffer_factor ; i++ ) {
179 m = fill_in_modes( m, pixel_bits,
180 depth_buffer_modes[i][0], depth_buffer_modes[i][1],
181 back_buffer_modes, back_buffer_factor,
182 GLX_TRUE_COLOR );
183 }
184
185 for ( i = 0 ; i < depth_buffer_factor ; i++ ) {
186 m = fill_in_modes( m, pixel_bits,
187 depth_buffer_modes[i][0], depth_buffer_modes[i][1],
188 back_buffer_modes, back_buffer_factor,
189 GLX_DIRECT_COLOR );
190 }
191
192 /* Mark the visual as slow if there are "fake" stencil bits.
193 */
194 for ( m = modes ; m != NULL ; m = m->next ) {
195 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ){
196 m->visualRating = GLX_SLOW_CONFIG;
197 }
198 }
199
200 return modes;
201 }
202
203
204 /* Create the device specific screen private data struct.
205 */
206 static mach64ScreenRec *
207 mach64CreateScreen( __DRIscreenPrivate *sPriv )
208 {
209 mach64ScreenPtr mach64Screen;
210 ATIDRIPtr serverInfo = (ATIDRIPtr)sPriv->pDevPriv;
211 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
212 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
213 void * const psc = sPriv->psc->screenConfigs;
214
215 if (sPriv->devPrivSize != sizeof(ATIDRIRec)) {
216 fprintf(stderr,"\nERROR! sizeof(ATIDRIRec) does not match passed size from device driver\n");
217 return GL_FALSE;
218 }
219
220 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
221 fprintf( stderr, "%s\n", __FUNCTION__ );
222
223 /* Allocate the private area */
224 mach64Screen = (mach64ScreenPtr) CALLOC( sizeof(*mach64Screen) );
225 if ( !mach64Screen ) return NULL;
226
227 /* parse information in __driConfigOptions */
228 driParseOptionInfo (&mach64Screen->optionCache,
229 __driConfigOptions, __driNConfigOptions);
230
231 mach64Screen->IsPCI = serverInfo->IsPCI;
232
233 {
234 drm_mach64_getparam_t gp;
235 int ret;
236
237 gp.param = MACH64_PARAM_IRQ_NR;
238 gp.value = (void *) &mach64Screen->irq;
239
240 ret = drmCommandWriteRead( sPriv->fd, DRM_MACH64_GETPARAM,
241 &gp, sizeof(gp));
242 if (ret) {
243 fprintf(stderr, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret);
244 FREE( mach64Screen );
245 return NULL;
246 }
247 }
248
249 mach64Screen->mmio.handle = serverInfo->regs;
250 mach64Screen->mmio.size = serverInfo->regsSize;
251 if ( drmMap( sPriv->fd,
252 mach64Screen->mmio.handle,
253 mach64Screen->mmio.size,
254 (drmAddressPtr)&mach64Screen->mmio.map ) != 0 ) {
255 FREE( mach64Screen );
256 return NULL;
257 }
258
259 mach64Screen->buffers = drmMapBufs( sPriv->fd );
260 if ( !mach64Screen->buffers ) {
261 drmUnmap( (drmAddress)mach64Screen->mmio.map,
262 mach64Screen->mmio.size );
263 FREE( mach64Screen );
264 return NULL;
265 }
266
267 if ( !mach64Screen->IsPCI ) {
268 mach64Screen->agpTextures.handle = serverInfo->agp;
269 mach64Screen->agpTextures.size = serverInfo->agpSize;
270 if ( drmMap( sPriv->fd,
271 mach64Screen->agpTextures.handle,
272 mach64Screen->agpTextures.size,
273 (drmAddressPtr)&mach64Screen->agpTextures.map ) ) {
274 drmUnmapBufs( mach64Screen->buffers );
275 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
276 FREE( mach64Screen );
277 return NULL;
278 }
279 }
280
281 mach64Screen->AGPMode = serverInfo->AGPMode;
282
283 mach64Screen->chipset = serverInfo->chipset;
284 mach64Screen->width = serverInfo->width;
285 mach64Screen->height = serverInfo->height;
286 mach64Screen->mem = serverInfo->mem;
287 mach64Screen->cpp = serverInfo->cpp;
288
289 mach64Screen->frontOffset = serverInfo->frontOffset;
290 mach64Screen->frontPitch = serverInfo->frontPitch;
291 mach64Screen->backOffset = serverInfo->backOffset;
292 mach64Screen->backPitch = serverInfo->backPitch;
293 mach64Screen->depthOffset = serverInfo->depthOffset;
294 mach64Screen->depthPitch = serverInfo->depthPitch;
295
296 mach64Screen->texOffset[MACH64_CARD_HEAP] = serverInfo->textureOffset;
297 mach64Screen->texSize[MACH64_CARD_HEAP] = serverInfo->textureSize;
298 mach64Screen->logTexGranularity[MACH64_CARD_HEAP] =
299 serverInfo->logTextureGranularity;
300
301 if ( mach64Screen->IsPCI ) {
302 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
303 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
304 mach64Screen->texOffset[MACH64_AGP_HEAP] = 0;
305 mach64Screen->texSize[MACH64_AGP_HEAP] = 0;
306 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = 0;
307 } else {
308 if (serverInfo->textureSize > 0) {
309 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS;
310 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
311 } else {
312 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
313 mach64Screen->firstTexHeap = MACH64_AGP_HEAP;
314 }
315 mach64Screen->texOffset[MACH64_AGP_HEAP] = serverInfo->agpTextureOffset;
316 mach64Screen->texSize[MACH64_AGP_HEAP] = serverInfo->agpSize;
317 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = serverInfo->logAgpTextureGranularity;
318 }
319
320 mach64Screen->driScreen = sPriv;
321
322 if ( glx_enable_extension != NULL ) {
323 if ( mach64Screen->irq != 0 ) {
324 (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
325 (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
326 (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
327 }
328
329 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
330 }
331
332 return mach64Screen;
333 }
334
335 /* Destroy the device specific screen private data struct.
336 */
337 static void
338 mach64DestroyScreen( __DRIscreenPrivate *driScreen )
339 {
340 mach64ScreenRec *mach64Screen = (mach64ScreenRec *) driScreen->private;
341
342 if ( !mach64Screen )
343 return;
344
345 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
346 fprintf( stderr, "%s\n", __FUNCTION__ );
347
348 if ( !mach64Screen->IsPCI ) {
349 drmUnmap( (drmAddress)mach64Screen->agpTextures.map,
350 mach64Screen->agpTextures.size );
351 }
352
353 drmUnmapBufs( mach64Screen->buffers );
354 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
355
356 FREE( mach64Screen );
357 driScreen->private = NULL;
358 }
359
360
361 /* Create and initialize the Mesa and driver specific pixmap buffer
362 * data.
363 */
364 static GLboolean
365 mach64CreateBuffer( __DRIscreenPrivate *driScrnPriv,
366 __DRIdrawablePrivate *driDrawPriv,
367 const __GLcontextModes *mesaVis,
368 GLboolean isPixmap )
369 {
370 mach64ScreenPtr screen = (mach64ScreenPtr) driScrnPriv->private;
371
372 if (isPixmap) {
373 return GL_FALSE; /* not implemented */
374 }
375 else {
376 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
377
378 {
379 driRenderbuffer *frontRb
380 = driNewRenderbuffer(GL_RGBA,
381 NULL,
382 screen->cpp,
383 screen->frontOffset, screen->frontPitch,
384 driDrawPriv);
385 mach64SetSpanFunctions(frontRb, mesaVis);
386 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
387 }
388
389 if (mesaVis->doubleBufferMode) {
390 driRenderbuffer *backRb
391 = driNewRenderbuffer(GL_RGBA,
392 NULL,
393 screen->cpp,
394 screen->backOffset, screen->backPitch,
395 driDrawPriv);
396 mach64SetSpanFunctions(backRb, mesaVis);
397 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
398 }
399
400 if (mesaVis->depthBits == 16) {
401 driRenderbuffer *depthRb
402 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
403 NULL, screen->cpp,
404 screen->depthOffset, screen->depthPitch,
405 driDrawPriv);
406 mach64SetSpanFunctions(depthRb, mesaVis);
407 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
408 }
409 else if (mesaVis->depthBits == 24) {
410 /* XXX I don't think 24-bit Z is supported - so this isn't used */
411 driRenderbuffer *depthRb
412 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
413 NULL,
414 screen->cpp,
415 screen->depthOffset, screen->depthPitch,
416 driDrawPriv);
417 mach64SetSpanFunctions(depthRb, mesaVis);
418 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
419 }
420
421 _mesa_add_soft_renderbuffers(fb,
422 GL_FALSE, /* color */
423 GL_FALSE, /* depth */
424 mesaVis->stencilBits > 0,
425 mesaVis->accumRedBits > 0,
426 GL_FALSE, /* alpha */
427 GL_FALSE /* aux */);
428 driDrawPriv->driverPrivate = (void *) fb;
429
430 return (driDrawPriv->driverPrivate != NULL);
431 }
432 }
433
434
435 static void
436 mach64DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
437 {
438 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
439 }
440
441
442 /* Copy the back color buffer to the front color buffer */
443 static void
444 mach64SwapBuffers(__DRIdrawablePrivate *dPriv)
445 {
446 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
447 mach64ContextPtr mmesa;
448 GLcontext *ctx;
449 mmesa = (mach64ContextPtr) dPriv->driContextPriv->driverPrivate;
450 ctx = mmesa->glCtx;
451 if (ctx->Visual.doubleBufferMode) {
452 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
453 mach64CopyBuffer( dPriv );
454 }
455 }
456 else {
457 /* XXX this shouldn't be an error but we can't handle it for now */
458 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
459 }
460 }
461
462
463 /* Initialize the driver specific screen private data.
464 */
465 static GLboolean
466 mach64InitDriver( __DRIscreenPrivate *driScreen )
467 {
468 driScreen->private = (void *) mach64CreateScreen( driScreen );
469
470 if ( !driScreen->private ) {
471 mach64DestroyScreen( driScreen );
472 return GL_FALSE;
473 }
474
475 return GL_TRUE;
476 }
477
478
479 static struct __DriverAPIRec mach64API = {
480 .InitDriver = mach64InitDriver,
481 .DestroyScreen = mach64DestroyScreen,
482 .CreateContext = mach64CreateContext,
483 .DestroyContext = mach64DestroyContext,
484 .CreateBuffer = mach64CreateBuffer,
485 .DestroyBuffer = mach64DestroyBuffer,
486 .SwapBuffers = mach64SwapBuffers,
487 .MakeCurrent = mach64MakeCurrent,
488 .UnbindContext = mach64UnbindContext,
489 .GetSwapInfo = NULL,
490 .GetMSC = driGetMSC32,
491 .WaitForMSC = driWaitForMSC32,
492 .WaitForSBC = NULL,
493 .SwapBuffersMSC = NULL
494 };
495
496
497 /**
498 * This is the bootstrap function for the driver. libGL supplies all of the
499 * requisite information about the system, and the driver initializes itself.
500 * This routine also fills in the linked list pointed to by \c driver_modes
501 * with the \c __GLcontextModes that the driver can support for windows or
502 * pbuffers.
503 *
504 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
505 * failure.
506 */
507 PUBLIC
508 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
509 const __GLcontextModes * modes,
510 const __DRIversion * ddx_version,
511 const __DRIversion * dri_version,
512 const __DRIversion * drm_version,
513 const __DRIframebuffer * frame_buffer,
514 drmAddress pSAREA, int fd,
515 int internal_api_version,
516 const __DRIinterfaceMethods * interface,
517 __GLcontextModes ** driver_modes )
518
519 {
520 __DRIscreenPrivate *psp;
521 static const __DRIversion ddx_expected = { 6, 4, 0 };
522 static const __DRIversion dri_expected = { 4, 0, 0 };
523 static const __DRIversion drm_expected = { 2, 0, 0 };
524
525 dri_interface = interface;
526
527 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
528 dri_version, & dri_expected,
529 ddx_version, & ddx_expected,
530 drm_version, & drm_expected ) ) {
531 return NULL;
532 }
533
534 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
535 ddx_version, dri_version, drm_version,
536 frame_buffer, pSAREA, fd,
537 internal_api_version, &mach64API);
538 if ( psp != NULL ) {
539 ATIDRIPtr dri_priv = (ATIDRIPtr) psp->pDevPriv;
540 *driver_modes = mach64FillInModes( dri_priv->cpp * 8,
541 16,
542 0,
543 1);
544
545 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
546 * enable the extensions. It just makes sure that all the dispatch offsets for all
547 * the extensions that *might* be enables are known. This is needed because the
548 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
549 * enable the extensions until we have a context pointer.
550 *
551 * Hello chicken. Hello egg. How are you two today?
552 */
553 driInitExtensions( NULL, card_extensions, GL_FALSE );
554 }
555
556 return (void *) psp;
557 }