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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <stdlib.h> /* malloc(), free() */
32 # include <conio.h> /* for glide ? */
35 #include <linutil.h> /* Glide kbhit() and getch() */
37 #include <videolan/vlc.h>
40 #include "video_output.h"
42 #include "interface.h"
44 #define GLIDE_WIDTH 800
45 #define GLIDE_HEIGHT 600
46 #define GLIDE_BITS_PER_PLANE 16
47 #define GLIDE_BYTES_PER_PIXEL 2
49 /*****************************************************************************
51 *****************************************************************************/
52 static void vout_getfunctions( function_list_t * p_function_list );
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 * );
63 static int NewPicture ( vout_thread_t *, picture_t * );
64 static int OpenDisplay ( vout_thread_t * );
65 static void CloseDisplay ( vout_thread_t * );
67 /*****************************************************************************
68 * Building configuration tree
69 *****************************************************************************/
74 SET_DESCRIPTION( "3dfx Glide module" )
75 ADD_CAPABILITY( VOUT, 20 )
76 ADD_SHORTCUT( "glide" )
77 ADD_SHORTCUT( "3dfx" )
81 vout_getfunctions( &p_module->p_functions->vout );
84 MODULE_DEACTIVATE_START
85 MODULE_DEACTIVATE_STOP
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
95 GrLfbInfo_t p_buffer_info; /* back buffer info */
97 /* Dummy video memory */
98 byte_t * p_video; /* base adress */
99 size_t i_page_size; /* page size */
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 )
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;
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 )
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 */
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 )
141 /* Allocate structure */
142 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
143 if( p_vout->p_sys == NULL )
145 intf_ErrMsg("error: %s", strerror(ENOMEM) );
149 /* Open and initialize device */
150 if( OpenDisplay( p_vout ) )
152 intf_ErrMsg("vout error: can't open display");
153 free( p_vout->p_sys );
160 /*****************************************************************************
161 * vout_Init: initialize Glide video thread output method
162 *****************************************************************************/
163 int vout_Init( vout_thread_t *p_vout )
168 I_OUTPUTPICTURES = 0;
170 /* Try to initialize up to 1 direct buffers */
171 while( I_OUTPUTPICTURES < 1 )
175 /* Find an empty picture slot */
176 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
178 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
180 p_pic = p_vout->p_picture + i_index;
185 /* Allocate the picture */
186 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
191 p_pic->i_status = DESTROYED_PICTURE;
192 p_pic->i_type = DIRECT_PICTURE;
194 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
202 /*****************************************************************************
203 * vout_End: terminate Glide video thread output method
204 *****************************************************************************/
205 void vout_End( vout_thread_t *p_vout )
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 )
217 CloseDisplay( p_vout );
218 free( p_vout->p_sys );
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 )
231 /* very Linux specific - see tlib.c in Glide for other versions */
239 p_main->p_intf->b_die = 1;
250 /*****************************************************************************
251 * vout_Render: renders previously calculated output
252 *****************************************************************************/
253 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
258 /*****************************************************************************
259 * vout_Display: displays previously rendered output
260 *****************************************************************************/
261 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
264 grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
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 )
272 intf_ErrMsg( "vout error: can't take 3dfx back buffer lock" );
276 /* following functions are local */
278 /*****************************************************************************
279 * OpenDisplay: open and initialize 3dfx device
280 *****************************************************************************/
282 static int OpenDisplay( vout_thread_t *p_vout )
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 */
289 p_vout->p_sys->i_page_size = GLIDE_WIDTH * GLIDE_HEIGHT
290 * GLIDE_BYTES_PER_PIXEL;
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 )
296 intf_ErrMsg( "vout error: can't map video memory (%s)",
301 grGlideGetVersion( version );
304 if( !grSstQueryHardware(&hwconfig) )
306 intf_ErrMsg( "vout error: can't get 3dfx hardware config" );
311 if( !grSstWinOpen(0, resolution, GR_REFRESH_60Hz,
312 GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1) )
314 intf_ErrMsg( "vout error: can't open 3dfx screen" );
318 /* disable dithering */
319 //grDitherMode( GR_DITHER_DISABLE );
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 );
328 p_vout->p_sys->p_buffer_info.size = sizeof( GrLfbInfo_t );
329 p_front_buffer_info.size = sizeof( GrLfbInfo_t );
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 )
336 intf_ErrMsg( "vout error: can't take 3dfx front buffer lock" );
340 grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
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 )
346 intf_ErrMsg( "vout error: can't take 3dfx back buffer lock" );
350 grLfbUnlock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
352 grBufferClear( 0, 0, 0 );
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 )
365 /* unlock the hidden buffer */
366 grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
370 free( p_vout->p_sys->p_video );
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 )
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;
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*/
392 p_pic->p->i_red_mask = 0xf800;
393 p_pic->p->i_green_mask = 0x07e0;
394 p_pic->p->i_blue_mask = 0x001f;