Fixes from https://bugs.freedesktop.org/attachment.cgi?id=3077
[mesa.git] / src / mesa / drivers / dri / unichrome / via_screen.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. 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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26
27 #include "utils.h"
28 #include "dri_util.h"
29 #include "glheader.h"
30 #include "context.h"
31 #include "framebuffer.h"
32 #include "renderbuffer.h"
33 #include "matrix.h"
34 #include "simple_list.h"
35 #include "vblank.h"
36
37 #include "via_state.h"
38 #include "via_tex.h"
39 #include "via_span.h"
40 #include "via_tris.h"
41 #include "via_ioctl.h"
42 #include "via_screen.h"
43 #include "via_fb.h"
44 #include "via_dri.h"
45
46 #include "GL/internal/dri_interface.h"
47
48 /* Radeon configuration
49 */
50 #include "xmlpool.h"
51
52 const char __driConfigOptions[] =
53 DRI_CONF_BEGIN
54 DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
57 DRI_CONF_SECTION_END
58 DRI_CONF_SECTION_DEBUG
59 DRI_CONF_NO_RAST(false)
60 DRI_CONF_SECTION_END
61 DRI_CONF_END;
62 static const GLuint __driNConfigOptions = 3;
63
64 extern const struct dri_extension card_extensions[];
65
66 static int getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
67
68 static drmBufMapPtr via_create_empty_buffers(void)
69 {
70 drmBufMapPtr retval;
71
72 retval = (drmBufMapPtr)MALLOC(sizeof(drmBufMap));
73 if (retval == NULL) return NULL;
74 memset(retval, 0, sizeof(drmBufMap));
75
76 retval->list = (drmBufPtr)MALLOC(sizeof(drmBuf) * VIA_DMA_BUF_NR);
77 if (retval->list == NULL) {
78 FREE(retval);
79 return NULL;
80 }
81 memset(retval->list, 0, sizeof(drmBuf) * VIA_DMA_BUF_NR);
82 return retval;
83 }
84
85 static void via_free_empty_buffers( drmBufMapPtr bufs )
86 {
87 if (bufs && bufs->list)
88 FREE(bufs->list);
89
90 if (bufs)
91 FREE(bufs);
92 }
93
94
95 static GLboolean
96 viaInitDriver(__DRIscreenPrivate *sPriv)
97 {
98 viaScreenPrivate *viaScreen;
99 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
100 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
101 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
102 void * const psc = sPriv->psc->screenConfigs;
103 drmAddress map;
104
105 if (sPriv->devPrivSize != sizeof(VIADRIRec)) {
106 fprintf(stderr,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
107 return GL_FALSE;
108 }
109
110 /* Allocate the private area */
111 viaScreen = (viaScreenPrivate *) CALLOC(sizeof(viaScreenPrivate));
112 if (!viaScreen) {
113 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
114 return GL_FALSE;
115 }
116
117 /* parse information in __driConfigOptions */
118 driParseOptionInfo (&viaScreen->optionCache,
119 __driConfigOptions, __driNConfigOptions);
120
121
122 viaScreen->driScrnPriv = sPriv;
123 sPriv->private = (void *)viaScreen;
124
125 viaScreen->deviceID = gDRIPriv->deviceID;
126 viaScreen->width = gDRIPriv->width;
127 viaScreen->height = gDRIPriv->height;
128 viaScreen->mem = gDRIPriv->mem;
129 viaScreen->bitsPerPixel = gDRIPriv->bytesPerPixel * 8;
130 viaScreen->bytesPerPixel = gDRIPriv->bytesPerPixel;
131 viaScreen->fbOffset = 0;
132 viaScreen->fbSize = gDRIPriv->fbSize;
133 viaScreen->irqEnabled = gDRIPriv->irqEnabled;
134 viaScreen->irqEnabled = 1;
135
136 if (VIA_DEBUG & DEBUG_DRI) {
137 fprintf(stderr, "deviceID = %08x\n", viaScreen->deviceID);
138 fprintf(stderr, "width = %08x\n", viaScreen->width);
139 fprintf(stderr, "height = %08x\n", viaScreen->height);
140 fprintf(stderr, "cpp = %08x\n", viaScreen->cpp);
141 fprintf(stderr, "fbOffset = %08x\n", viaScreen->fbOffset);
142 }
143
144 viaScreen->bufs = via_create_empty_buffers();
145 if (viaScreen->bufs == NULL) {
146 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
147 FREE(viaScreen);
148 return GL_FALSE;
149 }
150
151 if (drmMap(sPriv->fd,
152 gDRIPriv->regs.handle,
153 gDRIPriv->regs.size,
154 &map) != 0) {
155 FREE(viaScreen);
156 sPriv->private = NULL;
157 __driUtilMessage("viaInitDriver: drmMap regs failed");
158 return GL_FALSE;
159 }
160
161 if (gDRIPriv->agp.size) {
162 if (drmMap(sPriv->fd,
163 gDRIPriv->agp.handle,
164 gDRIPriv->agp.size,
165 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
166 drmUnmap(viaScreen->reg, gDRIPriv->agp.size);
167 FREE(viaScreen);
168 sPriv->private = NULL;
169 __driUtilMessage("viaInitDriver: drmMap agp failed");
170 return GL_FALSE;
171 }
172 viaScreen->agpBase = (GLuint *)gDRIPriv->agp.handle;
173 } else
174 viaScreen->agpLinearStart = 0;
175
176 viaScreen->sareaPrivOffset = gDRIPriv->sarea_priv_offset;
177
178 if ( glx_enable_extension != NULL ) {
179 if ( viaScreen->irqEnabled ) {
180 (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
181 (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
182 (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
183 }
184
185 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
186 }
187
188 return GL_TRUE;
189 }
190
191 static void
192 viaDestroyScreen(__DRIscreenPrivate *sPriv)
193 {
194 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
195 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
196
197 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
198 if (gDRIPriv->agp.size)
199 drmUnmap(viaScreen->agpLinearStart, gDRIPriv->agp.size);
200
201 via_free_empty_buffers(viaScreen->bufs);
202
203 FREE(viaScreen);
204 sPriv->private = NULL;
205 }
206
207
208 static GLboolean
209 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
210 __DRIdrawablePrivate *driDrawPriv,
211 const __GLcontextModes *mesaVis,
212 GLboolean isPixmap)
213 {
214 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
215
216 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
217 mesaVis->depthBits != 24);
218
219
220 if (isPixmap) {
221 /* KW: This needs work, disabled for now:
222 */
223 #if 0
224 driDrawPriv->driverPrivate = (void *)
225 _mesa_create_framebuffer(mesaVis,
226 GL_FALSE, /* software depth buffer? */
227 swStencil,
228 mesaVis->accumRedBits > 0,
229 GL_FALSE /* s/w alpha planes */);
230
231 return (driDrawPriv->driverPrivate != NULL);
232 #endif
233 return GL_FALSE;
234 }
235 else {
236 #if 0
237 driDrawPriv->driverPrivate = (void *)
238 _mesa_create_framebuffer(mesaVis,
239 GL_FALSE, /* software depth buffer? */
240 swStencil,
241 mesaVis->accumRedBits > 0,
242 GL_FALSE /* s/w alpha planes */);
243 #else
244 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
245
246 /* XXX check/fix the offset/pitch parameters! */
247 {
248 driRenderbuffer *frontRb
249 = driNewRenderbuffer(GL_RGBA, screen->bytesPerPixel,
250 0, screen->width);
251 viaSetSpanFunctions(frontRb, mesaVis);
252 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
253 }
254
255 if (mesaVis->doubleBufferMode) {
256 driRenderbuffer *backRb
257 = driNewRenderbuffer(GL_RGBA, screen->bytesPerPixel,
258 0, screen->width);
259 viaSetSpanFunctions(backRb, mesaVis);
260 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
261 }
262
263 if (mesaVis->depthBits == 16) {
264 driRenderbuffer *depthRb
265 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, screen->bytesPerPixel,
266 0, screen->width);
267 viaSetSpanFunctions(depthRb, mesaVis);
268 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
269 }
270 else if (mesaVis->depthBits == 24) {
271 driRenderbuffer *depthRb
272 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, screen->bytesPerPixel,
273 0, screen->width);
274 viaSetSpanFunctions(depthRb, mesaVis);
275 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
276 }
277 else if (mesaVis->depthBits == 32) {
278 driRenderbuffer *depthRb
279 = driNewRenderbuffer(GL_DEPTH_COMPONENT32, screen->bytesPerPixel,
280 0, screen->width);
281 viaSetSpanFunctions(depthRb, mesaVis);
282 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
283 }
284
285 if (mesaVis->stencilBits > 0 && !swStencil) {
286 driRenderbuffer *stencilRb
287 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, screen->bytesPerPixel,
288 0, screen->width);
289 viaSetSpanFunctions(stencilRb, mesaVis);
290 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
291 }
292
293 _mesa_add_soft_renderbuffers(fb,
294 GL_FALSE, /* color */
295 GL_FALSE, /* depth */
296 swStencil,
297 mesaVis->accumRedBits > 0,
298 GL_FALSE, /* alpha */
299 GL_FALSE /* aux */);
300 driDrawPriv->driverPrivate = (void *) fb;
301 #endif
302 return (driDrawPriv->driverPrivate != NULL);
303 }
304 }
305
306
307 static void
308 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
309 {
310 _mesa_destroy_framebuffer((GLframebuffer *)(driDrawPriv->driverPrivate));
311 }
312
313
314
315 static struct __DriverAPIRec viaAPI = {
316 .InitDriver = viaInitDriver,
317 .DestroyScreen = viaDestroyScreen,
318 .CreateContext = viaCreateContext,
319 .DestroyContext = viaDestroyContext,
320 .CreateBuffer = viaCreateBuffer,
321 .DestroyBuffer = viaDestroyBuffer,
322 .SwapBuffers = viaSwapBuffers,
323 .MakeCurrent = viaMakeCurrent,
324 .UnbindContext = viaUnbindContext,
325 .GetSwapInfo = getSwapInfo,
326 .GetMSC = driGetMSC32,
327 .WaitForMSC = driWaitForMSC32,
328 .WaitForSBC = NULL,
329 .SwapBuffersMSC = NULL
330 };
331
332
333 static __GLcontextModes *
334 viaFillInModes( unsigned pixel_bits, GLboolean have_back_buffer )
335 {
336 __GLcontextModes * modes;
337 __GLcontextModes * m;
338 unsigned num_modes;
339 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
340 GLenum fb_format;
341 GLenum fb_type;
342
343 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
344 * enough to add support. Basically, if a context is created with an
345 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
346 * will never be used.
347 */
348 static const GLenum back_buffer_modes[] = {
349 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
350 };
351
352 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
353 * enable it.
354 */
355 static const u_int8_t depth_bits_array[4] = { 0, 16, 24, 32 };
356 static const u_int8_t stencil_bits_array[4] = { 0, 0, 8, 0 };
357 const unsigned depth_buffer_factor = 3;
358
359
360 num_modes = depth_buffer_factor * back_buffer_factor * 4;
361
362 if ( pixel_bits == 16 ) {
363 fb_format = GL_RGB;
364 fb_type = GL_UNSIGNED_SHORT_5_6_5;
365 }
366 else {
367 fb_format = GL_BGRA;
368 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
369 }
370
371 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
372 m = modes;
373 if ( ! driFillInModes( & m, fb_format, fb_type,
374 depth_bits_array, stencil_bits_array,
375 depth_buffer_factor,
376 back_buffer_modes, back_buffer_factor,
377 GLX_TRUE_COLOR ) ) {
378 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
379 __func__, __LINE__ );
380 return NULL;
381 }
382
383 if ( ! driFillInModes( & m, fb_format, fb_type,
384 depth_bits_array, stencil_bits_array,
385 depth_buffer_factor,
386 back_buffer_modes, back_buffer_factor,
387 GLX_DIRECT_COLOR ) ) {
388 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
389 __func__, __LINE__ );
390 return NULL;
391 }
392
393 return modes;
394 }
395
396
397 /**
398 * This is the bootstrap function for the driver. libGL supplies all of the
399 * requisite information about the system, and the driver initializes itself.
400 * This routine also fills in the linked list pointed to by \c driver_modes
401 * with the \c __GLcontextModes that the driver can support for windows or
402 * pbuffers.
403 *
404 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
405 * failure.
406 */
407 PUBLIC
408 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn,
409 __DRIscreen *psc,
410 const __GLcontextModes * modes,
411 const __DRIversion * ddx_version,
412 const __DRIversion * dri_version,
413 const __DRIversion * drm_version,
414 const __DRIframebuffer * frame_buffer,
415 drmAddress pSAREA, int fd,
416 int internal_api_version,
417 const __DRIinterfaceMethods * interface,
418 __GLcontextModes ** driver_modes )
419
420 {
421 __DRIscreenPrivate *psp;
422 static const __DRIversion ddx_expected = { 4, 0, 0 };
423 static const __DRIversion dri_expected = { 4, 0, 0 };
424 static const __DRIversion drm_expected = { 2, 3, 0 };
425
426 dri_interface = interface;
427
428 if ( ! driCheckDriDdxDrmVersions2( "Unichrome",
429 dri_version, & dri_expected,
430 ddx_version, & ddx_expected,
431 drm_version, & drm_expected ) ) {
432 return NULL;
433 }
434
435 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
436 ddx_version, dri_version, drm_version,
437 frame_buffer, pSAREA, fd,
438 internal_api_version, &viaAPI);
439 if ( psp != NULL ) {
440 VIADRIPtr dri_priv = (VIADRIPtr) psp->pDevPriv;
441 *driver_modes = viaFillInModes( dri_priv->bytesPerPixel * 8,
442 GL_TRUE );
443
444 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
445 * enable the extensions. It just makes sure that all the dispatch offsets for all
446 * the extensions that *might* be enables are known. This is needed because the
447 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
448 * enable the extensions until we have a context pointer.
449 *
450 * Hello chicken. Hello egg. How are you two today?
451 */
452 driInitExtensions( NULL, card_extensions, GL_FALSE );
453 }
454
455 fprintf(stderr, "%s - succeeded\n", __FUNCTION__);
456 return (void *) psp;
457 }
458
459
460 /**
461 * Get information about previous buffer swaps.
462 */
463 static int
464 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
465 {
466 struct via_context *vmesa;
467
468 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
469 || (dPriv->driContextPriv->driverPrivate == NULL)
470 || (sInfo == NULL) ) {
471 return -1;
472 }
473
474 vmesa = (struct via_context *) dPriv->driContextPriv->driverPrivate;
475 sInfo->swap_count = vmesa->swap_count;
476 sInfo->swap_ust = vmesa->swap_ust;
477 sInfo->swap_missed_count = vmesa->swap_missed_count;
478
479 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
480 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
481 : 0.0;
482
483 return 0;
484 }