]> git.sesse.net Git - vlc/blob - modules/gui/qnx/vout.c
Merge branch 1.0-bugfix
[vlc] / modules / gui / qnx / vout.c
1 /*****************************************************************************
2  * vout.c: QNX RTOS video output display method
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  *
6  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
7  *          Pascal Levesque <Pascal.Levesque@mindready.com>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28
29 #include <photon/PtWidget.h>
30 #include <photon/PtWindow.h>
31 #include <photon/PtLabel.h>
32 #include <photon/PdDirect.h>
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <vlc_common.h>
39 #include <vlc_interface.h>
40 #include <vlc_vout.h>
41
42 /*****************************************************************************
43  * vout_sys_t: video output QNX method descriptor
44  *****************************************************************************
45  * This structure is part of the video output thread descriptor.
46  * It describes the QNX specific properties of an output thread. QNX video
47  * output is performed through regular resizable windows. Windows can be
48  * dynamically resized to adapt to the size of the streams.
49  *****************************************************************************/
50 #define MAX_DIRECTBUFFERS 2
51
52 #define MODE_NORMAL_MEM     0
53 #define MODE_SHARED_MEM     1
54 #define MODE_VIDEO_MEM      2
55 #define MODE_VIDEO_OVERLAY  3
56
57 struct vout_sys_t
58 {
59     /* video mode */
60     int                     i_mode;
61
62     /* internal stuff */
63     PtWidget_t *            p_window;
64
65     /* Color palette for 8bpp */
66     PgColor_t p_colors[255];
67
68     /* [shared] memory blit */
69     int                     i_img_type;
70
71     /* video memory blit */
72
73     /* video overlay */
74     PgVideoChannel_t *      p_channel;
75     int                     i_vc_flags;
76     int                     i_vc_format;
77
78     int                 i_screen_depth;
79     int                 i_bytes_per_pixel;
80     int                 i_bytes_per_line;
81
82     /* position & dimensions */
83     PhPoint_t               pos;
84     PhDim_t                 dim;
85     PhPoint_t               old_pos;
86     PhDim_t                 old_dim;
87     PhDim_t                 screen_dim;
88     PhRect_t                frame;
89 };
90
91
92 /*****************************************************************************
93  * picture_sys_t: direct buffer method descriptor
94  *****************************************************************************
95  * This structure is part of the picture descriptor, it describes the
96  * XVideo specific properties of a direct buffer.
97  *****************************************************************************/
98 struct picture_sys_t
99 {
100     /* [shared] memory blit */
101     PhImage_t *             p_image;
102
103     /* video memory blit and video overlay */
104     PdOffscreenContext_t *  p_ctx[3];   /* 0: y, 1: u, 2: v */
105     char *                  p_buf[3];
106 };
107
108
109 /*****************************************************************************
110  * Local prototypes
111  *****************************************************************************/
112 static int  QNXInit      ( vout_thread_t * );
113 static void QNXEnd       ( vout_thread_t * );
114 static int  QNXManage    ( vout_thread_t * );
115 static void QNXDisplay   ( vout_thread_t *, picture_t * );
116
117 static int  QNXInitDisplay ( vout_thread_t * );
118 static int  QNXCreateWnd   ( vout_thread_t * );
119 static int  QNXDestroyWnd  ( vout_thread_t * );
120
121 static int  NewPicture     ( vout_thread_t *, picture_t *, int );
122 static void FreePicture    ( vout_thread_t *, picture_t * );
123 static int  ResizeOverlayOutput ( vout_thread_t * );
124 static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
125
126 /*****************************************************************************
127  * OpenVideo: allocate QNX video thread output method
128  *****************************************************************************
129  * This function allocate and initialize a QNX vout method. It uses some of the
130  * vout properties to choose the window size, and change them according to the
131  * actual properties of the display.
132  *****************************************************************************/
133 int OpenVideo ( vlc_object_t *p_this )
134 {
135     vout_thread_t * p_vout = (vout_thread_t *)p_this;
136
137     /* init connection to photon */
138     if( PtInit( "/dev/photon" ) != 0 )
139     {
140         msg_Err( p_vout, "unable to connect to photon" );
141         return( 1 );
142     }
143
144     /* allocate structure */
145     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
146     if( p_vout->p_sys == NULL )
147         return( 1 );
148
149     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
150
151     p_vout->b_fullscreen = config_GetInt( p_vout, "fullscreen" );
152     p_vout->p_sys->i_mode = config_GetInt( p_vout, "overlay" ) ?
153                                 MODE_VIDEO_OVERLAY : MODE_VIDEO_MEM;
154     p_vout->p_sys->dim.w = p_vout->i_window_width;
155     p_vout->p_sys->dim.h = p_vout->i_window_height;
156
157     /* init display and create window */
158     if( QNXInitDisplay( p_vout ) || QNXCreateWnd( p_vout ) )
159     {
160         free( p_vout->p_sys );
161         return( 1 );
162     }
163
164     p_vout->pf_init = QNXInit;
165     p_vout->pf_end = QNXEnd;
166     p_vout->pf_manage = QNXManage;
167     p_vout->pf_render = NULL;
168     p_vout->pf_display = QNXDisplay;
169
170     return( 0 );
171 }
172
173 /*****************************************************************************
174  * QNXInit: initialize QNX video thread output method
175  *****************************************************************************
176  * This function create the buffers needed by the output thread. It is called
177  * at the beginning of the thread, but also each time the window is resized.
178  *****************************************************************************/
179 static int QNXInit( vout_thread_t *p_vout )
180 {
181     int i_index;
182     picture_t *p_pic;
183
184     I_OUTPUTPICTURES = 0;
185
186     switch( p_vout->p_sys->i_mode )
187     {
188     case MODE_NORMAL_MEM:
189     case MODE_SHARED_MEM:
190         p_vout->output.i_width = p_vout->p_sys->dim.w;
191         p_vout->output.i_height = p_vout->p_sys->dim.h;
192
193         /* Assume we have square pixels */
194         p_vout->output.i_aspect = p_vout->p_sys->dim.w
195                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->dim.h;
196         break;
197
198     case MODE_VIDEO_MEM:
199         p_vout->output.i_width = p_vout->p_sys->dim.w;
200         p_vout->output.i_height = p_vout->p_sys->dim.h;
201
202         /* Assume we have square pixels */
203         p_vout->output.i_aspect = p_vout->p_sys->dim.w
204                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->dim.h;
205         break;
206
207     case MODE_VIDEO_OVERLAY:
208         p_vout->output.i_width  = p_vout->render.i_width;
209         p_vout->output.i_height = p_vout->render.i_height;
210         p_vout->output.i_aspect = p_vout->render.i_aspect;
211
212         if (ResizeOverlayOutput(p_vout))
213         {
214             return (1);
215         }
216         break;
217
218     default:
219         /* This shouldn't happen ! */
220         break;
221     }
222
223     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
224     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
225     {
226         p_pic = NULL;
227
228         /* Find an empty picture slot */
229         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
230         {
231             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
232             {
233                 p_pic = p_vout->p_picture + i_index;
234                 break;
235             }
236         }
237
238         /* Allocate the picture */
239         if( p_pic == NULL || NewPicture( p_vout, p_pic, I_OUTPUTPICTURES ) )
240         {
241             break;
242         }
243
244         p_pic->i_status = DESTROYED_PICTURE;
245         p_pic->i_type   = DIRECT_PICTURE;
246
247         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
248
249         I_OUTPUTPICTURES++;
250     }
251
252     return( 0 );
253 }
254
255 /*****************************************************************************
256  * QNXEnd: terminate QNX video thread output method
257  *****************************************************************************
258  * Destroy the buffers created by QNXInit. It is called at the end of
259  * the thread, but also each time the window is resized.
260  *****************************************************************************/
261 static void QNXEnd( vout_thread_t *p_vout )
262 {
263     int i_index;
264
265     /* Free the direct buffers we allocated */
266     for( i_index = I_OUTPUTPICTURES ; i_index ; )
267     {
268         i_index--;
269         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
270     }
271 }
272
273 /*****************************************************************************
274  * CloseVideo: destroy QNX video thread output method
275  *****************************************************************************
276  * Terminate an output method created by QNXCreate
277  *****************************************************************************/
278 void CloseVideo ( vlc_object_t *p_this )
279 {
280     vout_thread_t * p_vout = (vout_thread_t *)p_this;
281
282     /* destroy the window */
283     QNXDestroyWnd( p_vout );
284
285     /* destroy structure */
286     free( p_vout->p_sys );
287 }
288
289 /*****************************************************************************
290  * QNXManage: handle QNX events
291  *****************************************************************************
292  * This function should be called regularly by video output thread. It allows
293  * window resizing. It returns a non null value on error.
294  *****************************************************************************/
295 static int QNXManage( vout_thread_t *p_vout )
296 {
297     int i_ev,  i_buflen;
298     PhEvent_t *p_event;
299     bool b_repos = 0;
300
301     if (!vlc_object_alive (p_vout))
302     {
303         return ( 0 );
304     }
305
306     /* allocate buffer for event */
307     i_buflen = sizeof( PhEvent_t ) * 4;
308     if( ( p_event = malloc( i_buflen ) ) == NULL )
309         return( 1 );
310
311     /* event loop */
312     do
313     {
314         memset( p_event, 0, i_buflen );
315         i_ev = PhEventPeek( p_event, i_buflen );
316
317         if( i_ev == Ph_RESIZE_MSG )
318         {
319             PhEvent_t *buf;
320
321             i_buflen = PhGetMsgSize( p_event );
322             buf = realloc( p_event, i_buflen );
323             if( buf == NULL )
324             {
325                 free( p_event );
326                 return( 1 );
327             }
328             p_event = buf;
329         }
330         else if( i_ev == Ph_EVENT_MSG )
331         {
332             PtEventHandler( p_event );
333
334             if( p_event->type == Ph_EV_WM )
335             {
336                 PhWindowEvent_t *p_ev = PhGetData( p_event );
337
338                 switch( p_ev->event_f )
339                 {
340                 case Ph_WM_CLOSE:
341                     p_vout->p_libvlc->b_die = true;
342                     break;
343
344                 case Ph_WM_MOVE:
345                     p_vout->p_sys->pos.x = p_ev->pos.x;
346                     p_vout->p_sys->pos.y = p_ev->pos.y;
347                     b_repos = 1;
348                     break;
349
350                 case Ph_WM_RESIZE:
351                     p_vout->p_sys->old_dim.w = p_vout->p_sys->dim.w;
352                     p_vout->p_sys->old_dim.h = p_vout->p_sys->dim.h;
353                     p_vout->p_sys->dim.w = p_ev->size.w;
354                     p_vout->p_sys->dim.h = p_ev->size.h;
355                     p_vout->i_changes |= VOUT_SIZE_CHANGE;
356                     break;
357                 }
358             }
359             else if( p_event->type == Ph_EV_KEY )
360             {
361                 PhKeyEvent_t *p_ev = PhGetData( p_event );
362                 long i_key = p_ev->key_sym;
363
364                 if( ( p_ev->key_flags & Pk_KF_Key_Down ) &&
365                     ( p_ev->key_flags & Pk_KF_Sym_Valid ) )
366                 {
367                     switch( i_key )
368                     {
369                     case Pk_q:
370                     case Pk_Q:
371                         p_vout->p_libvlc->b_die = true;
372                         break;
373
374                     case Pk_f:
375                     case Pk_F:
376                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
377                         break;
378
379                     default:
380                         break;
381                     }
382                 }
383             }
384         }
385     } while( i_ev != -1 && i_ev != 0 );
386
387     free( p_event );
388
389     /*
390      * fullscreen
391      */
392     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
393     {
394         PhDim_t dim;
395
396         p_vout->b_fullscreen = !p_vout->b_fullscreen;
397         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
398
399         if( p_vout->b_fullscreen )
400         {
401             p_vout->p_sys->old_pos.x = p_vout->p_sys->pos.x;
402             p_vout->p_sys->old_pos.y = p_vout->p_sys->pos.y;
403             p_vout->p_sys->pos.x = p_vout->p_sys->pos.y = 0;
404             dim.w = p_vout->p_sys->screen_dim.w + 1;
405             dim.h = p_vout->p_sys->screen_dim.h + 1;
406         }
407         else
408         {
409             p_vout->p_sys->pos.x = p_vout->p_sys->old_pos.x;
410             p_vout->p_sys->pos.y = p_vout->p_sys->old_pos.y;
411             dim.w = p_vout->p_sys->old_dim.w + 1;
412             dim.h = p_vout->p_sys->old_dim.h + 1;
413         }
414
415         /* modify render flags, border */
416         PtSetResource( p_vout->p_sys->p_window,
417             Pt_ARG_WINDOW_RENDER_FLAGS,
418             p_vout->b_fullscreen ? Pt_FALSE : Pt_TRUE,
419             Ph_WM_RENDER_BORDER | Ph_WM_RENDER_TITLE );
420
421         /* set position and dimension */
422         PtSetResource( p_vout->p_sys->p_window,
423                        Pt_ARG_POS, &p_vout->p_sys->pos, 0 );
424         PtSetResource( p_vout->p_sys->p_window,
425                        Pt_ARG_DIM, &dim, 0 );
426
427         /* mark as damaged to force redraw */
428         PtDamageWidget( p_vout->p_sys->p_window );
429     }
430
431     /*
432      * size change
433      */
434     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
435     {
436         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
437
438         if( p_vout->p_sys->i_mode == MODE_VIDEO_OVERLAY )
439         {
440             ResizeOverlayOutput(p_vout);
441         }
442 #if 0
443         else
444         {
445             p_vout->output.i_width = p_vout->p_sys->dim.w;
446             p_vout->output.i_height = p_vout->p_sys->dim.h;
447             p_vout->i_changes |= VOUT_YUV_CHANGE;
448
449             QNXEnd( p_vout );
450             if( QNXInit( p_vout ) )
451             {
452                 msg_Err( p_vout, "cannot resize display" );
453                 return( 1 );
454             }
455         }
456 #endif
457
458         msg_Dbg( p_vout, "video display resized (%dx%d)",
459                          p_vout->p_sys->dim.w, p_vout->p_sys->dim.h );
460     }
461
462     /*
463      * position change, move video channel
464      */
465     if( b_repos && p_vout->p_sys->i_mode == MODE_VIDEO_OVERLAY )
466     {
467         ResizeOverlayOutput(p_vout);
468     }
469
470     return( i_ev == -1 );
471 }
472
473 /*****************************************************************************
474  * QNXDisplay: displays previously rendered output
475  *****************************************************************************
476  * This function send the currently rendered image to QNX server, wait until
477  * it is displayed and switch the two rendering buffer, preparing next frame.
478  *****************************************************************************/
479 static void QNXDisplay( vout_thread_t *p_vout, picture_t *p_pic )
480 {
481     if( p_vout->p_sys->i_mode == MODE_NORMAL_MEM ||
482         p_vout->p_sys->i_mode == MODE_SHARED_MEM )
483     {
484         PhPoint_t pos = { 0, 0 };
485
486         PgSetRegion( PtWidgetRid( p_vout->p_sys->p_window ) );
487         if (p_vout->p_sys->i_screen_depth == 8)
488         {
489             PgSetPalette( p_vout->p_sys->p_colors, 0, 0, 255, Pg_PALSET_SOFT, 0);
490         }
491         PgDrawPhImagemx( &pos, p_pic->p_sys->p_image, 0 );
492         PgFlush();
493     }
494     else if( p_vout->p_sys->i_mode == MODE_VIDEO_MEM )
495     {
496         PhRect_t rc = { { 0, 0 }, { p_vout->output.i_width, p_vout->output.i_height } };
497
498 //        PgSetRegion( PtWidgetRid ( p_vout->p_sys->p_window ) );
499         PgContextBlit( p_pic->p_sys->p_ctx[0], &rc, NULL, &rc );
500         PgFlush();
501     }
502 }
503
504 /*****************************************************************************
505  * QNXInitDisplay: check screen resolution, depth, amount of video ram, etc
506  *****************************************************************************/
507 static int QNXInitDisplay( vout_thread_t * p_vout )
508 {
509     PgHWCaps_t hwcaps;
510     PgDisplaySettings_t cfg;
511     PgVideoModeInfo_t minfo;
512
513     /* get graphics card hw capabilities */
514     if( PgGetGraphicsHWCaps( &hwcaps ) != 0 )
515     {
516         msg_Err( p_vout, "unable to get gfx card capabilities" );
517         return( 1 );
518     }
519
520     /* get current video mode */
521     if( PgGetVideoMode( &cfg ) != 0 )
522     {
523         msg_Err( p_vout, "unable to get current video mode" );
524         return( 1 );
525     }
526
527     /* get video mode info */
528     if( PgGetVideoModeInfo( cfg.mode, &minfo ) != 0 )
529     {
530         msg_Err( p_vout, "unable to get info for video mode" );
531         return( 1 );
532     }
533
534     if( p_vout->p_sys->i_mode == MODE_VIDEO_OVERLAY )
535     {
536         int i = 0;
537         PgScalerCaps_t vcaps;
538
539         if( ( p_vout->p_sys->p_channel =
540             PgCreateVideoChannel( Pg_VIDEO_CHANNEL_SCALER, 0 ) ) == NULL )
541         {
542             msg_Err( p_vout, "unable to create video channel" );
543             printf("errno = %d\n", errno);
544             p_vout->p_sys->i_mode = MODE_NORMAL_MEM;
545         }
546         else
547         {
548             vcaps.size = sizeof( vcaps );
549             while( PgGetScalerCapabilities( p_vout->p_sys->p_channel,
550                                             i++, &vcaps ) == 0 )
551             {
552                 printf("vcaps.format = 0x%x\n", vcaps.format);
553                 if( vcaps.format == Pg_VIDEO_FORMAT_YV12 ||
554                     vcaps.format == Pg_VIDEO_FORMAT_YUV420 ||
555                     vcaps.format == Pg_VIDEO_FORMAT_YUY2 ||
556                     vcaps.format == Pg_VIDEO_FORMAT_UYVY ||
557                     vcaps.format == Pg_VIDEO_FORMAT_RGB555 ||
558                     vcaps.format == Pg_VIDEO_FORMAT_RGB565 ||
559                     vcaps.format == Pg_VIDEO_FORMAT_RGB8888 )
560                 {
561                     p_vout->p_sys->i_vc_flags  = vcaps.flags;
562                     p_vout->p_sys->i_vc_format = vcaps.format;
563                 }
564
565                 vcaps.size = sizeof( vcaps );
566             }
567
568             if( p_vout->p_sys->i_vc_format == 0 )
569             {
570                 msg_Warn( p_vout, "need YV12, YUY2 or RGB8888 overlay" );
571
572                 p_vout->p_sys->i_mode = MODE_NORMAL_MEM;
573             }
574         }
575     }
576
577     /* use video ram if we have enough available */
578     if( p_vout->p_sys->i_mode == MODE_NORMAL_MEM &&
579         (minfo.bits_per_pixel != 8) &&
580         hwcaps.currently_available_video_ram >=
581         ( ( minfo.width * minfo.height * minfo.bits_per_pixel * MAX_DIRECTBUFFERS) / 8 ) )
582     {
583         p_vout->p_sys->i_mode = MODE_VIDEO_MEM;
584         printf("Using video memory...\n");
585     }
586
587     p_vout->p_sys->i_img_type = minfo.type;
588     p_vout->p_sys->screen_dim.w = minfo.width;
589     p_vout->p_sys->screen_dim.h = minfo.height;
590     p_vout->p_sys->i_screen_depth = minfo.bits_per_pixel;
591
592     switch( p_vout->p_sys->i_screen_depth )
593     {
594         case 8:
595             p_vout->output.i_chroma = VLC_CODEC_RGB8;
596             p_vout->p_sys->i_bytes_per_pixel = 1;
597             p_vout->output.pf_setpalette = SetPalette;
598             break;
599
600         case 15:
601             p_vout->output.i_chroma = VLC_CODEC_RGB15;
602             p_vout->p_sys->i_bytes_per_pixel = 2;
603             p_vout->output.i_rmask = 0x7c00;
604             p_vout->output.i_gmask = 0x03e0;
605             p_vout->output.i_bmask = 0x001f;
606             break;
607
608         case 16:
609             p_vout->output.i_chroma = VLC_CODEC_RGB16;
610             p_vout->p_sys->i_bytes_per_pixel = 2;
611             p_vout->output.i_rmask = 0xf800;
612             p_vout->output.i_gmask = 0x07e0;
613             p_vout->output.i_bmask = 0x001f;
614             break;
615
616         case 24:
617             p_vout->output.i_chroma = VLC_CODEC_RGB24;
618             p_vout->p_sys->i_bytes_per_pixel = 3;
619             p_vout->output.i_rmask = 0xff0000;
620             p_vout->output.i_gmask = 0x00ff00;
621             p_vout->output.i_bmask = 0x0000ff;
622             break;
623
624         case 32:
625         default:
626             p_vout->output.i_chroma = VLC_CODEC_RGB32;
627             p_vout->p_sys->i_bytes_per_pixel = 4;
628             p_vout->output.i_rmask = 0xff0000;
629             p_vout->output.i_gmask = 0x00ff00;
630             p_vout->output.i_bmask = 0x0000ff;
631             break;
632     }
633
634     return( 0 );
635 }
636
637 /*****************************************************************************
638  * QNXCreateWnd: create and realize the main window
639  *****************************************************************************/
640 static int QNXCreateWnd( vout_thread_t * p_vout )
641 {
642     PtArg_t args[8];
643     PhPoint_t pos = { 0, 0 };
644     PgColor_t color = Pg_BLACK;
645
646     if( p_vout->p_sys->i_mode == MODE_VIDEO_OVERLAY )
647     {
648         if( p_vout->p_sys->i_vc_flags & Pg_SCALER_CAP_DST_CHROMA_KEY )
649         {
650             color = PgGetOverlayChromaColor();
651         }
652     }
653
654     /* fullscreen, set dimension */
655     if( p_vout->b_fullscreen )
656     {
657         p_vout->p_sys->old_dim.w = p_vout->p_sys->dim.w;
658         p_vout->p_sys->old_dim.h = p_vout->p_sys->dim.h;
659         p_vout->output.i_width = p_vout->p_sys->dim.w = p_vout->p_sys->screen_dim.w;
660         p_vout->output.i_height = p_vout->p_sys->dim.h = p_vout->p_sys->screen_dim.h;
661     }
662
663     /* set window parameters */
664     PtSetArg( &args[0], Pt_ARG_POS, &pos, 0 );
665     PtSetArg( &args[1], Pt_ARG_DIM, &p_vout->p_sys->dim, 0 );
666     PtSetArg( &args[2], Pt_ARG_FILL_COLOR, color, 0 );
667     PtSetArg( &args[3], Pt_ARG_WINDOW_TITLE, "VLC media player", 0 );
668     PtSetArg( &args[4], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_CLOSE );
669     PtSetArg( &args[5], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE,
670               Ph_WM_MOVE | Ph_WM_RESIZE | Ph_WM_CLOSE );
671     PtSetArg( &args[6], Pt_ARG_WINDOW_RENDER_FLAGS,
672               p_vout->b_fullscreen ? Pt_FALSE : Pt_TRUE,
673               Ph_WM_RENDER_BORDER | Ph_WM_RENDER_TITLE );
674
675     /* create window */
676     p_vout->p_sys->p_window = PtCreateWidget( PtWindow, Pt_NO_PARENT, 7, args);
677     if( p_vout->p_sys->p_window == NULL )
678     {
679         msg_Err( p_vout, "unable to create window" );
680         return( 1 );
681     }
682
683     /* realize the window widget */
684     if( PtRealizeWidget( p_vout->p_sys->p_window ) != 0 )
685     {
686         msg_Err( p_vout, "unable to realize window widget" );
687         PtDestroyWidget( p_vout->p_sys->p_window );
688         return( 1 );
689     }
690
691     /* get window frame size */
692     if( PtWindowFrameSize( NULL, p_vout->p_sys->p_window,
693                            &p_vout->p_sys->frame ) != 0 )
694     {
695         msg_Err( p_vout, "unable to get window frame size" );
696         PtDestroyWidget( p_vout->p_sys->p_window );
697         return( 1 );
698     }
699
700     return( 0 );
701 }
702
703 /*****************************************************************************
704  * QNXDestroyWnd: unrealize and destroy the main window
705  *****************************************************************************/
706 static int QNXDestroyWnd( vout_thread_t * p_vout )
707 {
708     /* destroy the window widget */
709     PtUnrealizeWidget( p_vout->p_sys->p_window );
710 //    PtDestroyWidget( p_vout->p_sys->p_window );
711
712     /* destroy video channel */
713     if( p_vout->p_sys->i_mode == MODE_VIDEO_OVERLAY )
714     {
715         PgDestroyVideoChannel( p_vout->p_sys->p_channel );
716     }
717
718     return( 0 );
719 }
720
721
722 /*****************************************************************************
723  * NewPicture: allocate a picture
724  *****************************************************************************
725  * Returns 0 on success, -1 otherwise
726  *****************************************************************************/
727 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic, int index )
728 {
729     /* We know the chroma, allocate a buffer which will be used
730      * directly by the decoder */
731     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
732
733     if( p_pic->p_sys == NULL )
734     {
735         return -1;
736     }
737
738     switch( p_vout->p_sys->i_mode )
739     {
740     case MODE_NORMAL_MEM:
741     case MODE_SHARED_MEM:
742         /* create images for [shared] memory blit */
743         if( !( p_pic->p_sys->p_image = PhCreateImage( NULL,
744                     p_vout->p_sys->dim.w, p_vout->p_sys->dim.h,
745                     p_vout->p_sys->i_img_type, NULL, 0,
746                     p_vout->p_sys->i_mode == MODE_SHARED_MEM ) ) ) {
747             msg_Err( p_vout, "cannot create image" );
748             free( p_pic->p_sys );
749             return( -1 );
750         }
751
752         p_pic->p->p_pixels = p_pic->p_sys->p_image->image;
753         p_pic->p->i_lines = p_pic->p_sys->p_image->size.h;
754         p_pic->p->i_visible_lines = p_pic->p_sys->p_image->size.h;
755         p_pic->p->i_pitch = p_pic->p_sys->p_image->bpl;
756         p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
757         p_pic->p->i_visible_pitch = p_vout->p_sys->i_bytes_per_pixel
758                                      * p_pic->p_sys->p_image->size.w;
759         p_pic->i_planes = 1;
760         break;
761
762     case MODE_VIDEO_MEM:
763         /* create offscreen contexts for video memory blit */
764         if( ( p_pic->p_sys->p_ctx[0] = PdCreateOffscreenContext( 0,
765                         p_vout->p_sys->dim.w, p_vout->p_sys->dim.h,
766                        Pg_OSC_MEM_PAGE_ALIGN) ) == NULL )
767         {
768             msg_Err( p_vout, "unable to create offscreen context" );
769             free( p_pic->p_sys );
770             return( -1 );
771         }
772
773         /* get context pointers */
774         if( (  p_pic->p_sys->p_buf[0] =
775             PdGetOffscreenContextPtr ( p_pic->p_sys->p_ctx[0] ) ) == NULL )
776         {
777             msg_Err( p_vout, "unable to get offscreen context ptr" );
778             PhDCRelease ( p_pic->p_sys->p_ctx[0] );
779             p_pic->p_sys->p_ctx[0] = NULL;
780             free( p_pic->p_sys );
781             return( -1 );
782         }
783
784         p_vout->p_sys->i_bytes_per_line = p_pic->p_sys->p_ctx[0]->pitch;
785         memset( p_pic->p_sys->p_buf[0], 0,
786             p_vout->p_sys->i_bytes_per_line * p_vout->p_sys->dim.h );
787
788         p_pic->p->p_pixels = p_pic->p_sys->p_buf[0];
789         p_pic->p->i_lines = p_pic->p_sys->p_ctx[0]->dim.h;
790         p_pic->p->i_visible_lines = p_pic->p_sys->p_ctx[0]->dim.h;
791         p_pic->p->i_pitch = p_pic->p_sys->p_ctx[0]->pitch;
792         p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
793         p_pic->p->i_visible_pitch = p_vout->p_sys->i_bytes_per_pixel
794                                      * p_pic->p_sys->p_ctx[0]->dim.w;
795         p_pic->i_planes = 1;
796         break;
797
798     case MODE_VIDEO_OVERLAY:
799         if (index == 0)
800         {
801             p_pic->p_sys->p_ctx[Y_PLANE] = p_vout->p_sys->p_channel->yplane1;
802             p_pic->p_sys->p_ctx[U_PLANE] = p_vout->p_sys->p_channel->uplane1;
803             p_pic->p_sys->p_ctx[V_PLANE] = p_vout->p_sys->p_channel->vplane1;
804         }
805         else
806         {
807             p_pic->p_sys->p_ctx[Y_PLANE] = p_vout->p_sys->p_channel->yplane2;
808             p_pic->p_sys->p_ctx[U_PLANE] = p_vout->p_sys->p_channel->uplane2;
809             p_pic->p_sys->p_ctx[V_PLANE] = p_vout->p_sys->p_channel->vplane2;
810         }
811
812         p_pic->p_sys->p_buf[Y_PLANE] = PdGetOffscreenContextPtr( p_pic->p_sys->p_ctx[Y_PLANE] );
813         if( p_pic->p_sys->p_buf[Y_PLANE] == NULL )
814         {
815             msg_Err( p_vout, "unable to get video channel ctx ptr" );
816             return( 1 );
817         }
818
819         switch (p_vout->p_sys->i_vc_format)
820         {
821             case Pg_VIDEO_FORMAT_YUV420:
822                 p_vout->output.i_chroma = VLC_CODEC_I420;
823
824                 p_pic->p_sys->p_buf[U_PLANE] = PdGetOffscreenContextPtr( p_pic->p_sys->p_ctx[U_PLANE] );
825                 p_pic->p_sys->p_buf[V_PLANE] = PdGetOffscreenContextPtr( p_pic->p_sys->p_ctx[V_PLANE] );
826
827                 if( p_pic->p_sys->p_buf[U_PLANE] == NULL ||
828                     p_pic->p_sys->p_buf[V_PLANE] == NULL )
829                 {
830                     msg_Err( p_vout, "unable to get video channel ctx ptr" );
831                     return( 1 );
832                 }
833
834                 p_pic->Y_PIXELS = p_pic->p_sys->p_buf[Y_PLANE];
835                 p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
836                 p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
837                 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
838                 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
839                 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
840
841                 p_pic->U_PIXELS = p_pic->p_sys->p_buf[U_PLANE];
842                 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_ctx[U_PLANE]->dim.h;
843                 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[U_PLANE]->dim.h;
844                 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_ctx[U_PLANE]->pitch;
845                 p_pic->p[U_PLANE].i_pixel_pitch = 1;
846                 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
847
848                 p_pic->V_PIXELS = p_pic->p_sys->p_buf[V_PLANE];
849                 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_ctx[V_PLANE]->dim.h;
850                 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[V_PLANE]->dim.h;
851                 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_ctx[V_PLANE]->pitch;
852                 p_pic->p[V_PLANE].i_pixel_pitch = 1;
853                 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
854
855                 p_pic->i_planes = 3;
856                 break;
857
858             case Pg_VIDEO_FORMAT_YV12:
859                 p_vout->output.i_chroma = VLC_CODEC_YV12;
860
861                 p_pic->p_sys->p_buf[U_PLANE] = PdGetOffscreenContextPtr( p_pic->p_sys->p_ctx[U_PLANE] );
862                 p_pic->p_sys->p_buf[V_PLANE] = PdGetOffscreenContextPtr( p_pic->p_sys->p_ctx[V_PLANE] );
863
864                 if( p_pic->p_sys->p_buf[U_PLANE] == NULL ||
865                     p_pic->p_sys->p_buf[V_PLANE] == NULL )
866                 {
867                     msg_Err( p_vout, "unable to get video channel ctx ptr" );
868                     return( 1 );
869                 }
870
871                 p_pic->Y_PIXELS = p_pic->p_sys->p_buf[Y_PLANE];
872                 p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
873                 p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
874                 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
875                 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
876                 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
877
878                 p_pic->U_PIXELS = p_pic->p_sys->p_buf[U_PLANE];
879                 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_ctx[U_PLANE]->dim.h;
880                 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[U_PLANE]->dim.h;
881                 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_ctx[U_PLANE]->pitch;
882                 p_pic->p[U_PLANE].i_pixel_pitch = 1;
883                 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
884
885                 p_pic->V_PIXELS = p_pic->p_sys->p_buf[V_PLANE];
886                 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_ctx[V_PLANE]->dim.h;
887                 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_ctx[V_PLANE]->dim.h;
888                 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_ctx[V_PLANE]->pitch;
889                 p_pic->p[V_PLANE].i_pixel_pitch = 1;
890                 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
891
892                 p_pic->i_planes = 3;
893                 break;
894
895             case Pg_VIDEO_FORMAT_UYVY:
896             case Pg_VIDEO_FORMAT_YUY2:
897                 if (p_vout->p_sys->i_vc_format == Pg_VIDEO_FORMAT_UYVY)
898                 {
899                     p_vout->output.i_chroma = VLC_CODEC_UYVY;
900                 }
901                 else
902                 {
903                     p_vout->output.i_chroma = VLC_CODEC_YUYV;
904                 }
905
906                 p_pic->p->p_pixels = p_pic->p_sys->p_buf[Y_PLANE];
907                 p_pic->p->i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
908                 p_pic->p->i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
909                 p_pic->p->i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
910                 p_pic->p->i_pixel_pitch = 4;
911                 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
912
913                 p_pic->i_planes = 1;
914                 break;
915
916             case Pg_VIDEO_FORMAT_RGB555:
917                 p_vout->output.i_chroma = VLC_CODEC_RGB15;
918                 p_vout->output.i_rmask = 0x001f;
919                 p_vout->output.i_gmask = 0x03e0;
920                 p_vout->output.i_bmask = 0x7c00;
921
922                 p_pic->p->p_pixels = p_pic->p_sys->p_buf[Y_PLANE];
923                 p_pic->p->i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
924                 p_pic->p->i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
925                 p_pic->p->i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
926                 p_pic->p->i_pixel_pitch = 2;
927                 p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_ctx[Y_PLANE]->dim.w;
928
929                 p_pic->i_planes = 1;
930                 break;
931
932             case Pg_VIDEO_FORMAT_RGB565:
933                 p_vout->output.i_chroma = VLC_CODEC_RGB16;
934                 p_vout->output.i_rmask = 0x001f;
935                 p_vout->output.i_gmask = 0x07e0;
936                 p_vout->output.i_bmask = 0xf800;
937
938                 p_pic->p->p_pixels = p_pic->p_sys->p_buf[Y_PLANE];
939                 p_pic->p->i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
940                 p_pic->p->i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
941                 p_pic->p->i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
942                 p_pic->p->i_pixel_pitch = 4;
943                 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_ctx[Y_PLANE]->dim.w;
944
945                 p_pic->i_planes = 1;
946                 break;
947
948             case Pg_VIDEO_FORMAT_RGB8888:
949                 p_vout->output.i_chroma = VLC_CODEC_RGB32;
950                 p_vout->output.i_rmask = 0x000000ff;
951                 p_vout->output.i_gmask = 0x0000ff00;
952                 p_vout->output.i_bmask = 0x00ff0000;
953
954                 p_pic->p->p_pixels = p_pic->p_sys->p_buf[Y_PLANE];
955                 p_pic->p->i_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
956                 p_pic->p->i_visible_lines = p_pic->p_sys->p_ctx[Y_PLANE]->dim.h;
957                 p_pic->p->i_pitch = p_pic->p_sys->p_ctx[Y_PLANE]->pitch;
958                 p_pic->p->i_pixel_pitch = 4;
959                 p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_ctx[Y_PLANE]->dim.w;
960
961                 p_pic->i_planes = 1;
962                 break;
963         }
964
965 #if 0
966     switch( p_vout->output.i_chroma )
967     {
968 #ifdef MODULE_NAME_IS_xvideo
969         case VLC_CODEC_Y211:
970
971             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
972                                   + p_pic->p_sys->p_image->offsets[0];
973             p_pic->p->i_lines = p_vout->output.i_height;
974             p_pic->p->i_visible_lines = p_vout->output.i_height;
975             /* XXX: this just looks so plain wrong... check it out ! */
976             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
977             p_pic->p->i_pixel_pitch = 4;
978             p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
979
980             p_pic->i_planes = 1;
981             break;
982 #endif
983
984 #endif
985
986     default:
987         /* This shouldn't happen ! */
988         break;
989     }
990
991     return 0;
992 }
993
994 /*****************************************************************************
995  * FreePicture: destroy a picture allocated with NewPicture
996  *****************************************************************************
997  * Destroy XImage AND associated data. If using Shm, detach shared memory
998  * segment from server and process, then free it. The XDestroyImage manpage
999  * says that both the image structure _and_ the data pointed to by the
1000  * image structure are freed, so no need to free p_image->data.
1001  *****************************************************************************/
1002 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1003 {
1004     if( ( p_vout->p_sys->i_mode == MODE_NORMAL_MEM ||
1005         p_vout->p_sys->i_mode == MODE_SHARED_MEM ) &&
1006         p_pic->p_sys->p_image )
1007     {
1008         PhReleaseImage( p_pic->p_sys->p_image );
1009         free( p_pic->p_sys->p_image );
1010     }
1011     else if( p_vout->p_sys->i_mode == MODE_VIDEO_MEM &&
1012              p_pic->p_sys->p_ctx[0] )
1013     {
1014         PhDCRelease( p_pic->p_sys->p_ctx[0] );
1015     }
1016
1017     free( p_pic->p_sys );
1018 }
1019
1020
1021 static int ResizeOverlayOutput(vout_thread_t *p_vout)
1022 {
1023     int i_width, i_height, i_x, i_y;
1024     int i_ret;
1025     PgScalerProps_t props;
1026
1027     props.size   = sizeof( props );
1028     props.format = p_vout->p_sys->i_vc_format;
1029     props.flags  = Pg_SCALER_PROP_SCALER_ENABLE |
1030                           Pg_SCALER_PROP_DOUBLE_BUFFER;
1031
1032     /* enable chroma keying if available */
1033     if( p_vout->p_sys->i_vc_flags & Pg_SCALER_CAP_DST_CHROMA_KEY )
1034     {
1035         props.flags |= Pg_SCALER_PROP_CHROMA_ENABLE;
1036     }
1037
1038     /* set viewport position */
1039     props.viewport.ul.x = p_vout->p_sys->pos.x;
1040     props.viewport.ul.y = p_vout->p_sys->pos.y;
1041     if( !p_vout->b_fullscreen )
1042     {
1043         props.viewport.ul.x += p_vout->p_sys->frame.ul.x;
1044         props.viewport.ul.y += p_vout->p_sys->frame.ul.y;
1045     }
1046
1047     /* set viewport dimension */
1048     vout_PlacePicture( p_vout, p_vout->p_sys->dim.w,
1049                            p_vout->p_sys->dim.h,
1050                            &i_x, &i_y, &i_width, &i_height );
1051
1052     props.viewport.ul.x += i_x;
1053     props.viewport.ul.y += i_y;
1054     props.viewport.lr.x = i_width + props.viewport.ul.x;
1055     props.viewport.lr.y = i_height + props.viewport.ul.y;
1056
1057     /* set source dimension */
1058     props.src_dim.w = p_vout->output.i_width;
1059     props.src_dim.h = p_vout->output.i_height;
1060
1061     /* configure scaler channel */
1062     i_ret = PgConfigScalerChannel( p_vout->p_sys->p_channel, &props );
1063
1064     if( i_ret == -1 )
1065     {
1066         msg_Err( p_vout, "unable to configure video channel" );
1067         return( 1 );
1068     }
1069
1070     return ( 0 );
1071 }
1072
1073
1074 /*****************************************************************************
1075  * SetPalette: sets an 8 bpp palette
1076  *****************************************************************************
1077  * This function sets the palette given as an argument. It does not return
1078  * anything, but could later send information on which colors it was unable
1079  * to set.
1080  *****************************************************************************/
1081 static void SetPalette( vout_thread_t *p_vout,
1082                         uint16_t *red, uint16_t *green, uint16_t *blue )
1083 {
1084     int i;
1085
1086     /* allocate palette */
1087     for( i = 0; i < 255; i++ )
1088     {
1089         /* kludge: colors are indexed reversely because color 255 seems
1090          * to be reserved for black even if we try to set it to white */
1091         p_vout->p_sys->p_colors[ i ] = PgRGB( red[ i ] >> 8, green[ i ] >> 8, blue[ i ] >> 8 );
1092     }
1093 }