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