Improved overlay detection, default colour space selection.
[vlc.git] / plugins / beos / vout_beos.cpp
1 /*****************************************************************************
2  * vout_beos.cpp: beos video output display method
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: vout_beos.cpp,v 1.50 2002/04/01 05:49:00 tcastley Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Tony Castley <tcastley@mail.powerup.com.au>
10  *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>
33 #include <string.h>                                            /* strerror() */
34 #include <InterfaceKit.h>
35 #include <DirectWindow.h>
36 #include <Application.h>
37 #include <Bitmap.h>
38
39 extern "C"
40 {
41 #include <videolan/vlc.h>
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "interface.h"
47
48 }
49
50 #include "VideoWindow.h"
51 #include "DrawingTidbits.h"
52
53 /*****************************************************************************
54  * vout_sys_t: BeOS video output method descriptor
55  *****************************************************************************
56  * This structure is part of the video output thread descriptor.
57  * It describes the BeOS specific properties of an output thread.
58  *****************************************************************************/
59 typedef struct vout_sys_s
60 {
61     VideoWindow *  p_window;
62
63     s32 i_width;
64     s32 i_height;
65
66     u8 *pp_buffer[2];
67     int i_index;
68
69 } vout_sys_t;
70
71 /*****************************************************************************
72  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
73  *****************************************************************************/
74 BWindow *beos_GetAppWindow(char *name)
75 {
76     int32       index;
77     BWindow     *window;
78     
79     for (index = 0 ; ; index++)
80     {
81         window = be_app->WindowAt(index);
82         if (window == NULL)
83             break;
84         if (window->LockWithTimeout(20000) == B_OK)
85         {
86             if (strcmp(window->Name(), name) == 0)
87             {
88                 window->Unlock();
89                 break;
90             }
91             window->Unlock();
92         }
93     }
94     return window; 
95 }
96 /****************************************************************************
97  * DrawingThread : thread that really does the drawing
98  ****************************************************************************/
99 int32 Draw(void *data)
100 {
101     VideoWindow* p_win;
102     p_win = (VideoWindow *) data;
103
104     if ( p_win->LockWithTimeout(50000) == B_OK )
105     {
106         if (p_win->vsync)
107         {
108             BScreen *screen;
109             screen = new BScreen(p_win);
110             screen-> WaitForRetrace(22000);
111             delete screen;
112         }
113         if (p_win-> mode == OVERLAY)
114         {
115             p_win-> overlaybitmap->LockBits();
116                 memcpy(p_win-> overlaybitmap->Bits(), 
117                        p_win-> bitmap[p_win-> i_buffer]->Bits(),
118                            p_win-> bitmap[p_win-> i_buffer]->BitsLength() );
119             p_win-> overlaybitmap->UnlockBits();
120             p_win-> view-> Invalidate();
121         }
122         else
123         {
124             p_win-> view-> DrawBitmap( p_win-> bitmap[p_win-> i_buffer], 
125                                        p_win-> view->Bounds() );
126         }                                
127         p_win-> Unlock();
128     }
129     else
130     {
131         intf_Msg("Dropped Frame");
132     }
133     return B_OK;
134 }
135
136 /*****************************************************************************
137  * VideoWindow constructor and destructor
138  *****************************************************************************/
139 VideoWindow::VideoWindow( int v_width, int v_height, 
140                           BRect frame )
141             : BWindow( frame, NULL, B_TITLED_WINDOW, 
142                     B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
143 {
144     BView *mainView =  new BView( Bounds(), "mainView", 
145                                   B_FOLLOW_ALL, B_FULL_UPDATE_ON_RESIZE);
146     AddChild(mainView);
147     mainView->SetViewColor(kBlack);
148                                   
149     /* create the view to do the display */
150     view = new VLCView( Bounds() );
151     mainView->AddChild(view);
152
153     /* set the VideoWindow variables */
154     teardownwindow = false;
155     is_zoomed = false;
156
157     /* call ScreenChanged to set vsync correctly */
158     BScreen *screen;
159     screen = new BScreen(this);
160     ScreenChanged(screen->Frame(), screen->ColorSpace());
161     delete screen;
162     
163     // remember current settings
164     i_width = frame.IntegerWidth();
165     i_height = frame.IntegerHeight();
166     FrameResized(frame.IntegerWidth(), frame.IntegerHeight());
167
168     mode = SelectDrawingMode(v_width, v_height);
169     Show();
170 }
171
172 VideoWindow::~VideoWindow()
173 {
174     int32 result;
175
176     teardownwindow = true;
177     Lock();
178     view->ClearViewOverlay();
179     wait_for_thread(fDrawThreadID, &result);
180     delete overlaybitmap;
181     delete bitmap[0];
182     delete bitmap[1];
183     Quit();
184 }
185
186 void VideoWindow::drawBuffer(int bufferIndex)
187 {
188     status_t status;
189
190     i_buffer = bufferIndex;
191     fDrawThreadID = spawn_thread(Draw, "drawing_thread",
192                                  B_DISPLAY_PRIORITY, (void*) this);
193     wait_for_thread(fDrawThreadID, &status);
194 }
195
196 void VideoWindow::Zoom(BPoint origin, float width, float height )
197 {
198     if(is_zoomed)
199     {
200         is_zoomed = !is_zoomed;
201         MoveTo(winSize.left, winSize.top);
202         ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
203         be_app->ShowCursor();
204     }
205     else
206     {
207         is_zoomed = !is_zoomed;
208         BScreen *screen;
209         screen = new BScreen(this);
210         BRect rect = screen->Frame();
211         delete screen;
212         MoveTo(0,0);
213         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
214         be_app->ObscureCursor();
215     }
216     view->Invalidate();
217 }
218
219 void VideoWindow::FrameMoved(BPoint origin) 
220 {
221         if (is_zoomed) return ;
222     winSize = Frame();
223     view->Invalidate();
224 }
225
226 void VideoWindow::FrameResized( float width, float height )
227 {
228     float out_width, out_height;
229     float out_left, out_top;
230     float width_scale = width / i_width;
231     float height_scale = height / i_height;
232
233     if (width_scale <= height_scale)
234     {
235         out_width = i_width * width_scale;
236         out_height = i_height * width_scale;
237         out_left = 0; 
238         out_top = (height - out_height) / 2;
239     }
240     else   /* if the height is proportionally smaller */
241     {
242         out_width = i_width * height_scale;
243         out_height = i_height * height_scale;
244         out_top = 0;
245         out_left = (width - out_width) /2;
246     }
247     view->MoveTo(out_left,out_top);
248     view->ResizeTo(out_width, out_height);
249         if (!is_zoomed)
250         {
251         winSize = Frame();
252     }
253     view->Invalidate();
254 }
255
256 void VideoWindow::ScreenChanged(BRect frame, color_space mode)
257 {
258     BScreen *screen;
259     float refresh;
260     
261     view->Invalidate();
262     screen = new BScreen(this);
263     display_mode disp_mode; 
264     
265     screen-> GetMode(&disp_mode); 
266     refresh = 
267          (disp_mode.timing.pixel_clock * 1000)/((disp_mode.timing.h_total)* 
268          (disp_mode.timing.v_total)); 
269     if (refresh  < 61) 
270     { 
271         vsync = true; 
272     } 
273 }
274
275 void VideoWindow::WindowActivated(bool active)
276 {
277     view->Invalidate();
278 }
279
280 int VideoWindow::SelectDrawingMode(int width, int height)
281 {
282     int drawingMode = BITMAP;
283
284     for (int i = 0; i < COLOR_COUNT; i++)
285     {
286         overlaybitmap = new BBitmap ( BRect( 0, 0, width, height ), 
287                                   B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL,
288                                   colspace[i].colspace);
289
290         if(overlaybitmap && overlaybitmap->InitCheck() == B_OK) 
291         {
292             drawingMode = OVERLAY;
293             colspace_index = i;
294             memset(overlaybitmap->Bits(), 0, overlaybitmap->BitsLength());
295             rgb_color key;
296                     view->SetViewOverlay(overlaybitmap, overlaybitmap->Bounds() ,view->Bounds(),
297                                          &key, B_FOLLOW_ALL,
298                                              B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
299                     view->SetViewColor(key);
300             SetTitle(VOUT_TITLE " (Overlay)");
301             intf_Msg("Color index good: %i", colspace_index);
302             break;
303         }
304         else
305         {
306             delete overlaybitmap;
307             intf_Msg("Color index bad: %i", colspace_index);
308         }        
309         }
310
311     if (drawingMode == BITMAP)
312         {
313         // fallback to RGB32
314         colspace_index = DEFAULT_COL;
315         SetTitle(VOUT_TITLE " (Bitmap)");
316     }
317     
318     bitmap[0] = new BBitmap( BRect( 0, 0, width, height ), colspace[colspace_index].colspace);
319     bitmap[1] = new BBitmap( BRect( 0, 0, width, height ), colspace[colspace_index].colspace);
320     memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
321     memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
322     intf_Msg("Color index used: %i", colspace_index);
323     return drawingMode;
324 }
325
326 /*****************************************************************************
327  * VLCView::VLCView
328  *****************************************************************************/
329 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_NONE,
330                                        B_WILL_DRAW)
331
332 {
333     SetViewColor(B_TRANSPARENT_32_BIT);
334 }
335
336 /*****************************************************************************
337  * VLCView::~VLCView
338  *****************************************************************************/
339 VLCView::~VLCView()
340 {
341 }
342
343 /*****************************************************************************
344  * VLCVIew::MouseDown
345  *****************************************************************************/
346 void VLCView::MouseDown(BPoint point)
347 {
348     BWindow *win = Window();
349     win->Zoom();
350 }
351
352 /*****************************************************************************
353  * VLCVIew::Draw
354  *****************************************************************************/
355 void VLCView::Draw(BRect updateRect) 
356 {
357     VideoWindow *win = (VideoWindow *) Window();
358     if (win->mode == BITMAP)
359       FillRect(updateRect);
360 }
361
362
363 extern "C"
364 {
365
366 /*****************************************************************************
367  * Local prototypes
368  *****************************************************************************/
369 static int  vout_Create     ( vout_thread_t * );
370 static int  vout_Init       ( vout_thread_t * );
371 static void vout_End        ( vout_thread_t * );
372 static void vout_Destroy    ( vout_thread_t * );
373 static int  vout_Manage     ( vout_thread_t * );
374 static void vout_Display    ( vout_thread_t *, picture_t * );
375 static void vout_Render     ( vout_thread_t *, picture_t * );
376
377 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
378 static void BeosCloseDisplay( vout_thread_t *p_vout );
379
380 /*****************************************************************************
381  * Functions exported as capabilities. They are declared as static so that
382  * we don't pollute the namespace too much.
383  *****************************************************************************/
384 void _M( vout_getfunctions )( function_list_t * p_function_list )
385 {
386     p_function_list->functions.vout.pf_create     = vout_Create;
387     p_function_list->functions.vout.pf_init       = vout_Init;
388     p_function_list->functions.vout.pf_end        = vout_End;
389     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
390     p_function_list->functions.vout.pf_manage     = vout_Manage;
391     p_function_list->functions.vout.pf_display    = vout_Display;
392     p_function_list->functions.vout.pf_render     = vout_Render;
393 }
394
395 /*****************************************************************************
396  * vout_Create: allocates BeOS video thread output method
397  *****************************************************************************
398  * This function allocates and initializes a BeOS vout method.
399  *****************************************************************************/
400 int vout_Create( vout_thread_t *p_vout )
401 {
402     /* Allocate structure */
403     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
404     if( p_vout->p_sys == NULL )
405     {
406         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
407         return( 1 );
408     }
409     p_vout->p_sys->i_width = p_vout->render.i_width;
410     p_vout->p_sys->i_height = p_vout->render.i_height;
411
412     return( 0 );
413 }
414
415 /*****************************************************************************
416  * vout_Init: initialize BeOS video thread output method
417  *****************************************************************************/
418 int vout_Init( vout_thread_t *p_vout )
419 {
420     int i_index;
421     picture_t *p_pic;
422
423     I_OUTPUTPICTURES = 0;
424
425     /* Open and initialize device */
426     if( BeosOpenDisplay( p_vout ) )
427     {
428         intf_ErrMsg("vout error: can't open display");
429         return 0;
430     }
431     /* Set the buffers */
432     p_vout->p_sys->pp_buffer[0] = (u8*)p_vout->p_sys->p_window->bitmap[0]->Bits();
433     p_vout->p_sys->pp_buffer[1] = (u8*)p_vout->p_sys->p_window->bitmap[1]->Bits();
434
435     p_vout->output.i_width  = p_vout->render.i_width;
436     p_vout->output.i_height = p_vout->render.i_height;
437
438     /* Assume we have square pixels */
439     p_vout->output.i_aspect = p_vout->p_sys->i_width
440                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
441
442     p_vout->output.i_chroma = colspace[p_vout->p_sys->p_window->colspace_index].chroma;
443
444     p_vout->output.i_rmask  = 0x00ff0000;
445     p_vout->output.i_gmask  = 0x0000ff00;
446     p_vout->output.i_bmask  = 0x000000ff;
447
448     p_pic = NULL;
449
450     /* Find an empty picture slot */
451     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
452     {
453         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
454         {
455             p_pic = p_vout->p_picture + i_index;
456             break;
457         }
458     }
459
460     if( p_pic == NULL )
461     {
462         return 0;
463     }
464
465     p_vout->p_sys->i_index = 0;
466     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[0];
467     p_pic->p->i_lines = p_vout->p_sys->i_height;
468     p_pic->p->b_margin = 0;
469     p_pic->p->i_pitch = p_vout->p_sys->p_window->bitmap[0]->BytesPerRow();
470     p_pic->p->i_pixel_bytes = p_pic->p->i_pitch / p_vout->p_sys->p_window->bitmap[0]->Bounds().IntegerWidth();
471
472     p_pic->i_planes = colspace[p_vout->p_sys->p_window->colspace_index].planes;
473
474     p_pic->i_status = DESTROYED_PICTURE;
475     p_pic->i_type   = DIRECT_PICTURE;
476
477     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
478
479     I_OUTPUTPICTURES++;
480
481     return( 0 );
482 }
483
484 /*****************************************************************************
485  * vout_End: terminate BeOS video thread output method
486  *****************************************************************************/
487 void vout_End( vout_thread_t *p_vout )
488 {
489     BeosCloseDisplay( p_vout );
490 }
491
492 /*****************************************************************************
493  * vout_Destroy: destroy BeOS video thread output method
494  *****************************************************************************
495  * Terminate an output method created by DummyCreateOutputMethod
496  *****************************************************************************/
497 void vout_Destroy( vout_thread_t *p_vout )
498 {
499     free( p_vout->p_sys );
500 }
501
502 /*****************************************************************************
503  * vout_Manage: handle BeOS events
504  *****************************************************************************
505  * This function should be called regularly by video output thread. It manages
506  * console events. It returns a non null value on error.
507  *****************************************************************************/
508 int vout_Manage( vout_thread_t *p_vout )
509 {
510                           
511     return( 0 );
512 }
513
514 /*****************************************************************************
515  * vout_Render: render previously calculated output
516  *****************************************************************************/
517 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
518 {
519     ;
520 }
521
522 /*****************************************************************************
523  * vout_Display: displays previously rendered output
524  *****************************************************************************
525  * This function send the currently rendered image to BeOS image, waits until
526  * it is displayed and switch the two rendering buffers, preparing next frame.
527  *****************************************************************************/
528 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
529 {
530     VideoWindow * p_win = p_vout->p_sys->p_window;
531
532     /* draw buffer if required */    
533     if (!p_win->teardownwindow)
534     { 
535        p_win->drawBuffer(p_vout->p_sys->i_index);
536     }
537     /* change buffer */
538     p_vout->p_sys->i_index = ++p_vout->p_sys->i_index & 1;
539     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index];
540 }
541
542 /* following functions are local */
543
544 /*****************************************************************************
545  * BeosOpenDisplay: open and initialize BeOS device
546  *****************************************************************************/
547 static int BeosOpenDisplay( vout_thread_t *p_vout )
548
549
550     p_vout->p_sys->p_window = new VideoWindow( p_vout->p_sys->i_width - 1,
551                                                p_vout->p_sys->i_height - 1,
552                                                BRect( 20, 50,
553                                                       20 + p_vout->i_window_width -1, 
554                                                       50 + p_vout->i_window_height ));
555
556     if( p_vout->p_sys->p_window == NULL )
557     {
558         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
559         return( 1 );
560     }   
561     
562     return( 0 );
563 }
564
565 /*****************************************************************************
566  * BeosDisplay: close and reset BeOS device
567  *****************************************************************************
568  * Returns all resources allocated by BeosOpenDisplay and restore the original
569  * state of the device.
570  *****************************************************************************/
571 static void BeosCloseDisplay( vout_thread_t *p_vout )
572 {    
573     /* Destroy the video window */
574     delete p_vout->p_sys->p_window;
575 }
576
577
578
579 } /* extern "C" */