__driConfigOptions must be PUBLIC.
[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 #include "drirenderbuffer.h"
48
49 #include "xmlpool.h"
50
51 PUBLIC const char __driConfigOptions[] =
52 DRI_CONF_BEGIN
53 DRI_CONF_SECTION_PERFORMANCE
54 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
55 DRI_CONF_SECTION_END
56 DRI_CONF_SECTION_QUALITY
57 DRI_CONF_EXCESS_MIPMAP(false)
58 DRI_CONF_SECTION_END
59 DRI_CONF_SECTION_DEBUG
60 DRI_CONF_NO_RAST(false)
61 DRI_CONF_SECTION_END
62 DRI_CONF_END;
63 static const GLuint __driNConfigOptions = 3;
64
65 extern const struct dri_extension card_extensions[];
66
67 static int getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
68
69 static drmBufMapPtr via_create_empty_buffers(void)
70 {
71 drmBufMapPtr retval;
72
73 retval = (drmBufMapPtr)MALLOC(sizeof(drmBufMap));
74 if (retval == NULL) return NULL;
75 memset(retval, 0, sizeof(drmBufMap));
76
77 retval->list = (drmBufPtr)MALLOC(sizeof(drmBuf) * VIA_DMA_BUF_NR);
78 if (retval->list == NULL) {
79 FREE(retval);
80 return NULL;
81 }
82 memset(retval->list, 0, sizeof(drmBuf) * VIA_DMA_BUF_NR);
83 return retval;
84 }
85
86 static void via_free_empty_buffers( drmBufMapPtr bufs )
87 {
88 if (bufs && bufs->list)
89 FREE(bufs->list);
90
91 if (bufs)
92 FREE(bufs);
93 }
94
95
96 static GLboolean
97 viaInitDriver(__DRIscreenPrivate *sPriv)
98 {
99 viaScreenPrivate *viaScreen;
100 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
101 int i;
102
103 if (sPriv->devPrivSize != sizeof(VIADRIRec)) {
104 fprintf(stderr,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
105 return GL_FALSE;
106 }
107
108 /* Allocate the private area */
109 viaScreen = (viaScreenPrivate *) CALLOC(sizeof(viaScreenPrivate));
110 if (!viaScreen) {
111 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
112 return GL_FALSE;
113 }
114
115 /* parse information in __driConfigOptions */
116 driParseOptionInfo (&viaScreen->optionCache,
117 __driConfigOptions, __driNConfigOptions);
118
119
120 viaScreen->driScrnPriv = sPriv;
121 sPriv->private = (void *)viaScreen;
122
123 viaScreen->deviceID = gDRIPriv->deviceID;
124 viaScreen->width = gDRIPriv->width;
125 viaScreen->height = gDRIPriv->height;
126 viaScreen->mem = gDRIPriv->mem;
127 viaScreen->bitsPerPixel = gDRIPriv->bytesPerPixel * 8;
128 viaScreen->bytesPerPixel = gDRIPriv->bytesPerPixel;
129 viaScreen->fbOffset = 0;
130 viaScreen->fbSize = gDRIPriv->fbSize;
131 viaScreen->irqEnabled = gDRIPriv->irqEnabled;
132
133 if (VIA_DEBUG & DEBUG_DRI) {
134 fprintf(stderr, "deviceID = %08x\n", viaScreen->deviceID);
135 fprintf(stderr, "width = %08x\n", viaScreen->width);
136 fprintf(stderr, "height = %08x\n", viaScreen->height);
137 fprintf(stderr, "cpp = %08x\n", viaScreen->cpp);
138 fprintf(stderr, "fbOffset = %08x\n", viaScreen->fbOffset);
139 }
140
141 viaScreen->bufs = via_create_empty_buffers();
142 if (viaScreen->bufs == NULL) {
143 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
144 FREE(viaScreen);
145 return GL_FALSE;
146 }
147
148 if (drmMap(sPriv->fd,
149 gDRIPriv->regs.handle,
150 gDRIPriv->regs.size,
151 &viaScreen->reg) != 0) {
152 FREE(viaScreen);
153 sPriv->private = NULL;
154 __driUtilMessage("viaInitDriver: drmMap regs failed");
155 return GL_FALSE;
156 }
157
158 if (gDRIPriv->agp.size) {
159 if (drmMap(sPriv->fd,
160 gDRIPriv->agp.handle,
161 gDRIPriv->agp.size,
162 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
163 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
164 FREE(viaScreen);
165 sPriv->private = NULL;
166 __driUtilMessage("viaInitDriver: drmMap agp failed");
167 return GL_FALSE;
168 }
169
170 viaScreen->agpBase = drmAgpBase(sPriv->fd);
171 } else
172 viaScreen->agpLinearStart = 0;
173
174 viaScreen->sareaPrivOffset = gDRIPriv->sarea_priv_offset;
175
176 i = 0;
177 viaScreen->extensions[i++] = &driFrameTrackingExtension.base;
178 viaScreen->extensions[i++] = &driReadDrawableExtension;
179 if ( viaScreen->irqEnabled ) {
180 viaScreen->extensions[i++] = &driSwapControlExtension.base;
181 viaScreen->extensions[i++] = &driMediaStreamCounterExtension.base;
182 }
183
184 viaScreen->extensions[i++] = NULL;
185 sPriv->extensions = viaScreen->extensions;
186
187 return GL_TRUE;
188 }
189
190 static void
191 viaDestroyScreen(__DRIscreenPrivate *sPriv)
192 {
193 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
194 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
195
196 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
197 if (gDRIPriv->agp.size)
198 drmUnmap(viaScreen->agpLinearStart, gDRIPriv->agp.size);
199
200 via_free_empty_buffers(viaScreen->bufs);
201
202 driDestroyOptionInfo(&viaScreen->optionCache);
203
204 FREE(viaScreen);
205 sPriv->private = NULL;
206 }
207
208
209 static GLboolean
210 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
211 __DRIdrawablePrivate *driDrawPriv,
212 const __GLcontextModes *mesaVis,
213 GLboolean isPixmap)
214 {
215 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
216
217 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
218 mesaVis->depthBits != 24);
219 GLboolean swAccum = mesaVis->accumRedBits > 0;
220
221 if (isPixmap) {
222 /* KW: This needs work, disabled for now:
223 */
224 #if 0
225 driDrawPriv->driverPrivate = (void *)
226 _mesa_create_framebuffer(mesaVis,
227 GL_FALSE, /* software depth buffer? */
228 swStencil,
229 mesaVis->accumRedBits > 0,
230 GL_FALSE /* s/w alpha planes */);
231
232 return (driDrawPriv->driverPrivate != NULL);
233 #endif
234 return GL_FALSE;
235 }
236 else {
237 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
238
239 /* The front color, back color and depth renderbuffers are
240 * set up later in calculate_buffer_parameters().
241 * Only create/connect software-based buffers here.
242 */
243
244 #if 000
245 /* This code _should_ be put to use. We have to move the
246 * viaRenderbuffer members out of the via_context structure.
247 * Those members should just be the renderbuffers hanging off the
248 * gl_framebuffer object.
249 */
250 /* XXX check/fix the offset/pitch parameters! */
251 {
252 driRenderbuffer *frontRb
253 = driNewRenderbuffer(GL_RGBA, NULL,
254 screen->bytesPerPixel,
255 0, screen->width, driDrawPriv);
256 viaSetSpanFunctions(frontRb, mesaVis);
257 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
258 }
259
260 if (mesaVis->doubleBufferMode) {
261 driRenderbuffer *backRb
262 = driNewRenderbuffer(GL_RGBA, NULL,
263 screen->bytesPerPixel,
264 0, screen->width, driDrawPriv);
265 viaSetSpanFunctions(backRb, mesaVis);
266 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
267 }
268
269 if (mesaVis->depthBits == 16) {
270 driRenderbuffer *depthRb
271 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, NULL,
272 screen->bytesPerPixel,
273 0, screen->width, driDrawPriv);
274 viaSetSpanFunctions(depthRb, mesaVis);
275 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
276 }
277 else if (mesaVis->depthBits == 24) {
278 driRenderbuffer *depthRb
279 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, NULL,
280 screen->bytesPerPixel,
281 0, screen->width, driDrawPriv);
282 viaSetSpanFunctions(depthRb, mesaVis);
283 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
284 }
285 else if (mesaVis->depthBits == 32) {
286 driRenderbuffer *depthRb
287 = driNewRenderbuffer(GL_DEPTH_COMPONENT32, NULL,
288 screen->bytesPerPixel,
289 0, screen->width, driDrawPriv);
290 viaSetSpanFunctions(depthRb, mesaVis);
291 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
292 }
293
294 if (mesaVis->stencilBits > 0 && !swStencil) {
295 driRenderbuffer *stencilRb
296 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, NULL,
297 screen->bytesPerPixel,
298 0, screen->width, driDrawPriv);
299 viaSetSpanFunctions(stencilRb, mesaVis);
300 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
301 }
302 #endif
303
304 _mesa_add_soft_renderbuffers(fb,
305 GL_FALSE, /* color */
306 GL_FALSE, /* depth */
307 swStencil,
308 swAccum,
309 GL_FALSE, /* alpha */
310 GL_FALSE /* aux */);
311 driDrawPriv->driverPrivate = (void *) fb;
312
313 return (driDrawPriv->driverPrivate != NULL);
314 }
315 }
316
317
318 static void
319 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
320 {
321 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
322 }
323
324
325
326 static struct __DriverAPIRec viaAPI = {
327 .DestroyScreen = viaDestroyScreen,
328 .CreateContext = viaCreateContext,
329 .DestroyContext = viaDestroyContext,
330 .CreateBuffer = viaCreateBuffer,
331 .DestroyBuffer = viaDestroyBuffer,
332 .SwapBuffers = viaSwapBuffers,
333 .MakeCurrent = viaMakeCurrent,
334 .UnbindContext = viaUnbindContext,
335 .GetSwapInfo = getSwapInfo,
336 .GetMSC = driGetMSC32,
337 .GetDrawableMSC = driDrawableGetMSC32,
338 .WaitForMSC = driWaitForMSC32,
339 .WaitForSBC = NULL,
340 .SwapBuffersMSC = NULL
341 };
342
343
344 static __GLcontextModes *
345 viaFillInModes( unsigned pixel_bits, GLboolean have_back_buffer )
346 {
347 __GLcontextModes * modes;
348 __GLcontextModes * m;
349 unsigned num_modes;
350 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
351 GLenum fb_format;
352 GLenum fb_type;
353
354 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
355 * enough to add support. Basically, if a context is created with an
356 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
357 * will never be used.
358 */
359 static const GLenum back_buffer_modes[] = {
360 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
361 };
362
363 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
364 * enable it.
365 */
366 static const u_int8_t depth_bits_array[4] = { 0, 16, 24, 32 };
367 static const u_int8_t stencil_bits_array[4] = { 0, 0, 8, 0 };
368 const unsigned depth_buffer_factor = 3;
369
370
371 num_modes = depth_buffer_factor * back_buffer_factor * 4;
372
373 if ( pixel_bits == 16 ) {
374 fb_format = GL_RGB;
375 fb_type = GL_UNSIGNED_SHORT_5_6_5;
376 }
377 else {
378 fb_format = GL_BGRA;
379 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
380 }
381
382 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
383 m = modes;
384 if ( ! driFillInModes( & m, fb_format, fb_type,
385 depth_bits_array, stencil_bits_array,
386 depth_buffer_factor,
387 back_buffer_modes, back_buffer_factor,
388 GLX_TRUE_COLOR ) ) {
389 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
390 __func__, __LINE__ );
391 return NULL;
392 }
393
394 if ( ! driFillInModes( & m, fb_format, fb_type,
395 depth_bits_array, stencil_bits_array,
396 depth_buffer_factor,
397 back_buffer_modes, back_buffer_factor,
398 GLX_DIRECT_COLOR ) ) {
399 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
400 __func__, __LINE__ );
401 return NULL;
402 }
403
404 return modes;
405 }
406
407
408 /**
409 * This is the driver specific part of the createNewScreen entry point.
410 *
411 * \todo maybe fold this into intelInitDriver
412 *
413 * \return the __GLcontextModes supported by this driver
414 */
415 __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
416 {
417 static const __DRIversion ddx_expected = { VIA_DRIDDX_VERSION_MAJOR,
418 VIA_DRIDDX_VERSION_MINOR,
419 VIA_DRIDDX_VERSION_PATCH };
420 static const __DRIversion dri_expected = { 4, 0, 0 };
421 static const __DRIversion drm_expected = { 2, 3, 0 };
422 static const char *driver_name = "Unichrome";
423 VIADRIPtr dri_priv = (VIADRIPtr) psp->pDevPriv;
424
425 if ( ! driCheckDriDdxDrmVersions2( driver_name,
426 &psp->dri_version, & dri_expected,
427 &psp->ddx_version, & ddx_expected,
428 &psp->drm_version, & drm_expected) )
429 return NULL;
430
431 psp->DriverAPI = viaAPI;
432
433 /* Calling driInitExtensions here, with a NULL context pointer,
434 * does not actually enable the extensions. It just makes sure
435 * that all the dispatch offsets for all the extensions that
436 * *might* be enables are known. This is needed because the
437 * dispatch offsets need to be known when _mesa_context_create is
438 * called, but we can't enable the extensions until we have a
439 * context pointer.
440 *
441 * Hello chicken. Hello egg. How are you two today?
442 */
443 driInitExtensions( NULL, card_extensions, GL_FALSE );
444
445 if (!viaInitDriver(psp))
446 return NULL;
447
448 return viaFillInModes( dri_priv->bytesPerPixel * 8, GL_TRUE );
449
450 }
451
452
453 /**
454 * Get information about previous buffer swaps.
455 */
456 static int
457 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
458 {
459 struct via_context *vmesa;
460
461 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
462 || (dPriv->driContextPriv->driverPrivate == NULL)
463 || (sInfo == NULL) ) {
464 return -1;
465 }
466
467 vmesa = (struct via_context *) dPriv->driContextPriv->driverPrivate;
468 sInfo->swap_count = vmesa->swap_count;
469 sInfo->swap_ust = vmesa->swap_ust;
470 sInfo->swap_missed_count = vmesa->swap_missed_count;
471
472 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
473 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
474 : 0.0;
475
476 return 0;
477 }