1 /*****************************************************************************
2 * vout_beos.cpp: beos video output display method
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
6 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
33 #include <kernel/OS.h>
35 #include <Application.h>
36 #include <DirectWindow.h>
51 #include "video_output.h"
53 #include "interface.h" /* XXX maybe to remove if beos_window.h is splitted */
59 #include "beos_window.h"
63 #define BITS_PER_PLANE 16
64 #define BYTES_PER_PIXEL 2
66 /*****************************************************************************
67 * vout_sys_t: dummy video output method descriptor
68 *****************************************************************************
69 * This structure is part of the video output thread descriptor.
70 * It describes the dummy specific properties of an output thread.
71 *****************************************************************************/
73 typedef struct vout_sys_s
75 VideoWindow * p_window;
77 byte_t * pp_buffer[2];
83 /*****************************************************************************
84 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
85 *****************************************************************************/
87 BWindow *beos_GetAppWindow(char *name)
92 for (index = 0 ; ; index++)
94 window = be_app->WindowAt(index);
97 if (window->LockWithTimeout(200000) == B_OK)
99 if (strcmp(window->Name(), name) == 0)
110 /*****************************************************************************
111 * DrawingThread : thread that really does the drawing
112 *****************************************************************************/
114 int32 DrawingThread(void *data)
120 uint32 height, bytes_per_line;
124 w = (VideoWindow*) data;
126 while(!w->fConnectionDisabled)
131 if( w->fDirty && (!w->fReady || w->i_screen_depth != w->p_vout->i_screen_depth) )
133 bytes_per_line = w->fRowBytes;
134 for( i=0 ; i < w->fNumClipRects ; i++ )
136 clip = &(w->fClipList[i]);
137 height = clip->bottom - clip->top +1;
138 byte_width = w->i_bytes_per_pixel * ((clip->right - clip->left)+1);
139 p = w->fBits + clip->top*w->fRowBytes + clip->left * w->i_bytes_per_pixel;
140 for( y=0 ; y < height ; )
143 for( j=0 ; j < byte_width/64 ; j++ )
154 memset( pp , 0, byte_width & 63 );
162 bytes_per_line = w->fRowBytes;
163 for( i=0 ; i < w->fNumClipRects ; i++ )
165 clip = &(w->fClipList[i]);
166 height = clip->bottom - clip->top +1;
167 byte_width = w->i_bytes_per_pixel * ((clip->right - clip->left)+1);
168 p = w->fBits + clip->top * bytes_per_line + clip->left * w->i_bytes_per_pixel;
169 q = w->p_vout->p_sys->pp_buffer[ !w->p_vout->i_buffer_index ] +
170 clip->top * w->p_vout->i_bytes_per_line + clip->left *
171 w->p_vout->i_bytes_per_pixel;
172 for( y=0 ; y < height ; )
176 for( j=0 ; j < byte_width/64 ; j++ )
187 memcpy( pp , qq, byte_width & 63 );
190 q += w->p_vout->p_sys->i_width * w->p_vout->i_bytes_per_pixel;
202 /*****************************************************************************
203 * VideoWindow constructor and destructor
204 *****************************************************************************/
206 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
207 : BDirectWindow(frame, name, B_TITLED_WINDOW, B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
213 fConnectionDisabled = false;
214 locker = new BLocker();
217 p_vout = p_video_output;
219 view = new BView(Bounds(), "", B_FOLLOW_ALL, B_WILL_DRAW);
220 view->SetViewColor(B_TRANSPARENT_32_BIT);
223 if(!SupportsWindowMode())
229 fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
230 B_DISPLAY_PRIORITY, (void*) this);
231 resume_thread(fDrawThreadID);
235 VideoWindow::~VideoWindow()
239 fConnectionDisabled = true;
242 wait_for_thread(fDrawThreadID, &result);
247 /*****************************************************************************
248 * VideoWindow::DirectConnected
249 *****************************************************************************/
251 void VideoWindow::DirectConnected(direct_buffer_info *info)
255 if(!fConnected && fConnectionDisabled)
261 switch(info->buffer_state & B_DIRECT_MODE_MASK)
265 case B_DIRECT_MODIFY:
266 fBits = (uint8*)((char*)info->bits +
267 (info->window_bounds.top) * info->bytes_per_row +
268 (info->window_bounds.left) * (info->bits_per_pixel>>3));;
270 i_bytes_per_pixel = info->bits_per_pixel >> 3;
271 i_screen_depth = info->bits_per_pixel;
273 fRowBytes = info->bytes_per_row;
274 fFormat = info->pixel_format;
275 fBounds = info->window_bounds;
283 fNumClipRects = info->clip_list_count;
284 fClipList = (clipping_rect*) malloc(fNumClipRects*sizeof(clipping_rect));
285 for( i=0 ; i<info->clip_list_count ; i++ )
287 fClipList[i].top = info->clip_list[i].top - info->window_bounds.top;
288 fClipList[i].left = info->clip_list[i].left - info->window_bounds.left;
289 fClipList[i].bottom = info->clip_list[i].bottom - info->window_bounds.top;
290 fClipList[i].right = info->clip_list[i].right - info->window_bounds.left;
300 /*****************************************************************************
301 * VideoWindow::MessageReceived
302 *****************************************************************************/
304 void VideoWindow::MessageReceived( BMessage * p_message )
308 switch( p_message->what )
311 // post the message to the interface window which will handle it
312 p_win = beos_GetAppWindow( "interface" );
315 p_win->PostMessage( p_message );
320 BWindow::MessageReceived( p_message );
325 /*****************************************************************************
326 * VideoWindow::QuitRequested
327 *****************************************************************************/
329 bool VideoWindow::QuitRequested()
337 /*****************************************************************************
339 *****************************************************************************/
340 static int vout_Probe ( probedata_t *p_data );
341 static int vout_Create ( struct vout_thread_s * );
342 static int vout_Init ( struct vout_thread_s * );
343 static void vout_End ( struct vout_thread_s * );
344 static void vout_Destroy ( struct vout_thread_s * );
345 static int vout_Manage ( struct vout_thread_s * );
346 static void vout_Display ( struct vout_thread_s * );
348 static int BeosOpenDisplay ( vout_thread_t *p_vout );
349 static void BeosCloseDisplay( vout_thread_t *p_vout );
351 /*****************************************************************************
352 * Functions exported as capabilities. They are declared as static so that
353 * we don't pollute the namespace too much.
354 *****************************************************************************/
355 void vout_getfunctions( function_list_t * p_function_list )
357 p_function_list->pf_probe = vout_Probe;
358 p_function_list->functions.vout.pf_create = vout_Create;
359 p_function_list->functions.vout.pf_init = vout_Init;
360 p_function_list->functions.vout.pf_end = vout_End;
361 p_function_list->functions.vout.pf_destroy = vout_Destroy;
362 p_function_list->functions.vout.pf_manage = vout_Manage;
363 p_function_list->functions.vout.pf_display = vout_Display;
364 p_function_list->functions.vout.pf_setpalette = NULL;
367 /*****************************************************************************
368 * vout_Probe: probe the video driver and return a score
369 *****************************************************************************
370 * This function tries to initialize SDL and returns a score to the
371 * plugin manager so that it can select the best plugin.
372 *****************************************************************************/
373 static int vout_Probe( probedata_t *p_data )
375 if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
383 /*****************************************************************************
384 * vout_Create: allocates dummy video thread output method
385 *****************************************************************************
386 * This function allocates and initializes a dummy vout method.
387 *****************************************************************************/
388 int vout_Create( vout_thread_t *p_vout )
390 /* Allocate structure */
391 p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
392 if( p_vout->p_sys == NULL )
394 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
398 /* Set video window's size */
399 p_vout->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
400 VOUT_WIDTH_DEFAULT );
401 p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
402 VOUT_HEIGHT_DEFAULT );
404 /* Open and initialize device */
405 if( BeosOpenDisplay( p_vout ) )
407 intf_ErrMsg("vout error: can't open display");
408 free( p_vout->p_sys );
415 /*****************************************************************************
416 * vout_Init: initialize dummy video thread output method
417 *****************************************************************************/
418 int vout_Init( vout_thread_t *p_vout )
420 VideoWindow * p_win = p_vout->p_sys->p_window;
423 p_win->locker->Lock();
425 i_page_size = p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
427 p_vout->p_sys->i_width = p_vout->i_width;
428 p_vout->p_sys->i_height = p_vout->i_height;
430 /* Allocate memory for the 2 display buffers */
431 p_vout->p_sys->pp_buffer[0] = (byte_t*) malloc( i_page_size );
432 p_vout->p_sys->pp_buffer[1] = (byte_t*) malloc( i_page_size );
433 if( p_vout->p_sys->pp_buffer[0] == NULL || p_vout->p_sys->pp_buffer[0] == NULL )
435 intf_ErrMsg("vout error: can't allocate video memory (%s)", strerror(errno) );
436 if( p_vout->p_sys->pp_buffer[0] != NULL ) free( p_vout->p_sys->pp_buffer[0] );
437 if( p_vout->p_sys->pp_buffer[1] != NULL ) free( p_vout->p_sys->pp_buffer[1] );
438 p_win->locker->Unlock();
442 /* Set and initialize buffers */
443 vout_SetBuffers( p_vout, p_vout->p_sys->pp_buffer[0],
444 p_vout->p_sys->pp_buffer[1] );
446 p_win->locker->Unlock();
450 /*****************************************************************************
451 * vout_End: terminate dummy video thread output method
452 *****************************************************************************/
453 void vout_End( vout_thread_t *p_vout )
455 VideoWindow * p_win = p_vout->p_sys->p_window;
459 free( p_vout->p_sys->pp_buffer[0] );
460 free( p_vout->p_sys->pp_buffer[1] );
462 p_win->fReady = false;
466 /*****************************************************************************
467 * vout_Destroy: destroy dummy video thread output method
468 *****************************************************************************
469 * Terminate an output method created by DummyCreateOutputMethod
470 *****************************************************************************/
471 void vout_Destroy( vout_thread_t *p_vout )
473 BeosCloseDisplay( p_vout );
475 free( p_vout->p_sys );
478 /*****************************************************************************
479 * vout_Manage: handle dummy events
480 *****************************************************************************
481 * This function should be called regularly by video output thread. It manages
482 * console events. It returns a non null value on error.
483 *****************************************************************************/
484 int vout_Manage( vout_thread_t *p_vout )
486 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
488 intf_DbgMsg( "resizing window" );
489 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
492 p_vout->p_sys->p_window->ResizeTo( p_vout->i_width, p_vout->i_height );
494 /* Destroy XImages to change their size */
497 /* Recreate XImages. If SysInit failed, the thread can't go on. */
498 if( vout_Init( p_vout ) )
500 intf_ErrMsg( "error: can't resize display" );
504 /* Tell the video output thread that it will need to rebuild YUV
505 * tables. This is needed since convertion buffer size may have
507 p_vout->i_changes |= VOUT_YUV_CHANGE;
508 intf_Msg( "vout: video display resized (%dx%d)",
509 p_vout->i_width, p_vout->i_height );
514 /*****************************************************************************
515 * vout_Display: displays previously rendered output
516 *****************************************************************************
517 * This function send the currently rendered image to dummy image, waits until
518 * it is displayed and switch the two rendering buffers, preparing next frame.
519 *****************************************************************************/
520 void vout_Display( vout_thread_t *p_vout )
522 VideoWindow * p_win = p_vout->p_sys->p_window;
524 p_win->locker->Lock();
525 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
526 p_win->fReady = true;
527 p_win->fDirty = true;
528 p_win->locker->Unlock();
531 /* following functions are local */
533 /*****************************************************************************
534 * BeosOpenDisplay: open and initialize dummy device
535 *****************************************************************************
536 * XXX?? The framebuffer mode is only provided as a fast and efficient way to
537 * display video, providing the card is configured and the mode ok. It is
538 * not portable, and is not supposed to work with many cards. Use at your
540 *****************************************************************************/
542 static int BeosOpenDisplay( vout_thread_t *p_vout )
544 /* Create the DirectDraw video window */
545 p_vout->p_sys->p_window =
546 new VideoWindow( BRect( 50, 150, 50+p_vout->i_width-1, 150+p_vout->i_height-1 ), VOUT_TITLE " (BeOS output)", p_vout );
547 if( p_vout->p_sys->p_window == 0 )
549 free( p_vout->p_sys );
550 intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
553 VideoWindow * p_win = p_vout->p_sys->p_window;
555 /* Wait until DirectConnected has been called */
556 while( !p_win->fConnected )
559 p_vout->i_screen_depth = p_win->i_screen_depth;
560 p_vout->i_bytes_per_pixel = p_win->i_bytes_per_pixel;
561 p_vout->i_bytes_per_line = p_vout->i_width*p_win->i_bytes_per_pixel;
563 switch( p_vout->i_screen_depth )
566 intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
569 p_vout->i_red_mask = 0x7c00;
570 p_vout->i_green_mask = 0x03e0;
571 p_vout->i_blue_mask = 0x001f;
574 p_vout->i_red_mask = 0xf800;
575 p_vout->i_green_mask = 0x07e0;
576 p_vout->i_blue_mask = 0x001f;
581 p_vout->i_red_mask = 0xff0000;
582 p_vout->i_green_mask = 0x00ff00;
583 p_vout->i_blue_mask = 0x0000ff;
590 /*****************************************************************************
591 * BeosDisplay: close and reset dummy device
592 *****************************************************************************
593 * Returns all resources allocated by BeosOpenDisplay and restore the original
594 * state of the device.
595 *****************************************************************************/
596 static void BeosCloseDisplay( vout_thread_t *p_vout )
598 /* Destroy the video window */
599 p_vout->p_sys->p_window->Lock();
600 p_vout->p_sys->p_window->Quit();