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