fix potential NULL dereference (bug 11879)
[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 void * const psc = sPriv->psc->screenConfigs;
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
135 if (VIA_DEBUG & DEBUG_DRI) {
136 fprintf(stderr, "deviceID = %08x\n", viaScreen->deviceID);
137 fprintf(stderr, "width = %08x\n", viaScreen->width);
138 fprintf(stderr, "height = %08x\n", viaScreen->height);
139 fprintf(stderr, "cpp = %08x\n", viaScreen->cpp);
140 fprintf(stderr, "fbOffset = %08x\n", viaScreen->fbOffset);
141 }
142
143 viaScreen->bufs = via_create_empty_buffers();
144 if (viaScreen->bufs == NULL) {
145 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
146 FREE(viaScreen);
147 return GL_FALSE;
148 }
149
150 if (drmMap(sPriv->fd,
151 gDRIPriv->regs.handle,
152 gDRIPriv->regs.size,
153 &viaScreen->reg) != 0) {
154 FREE(viaScreen);
155 sPriv->private = NULL;
156 __driUtilMessage("viaInitDriver: drmMap regs failed");
157 return GL_FALSE;
158 }
159
160 if (gDRIPriv->agp.size) {
161 if (drmMap(sPriv->fd,
162 gDRIPriv->agp.handle,
163 gDRIPriv->agp.size,
164 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
165 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
166 FREE(viaScreen);
167 sPriv->private = NULL;
168 __driUtilMessage("viaInitDriver: drmMap agp failed");
169 return GL_FALSE;
170 }
171
172 viaScreen->agpBase = drmAgpBase(sPriv->fd);
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_SGI_make_current_read" );
186 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
187 }
188
189 return GL_TRUE;
190 }
191
192 static void
193 viaDestroyScreen(__DRIscreenPrivate *sPriv)
194 {
195 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
196 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
197
198 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
199 if (gDRIPriv->agp.size)
200 drmUnmap(viaScreen->agpLinearStart, gDRIPriv->agp.size);
201
202 via_free_empty_buffers(viaScreen->bufs);
203
204 driDestroyOptionInfo(&viaScreen->optionCache);
205
206 FREE(viaScreen);
207 sPriv->private = NULL;
208 }
209
210
211 static GLboolean
212 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
213 __DRIdrawablePrivate *driDrawPriv,
214 const __GLcontextModes *mesaVis,
215 GLboolean isPixmap)
216 {
217 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
218
219 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
220 mesaVis->depthBits != 24);
221 GLboolean swAccum = mesaVis->accumRedBits > 0;
222
223 if (isPixmap) {
224 /* KW: This needs work, disabled for now:
225 */
226 #if 0
227 driDrawPriv->driverPrivate = (void *)
228 _mesa_create_framebuffer(mesaVis,
229 GL_FALSE, /* software depth buffer? */
230 swStencil,
231 mesaVis->accumRedBits > 0,
232 GL_FALSE /* s/w alpha planes */);
233
234 return (driDrawPriv->driverPrivate != NULL);
235 #endif
236 return GL_FALSE;
237 }
238 else {
239 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
240
241 /* The front color, back color and depth renderbuffers are
242 * set up later in calculate_buffer_parameters().
243 * Only create/connect software-based buffers here.
244 */
245
246 #if 000
247 /* This code _should_ be put to use. We have to move the
248 * viaRenderbuffer members out of the via_context structure.
249 * Those members should just be the renderbuffers hanging off the
250 * gl_framebuffer object.
251 */
252 /* XXX check/fix the offset/pitch parameters! */
253 {
254 driRenderbuffer *frontRb
255 = driNewRenderbuffer(GL_RGBA, NULL,
256 screen->bytesPerPixel,
257 0, screen->width, driDrawPriv);
258 viaSetSpanFunctions(frontRb, mesaVis);
259 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
260 }
261
262 if (mesaVis->doubleBufferMode) {
263 driRenderbuffer *backRb
264 = driNewRenderbuffer(GL_RGBA, NULL,
265 screen->bytesPerPixel,
266 0, screen->width, driDrawPriv);
267 viaSetSpanFunctions(backRb, mesaVis);
268 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
269 }
270
271 if (mesaVis->depthBits == 16) {
272 driRenderbuffer *depthRb
273 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, NULL,
274 screen->bytesPerPixel,
275 0, screen->width, driDrawPriv);
276 viaSetSpanFunctions(depthRb, mesaVis);
277 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
278 }
279 else if (mesaVis->depthBits == 24) {
280 driRenderbuffer *depthRb
281 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, NULL,
282 screen->bytesPerPixel,
283 0, screen->width, driDrawPriv);
284 viaSetSpanFunctions(depthRb, mesaVis);
285 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
286 }
287 else if (mesaVis->depthBits == 32) {
288 driRenderbuffer *depthRb
289 = driNewRenderbuffer(GL_DEPTH_COMPONENT32, NULL,
290 screen->bytesPerPixel,
291 0, screen->width, driDrawPriv);
292 viaSetSpanFunctions(depthRb, mesaVis);
293 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
294 }
295
296 if (mesaVis->stencilBits > 0 && !swStencil) {
297 driRenderbuffer *stencilRb
298 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, NULL,
299 screen->bytesPerPixel,
300 0, screen->width, driDrawPriv);
301 viaSetSpanFunctions(stencilRb, mesaVis);
302 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
303 }
304 #endif
305
306 _mesa_add_soft_renderbuffers(fb,
307 GL_FALSE, /* color */
308 GL_FALSE, /* depth */
309 swStencil,
310 swAccum,
311 GL_FALSE, /* alpha */
312 GL_FALSE /* aux */);
313 driDrawPriv->driverPrivate = (void *) fb;
314
315 return (driDrawPriv->driverPrivate != NULL);
316 }
317 }
318
319
320 static void
321 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
322 {
323 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
324 }
325
326
327
328 static struct __DriverAPIRec viaAPI = {
329 .InitDriver = viaInitDriver,
330 .DestroyScreen = viaDestroyScreen,
331 .CreateContext = viaCreateContext,
332 .DestroyContext = viaDestroyContext,
333 .CreateBuffer = viaCreateBuffer,
334 .DestroyBuffer = viaDestroyBuffer,
335 .SwapBuffers = viaSwapBuffers,
336 .MakeCurrent = viaMakeCurrent,
337 .UnbindContext = viaUnbindContext,
338 .GetSwapInfo = getSwapInfo,
339 .GetMSC = driGetMSC32,
340 .WaitForMSC = driWaitForMSC32,
341 .WaitForSBC = NULL,
342 .SwapBuffersMSC = NULL
343 };
344
345
346 static __GLcontextModes *
347 viaFillInModes( unsigned pixel_bits, GLboolean have_back_buffer )
348 {
349 __GLcontextModes * modes;
350 __GLcontextModes * m;
351 unsigned num_modes;
352 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
353 GLenum fb_format;
354 GLenum fb_type;
355
356 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
357 * enough to add support. Basically, if a context is created with an
358 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
359 * will never be used.
360 */
361 static const GLenum back_buffer_modes[] = {
362 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
363 };
364
365 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
366 * enable it.
367 */
368 static const u_int8_t depth_bits_array[4] = { 0, 16, 24, 32 };
369 static const u_int8_t stencil_bits_array[4] = { 0, 0, 8, 0 };
370 const unsigned depth_buffer_factor = 3;
371
372
373 num_modes = depth_buffer_factor * back_buffer_factor * 4;
374
375 if ( pixel_bits == 16 ) {
376 fb_format = GL_RGB;
377 fb_type = GL_UNSIGNED_SHORT_5_6_5;
378 }
379 else {
380 fb_format = GL_BGRA;
381 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
382 }
383
384 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
385 m = modes;
386 if ( ! driFillInModes( & m, fb_format, fb_type,
387 depth_bits_array, stencil_bits_array,
388 depth_buffer_factor,
389 back_buffer_modes, back_buffer_factor,
390 GLX_TRUE_COLOR ) ) {
391 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
392 __func__, __LINE__ );
393 return NULL;
394 }
395
396 if ( ! driFillInModes( & m, fb_format, fb_type,
397 depth_bits_array, stencil_bits_array,
398 depth_buffer_factor,
399 back_buffer_modes, back_buffer_factor,
400 GLX_DIRECT_COLOR ) ) {
401 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
402 __func__, __LINE__ );
403 return NULL;
404 }
405
406 return modes;
407 }
408
409
410 /**
411 * This is the bootstrap function for the driver. libGL supplies all of the
412 * requisite information about the system, and the driver initializes itself.
413 * This routine also fills in the linked list pointed to by \c driver_modes
414 * with the \c __GLcontextModes that the driver can support for windows or
415 * pbuffers.
416 *
417 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
418 * failure.
419 */
420 PUBLIC
421 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn,
422 __DRIscreen *psc,
423 const __GLcontextModes * modes,
424 const __DRIversion * ddx_version,
425 const __DRIversion * dri_version,
426 const __DRIversion * drm_version,
427 const __DRIframebuffer * frame_buffer,
428 drmAddress pSAREA, int fd,
429 int internal_api_version,
430 const __DRIinterfaceMethods * interface,
431 __GLcontextModes ** driver_modes )
432
433 {
434 __DRIscreenPrivate *psp;
435 static const __DRIversion ddx_expected = { VIA_DRIDDX_VERSION_MAJOR,
436 VIA_DRIDDX_VERSION_MINOR,
437 VIA_DRIDDX_VERSION_PATCH };
438 static const __DRIversion dri_expected = { 4, 0, 0 };
439 static const __DRIversion drm_expected = { 2, 3, 0 };
440 static const char *driver_name = "Unichrome";
441
442 dri_interface = interface;
443
444 if ( ! driCheckDriDdxDrmVersions2( driver_name,
445 dri_version, & dri_expected,
446 ddx_version, & ddx_expected,
447 drm_version, & drm_expected) ) {
448 return NULL;
449 }
450
451 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
452 ddx_version, dri_version, drm_version,
453 frame_buffer, pSAREA, fd,
454 internal_api_version, &viaAPI);
455 if ( psp != NULL ) {
456 VIADRIPtr dri_priv = (VIADRIPtr) psp->pDevPriv;
457 *driver_modes = viaFillInModes( dri_priv->bytesPerPixel * 8,
458 GL_TRUE );
459
460 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
461 * enable the extensions. It just makes sure that all the dispatch offsets for all
462 * the extensions that *might* be enables are known. This is needed because the
463 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
464 * enable the extensions until we have a context pointer.
465 *
466 * Hello chicken. Hello egg. How are you two today?
467 */
468 driInitExtensions( NULL, card_extensions, GL_FALSE );
469 }
470
471 return (void *) psp;
472 }
473
474
475 /**
476 * Get information about previous buffer swaps.
477 */
478 static int
479 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
480 {
481 struct via_context *vmesa;
482
483 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
484 || (dPriv->driContextPriv->driverPrivate == NULL)
485 || (sInfo == NULL) ) {
486 return -1;
487 }
488
489 vmesa = (struct via_context *) dPriv->driContextPriv->driverPrivate;
490 sInfo->swap_count = vmesa->swap_count;
491 sInfo->swap_ust = vmesa->swap_ust;
492 sInfo->swap_missed_count = vmesa->swap_missed_count;
493
494 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
495 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
496 : 0.0;
497
498 return 0;
499 }