Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / drivers / dri / mach64 / mach64_screen.c
1 /* -*- 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 "main/context.h"
39 #include "main/imports.h"
40 #include "main/framebuffer.h"
41 #include "main/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 const __DRIconfig **
73 mach64FillInModes( __DRIscreenPrivate *psp,
74 unsigned pixel_bits, unsigned depth_bits,
75 unsigned stencil_bits, GLboolean have_back_buffer )
76 {
77 __DRIconfig **configs;
78 __GLcontextModes * m;
79 GLenum fb_format;
80 GLenum fb_type;
81 unsigned depth_buffer_factor;
82 unsigned back_buffer_factor;
83 unsigned i;
84
85 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
86 * enough to add support. Basically, if a context is created with an
87 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
88 * will never be used.
89 */
90 static const GLenum back_buffer_modes[] = {
91 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
92 };
93
94 uint8_t depth_bits_array[2];
95 uint8_t stencil_bits_array[2];
96 uint8_t msaa_samples_array[1];
97
98 depth_bits_array[0] = depth_bits;
99 depth_bits_array[1] = depth_bits;
100
101 /* Just like with the accumulation buffer, always provide some modes
102 * with a stencil buffer. It will be a sw fallback, but some apps won't
103 * care about that.
104 */
105 stencil_bits_array[0] = 0;
106 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
107
108 msaa_samples_array[0] = 0;
109
110 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
111 back_buffer_factor = (have_back_buffer) ? 2 : 1;
112
113 if (pixel_bits == 16) {
114 fb_format = GL_RGB;
115 fb_type = GL_UNSIGNED_SHORT_5_6_5;
116 }
117 else {
118 fb_format = GL_BGRA;
119 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
120 }
121
122 configs = driCreateConfigs(fb_format, fb_type,
123 depth_bits_array, stencil_bits_array,
124 depth_buffer_factor, back_buffer_modes,
125 back_buffer_factor,
126 msaa_samples_array, 1);
127 if (configs == NULL) {
128 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n",
129 __func__, __LINE__);
130 return NULL;
131 }
132
133 /* Mark the visual as slow if there are "fake" stencil bits.
134 */
135 for (i = 0; configs[i]; i++) {
136 m = &configs[i]->modes;
137 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
138 m->visualRating = GLX_SLOW_CONFIG;
139 }
140 }
141
142 return (const __DRIconfig **) configs;
143 }
144
145
146 /* Create the device specific screen private data struct.
147 */
148 static mach64ScreenRec *
149 mach64CreateScreen( __DRIscreenPrivate *sPriv )
150 {
151 mach64ScreenPtr mach64Screen;
152 ATIDRIPtr serverInfo = (ATIDRIPtr)sPriv->pDevPriv;
153 int i;
154
155 if (sPriv->devPrivSize != sizeof(ATIDRIRec)) {
156 fprintf(stderr,"\nERROR! sizeof(ATIDRIRec) does not match passed size from device driver\n");
157 return GL_FALSE;
158 }
159
160 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
161 fprintf( stderr, "%s\n", __FUNCTION__ );
162
163 /* Allocate the private area */
164 mach64Screen = (mach64ScreenPtr) CALLOC( sizeof(*mach64Screen) );
165 if ( !mach64Screen ) return NULL;
166
167 /* parse information in __driConfigOptions */
168 driParseOptionInfo (&mach64Screen->optionCache,
169 __driConfigOptions, __driNConfigOptions);
170
171 mach64Screen->IsPCI = serverInfo->IsPCI;
172
173 {
174 drm_mach64_getparam_t gp;
175 int ret;
176
177 gp.param = MACH64_PARAM_IRQ_NR;
178 gp.value = (void *) &mach64Screen->irq;
179
180 ret = drmCommandWriteRead( sPriv->fd, DRM_MACH64_GETPARAM,
181 &gp, sizeof(gp));
182 if (ret) {
183 fprintf(stderr, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret);
184 FREE( mach64Screen );
185 return NULL;
186 }
187 }
188
189 mach64Screen->mmio.handle = serverInfo->regs;
190 mach64Screen->mmio.size = serverInfo->regsSize;
191 if ( drmMap( sPriv->fd,
192 mach64Screen->mmio.handle,
193 mach64Screen->mmio.size,
194 (drmAddressPtr)&mach64Screen->mmio.map ) != 0 ) {
195 FREE( mach64Screen );
196 return NULL;
197 }
198
199 mach64Screen->buffers = drmMapBufs( sPriv->fd );
200 if ( !mach64Screen->buffers ) {
201 drmUnmap( (drmAddress)mach64Screen->mmio.map,
202 mach64Screen->mmio.size );
203 FREE( mach64Screen );
204 return NULL;
205 }
206
207 if ( !mach64Screen->IsPCI ) {
208 mach64Screen->agpTextures.handle = serverInfo->agp;
209 mach64Screen->agpTextures.size = serverInfo->agpSize;
210 if ( drmMap( sPriv->fd,
211 mach64Screen->agpTextures.handle,
212 mach64Screen->agpTextures.size,
213 (drmAddressPtr)&mach64Screen->agpTextures.map ) ) {
214 drmUnmapBufs( mach64Screen->buffers );
215 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
216 FREE( mach64Screen );
217 return NULL;
218 }
219 }
220
221 mach64Screen->AGPMode = serverInfo->AGPMode;
222
223 mach64Screen->chipset = serverInfo->chipset;
224 mach64Screen->width = serverInfo->width;
225 mach64Screen->height = serverInfo->height;
226 mach64Screen->mem = serverInfo->mem;
227 mach64Screen->cpp = serverInfo->cpp;
228
229 mach64Screen->frontOffset = serverInfo->frontOffset;
230 mach64Screen->frontPitch = serverInfo->frontPitch;
231 mach64Screen->backOffset = serverInfo->backOffset;
232 mach64Screen->backPitch = serverInfo->backPitch;
233 mach64Screen->depthOffset = serverInfo->depthOffset;
234 mach64Screen->depthPitch = serverInfo->depthPitch;
235
236 mach64Screen->texOffset[MACH64_CARD_HEAP] = serverInfo->textureOffset;
237 mach64Screen->texSize[MACH64_CARD_HEAP] = serverInfo->textureSize;
238 mach64Screen->logTexGranularity[MACH64_CARD_HEAP] =
239 serverInfo->logTextureGranularity;
240
241 if ( mach64Screen->IsPCI ) {
242 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
243 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
244 mach64Screen->texOffset[MACH64_AGP_HEAP] = 0;
245 mach64Screen->texSize[MACH64_AGP_HEAP] = 0;
246 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = 0;
247 } else {
248 if (serverInfo->textureSize > 0) {
249 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS;
250 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
251 } else {
252 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
253 mach64Screen->firstTexHeap = MACH64_AGP_HEAP;
254 }
255 mach64Screen->texOffset[MACH64_AGP_HEAP] = serverInfo->agpTextureOffset;
256 mach64Screen->texSize[MACH64_AGP_HEAP] = serverInfo->agpSize;
257 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = serverInfo->logAgpTextureGranularity;
258 }
259
260 mach64Screen->driScreen = sPriv;
261
262 i = 0;
263 mach64Screen->extensions[i++] = &driFrameTrackingExtension.base;
264 if ( mach64Screen->irq != 0 ) {
265 mach64Screen->extensions[i++] = &driSwapControlExtension.base;
266 mach64Screen->extensions[i++] = &driMediaStreamCounterExtension.base;
267 }
268 mach64Screen->extensions[i++] = NULL;
269 sPriv->extensions = mach64Screen->extensions;
270
271 return mach64Screen;
272 }
273
274 /* Destroy the device specific screen private data struct.
275 */
276 static void
277 mach64DestroyScreen( __DRIscreenPrivate *driScreen )
278 {
279 mach64ScreenRec *mach64Screen = (mach64ScreenRec *) driScreen->private;
280
281 if ( !mach64Screen )
282 return;
283
284 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
285 fprintf( stderr, "%s\n", __FUNCTION__ );
286
287 if ( !mach64Screen->IsPCI ) {
288 drmUnmap( (drmAddress)mach64Screen->agpTextures.map,
289 mach64Screen->agpTextures.size );
290 }
291
292 drmUnmapBufs( mach64Screen->buffers );
293 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
294
295 FREE( mach64Screen );
296 driScreen->private = NULL;
297 }
298
299
300 /* Create and initialize the Mesa and driver specific pixmap buffer
301 * data.
302 */
303 static GLboolean
304 mach64CreateBuffer( __DRIscreenPrivate *driScrnPriv,
305 __DRIdrawablePrivate *driDrawPriv,
306 const __GLcontextModes *mesaVis,
307 GLboolean isPixmap )
308 {
309 mach64ScreenPtr screen = (mach64ScreenPtr) driScrnPriv->private;
310
311 if (isPixmap) {
312 return GL_FALSE; /* not implemented */
313 }
314 else {
315 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
316
317 {
318 driRenderbuffer *frontRb
319 = driNewRenderbuffer(GL_RGBA,
320 NULL,
321 screen->cpp,
322 screen->frontOffset, screen->frontPitch,
323 driDrawPriv);
324 mach64SetSpanFunctions(frontRb, mesaVis);
325 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
326 }
327
328 if (mesaVis->doubleBufferMode) {
329 driRenderbuffer *backRb
330 = driNewRenderbuffer(GL_RGBA,
331 NULL,
332 screen->cpp,
333 screen->backOffset, screen->backPitch,
334 driDrawPriv);
335 mach64SetSpanFunctions(backRb, mesaVis);
336 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
337 }
338
339 if (mesaVis->depthBits == 16) {
340 driRenderbuffer *depthRb
341 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
342 NULL, screen->cpp,
343 screen->depthOffset, screen->depthPitch,
344 driDrawPriv);
345 mach64SetSpanFunctions(depthRb, mesaVis);
346 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
347 }
348 else if (mesaVis->depthBits == 24) {
349 /* XXX I don't think 24-bit Z is supported - so this isn't used */
350 driRenderbuffer *depthRb
351 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
352 NULL,
353 screen->cpp,
354 screen->depthOffset, screen->depthPitch,
355 driDrawPriv);
356 mach64SetSpanFunctions(depthRb, mesaVis);
357 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
358 }
359
360 _mesa_add_soft_renderbuffers(fb,
361 GL_FALSE, /* color */
362 GL_FALSE, /* depth */
363 mesaVis->stencilBits > 0,
364 mesaVis->accumRedBits > 0,
365 GL_FALSE, /* alpha */
366 GL_FALSE /* aux */);
367 driDrawPriv->driverPrivate = (void *) fb;
368
369 return (driDrawPriv->driverPrivate != NULL);
370 }
371 }
372
373
374 static void
375 mach64DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
376 {
377 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
378 }
379
380
381 /* Copy the back color buffer to the front color buffer */
382 static void
383 mach64SwapBuffers(__DRIdrawablePrivate *dPriv)
384 {
385 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
386 mach64ContextPtr mmesa;
387 GLcontext *ctx;
388 mmesa = (mach64ContextPtr) dPriv->driContextPriv->driverPrivate;
389 ctx = mmesa->glCtx;
390 if (ctx->Visual.doubleBufferMode) {
391 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
392 mach64CopyBuffer( dPriv );
393 }
394 }
395 else {
396 /* XXX this shouldn't be an error but we can't handle it for now */
397 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
398 }
399 }
400
401
402 /* Initialize the driver specific screen private data.
403 */
404 static GLboolean
405 mach64InitDriver( __DRIscreenPrivate *driScreen )
406 {
407 driScreen->private = (void *) mach64CreateScreen( driScreen );
408
409 if ( !driScreen->private ) {
410 mach64DestroyScreen( driScreen );
411 return GL_FALSE;
412 }
413
414 return GL_TRUE;
415 }
416
417 /**
418 * This is the driver specific part of the createNewScreen entry point.
419 *
420 * \todo maybe fold this into intelInitDriver
421 *
422 * \return the __GLcontextModes supported by this driver
423 */
424 static const __DRIconfig **
425 mach64InitScreen(__DRIscreenPrivate *psp)
426 {
427 static const __DRIversion ddx_expected = { 6, 4, 0 };
428 static const __DRIversion dri_expected = { 4, 0, 0 };
429 static const __DRIversion drm_expected = { 2, 0, 0 };
430 ATIDRIPtr dri_priv = (ATIDRIPtr) psp->pDevPriv;
431
432 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
433 &psp->dri_version, & dri_expected,
434 &psp->ddx_version, & ddx_expected,
435 &psp->drm_version, & drm_expected ) ) {
436 return NULL;
437 }
438
439 /* Calling driInitExtensions here, with a NULL context pointer,
440 * does not actually enable the extensions. It just makes sure
441 * that all the dispatch offsets for all the extensions that
442 * *might* be enables are known. This is needed because the
443 * dispatch offsets need to be known when _mesa_context_create is
444 * called, but we can't enable the extensions until we have a
445 * context pointer.
446 *
447 * Hello chicken. Hello egg. How are you two today?
448 */
449 driInitExtensions( NULL, card_extensions, GL_FALSE );
450
451 if (!mach64InitDriver(psp))
452 return NULL;
453
454 return mach64FillInModes( psp, dri_priv->cpp * 8, 16, 0, 1);
455 }
456
457 const struct __DriverAPIRec driDriverAPI = {
458 .InitScreen = mach64InitScreen,
459 .DestroyScreen = mach64DestroyScreen,
460 .CreateContext = mach64CreateContext,
461 .DestroyContext = mach64DestroyContext,
462 .CreateBuffer = mach64CreateBuffer,
463 .DestroyBuffer = mach64DestroyBuffer,
464 .SwapBuffers = mach64SwapBuffers,
465 .MakeCurrent = mach64MakeCurrent,
466 .UnbindContext = mach64UnbindContext,
467 .GetSwapInfo = NULL,
468 .GetDrawableMSC = driDrawableGetMSC32,
469 .WaitForMSC = driWaitForMSC32,
470 .WaitForSBC = NULL,
471 .SwapBuffersMSC = NULL
472 };
473