Merge branch '965-glsl'
[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 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 .WaitForMSC = driWaitForMSC32,
338 .WaitForSBC = NULL,
339 .SwapBuffersMSC = NULL
340 };
341
342
343 static __GLcontextModes *
344 viaFillInModes( unsigned pixel_bits, GLboolean have_back_buffer )
345 {
346 __GLcontextModes * modes;
347 __GLcontextModes * m;
348 unsigned num_modes;
349 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
350 GLenum fb_format;
351 GLenum fb_type;
352
353 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
354 * enough to add support. Basically, if a context is created with an
355 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
356 * will never be used.
357 */
358 static const GLenum back_buffer_modes[] = {
359 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
360 };
361
362 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
363 * enable it.
364 */
365 static const u_int8_t depth_bits_array[4] = { 0, 16, 24, 32 };
366 static const u_int8_t stencil_bits_array[4] = { 0, 0, 8, 0 };
367 const unsigned depth_buffer_factor = 3;
368
369
370 num_modes = depth_buffer_factor * back_buffer_factor * 4;
371
372 if ( pixel_bits == 16 ) {
373 fb_format = GL_RGB;
374 fb_type = GL_UNSIGNED_SHORT_5_6_5;
375 }
376 else {
377 fb_format = GL_BGRA;
378 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
379 }
380
381 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
382 m = modes;
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_TRUE_COLOR ) ) {
388 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
389 __func__, __LINE__ );
390 return NULL;
391 }
392
393 if ( ! driFillInModes( & m, fb_format, fb_type,
394 depth_bits_array, stencil_bits_array,
395 depth_buffer_factor,
396 back_buffer_modes, back_buffer_factor,
397 GLX_DIRECT_COLOR ) ) {
398 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
399 __func__, __LINE__ );
400 return NULL;
401 }
402
403 return modes;
404 }
405
406
407 /**
408 * This is the driver specific part of the createNewScreen entry point.
409 *
410 * \todo maybe fold this into intelInitDriver
411 *
412 * \return the __GLcontextModes supported by this driver
413 */
414 __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
415 {
416 static const __DRIversion ddx_expected = { VIA_DRIDDX_VERSION_MAJOR,
417 VIA_DRIDDX_VERSION_MINOR,
418 VIA_DRIDDX_VERSION_PATCH };
419 static const __DRIversion dri_expected = { 4, 0, 0 };
420 static const __DRIversion drm_expected = { 2, 3, 0 };
421 static const char *driver_name = "Unichrome";
422 VIADRIPtr dri_priv = (VIADRIPtr) psp->pDevPriv;
423
424 if ( ! driCheckDriDdxDrmVersions2( driver_name,
425 &psp->dri_version, & dri_expected,
426 &psp->ddx_version, & ddx_expected,
427 &psp->drm_version, & drm_expected) )
428 return NULL;
429
430 psp->DriverAPI = viaAPI;
431
432 /* Calling driInitExtensions here, with a NULL context pointer,
433 * does not actually enable the extensions. It just makes sure
434 * that all the dispatch offsets for all the extensions that
435 * *might* be enables are known. This is needed because the
436 * dispatch offsets need to be known when _mesa_context_create is
437 * called, but we can't enable the extensions until we have a
438 * context pointer.
439 *
440 * Hello chicken. Hello egg. How are you two today?
441 */
442 driInitExtensions( NULL, card_extensions, GL_FALSE );
443
444 if (!viaInitDriver(psp))
445 return NULL;
446
447 return viaFillInModes( dri_priv->bytesPerPixel * 8, GL_TRUE );
448
449 }
450
451
452 /**
453 * Get information about previous buffer swaps.
454 */
455 static int
456 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
457 {
458 struct via_context *vmesa;
459
460 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
461 || (dPriv->driContextPriv->driverPrivate == NULL)
462 || (sInfo == NULL) ) {
463 return -1;
464 }
465
466 vmesa = (struct via_context *) dPriv->driContextPriv->driverPrivate;
467 sInfo->swap_count = vmesa->swap_count;
468 sInfo->swap_ust = vmesa->swap_ust;
469 sInfo->swap_missed_count = vmesa->swap_missed_count;
470
471 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
472 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
473 : 0.0;
474
475 return 0;
476 }