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