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