]> git.sesse.net Git - vlc/blob - plugins/glide/glide.c
4e8efa84b37527ddae594576a280dd66bb4dda68
[vlc] / plugins / glide / glide.c
1 /*****************************************************************************
2  * glide.c : 3dfx Glide plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: glide.c,v 1.15 2002/06/01 12:31:59 sam Exp $
6  *
7  * Authors: 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 <errno.h>                                                 /* ENOMEM */
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33 #include <vlc/vout.h>
34
35 #ifndef __linux__
36 #   include <conio.h>                                         /* for glide ? */
37 #endif
38 #include <glide.h>
39 #include <linutil.h>                            /* Glide kbhit() and getch() */
40
41 #define GLIDE_WIDTH 800
42 #define GLIDE_HEIGHT 600
43 #define GLIDE_BITS_PER_PLANE 16
44 #define GLIDE_BYTES_PER_PIXEL 2
45
46 /*****************************************************************************
47  * Local prototypes.
48  *****************************************************************************/
49 static void vout_getfunctions( function_list_t * p_function_list );
50
51 static int  vout_Create    ( vout_thread_t * );
52 static int  vout_Init      ( vout_thread_t * );
53 static void vout_End       ( vout_thread_t * );
54 static void vout_Destroy   ( vout_thread_t * );
55 static int  vout_Manage    ( vout_thread_t * );
56 static void vout_Render    ( vout_thread_t *, picture_t * );
57 static void vout_Display   ( vout_thread_t *, picture_t * );
58
59 static int  OpenDisplay    ( vout_thread_t * );
60 static void CloseDisplay   ( vout_thread_t * );
61
62 /*****************************************************************************
63  * Building configuration tree
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 MODULE_CONFIG_STOP
67
68 MODULE_INIT_START
69     SET_DESCRIPTION( _("3dfx Glide module") )
70     ADD_CAPABILITY( VOUT, 20 )
71     ADD_SHORTCUT( "3dfx" )
72 MODULE_INIT_STOP
73
74 MODULE_ACTIVATE_START
75     vout_getfunctions( &p_module->p_functions->vout );
76 MODULE_ACTIVATE_STOP
77
78 MODULE_DEACTIVATE_START
79 MODULE_DEACTIVATE_STOP
80
81 /*****************************************************************************
82  * vout_sys_t: Glide video output method descriptor
83  *****************************************************************************
84  * This structure is part of the video output thread descriptor.
85  * It describes the Glide specific properties of an output thread.
86  *****************************************************************************/
87 struct vout_sys_s
88 {
89     GrLfbInfo_t                 p_buffer_info;           /* back buffer info */
90
91     u8* pp_buffer[2];
92     int i_index;
93 };
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 void _M( vout_getfunctions )( function_list_t * p_function_list )
100 {
101     p_function_list->functions.vout.pf_create     = vout_Create;
102     p_function_list->functions.vout.pf_init       = vout_Init;
103     p_function_list->functions.vout.pf_end        = vout_End;
104     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
105     p_function_list->functions.vout.pf_manage     = vout_Manage;
106     p_function_list->functions.vout.pf_render     = vout_Render;
107     p_function_list->functions.vout.pf_display    = vout_Display;
108 }
109
110 /*****************************************************************************
111  * vout_Create: allocates Glide video thread output method
112  *****************************************************************************
113  * This function allocates and initializes a Glide vout method.
114  *****************************************************************************/
115 int vout_Create( vout_thread_t *p_vout )
116 {
117     /* Allocate structure */
118     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
119     if( p_vout->p_sys == NULL )
120     {
121         msg_Err( p_vout, "out of memory" );
122         return( 1 );
123     }
124
125     /* Open and initialize device */
126     if( OpenDisplay( p_vout ) )
127     {
128         msg_Err( p_vout, "cannot open display" );
129         free( p_vout->p_sys );
130         return( 1 );
131     }
132
133     return( 0 );
134 }
135
136 /*****************************************************************************
137  * vout_Init: initialize Glide video thread output method
138  *****************************************************************************/
139 int vout_Init( vout_thread_t *p_vout )
140 {
141     int i_index;
142     picture_t *p_pic;
143
144     /* FIXME: we don't set i_chroma !! */
145     p_vout->output.i_rmask = 0xf800;
146     p_vout->output.i_gmask = 0x07e0;
147     p_vout->output.i_bmask = 0x001f;
148
149     I_OUTPUTPICTURES = 0;
150
151     p_pic = NULL;
152
153     /* Find an empty picture slot */
154     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
155     {
156         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
157         {
158             p_pic = p_vout->p_picture + i_index;
159             break;
160         }
161     }
162
163     if( p_pic == NULL )
164     {
165         return -1;
166     }
167
168     /* We know the chroma, allocate a buffer which will be used
169      * directly by the decoder */
170     p_pic->i_planes = 1;
171
172     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index];
173     p_pic->p->i_pixel_bytes = GLIDE_BYTES_PER_PIXEL;
174     p_pic->p->i_lines = GLIDE_HEIGHT;
175
176     p_pic->p->b_margin = 1;
177     p_pic->p->b_hidden = 1;
178     p_pic->p->i_visible_bytes = GLIDE_WIDTH * GLIDE_BYTES_PER_PIXEL;
179     p_pic->p->i_pitch = p_vout->p_sys->p_buffer_info.strideInBytes;
180                          /*1024 * GLIDE_BYTES_PER_PIXEL*/
181
182     p_pic->i_status = DESTROYED_PICTURE;
183     p_pic->i_type   = DIRECT_PICTURE;
184
185     PP_OUTPUTPICTURE[ 0 ] = p_pic;
186
187     I_OUTPUTPICTURES = 1;
188
189     return 0;
190 }
191
192 /*****************************************************************************
193  * vout_End: terminate Glide video thread output method
194  *****************************************************************************/
195 void vout_End( vout_thread_t *p_vout )
196 {
197     ;
198 }
199
200 /*****************************************************************************
201  * vout_Destroy: destroy Glide video thread output method
202  *****************************************************************************
203  * Terminate an output method created by vout_CreateOutputMethod
204  *****************************************************************************/
205 void vout_Destroy( vout_thread_t *p_vout )
206 {
207     CloseDisplay( p_vout );
208     free( p_vout->p_sys );
209 }
210
211 /*****************************************************************************
212  * vout_Manage: handle Glide events
213  *****************************************************************************
214  * This function should be called regularly by video output thread. It manages
215  * console events. It returns a non null value on error.
216  *****************************************************************************/
217 int vout_Manage( vout_thread_t *p_vout )
218 {
219     int buf;
220
221     /* very Linux specific - see tlib.c in Glide for other versions */
222     while( kbhit() )
223     {
224         buf = getch();
225
226         switch( (char)buf )
227         {
228         case 'q':
229             p_vout->p_vlc->b_die = 1;
230             break;
231
232         default:
233             break;
234         }
235     }
236
237     return 0;
238 }
239
240 /*****************************************************************************
241  * vout_Render: renders previously calculated output
242  *****************************************************************************/
243 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
244 {
245     ;
246 }
247         
248 /*****************************************************************************
249  * vout_Display: displays previously rendered output
250  *****************************************************************************/
251 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
252
253 {
254     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
255
256     grBufferSwap( 0 );
257
258     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
259                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
260                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
261     {
262         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
263     }
264 }
265
266 /* following functions are local */
267
268 /*****************************************************************************
269  * OpenDisplay: open and initialize 3dfx device
270  *****************************************************************************/
271
272 static int OpenDisplay( vout_thread_t *p_vout )
273 {
274     static char version[80];
275     GrHwConfiguration hwconfig;
276     GrScreenResolution_t resolution = GR_RESOLUTION_800x600;
277     GrLfbInfo_t p_front_buffer_info;                    /* front buffer info */
278
279     grGlideGetVersion( version );
280     grGlideInit();
281
282     if( !grSstQueryHardware(&hwconfig) )
283     {
284         msg_Err( p_vout, "cannot get 3dfx hardware config" );
285         return( 1 );
286     }
287
288     grSstSelect( 0 );
289     if( !grSstWinOpen( 0, resolution, GR_REFRESH_60Hz,
290                        GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ) )
291     {
292         msg_Err( p_vout, "cannot open 3dfx screen" );
293         return( 1 );
294     }
295
296     /* disable dithering */
297     //grDitherMode( GR_DITHER_DISABLE );
298
299     /* clear both buffers */
300     grRenderBuffer( GR_BUFFER_BACKBUFFER );
301     grBufferClear( 0, 0, 0 );
302     grRenderBuffer( GR_BUFFER_FRONTBUFFER );
303     grBufferClear( 0, 0, 0 );
304     grRenderBuffer( GR_BUFFER_BACKBUFFER );
305
306     p_vout->p_sys->p_buffer_info.size = sizeof( GrLfbInfo_t );
307     p_front_buffer_info.size          = sizeof( GrLfbInfo_t );
308
309     /* lock the buffers to find their adresses */
310     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER,
311                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
312                    &p_front_buffer_info) == FXFALSE )
313     {
314         msg_Err( p_vout, "cannot take 3dfx front buffer lock" );
315         grGlideShutdown();
316         return( 1 );
317     }
318     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
319
320     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
321                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
322                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
323     {
324         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
325         grGlideShutdown();
326         return( 1 );
327     }
328     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
329     
330     grBufferClear( 0, 0, 0 );
331
332     p_vout->p_sys->pp_buffer[0] = p_vout->p_sys->p_buffer_info.lfbPtr;
333     p_vout->p_sys->pp_buffer[1] = p_front_buffer_info.lfbPtr;
334     p_vout->p_sys->i_index = 0;
335
336     return( 0 );
337 }
338
339 /*****************************************************************************
340  * CloseDisplay: close and reset 3dfx device
341  *****************************************************************************
342  * Returns all resources allocated by OpenDisplay and restore the original
343  * state of the device.
344  *****************************************************************************/
345 static void CloseDisplay( vout_thread_t *p_vout )
346 {
347     /* unlock the hidden buffer */
348     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
349
350     /* shutdown Glide */
351     grGlideShutdown();
352 }
353