]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
468fc73f041f31dba319a85f0885ec4f36d43edf
[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.25 2001/04/29 17:03:20 sam 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     if (w->Lock())
130     {
131       if( w->fDirty )
132       {
133         if(w->fUsingOverlay)
134         {
135           rgb_color key;
136            w->view->SetViewOverlay( w->bitmap[w->i_buffer_index],
137                                     w->bitmap[w->i_buffer_index]->Bounds(),
138                                     w->Bounds(),
139                                     &key,
140                                     B_FOLLOW_ALL,
141                                                     B_OVERLAY_FILTER_HORIZONTAL | B_OVERLAY_FILTER_VERTICAL
142                                                     | B_OVERLAY_TRANSFER_CHANNEL );
143            w->view->SetViewColor(key);
144          }
145          else
146          {
147            w->view->DrawBitmap( w->bitmap[w->i_buffer_index],
148                                 w->bitmap[w->i_buffer_index]->Bounds(),
149                                 w->Bounds());
150          }
151          
152          w->fDirty = false;
153        }
154      w->Unlock();
155    }
156    else  // we couldn't lock the window, it probably closed.
157      return B_ERROR;
158      
159    snooze (20000);
160  } // while
161
162   return B_OK;
163 }
164
165 /*****************************************************************************
166  * VideoWindow constructor and destructor
167  *****************************************************************************/
168
169 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
170         : BWindow(frame, name, B_TITLED_WINDOW, NULL)
171 {
172         float minWidth, minHeight, maxWidth, maxHeight; 
173
174     teardownwindow = false;
175     is_zoomed = false;
176     p_vout = p_video_output;
177         fDrawThreadID = NULL;
178         bitmap[0] = NULL;
179         bitmap[1] = NULL;
180         
181     rect = Frame();
182     view = new VLCView(Bounds());
183     AddChild(view);
184         bitmap[0] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL, B_YCbCr422);
185         bitmap[1] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY, B_YCbCr422);
186         fUsingOverlay = true;
187         i_screen_depth = 16;
188         p_vout->b_YCbr = true;
189         
190         if ((bitmap[0]->InitCheck() != B_OK) || (bitmap[1]->InitCheck() != B_OK))
191         {
192                 delete bitmap[0];
193                 delete bitmap[1];
194                 p_vout->b_YCbr = false;
195                 fUsingOverlay = false;
196                 BScreen *screen;
197                 screen = new BScreen();
198                 color_space space = screen->ColorSpace();
199                 delete screen;
200
201                 if(space == B_RGB15)
202                         {
203                         bitmap[0] = new BBitmap(Bounds(), B_RGB15);
204                         bitmap[1] = new BBitmap(Bounds(), B_RGB15);
205                 i_screen_depth = 15;
206                     }
207                     else if(space == B_RGB16)
208                         {
209                         bitmap[0] = new BBitmap(Bounds(), B_RGB16);
210                         bitmap[1] = new BBitmap(Bounds(), B_RGB16);
211                         i_screen_depth = 16;
212                         }
213                         else //default to 32bpp
214                         {
215                         bitmap[0] = new BBitmap(Bounds(), B_RGB32);
216                         bitmap[1] = new BBitmap(Bounds(), B_RGB32);
217                         i_screen_depth = 32;
218                         }
219                 SetTitle(VOUT_TITLE " (BBitmap output)");
220          }
221
222         if(fUsingOverlay)
223                 {
224                 rgb_color key;
225                 view->SetViewOverlay(bitmap[0], bitmap[0]->Bounds(), Bounds(), &key, B_FOLLOW_ALL,
226                                 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
227                 view->SetViewColor(key);
228                 SetTitle(VOUT_TITLE " (Overlay output)");
229                 GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 
230                 SetSizeLimits((float) Bounds().IntegerWidth(), maxWidth, (float) Bounds().IntegerHeight(), maxHeight);
231                 }
232 //      else
233                 {
234         fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
235                     B_DISPLAY_PRIORITY, (void*) this);
236         resume_thread(fDrawThreadID);
237         }
238
239         memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
240         memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
241         i_bytes_per_pixel = bitmap[0]->BytesPerRow()/bitmap[0]->Bounds().IntegerWidth();
242     fRowBytes = bitmap[0]->BytesPerRow();
243     fDirty = false;
244     Show();
245 }
246
247 VideoWindow::~VideoWindow()
248 {
249     int32 result;
250
251     Hide();
252     Sync();
253 //    if(!fUsingOverlay)
254 //      {
255             teardownwindow = true;
256             wait_for_thread(fDrawThreadID, &result);
257         delete bitmap[0];
258         delete bitmap[1];
259 //      }
260  }
261
262
263 /*****************************************************************************
264  * VideoWindow::FrameResized
265  *****************************************************************************/
266 void VideoWindow::FrameResized( float width, float height )
267 {
268 }
269
270 /*****************************************************************************
271  * VideoWindow::Zoom
272  *****************************************************************************/
273
274 void VideoWindow::Zoom(BPoint origin, float width, float height )
275 {
276 if(is_zoomed)
277         {
278         MoveTo(rect.left, rect.top);
279         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
280         be_app->ShowCursor();
281         }
282 else
283         {
284         rect = Frame();
285         BScreen *screen;
286         screen = new BScreen(this);
287         BRect rect = screen->Frame();
288         delete screen;
289         MoveTo(0,0);
290         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
291         be_app->HideCursor();
292         }
293 is_zoomed = !is_zoomed;
294 }
295
296 /*****************************************************************************
297  * VideoWindow::MessageReceived
298  *****************************************************************************/
299
300 void VideoWindow::MessageReceived( BMessage * p_message )
301 {
302     BWindow * p_win;
303     
304     switch( p_message->what )
305     {
306     case B_KEY_DOWN:
307     case B_SIMPLE_DATA:
308         // post the message to the interface window which will handle it
309         p_win = beos_GetAppWindow( "interface" );
310         if( p_win != NULL )
311         {
312             p_win->PostMessage( p_message );
313         }
314         break;
315     
316     default:
317         BWindow::MessageReceived( p_message );
318         break;
319     }
320 }
321
322 /*****************************************************************************
323  * VideoWindow::QuitRequested
324  *****************************************************************************/
325
326 bool VideoWindow::QuitRequested()
327 {
328     /* FIXME: send a message ! */
329     p_main->p_intf->b_die = 1;
330     teardownwindow = true;
331     return( false );
332 }
333
334 /*****************************************************************************
335  * VLCView::VLCView
336  *****************************************************************************/
337 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
338 {
339 SetViewColor(B_TRANSPARENT_32_BIT);
340 }
341
342 /*****************************************************************************
343  * VLCView::~VLCView
344  *****************************************************************************/
345 VLCView::~VLCView()
346 {
347
348 }
349
350 /*****************************************************************************
351  * VLCVIew::~VLCView
352  *****************************************************************************/
353 void VLCView::MouseDown(BPoint point)
354 {
355 VideoWindow *w = (VideoWindow *) Window();
356 if(w->is_zoomed)
357         {
358         BWindow *win = Window();
359         win->Zoom();
360         }
361 }
362
363 extern "C"
364 {
365
366 /*****************************************************************************
367  * Local prototypes
368  *****************************************************************************/
369 static int  vout_Probe      ( probedata_t *p_data );
370 static int  vout_Create     ( struct vout_thread_s * );
371 static int  vout_Init       ( struct vout_thread_s * );
372 static void vout_End        ( struct vout_thread_s * );
373 static void vout_Destroy    ( struct vout_thread_s * );
374 static int  vout_Manage     ( struct vout_thread_s * );
375 static void vout_Display    ( struct vout_thread_s * );
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->pf_probe = vout_Probe;
387     p_function_list->functions.vout.pf_create     = vout_Create;
388     p_function_list->functions.vout.pf_init       = vout_Init;
389     p_function_list->functions.vout.pf_end        = vout_End;
390     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
391     p_function_list->functions.vout.pf_manage     = vout_Manage;
392     p_function_list->functions.vout.pf_display    = vout_Display;
393     p_function_list->functions.vout.pf_setpalette = NULL;
394 }
395
396 /*****************************************************************************
397  * vout_Probe: probe the video driver and return a score
398  *****************************************************************************
399  * This function tries to initialize SDL and returns a score to the
400  * plugin manager so that it can select the best plugin.
401  *****************************************************************************/
402 static int vout_Probe( probedata_t *p_data )
403 {
404     if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
405     {
406         return( 999 );
407     }
408
409     return( 100 );
410 }
411
412 /*****************************************************************************
413  * vout_Create: allocates BeOS video thread output method
414  *****************************************************************************
415  * This function allocates and initializes a BeOS vout method.
416  *****************************************************************************/
417 int vout_Create( vout_thread_t *p_vout )
418 {
419     /* Allocate structure */
420     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
421     if( p_vout->p_sys == NULL )
422     {
423         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
424         return( 1 );
425     }
426     
427     /* Set video window's size */
428     p_vout->i_width =  main_GetIntVariable( VOUT_WIDTH_VAR,
429                                             VOUT_WIDTH_DEFAULT );
430     p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
431                                             VOUT_HEIGHT_DEFAULT );
432
433     /* Open and initialize device */
434     if( BeosOpenDisplay( p_vout ) )
435     {
436         intf_ErrMsg("vout error: can't open display");
437         free( p_vout->p_sys );
438         return( 1 );
439     }
440
441     return( 0 );
442 }
443
444 /*****************************************************************************
445  * vout_Init: initialize BeOS video thread output method
446  *****************************************************************************/
447 int vout_Init( vout_thread_t *p_vout )
448 {
449     VideoWindow * p_win = p_vout->p_sys->p_window;
450     u32 i_page_size;
451
452
453     i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
454     
455     p_vout->p_sys->i_width =         p_vout->i_width;
456     p_vout->p_sys->i_height =        p_vout->i_height;    
457
458 /*    if(p_win->fUsingOverlay)
459         {
460             if(p_win->bitmap[0] != NULL)
461                 {
462                     vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
463                          (byte_t *)p_win->bitmap[0]->Bits());
464                 delete p_win->bitmap[0];
465                 p_win->bitmap[0] = NULL;
466                 }
467         }
468     else
469                 {
470             if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
471                 {
472                 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
473                          (byte_t *)p_win->bitmap[1]->Bits());
474                 }
475         }*/
476             if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
477                 {
478                 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
479                          (byte_t *)p_win->bitmap[1]->Bits());
480                 }
481     
482     return( 0 );
483 }
484
485 /*****************************************************************************
486  * vout_End: terminate BeOS video thread output method
487  *****************************************************************************/
488 void vout_End( vout_thread_t *p_vout )
489 {
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     BeosCloseDisplay( p_vout );
500     
501     free( p_vout->p_sys );
502 }
503
504 /*****************************************************************************
505  * vout_Manage: handle BeOS events
506  *****************************************************************************
507  * This function should be called regularly by video output thread. It manages
508  * console events. It returns a non null value on error.
509  *****************************************************************************/
510 int vout_Manage( vout_thread_t *p_vout )
511 {
512 VideoWindow * p_win = p_vout->p_sys->p_window;
513 rgb_color key;
514 float minWidth, minHeight, maxWidth, maxHeight; 
515
516 if( (p_vout->i_width  != p_vout->p_sys->i_width) ||
517              (p_vout->i_height != p_vout->p_sys->i_height) )
518     {
519         /* If video output size has changed, change interface window size */
520         intf_DbgMsg( "resizing output window" );
521         if(p_win->fUsingOverlay)
522                 {
523                 p_win->Lock();
524                 p_win->view->ClearViewOverlay();
525                 delete p_win->bitmap[0];
526                 delete p_win->bitmap[1];
527                 p_vout->p_sys->i_width =    p_vout->i_width;
528                 p_vout->p_sys->i_height =   p_vout->i_height;;
529                         p_win->GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 
530                         p_win->SetSizeLimits((float) p_vout->p_sys->i_width, maxWidth, (float) p_vout->p_sys->i_height, maxHeight);       
531                 p_win->ResizeTo(p_vout->p_sys->i_width, p_vout->p_sys->i_height);
532                 p_win->bitmap[0] = new BBitmap(p_win->Bounds(),
533                                                 B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL,
534                                                 B_YCbCr422);
535                 p_win->bitmap[0] = new BBitmap(p_win->Bounds(),
536                                                 B_BITMAP_WILL_OVERLAY, B_YCbCr422);
537                         memset(p_win->bitmap[0]->Bits(), 0, p_win->bitmap[0]->BitsLength());
538                         memset(p_win->bitmap[1]->Bits(), 0, p_win->bitmap[1]->BitsLength());
539                         p_win->view->SetViewOverlay(p_win->bitmap[0], p_win->bitmap[0]->Bounds(), p_win->Bounds(), &key, B_FOLLOW_ALL,
540                                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|B_OVERLAY_TRANSFER_CHANNEL);
541                         p_win->view->SetViewColor(key);
542                         p_win->Unlock();
543                     vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
544                                  (byte_t *)p_win->bitmap[0]->Bits());
545                     delete p_win->bitmap[0];
546                     }
547             }
548 return( 0 );
549 }
550
551 /*****************************************************************************
552  * vout_Display: displays previously rendered output
553  *****************************************************************************
554  * This function send the currently rendered image to BeOS image, waits until
555  * it is displayed and switch the two rendering buffers, preparing next frame.
556  *****************************************************************************/
557 void vout_Display( vout_thread_t *p_vout )
558 {
559     VideoWindow * p_win = p_vout->p_sys->p_window;
560     
561         p_win->i_buffer_index = p_vout->i_buffer_index;
562         if(p_win->fUsingOverlay)
563                 p_vout->i_buffer_index = p_vout->i_buffer_index & 1;
564         else
565                 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
566     p_win->fDirty = true;
567 }
568
569 /* following functions are local */
570
571 /*****************************************************************************
572  * BeosOpenDisplay: open and initialize BeOS device
573  *****************************************************************************
574  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
575  * display video, providing the card is configured and the mode ok. It is
576  * not portable, and is not supposed to work with many cards. Use at your
577  * own risk !
578  *****************************************************************************/
579
580 static int BeosOpenDisplay( vout_thread_t *p_vout )
581
582     p_vout->p_sys->p_window =
583         new VideoWindow(  BRect( 80, 50, 80+p_vout->i_width-1, 50+p_vout->i_height-1 ), NULL, p_vout );
584     if( p_vout->p_sys->p_window == 0 )
585     {
586         free( p_vout->p_sys );
587         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
588         return( 1 );
589     }   
590     VideoWindow * p_win = p_vout->p_sys->p_window;
591     
592     p_vout->i_screen_depth =         p_win->i_screen_depth;
593     p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
594     p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
595     
596     switch( p_vout->i_screen_depth )
597     {
598     case 8:
599         intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
600         break;
601     case 15:
602         p_vout->i_red_mask =        0x7c00;
603         p_vout->i_green_mask =      0x03e0;
604         p_vout->i_blue_mask =       0x001f;
605         break;
606     case 16:
607         p_vout->i_red_mask =        0xf800;
608         p_vout->i_green_mask =      0x07e0;
609         p_vout->i_blue_mask =       0x001f;
610         break;
611     case 24:
612     case 32:
613     default:
614         p_vout->i_red_mask =        0xff0000;
615         p_vout->i_green_mask =      0x00ff00;
616         p_vout->i_blue_mask =       0x0000ff;
617         break;
618     }
619     return( 0 );
620 }
621
622 /*****************************************************************************
623  * BeosDisplay: close and reset BeOS device
624  *****************************************************************************
625  * Returns all resources allocated by BeosOpenDisplay and restore the original
626  * state of the device.
627  *****************************************************************************/
628 static void BeosCloseDisplay( vout_thread_t *p_vout )
629 {    
630     /* Destroy the video window */
631     p_vout->p_sys->p_window->Lock();
632     p_vout->p_sys->p_window->Quit();
633 }
634
635 } /* extern "C" */