]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
vout_beos now uses double buffering for YCbCr overlay.
[vlc] / 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.24 2001/04/02 22:40:07 richards 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 #define MODULE_NAME beos
28 #include "modules_inner.h"
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include "defs.h"
34
35 #include <errno.h>                                                 /* ENOMEM */
36 #include <stdlib.h>                                                /* free() */
37 #include <stdio.h>
38 #include <string.h>                                            /* strerror() */
39 #include <kernel/OS.h>
40 #include <View.h>
41 #include <Application.h>
42 #include <Window.h>
43 #include <Locker.h>
44 #include <Screen.h>
45 #include <malloc.h>
46 #include <string.h>
47
48 extern "C"
49 {
50 #include "config.h"
51 #include "common.h"
52 #include "threads.h"
53 #include "mtime.h"
54 #include "tests.h"
55 #include "modules.h"
56
57 #include "video.h"
58 #include "video_output.h"
59
60 #include "interface.h"
61 #include "intf_msg.h"
62
63 #include "main.h"
64 }
65
66 #include "VideoWindow.h"
67 #include <Screen.h>
68
69 #define WIDTH 128
70 #define HEIGHT 64
71 #define BITS_PER_PLANE 16
72 #define BYTES_PER_PIXEL 2
73
74 /*****************************************************************************
75  * vout_sys_t: BeOS video output method descriptor
76  *****************************************************************************
77  * This structure is part of the video output thread descriptor.
78  * It describes the BeOS specific properties of an output thread.
79  *****************************************************************************/
80  
81 typedef struct vout_sys_s
82 {
83     VideoWindow *         p_window;
84
85     byte_t *              pp_buffer[2];
86     s32                   i_width;
87     s32                   i_height;
88 } vout_sys_t;
89
90
91 /*****************************************************************************
92  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
93  *****************************************************************************/
94
95 BWindow *beos_GetAppWindow(char *name)
96 {
97     int32       index;
98     BWindow     *window;
99     
100     for (index = 0 ; ; index++)
101     {
102         window = be_app->WindowAt(index);
103         if (window == NULL)
104             break;
105         if (window->LockWithTimeout(200000) == B_OK)
106         {
107             if (strcmp(window->Name(), name) == 0)
108             {
109                 window->Unlock();
110                 break;
111             }
112             window->Unlock();
113         }
114     }
115     return window; 
116 }
117
118 /*****************************************************************************
119  * DrawingThread : thread that really does the drawing
120  *****************************************************************************/
121
122 int32 DrawingThread(void *data)
123 {
124     VideoWindow *w;
125     w = (VideoWindow*) data;
126     
127     while(!w->teardownwindow)
128     {
129     w->Lock();
130        if( w->fDirty )
131             {
132                 if(w->fUsingOverlay)
133                                 {
134                                 rgb_color key;
135                                 w->view->SetViewOverlay(w->bitmap[w->i_buffer_index], w->bitmap[w->i_buffer_index]->Bounds(), w->Bounds(), &key, B_FOLLOW_ALL,
136                                 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|B_OVERLAY_TRANSFER_CHANNEL);
137                                 w->view->SetViewColor(key);
138                                 }
139                 else
140                         w->view->DrawBitmap(w->bitmap[w->i_buffer_index], w->bitmap[w->i_buffer_index]->Bounds(), w->Bounds());
141             w->fDirty = false;
142             }
143     w->Unlock();
144         snooze(20000);
145     }
146     return B_OK;
147 }
148
149 /*****************************************************************************
150  * VideoWindow constructor and destructor
151  *****************************************************************************/
152
153 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
154         : BWindow(frame, name, B_TITLED_WINDOW, NULL)
155 {
156         float minWidth, minHeight, maxWidth, maxHeight; 
157
158     teardownwindow = false;
159     is_zoomed = false;
160     p_vout = p_video_output;
161         fDrawThreadID = NULL;
162         bitmap[0] = NULL;
163         bitmap[1] = NULL;
164         
165     rect = Frame();
166     view = new VLCView(Bounds());
167     AddChild(view);
168         bitmap[0] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL, B_YCbCr422);
169         bitmap[1] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY, B_YCbCr422);
170         fUsingOverlay = true;
171         i_screen_depth = 16;
172         p_vout->b_YCbr = true;
173         
174         if ((bitmap[0]->InitCheck() != B_OK) || (bitmap[1]->InitCheck() != B_OK))
175         {
176                 delete bitmap[0];
177                 delete bitmap[1];
178                 p_vout->b_YCbr = false;
179                 fUsingOverlay = false;
180                 BScreen *screen;
181                 screen = new BScreen();
182                 color_space space = screen->ColorSpace();
183                 delete screen;
184
185                 if(space == B_RGB15)
186                         {
187                         bitmap[0] = new BBitmap(Bounds(), B_RGB15);
188                         bitmap[1] = new BBitmap(Bounds(), B_RGB15);
189                 i_screen_depth = 15;
190                     }
191                     else if(space == B_RGB16)
192                         {
193                         bitmap[0] = new BBitmap(Bounds(), B_RGB16);
194                         bitmap[1] = new BBitmap(Bounds(), B_RGB16);
195                         i_screen_depth = 16;
196                         }
197                         else //default to 32bpp
198                         {
199                         bitmap[0] = new BBitmap(Bounds(), B_RGB32);
200                         bitmap[1] = new BBitmap(Bounds(), B_RGB32);
201                         i_screen_depth = 32;
202                         }
203                 SetTitle(VOUT_TITLE " (BBitmap output)");
204          }
205
206         if(fUsingOverlay)
207                 {
208                 rgb_color key;
209                 view->SetViewOverlay(bitmap[0], bitmap[0]->Bounds(), Bounds(), &key, B_FOLLOW_ALL,
210                                 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
211                 view->SetViewColor(key);
212                 SetTitle(VOUT_TITLE " (Overlay output)");
213                 GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 
214                 SetSizeLimits((float) Bounds().IntegerWidth(), maxWidth, (float) Bounds().IntegerHeight(), maxHeight);
215                 }
216 //      else
217                 {
218         fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
219                     B_DISPLAY_PRIORITY, (void*) this);
220         resume_thread(fDrawThreadID);
221         }
222
223         memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
224         memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
225         i_bytes_per_pixel = bitmap[0]->BytesPerRow()/bitmap[0]->Bounds().IntegerWidth();
226     fRowBytes = bitmap[0]->BytesPerRow();
227     fDirty = false;
228     Show();
229 }
230
231 VideoWindow::~VideoWindow()
232 {
233     int32 result;
234
235     Hide();
236     Sync();
237 //    if(!fUsingOverlay)
238 //      {
239             teardownwindow = true;
240             wait_for_thread(fDrawThreadID, &result);
241         delete bitmap[0];
242         delete bitmap[1];
243 //      }
244  }
245
246
247 /*****************************************************************************
248  * VideoWindow::FrameResized
249  *****************************************************************************/
250 void VideoWindow::FrameResized( float width, float height )
251 {
252 }
253
254 /*****************************************************************************
255  * VideoWindow::Zoom
256  *****************************************************************************/
257
258 void VideoWindow::Zoom(BPoint origin, float width, float height )
259 {
260 if(is_zoomed)
261         {
262         MoveTo(rect.left, rect.top);
263         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
264         be_app->ShowCursor();
265         }
266 else
267         {
268         rect = Frame();
269         BScreen *screen;
270         screen = new BScreen(this);
271         BRect rect = screen->Frame();
272         delete screen;
273         MoveTo(0,0);
274         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
275         be_app->HideCursor();
276         }
277 is_zoomed = !is_zoomed;
278 }
279
280 /*****************************************************************************
281  * VideoWindow::MessageReceived
282  *****************************************************************************/
283
284 void VideoWindow::MessageReceived( BMessage * p_message )
285 {
286     BWindow * p_win;
287     
288     switch( p_message->what )
289     {
290     case B_KEY_DOWN:
291     case B_SIMPLE_DATA:
292         // post the message to the interface window which will handle it
293         p_win = beos_GetAppWindow( "interface" );
294         if( p_win != NULL )
295         {
296             p_win->PostMessage( p_message );
297         }
298         break;
299     
300     default:
301         BWindow::MessageReceived( p_message );
302         break;
303     }
304 }
305
306 /*****************************************************************************
307  * VideoWindow::QuitRequested
308  *****************************************************************************/
309
310 bool VideoWindow::QuitRequested()
311 {
312     /* FIXME: send a message ! */
313     p_main->p_intf->b_die = 1;
314     teardownwindow = true;
315     return( false );
316 }
317
318 /*****************************************************************************
319  * VLCView::VLCView
320  *****************************************************************************/
321 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
322 {
323 SetViewColor(B_TRANSPARENT_32_BIT);
324 }
325
326 /*****************************************************************************
327  * VLCView::~VLCView
328  *****************************************************************************/
329 VLCView::~VLCView()
330 {
331
332 }
333
334 /*****************************************************************************
335  * VLCVIew::~VLCView
336  *****************************************************************************/
337 void VLCView::MouseDown(BPoint point)
338 {
339 VideoWindow *w = (VideoWindow *) Window();
340 if(w->is_zoomed)
341         {
342         BWindow *win = Window();
343         win->Zoom();
344         }
345 }
346
347 extern "C"
348 {
349
350 /*****************************************************************************
351  * Local prototypes
352  *****************************************************************************/
353 static int  vout_Probe      ( probedata_t *p_data );
354 static int  vout_Create     ( struct vout_thread_s * );
355 static int  vout_Init       ( struct vout_thread_s * );
356 static void vout_End        ( struct vout_thread_s * );
357 static void vout_Destroy    ( struct vout_thread_s * );
358 static int  vout_Manage     ( struct vout_thread_s * );
359 static void vout_Display    ( struct vout_thread_s * );
360
361 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
362 static void BeosCloseDisplay( vout_thread_t *p_vout );
363
364 /*****************************************************************************
365  * Functions exported as capabilities. They are declared as static so that
366  * we don't pollute the namespace too much.
367  *****************************************************************************/
368 void _M( vout_getfunctions )( function_list_t * p_function_list )
369 {
370     p_function_list->pf_probe = vout_Probe;
371     p_function_list->functions.vout.pf_create     = vout_Create;
372     p_function_list->functions.vout.pf_init       = vout_Init;
373     p_function_list->functions.vout.pf_end        = vout_End;
374     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
375     p_function_list->functions.vout.pf_manage     = vout_Manage;
376     p_function_list->functions.vout.pf_display    = vout_Display;
377     p_function_list->functions.vout.pf_setpalette = NULL;
378 }
379
380 /*****************************************************************************
381  * vout_Probe: probe the video driver and return a score
382  *****************************************************************************
383  * This function tries to initialize SDL and returns a score to the
384  * plugin manager so that it can select the best plugin.
385  *****************************************************************************/
386 static int vout_Probe( probedata_t *p_data )
387 {
388     if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
389     {
390         return( 999 );
391     }
392
393     return( 100 );
394 }
395
396 /*****************************************************************************
397  * vout_Create: allocates BeOS video thread output method
398  *****************************************************************************
399  * This function allocates and initializes a BeOS vout method.
400  *****************************************************************************/
401 int vout_Create( vout_thread_t *p_vout )
402 {
403     /* Allocate structure */
404     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
405     if( p_vout->p_sys == NULL )
406     {
407         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
408         return( 1 );
409     }
410     
411     /* Set video window's size */
412     p_vout->i_width =  main_GetIntVariable( VOUT_WIDTH_VAR,
413                                             VOUT_WIDTH_DEFAULT );
414     p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
415                                             VOUT_HEIGHT_DEFAULT );
416
417     /* Open and initialize device */
418     if( BeosOpenDisplay( p_vout ) )
419     {
420         intf_ErrMsg("vout error: can't open display");
421         free( p_vout->p_sys );
422         return( 1 );
423     }
424
425     return( 0 );
426 }
427
428 /*****************************************************************************
429  * vout_Init: initialize BeOS video thread output method
430  *****************************************************************************/
431 int vout_Init( vout_thread_t *p_vout )
432 {
433     VideoWindow * p_win = p_vout->p_sys->p_window;
434     u32 i_page_size;
435
436
437     i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
438     
439     p_vout->p_sys->i_width =         p_vout->i_width;
440     p_vout->p_sys->i_height =        p_vout->i_height;    
441
442 /*    if(p_win->fUsingOverlay)
443         {
444             if(p_win->bitmap[0] != NULL)
445                 {
446                     vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
447                          (byte_t *)p_win->bitmap[0]->Bits());
448                 delete p_win->bitmap[0];
449                 p_win->bitmap[0] = NULL;
450                 }
451         }
452     else
453                 {
454             if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
455                 {
456                 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
457                          (byte_t *)p_win->bitmap[1]->Bits());
458                 }
459         }*/
460             if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
461                 {
462                 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
463                          (byte_t *)p_win->bitmap[1]->Bits());
464                 }
465     
466     return( 0 );
467 }
468
469 /*****************************************************************************
470  * vout_End: terminate BeOS video thread output method
471  *****************************************************************************/
472 void vout_End( vout_thread_t *p_vout )
473 {
474 }
475
476 /*****************************************************************************
477  * vout_Destroy: destroy BeOS video thread output method
478  *****************************************************************************
479  * Terminate an output method created by DummyCreateOutputMethod
480  *****************************************************************************/
481 void vout_Destroy( vout_thread_t *p_vout )
482 {
483     BeosCloseDisplay( p_vout );
484     
485     free( p_vout->p_sys );
486 }
487
488 /*****************************************************************************
489  * vout_Manage: handle BeOS events
490  *****************************************************************************
491  * This function should be called regularly by video output thread. It manages
492  * console events. It returns a non null value on error.
493  *****************************************************************************/
494 int vout_Manage( vout_thread_t *p_vout )
495 {
496 VideoWindow * p_win = p_vout->p_sys->p_window;
497 rgb_color key;
498 float minWidth, minHeight, maxWidth, maxHeight; 
499
500 if( (p_vout->i_width  != p_vout->p_sys->i_width) ||
501              (p_vout->i_height != p_vout->p_sys->i_height) )
502     {
503         /* If video output size has changed, change interface window size */
504         intf_DbgMsg( "resizing output window" );
505         if(p_win->fUsingOverlay)
506                 {
507                 p_win->Lock();
508                 p_win->view->ClearViewOverlay();
509                 delete p_win->bitmap[0];
510                 delete p_win->bitmap[1];
511                 p_vout->p_sys->i_width =    p_vout->i_width;
512                 p_vout->p_sys->i_height =   p_vout->i_height;;
513                         p_win->GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 
514                         p_win->SetSizeLimits((float) p_vout->p_sys->i_width, maxWidth, (float) p_vout->p_sys->i_height, maxHeight);       
515                 p_win->ResizeTo(p_vout->p_sys->i_width, p_vout->p_sys->i_height);
516                 p_win->bitmap[0] = new BBitmap(p_win->Bounds(),
517                                                 B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL,
518                                                 B_YCbCr422);
519                 p_win->bitmap[0] = new BBitmap(p_win->Bounds(),
520                                                 B_BITMAP_WILL_OVERLAY, B_YCbCr422);
521                         memset(p_win->bitmap[0]->Bits(), 0, p_win->bitmap[0]->BitsLength());
522                         memset(p_win->bitmap[1]->Bits(), 0, p_win->bitmap[1]->BitsLength());
523                         p_win->view->SetViewOverlay(p_win->bitmap[0], p_win->bitmap[0]->Bounds(), p_win->Bounds(), &key, B_FOLLOW_ALL,
524                                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|B_OVERLAY_TRANSFER_CHANNEL);
525                         p_win->view->SetViewColor(key);
526                         p_win->Unlock();
527                     vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
528                                  (byte_t *)p_win->bitmap[0]->Bits());
529                     delete p_win->bitmap[0];
530                     }
531             }
532 return( 0 );
533 }
534
535 /*****************************************************************************
536  * vout_Display: displays previously rendered output
537  *****************************************************************************
538  * This function send the currently rendered image to BeOS image, waits until
539  * it is displayed and switch the two rendering buffers, preparing next frame.
540  *****************************************************************************/
541 void vout_Display( vout_thread_t *p_vout )
542 {
543     VideoWindow * p_win = p_vout->p_sys->p_window;
544     
545         p_win->i_buffer_index = p_vout->i_buffer_index;
546         if(p_win->fUsingOverlay)
547                 p_vout->i_buffer_index = p_vout->i_buffer_index & 1;
548         else
549                 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
550     p_win->fDirty = true;
551 }
552
553 /* following functions are local */
554
555 /*****************************************************************************
556  * BeosOpenDisplay: open and initialize BeOS device
557  *****************************************************************************
558  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
559  * display video, providing the card is configured and the mode ok. It is
560  * not portable, and is not supposed to work with many cards. Use at your
561  * own risk !
562  *****************************************************************************/
563
564 static int BeosOpenDisplay( vout_thread_t *p_vout )
565
566     p_vout->p_sys->p_window =
567         new VideoWindow(  BRect( 80, 50, 80+p_vout->i_width-1, 50+p_vout->i_height-1 ), NULL, p_vout );
568     if( p_vout->p_sys->p_window == 0 )
569     {
570         free( p_vout->p_sys );
571         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
572         return( 1 );
573     }   
574     VideoWindow * p_win = p_vout->p_sys->p_window;
575     
576     p_vout->i_screen_depth =         p_win->i_screen_depth;
577     p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
578     p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
579     
580     switch( p_vout->i_screen_depth )
581     {
582     case 8:
583         intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
584         break;
585     case 15:
586         p_vout->i_red_mask =        0x7c00;
587         p_vout->i_green_mask =      0x03e0;
588         p_vout->i_blue_mask =       0x001f;
589         break;
590     case 16:
591         p_vout->i_red_mask =        0xf800;
592         p_vout->i_green_mask =      0x07e0;
593         p_vout->i_blue_mask =       0x001f;
594         break;
595     case 24:
596     case 32:
597     default:
598         p_vout->i_red_mask =        0xff0000;
599         p_vout->i_green_mask =      0x00ff00;
600         p_vout->i_blue_mask =       0x0000ff;
601         break;
602     }
603     return( 0 );
604 }
605
606 /*****************************************************************************
607  * BeosDisplay: close and reset BeOS device
608  *****************************************************************************
609  * Returns all resources allocated by BeosOpenDisplay and restore the original
610  * state of the device.
611  *****************************************************************************/
612 static void BeosCloseDisplay( vout_thread_t *p_vout )
613 {    
614     /* Destroy the video window */
615     p_vout->p_sys->p_window->Lock();
616     p_vout->p_sys->p_window->Quit();
617 }
618
619 } /* extern "C" */