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