10l initial patch by Oded Shimon <ods15 at ods15.dyndns.org>
[mplayer/greg.git] / libvo / vo_tdfxfb.c
blobb67827d9db55efb11a179061fd4d10d7ed76cec4
1 /* Copyright (C) Mark Zealey, 2002, <mark@zealos.org>. Released under the terms
2 * and conditions of the GPL.
4 * 30/03/02: An almost total rewrite, added DR support and support for modes
5 * other than 16bpp. Fixed the crash when playing multiple files
6 * 07/04/02: Fixed DR support, added YUY2 support, fixed OSD stuff.
7 * 08/04/02: Fixed a wierd sound corruption problem caused by some optomizations
8 * I made.
9 * 09/04/02: Fixed a problem with changing the variables passed to draw_slice().
10 * Fixed DR support for YV12 et al. Added BGR support. Removed lots of dud code.
11 * 10/04/02: Changed the memcpy functions to mem2agpcpy.. should be a tad
12 * faster.
13 * 11/04/02: Added a compile option so you can watch the film with the console
14 * as the background, or not.
15 * 13/04/02: Fix rough OSD stuff by rendering it straight onto the output
16 * buffer. Added double-buffering. Supports hardware zoom/reduce zoom modes.
17 * 13/04/02: Misc cleanups of the code.
18 * 22/10/02: Added geometry support to it
20 * Hints and tricks:
21 * - Use -dr to get direct rendering
22 * - Use -vf yuy2 to get yuy2 rendering, *MUCH* faster than yv12
23 * - To get a black background and nice smooth OSD, use -double
24 * - To get the console as a background, but with scaled OSD, use -nodouble
25 * - The driver supports both scaling and shrinking the image using the -x and
26 * -y options on the mplayer commandline. Also repositioning via the -geometry
27 * option.
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <sys/mman.h>
38 #include <linux/fb.h>
40 #include "config.h"
41 #include "fastmemcpy.h"
42 #include "video_out.h"
43 #include "video_out_internal.h"
44 #include "drivers/3dfx.h"
45 #include "aspect.h"
46 #include "sub.h"
48 static vo_info_t info =
50 "3Dfx Banshee/Voodoo3/Voodoo5",
51 "tdfxfb",
52 "Mark Zealey <mark@zealos.org>",
56 LIBVO_EXTERN(tdfxfb)
58 /* Some registers on the card */
59 #define S2S_STRECH_BLT 2 // BLT + Strech
60 #define S2S_IMMED (1 << 8) // Do it immediatly
61 #define S2S_ROP (0xCC << 24) // ???
63 /* Stepping between the different YUV plane registers */
64 #define YUV_STRIDE 1024
65 struct YUV_plane {
66 char Y[0x0100000];
67 char U[0x0100000];
68 char V[0x0100000];
71 static int fd = -1;
72 static struct fb_fix_screeninfo fb_finfo;
73 static struct fb_var_screeninfo fb_vinfo;
74 static uint32_t in_width, in_height, in_format, in_depth, in_voodoo_format,
75 screenwidth, screenheight, screendepth, vidwidth, vidheight, vidx, vidy,
76 vid_voodoo_format, *vidpage, *hidpage, *inpage, vidpageoffset,
77 hidpageoffset, inpageoffset, *memBase0 = NULL, *memBase1 = NULL, r_width, r_height;
78 static volatile voodoo_io_reg *reg_IO;
79 static volatile voodoo_2d_reg *reg_2d;
80 static voodoo_yuv_reg *reg_YUV;
81 static struct YUV_plane *YUV;
82 static void (*alpha_func)(), (*alpha_func_double)();
84 static uint32_t preinit(const char *arg)
86 char *name;
88 if(arg)
89 name = (char*)arg;
90 else if(!(name = getenv("FRAMEBUFFER")))
91 name = "/dev/fb0";
93 if((fd = open(name, O_RDWR)) == -1) {
94 printf("tdfxfb: can't open %s: %s\n", name, strerror(errno));
95 return -1;
98 if(ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
99 printf("tdfxfb: problem with FBITGET_FSCREENINFO ioctl: %s\n",
100 strerror(errno));
101 close(fd);
102 fd = -1;
103 return -1;
106 if(ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
107 printf("tdfxfb: problem with FBITGET_VSCREENINFO ioctl: %s\n",
108 strerror(errno));
109 close(fd);
110 fd = -1;
111 return -1;
114 /* BANSHEE means any of the series aparently */
115 if (fb_finfo.accel != FB_ACCEL_3DFX_BANSHEE) {
116 printf("tdfxfb: This driver is only supports the 3Dfx Banshee,"
117 " Voodoo3 and Voodoo 5\n");
118 close(fd);
119 fd = -1;
120 return -1;
123 // Check the depth now as config() musn't fail
124 switch(fb_vinfo.bits_per_pixel) {
125 case 16:
126 case 24:
127 case 32:
128 break; // Ok
129 default:
130 printf("tdfxfb: %d bpp output is not supported\n", fb_vinfo.bits_per_pixel);
131 close(fd);
132 fd = -1;
133 return -1;
136 /* Open up a window to the hardware */
137 memBase1 = mmap(0, fb_finfo.smem_len, PROT_READ | PROT_WRITE,
138 MAP_SHARED, fd, 0);
139 memBase0 = mmap(0, fb_finfo.mmio_len, PROT_READ | PROT_WRITE,
140 MAP_SHARED, fd, fb_finfo.smem_len);
142 if((long)memBase0 == -1 || (long)memBase1 == -1) {
143 printf("tdfxfb: Couldn't map memory areas: %s\n", strerror(errno));
144 if((long)memBase0 != -1)
145 munmap(memBase0, fb_finfo.smem_len);
146 if((long)memBase1 != -1)
147 munmap(memBase1, fb_finfo.smem_len);
148 memBase0 = memBase1 = NULL;
149 return -1;
152 /* Set up global pointers to the voodoo's regs */
153 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
154 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
155 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
156 YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
158 return 0;
161 static void uninit(void)
163 if(reg_IO) {
164 /* Restore the screen (Linux lives at 0) */
165 reg_IO->vidDesktopStartAddr = 0;
166 reg_IO = NULL;
169 /* And close our mess */
170 if(memBase1) {
171 munmap(memBase1, fb_finfo.smem_len);
172 memBase1 = NULL;
175 if(memBase0) {
176 munmap(memBase0, fb_finfo.mmio_len);
177 memBase0 = NULL;
180 if(fd != -1) {
181 close(fd);
182 fd = -1;
186 static void clear_screen()
188 /* There needs to be some sort of delay here or else things seriously
189 * screw up. Causes the image to not be the right size on screen if
190 * this isn't like this. A printf before the memset call also seems to
191 * work, but this made more sense since it actually checks the status of
192 * the card.
194 if(vo_doublebuffering) {
195 /* first wait for the card to be ready, do not try to write
196 * every time - alex */
197 do {} while((reg_IO->status & 0x1f) < 1);
198 memset(vidpage, 0, screenwidth * screenheight * screendepth);
199 memset(hidpage, 0, screenwidth * screenheight * screendepth);
203 /* Setup output screen dimensions etc */
204 static void setup_screen(uint32_t full)
206 aspect(&vidwidth, &vidheight, full ? A_ZOOM : A_NOZOOM);
208 geometry(&vidx, &vidy, &vidwidth, &vidheight, screenwidth, screenheight);
209 vo_fs = full;
210 clear_screen();
213 static uint32_t config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
214 uint32_t flags, char *title, uint32_t format)
216 screenwidth = fb_vinfo.xres;
217 screenheight = fb_vinfo.yres;
218 aspect_save_screenres(fb_vinfo.xres,fb_vinfo.yres);
220 in_width = width;
221 in_height = height;
222 in_format = format;
223 aspect_save_orig(width,height);
225 r_width = d_width;
226 r_height = d_height;
227 aspect_save_prescale(d_width,d_height);
229 /* Setup the screen for rendering to */
230 switch(fb_vinfo.bits_per_pixel) {
231 case 16:
232 screendepth = 2;
233 vid_voodoo_format = VOODOO_BLT_FORMAT_16;
234 alpha_func_double = vo_draw_alpha_rgb16;
235 break;
237 case 24:
238 screendepth = 3;
239 vid_voodoo_format = VOODOO_BLT_FORMAT_24;
240 alpha_func_double = vo_draw_alpha_rgb24;
241 break;
243 case 32:
244 screendepth = 4;
245 vid_voodoo_format = VOODOO_BLT_FORMAT_32;
246 alpha_func_double = vo_draw_alpha_rgb32;
247 break;
249 default:
250 printf("tdfxfb: %d bpp output is not supported (This should never happend)\n", fb_vinfo.bits_per_pixel);
251 return -1;
254 vid_voodoo_format |= screenwidth * screendepth;
256 /* Some defaults here */
257 in_voodoo_format = VOODOO_BLT_FORMAT_YUYV;
258 in_depth = 2;
259 alpha_func = vo_draw_alpha_yuy2;
261 switch(in_format) {
262 case IMGFMT_YV12:
263 case IMGFMT_I420:
264 case IMGFMT_IYUV:
265 case IMGFMT_YUY2:
266 break;
267 case IMGFMT_UYVY:
268 in_voodoo_format = VOODOO_BLT_FORMAT_UYVY;
269 break;
270 case IMGFMT_BGR16:
271 in_voodoo_format = VOODOO_BLT_FORMAT_16;
272 alpha_func = vo_draw_alpha_rgb16;
273 break;
275 case IMGFMT_BGR24:
276 in_depth = 3;
277 in_voodoo_format = VOODOO_BLT_FORMAT_24;
278 alpha_func = vo_draw_alpha_rgb24;
279 break;
281 case IMGFMT_BGR32:
282 in_depth = 4;
283 in_voodoo_format = VOODOO_BLT_FORMAT_32;
284 alpha_func = vo_draw_alpha_rgb32;
285 break;
287 default:
288 printf("tdfxfb: Eik! Something's wrong with control().\n");
289 return -1;
292 in_voodoo_format |= in_width * in_depth;
294 /* Linux lives in the first frame */
295 if(vo_doublebuffering) {
296 vidpageoffset = screenwidth * screenheight * screendepth;
297 hidpageoffset = vidpageoffset + screenwidth * screenheight * screendepth;
298 } else
299 vidpageoffset = hidpageoffset = 0; /* Console background */
302 inpageoffset = hidpageoffset + screenwidth * screenheight * screendepth;
304 if(inpageoffset + in_width * in_depth * in_height > fb_finfo.smem_len) {
305 printf("tdfxfb: Not enough video memory to play this movie. Try at a lower resolution\n");
306 return -1;
309 vidpage = (void *)memBase1 + (unsigned long)vidpageoffset;
310 hidpage = (void *)memBase1 + (unsigned long)hidpageoffset;
311 inpage = (void *)memBase1 + (unsigned long)inpageoffset;
313 setup_screen(flags & VOFLAG_FULLSCREEN);
315 memset(inpage, 0, in_width * in_height * in_depth);
317 printf("tdfxfb: screen is %dx%d at %d bpp, in is %dx%d at %d bpp, norm is %dx%d\n",
318 screenwidth, screenheight, screendepth * 8,
319 in_width, in_height, in_depth * 8,
320 d_width, d_height);
322 return 0;
325 /* Double-buffering draw_alpha */
326 static void draw_alpha_double(int x, int y, int w, int h, unsigned char *src,
327 unsigned char *srca, int stride)
329 char *dst = (char *)vidpage + ((y + vidy) * screenwidth + x + vidx) * screendepth;
330 alpha_func_double(w, h, src, srca, stride, dst, screenwidth * screendepth);
333 /* Single-buffering draw_alpha */
334 static void draw_alpha(int x, int y, int w, int h, unsigned char *src,
335 unsigned char *srca, int stride)
337 char *dst = (char *)inpage + (y * in_width + x) * in_depth;
338 alpha_func(w, h, src, srca, stride, dst, in_width * in_depth);
341 static void draw_osd(void)
343 if(!vo_doublebuffering)
344 vo_draw_text(in_width, in_height, draw_alpha);
347 /* Render onto the screen */
348 static void flip_page(void)
350 voodoo_2d_reg regs = *reg_2d; /* Copy the regs */
351 int i = 0;
353 if(vo_doublebuffering) {
354 /* Flip to an offscreen buffer for rendering */
355 uint32_t t = vidpageoffset;
356 void *j = vidpage;
358 vidpage = hidpage;
359 hidpage = j;
360 vidpageoffset = hidpageoffset;
361 hidpageoffset = t;
364 reg_2d->commandExtra = 0;
365 reg_2d->clip0Min = 0;
366 reg_2d->clip0Max = 0xffffffff;
368 reg_2d->srcBaseAddr = inpageoffset;
369 reg_2d->srcXY = 0;
370 reg_2d->srcFormat = in_voodoo_format;
371 reg_2d->srcSize = XYREG(in_width, in_height);
373 reg_2d->dstBaseAddr = vidpageoffset;
374 reg_2d->dstXY = XYREG(vidx, vidy);
375 reg_2d->dstFormat = vid_voodoo_format;
376 reg_2d->dstSize = XYREG(vidwidth, vidheight);
377 reg_2d->command = S2S_STRECH_BLT | S2S_IMMED | S2S_ROP;
379 /* Wait for the command to finish (If we don't do this, we get wierd
380 * sound corruption... */
381 while((reg_IO->status & 0x1f) < 1)
382 /* Wait */;
384 *((volatile uint32_t *)((uint32_t *)reg_IO + COMMAND_3D)) = COMMAND_3D_NOP;
386 while(i < 3)
387 if(!(reg_IO->status & STATUS_BUSY))
388 i++;
390 /* Restore the old regs now */
391 reg_2d->commandExtra = regs.commandExtra;
392 reg_2d->clip0Min = regs.clip0Min;
393 reg_2d->clip0Max = regs.clip0Max;
395 reg_2d->srcBaseAddr = regs.srcBaseAddr;
396 reg_2d->srcXY = regs.srcXY;
397 reg_2d->srcFormat = regs.srcFormat;
398 reg_2d->srcSize = regs.srcSize;
400 reg_2d->dstBaseAddr = regs.dstBaseAddr;
401 reg_2d->dstXY = regs.dstXY;
402 reg_2d->dstFormat = regs.dstFormat;
403 reg_2d->dstSize = regs.dstSize;
405 reg_2d->command = 0;
407 /* Render any text onto this buffer */
408 if(vo_doublebuffering)
409 vo_draw_text(vidwidth, vidheight, draw_alpha_double);
411 /* And flip to the new buffer! */
412 reg_IO->vidDesktopStartAddr = vidpageoffset;
415 static uint32_t draw_frame(uint8_t *src[])
417 mem2agpcpy(inpage, src[0], in_width * in_depth * in_height);
418 return 0;
421 static uint32_t draw_slice(uint8_t *i[], int s[], int w, int h, int x, int y)
423 /* We want to render to the YUV to the input page + the location
424 * of the stripes we're doing */
425 reg_YUV->yuvBaseAddr = inpageoffset + in_width * in_depth * y + x;
426 reg_YUV->yuvStride = in_width * in_depth;
428 /* Put the YUV channels into the voodoos internal combiner unit
429 * thingie */
430 mem2agpcpy_pic(YUV->Y, i[0], s[0], h , YUV_STRIDE, s[0]);
431 mem2agpcpy_pic(YUV->U, i[1], s[1], h / 2, YUV_STRIDE, s[1]);
432 mem2agpcpy_pic(YUV->V, i[2], s[2], h / 2, YUV_STRIDE, s[2]);
433 return 0;
436 /* Attempt to start doing DR */
437 static uint32_t get_image(mp_image_t *mpi)
440 if(mpi->flags & MP_IMGFLAG_READABLE)
441 return VO_FALSE;
442 if(mpi->type == MP_IMGTYPE_STATIC && vo_doublebuffering)
443 return VO_FALSE;
444 if(mpi->type > MP_IMGTYPE_TEMP)
445 return VO_FALSE; // TODO ??
447 switch(in_format) {
448 case IMGFMT_YUY2:
449 case IMGFMT_BGR16:
450 case IMGFMT_BGR24:
451 case IMGFMT_BGR32:
452 case IMGFMT_UYVY:
453 mpi->planes[0] = (char *)inpage;
454 mpi->stride[0] = in_width * in_depth;
455 break;
457 case IMGFMT_YV12:
458 case IMGFMT_I420:
459 case IMGFMT_IYUV:
460 if(!(mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) && mpi->w != YUV_STRIDE)
461 return VO_FALSE;
462 mpi->planes[0] = YUV->Y;
463 mpi->planes[1] = YUV->U;
464 mpi->planes[2] = YUV->V;
465 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = YUV_STRIDE;
466 reg_YUV->yuvBaseAddr = inpageoffset;
467 reg_YUV->yuvStride = in_width * in_depth;
468 break;
470 default:
471 return VO_FALSE;
474 mpi->width = in_width;
475 mpi->flags |= MP_IMGFLAG_DIRECT;
477 return VO_TRUE;
480 static uint32_t control(uint32_t request, void *data, ...)
482 switch(request) {
483 case VOCTRL_GET_IMAGE:
484 return get_image(data);
486 case VOCTRL_QUERY_FORMAT:
487 switch(*((uint32_t*)data)) {
488 case IMGFMT_YV12:
489 case IMGFMT_I420:
490 case IMGFMT_IYUV:
491 case IMGFMT_YUY2:
492 case IMGFMT_UYVY:
493 case IMGFMT_BGR16:
494 case IMGFMT_BGR24:
495 case IMGFMT_BGR32:
496 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
497 VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
500 return 0; /* Not supported */
502 case VOCTRL_FULLSCREEN:
503 setup_screen(!vo_fs);
504 return 0;
507 return VO_NOTIMPL;
510 /* Dummy funcs */
511 static void check_events(void) {}