53f4c5c56424abb0d8f7ede96c752070bc11b19a
[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 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
102 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
103
104 if (sPriv->devPrivSize != sizeof(VIADRIRec)) {
105 fprintf(stderr,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
106 return GL_FALSE;
107 }
108
109 /* Allocate the private area */
110 viaScreen = (viaScreenPrivate *) CALLOC(sizeof(viaScreenPrivate));
111 if (!viaScreen) {
112 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
113 return GL_FALSE;
114 }
115
116 /* parse information in __driConfigOptions */
117 driParseOptionInfo (&viaScreen->optionCache,
118 __driConfigOptions, __driNConfigOptions);
119
120
121 viaScreen->driScrnPriv = sPriv;
122 sPriv->private = (void *)viaScreen;
123
124 viaScreen->deviceID = gDRIPriv->deviceID;
125 viaScreen->width = gDRIPriv->width;
126 viaScreen->height = gDRIPriv->height;
127 viaScreen->mem = gDRIPriv->mem;
128 viaScreen->bitsPerPixel = gDRIPriv->bytesPerPixel * 8;
129 viaScreen->bytesPerPixel = gDRIPriv->bytesPerPixel;
130 viaScreen->fbOffset = 0;
131 viaScreen->fbSize = gDRIPriv->fbSize;
132 viaScreen->irqEnabled = gDRIPriv->irqEnabled;
133
134 if (VIA_DEBUG & DEBUG_DRI) {
135 fprintf(stderr, "deviceID = %08x\n", viaScreen->deviceID);
136 fprintf(stderr, "width = %08x\n", viaScreen->width);
137 fprintf(stderr, "height = %08x\n", viaScreen->height);
138 fprintf(stderr, "cpp = %08x\n", viaScreen->cpp);
139 fprintf(stderr, "fbOffset = %08x\n", viaScreen->fbOffset);
140 }
141
142 viaScreen->bufs = via_create_empty_buffers();
143 if (viaScreen->bufs == NULL) {
144 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
145 FREE(viaScreen);
146 return GL_FALSE;
147 }
148
149 if (drmMap(sPriv->fd,
150 gDRIPriv->regs.handle,
151 gDRIPriv->regs.size,
152 &viaScreen->reg) != 0) {
153 FREE(viaScreen);
154 sPriv->private = NULL;
155 __driUtilMessage("viaInitDriver: drmMap regs failed");
156 return GL_FALSE;
157 }
158
159 if (gDRIPriv->agp.size) {
160 if (drmMap(sPriv->fd,
161 gDRIPriv->agp.handle,
162 gDRIPriv->agp.size,
163 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
164 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
165 FREE(viaScreen);
166 sPriv->private = NULL;
167 __driUtilMessage("viaInitDriver: drmMap agp failed");
168 return GL_FALSE;
169 }
170
171 viaScreen->agpBase = drmAgpBase(sPriv->fd);
172 } else
173 viaScreen->agpLinearStart = 0;
174
175 viaScreen->sareaPrivOffset = gDRIPriv->sarea_priv_offset;
176
177 if ( glx_enable_extension != NULL ) {
178 if ( viaScreen->irqEnabled ) {
179 (*glx_enable_extension)( sPriv->psc, "GLX_SGI_swap_control" );
180 (*glx_enable_extension)( sPriv->psc, "GLX_SGI_video_sync" );
181 (*glx_enable_extension)( sPriv->psc, "GLX_MESA_swap_control" );
182 }
183
184 (*glx_enable_extension)( sPriv->psc, "GLX_SGI_make_current_read" );
185 (*glx_enable_extension)( sPriv->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 driDestroyOptionInfo(&viaScreen->optionCache);
204
205 FREE(viaScreen);
206 sPriv->private = NULL;
207 }
208
209
210 static GLboolean
211 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
212 __DRIdrawablePrivate *driDrawPriv,
213 const __GLcontextModes *mesaVis,
214 GLboolean isPixmap)
215 {
216 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
217
218 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
219 mesaVis->depthBits != 24);
220 GLboolean swAccum = mesaVis->accumRedBits > 0;
221
222 if (isPixmap) {
223 /* KW: This needs work, disabled for now:
224 */
225 #if 0
226 driDrawPriv->driverPrivate = (void *)
227 _mesa_create_framebuffer(mesaVis,
228 GL_FALSE, /* software depth buffer? */
229 swStencil,
230 mesaVis->accumRedBits > 0,
231 GL_FALSE /* s/w alpha planes */);
232
233 return (driDrawPriv->driverPrivate != NULL);
234 #endif
235 return GL_FALSE;
236 }
237 else {
238 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
239
240 /* The front color, back color and depth renderbuffers are
241 * set up later in calculate_buffer_parameters().
242 * Only create/connect software-based buffers here.
243 */
244
245 #if 000
246 /* This code _should_ be put to use. We have to move the
247 * viaRenderbuffer members out of the via_context structure.
248 * Those members should just be the renderbuffers hanging off the
249 * gl_framebuffer object.
250 */
251 /* XXX check/fix the offset/pitch parameters! */
252 {
253 driRenderbuffer *frontRb
254 = driNewRenderbuffer(GL_RGBA, NULL,
255 screen->bytesPerPixel,
256 0, screen->width, driDrawPriv);
257 viaSetSpanFunctions(frontRb, mesaVis);
258 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
259 }
260
261 if (mesaVis->doubleBufferMode) {
262 driRenderbuffer *backRb
263 = driNewRenderbuffer(GL_RGBA, NULL,
264 screen->bytesPerPixel,
265 0, screen->width, driDrawPriv);
266 viaSetSpanFunctions(backRb, mesaVis);
267 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
268 }
269
270 if (mesaVis->depthBits == 16) {
271 driRenderbuffer *depthRb
272 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, NULL,
273 screen->bytesPerPixel,
274 0, screen->width, driDrawPriv);
275 viaSetSpanFunctions(depthRb, mesaVis);
276 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
277 }
278 else if (mesaVis->depthBits == 24) {
279 driRenderbuffer *depthRb
280 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, NULL,
281 screen->bytesPerPixel,
282 0, screen->width, driDrawPriv);
283 viaSetSpanFunctions(depthRb, mesaVis);
284 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
285 }
286 else if (mesaVis->depthBits == 32) {
287 driRenderbuffer *depthRb
288 = driNewRenderbuffer(GL_DEPTH_COMPONENT32, NULL,
289 screen->bytesPerPixel,
290 0, screen->width, driDrawPriv);
291 viaSetSpanFunctions(depthRb, mesaVis);
292 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
293 }
294
295 if (mesaVis->stencilBits > 0 && !swStencil) {
296 driRenderbuffer *stencilRb
297 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, NULL,
298 screen->bytesPerPixel,
299 0, screen->width, driDrawPriv);
300 viaSetSpanFunctions(stencilRb, mesaVis);
301 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
302 }
303 #endif
304
305 _mesa_add_soft_renderbuffers(fb,
306 GL_FALSE, /* color */
307 GL_FALSE, /* depth */
308 swStencil,
309 swAccum,
310 GL_FALSE, /* alpha */
311 GL_FALSE /* aux */);
312 driDrawPriv->driverPrivate = (void *) fb;
313
314 return (driDrawPriv->driverPrivate != NULL);
315 }
316 }
317
318
319 static void
320 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
321 {
322 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
323 }
324
325
326
327 static struct __DriverAPIRec viaAPI = {
328 .DestroyScreen = viaDestroyScreen,
329 .CreateContext = viaCreateContext,
330 .DestroyContext = viaDestroyContext,
331 .CreateBuffer = viaCreateBuffer,
332 .DestroyBuffer = viaDestroyBuffer,
333 .SwapBuffers = viaSwapBuffers,
334 .MakeCurrent = viaMakeCurrent,
335 .UnbindContext = viaUnbindContext,
336 .GetSwapInfo = getSwapInfo,
337 .GetMSC = driGetMSC32,
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 }