]> git.sesse.net Git - vlc/blob - plugins/glide/glide.c
1d053d54ce39f5a1996fc62e5e921bbdfdb87977
[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.10 2002/01/07 02:12:29 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_Probe     ( probedata_t *p_data );
55 static int  vout_Create    ( vout_thread_t * );
56 static int  vout_Init      ( vout_thread_t * );
57 static void vout_End       ( vout_thread_t * );
58 static void vout_Destroy   ( vout_thread_t * );
59 static int  vout_Manage    ( vout_thread_t * );
60 static void vout_Render    ( vout_thread_t *, picture_t * );
61 static void vout_Display   ( vout_thread_t *, picture_t * );
62
63 static int  NewPicture     ( vout_thread_t *, picture_t * );
64 static int  OpenDisplay    ( vout_thread_t * );
65 static void CloseDisplay   ( vout_thread_t * );
66
67 /*****************************************************************************
68  * Building configuration tree
69  *****************************************************************************/
70 MODULE_CONFIG_START
71 MODULE_CONFIG_STOP
72
73 MODULE_INIT_START
74     SET_DESCRIPTION( "3dfx Glide module" )
75     ADD_CAPABILITY( VOUT, 20 )
76     ADD_SHORTCUT( "glide" )
77     ADD_SHORTCUT( "3dfx" )
78 MODULE_INIT_STOP
79
80 MODULE_ACTIVATE_START
81     vout_getfunctions( &p_module->p_functions->vout );
82 MODULE_ACTIVATE_STOP
83
84 MODULE_DEACTIVATE_START
85 MODULE_DEACTIVATE_STOP
86
87 /*****************************************************************************
88  * vout_sys_t: Glide video output method descriptor
89  *****************************************************************************
90  * This structure is part of the video output thread descriptor.
91  * It describes the Glide specific properties of an output thread.
92  *****************************************************************************/
93 typedef struct vout_sys_s
94 {
95     GrLfbInfo_t                 p_buffer_info;           /* back buffer info */
96
97     /* Dummy video memory */
98     byte_t *                    p_video;                      /* base adress */
99     size_t                      i_page_size;                    /* page size */
100
101 } vout_sys_t;
102
103 /*****************************************************************************
104  * Functions exported as capabilities. They are declared as static so that
105  * we don't pollute the namespace too much.
106  *****************************************************************************/
107 void _M( vout_getfunctions )( function_list_t * p_function_list )
108 {
109     p_function_list->pf_probe = vout_Probe;
110     p_function_list->functions.vout.pf_create     = vout_Create;
111     p_function_list->functions.vout.pf_init       = vout_Init;
112     p_function_list->functions.vout.pf_end        = vout_End;
113     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
114     p_function_list->functions.vout.pf_manage     = vout_Manage;
115     p_function_list->functions.vout.pf_render     = vout_Render;
116     p_function_list->functions.vout.pf_display    = vout_Display;
117 }
118
119 /*****************************************************************************
120  * vout_Probe: probe the video driver and return a score
121  *****************************************************************************
122  * This function tries to initialize SDL and returns a score to the
123  * plugin manager so that it can select the best plugin.
124  *****************************************************************************/
125 static int vout_Probe( probedata_t *p_data )
126 {
127     /* We could do a grSstQueryBoards( GrHwConfiguration *hwConfig ) at
128      * this point, but if the user didn't configure his 3dfx card, we
129      * have great chances to segfault here. So we'd better assume
130      * everything is fine and worry only if we really need to use Glide */
131     return( 1 );
132 }
133
134 /*****************************************************************************
135  * vout_Create: allocates Glide video thread output method
136  *****************************************************************************
137  * This function allocates and initializes a Glide vout method.
138  *****************************************************************************/
139 int vout_Create( vout_thread_t *p_vout )
140 {
141     /* Allocate structure */
142     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
143     if( p_vout->p_sys == NULL )
144     {
145         intf_ErrMsg("error: %s", strerror(ENOMEM) );
146         return( 1 );
147     }
148
149     /* Open and initialize device */
150     if( OpenDisplay( p_vout ) )
151     {
152         intf_ErrMsg("vout error: can't open display");
153         free( p_vout->p_sys );
154         return( 1 );
155     }
156
157     return( 0 );
158 }
159
160 /*****************************************************************************
161  * vout_Init: initialize Glide video thread output method
162  *****************************************************************************/
163 int vout_Init( vout_thread_t *p_vout )
164 {
165     int i_index;
166     picture_t *p_pic;
167
168     I_OUTPUTPICTURES = 0;
169
170     /* Try to initialize up to 1 direct buffers */
171     while( I_OUTPUTPICTURES < 1 )
172     {
173         p_pic = NULL;
174
175         /* Find an empty picture slot */
176         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
177         {
178             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
179             {
180                 p_pic = p_vout->p_picture + i_index;
181                 break;
182             }
183         }
184
185         /* Allocate the picture */
186         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
187         {
188             break;
189         }
190
191         p_pic->i_status = DESTROYED_PICTURE;
192         p_pic->i_type   = DIRECT_PICTURE;
193
194         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
195
196         I_OUTPUTPICTURES++;
197     }
198
199     return( 0 );
200 }
201
202 /*****************************************************************************
203  * vout_End: terminate Glide video thread output method
204  *****************************************************************************/
205 void vout_End( vout_thread_t *p_vout )
206 {
207     ;
208 }
209
210 /*****************************************************************************
211  * vout_Destroy: destroy Glide video thread output method
212  *****************************************************************************
213  * Terminate an output method created by vout_CreateOutputMethod
214  *****************************************************************************/
215 void vout_Destroy( vout_thread_t *p_vout )
216 {
217     CloseDisplay( p_vout );
218     free( p_vout->p_sys );
219 }
220
221 /*****************************************************************************
222  * vout_Manage: handle Glide events
223  *****************************************************************************
224  * This function should be called regularly by video output thread. It manages
225  * console events. It returns a non null value on error.
226  *****************************************************************************/
227 int vout_Manage( vout_thread_t *p_vout )
228 {
229     int buf;
230
231     /* very Linux specific - see tlib.c in Glide for other versions */
232     while( kbhit() )
233     {
234         buf = getch();
235
236         switch( (char)buf )
237         {
238         case 'q':
239             p_main->p_intf->b_die = 1;
240             break;
241
242         default:
243             break;
244         }
245     }
246
247     return 0;
248 }
249
250 /*****************************************************************************
251  * vout_Render: renders previously calculated output
252  *****************************************************************************/
253 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
254 {
255     ;
256 }
257         
258 /*****************************************************************************
259  * vout_Display: displays previously rendered output
260  *****************************************************************************/
261 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
262
263 {
264     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
265
266     grBufferSwap( 0 );
267
268     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
269                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
270                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
271     {
272         intf_ErrMsg( "vout error: can't take 3dfx back buffer lock" );
273     }
274 }
275
276 /* following functions are local */
277
278 /*****************************************************************************
279  * OpenDisplay: open and initialize 3dfx device
280  *****************************************************************************/
281
282 static int OpenDisplay( vout_thread_t *p_vout )
283 {
284     static char version[80];
285     GrHwConfiguration hwconfig;
286     GrScreenResolution_t resolution = GR_RESOLUTION_800x600;
287     GrLfbInfo_t p_front_buffer_info;                    /* front buffer info */
288
289     p_vout->p_sys->i_page_size = GLIDE_WIDTH * GLIDE_HEIGHT
290                                   * GLIDE_BYTES_PER_PIXEL;
291
292     /* Map two framebuffers a the very beginning of the fb */
293     p_vout->p_sys->p_video = malloc( p_vout->p_sys->i_page_size * 2 );
294     if( (int)p_vout->p_sys->p_video == -1 )
295     {
296         intf_ErrMsg( "vout error: can't map video memory (%s)",
297                      strerror(errno) );
298         return( 1 );
299     }
300
301     grGlideGetVersion( version );
302     grGlideInit();
303
304     if( !grSstQueryHardware(&hwconfig) )
305     {
306         intf_ErrMsg( "vout error: can't get 3dfx hardware config" );
307         return( 1 );
308     }
309
310     grSstSelect( 0 );
311     if( !grSstWinOpen(0, resolution, GR_REFRESH_60Hz,
312                         GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1) )
313     {
314         intf_ErrMsg( "vout error: can't open 3dfx screen" );
315         return( 1 );
316     }
317
318     /* disable dithering */
319     //grDitherMode( GR_DITHER_DISABLE );
320
321     /* clear both buffers */
322     grRenderBuffer( GR_BUFFER_BACKBUFFER );
323     grBufferClear( 0, 0, 0 );
324     grRenderBuffer( GR_BUFFER_FRONTBUFFER );
325     grBufferClear( 0, 0, 0 );
326     grRenderBuffer( GR_BUFFER_BACKBUFFER );
327
328     p_vout->p_sys->p_buffer_info.size = sizeof( GrLfbInfo_t );
329     p_front_buffer_info.size          = sizeof( GrLfbInfo_t );
330
331     /* lock the buffers to find their adresses */
332     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER,
333                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
334                    &p_front_buffer_info) == FXFALSE )
335     {
336         intf_ErrMsg( "vout error: can't take 3dfx front buffer lock" );
337         grGlideShutdown();
338         return( 1 );
339     }
340     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
341
342     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
343                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
344                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
345     {
346         intf_ErrMsg( "vout error: can't take 3dfx back buffer lock" );
347         grGlideShutdown();
348         return( 1 );
349     }
350     grLfbUnlock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
351     
352     grBufferClear( 0, 0, 0 );
353
354     return( 0 );
355 }
356
357 /*****************************************************************************
358  * CloseDisplay: close and reset 3dfx device
359  *****************************************************************************
360  * Returns all resources allocated by OpenDisplay and restore the original
361  * state of the device.
362  *****************************************************************************/
363 static void CloseDisplay( vout_thread_t *p_vout )
364 {
365     /* unlock the hidden buffer */
366     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
367
368     /* shutdown Glide */
369     grGlideShutdown();
370     free( p_vout->p_sys->p_video );
371 }
372
373 /*****************************************************************************
374  * NewPicture: allocate a picture
375  *****************************************************************************
376  * Returns 0 on success, -1 otherwise
377  *****************************************************************************/
378 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
379 {
380     /* We know the chroma, allocate a buffer which will be used
381      * directly by the decoder */
382     p_pic->p->p_pixels = p_vout->p_sys->p_video;
383     p_pic->p->i_pixel_bytes = GLIDE_BYTES_PER_PIXEL;
384     p_pic->p->i_lines = GLIDE_HEIGHT;
385
386     p_pic->p->b_margin = 1;
387     p_pic->p->b_hidden = 1;
388     p_pic->p->i_visible_bytes = GLIDE_WIDTH * GLIDE_BYTES_PER_PIXEL;
389     p_pic->p->i_pitch = p_vout->p_sys->p_buffer_info.strideInBytes;
390                          /*1024 * GLIDE_BYTES_PER_PIXEL*/
391
392     p_pic->p->i_red_mask =   0xf800;
393     p_pic->p->i_green_mask = 0x07e0;
394     p_pic->p->i_blue_mask =  0x001f;
395
396     p_pic->i_planes = 1;
397
398     return 0;
399 }
400