1 /*****************************************************************************
2 * omapfb.c : omap framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008-2009 the VideoLAN team
7 * Authors: Antoine Lejeune <phytos @ videolan.org>
8 * Based on fb.c and work of Siarhei Siamashka on mplayer for Maemo
9 * Needs a recent omapfb.h to compile
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <fcntl.h> /* open() */
32 #include <unistd.h> /* close() */
34 #include <sys/ioctl.h>
35 #include <sys/mman.h> /* mmap() */
38 #include <asm/arch-omap/omapfb.h>
40 /* Embedded window handling */
42 #include <X11/Xutil.h>
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
47 #include <vlc_vout_window.h>
48 #include <vlc_playlist.h>
49 #include <vlc_charset.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Create ( vlc_object_t * );
55 static void Destroy ( vlc_object_t * );
57 static int Init ( vout_thread_t * );
58 static void End ( vout_thread_t * );
59 static int Manage ( vout_thread_t * );
60 static void DisplayVideo ( vout_thread_t *, picture_t * );
61 static int Control ( vout_thread_t *, int, va_list );
63 static void FreePicture ( vout_thread_t *, picture_t * );
65 static int OpenDisplay ( vout_thread_t * );
66 static void CloseDisplay ( vout_thread_t * );
67 static void UpdateScreen ( vout_thread_t *,
68 int, int, int, int, int, int, int );
70 static int InitWindow ( vout_thread_t * );
71 static void CreateWindow ( vout_sys_t * );
72 static void ToggleFullScreen ( vout_thread_t * );
74 /*****************************************************************************
76 *****************************************************************************/
77 #define FB_DEV_VAR "omapfbdev"
79 #define DEVICE_TEXT N_("OMAP Framebuffer device")
80 #define DEVICE_LONGTEXT N_( \
81 "OMAP Framebuffer device to use for rendering (usually /dev/fb0).")
83 #define CHROMA_TEXT N_("Chroma used.")
84 #define CHROMA_LONGTEXT N_( \
85 "Force use of a specific chroma for output. Default is Y420 (specific to N770/N8xx hardware)." )
87 #define OVERLAY_TEXT N_("Embed the overlay")
88 #define OVERLAY_LONGTEXT N_( \
89 "Embed the framebuffer overlay into a X11 window" )
92 set_shortname( "OMAP framebuffer" );
93 set_category( CAT_VIDEO );
94 set_subcategory( SUBCAT_VIDEO_VOUT );
95 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
97 add_string( "omap-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
99 add_bool( "omap-embedded", true, NULL, OVERLAY_TEXT, OVERLAY_LONGTEXT,
101 set_description( N_("OMAP framebuffer video output") );
102 set_capability( "video output", 200 );
103 set_callbacks( Create, Destroy );
106 /*****************************************************************************
107 * omap_window_t: simple structure with the geometry of a window
108 *****************************************************************************/
117 /*****************************************************************************
118 * vout_sys_t: video output omap framebuffer method descriptor
119 *****************************************************************************
120 * This structure is part of the video output thread descriptor.
121 * It describes the FB specific properties of an output thread.
122 *****************************************************************************/
125 /* Framebuffer information */
126 int i_fd; /* device handle */
127 struct fb_var_screeninfo fb_vinfo; /* current mode information */
128 struct fb_fix_screeninfo fb_finfo;
129 struct omapfb_caps caps;
132 /* Window information */
133 struct omap_window_t output_window; /* Size of the real output window */
134 struct omap_window_t main_window; /* Size of the area we got to display */
135 struct omap_window_t embedded_window; /* Size of the embedded window */
137 /* Video information */
138 uint32_t i_video_width; /* video width */
139 uint32_t i_video_height; /* video height */
140 vlc_fourcc_t i_chroma;
141 int i_color_format; /* OMAPFB_COLOR_* */
142 bool b_video_enabled; /* Video must be displayed? */
143 picture_t *p_output_picture;
146 uint8_t *p_video; /* base adress */
147 uint8_t *p_center; /* output adress */
148 size_t i_page_size; /* page size */
149 int i_bytes_per_pixel; /* Bytes used by one pixel */
150 int i_line_len; /* Length of one line in bytes */
154 vout_window_t *owner_window;
156 mtime_t i_time_button_last_pressed; /* To detect double click */
163 /*****************************************************************************
164 * Create: allocates omapfb video thread output method
165 *****************************************************************************
166 * This function allocates and initializes a FB vout method.
167 *****************************************************************************/
168 static int Create( vlc_object_t *p_this )
170 vout_thread_t *p_vout = (vout_thread_t *)p_this;
173 if( p_vout->fmt_in.i_chroma != VLC_CODEC_I420 &&
174 p_vout->fmt_in.i_chroma != VLC_CODEC_YV12 )
177 /* Allocate instance and initialize some members */
178 p_vout->p_sys = p_sys = calloc( 1, sizeof( vout_sys_t ) );
179 if( p_vout->p_sys == NULL )
182 p_vout->pf_init = Init;
183 p_vout->pf_end = End;
184 p_vout->pf_manage = Manage;
185 p_vout->pf_render = NULL;
186 p_vout->pf_display = DisplayVideo;
187 p_vout->pf_control = Control;
188 p_sys->b_video_enabled = true;
190 if( OpenDisplay( p_vout ) )
192 free( p_vout->p_sys );
196 if( InitWindow( p_vout ) )
198 free( p_vout->p_sys );
205 /*****************************************************************************
206 * Destroy: destroy omapfb video thread output method
207 *****************************************************************************
208 * Terminate an output method created by Create
209 *****************************************************************************/
210 static void Destroy( vlc_object_t *p_this )
212 vout_thread_t *p_vout = (vout_thread_t *)p_this;
214 CloseDisplay( p_vout );
216 if( p_vout->p_sys->owner_window )
218 vout_window_Delete( p_vout->p_sys->owner_window );
219 XCloseDisplay( p_vout->p_sys->p_display );
222 /* Destroy structure */
223 free( p_vout->p_sys );
227 /*****************************************************************************
228 * Init: initialize omap framebuffer video thread output method
229 *****************************************************************************/
230 static int Init( vout_thread_t *p_vout )
232 vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
234 // We want to keep the same aspect
235 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect = p_vout->render.i_aspect;
236 // We ask where the video should be displayed in the video area
237 vout_PlacePicture( p_vout, p_sys->main_window.i_width,
238 p_sys->main_window.i_height,
239 &p_sys->output_window.i_x,
240 &p_sys->output_window.i_y,
241 &p_sys->output_window.i_width,
242 &p_sys->output_window.i_height );
243 p_sys->output_window.i_x = ( p_sys->output_window.i_x +
244 p_sys->main_window.i_x ) & ~1;
245 p_sys->output_window.i_y = ( p_sys->output_window.i_y +
246 p_sys->main_window.i_y ) & ~1;
248 // Hardware upscaling better than software
249 if( p_vout->fmt_render.i_width <= p_sys->main_window.i_width &&
250 p_vout->fmt_render.i_height <= p_sys->main_window.i_height )
252 p_sys->i_video_width =
253 p_vout->output.i_width =
254 p_vout->fmt_out.i_width =
255 p_vout->fmt_out.i_visible_width = p_vout->fmt_render.i_width;
256 p_sys->i_video_height =
257 p_vout->output.i_height =
258 p_vout->fmt_out.i_height =
259 p_vout->fmt_out.i_visible_height = p_vout->fmt_render.i_height;
263 p_sys->i_video_width =
264 p_vout->output.i_width =
265 p_vout->fmt_out.i_width =
266 p_vout->fmt_out.i_visible_width = p_sys->output_window.i_width;
267 p_sys->i_video_height =
268 p_vout->output.i_height =
269 p_vout->fmt_out.i_height =
270 p_vout->fmt_out.i_visible_height = p_sys->output_window.i_height;
273 p_vout->output.i_chroma =
274 p_vout->fmt_out.i_chroma = VLC_FOURCC( 'Y','4','2','0' );
275 p_sys->i_color_format = OMAPFB_COLOR_YUV420;
277 // place in the framebuffer where we have to write
278 p_sys->p_center = p_sys->p_video + p_sys->output_window.i_x*p_sys->i_bytes_per_pixel
279 + p_sys->output_window.i_y*p_sys->i_line_len;
281 // We get and set a direct render vlc picture
282 I_OUTPUTPICTURES = 0;
283 picture_t *p_pic = NULL;
286 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
288 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
290 p_pic = p_vout->p_picture + i_index;
295 /* Allocate the picture */
301 p_sys->p_output_picture = p_pic;
302 p_pic->p->p_pixels = p_vout->p_sys->p_center;
303 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
304 p_pic->p->i_lines = p_sys->i_video_height;
305 p_pic->p->i_visible_lines = p_sys->i_video_height;
306 p_pic->p->i_pitch = p_sys->i_line_len;
307 p_pic->p->i_visible_pitch = p_sys->i_line_len;
309 p_pic->i_status = DESTROYED_PICTURE;
310 p_pic->i_type = DIRECT_PICTURE;
312 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
319 /*****************************************************************************
320 * End: terminate omap framebuffer video thread output method
321 *****************************************************************************/
322 static void End( vout_thread_t *p_vout )
324 /* Clear the screen */
325 UpdateScreen( p_vout, 0, 0,
326 p_vout->p_sys->fb_vinfo.xres,
327 p_vout->p_sys->fb_vinfo.yres,
328 p_vout->p_sys->fb_vinfo.xres,
329 p_vout->p_sys->fb_vinfo.yres,
330 OMAPFB_COLOR_RGB565 );
333 /*****************************************************************************
334 * Control: control facility for the vout
335 *****************************************************************************/
336 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
341 /*****************************************************************************
342 * FreePicture: Destroy the picture and make it free again
343 ******************************************************************************/
344 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
346 p_pic->p->p_pixels = NULL;
347 p_pic->i_status = FREE_PICTURE;
350 /*****************************************************************************
351 * Manage: handle omapfb events
352 *****************************************************************************
353 * This function should be called regularly by video output thread.
354 *****************************************************************************/
355 static int Manage( vout_thread_t *p_vout )
359 while( XPending( p_vout->p_sys->p_display ) )
361 XNextEvent( p_vout->p_sys->p_display, &xevent );
363 if( xevent.type == ButtonPress &&
364 ((XButtonEvent *)&xevent)->button == Button1 )
366 /* detect double-clicks */
367 if( ( ((XButtonEvent *)&xevent)->time -
368 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
370 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
373 p_vout->p_sys->i_time_button_last_pressed =
374 ((XButtonEvent *)&xevent)->time;
376 else if( ( xevent.type == VisibilityNotify &&
377 xevent.xvisibility.state != VisibilityUnobscured ) ||
378 xevent.type == UnmapNotify )
380 UpdateScreen( p_vout, 0, 0,
381 p_vout->p_sys->fb_vinfo.xres,
382 p_vout->p_sys->fb_vinfo.yres,
383 p_vout->p_sys->fb_vinfo.xres,
384 p_vout->p_sys->fb_vinfo.yres,
385 OMAPFB_COLOR_RGB565 );
386 p_vout->p_sys->b_video_enabled = false;
387 p_vout->p_sys->p_output_picture->p->p_pixels =
388 p_vout->p_sys->p_null;
392 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
394 /* Update the object variable and trigger callback */
395 p_vout->b_fullscreen = !p_vout->b_fullscreen;
396 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
398 if( p_vout->p_sys->owner_window )
399 vout_window_SetFullscreen( p_vout->p_sys->owner_window,
400 p_vout->b_fullscreen );
401 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
404 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
406 FreePicture( p_vout, p_vout->p_sys->p_output_picture );
409 msg_Err( p_vout, "cannot reinit framebuffer screen" );
416 /*****************************************************************************
417 * DisplayVideo: displays previously rendered output
418 *****************************************************************************
419 * This function update the screen resulting in the display of the last
421 *****************************************************************************/
422 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
426 if( !p_vout->p_sys->b_video_enabled )
429 UpdateScreen( p_vout,
430 p_vout->p_sys->output_window.i_x,
431 p_vout->p_sys->output_window.i_y,
432 p_vout->p_sys->i_video_width,
433 p_vout->p_sys->i_video_height,
434 p_vout->p_sys->output_window.i_width,
435 p_vout->p_sys->output_window.i_height,
436 p_vout->p_sys->i_color_format );
438 // Wait for the window to be fully displayed
439 ioctl( p_vout->p_sys->i_fd, OMAPFB_SYNC_GFX);
442 /*****************************************************************************
443 * OpenDisplay: initialize framebuffer
444 *****************************************************************************/
445 static int OpenDisplay( vout_thread_t *p_vout )
447 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
448 char *psz_device; /* framebuffer device path */
450 /* Open framebuffer device */
451 if( !(psz_device = var_CreateGetNonEmptyString( p_vout, FB_DEV_VAR )) )
453 msg_Err( p_vout, "don't know which fb device to open" );
457 p_sys->i_fd = utf8_open( psz_device, O_RDWR );
458 if( p_sys->i_fd == -1 )
460 msg_Err( p_vout, "cannot open %s (%m)", psz_device );
466 // Get caps, try older interface if needed
467 if( ioctl( p_sys->i_fd, OMAPFB_GET_CAPS, &p_sys->caps ) != 0 )
469 if( ioctl( p_sys->i_fd, OMAP_IOR( 42, unsigned long ), &p_sys->caps ) != 0 )
471 msg_Err( p_vout, "OMAPFB_GET_CAPS ioctl failed" );
472 close( p_sys->i_fd );
477 if( ( p_sys->caps.ctrl & OMAPFB_CAPS_TEARSYNC ) != 0 )
478 p_sys->b_tearsync = true;
480 if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->fb_vinfo ) )
482 msg_Err( p_vout, "Can't get VSCREENINFO: %m" );
483 close( p_sys->i_fd );
486 if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &p_sys->fb_finfo ) )
488 msg_Err( p_vout, "Can't get FSCREENINFO: %m" );
489 close( p_sys->i_fd );
493 p_sys->i_bytes_per_pixel = 2;
494 p_sys->i_page_size = p_sys->fb_finfo.smem_len;
495 p_sys->i_line_len = p_sys->fb_finfo.line_length;
497 if( (p_sys->p_video = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
498 p_sys->i_fd, 0 )) == MAP_FAILED )
500 msg_Err( p_vout, "Can't mmap: %m" );
501 close( p_sys->i_fd );
505 p_sys->p_display = XOpenDisplay( NULL );
507 /* Open /dev/null and map it */
508 p_sys->i_null_fd = utf8_open( "/dev/zero", O_RDWR );
509 if( p_sys->i_null_fd == -1 )
511 msg_Err( p_vout, "cannot open /dev/zero (%m)" );
512 munmap( p_sys->p_video, p_sys->i_page_size );
513 close( p_sys->i_fd );
517 if( (p_sys->p_null = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE,
518 MAP_PRIVATE, p_sys->i_null_fd, 0 )) == MAP_FAILED )
520 msg_Err( p_vout, "Can't mmap 2: %m" );
521 munmap( p_sys->p_video, p_sys->i_page_size );
522 close( p_sys->i_null_fd );
523 close( p_sys->i_fd );
530 /*****************************************************************************
531 * CloseDisplay: terminate FB video thread output method
532 *****************************************************************************/
533 static void CloseDisplay( vout_thread_t *p_vout )
535 munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
536 munmap( p_vout->p_sys->p_null, p_vout->p_sys->i_page_size );
538 close( p_vout->p_sys->i_fd );
539 close( p_vout->p_sys->i_null_fd );
542 /*****************************************************************************
543 * UpdateScreen: update the screen of the omapfb
544 *****************************************************************************/
545 static void UpdateScreen( vout_thread_t *p_vout, int i_x, int i_y,
546 int i_width, int i_height,
547 int i_out_width, int i_out_height, int i_format )
549 struct omapfb_update_window update;
552 update.width = i_width;
553 update.height = i_height;
556 update.out_width = i_out_width;
557 update.out_height = i_out_height;
558 update.format = i_format;
559 if( p_vout->p_sys->b_tearsync )
560 update.format |= OMAPFB_FORMAT_FLAG_TEARSYNC;
561 ioctl( p_vout->p_sys->i_fd, OMAPFB_UPDATE_WINDOW, &update );
564 /*****************************************************************************
565 * InitWindow: get embedded window and init X11
566 *****************************************************************************/
567 static int InitWindow( vout_thread_t *p_vout )
569 vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
571 if( var_CreateGetBool( p_vout, "omap-embedded" ) )
573 p_sys->p_display = XOpenDisplay( NULL );
575 // Request window from interface
576 vout_window_cfg_t wnd_cfg;
578 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
579 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
580 wnd_cfg.x = p_sys->embedded_window.i_x;
581 wnd_cfg.y = p_sys->embedded_window.i_y;
582 wnd_cfg.width = p_sys->embedded_window.i_width;
583 wnd_cfg.height = p_sys->embedded_window.i_height;
585 p_sys->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
586 p_sys->main_window = p_sys->embedded_window;
588 // We have to create a new window to get some events
589 CreateWindow( p_sys );
593 // No embedding, fullscreen framebuffer overlay with no events handling
594 p_sys->main_window.i_x = p_sys->main_window.i_y = 0;
595 p_sys->main_window.i_width = p_sys->fb_vinfo.xres;
596 p_sys->main_window.i_height = p_sys->fb_vinfo.yres;
597 p_vout->b_fullscreen = true;
598 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
604 static void CreateWindow( vout_sys_t *p_sys )
606 XSetWindowAttributes xwindow_attributes;
607 xwindow_attributes.backing_store = Always;
608 xwindow_attributes.background_pixel =
609 BlackPixel( p_sys->p_display, DefaultScreen(p_sys->p_display) );
610 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask
611 | VisibilityChangeMask;
612 p_sys->window = XCreateWindow( p_sys->p_display,
613 p_sys->owner_window->xid,
615 p_sys->main_window.i_width,
616 p_sys->main_window.i_height,
619 CWBackingStore | CWBackPixel | CWEventMask,
620 &xwindow_attributes );
622 XMapWindow( p_sys->p_display, p_sys->window );
623 XSelectInput( p_sys->p_display, p_sys->owner_window->xid,
624 StructureNotifyMask );