]> git.sesse.net Git - vlc/blob - plugins/glide/glide.c
3fc9cecce6794e2077b9d5b7fc8753d14cfa0782
[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.17 2002/07/23 00:39:17 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_t
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_lines = GLIDE_HEIGHT;
174     p_pic->p->i_pitch = p_vout->p_sys->p_buffer_info.strideInBytes;
175                          /*1024 * GLIDE_BYTES_PER_PIXEL*/
176     p_pic->p->i_pixel_pitch = GLIDE_BYTES_PER_PIXEL;
177     p_pic->p->i_visible_pitch = GLIDE_WIDTH * GLIDE_BYTES_PER_PIXEL;
178
179     p_pic->i_status = DESTROYED_PICTURE;
180     p_pic->i_type   = DIRECT_PICTURE;
181
182     PP_OUTPUTPICTURE[ 0 ] = p_pic;
183
184     I_OUTPUTPICTURES = 1;
185
186     return 0;
187 }
188
189 /*****************************************************************************
190  * vout_End: terminate Glide video thread output method
191  *****************************************************************************/
192 void vout_End( vout_thread_t *p_vout )
193 {
194     ;
195 }
196
197 /*****************************************************************************
198  * vout_Destroy: destroy Glide video thread output method
199  *****************************************************************************
200  * Terminate an output method created by vout_CreateOutputMethod
201  *****************************************************************************/
202 void vout_Destroy( vout_thread_t *p_vout )
203 {
204     CloseDisplay( p_vout );
205     free( p_vout->p_sys );
206 }
207
208 /*****************************************************************************
209  * vout_Manage: handle Glide events
210  *****************************************************************************
211  * This function should be called regularly by video output thread. It manages
212  * console events. It returns a non null value on error.
213  *****************************************************************************/
214 int vout_Manage( vout_thread_t *p_vout )
215 {
216     int buf;
217
218     /* very Linux specific - see tlib.c in Glide for other versions */
219     while( kbhit() )
220     {
221         buf = getch();
222
223         switch( (char)buf )
224         {
225         case 'q':
226             p_vout->p_vlc->b_die = 1;
227             break;
228
229         default:
230             break;
231         }
232     }
233
234     return 0;
235 }
236
237 /*****************************************************************************
238  * vout_Render: renders previously calculated output
239  *****************************************************************************/
240 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
241 {
242     ;
243 }
244         
245 /*****************************************************************************
246  * vout_Display: displays previously rendered output
247  *****************************************************************************/
248 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
249
250 {
251     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
252
253     grBufferSwap( 0 );
254
255     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
256                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
257                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
258     {
259         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
260     }
261 }
262
263 /* following functions are local */
264
265 /*****************************************************************************
266  * OpenDisplay: open and initialize 3dfx device
267  *****************************************************************************/
268
269 static int OpenDisplay( vout_thread_t *p_vout )
270 {
271     static char version[80];
272     GrHwConfiguration hwconfig;
273     GrScreenResolution_t resolution = GR_RESOLUTION_800x600;
274     GrLfbInfo_t p_front_buffer_info;                    /* front buffer info */
275
276     grGlideGetVersion( version );
277     grGlideInit();
278
279     if( !grSstQueryHardware(&hwconfig) )
280     {
281         msg_Err( p_vout, "cannot get 3dfx hardware config" );
282         return( 1 );
283     }
284
285     grSstSelect( 0 );
286     if( !grSstWinOpen( 0, resolution, GR_REFRESH_60Hz,
287                        GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ) )
288     {
289         msg_Err( p_vout, "cannot open 3dfx screen" );
290         return( 1 );
291     }
292
293     /* disable dithering */
294     //grDitherMode( GR_DITHER_DISABLE );
295
296     /* clear both buffers */
297     grRenderBuffer( GR_BUFFER_BACKBUFFER );
298     grBufferClear( 0, 0, 0 );
299     grRenderBuffer( GR_BUFFER_FRONTBUFFER );
300     grBufferClear( 0, 0, 0 );
301     grRenderBuffer( GR_BUFFER_BACKBUFFER );
302
303     p_vout->p_sys->p_buffer_info.size = sizeof( GrLfbInfo_t );
304     p_front_buffer_info.size          = sizeof( GrLfbInfo_t );
305
306     /* lock the buffers to find their adresses */
307     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER,
308                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
309                    &p_front_buffer_info) == FXFALSE )
310     {
311         msg_Err( p_vout, "cannot take 3dfx front buffer lock" );
312         grGlideShutdown();
313         return( 1 );
314     }
315     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
316
317     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
318                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
319                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
320     {
321         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
322         grGlideShutdown();
323         return( 1 );
324     }
325     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
326     
327     grBufferClear( 0, 0, 0 );
328
329     p_vout->p_sys->pp_buffer[0] = p_vout->p_sys->p_buffer_info.lfbPtr;
330     p_vout->p_sys->pp_buffer[1] = p_front_buffer_info.lfbPtr;
331     p_vout->p_sys->i_index = 0;
332
333     return( 0 );
334 }
335
336 /*****************************************************************************
337  * CloseDisplay: close and reset 3dfx device
338  *****************************************************************************
339  * Returns all resources allocated by OpenDisplay and restore the original
340  * state of the device.
341  *****************************************************************************/
342 static void CloseDisplay( vout_thread_t *p_vout )
343 {
344     /* unlock the hidden buffer */
345     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
346
347     /* shutdown Glide */
348     grGlideShutdown();
349 }
350