Add const where appropriate, also gets rid of a compiler warning.
[mplayer/glamo.git] / libvo / vo_3dfx.c
blobb86e593281330deb1335b831171de8bc141b184e
1 /*
2 * video_out_3dfx.c
3 * Heavily based on video_out_mga.c of Aaron Holtzman's mpeg2dec.
5 * Copyright (C) Colin Cross Apr 2000
7 * This file is part of MPlayer.
9 * MPlayer 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 * MPlayer 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 along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "config.h"
29 #include "mp_msg.h"
30 #include "help_mp.h"
31 #include "video_out.h"
32 #include "video_out_internal.h"
33 #include "x11_common.h"
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/mman.h>
39 #include <errno.h>
40 #include <wchar.h>
41 #include <signal.h>
43 #include <X11/Xlib.h>
44 #include <X11/extensions/xf86dga.h>
45 #include <X11/Xutil.h>
47 //#define LOG(x) syslog(LOG_USER | LOG_DEBUG,x)
48 #define LOG(x)
50 #include "drivers/3dfx.h"
52 #include "fastmemcpy.h"
54 static const vo_info_t info =
56 "3dfx (/dev/3dfx)",
57 "3dfx",
58 "Colin Cross <colin@MIT.EDU>",
62 const LIBVO_EXTERN(3dfx)
64 static uint32_t is_fullscreen = 1;
66 static uint32_t vidwidth;
67 static uint32_t vidheight;
69 static uint32_t screenwidth;
70 static uint32_t screenheight;
71 static uint32_t screendepth = 2; //Only 16bpp supported right now
73 static uint32_t dispwidth = 1280; // You can change these to whatever you want
74 static uint32_t dispheight = 720; // 16:9 screen ratio??
75 static uint32_t dispx;
76 static uint32_t dispy;
78 static uint32_t *vidpage0;
79 static uint32_t *vidpage1;
80 static uint32_t *vidpage2;
82 static uint32_t vidpage0offset;
83 static uint32_t vidpage1offset;
84 static uint32_t vidpage2offset;
86 // Current pointer into framebuffer where display is located
87 static uint32_t targetoffset;
89 static uint32_t page_space;
91 static voodoo_io_reg *reg_IO;
92 static voodoo_2d_reg *reg_2d;
93 static voodoo_yuv_reg *reg_YUV;
94 static voodoo_yuv_fb *fb_YUV;
96 static uint32_t *memBase0, *memBase1;
97 static uint32_t baseAddr0, baseAddr1;
100 /* X11 related variables */
101 static Display *display;
102 static Window mywindow;
103 static int bpp;
104 static XWindowAttributes attribs;
106 static int fd=-1;
109 static void
110 restore(void)
112 //reg_IO->vidDesktopStartAddr = vidpage0offset;
113 XF86DGADirectVideo(display,0,0);
116 static void
117 sighup(int foo)
119 //reg_IO->vidDesktopStartAddr = vidpage0offset;
120 XF86DGADirectVideo(display,0,0);
121 exit(0);
124 static void
125 restore_regs(voodoo_2d_reg *regs)
127 reg_2d->commandExtra = regs->commandExtra;
128 reg_2d->clip0Min = regs->clip0Min;
129 reg_2d->clip0Max = regs->clip0Max;
131 reg_2d->srcBaseAddr = regs->srcBaseAddr;
132 reg_2d->srcXY = regs->srcXY;
133 reg_2d->srcFormat = regs->srcFormat;
134 reg_2d->srcSize = regs->srcSize;
136 reg_2d->dstBaseAddr = regs->dstBaseAddr;
137 reg_2d->dstXY = regs->dstXY;
138 reg_2d->dstFormat = regs->dstFormat;
140 reg_2d->dstSize = regs->dstSize;
141 reg_2d->command = 0;
144 static uint32_t
145 create_window(Display *display, char *title)
147 int screen;
148 unsigned int fg, bg;
149 XSizeHints hint;
150 XVisualInfo vinfo;
151 XEvent xev;
153 Colormap theCmap;
154 XSetWindowAttributes xswa;
155 unsigned long xswamask;
157 screen = DefaultScreen(display);
159 hint.x = 0;
160 hint.y = 10;
161 hint.width = dispwidth;
162 hint.height = dispheight;
163 hint.flags = PPosition | PSize;
165 bg = WhitePixel(display, screen);
166 fg = BlackPixel(display, screen);
168 XGetWindowAttributes(display, DefaultRootWindow(display), &attribs);
169 bpp = attribs.depth;
170 if (bpp != 16)
172 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_Only16BppSupported);
173 exit(-1);
176 XMatchVisualInfo(display,screen,bpp,TrueColor,&vinfo);
177 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_3DFX_VisualIdIs,vinfo.visualid);
179 theCmap = XCreateColormap(display, RootWindow(display,screen),
180 vinfo.visual, AllocNone);
182 xswa.background_pixel = 0;
183 xswa.border_pixel = 1;
184 xswa.colormap = theCmap;
185 xswamask = CWBackPixel | CWBorderPixel |CWColormap;
188 mywindow = XCreateWindow(display, RootWindow(display,screen),
189 hint.x, hint.y, hint.width, hint.height, 4, bpp,CopyFromParent,vinfo.visual,xswamask,&xswa);
190 vo_x11_classhint( display,mywindow,"3dfx" );
192 XSelectInput(display, mywindow, StructureNotifyMask);
194 /* Tell other applications about this window */
196 XSetStandardProperties(display, mywindow, title, title, None, NULL, 0, &hint);
198 /* Map window. */
200 XMapWindow(display, mywindow);
202 /* Wait for map. */
205 XNextEvent(display, &xev);
207 while (xev.type != MapNotify || xev.xmap.event != mywindow);
209 XSelectInput(display, mywindow, NoEventMask);
211 XSync(display, False);
213 return 0;
216 static void
217 dump_yuv_planar(uint32_t *y, uint32_t *u, uint32_t *v, uint32_t to, uint32_t width, uint32_t height)
219 // YUV conversion works like this:
221 // We write the Y, U, and V planes separately into 3dfx YUV Planar memory
222 // region. The nice chip then takes these and packs them into the YUYV
223 // format in the regular frame buffer, starting at yuvBaseAddr, page 2 here.
224 // Then we tell the 3dfx to do a Screen to Screen Stretch BLT to copy all
225 // of the data on page 2 onto page 1, converting it to 16 bpp RGB as it goes.
226 // The result is a nice image on page 1 ready for display.
228 uint32_t j;
229 uint32_t y_imax, uv_imax, jmax;
231 reg_YUV->yuvBaseAddr = to;
232 reg_YUV->yuvStride = screenwidth*2;
234 LOG("video_out_3dfx: starting planar dump\n");
235 jmax = height>>1; // vidheight/2, height of U and V planes
236 y_imax = width; // Y plane is twice as wide as U and V planes
237 uv_imax = width>>1; // vidwidth/2/4, width of U and V planes in 32-bit words
239 for (j=0;j<jmax;j++)
241 //XXX this should be hand-rolled 32 bit memcpy for safeness.
242 fast_memcpy(fb_YUV->U + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) u) + uv_imax* j , uv_imax);
243 fast_memcpy(fb_YUV->V + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) v) + uv_imax* j , uv_imax);
244 fast_memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE* (j<<1) ,((uint8_t*) y) + y_imax * (j<<1) , y_imax);
245 fast_memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE*((j<<1)+1),((uint8_t*) y) + y_imax *((j<<1)+1), y_imax);
247 LOG("video_out_3dfx: done planar dump\n");
250 static void
251 screen_to_screen_stretch_blt(uint32_t to, uint32_t from, uint32_t width, uint32_t height)
253 //FIXME - this function should be called by a show_frame function that
254 // uses a series of blts to show only those areas not covered
255 // by another window
256 voodoo_2d_reg saved_regs;
258 LOG("video_out_3dfx: saving registers\n");
259 // Save VGA regs (so X kinda works when we're done)
260 saved_regs = *reg_2d;
262 /* The following lines set up the screen to screen stretch blt from page2 to
263 page 1
266 LOG("video_out_3dfx: setting blt registers\n");
267 reg_2d->commandExtra = 4; //disable colorkeying, enable wait for v-refresh (0100b)
268 reg_2d->clip0Min = 0;
269 reg_2d->clip0Max = 0xFFFFFFFF; //no clipping
271 reg_2d->srcBaseAddr = from;
272 reg_2d->srcXY = 0;
273 reg_2d->srcFormat = screenwidth*2 | VOODOO_BLT_FORMAT_YUYV; // | 1<<21;
274 reg_2d->srcSize = vidwidth | (vidheight << 16);
276 reg_2d->dstBaseAddr = to;
277 reg_2d->dstXY = 0;
278 reg_2d->dstFormat = screenwidth*2 | VOODOO_BLT_FORMAT_16;
280 reg_2d->dstSize = width | (height << 16);
282 LOG("video_out_3dfx: starting blt\n");
283 // Executes screen to screen stretch blt
284 reg_2d->command = 2 | 1<<8 | 0xCC<<24;
286 LOG("video_out_3dfx: restoring regs\n");
287 restore_regs(&saved_regs);
289 LOG("video_out_3dfx: done blt\n");
292 static void
293 update_target(void)
295 uint32_t xp, yp, w, h, b, d;
296 Window root;
298 XGetGeometry(display,mywindow,&root,&xp,&yp,&w,&h,&b,&d);
299 XTranslateCoordinates(display,mywindow,root,0,0,&xp,&yp,&root);
300 dispx = (uint32_t) xp;
301 dispy = (uint32_t) yp;
302 dispwidth = (uint32_t) w;
303 dispheight = (uint32_t) h;
305 if (is_fullscreen)
306 targetoffset = vidpage0offset + (screenheight - dispheight)/2*screenwidth*screendepth + (screenwidth-dispwidth)/2*screendepth;
307 else
308 targetoffset = vidpage0offset + (dispy*screenwidth + dispx)*screendepth;
311 static int
312 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format)
314 char *name = ":0.0";
315 pioData data;
316 uint32_t retval;
318 //TODO use x11_common for X and window handling
320 if(getenv("DISPLAY"))
321 name = getenv("DISPLAY");
322 display = XOpenDisplay(name);
324 screenwidth = XDisplayWidth(display,0);
325 screenheight = XDisplayHeight(display,0);
327 page_space = screenwidth*screenheight*screendepth;
328 vidpage0offset = 0;
329 vidpage1offset = page_space; // Use third and fourth pages
330 vidpage2offset = page_space*2;
332 signal(SIGALRM,sighup);
333 //alarm(120);
335 // Open driver device
336 if ( fd == -1 )
338 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_UnableToOpenDevice);
339 return -1;
342 // Store sizes for later
343 vidwidth = width;
344 vidheight = height;
346 is_fullscreen = fullscreen = 0;
347 if (!is_fullscreen)
348 create_window(display, title);
350 // Ask 3dfx driver for base memory address 0
351 data.port = 0x10; // PCI_BASE_ADDRESS_0_LINUX;
352 data.size = 4;
353 data.value = &baseAddr0;
354 data.device = 0;
355 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
357 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_Error,retval);
358 return -1;
361 // Ask 3dfx driver for base memory address 1
362 data.port = 0x14; // PCI_BASE_ADDRESS_1_LINUX;
363 data.size = 4;
364 data.value = &baseAddr1;
365 data.device = 0;
366 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
368 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_Error,retval);
369 return -1;
372 // Map all 3dfx memory areas
373 memBase0 = mmap(0,0x1000000,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr0);
374 memBase1 = mmap(0,3*page_space,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr1);
375 if (memBase0 == (uint32_t *) 0xFFFFFFFF || memBase1 == (uint32_t *) 0xFFFFFFFF)
377 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_CouldntMapMemoryArea,
378 memBase0,memBase1,errno);
381 // Set up global pointers
382 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
383 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
384 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
385 fb_YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
387 vidpage0 = (void *)memBase1 + (unsigned long int)vidpage0offset;
388 vidpage1 = (void *)memBase1 + (unsigned long int)vidpage1offset;
389 vidpage2 = (void *)memBase1 + (unsigned long int)vidpage2offset;
391 // Clear pages 1,2,3
392 // leave page 0, that belongs to X.
393 // So does part of 1. Oops.
394 memset(vidpage1,0x00,page_space);
395 memset(vidpage2,0x00,page_space);
397 if (is_fullscreen)
398 memset(vidpage0,0x00,page_space);
401 #ifndef VOODOO_DEBUG
402 // Show page 0 (unblanked)
403 reg_IO->vidDesktopStartAddr = vidpage0offset;
405 /* Stop X from messing with my video registers!
406 Find a better way to do this?
407 Currently I use DGA to tell XF86 to not screw with registers, but I can't really use it
408 to do FB stuff because I need to know the absolute FB position and offset FB position
409 to feed to BLT command
411 //XF86DGADirectVideo(display,0,XF86DGADirectGraphics); //| XF86DGADirectMouse | XF86DGADirectKeyb);
412 #endif
414 atexit(restore);
416 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_3DFX_DisplayInitialized,memBase1);
417 return 0;
420 static int
421 draw_frame(uint8_t *src[])
423 LOG("video_out_3dfx: starting display_frame\n");
425 // Put packed data onto page 2
426 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],
427 vidpage2offset,vidwidth,vidheight);
429 LOG("video_out_3dfx: done display_frame\n");
430 return 0;
433 static int
434 //draw_slice(uint8_t *src[], uint32_t slice_num)
435 draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
437 uint32_t target;
439 target = vidpage2offset + (screenwidth*2 * y);
440 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],target,vidwidth,h);
441 return 0;
444 static void draw_osd(void)
448 static void
449 flip_page(void)
451 //FIXME - update_target() should be called by event handler when window
452 // is resized or moved
453 update_target();
454 LOG("video_out_3dfx: calling blt function\n");
455 screen_to_screen_stretch_blt(targetoffset, vidpage2offset, dispwidth, dispheight);
458 static int
459 query_format(uint32_t format)
461 /* does this supports scaling? up & down? */
462 switch(format){
463 case IMGFMT_YV12:
464 // case IMGFMT_YUY2:
465 // case IMGFMT_RGB|24:
466 // case IMGFMT_BGR|24:
467 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
469 return 0;
472 static void
473 uninit(void)
475 if( fd != -1 )
476 close(fd);
480 static void check_events(void)
484 static int preinit(const char *arg)
486 if ( (fd = open("/dev/3dfx",O_RDWR) ) == -1)
488 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_UnableToOpenDevice);
489 return -1;
492 if(arg)
494 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_3DFX_UnknownSubdevice,arg);
495 return ENOSYS;
497 return 0;
500 static int control(uint32_t request, void *data, ...)
502 switch (request) {
503 case VOCTRL_QUERY_FORMAT:
504 return query_format(*((uint32_t*)data));
506 return VO_NOTIMPL;