]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
6d89bde41df0bc84965dd7968099d657ceddb9a9
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <stdio.h>
32 #include <string.h>                                            /* strerror() */
33 #include <kernel/OS.h>
34 #include <View.h>
35 #include <Application.h>
36 #include <DirectWindow.h>
37 #include <Locker.h>
38 #include <malloc.h>
39 #include <string.h>
40
41 extern "C"
42 {
43 #include "config.h"
44 #include "common.h"
45 #include "threads.h"
46 #include "mtime.h"
47 #include "tests.h"
48 #include "modules.h"
49
50 #include "video.h"
51 #include "video_output.h"
52
53 #include "interface.h" /* XXX maybe to remove if beos_window.h is splitted */
54 #include "intf_msg.h"
55
56 #include "main.h"
57 }
58
59 #include "beos_window.h"
60
61 #define WIDTH 128
62 #define HEIGHT 64
63 #define BITS_PER_PLANE 16
64 #define BYTES_PER_PIXEL 2
65
66 /*****************************************************************************
67  * vout_sys_t: BeOS video output method descriptor
68  *****************************************************************************
69  * This structure is part of the video output thread descriptor.
70  * It describes the BeOS specific properties of an output thread.
71  *****************************************************************************/
72  
73 typedef struct vout_sys_s
74 {
75     VideoWindow *         p_window;
76
77     byte_t *              pp_buffer[2];
78     s32                   i_width;
79     s32                   i_height;
80 } vout_sys_t;
81
82
83 /*****************************************************************************
84  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
85  *****************************************************************************/
86
87 BWindow *beos_GetAppWindow(char *name)
88 {
89     int32       index;
90     BWindow     *window;
91     
92     for (index = 0 ; ; index++)
93     {
94         window = be_app->WindowAt(index);
95         if (window == NULL)
96             break;
97         if (window->LockWithTimeout(200000) == B_OK)
98         {
99             if (strcmp(window->Name(), name) == 0)
100             {
101                 window->Unlock();
102                 break;
103             }
104             window->Unlock();
105         }
106     }
107     return window; 
108 }
109
110 /*****************************************************************************
111  * DrawingThread : thread that really does the drawing
112  *****************************************************************************/
113
114 int32 DrawingThread(void *data)
115 {
116     uint32 i, j, y;
117     uint64 *pp, *qq;
118     uint8 *p, *q;
119     uint32 byte_width;
120     uint32 height, bytes_per_line;
121     clipping_rect *clip;
122
123     VideoWindow *w;
124     w = (VideoWindow*) data;
125     
126     while(!w->fConnectionDisabled)
127     {
128         w->locker->Lock();
129         if( w->fConnected )
130         {
131             if( w->fDirty && (!w->fReady || w->i_screen_depth != w->p_vout->i_screen_depth) )
132             {
133                 bytes_per_line = w->fRowBytes;
134                 for( i=0 ; i < w->fNumClipRects ; i++ )
135                 {
136                     clip = &(w->fClipList[i]);
137                     height = clip->bottom - clip->top +1;
138                     byte_width = w->i_bytes_per_pixel * ((clip->right - clip->left)+1);
139                     p = w->fBits + clip->top*w->fRowBytes + clip->left * w->i_bytes_per_pixel;
140                     for( y=0 ; y < height ; )
141                     {
142                         pp = (uint64*) p;
143                         for( j=0 ; j < byte_width/64 ; j++ )
144                         {
145                             *pp++ = 0;
146                             *pp++ = 0; 
147                             *pp++ = 0;
148                             *pp++ = 0; 
149                             *pp++ = 0;
150                             *pp++ = 0; 
151                             *pp++ = 0;
152                             *pp++ = 0; 
153                         }
154                         memset( pp , 0, byte_width & 63 );
155                         y++;
156                         p += bytes_per_line;
157                     }
158                 }
159             }
160             else if( w->fDirty )
161             {
162                 bytes_per_line = w->fRowBytes;
163                 for( i=0 ; i < w->fNumClipRects ; i++ )
164                 {
165                     clip = &(w->fClipList[i]);
166                     height = clip->bottom - clip->top +1;
167                     byte_width = w->i_bytes_per_pixel * ((clip->right - clip->left)+1);
168                     p = w->fBits + clip->top * bytes_per_line + clip->left * w->i_bytes_per_pixel;
169                     q = w->p_vout->p_sys->pp_buffer[ !w->p_vout->i_buffer_index ] +
170                         clip->top * w->p_vout->i_bytes_per_line + clip->left *
171                         w->p_vout->i_bytes_per_pixel;
172                     for( y=0 ; y < height ; )
173                     {
174                         pp = (uint64*) p;
175                         qq = (uint64*) q;
176                         for( j=0 ; j < byte_width/64 ; j++ )
177                         {
178                             *pp++ = *qq++;
179                             *pp++ = *qq++; 
180                             *pp++ = *qq++;
181                             *pp++ = *qq++; 
182                             *pp++ = *qq++;
183                             *pp++ = *qq++; 
184                             *pp++ = *qq++;
185                             *pp++ = *qq++; 
186                         }
187                         memcpy( pp , qq, byte_width & 63 );
188                         y++;
189                         p += bytes_per_line;
190                         q += w->p_vout->p_sys->i_width * w->p_vout->i_bytes_per_pixel;
191                     }
192                 }
193             }
194             w->fDirty = false;
195         }
196         w->locker->Unlock();
197         snooze( 20000 );
198     }
199     return B_OK;
200 }
201
202 /*****************************************************************************
203  * VideoWindow constructor and destructor
204  *****************************************************************************/
205
206 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
207         : BDirectWindow(frame, name, B_TITLED_WINDOW, B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
208 {
209     BView * view;
210
211     fReady = false;
212     fConnected = false;
213     fConnectionDisabled = false;
214     locker = new BLocker();
215     fClipList = NULL;
216     fNumClipRects = 0;
217     p_vout = p_video_output;
218
219     view = new BView(Bounds(), "", B_FOLLOW_ALL, B_WILL_DRAW);
220     view->SetViewColor(B_TRANSPARENT_32_BIT);
221     AddChild(view);
222 /*
223     if(!SupportsWindowMode())
224     {
225         SetFullScreen(true);
226     }
227 */
228     fDirty = false;
229     fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
230                     B_DISPLAY_PRIORITY, (void*) this);
231     resume_thread(fDrawThreadID);
232     Show();
233 }
234
235 VideoWindow::~VideoWindow()
236 {
237     int32 result;
238
239     fConnectionDisabled = true;
240     Hide();
241     Sync();
242     wait_for_thread(fDrawThreadID, &result);
243     free(fClipList);
244     delete locker;
245 }
246
247 /*****************************************************************************
248  * VideoWindow::DirectConnected
249  *****************************************************************************/
250
251 void VideoWindow::DirectConnected(direct_buffer_info *info)
252 {
253     unsigned int i;
254
255     if(!fConnected && fConnectionDisabled)
256     {
257         return;
258     }
259     locker->Lock();
260
261     switch(info->buffer_state & B_DIRECT_MODE_MASK)
262     {
263     case B_DIRECT_START:
264         fConnected = true;
265     case B_DIRECT_MODIFY:
266         fBits = (uint8*)((char*)info->bits +
267         (info->window_bounds.top) * info->bytes_per_row +
268         (info->window_bounds.left) * (info->bits_per_pixel>>3));;
269         
270         i_bytes_per_pixel = info->bits_per_pixel >> 3;
271         i_screen_depth = info->bits_per_pixel;
272         
273         fRowBytes = info->bytes_per_row;
274         fFormat = info->pixel_format;
275         fBounds = info->window_bounds;
276         fDirty = true;
277
278         if(fClipList)
279         {
280             free(fClipList);
281             fClipList = NULL;
282         }
283         fNumClipRects = info->clip_list_count;
284         fClipList = (clipping_rect*) malloc(fNumClipRects*sizeof(clipping_rect));
285         for( i=0 ; i<info->clip_list_count ; i++ )
286         {
287             fClipList[i].top = info->clip_list[i].top - info->window_bounds.top;
288             fClipList[i].left = info->clip_list[i].left - info->window_bounds.left;
289             fClipList[i].bottom = info->clip_list[i].bottom - info->window_bounds.top;
290             fClipList[i].right = info->clip_list[i].right - info->window_bounds.left;
291         }
292         break;
293     case B_DIRECT_STOP:
294         fConnected = false;
295         break;
296     }
297     locker->Unlock();
298 }
299
300 /*****************************************************************************
301  * VideoWindow::FrameResized
302  *****************************************************************************/
303
304 void VideoWindow::FrameResized( float width, float height )
305 {
306     b_resized = 1;
307 }
308
309 /*****************************************************************************
310  * VideoWindow::MessageReceived
311  *****************************************************************************/
312
313 void VideoWindow::MessageReceived( BMessage * p_message )
314 {
315     BWindow * p_win;
316     
317     switch( p_message->what )
318     {
319     case B_KEY_DOWN:
320     case B_SIMPLE_DATA:
321         // post the message to the interface window which will handle it
322         p_win = beos_GetAppWindow( "interface" );
323         if( p_win != NULL )
324         {
325             p_win->PostMessage( p_message );
326         }
327         break;
328     
329     default:
330         BWindow::MessageReceived( p_message );
331         break;
332     }
333 }
334
335 /*****************************************************************************
336  * VideoWindow::QuitRequested
337  *****************************************************************************/
338
339 bool VideoWindow::QuitRequested()
340 {
341     /* FIXME: send a message ! */
342     p_main->p_intf->b_die = 1;
343
344     return( false );
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 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     p_win->locker->Lock();
437
438     i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
439     
440     p_vout->p_sys->i_width =         p_vout->i_width;
441     p_vout->p_sys->i_height =        p_vout->i_height;    
442
443     /* Allocate memory for the 2 display buffers */
444     p_vout->p_sys->pp_buffer[0] = (byte_t*) malloc( i_page_size );
445     p_vout->p_sys->pp_buffer[1] = (byte_t*) malloc( i_page_size );
446     if( p_vout->p_sys->pp_buffer[0] == NULL  || p_vout->p_sys->pp_buffer[0] == NULL )
447     {
448         intf_ErrMsg("vout error: can't allocate video memory (%s)", strerror(errno) );
449         if( p_vout->p_sys->pp_buffer[0] != NULL ) free( p_vout->p_sys->pp_buffer[0] );
450         if( p_vout->p_sys->pp_buffer[1] != NULL ) free( p_vout->p_sys->pp_buffer[1] );
451         p_win->locker->Unlock();
452         return( 1 );
453     }
454
455     /* Set and initialize buffers */
456     vout_SetBuffers( p_vout, p_vout->p_sys->pp_buffer[0],
457                      p_vout->p_sys->pp_buffer[1] );
458
459     p_win->locker->Unlock();
460     return( 0 );
461 }
462
463 /*****************************************************************************
464  * vout_End: terminate BeOS video thread output method
465  *****************************************************************************/
466 void vout_End( vout_thread_t *p_vout )
467 {
468    VideoWindow * p_win = p_vout->p_sys->p_window;
469    
470    p_win->Lock();
471    
472    free( p_vout->p_sys->pp_buffer[0] );
473    free( p_vout->p_sys->pp_buffer[1] );
474
475    p_win->fReady = false;
476    p_win->Unlock();   
477 }
478
479 /*****************************************************************************
480  * vout_Destroy: destroy BeOS video thread output method
481  *****************************************************************************
482  * Terminate an output method created by DummyCreateOutputMethod
483  *****************************************************************************/
484 void vout_Destroy( vout_thread_t *p_vout )
485 {
486     BeosCloseDisplay( p_vout );
487     
488     free( p_vout->p_sys );
489 }
490
491 /*****************************************************************************
492  * vout_Manage: handle BeOS events
493  *****************************************************************************
494  * This function should be called regularly by video output thread. It manages
495  * console events. It returns a non null value on error.
496  *****************************************************************************/
497 int vout_Manage( vout_thread_t *p_vout )
498 {
499     if( p_vout->p_sys->p_window->b_resized )
500     {
501         p_vout->p_sys->p_window->b_resized = 0;
502         p_vout->i_changes |= VOUT_SIZE_CHANGE;
503     }
504
505     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
506     {
507         intf_WarnMsg( 1, "resizing window" );
508         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
509
510         /* Resize window */
511         p_vout->p_sys->p_window->ResizeTo( p_vout->i_width, p_vout->i_height );
512
513         /* Destroy XImages to change their size */
514         vout_End( p_vout );
515
516         /* Recreate XImages. If SysInit failed, the thread can't go on. */
517         if( vout_Init( p_vout ) )
518         {
519             intf_ErrMsg( "error: can't resize display" );
520             return( 1 );
521         }
522
523         /* Tell the video output thread that it will need to rebuild YUV
524          * tables. This is needed since convertion buffer size may have
525          * changed */
526         p_vout->i_changes |= VOUT_YUV_CHANGE;
527         intf_Msg( "vout: video display resized (%dx%d)",
528                   p_vout->i_width, p_vout->i_height );
529     }
530
531     return( 0 );
532 }
533
534 /*****************************************************************************
535  * vout_Display: displays previously rendered output
536  *****************************************************************************
537  * This function send the currently rendered image to BeOS image, waits until
538  * it is displayed and switch the two rendering buffers, preparing next frame.
539  *****************************************************************************/
540 void vout_Display( vout_thread_t *p_vout )
541 {
542     VideoWindow * p_win = p_vout->p_sys->p_window;
543     
544     p_win->locker->Lock();
545     p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
546     p_win->fReady = true;
547     p_win->fDirty = true;
548     p_win->locker->Unlock();
549 }
550
551 /* following functions are local */
552
553 /*****************************************************************************
554  * BeosOpenDisplay: open and initialize BeOS device
555  *****************************************************************************
556  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
557  * display video, providing the card is configured and the mode ok. It is
558  * not portable, and is not supposed to work with many cards. Use at your
559  * own risk !
560  *****************************************************************************/
561
562 static int BeosOpenDisplay( vout_thread_t *p_vout )
563
564     /* Create the DirectDraw video window */
565     p_vout->p_sys->p_window =
566         new VideoWindow(  BRect( 50, 150, 50+p_vout->i_width-1, 150+p_vout->i_height-1 ), VOUT_TITLE " (BeOS output) - drop a file here to play it !", p_vout );
567     if( p_vout->p_sys->p_window == 0 )
568     {
569         free( p_vout->p_sys );
570         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
571         return( 1 );
572     }   
573     VideoWindow * p_win = p_vout->p_sys->p_window;
574     
575     /* Wait until DirectConnected has been called */
576     while( !p_win->fConnected )
577         snooze( 50000 );
578
579     p_vout->i_screen_depth =         p_win->i_screen_depth;
580     p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
581     p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
582     
583     switch( p_vout->i_screen_depth )
584     {
585     case 8:
586         intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
587         break;
588     case 15:
589         p_vout->i_red_mask =        0x7c00;
590         p_vout->i_green_mask =      0x03e0;
591         p_vout->i_blue_mask =       0x001f;
592         break;
593     case 16:
594         p_vout->i_red_mask =        0xf800;
595         p_vout->i_green_mask =      0x07e0;
596         p_vout->i_blue_mask =       0x001f;
597         break;
598     case 24:
599     case 32:
600     default:
601         p_vout->i_red_mask =        0xff0000;
602         p_vout->i_green_mask =      0x00ff00;
603         p_vout->i_blue_mask =       0x0000ff;
604         break;
605     }
606
607     return( 0 );
608 }
609
610 /*****************************************************************************
611  * BeosDisplay: close and reset BeOS device
612  *****************************************************************************
613  * Returns all resources allocated by BeosOpenDisplay and restore the original
614  * state of the device.
615  *****************************************************************************/
616 static void BeosCloseDisplay( vout_thread_t *p_vout )
617 {    
618     /* Destroy the video window */
619     p_vout->p_sys->p_window->Lock();
620     p_vout->p_sys->p_window->Quit();
621 }
622
623 } /* extern "C" */