1 /*****************************************************************************
2 * vout_mga.c: MGA video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
31 #include <fcntl.h> /* open() */
32 #include <sys/ioctl.h> /* ioctl() */
33 #include <sys/mman.h> /* PROT_WRITE */
36 #include <sys/types.h> /* typedef ushort */
39 #include <sys/shm.h> /* shmget(), shmctl() */
41 #include <X11/Xutil.h>
42 #include <X11/extensions/XShm.h>
51 #include "video_output.h"
57 /*****************************************************************************
58 * vout_MGACreate: allocate X11 video thread output method
59 *****************************************************************************
60 * This function allocate and initialize a X11 vout method. It uses some of the
61 * vout properties to choose the window size, and change them according to the
62 * actual properties of the display.
63 *****************************************************************************/
64 int vout_MGACreate( vout_thread_t *p_vout, char *psz_display,
65 int i_root_window, void *p_data )
67 /* Allocate structure */
68 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
69 if( p_vout->p_sys == NULL )
71 intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
75 /* Allocate MGA configuration structure */
76 p_vout->p_sys->p_mga = malloc( sizeof( mga_vid_config_t ) );
77 if( p_vout->p_sys->p_mga == NULL )
79 intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
80 free( p_vout->p_sys );
84 if( (p_vout->p_sys->i_fd = open("/dev/mga_vid",O_RDWR)) == -1 )
86 intf_ErrMsg("error: can't open MGA driver /dev/mga_vid\n" );
87 free( p_vout->p_sys->p_mga );
88 free( p_vout->p_sys );
92 /* Open and initialize device. This function issues its own error messages.
93 * Since XLib is usually not thread-safe, we can't use the same display
94 * pointer than the interface or another thread. However, the root window
95 * id is still valid. */
96 if( X11OpenDisplay( p_vout, psz_display, i_root_window ) )
98 intf_ErrMsg("error: can't initialize X11 display\n" );
99 free( p_vout->p_sys->p_mga );
100 free( p_vout->p_sys );
107 /*****************************************************************************
108 * vout_MGAInit: initialize X11 video thread output method
109 *****************************************************************************
110 * This function create the XImages needed by the output thread. It is called
111 * at the beginning of the thread, but also each time the window is resized.
112 *****************************************************************************/
113 int vout_MGAInit( vout_thread_t *p_vout )
117 /* create the MGA output */
118 p_vout->p_sys->p_mga->src_width = p_vout->i_width;
119 p_vout->p_sys->p_mga->src_height = p_vout->i_height;
120 /* FIXME: we should initialize these ones according to the streams */
121 p_vout->p_sys->p_mga->dest_width = p_vout->i_width;
122 p_vout->p_sys->p_mga->dest_height = p_vout->i_height;
123 //p_vout->p_sys->p_mga->dest_width = 400;
124 //p_vout->p_sys->p_mga->dest_height = 300;
125 p_vout->p_sys->p_mga->x_org = 100;
126 p_vout->p_sys->p_mga->y_org = 100;
127 p_vout->p_sys->p_mga->colkey_on = 0;
129 if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, p_vout->p_sys->p_mga) )
131 intf_ErrMsg("error in config ioctl\n");
134 if (p_vout->p_sys->p_mga->card_type == MGA_G200)
136 intf_Msg( "detected MGA G200 (%d MB Ram)\n",
137 p_vout->p_sys->p_mga->ram_size );
138 p_vout->p_sys->b_g400 = 0;
142 intf_Msg( "detected MGA G400 (%d MB Ram)\n",
143 p_vout->p_sys->p_mga->ram_size );
144 p_vout->p_sys->b_g400 = 1;
147 ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
149 p_vout->p_sys->i_size = ( (p_vout->p_sys->p_mga->src_width + 31) & ~31 )
150 * p_vout->p_sys->p_mga->src_height;
152 p_vout->p_sys->p_mga_vid_base = mmap( 0, p_vout->p_sys->i_size
153 + p_vout->p_sys->i_size / 2,
154 PROT_WRITE, MAP_SHARED,
155 p_vout->p_sys->i_fd, 0 );
157 memset( p_vout->p_sys->p_mga_vid_base,
158 0x00, p_vout->p_sys->i_size );
160 memset( p_vout->p_sys->p_mga_vid_base + p_vout->p_sys->i_size,
161 0x80, p_vout->p_sys->i_size / 2 );
163 /* Create XImages using XShm extension - on failure, fall back to regular
164 * way (and destroy the first image if it was created successfully) */
165 if( p_vout->p_sys->b_shm )
167 /* Create first image */
168 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
169 &p_vout->p_sys->shm_info[0] );
170 if( !i_err ) /* first image has been created */
172 /* Create second image */
173 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
174 &p_vout->p_sys->shm_info[1] ) )
175 { /* error creating the second image */
176 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
177 &p_vout->p_sys->shm_info[0] );
181 if( i_err ) /* an error occured */
183 intf_Msg("XShm video sextension deactivated\n" );
184 p_vout->p_sys->b_shm = 0;
188 /* Create XImages without XShm extension */
189 if( !p_vout->p_sys->b_shm )
191 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
193 intf_ErrMsg("error: can't create images\n");
194 p_vout->p_sys->p_ximage[0] = NULL;
195 p_vout->p_sys->p_ximage[1] = NULL;
198 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
200 intf_ErrMsg("error: can't create images\n");
201 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
202 p_vout->p_sys->p_ximage[0] = NULL;
203 p_vout->p_sys->p_ximage[1] = NULL;
208 /* Set bytes per line and initialize buffers */
209 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
210 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
211 p_vout->p_sys->p_ximage[ 1 ]->data );
215 /*****************************************************************************
216 * vout_MGAEnd: terminate X11 video thread output method
217 *****************************************************************************
218 * Destroy the X11 XImages created by vout_MGAInit. It is called at the end of
219 * the thread, but also each time the window is resized.
220 *****************************************************************************/
221 void vout_MGAEnd( vout_thread_t *p_vout )
223 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
225 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
226 &p_vout->p_sys->shm_info[0] );
227 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
228 &p_vout->p_sys->shm_info[1] );
230 else /* ...or regular XImages */
232 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
233 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
237 /*****************************************************************************
238 * vout_MGADestroy: destroy X11 video thread output method
239 *****************************************************************************
240 * Terminate an output method created by vout_CreateOutputMethod
241 *****************************************************************************/
242 void vout_MGADestroy( vout_thread_t *p_vout )
244 X11CloseDisplay( p_vout );
246 ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
247 close( p_vout->p_sys->i_fd );
249 free( p_vout->p_sys->p_mga );
250 free( p_vout->p_sys );
253 /*****************************************************************************
254 * vout_MGAManage: handle X11 events
255 *****************************************************************************
256 * This function should be called regularly by video output thread. It manages
257 * X11 events and allows window resizing. It returns a non null value on
259 *****************************************************************************/
260 int vout_MGAManage( vout_thread_t *p_vout )
263 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
265 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE) && (p_vout->i_screen_depth == 8) )
267 /* FIXME: clear flags ?? */
273 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
275 intf_DbgMsg("resizing window\n");
276 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
279 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
280 p_vout->i_width, p_vout->i_height );
282 /* Destroy XImages to change their size */
283 vout_MGAEnd( p_vout );
285 /* Recreate XImages. If SysInit failed, the thread can't go on. */
286 if( vout_MGAInit( p_vout ) )
288 intf_ErrMsg("error: can't resize display\n");
292 /* Tell the video output thread that it will need to rebuild YUV
293 * tables. This is needed since convertion buffer size may have changed */
294 p_vout->i_changes |= VOUT_YUV_CHANGE;
295 intf_Msg("Video display resized (%dx%d)\n", p_vout->i_width, p_vout->i_height);
301 /*****************************************************************************
302 * vout_MGADisplay: displays previously rendered output
303 *****************************************************************************
304 * This function send the currently rendered image to X11 server, wait until
305 * it is displayed and switch the two rendering buffer, preparing next frame.
306 *****************************************************************************/
307 void vout_MGADisplay( vout_thread_t *p_vout )
309 if( p_vout->p_sys->b_shm) /* XShm is used */
311 /* Display rendered image using shared memory extension */
312 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
313 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
315 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
316 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
318 /* Send the order to the X server */
319 XFlush(p_vout->p_sys->p_display);
321 else /* regular X11 capabilities are used */
323 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
324 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
326 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
327 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
329 /* Send the order to the X server */
330 XFlush(p_vout->p_sys->p_display);
334 /* following functions are local */
336 /*****************************************************************************
337 * X11OpenDisplay: open and initialize X11 device
338 *****************************************************************************
339 * Create a window according to video output given size, and set other
340 * properties according to the display properties.
341 *****************************************************************************/
342 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window )
344 XPixmapFormatValues * p_xpixmap_format; /* pixmap formats */
345 XVisualInfo * p_xvisual; /* visuals informations */
346 XVisualInfo xvisual_template; /* visual template */
347 int i_count; /* array size */
350 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
351 if( p_vout->p_sys->p_display == NULL )
353 intf_ErrMsg("error: can't open display %s\n", psz_display );
357 /* Initialize structure */
358 p_vout->p_sys->root_window = root_window;
359 p_vout->p_sys->b_shm = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
360 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
361 if( !p_vout->p_sys->b_shm )
363 intf_Msg("XShm video extension is not available\n");
366 /* Get screen depth */
367 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
368 switch( p_vout->i_screen_depth )
372 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
374 xvisual_template.screen = p_vout->p_sys->i_screen;
375 xvisual_template.class = DirectColor;
376 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
377 &xvisual_template, &i_count );
378 if( p_xvisual == NULL )
380 intf_ErrMsg("error: no PseudoColor visual available\n");
381 XCloseDisplay( p_vout->p_sys->p_display );
384 /* FIXME: SetColormap; ?? */
385 p_vout->i_bytes_per_pixel = 1;
392 * Screen depth is higher than 8bpp. TrueColor visual is used.
394 xvisual_template.screen = p_vout->p_sys->i_screen;
395 xvisual_template.class = TrueColor;
396 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
397 &xvisual_template, &i_count );
398 if( p_xvisual == NULL )
400 intf_ErrMsg("error: no TrueColor visual available\n");
401 XCloseDisplay( p_vout->p_sys->p_display );
404 p_vout->i_red_mask = p_xvisual->red_mask;
405 p_vout->i_green_mask = p_xvisual->green_mask;
406 p_vout->i_blue_mask = p_xvisual->blue_mask;
408 /* There is no difference yet between 3 and 4 Bpp. The only way to find
409 * the actual number of bytes per pixel is to list supported pixmap
411 p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
412 p_vout->i_bytes_per_pixel = 0;
413 for( ; i_count--; p_xpixmap_format++ )
415 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
417 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
422 p_vout->p_sys->p_visual = p_xvisual->visual;
425 /* Create a window */
426 if( X11CreateWindow( p_vout ) )
428 intf_ErrMsg("error: can't open a window\n");
429 XCloseDisplay( p_vout->p_sys->p_display );
435 /*****************************************************************************
436 * X11CloseDisplay: close X11 device
437 *****************************************************************************
438 * Returns all resources allocated by X11OpenDisplay and restore the original
439 * state of the display.
440 *****************************************************************************/
441 static void X11CloseDisplay( vout_thread_t *p_vout )
443 /* Destroy colormap */
444 if( p_vout->i_screen_depth == 8 )
446 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
449 /* Destroy window and close display */
450 X11DestroyWindow( p_vout );
451 XCloseDisplay( p_vout->p_sys->p_display );
454 /*****************************************************************************
455 * X11CreateWindow: create X11 vout window
456 *****************************************************************************
457 * The video output window will be created. Normally, this window is wether
458 * full screen or part of a parent window. Therefore, it does not need a
459 * title or other hints. Thery are still supplied in case the window would be
460 * spawned as a standalone one by the interface.
461 *****************************************************************************/
462 static int X11CreateWindow( vout_thread_t *p_vout )
464 XSetWindowAttributes xwindow_attributes; /* window attributes */
465 XGCValues xgcvalues; /* graphic context configuration */
466 XEvent xevent; /* first events */
467 boolean_t b_expose; /* 'expose' event received */
468 boolean_t b_map_notify; /* 'map_notify' event received */
470 /* Prepare window attributes */
471 xwindow_attributes.backing_store = Always; /* save the hidden part */
473 /* Create the window and set hints */
474 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
475 p_vout->p_sys->root_window,
477 p_vout->i_width, p_vout->i_height,
479 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
480 ExposureMask | StructureNotifyMask );
481 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
482 CWBackingStore, &xwindow_attributes);
484 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
485 when using functions like XCopyArea */
486 xgcvalues.graphics_exposures = False;
487 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
488 GCGraphicsExposures, &xgcvalues);
490 /* Send orders to server, and wait until window is displayed - two events
491 * must be received: a MapNotify event, an Expose event allowing drawing in the
495 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
498 XNextEvent( p_vout->p_sys->p_display, &xevent);
499 if( (xevent.type == Expose)
500 && (xevent.xexpose.window == p_vout->p_sys->window) )
504 else if( (xevent.type == MapNotify)
505 && (xevent.xmap.window == p_vout->p_sys->window) )
510 while( !( b_expose && b_map_notify ) );
511 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
513 /* At this stage, the window is openned, displayed, and ready to receive
518 /*****************************************************************************
519 * X11DestroyWindow: destroy X11 window
520 *****************************************************************************
521 * Destroy an X11 window created by vout_CreateWindow
522 *****************************************************************************/
523 static void X11DestroyWindow( vout_thread_t *p_vout )
525 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
526 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
527 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
530 /*****************************************************************************
531 * X11CreateImage: create an XImage
532 *****************************************************************************
533 * Create a simple XImage used as a buffer.
534 *****************************************************************************/
535 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
537 byte_t * pb_data; /* image data storage zone */
538 int i_quantum; /* XImage quantum (see below) */
540 /* Allocate memory for image */
541 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
542 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
543 if( !pb_data ) /* error */
545 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
549 /* Optimize the quantum of a scanline regarding its size - the quantum is
550 a diviser of the number of bits between the start of two scanlines. */
551 if( !(( p_vout->i_bytes_per_line ) % 32) )
557 if( !(( p_vout->i_bytes_per_line ) % 16) )
568 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
569 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
570 p_vout->i_width, p_vout->i_height, i_quantum, 0);
571 if(! *pp_ximage ) /* error */
573 intf_ErrMsg( "error: XCreateImage() failed\n" );
581 /*****************************************************************************
582 * X11CreateShmImage: create an XImage using shared memory extension
583 *****************************************************************************
584 * Prepare an XImage for DisplayX11ShmImage function.
585 * The order of the operations respects the recommandations of the mit-shm
586 * document by J.Corbet and K.Packard. Most of the parameters were copied from
588 *****************************************************************************/
589 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
590 XShmSegmentInfo *p_shm_info)
593 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
594 p_vout->i_screen_depth, ZPixmap, 0,
595 p_shm_info, p_vout->i_width, p_vout->i_height );
596 if(! *pp_ximage ) /* error */
598 intf_ErrMsg("error: XShmCreateImage() failed\n");
602 /* Allocate shared memory segment - 0777 set the access permission
603 * rights (like umask), they are not yet supported by X servers */
604 p_shm_info->shmid = shmget( IPC_PRIVATE,
605 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
607 if( p_shm_info->shmid < 0) /* error */
609 intf_ErrMsg("error: can't allocate shared image data (%s)\n",
611 XDestroyImage( *pp_ximage );
615 /* Attach shared memory segment to process (read/write) */
616 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
617 if(! p_shm_info->shmaddr )
619 intf_ErrMsg("error: can't attach shared memory (%s)\n",
621 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
622 XDestroyImage( *pp_ximage );
626 /* Mark the shm segment to be removed when there will be no more
627 * attachements, so it is automatic on process exit or after shmdt */
628 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
630 /* Attach shared memory segment to X server (read only) */
631 p_shm_info->readOnly = True;
632 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
634 intf_ErrMsg("error: can't attach shared memory to X11 server\n");
635 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
636 * and automatic free */
637 XDestroyImage( *pp_ximage );
641 /* Send image to X server. This instruction is required, since having
642 * built a Shm XImage and not using it causes an error on XCloseDisplay */
643 XFlush( p_vout->p_sys->p_display );
647 /*****************************************************************************
648 * X11DestroyImage: destroy an XImage
649 *****************************************************************************
650 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
651 * destroyed (see vout_ManageOutputMethod())
652 *****************************************************************************/
653 static void X11DestroyImage( XImage *p_ximage )
655 if( p_ximage != NULL )
657 XDestroyImage( p_ximage ); /* no free() required */
661 /*****************************************************************************
663 *****************************************************************************
664 * Destroy XImage AND associated data. Detach shared memory segment from
665 * server and process, then free it. If pointer is NULL, the image won't be
666 * destroyed (see vout_ManageOutputMethod())
667 *****************************************************************************/
668 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
669 XShmSegmentInfo *p_shm_info )
671 /* If pointer is NULL, do nothing */
672 if( p_ximage == NULL )
677 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
678 XDestroyImage( p_ximage );
679 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
680 { /* also automatic freeing... */
681 intf_ErrMsg("error: can't detach shared memory (%s)\n",