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