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>
48 #include <vlc_common.h>
49 #include <vlc_plugin.h>
51 #include <vlc_vout_window.h>
52 #include <vlc_playlist.h>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Create ( vlc_object_t * );
58 static void Destroy ( vlc_object_t * );
60 static int Init ( vout_thread_t * );
61 static void End ( vout_thread_t * );
62 static int Manage ( vout_thread_t * );
63 static void DisplayVideo ( vout_thread_t *, picture_t * );
64 static int Control ( vout_thread_t *, int, va_list );
66 static void FreePicture ( vout_thread_t *, picture_t * );
68 static int OpenDisplay ( vout_thread_t * );
69 static void CloseDisplay ( vout_thread_t * );
70 static void UpdateScreen ( vout_thread_t *,
71 int, int, int, int, int, int, int );
73 static int InitWindow ( vout_thread_t * );
74 static void CreateWindow ( vout_sys_t * );
75 static void ToggleFullScreen ( vout_thread_t * );
78 static const int i_backlight_on_interval = 300;
81 /*****************************************************************************
83 *****************************************************************************/
84 #define FB_DEV_VAR "omapfbdev"
86 #define DEVICE_TEXT N_("OMAP Framebuffer device")
87 #define DEVICE_LONGTEXT N_( \
88 "OMAP Framebuffer device to use for rendering (usually /dev/fb0).")
90 #define CHROMA_TEXT N_("Chroma used.")
91 #define CHROMA_LONGTEXT N_( \
92 "Force use of a specific chroma for output. Default is Y420 (specific to N770/N8xx hardware)." )
94 #define OVERLAY_TEXT N_("Embed the overlay")
95 #define OVERLAY_LONGTEXT N_( \
96 "Embed the framebuffer overlay into a X11 window" )
99 set_shortname( "OMAP framebuffer" );
100 set_category( CAT_VIDEO );
101 set_subcategory( SUBCAT_VIDEO_VOUT );
102 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
104 add_string( "omap-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
106 add_bool( "omap-embedded", true, NULL, OVERLAY_TEXT, OVERLAY_LONGTEXT,
108 set_description( N_("OMAP framebuffer video output") );
109 set_capability( "video output", 200 );
110 set_callbacks( Create, Destroy );
113 /*****************************************************************************
114 * omap_window_t: simple structure with the geometry of a window
115 *****************************************************************************/
124 /*****************************************************************************
125 * vout_sys_t: video output omap framebuffer method descriptor
126 *****************************************************************************
127 * This structure is part of the video output thread descriptor.
128 * It describes the FB specific properties of an output thread.
129 *****************************************************************************/
132 /* Framebuffer information */
133 int i_fd; /* device handle */
134 struct fb_var_screeninfo fb_vinfo; /* current mode information */
135 struct fb_fix_screeninfo fb_finfo;
136 struct omapfb_caps caps;
139 /* Window information */
140 struct omap_window_t output_window; /* Size of the real output window */
141 struct omap_window_t main_window; /* Size of the area we got to display */
142 struct omap_window_t embedded_window; /* Size of the embedded window */
144 /* Video information */
145 uint32_t i_video_width; /* video width */
146 uint32_t i_video_height; /* video height */
147 vlc_fourcc_t i_chroma;
148 int i_color_format; /* OMAPFB_COLOR_* */
149 bool b_video_enabled; /* Video must be displayed? */
150 picture_t *p_output_picture;
153 uint8_t *p_video; /* base adress */
154 uint8_t *p_center; /* output adress */
155 size_t i_page_size; /* page size */
156 int i_bytes_per_pixel; /* Bytes used by one pixel */
157 int i_line_len; /* Length of one line in bytes */
161 vout_window_t *owner_window;
163 mtime_t i_time_button_last_pressed; /* To detect double click */
170 osso_context_t *p_octx;
171 int i_backlight_on_counter;
175 /*****************************************************************************
176 * Create: allocates omapfb video thread output method
177 *****************************************************************************
178 * This function allocates and initializes a FB vout method.
179 *****************************************************************************/
180 static int Create( vlc_object_t *p_this )
182 vout_thread_t *p_vout = (vout_thread_t *)p_this;
185 if( p_vout->fmt_in.i_chroma != VLC_CODEC_I420 &&
186 p_vout->fmt_in.i_chroma != VLC_CODEC_YV12 )
189 /* Allocate instance and initialize some members */
190 p_vout->p_sys = p_sys = calloc( 1, sizeof( vout_sys_t ) );
191 if( p_vout->p_sys == NULL )
194 p_vout->pf_init = Init;
195 p_vout->pf_end = End;
196 p_vout->pf_manage = Manage;
197 p_vout->pf_render = NULL;
198 p_vout->pf_display = DisplayVideo;
199 p_vout->pf_control = Control;
200 p_sys->b_video_enabled = true;
202 if( OpenDisplay( p_vout ) )
204 free( p_vout->p_sys );
208 if( InitWindow( p_vout ) )
210 free( p_vout->p_sys );
215 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
216 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
217 if ( p_vout->p_sys->p_octx == NULL ) {
218 msg_Err( p_vout, "Could not get osso context" );
220 msg_Dbg( p_vout, "Initialized osso context" );
227 /*****************************************************************************
228 * Destroy: destroy omapfb video thread output method
229 *****************************************************************************
230 * Terminate an output method created by Create
231 *****************************************************************************/
232 static void Destroy( vlc_object_t *p_this )
234 vout_thread_t *p_vout = (vout_thread_t *)p_this;
236 CloseDisplay( p_vout );
238 if( p_vout->p_sys->owner_window )
240 vout_window_Delete( p_vout->p_sys->owner_window );
241 XCloseDisplay( p_vout->p_sys->p_display );
245 if ( p_vout->p_sys->p_octx != NULL ) {
246 msg_Dbg( p_vout, "Deinitializing osso context" );
247 osso_deinitialize( p_vout->p_sys->p_octx );
251 /* Destroy structure */
252 free( p_vout->p_sys );
256 /*****************************************************************************
257 * Init: initialize omap framebuffer video thread output method
258 *****************************************************************************/
259 static int Init( vout_thread_t *p_vout )
261 vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
263 // We want to keep the same aspect
264 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect = p_vout->render.i_aspect;
265 // We ask where the video should be displayed in the video area
266 vout_PlacePicture( p_vout, p_sys->main_window.i_width,
267 p_sys->main_window.i_height,
268 &p_sys->output_window.i_x,
269 &p_sys->output_window.i_y,
270 &p_sys->output_window.i_width,
271 &p_sys->output_window.i_height );
272 p_sys->output_window.i_x = ( p_sys->output_window.i_x +
273 p_sys->main_window.i_x ) & ~1;
274 p_sys->output_window.i_y = ( p_sys->output_window.i_y +
275 p_sys->main_window.i_y ) & ~1;
277 // Hardware upscaling better than software
278 if( p_vout->fmt_render.i_width <= p_sys->main_window.i_width &&
279 p_vout->fmt_render.i_height <= p_sys->main_window.i_height )
281 p_sys->i_video_width =
282 p_vout->output.i_width =
283 p_vout->fmt_out.i_width =
284 p_vout->fmt_out.i_visible_width = p_vout->fmt_render.i_width;
285 p_sys->i_video_height =
286 p_vout->output.i_height =
287 p_vout->fmt_out.i_height =
288 p_vout->fmt_out.i_visible_height = p_vout->fmt_render.i_height;
292 p_sys->i_video_width =
293 p_vout->output.i_width =
294 p_vout->fmt_out.i_width =
295 p_vout->fmt_out.i_visible_width = p_sys->output_window.i_width;
296 p_sys->i_video_height =
297 p_vout->output.i_height =
298 p_vout->fmt_out.i_height =
299 p_vout->fmt_out.i_visible_height = p_sys->output_window.i_height;
302 p_vout->output.i_chroma =
303 p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
304 p_sys->i_color_format = OMAPFB_COLOR_YUV420;
306 // place in the framebuffer where we have to write
307 p_sys->p_center = p_sys->p_video + p_sys->output_window.i_x*p_sys->i_bytes_per_pixel
308 + p_sys->output_window.i_y*p_sys->i_line_len;
310 // We get and set a direct render vlc picture
311 I_OUTPUTPICTURES = 0;
312 picture_t *p_pic = NULL;
315 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
317 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
319 p_pic = p_vout->p_picture + i_index;
324 /* Allocate the picture */
330 p_sys->p_output_picture = p_pic;
331 p_pic->p->p_pixels = p_vout->p_sys->p_center;
332 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
333 p_pic->p->i_lines = p_sys->i_video_height;
334 p_pic->p->i_visible_lines = p_sys->i_video_height;
335 p_pic->p->i_pitch = p_sys->i_line_len;
336 p_pic->p->i_visible_pitch = p_sys->i_line_len;
338 p_pic->i_status = DESTROYED_PICTURE;
339 p_pic->i_type = DIRECT_PICTURE;
341 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
348 /*****************************************************************************
349 * End: terminate omap framebuffer video thread output method
350 *****************************************************************************/
351 static void End( vout_thread_t *p_vout )
353 /* Clear the screen */
354 UpdateScreen( p_vout, 0, 0,
355 p_vout->p_sys->fb_vinfo.xres,
356 p_vout->p_sys->fb_vinfo.yres,
357 p_vout->p_sys->fb_vinfo.xres,
358 p_vout->p_sys->fb_vinfo.yres,
359 OMAPFB_COLOR_RGB565 );
362 /*****************************************************************************
363 * Control: control facility for the vout
364 *****************************************************************************/
365 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
370 /*****************************************************************************
371 * FreePicture: Destroy the picture and make it free again
372 ******************************************************************************/
373 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
375 p_pic->p->p_pixels = NULL;
376 p_pic->i_status = FREE_PICTURE;
379 /*****************************************************************************
380 * Manage: handle omapfb events
381 *****************************************************************************
382 * This function should be called regularly by video output thread.
383 *****************************************************************************/
384 static int Manage( vout_thread_t *p_vout )
388 while( XPending( p_vout->p_sys->p_display ) )
390 XNextEvent( p_vout->p_sys->p_display, &xevent );
392 if( xevent.type == ButtonPress &&
393 ((XButtonEvent *)&xevent)->button == Button1 )
395 /* detect double-clicks */
396 if( ( ((XButtonEvent *)&xevent)->time -
397 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
399 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
402 p_vout->p_sys->i_time_button_last_pressed =
403 ((XButtonEvent *)&xevent)->time;
405 else if( ( xevent.type == VisibilityNotify &&
406 xevent.xvisibility.state != VisibilityUnobscured ) ||
407 xevent.type == UnmapNotify )
409 UpdateScreen( p_vout, 0, 0,
410 p_vout->p_sys->fb_vinfo.xres,
411 p_vout->p_sys->fb_vinfo.yres,
412 p_vout->p_sys->fb_vinfo.xres,
413 p_vout->p_sys->fb_vinfo.yres,
414 OMAPFB_COLOR_RGB565 );
415 p_vout->p_sys->b_video_enabled = false;
416 p_vout->p_sys->p_output_picture->p->p_pixels =
417 p_vout->p_sys->p_null;
421 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
423 /* Update the object variable and trigger callback */
424 p_vout->b_fullscreen = !p_vout->b_fullscreen;
425 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
427 if( p_vout->p_sys->owner_window )
428 vout_window_SetFullscreen( p_vout->p_sys->owner_window,
429 p_vout->b_fullscreen );
430 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
433 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
435 FreePicture( p_vout, p_vout->p_sys->p_output_picture );
438 msg_Err( p_vout, "cannot reinit framebuffer screen" );
445 if ( p_vout->p_sys->p_octx != NULL ) {
446 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
447 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
448 msg_Err( p_vout, "Could not disable backlight blanking" );
450 msg_Dbg( p_vout, "Backlight blanking disabled" );
452 p_vout->p_sys->i_backlight_on_counter = 0;
454 p_vout->p_sys->i_backlight_on_counter ++;
462 /*****************************************************************************
463 * DisplayVideo: displays previously rendered output
464 *****************************************************************************
465 * This function update the screen resulting in the display of the last
467 *****************************************************************************/
468 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
472 if( !p_vout->p_sys->b_video_enabled )
475 UpdateScreen( p_vout,
476 p_vout->p_sys->output_window.i_x,
477 p_vout->p_sys->output_window.i_y,
478 p_vout->p_sys->i_video_width,
479 p_vout->p_sys->i_video_height,
480 p_vout->p_sys->output_window.i_width,
481 p_vout->p_sys->output_window.i_height,
482 p_vout->p_sys->i_color_format );
484 // Wait for the window to be fully displayed
485 ioctl( p_vout->p_sys->i_fd, OMAPFB_SYNC_GFX);
488 /*****************************************************************************
489 * OpenDisplay: initialize framebuffer
490 *****************************************************************************/
491 static int OpenDisplay( vout_thread_t *p_vout )
493 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
494 char *psz_device; /* framebuffer device path */
496 /* Open framebuffer device */
497 if( !(psz_device = var_CreateGetNonEmptyString( p_vout, FB_DEV_VAR )) )
499 msg_Err( p_vout, "don't know which fb device to open" );
503 p_sys->i_fd = open( psz_device, O_RDWR );
504 if( p_sys->i_fd == -1 )
506 msg_Err( p_vout, "cannot open %s (%m)", psz_device );
512 // Get caps, try older interface if needed
513 if( ioctl( p_sys->i_fd, OMAPFB_GET_CAPS, &p_sys->caps ) != 0 )
515 if( ioctl( p_sys->i_fd, OMAP_IOR( 42, unsigned long ), &p_sys->caps ) != 0 )
517 msg_Err( p_vout, "OMAPFB_GET_CAPS ioctl failed" );
518 close( p_sys->i_fd );
523 if( ( p_sys->caps.ctrl & OMAPFB_CAPS_TEARSYNC ) != 0 )
524 p_sys->b_tearsync = true;
526 if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->fb_vinfo ) )
528 msg_Err( p_vout, "Can't get VSCREENINFO: %s", strerror(errno) );
529 close( p_sys->i_fd );
532 if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &p_sys->fb_finfo ) )
534 msg_Err( p_vout, "Can't get FSCREENINFO: %s", strerror(errno) );
535 close( p_sys->i_fd );
539 p_sys->i_bytes_per_pixel = 2;
540 p_sys->i_page_size = p_sys->fb_finfo.smem_len;
541 p_sys->i_line_len = p_sys->fb_finfo.line_length;
543 if( (p_sys->p_video = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
544 p_sys->i_fd, 0 )) == MAP_FAILED )
546 msg_Err( p_vout, "Can't mmap: %s", strerror(errno) );
547 close( p_sys->i_fd );
551 p_sys->p_display = XOpenDisplay( NULL );
553 /* Open /dev/null and map it */
554 p_sys->i_null_fd = open( "/dev/zero", O_RDWR );
555 if( p_sys->i_null_fd == -1 )
557 msg_Err( p_vout, "cannot open /dev/zero (%m)" );
558 munmap( p_sys->p_video, p_sys->i_page_size );
559 close( p_sys->i_fd );
563 if( (p_sys->p_null = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE,
564 MAP_PRIVATE, p_sys->i_null_fd, 0 )) == MAP_FAILED )
566 msg_Err( p_vout, "Can't mmap 2: %s", strerror(errno) );
567 munmap( p_sys->p_video, p_sys->i_page_size );
568 close( p_sys->i_null_fd );
569 close( p_sys->i_fd );
576 /*****************************************************************************
577 * CloseDisplay: terminate FB video thread output method
578 *****************************************************************************/
579 static void CloseDisplay( vout_thread_t *p_vout )
581 munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
582 munmap( p_vout->p_sys->p_null, p_vout->p_sys->i_page_size );
584 close( p_vout->p_sys->i_fd );
585 close( p_vout->p_sys->i_null_fd );
588 /*****************************************************************************
589 * UpdateScreen: update the screen of the omapfb
590 *****************************************************************************/
591 static void UpdateScreen( vout_thread_t *p_vout, int i_x, int i_y,
592 int i_width, int i_height,
593 int i_out_width, int i_out_height, int i_format )
595 struct omapfb_update_window update;
598 update.width = i_width;
599 update.height = i_height;
602 update.out_width = i_out_width;
603 update.out_height = i_out_height;
604 update.format = i_format;
605 if( p_vout->p_sys->b_tearsync )
606 update.format |= OMAPFB_FORMAT_FLAG_TEARSYNC;
607 ioctl( p_vout->p_sys->i_fd, OMAPFB_UPDATE_WINDOW, &update );
610 /*****************************************************************************
611 * InitWindow: get embedded window and init X11
612 *****************************************************************************/
613 static int InitWindow( vout_thread_t *p_vout )
615 vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
617 if( var_CreateGetBool( p_vout, "omap-embedded" ) )
619 p_sys->p_display = XOpenDisplay( NULL );
621 // Request window from interface
622 vout_window_cfg_t wnd_cfg;
624 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
625 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
626 wnd_cfg.x = p_sys->embedded_window.i_x;
627 wnd_cfg.y = p_sys->embedded_window.i_y;
628 wnd_cfg.width = p_sys->embedded_window.i_width;
629 wnd_cfg.height = p_sys->embedded_window.i_height;
631 p_sys->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
632 p_sys->main_window = p_sys->embedded_window;
634 // We have to create a new window to get some events
635 CreateWindow( p_sys );
639 // No embedding, fullscreen framebuffer overlay with no events handling
640 p_sys->main_window.i_x = p_sys->main_window.i_y = 0;
641 p_sys->main_window.i_width = p_sys->fb_vinfo.xres;
642 p_sys->main_window.i_height = p_sys->fb_vinfo.yres;
643 p_vout->b_fullscreen = true;
644 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
650 static void CreateWindow( vout_sys_t *p_sys )
652 XSetWindowAttributes xwindow_attributes;
653 xwindow_attributes.backing_store = Always;
654 xwindow_attributes.background_pixel =
655 BlackPixel( p_sys->p_display, DefaultScreen(p_sys->p_display) );
656 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask
657 | VisibilityChangeMask;
658 p_sys->window = XCreateWindow( p_sys->p_display,
659 p_sys->owner_window->handle.xid,
661 p_sys->main_window.i_width,
662 p_sys->main_window.i_height,
665 CWBackingStore | CWBackPixel | CWEventMask,
666 &xwindow_attributes );
668 XMapWindow( p_sys->p_display, p_sys->window );
669 XSelectInput( p_sys->p_display, p_sys->owner_window->handle.xid,
670 StructureNotifyMask );