c5806adc3d19f5d7bc1affce7863a504a2417e03
[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
37 #include "context.h"
38 #include "imports.h"
39
40 #include "utils.h"
41 #include "vblank.h"
42
43 #include "GL/internal/dri_interface.h"
44
45 /* Mach64 configuration
46 */
47 #include "xmlpool.h"
48
49 PUBLIC const char __driConfigOptions[] =
50 DRI_CONF_BEGIN
51 DRI_CONF_SECTION_PERFORMANCE
52 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
53 DRI_CONF_SECTION_END
54 #if ENABLE_PERF_BOXES
55 DRI_CONF_SECTION_DEBUG
56 DRI_CONF_PERFORMANCE_BOXES(false)
57 DRI_CONF_SECTION_END
58 #endif
59 DRI_CONF_END;
60 #if ENABLE_PERF_BOXES
61 static const GLuint __driNConfigOptions = 2;
62 #else
63 static const GLuint __driNConfigOptions = 1;
64 #endif
65
66 #ifdef USE_NEW_INTERFACE
67 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
68 #endif /* USE_NEW_INTERFACE */
69
70 #ifdef USE_NEW_INTERFACE
71 static __GLcontextModes * fill_in_modes( __GLcontextModes * modes,
72 unsigned pixel_bits,
73 unsigned depth_bits,
74 unsigned stencil_bits,
75 const GLenum * db_modes,
76 unsigned num_db_modes,
77 int visType )
78 {
79 static const u_int8_t bits[2][4] = {
80 { 5, 6, 5, 0 },
81 { 8, 8, 8, 0 }
82 };
83
84 static const u_int32_t masks[2][4] = {
85 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
86 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }
87 };
88
89 unsigned i;
90 unsigned j;
91 const unsigned index = ((pixel_bits + 15) / 16) - 1;
92
93 for ( i = 0 ; i < num_db_modes ; i++ ) {
94 for ( j = 0 ; j < 2 ; j++ ) {
95
96 modes->redBits = bits[index][0];
97 modes->greenBits = bits[index][1];
98 modes->blueBits = bits[index][2];
99 modes->alphaBits = bits[index][3];
100 modes->redMask = masks[index][0];
101 modes->greenMask = masks[index][1];
102 modes->blueMask = masks[index][2];
103 modes->alphaMask = masks[index][3];
104 modes->rgbBits = modes->redBits + modes->greenBits
105 + modes->blueBits + modes->alphaBits;
106
107 modes->accumRedBits = 16 * j;
108 modes->accumGreenBits = 16 * j;
109 modes->accumBlueBits = 16 * j;
110 modes->accumAlphaBits = 0;
111 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
112 modes->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
113 modes->stencilBits = stencil_bits;
114 modes->depthBits = depth_bits;
115
116 modes->visualType = visType;
117 modes->renderType = GLX_RGBA_BIT;
118 modes->rgbMode = GL_TRUE;
119
120 if ( db_modes[i] == GLX_NONE ) {
121
122 modes->doubleBufferMode = GL_FALSE;
123 }
124 else {
125 modes->doubleBufferMode = GL_TRUE;
126 modes->swapMethod = db_modes[i];
127 }
128
129 modes = modes->next;
130 }
131 }
132
133 return modes;
134 }
135 #endif /* USE_NEW_INTERFACE */
136
137
138 #ifdef USE_NEW_INTERFACE
139 static __GLcontextModes *
140 mach64FillInModes( unsigned pixel_bits, unsigned depth_bits,
141 unsigned stencil_bits, GLboolean have_back_buffer )
142 {
143 __GLcontextModes * modes;
144 __GLcontextModes * m;
145 unsigned num_modes;
146 unsigned depth_buffer_factor;
147 unsigned back_buffer_factor;
148 unsigned i;
149
150 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
151 * enough to add support. Basically, if a context is created with an
152 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
153 * will never be used.
154 */
155 static const GLenum back_buffer_modes[] = {
156 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
157 };
158
159 int depth_buffer_modes[2][2];
160
161
162 depth_buffer_modes[0][0] = depth_bits;
163 depth_buffer_modes[1][0] = depth_bits;
164
165 /* Just like with the accumulation buffer, always provide some modes
166 * with a stencil buffer. It will be a sw fallback, but some apps won't
167 * care about that.
168 */
169 depth_buffer_modes[0][1] = 0;
170 depth_buffer_modes[1][1] = (stencil_bits == 0) ? 8 : stencil_bits;
171
172 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
173 back_buffer_factor = (have_back_buffer) ? 2 : 1;
174
175 num_modes = depth_buffer_factor * back_buffer_factor * 4;
176
177 modes = (*create_context_modes)( num_modes, sizeof( __GLcontextModes ) );
178 m = modes;
179 for ( i = 0 ; i < depth_buffer_factor ; i++ ) {
180 m = fill_in_modes( m, pixel_bits,
181 depth_buffer_modes[i][0], depth_buffer_modes[i][1],
182 back_buffer_modes, back_buffer_factor,
183 GLX_TRUE_COLOR );
184 }
185
186 for ( i = 0 ; i < depth_buffer_factor ; i++ ) {
187 m = fill_in_modes( m, pixel_bits,
188 depth_buffer_modes[i][0], depth_buffer_modes[i][1],
189 back_buffer_modes, back_buffer_factor,
190 GLX_DIRECT_COLOR );
191 }
192
193 /* Mark the visual as slow if there are "fake" stencil bits.
194 */
195 for ( m = modes ; m != NULL ; m = m->next ) {
196 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ){
197 m->visualRating = GLX_SLOW_CONFIG;
198 }
199 }
200
201 return modes;
202 }
203 #endif /* USE_NEW_INTERFACE */
204
205
206 /* Create the device specific screen private data struct.
207 */
208 static mach64ScreenRec *
209 mach64CreateScreen( __DRIscreenPrivate *sPriv )
210 {
211 mach64ScreenPtr mach64Screen;
212 ATIDRIPtr serverInfo = (ATIDRIPtr)sPriv->pDevPriv;
213
214 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
215 fprintf( stderr, "%s\n", __FUNCTION__ );
216
217 /* Allocate the private area */
218 mach64Screen = (mach64ScreenPtr) CALLOC( sizeof(*mach64Screen) );
219 if ( !mach64Screen ) return NULL;
220
221 /* parse information in __driConfigOptions */
222 driParseOptionInfo (&mach64Screen->optionCache,
223 __driConfigOptions, __driNConfigOptions);
224
225 mach64Screen->IsPCI = serverInfo->IsPCI;
226
227 {
228 drm_mach64_getparam_t gp;
229 int ret;
230
231 gp.param = MACH64_PARAM_IRQ_NR;
232 gp.value = (void *) &mach64Screen->irq;
233
234 ret = drmCommandWriteRead( sPriv->fd, DRM_MACH64_GETPARAM,
235 &gp, sizeof(gp));
236 if (ret) {
237 fprintf(stderr, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret);
238 FREE( mach64Screen );
239 return NULL;
240 }
241 }
242
243 mach64Screen->mmio.handle = serverInfo->regs;
244 mach64Screen->mmio.size = serverInfo->regsSize;
245 if ( drmMap( sPriv->fd,
246 mach64Screen->mmio.handle,
247 mach64Screen->mmio.size,
248 (drmAddressPtr)&mach64Screen->mmio.map ) != 0 ) {
249 FREE( mach64Screen );
250 return NULL;
251 }
252
253 mach64Screen->buffers = drmMapBufs( sPriv->fd );
254 if ( !mach64Screen->buffers ) {
255 drmUnmap( (drmAddress)mach64Screen->mmio.map,
256 mach64Screen->mmio.size );
257 FREE( mach64Screen );
258 return NULL;
259 }
260
261 if ( !mach64Screen->IsPCI ) {
262 mach64Screen->agpTextures.handle = serverInfo->agp;
263 mach64Screen->agpTextures.size = serverInfo->agpSize;
264 if ( drmMap( sPriv->fd,
265 mach64Screen->agpTextures.handle,
266 mach64Screen->agpTextures.size,
267 (drmAddressPtr)&mach64Screen->agpTextures.map ) ) {
268 drmUnmapBufs( mach64Screen->buffers );
269 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
270 FREE( mach64Screen );
271 return NULL;
272 }
273 }
274
275 mach64Screen->AGPMode = serverInfo->AGPMode;
276
277 mach64Screen->chipset = serverInfo->chipset;
278 mach64Screen->width = serverInfo->width;
279 mach64Screen->height = serverInfo->height;
280 mach64Screen->mem = serverInfo->mem;
281 mach64Screen->cpp = serverInfo->cpp;
282
283 mach64Screen->frontOffset = serverInfo->frontOffset;
284 mach64Screen->frontPitch = serverInfo->frontPitch;
285 mach64Screen->backOffset = serverInfo->backOffset;
286 mach64Screen->backPitch = serverInfo->backPitch;
287 mach64Screen->depthOffset = serverInfo->depthOffset;
288 mach64Screen->depthPitch = serverInfo->depthPitch;
289
290 mach64Screen->texOffset[MACH64_CARD_HEAP] = serverInfo->textureOffset;
291 mach64Screen->texSize[MACH64_CARD_HEAP] = serverInfo->textureSize;
292 mach64Screen->logTexGranularity[MACH64_CARD_HEAP] =
293 serverInfo->logTextureGranularity;
294
295 if ( mach64Screen->IsPCI ) {
296 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
297 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
298 mach64Screen->texOffset[MACH64_AGP_HEAP] = 0;
299 mach64Screen->texSize[MACH64_AGP_HEAP] = 0;
300 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = 0;
301 } else {
302 if (mach64Screen->texSize[MACH64_CARD_HEAP] > 0) {
303 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS;
304 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
305 } else {
306 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
307 mach64Screen->firstTexHeap = MACH64_AGP_HEAP;
308 }
309 mach64Screen->texOffset[MACH64_AGP_HEAP] = serverInfo->agpTextureOffset;
310 mach64Screen->texSize[MACH64_AGP_HEAP] = serverInfo->agpSize;
311 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = serverInfo->logAgpTextureGranularity;
312 }
313
314 mach64Screen->driScreen = sPriv;
315 if ( driCompareGLXAPIVersion( 20030813 ) >= 0 ) {
316 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
317 (PFNGLXSCRENABLEEXTENSIONPROC) glXGetProcAddress( (const GLubyte *) "__glXScrEnableExtension" );
318 void * const psc = sPriv->psc->screenConfigs;
319
320 if ( glx_enable_extension != NULL ) {
321 if ( mach64Screen->irq != 0 ) {
322 (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
323 (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
324 (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
325 }
326
327 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
328 }
329 }
330 return mach64Screen;
331 }
332
333 /* Destroy the device specific screen private data struct.
334 */
335 static void
336 mach64DestroyScreen( __DRIscreenPrivate *driScreen )
337 {
338 mach64ScreenRec *mach64Screen = (mach64ScreenRec *) driScreen->private;
339
340 if ( !mach64Screen )
341 return;
342
343 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
344 fprintf( stderr, "%s\n", __FUNCTION__ );
345
346 if ( !mach64Screen->IsPCI ) {
347 drmUnmap( (drmAddress)mach64Screen->agpTextures.map,
348 mach64Screen->agpTextures.size );
349 }
350
351 drmUnmapBufs( mach64Screen->buffers );
352 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
353
354 FREE( mach64Screen );
355 driScreen->private = NULL;
356 }
357
358 /* Create and initialize the Mesa and driver specific pixmap buffer
359 * data.
360 */
361 static GLboolean
362 mach64CreateBuffer( __DRIscreenPrivate *driScrnPriv,
363 __DRIdrawablePrivate *driDrawPriv,
364 const __GLcontextModes *mesaVis,
365 GLboolean isPixmap )
366 {
367 if (isPixmap) {
368 return GL_FALSE; /* not implemented */
369 }
370 else {
371 driDrawPriv->driverPrivate = (void *)
372 _mesa_create_framebuffer( mesaVis,
373 GL_FALSE, /* software depth buffer? */
374 mesaVis->stencilBits > 0,
375 mesaVis->accumRedBits > 0,
376 mesaVis->alphaBits > 0 );
377 return (driDrawPriv->driverPrivate != NULL);
378 }
379 }
380
381
382 static void
383 mach64DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
384 {
385 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
386 }
387
388
389 /* Copy the back color buffer to the front color buffer */
390 static void
391 mach64SwapBuffers(__DRIdrawablePrivate *dPriv)
392 {
393 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
394 mach64ContextPtr mmesa;
395 GLcontext *ctx;
396 mmesa = (mach64ContextPtr) dPriv->driContextPriv->driverPrivate;
397 ctx = mmesa->glCtx;
398 if (ctx->Visual.doubleBufferMode) {
399 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
400 mach64CopyBuffer( dPriv );
401 }
402 }
403 else {
404 /* XXX this shouldn't be an error but we can't handle it for now */
405 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
406 }
407 }
408
409
410 /* Initialize the driver specific screen private data.
411 */
412 static GLboolean
413 mach64InitDriver( __DRIscreenPrivate *driScreen )
414 {
415 driScreen->private = (void *) mach64CreateScreen( driScreen );
416
417 if ( !driScreen->private ) {
418 mach64DestroyScreen( driScreen );
419 return GL_FALSE;
420 }
421
422 return GL_TRUE;
423 }
424
425
426 static struct __DriverAPIRec mach64API = {
427 .InitDriver = mach64InitDriver,
428 .DestroyScreen = mach64DestroyScreen,
429 .CreateContext = mach64CreateContext,
430 .DestroyContext = mach64DestroyContext,
431 .CreateBuffer = mach64CreateBuffer,
432 .DestroyBuffer = mach64DestroyBuffer,
433 .SwapBuffers = mach64SwapBuffers,
434 .MakeCurrent = mach64MakeCurrent,
435 .UnbindContext = mach64UnbindContext,
436 .GetSwapInfo = NULL,
437 .GetMSC = driGetMSC32,
438 .WaitForMSC = driWaitForMSC32,
439 .WaitForSBC = NULL,
440 .SwapBuffersMSC = NULL
441 };
442
443
444 /*
445 * This is the bootstrap function for the driver.
446 * The __driCreateScreen name is the symbol that libGL.so fetches.
447 * Return: pointer to a __DRIscreenPrivate.
448 */
449 #if !defined(DRI_NEW_INTERFACE_ONLY)
450 void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
451 int numConfigs, __GLXvisualConfig *config)
452 {
453 __DRIscreenPrivate *psp;
454 psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &mach64API);
455 return (void *) psp;
456 }
457 #endif /* !defined(DRI_NEW_INTERFACE_ONLY) */
458
459 /**
460 * This is the bootstrap function for the driver. libGL supplies all of the
461 * requisite information about the system, and the driver initializes itself.
462 * This routine also fills in the linked list pointed to by \c driver_modes
463 * with the \c __GLcontextModes that the driver can support for windows or
464 * pbuffers.
465 *
466 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
467 * failure.
468 */
469 #ifdef USE_NEW_INTERFACE
470 PUBLIC
471 void * __driCreateNewScreen( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
472 const __GLcontextModes * modes,
473 const __DRIversion * ddx_version,
474 const __DRIversion * dri_version,
475 const __DRIversion * drm_version,
476 const __DRIframebuffer * frame_buffer,
477 drmAddress pSAREA, int fd,
478 int internal_api_version,
479 __GLcontextModes ** driver_modes )
480
481 {
482 __DRIscreenPrivate *psp;
483 static const __DRIversion ddx_expected = { 6, 4, 0 };
484 static const __DRIversion dri_expected = { 4, 0, 0 };
485 static const __DRIversion drm_expected = { 1, 0, 0 };
486
487 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
488 dri_version, & dri_expected,
489 ddx_version, & ddx_expected,
490 drm_version, & drm_expected ) ) {
491 return NULL;
492 }
493
494 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
495 ddx_version, dri_version, drm_version,
496 frame_buffer, pSAREA, fd,
497 internal_api_version, &mach64API);
498 if ( psp != NULL ) {
499 create_context_modes = (PFNGLXCREATECONTEXTMODES)
500 glXGetProcAddress( (const GLubyte *) "__glXCreateContextModes" );
501 if ( create_context_modes != NULL ) {
502 ATIDRIPtr dri_priv = (ATIDRIPtr) psp->pDevPriv;
503 *driver_modes = mach64FillInModes( dri_priv->cpp * 8,
504 16,
505 0,
506 1);
507 }
508 }
509
510 return (void *) psp;
511 }
512 #endif /* USE_NEW_INTERFACE */