comparison libvo/vo_3dfx.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 1fc618eba830
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
1 /*
2 * video_out_3dfx.c
3 *
4 * Copyright (C) Colin Cross Apr 2000
5 *
6 * This file heavily based off of video_out_mga.c of Aaron Holtzman's
7 * mpeg2dec
8 *
9 * mpeg2dec 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, or (at your option)
12 * any later version.
13 *
14 * mpeg2dec 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 GNU Make; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "config.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
32
33 LIBVO_EXTERN(3dfx)
34
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>
42
43 #include <X11/Xlib.h>
44 #include <X11/extensions/xf86dga.h>
45 #include <X11/Xutil.h>
46
47 //#define LOG(x) syslog(LOG_USER | LOG_DEBUG,x)
48 #define LOG(x)
49
50 #include "drivers/3dfx.h"
51
52 static vo_info_t vo_info =
53 {
54 "3dfx (/dev/3dfx)",
55 "3dfx",
56 "Colin Cross <colin@MIT.EDU>",
57 ""
58 };
59
60 static uint32_t is_fullscreen = 1;
61
62 static uint32_t vidwidth;
63 static uint32_t vidheight;
64
65 static uint32_t screenwidth;
66 static uint32_t screenheight;
67 static uint32_t screendepth = 2; //Only 16bpp supported right now
68
69 static uint32_t dispwidth = 1280; // You can change these to whatever you want
70 static uint32_t dispheight = 720; // 16:9 screen ratio??
71 static uint32_t dispx;
72 static uint32_t dispy;
73
74 static uint32_t *vidpage0;
75 static uint32_t *vidpage1;
76 static uint32_t *vidpage2;
77
78 static uint32_t vidpage0offset;
79 static uint32_t vidpage1offset;
80 static uint32_t vidpage2offset;
81
82 // Current pointer into framebuffer where display is located
83 static uint32_t targetoffset;
84
85 static uint32_t page_space;
86
87 static voodoo_io_reg *reg_IO;
88 static voodoo_2d_reg *reg_2d;
89 static voodoo_yuv_reg *reg_YUV;
90 static voodoo_yuv_fb *fb_YUV;
91
92 static uint32_t *memBase0, *memBase1;
93 static uint32_t baseAddr0, baseAddr1;
94
95
96 /* X11 related variables */
97 static Display *display;
98 static Window mywindow;
99 static int bpp;
100 static XWindowAttributes attribs;
101 static int X_already_started = 0;
102
103
104 static void
105 restore(void)
106 {
107 //reg_IO->vidDesktopStartAddr = vidpage0offset;
108 XF86DGADirectVideo(display,0,0);
109 }
110
111 static void
112 sighup(int foo)
113 {
114 //reg_IO->vidDesktopStartAddr = vidpage0offset;
115 XF86DGADirectVideo(display,0,0);
116 exit(0);
117 }
118
119 static void
120 restore_regs(voodoo_2d_reg *regs)
121 {
122 reg_2d->commandExtra = regs->commandExtra;
123 reg_2d->clip0Min = regs->clip0Min;
124 reg_2d->clip0Max = regs->clip0Max;
125
126 reg_2d->srcBaseAddr = regs->srcBaseAddr;
127 reg_2d->srcXY = regs->srcXY;
128 reg_2d->srcFormat = regs->srcFormat;
129 reg_2d->srcSize = regs->srcSize;
130
131 reg_2d->dstBaseAddr = regs->dstBaseAddr;
132 reg_2d->dstXY = regs->dstXY;
133 reg_2d->dstFormat = regs->dstFormat;
134
135 reg_2d->dstSize = regs->dstSize;
136 reg_2d->command = 0;
137 }
138
139 static uint32_t
140 create_window(Display *display)
141 {
142 int screen;
143 unsigned int fg, bg;
144 char *hello = "I hate X11";
145 XSizeHints hint;
146 XVisualInfo vinfo;
147 XEvent xev;
148
149 Colormap theCmap;
150 XSetWindowAttributes xswa;
151 unsigned long xswamask;
152
153 if (X_already_started)
154 return -1;
155
156 screen = DefaultScreen(display);
157
158 hint.x = 0;
159 hint.y = 10;
160 hint.width = dispwidth;
161 hint.height = dispheight;
162 hint.flags = PPosition | PSize;
163
164 bg = WhitePixel(display, screen);
165 fg = BlackPixel(display, screen);
166
167 XGetWindowAttributes(display, DefaultRootWindow(display), &attribs);
168 bpp = attribs.depth;
169 if (bpp != 16)
170 {
171 fprintf(stderr,"Only 16bpp supported!");
172 exit(-1);
173 }
174
175 XMatchVisualInfo(display,screen,bpp,TrueColor,&vinfo);
176 printf("visual id is %lx\n",vinfo.visualid);
177
178 theCmap = XCreateColormap(display, RootWindow(display,screen),
179 vinfo.visual, AllocNone);
180
181 xswa.background_pixel = 0;
182 xswa.border_pixel = 1;
183 xswa.colormap = theCmap;
184 xswamask = CWBackPixel | CWBorderPixel |CWColormap;
185
186
187 mywindow = XCreateWindow(display, RootWindow(display,screen),
188 hint.x, hint.y, hint.width, hint.height, 4, bpp,CopyFromParent,vinfo.visual,xswamask,&xswa);
189
190 XSelectInput(display, mywindow, StructureNotifyMask);
191
192 /* Tell other applications about this window */
193
194 XSetStandardProperties(display, mywindow, hello, hello, None, NULL, 0, &hint);
195
196 /* Map window. */
197
198 XMapWindow(display, mywindow);
199
200 /* Wait for map. */
201 do
202 {
203 XNextEvent(display, &xev);
204 }
205 while (xev.type != MapNotify || xev.xmap.event != mywindow);
206
207 XSelectInput(display, mywindow, NoEventMask);
208
209 XFlush(display);
210 XSync(display, False);
211
212 X_already_started++;
213 return 0;
214 }
215
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)
218 {
219 // YUV conversion works like this:
220 //
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.
227
228 uint32_t j;
229 uint32_t y_imax, uv_imax, jmax;
230
231 reg_YUV->yuvBaseAddr = to;
232 reg_YUV->yuvStride = screenwidth*2;
233
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
238
239 for (j=0;j<jmax;j++)
240 {
241 //XXX this should be hand-rolled 32 bit memcpy for safeness.
242 memcpy(fb_YUV->U + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) u) + uv_imax* j , uv_imax);
243 memcpy(fb_YUV->V + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) v) + uv_imax* j , uv_imax);
244 memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE* (j<<1) ,((uint8_t*) y) + y_imax * (j<<1) , y_imax);
245 memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE*((j<<1)+1),((uint8_t*) y) + y_imax *((j<<1)+1), y_imax);
246 }
247 LOG("video_out_3dfx: done planar dump\n");
248 }
249
250 static void
251 screen_to_screen_stretch_blt(uint32_t to, uint32_t from, uint32_t width, uint32_t height)
252 {
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;
257
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;
261
262 /* The following lines set up the screen to screen stretch blt from page2 to
263 page 1
264 */
265
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
270
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);
275
276 reg_2d->dstBaseAddr = to;
277 reg_2d->dstXY = 0;
278 reg_2d->dstFormat = screenwidth*2 | VOODOO_BLT_FORMAT_16;
279
280 reg_2d->dstSize = width | (height << 16);
281
282 LOG("video_out_3dfx: starting blt\n");
283 // Executes screen to screen stretch blt
284 reg_2d->command = 2 | 1<<8 | 0xCC<<24;
285
286 LOG("video_out_3dfx: restoring regs\n");
287 restore_regs(&saved_regs);
288
289 LOG("video_out_3dfx: done blt\n");
290 }
291
292 static void
293 update_target(void)
294 {
295 uint32_t xp, yp, w, h, b, d;
296 Window root;
297
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;
304
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;
309 }
310
311 static uint32_t
312 init(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format)
313 {
314 int fd;
315 char *name = ":0.0";
316 pioData data;
317 uint32_t retval;
318
319 if(getenv("DISPLAY"))
320 name = getenv("DISPLAY");
321 display = XOpenDisplay(name);
322
323 screenwidth = XDisplayWidth(display,0);
324 screenheight = XDisplayHeight(display,0);
325
326 page_space = screenwidth*screenheight*screendepth;
327 vidpage0offset = 0;
328 vidpage1offset = page_space; // Use third and fourth pages
329 vidpage2offset = page_space*2;
330
331 signal(SIGALRM,sighup);
332 //alarm(120);
333
334 // Open driver device
335 if ( (fd = open("/dev/3dfx",O_RDWR) ) == -1)
336 {
337 fprintf(stderr,"Couldn't open /dev/3dfx\n");
338 exit(1);
339 }
340
341 // Store sizes for later
342 vidwidth = width;
343 vidheight = height;
344
345 is_fullscreen = fullscreen = 0;
346 if (!is_fullscreen)
347 create_window(display);
348
349 // Ask 3dfx driver for base memory address 0
350 data.port = 0x10; // PCI_BASE_ADDRESS_0_LINUX;
351 data.size = 4;
352 data.value = &baseAddr0;
353 data.device = 0;
354 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
355 {
356 printf("Error: %d\n",retval);
357 //return -1;
358 }
359
360 // Ask 3dfx driver for base memory address 1
361 data.port = 0x14; // PCI_BASE_ADDRESS_1_LINUX;
362 data.size = 4;
363 data.value = &baseAddr1;
364 data.device = 0;
365 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
366 {
367 printf("Error: %d\n",retval);
368 //return -1;
369 }
370
371 // Map all 3dfx memory areas
372 memBase0 = mmap(0,0x1000000,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr0);
373 memBase1 = mmap(0,3*page_space,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr1);
374 if (memBase0 == (uint32_t *) 0xFFFFFFFF || memBase1 == (uint32_t *) 0xFFFFFFFF)
375 {
376 printf("Couldn't map 3dfx memory areas: %p,%p,%d\n",
377 memBase0,memBase1,errno);
378 }
379
380 // Set up global pointers
381 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
382 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
383 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
384 fb_YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
385
386 vidpage0 = (void *)memBase1 + (unsigned long int)vidpage0offset;
387 vidpage1 = (void *)memBase1 + (unsigned long int)vidpage1offset;
388 vidpage2 = (void *)memBase1 + (unsigned long int)vidpage2offset;
389
390 // Clear pages 1,2,3
391 // leave page 0, that belongs to X.
392 // So does part of 1. Oops.
393 memset(vidpage1,0x00,page_space);
394 memset(vidpage2,0x00,page_space);
395
396 if (is_fullscreen)
397 memset(vidpage0,0x00,page_space);
398
399
400 #ifndef VOODOO_DEBUG
401 // Show page 0 (unblanked)
402 reg_IO->vidDesktopStartAddr = vidpage0offset;
403
404 /* Stop X from messing with my video registers!
405 Find a better way to do this?
406 Currently I use DGA to tell XF86 to not screw with registers, but I can't really use it
407 to do FB stuff because I need to know the absolute FB position and offset FB position
408 to feed to BLT command
409 */
410 //XF86DGADirectVideo(display,0,XF86DGADirectGraphics); //| XF86DGADirectMouse | XF86DGADirectKeyb);
411 #endif
412
413 /* fd is deliberately not closed - if it were, mmaps might be released??? */
414
415 atexit(restore);
416
417 printf("(display) 3dfx initialized %p\n",memBase1);
418 return 0;
419 }
420
421 static const vo_info_t*
422 get_info(void)
423 {
424 return &vo_info;
425 }
426
427 static uint32_t
428 draw_frame(uint8_t *src[])
429 {
430 LOG("video_out_3dfx: starting display_frame\n");
431
432 // Put packed data onto page 2
433 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],
434 vidpage2offset,vidwidth,vidheight);
435
436 LOG("video_out_3dfx: done display_frame\n");
437 return 0;
438 }
439
440 static uint32_t
441 draw_slice(uint8_t *src[], uint32_t slice_num)
442 {
443 uint32_t target;
444
445 target = vidpage2offset + (screenwidth*2 * 16*slice_num);
446 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],target,vidwidth,16);
447 return 0;
448 }
449
450 static void
451 flip_page(void)
452 {
453 //FIXME - update_target() should be called by event handler when window
454 // is resized or moved
455 update_target();
456 LOG("video_out_3dfx: calling blt function\n");
457 screen_to_screen_stretch_blt(targetoffset, vidpage2offset, dispwidth, dispheight);
458 }
459
460 static uint32_t
461 query_format(uint32_t format)
462 {
463 switch(format){
464 case IMGFMT_YV12:
465 // case IMGFMT_YUY2:
466 // case IMGFMT_RGB|24:
467 // case IMGFMT_BGR|24:
468 return 1;
469 }
470 return 0;
471 }
472
473 static void
474 uninit(void)
475 {
476 }
477
478