1
|
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 #include <sys/ioctl.h>
|
|
34 #include <unistd.h>
|
|
35 #include <fcntl.h>
|
|
36 #include <sys/mman.h>
|
|
37 #include <errno.h>
|
|
38 #include <wchar.h>
|
|
39 #include <signal.h>
|
|
40
|
|
41 #include <X11/Xlib.h>
|
|
42 #include <X11/extensions/xf86dga.h>
|
|
43 #include <X11/Xutil.h>
|
|
44
|
|
45 //#define LOG(x) syslog(LOG_USER | LOG_DEBUG,x)
|
|
46 #define LOG(x)
|
|
47
|
|
48 #include "drivers/3dfx.h"
|
|
49
|
354
|
50 #include "fastmemcpy.h"
|
350
|
51
|
8148
|
52 static vo_info_t info =
|
1
|
53 {
|
|
54 "3dfx (/dev/3dfx)",
|
|
55 "3dfx",
|
|
56 "Colin Cross <colin@MIT.EDU>",
|
|
57 ""
|
|
58 };
|
|
59
|
8148
|
60 LIBVO_EXTERN(3dfx)
|
|
61
|
1
|
62 static uint32_t is_fullscreen = 1;
|
|
63
|
|
64 static uint32_t vidwidth;
|
|
65 static uint32_t vidheight;
|
|
66
|
|
67 static uint32_t screenwidth;
|
|
68 static uint32_t screenheight;
|
|
69 static uint32_t screendepth = 2; //Only 16bpp supported right now
|
|
70
|
|
71 static uint32_t dispwidth = 1280; // You can change these to whatever you want
|
|
72 static uint32_t dispheight = 720; // 16:9 screen ratio??
|
|
73 static uint32_t dispx;
|
|
74 static uint32_t dispy;
|
|
75
|
|
76 static uint32_t *vidpage0;
|
|
77 static uint32_t *vidpage1;
|
|
78 static uint32_t *vidpage2;
|
|
79
|
|
80 static uint32_t vidpage0offset;
|
|
81 static uint32_t vidpage1offset;
|
|
82 static uint32_t vidpage2offset;
|
|
83
|
|
84 // Current pointer into framebuffer where display is located
|
|
85 static uint32_t targetoffset;
|
|
86
|
|
87 static uint32_t page_space;
|
|
88
|
|
89 static voodoo_io_reg *reg_IO;
|
|
90 static voodoo_2d_reg *reg_2d;
|
|
91 static voodoo_yuv_reg *reg_YUV;
|
|
92 static voodoo_yuv_fb *fb_YUV;
|
|
93
|
|
94 static uint32_t *memBase0, *memBase1;
|
|
95 static uint32_t baseAddr0, baseAddr1;
|
|
96
|
|
97
|
|
98 /* X11 related variables */
|
|
99 static Display *display;
|
|
100 static Window mywindow;
|
|
101 static int bpp;
|
|
102 static XWindowAttributes attribs;
|
|
103
|
17162
|
104 static int fd=-1;
|
|
105
|
1
|
106
|
|
107 static void
|
|
108 restore(void)
|
|
109 {
|
|
110 //reg_IO->vidDesktopStartAddr = vidpage0offset;
|
|
111 XF86DGADirectVideo(display,0,0);
|
|
112 }
|
|
113
|
|
114 static void
|
|
115 sighup(int foo)
|
|
116 {
|
|
117 //reg_IO->vidDesktopStartAddr = vidpage0offset;
|
|
118 XF86DGADirectVideo(display,0,0);
|
|
119 exit(0);
|
|
120 }
|
|
121
|
|
122 static void
|
|
123 restore_regs(voodoo_2d_reg *regs)
|
|
124 {
|
|
125 reg_2d->commandExtra = regs->commandExtra;
|
|
126 reg_2d->clip0Min = regs->clip0Min;
|
|
127 reg_2d->clip0Max = regs->clip0Max;
|
|
128
|
|
129 reg_2d->srcBaseAddr = regs->srcBaseAddr;
|
|
130 reg_2d->srcXY = regs->srcXY;
|
|
131 reg_2d->srcFormat = regs->srcFormat;
|
|
132 reg_2d->srcSize = regs->srcSize;
|
|
133
|
|
134 reg_2d->dstBaseAddr = regs->dstBaseAddr;
|
|
135 reg_2d->dstXY = regs->dstXY;
|
|
136 reg_2d->dstFormat = regs->dstFormat;
|
|
137
|
|
138 reg_2d->dstSize = regs->dstSize;
|
|
139 reg_2d->command = 0;
|
|
140 }
|
|
141
|
|
142 static uint32_t
|
10994
|
143 create_window(Display *display, char *title)
|
1
|
144 {
|
|
145 int screen;
|
|
146 unsigned int fg, bg;
|
|
147 XSizeHints hint;
|
|
148 XVisualInfo vinfo;
|
|
149 XEvent xev;
|
|
150
|
|
151 Colormap theCmap;
|
|
152 XSetWindowAttributes xswa;
|
|
153 unsigned long xswamask;
|
|
154
|
|
155 screen = DefaultScreen(display);
|
|
156
|
|
157 hint.x = 0;
|
|
158 hint.y = 10;
|
|
159 hint.width = dispwidth;
|
|
160 hint.height = dispheight;
|
|
161 hint.flags = PPosition | PSize;
|
|
162
|
|
163 bg = WhitePixel(display, screen);
|
|
164 fg = BlackPixel(display, screen);
|
|
165
|
|
166 XGetWindowAttributes(display, DefaultRootWindow(display), &attribs);
|
|
167 bpp = attribs.depth;
|
|
168 if (bpp != 16)
|
|
169 {
|
614
|
170 printf("Only 16bpp supported!");
|
1
|
171 exit(-1);
|
|
172 }
|
|
173
|
|
174 XMatchVisualInfo(display,screen,bpp,TrueColor,&vinfo);
|
|
175 printf("visual id is %lx\n",vinfo.visualid);
|
|
176
|
|
177 theCmap = XCreateColormap(display, RootWindow(display,screen),
|
|
178 vinfo.visual, AllocNone);
|
|
179
|
|
180 xswa.background_pixel = 0;
|
|
181 xswa.border_pixel = 1;
|
|
182 xswa.colormap = theCmap;
|
|
183 xswamask = CWBackPixel | CWBorderPixel |CWColormap;
|
|
184
|
|
185
|
|
186 mywindow = XCreateWindow(display, RootWindow(display,screen),
|
|
187 hint.x, hint.y, hint.width, hint.height, 4, bpp,CopyFromParent,vinfo.visual,xswamask,&xswa);
|
1137
|
188 vo_x11_classhint( display,mywindow,"3dfx" );
|
1
|
189
|
|
190 XSelectInput(display, mywindow, StructureNotifyMask);
|
|
191
|
|
192 /* Tell other applications about this window */
|
|
193
|
10994
|
194 XSetStandardProperties(display, mywindow, title, title, None, NULL, 0, &hint);
|
1
|
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 XSync(display, False);
|
|
210
|
|
211 return 0;
|
|
212 }
|
|
213
|
|
214 static void
|
|
215 dump_yuv_planar(uint32_t *y, uint32_t *u, uint32_t *v, uint32_t to, uint32_t width, uint32_t height)
|
|
216 {
|
|
217 // YUV conversion works like this:
|
|
218 //
|
|
219 // We write the Y, U, and V planes separately into 3dfx YUV Planar memory
|
|
220 // region. The nice chip then takes these and packs them into the YUYV
|
|
221 // format in the regular frame buffer, starting at yuvBaseAddr, page 2 here.
|
|
222 // Then we tell the 3dfx to do a Screen to Screen Stretch BLT to copy all
|
|
223 // of the data on page 2 onto page 1, converting it to 16 bpp RGB as it goes.
|
|
224 // The result is a nice image on page 1 ready for display.
|
|
225
|
|
226 uint32_t j;
|
|
227 uint32_t y_imax, uv_imax, jmax;
|
|
228
|
|
229 reg_YUV->yuvBaseAddr = to;
|
|
230 reg_YUV->yuvStride = screenwidth*2;
|
|
231
|
|
232 LOG("video_out_3dfx: starting planar dump\n");
|
|
233 jmax = height>>1; // vidheight/2, height of U and V planes
|
|
234 y_imax = width; // Y plane is twice as wide as U and V planes
|
|
235 uv_imax = width>>1; // vidwidth/2/4, width of U and V planes in 32-bit words
|
|
236
|
|
237 for (j=0;j<jmax;j++)
|
|
238 {
|
|
239 //XXX this should be hand-rolled 32 bit memcpy for safeness.
|
|
240 memcpy(fb_YUV->U + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) u) + uv_imax* j , uv_imax);
|
|
241 memcpy(fb_YUV->V + (uint32_t) VOODOO_YUV_STRIDE* j ,((uint8_t*) v) + uv_imax* j , uv_imax);
|
|
242 memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE* (j<<1) ,((uint8_t*) y) + y_imax * (j<<1) , y_imax);
|
|
243 memcpy(fb_YUV->Y + (uint32_t) VOODOO_YUV_STRIDE*((j<<1)+1),((uint8_t*) y) + y_imax *((j<<1)+1), y_imax);
|
|
244 }
|
|
245 LOG("video_out_3dfx: done planar dump\n");
|
|
246 }
|
|
247
|
|
248 static void
|
|
249 screen_to_screen_stretch_blt(uint32_t to, uint32_t from, uint32_t width, uint32_t height)
|
|
250 {
|
|
251 //FIXME - this function should be called by a show_frame function that
|
|
252 // uses a series of blts to show only those areas not covered
|
|
253 // by another window
|
|
254 voodoo_2d_reg saved_regs;
|
|
255
|
|
256 LOG("video_out_3dfx: saving registers\n");
|
|
257 // Save VGA regs (so X kinda works when we're done)
|
|
258 saved_regs = *reg_2d;
|
|
259
|
|
260 /* The following lines set up the screen to screen stretch blt from page2 to
|
|
261 page 1
|
|
262 */
|
|
263
|
|
264 LOG("video_out_3dfx: setting blt registers\n");
|
|
265 reg_2d->commandExtra = 4; //disable colorkeying, enable wait for v-refresh (0100b)
|
|
266 reg_2d->clip0Min = 0;
|
|
267 reg_2d->clip0Max = 0xFFFFFFFF; //no clipping
|
|
268
|
|
269 reg_2d->srcBaseAddr = from;
|
|
270 reg_2d->srcXY = 0;
|
|
271 reg_2d->srcFormat = screenwidth*2 | VOODOO_BLT_FORMAT_YUYV; // | 1<<21;
|
|
272 reg_2d->srcSize = vidwidth | (vidheight << 16);
|
|
273
|
|
274 reg_2d->dstBaseAddr = to;
|
|
275 reg_2d->dstXY = 0;
|
|
276 reg_2d->dstFormat = screenwidth*2 | VOODOO_BLT_FORMAT_16;
|
|
277
|
|
278 reg_2d->dstSize = width | (height << 16);
|
|
279
|
|
280 LOG("video_out_3dfx: starting blt\n");
|
|
281 // Executes screen to screen stretch blt
|
|
282 reg_2d->command = 2 | 1<<8 | 0xCC<<24;
|
|
283
|
|
284 LOG("video_out_3dfx: restoring regs\n");
|
|
285 restore_regs(&saved_regs);
|
|
286
|
|
287 LOG("video_out_3dfx: done blt\n");
|
|
288 }
|
|
289
|
|
290 static void
|
|
291 update_target(void)
|
|
292 {
|
|
293 uint32_t xp, yp, w, h, b, d;
|
|
294 Window root;
|
|
295
|
|
296 XGetGeometry(display,mywindow,&root,&xp,&yp,&w,&h,&b,&d);
|
|
297 XTranslateCoordinates(display,mywindow,root,0,0,&xp,&yp,&root);
|
|
298 dispx = (uint32_t) xp;
|
|
299 dispy = (uint32_t) yp;
|
|
300 dispwidth = (uint32_t) w;
|
|
301 dispheight = (uint32_t) h;
|
|
302
|
|
303 if (is_fullscreen)
|
|
304 targetoffset = vidpage0offset + (screenheight - dispheight)/2*screenwidth*screendepth + (screenwidth-dispwidth)/2*screendepth;
|
|
305 else
|
|
306 targetoffset = vidpage0offset + (dispy*screenwidth + dispx)*screendepth;
|
|
307 }
|
|
308
|
16171
|
309 static int
|
7124
|
310 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format)
|
1
|
311 {
|
|
312 char *name = ":0.0";
|
|
313 pioData data;
|
|
314 uint32_t retval;
|
|
315
|
17162
|
316 //TODO use x11_common for X and window handling
|
|
317
|
1
|
318 if(getenv("DISPLAY"))
|
|
319 name = getenv("DISPLAY");
|
|
320 display = XOpenDisplay(name);
|
|
321
|
|
322 screenwidth = XDisplayWidth(display,0);
|
|
323 screenheight = XDisplayHeight(display,0);
|
|
324
|
|
325 page_space = screenwidth*screenheight*screendepth;
|
|
326 vidpage0offset = 0;
|
|
327 vidpage1offset = page_space; // Use third and fourth pages
|
|
328 vidpage2offset = page_space*2;
|
|
329
|
|
330 signal(SIGALRM,sighup);
|
|
331 //alarm(120);
|
|
332
|
|
333 // Open driver device
|
17162
|
334 if ( fd == -1 )
|
1
|
335 {
|
17162
|
336 printf("Device not opened /dev/3dfx\n");
|
17160
|
337 return -1;
|
1
|
338 }
|
|
339
|
|
340 // Store sizes for later
|
|
341 vidwidth = width;
|
|
342 vidheight = height;
|
|
343
|
|
344 is_fullscreen = fullscreen = 0;
|
|
345 if (!is_fullscreen)
|
10994
|
346 create_window(display, title);
|
1
|
347
|
|
348 // Ask 3dfx driver for base memory address 0
|
|
349 data.port = 0x10; // PCI_BASE_ADDRESS_0_LINUX;
|
|
350 data.size = 4;
|
|
351 data.value = &baseAddr0;
|
|
352 data.device = 0;
|
|
353 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
|
|
354 {
|
|
355 printf("Error: %d\n",retval);
|
17162
|
356 return -1;
|
1
|
357 }
|
|
358
|
|
359 // Ask 3dfx driver for base memory address 1
|
|
360 data.port = 0x14; // PCI_BASE_ADDRESS_1_LINUX;
|
|
361 data.size = 4;
|
|
362 data.value = &baseAddr1;
|
|
363 data.device = 0;
|
|
364 if ((retval = ioctl(fd,_IOC(_IOC_READ,'3',3,0),&data)) < 0)
|
|
365 {
|
|
366 printf("Error: %d\n",retval);
|
17162
|
367 return -1;
|
1
|
368 }
|
|
369
|
|
370 // Map all 3dfx memory areas
|
|
371 memBase0 = mmap(0,0x1000000,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr0);
|
|
372 memBase1 = mmap(0,3*page_space,PROT_READ | PROT_WRITE,MAP_SHARED,fd,baseAddr1);
|
|
373 if (memBase0 == (uint32_t *) 0xFFFFFFFF || memBase1 == (uint32_t *) 0xFFFFFFFF)
|
|
374 {
|
|
375 printf("Couldn't map 3dfx memory areas: %p,%p,%d\n",
|
|
376 memBase0,memBase1,errno);
|
|
377 }
|
|
378
|
|
379 // Set up global pointers
|
|
380 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
|
|
381 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
|
|
382 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
|
|
383 fb_YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
|
|
384
|
|
385 vidpage0 = (void *)memBase1 + (unsigned long int)vidpage0offset;
|
|
386 vidpage1 = (void *)memBase1 + (unsigned long int)vidpage1offset;
|
|
387 vidpage2 = (void *)memBase1 + (unsigned long int)vidpage2offset;
|
|
388
|
|
389 // Clear pages 1,2,3
|
|
390 // leave page 0, that belongs to X.
|
|
391 // So does part of 1. Oops.
|
|
392 memset(vidpage1,0x00,page_space);
|
|
393 memset(vidpage2,0x00,page_space);
|
|
394
|
|
395 if (is_fullscreen)
|
|
396 memset(vidpage0,0x00,page_space);
|
|
397
|
|
398
|
|
399 #ifndef VOODOO_DEBUG
|
|
400 // Show page 0 (unblanked)
|
|
401 reg_IO->vidDesktopStartAddr = vidpage0offset;
|
|
402
|
|
403 /* Stop X from messing with my video registers!
|
|
404 Find a better way to do this?
|
|
405 Currently I use DGA to tell XF86 to not screw with registers, but I can't really use it
|
|
406 to do FB stuff because I need to know the absolute FB position and offset FB position
|
|
407 to feed to BLT command
|
|
408 */
|
|
409 //XF86DGADirectVideo(display,0,XF86DGADirectGraphics); //| XF86DGADirectMouse | XF86DGADirectKeyb);
|
|
410 #endif
|
|
411
|
|
412 atexit(restore);
|
|
413
|
|
414 printf("(display) 3dfx initialized %p\n",memBase1);
|
|
415 return 0;
|
|
416 }
|
|
417
|
16171
|
418 static int
|
1
|
419 draw_frame(uint8_t *src[])
|
|
420 {
|
|
421 LOG("video_out_3dfx: starting display_frame\n");
|
|
422
|
|
423 // Put packed data onto page 2
|
|
424 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],
|
|
425 vidpage2offset,vidwidth,vidheight);
|
|
426
|
|
427 LOG("video_out_3dfx: done display_frame\n");
|
|
428 return 0;
|
|
429 }
|
|
430
|
16171
|
431 static int
|
1978
|
432 //draw_slice(uint8_t *src[], uint32_t slice_num)
|
|
433 draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
|
1
|
434 {
|
|
435 uint32_t target;
|
|
436
|
1978
|
437 target = vidpage2offset + (screenwidth*2 * y);
|
|
438 dump_yuv_planar((uint32_t *)src[0],(uint32_t *)src[1],(uint32_t *)src[2],target,vidwidth,h);
|
1
|
439 return 0;
|
|
440 }
|
|
441
|
1501
|
442 static void draw_osd(void)
|
|
443 {
|
|
444 }
|
|
445
|
1
|
446 static void
|
|
447 flip_page(void)
|
|
448 {
|
|
449 //FIXME - update_target() should be called by event handler when window
|
|
450 // is resized or moved
|
|
451 update_target();
|
|
452 LOG("video_out_3dfx: calling blt function\n");
|
|
453 screen_to_screen_stretch_blt(targetoffset, vidpage2offset, dispwidth, dispheight);
|
|
454 }
|
|
455
|
16171
|
456 static int
|
1
|
457 query_format(uint32_t format)
|
|
458 {
|
6207
|
459 /* does this supports scaling? up & down? */
|
1
|
460 switch(format){
|
|
461 case IMGFMT_YV12:
|
|
462 // case IMGFMT_YUY2:
|
|
463 // case IMGFMT_RGB|24:
|
|
464 // case IMGFMT_BGR|24:
|
6207
|
465 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
|
1
|
466 }
|
|
467 return 0;
|
|
468 }
|
|
469
|
|
470 static void
|
|
471 uninit(void)
|
|
472 {
|
17162
|
473 if( fd != -1 )
|
|
474 close(fd);
|
1
|
475 }
|
|
476
|
|
477
|
31
|
478 static void check_events(void)
|
|
479 {
|
|
480 }
|
|
481
|
16171
|
482 static int preinit(const char *arg)
|
4352
|
483 {
|
17162
|
484 if ( (fd = open("/dev/3dfx",O_RDWR) ) == -1)
|
|
485 {
|
|
486 printf("Couldn't open /dev/3dfx\n");
|
|
487 return -1;
|
|
488 }
|
|
489
|
4737
|
490 if(arg)
|
|
491 {
|
|
492 printf("vo_3dfx: Unknown subdevice: %s\n",arg);
|
|
493 return ENOSYS;
|
|
494 }
|
|
495 return 0;
|
4352
|
496 }
|
31
|
497
|
16171
|
498 static int control(uint32_t request, void *data, ...)
|
4352
|
499 {
|
4592
|
500 switch (request) {
|
|
501 case VOCTRL_QUERY_FORMAT:
|
|
502 return query_format(*((uint32_t*)data));
|
|
503 }
|
|
504 return VO_NOTIMPL;
|
4352
|
505 }
|