stream/dvbin.h: Use angular brackets for system #includes.
[mplayer/glamo.git] / libvo / vo_dga.c
blob879831761d184b6f18e04df5dc16644680d88e2d
1 /*
2 * X11 DGA Interface
4 * Copyright (C) 2001 Andreas Ackermann <acki@acki-netz.de>
5 * Sourceforge username: acki2
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>
27 #include <errno.h>
29 #include "config.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
32 #include "aspect.h"
33 #include "x11_common.h"
34 #include "fastmemcpy.h"
35 #include "mp_msg.h"
37 #include <X11/Xlib.h>
38 #include <X11/extensions/xf86dga.h>
40 #ifdef CONFIG_XF86VM
41 #include <X11/extensions/xf86vmode.h>
42 #endif
44 static const vo_info_t info = {
45 #ifdef CONFIG_DGA2
46 "DGA ( Direct Graphic Access V2.0 )",
47 #else
48 #ifdef CONFIG_XF86VM
49 "DGA ( Direct Graphic Access V1.0+XF86VidModeExt. )",
50 #else
51 "DGA ( Direct Graphic Access V1.0 )",
52 #endif
53 #endif
54 "dga",
55 "Andreas Ackermann <acki@acki-netz.de>",
59 const LIBVO_EXTERN(dga)
60 //------------------------------------------------------------------
61 //#define BITSPP (vo_dga_modes[vo_dga_active_mode].vdm_bitspp)
62 //#define BYTESPP (vo_dga_modes[vo_dga_active_mode].vdm_bytespp)
63 #define VO_DGA_INVALID_RES 100000
64 #define HW_MODE (vo_dga_modes[vo_dga_hw_mode])
65 #define SRC_MODE (vo_dga_modes[vo_dga_src_mode])
66 struct vd_modes
68 int vdm_mplayer_depth;
69 int vdm_supported;
70 int vdm_depth;
71 int vdm_bitspp;
72 int vdm_bytespp;
73 int vdm_rmask;
74 int vdm_gmask;
75 int vdm_bmask;
76 int vdm_hw_mode;
79 //------------------------------------------------------------------
81 static struct vd_modes vo_dga_modes[] = {
82 // these entries describe HW modes
83 // however, we use the same entries to tell mplayer what we support
84 // so the last two values describe, which HW mode to use and which conversion
85 // function to use for a mode that is not supported by HW
87 {0, 0, 0, 0, 0, 0, 0, 0, 0,},
88 {15, 0, 15, 16, 2, 0x7c00, 0x03e0, 0x001f, 2,},
89 {16, 0, 16, 16, 2, 0xf800, 0x07e0, 0x001f, 2,},
90 {24, 0, 24, 24, 3, 0xff0000, 0x00ff00, 0x0000ff, 4},
91 {32, 0, 24, 32, 4, 0x00ff0000, 0x0000ff00, 0x000000ff, 4}
94 static int vo_dga_mode_num =
95 sizeof(vo_dga_modes) / sizeof(struct vd_modes);
97 // enable a HW mode (by description)
98 static int vd_EnableMode(int depth, int bitspp,
99 int rmask, int gmask, int bmask)
101 int i;
103 for (i = 1; i < vo_dga_mode_num; i++)
105 if (vo_dga_modes[i].vdm_depth == depth &&
106 vo_dga_modes[i].vdm_bitspp == bitspp &&
107 vo_dga_modes[i].vdm_rmask == rmask &&
108 vo_dga_modes[i].vdm_gmask == gmask &&
109 vo_dga_modes[i].vdm_bmask == bmask)
111 vo_dga_modes[i].vdm_supported = 1;
112 vo_dga_modes[i].vdm_hw_mode = i;
113 return i;
116 return 0;
119 static int vd_ModeEqual(int depth, int bitspp,
120 int rmask, int gmask, int bmask, int index)
122 return (vo_dga_modes[index].vdm_depth == depth &&
123 vo_dga_modes[index].vdm_bitspp == bitspp &&
124 vo_dga_modes[index].vdm_rmask == rmask &&
125 vo_dga_modes[index].vdm_gmask == gmask &&
126 vo_dga_modes[index].vdm_bmask == bmask) ? 1 : 0;
130 // enable a HW mode (mplayer_depth decides which)
131 static int vd_ValidateMode(int mplayer_depth)
133 int i;
135 if (mplayer_depth == 0)
136 return 0;
137 for (i = 1; i < vo_dga_mode_num; i++)
139 if (vo_dga_modes[i].vdm_mplayer_depth == mplayer_depth)
141 vo_dga_modes[i].vdm_supported = 1;
142 vo_dga_modes[i].vdm_hw_mode = i;
143 return i;
146 return 0;
149 // do we support this mode? (not important whether native or conversion)
150 static int vd_ModeValid(int mplayer_depth)
152 int i;
154 if (mplayer_depth == 0)
155 return 0;
156 for (i = 1; i < vo_dga_mode_num; i++)
158 if (vo_dga_modes[i].vdm_mplayer_depth == mplayer_depth &&
159 vo_dga_modes[i].vdm_supported != 0)
161 return i;
164 return 0;
167 static char *vd_GetModeString(int index)
170 #define VO_DGA_MAX_STRING_LEN 100
171 static char stringbuf[VO_DGA_MAX_STRING_LEN];
173 stringbuf[VO_DGA_MAX_STRING_LEN - 1] = 0;
174 snprintf(stringbuf, VO_DGA_MAX_STRING_LEN - 2,
175 "depth=%d, bpp=%d, r=%06x, g=%06x, b=%06x, %s (-bpp %d)",
176 vo_dga_modes[index].vdm_depth,
177 vo_dga_modes[index].vdm_bitspp,
178 vo_dga_modes[index].vdm_rmask,
179 vo_dga_modes[index].vdm_gmask,
180 vo_dga_modes[index].vdm_bmask,
181 vo_dga_modes[index].
182 vdm_supported ? "native" : "not supported",
183 vo_dga_modes[index].vdm_mplayer_depth);
184 return stringbuf;
187 //-----------------------------------------------------------------
189 #if defined(CONFIG_XF86VM) && !defined(CONFIG_DGA2)
190 static XF86VidModeModeInfo **vo_dga_vidmodes = NULL;
191 #endif
194 static int vo_dga_src_format;
195 static int vo_dga_width; // bytes per line in framebuffer
196 static int vo_dga_vp_width; // visible pixels per line in
198 // framebuffer
199 static int vo_dga_vp_height; // visible lines in framebuffer
200 static int vo_dga_is_running = 0;
201 static int vo_dga_src_width; // width of video in pixels
202 static int vo_dga_src_height; // height of video in pixels
203 static int vo_dga_src_offset = 0; // offset in src
204 static int vo_dga_vp_offset = 0; // offset in dest
205 static int vo_dga_bytes_per_line; // bytes per line to copy
206 static int vo_dga_vp_skip; // dto. for dest
207 static int vo_dga_lines; // num of lines to copy
208 static int vo_dga_hw_mode = 0; // index in mode list that is actually
210 // used by framebuffer
211 static int vo_dga_src_mode = 0; // index in mode list that is used by
213 // codec
214 static int vo_dga_XServer_mode = 0; // index in mode list for resolution
216 #ifdef CONFIG_DGA2
217 static XDGAMode *vo_modelines;
218 static int vo_modecount;
219 #endif
221 #define MAX_NR_VIDEO_BUFFERS 3
223 #define CURRENT_VIDEO_BUFFER \
224 (vo_dga_video_buffer[vo_dga_current_video_buffer])
226 static int vo_dga_nr_video_buffers; // Total number of frame buffers.
227 static int vo_dga_current_video_buffer; // Buffer available for rendering.
229 static struct video_buffer
231 int y;
232 uint8_t *data;
233 } vo_dga_video_buffer[MAX_NR_VIDEO_BUFFERS];
235 /* saved src and dst dimensions for SwScaler */
236 static unsigned int scale_srcW = 0,
237 scale_dstW = 0, scale_srcH = 0, scale_dstH = 0;
239 //---------------------------------------------------------
241 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
242 unsigned char *srca, int stride)
245 char *d;
246 unsigned int offset;
247 int buffer_stride;
249 offset = vo_dga_width * y0 + x0;
250 buffer_stride = vo_dga_width;
251 d = CURRENT_VIDEO_BUFFER.data + vo_dga_vp_offset;
253 switch (HW_MODE.vdm_mplayer_depth)
256 case 32:
257 vo_draw_alpha_rgb32(w, h, src, srca, stride, d + 4 * offset,
258 4 * buffer_stride);
259 break;
260 case 24:
261 vo_draw_alpha_rgb24(w, h, src, srca, stride, d + 3 * offset,
262 3 * buffer_stride);
263 break;
264 case 15:
265 vo_draw_alpha_rgb15(w, h, src, srca, stride, d + 2 * offset,
266 2 * buffer_stride);
267 break;
268 case 16:
269 vo_draw_alpha_rgb16(w, h, src, srca, stride, d + 2 * offset,
270 2 * buffer_stride);
271 break;
276 //---------------------------------------------------------
281 // quick & dirty - for debugging only
282 #if 0
283 static void fillblock(char *strt, int yoff, int lines, int val)
285 char *i;
287 for (i = strt + yoff * vo_dga_width * HW_MODE.vdm_bytespp;
288 i < strt + (lines + yoff) * vo_dga_width * HW_MODE.vdm_bytespp;)
290 *i++ = val;
293 #endif
295 //---------------------------------------------------------
297 static int draw_frame(uint8_t * src[])
300 int numlines = vo_dga_lines;
302 char *s, *d;
304 s = *src;
305 d = CURRENT_VIDEO_BUFFER.data + vo_dga_vp_offset;
307 mem2agpcpy_pic(d, s,
308 vo_dga_bytes_per_line,
309 numlines,
310 vo_dga_bytes_per_line + vo_dga_vp_skip,
311 vo_dga_bytes_per_line);
313 // DBG-COde
315 #if 0
316 d = CURRENT_VIDEO_BUFFER.data + vo_dga_vp_offset;
317 fillblock(d, 0, 10, 0x800000ff);
318 fillblock(d, 10, 10, 0x8000ff00);
319 fillblock(d, 20, 10, 0x80ff0000);
320 fillblock(d, 30, 10, 0xff0000ff);
321 fillblock(d, 40, 10, 0x800000ff);
322 fillblock(d, 50, 10, 0x0f0000ff);
323 #endif
324 return 0;
327 //---------------------------------------------------------
329 static void check_events(void)
331 vo_x11_check_events(mDisplay);
334 //---------------------------------------------------------
336 #include "sub.h"
338 static void draw_osd(void)
340 vo_draw_text(vo_dga_src_width, vo_dga_src_height, draw_alpha);
343 static void switch_video_buffers(void)
345 vo_dga_current_video_buffer =
346 (vo_dga_current_video_buffer + 1) % vo_dga_nr_video_buffers;
349 static void flip_page(void)
351 if (1 < vo_dga_nr_video_buffers)
353 #ifdef CONFIG_DGA2
354 XDGASetViewport(mDisplay, mScreen,
355 0, CURRENT_VIDEO_BUFFER.y, XDGAFlipRetrace);
356 #else
357 XF86DGASetViewPort(mDisplay, mScreen, 0, CURRENT_VIDEO_BUFFER.y);
358 #endif
359 switch_video_buffers();
363 //---------------------------------------------------------
365 static int draw_slice(uint8_t * src[], int stride[],
366 int w, int h, int x, int y)
368 return 0;
371 //---------------------------------------------------------
373 static int query_format(uint32_t format)
376 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR &&
377 vd_ModeValid(format & 0xff))
379 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD;
382 return 0;
385 //---------------------------------------------------------
387 static void uninit(void)
390 #ifdef CONFIG_DGA2
391 XDGADevice *dgadevice;
392 #endif
394 if (!vo_config_count)
395 return;
397 if (vo_dga_is_running)
399 vo_dga_is_running = 0;
400 mp_msg(MSGT_VO, MSGL_V, "vo_dga: in uninit\n");
401 if (vo_grabpointer)
402 XUngrabPointer(mDisplay, CurrentTime);
403 XUngrabKeyboard(mDisplay, CurrentTime);
404 #ifdef CONFIG_DGA2
405 XDGACloseFramebuffer(mDisplay, mScreen);
406 dgadevice = XDGASetMode(mDisplay, mScreen, 0);
407 if (dgadevice != NULL)
409 XFree(dgadevice);
411 #else
412 XF86DGADirectVideo(mDisplay, mScreen, 0);
413 // first disable DirectVideo and then switch mode back!
414 #ifdef CONFIG_XF86VM
415 if (vo_dga_vidmodes != NULL)
417 int screen;
419 screen = XDefaultScreen(mDisplay);
420 mp_msg(MSGT_VO, MSGL_V,
421 "vo_dga: VidModeExt: Switching back..\n");
422 // seems some graphics adaptors need this more than once ...
423 XF86VidModeSwitchToMode(mDisplay, screen, vo_dga_vidmodes[0]);
424 XF86VidModeSwitchToMode(mDisplay, screen, vo_dga_vidmodes[0]);
425 XF86VidModeSwitchToMode(mDisplay, screen, vo_dga_vidmodes[0]);
426 XF86VidModeSwitchToMode(mDisplay, screen, vo_dga_vidmodes[0]);
427 XFree(vo_dga_vidmodes);
429 #endif
430 #endif
432 vo_x11_uninit();
436 //----------------------------------------------------------
437 // TODO: check for larger maxy value
438 // (useful for double buffering!!!)
440 static int check_res(int num, int x, int y, int bpp,
441 int new_x, int new_y, int new_vbi, int new_maxy,
442 int *old_x, int *old_y, int *old_vbi, int *old_maxy)
445 mp_msg(MSGT_VO, MSGL_V,
446 "vo_dga: (%3d) Trying %4d x %4d @ %3d Hz @ depth %2d ..", num,
447 new_x, new_y, new_vbi, bpp);
448 mp_msg(MSGT_VO, MSGL_V, "(old: %dx%d@%d).", *old_x, *old_y, *old_vbi);
449 if ((new_x >= x) && (new_y >= y) && (
450 // prefer a better resolution either in X or in Y
451 // as long as the other dimension is at least the same
453 // hmm ... MAYBE it would be more clever to focus on the
454 // x-resolution; I had 712x400 and 640x480 and the movie
455 // was 640x360; 640x480 would be the 'right thing' here
456 // but since 712x400 was queried first I got this one.
457 // I think there should be a cmd-line switch to let the
458 // user choose the mode he likes ... (acki2)
459 (((new_x < *old_x) &&
460 !(new_y > *old_y)) ||
461 ((new_y < *old_y) &&
462 !(new_x > *old_x)))
463 // but if we get an identical resolution choose
464 // the one with the lower refreshrate (saves bandwidth !!!)
465 // as long as it's above 50 Hz (acki2 on 30/3/2001)
467 (((new_x == *old_x) &&
468 (new_y == *old_y) &&
469 ((new_vbi >= *old_vbi
470 && *old_vbi < 50)
471 || (*old_vbi >= 50
472 && new_vbi < *old_vbi
473 && new_vbi >= 50))) ||
474 // if everything is equal, then use the mode with the lower
475 // stride
476 ((new_x == *old_x) &&
477 (new_y == *old_y) &&
478 (new_vbi == *old_vbi) &&
479 (new_maxy > *old_maxy)))))
481 *old_x = new_x;
482 *old_y = new_y;
483 *old_maxy = new_maxy;
484 *old_vbi = new_vbi;
485 mp_msg(MSGT_VO, MSGL_V, ".ok!!\n");
486 return 1;
487 } else
489 mp_msg(MSGT_VO, MSGL_V, ".no\n");
490 return 0;
496 //---------------------------------------------------------
498 static void init_video_buffers(uint8_t * buffer_base,
499 int view_port_height,
500 int bytes_per_scanline,
501 int max_view_port_y,
502 int use_multiple_buffers)
504 int bytes_per_buffer = view_port_height * bytes_per_scanline;
505 int i;
507 if (use_multiple_buffers)
508 vo_dga_nr_video_buffers = max_view_port_y / view_port_height;
509 else
510 vo_dga_nr_video_buffers = 1;
512 if (vo_dga_nr_video_buffers > MAX_NR_VIDEO_BUFFERS)
513 vo_dga_nr_video_buffers = MAX_NR_VIDEO_BUFFERS;
514 if (vo_dga_nr_video_buffers <= 0)
515 vo_dga_nr_video_buffers = 1;
517 vo_dga_current_video_buffer = 0;
519 for (i = 0; i < vo_dga_nr_video_buffers; i++)
521 vo_dga_video_buffer[i].y = i * view_port_height;
522 vo_dga_video_buffer[i].data = buffer_base + i * bytes_per_buffer;
524 // Clear video buffer.
525 memset(vo_dga_video_buffer[i].data, 0, bytes_per_buffer);
529 static int config(uint32_t width, uint32_t height,
530 uint32_t d_width, uint32_t d_height,
531 uint32_t flags, char *title, uint32_t format)
534 int x_off, y_off;
535 int wanted_width, wanted_height;
537 static unsigned char *vo_dga_base;
538 static int prev_width, prev_height;
540 #ifdef CONFIG_DGA2
541 // needed to change DGA video mode
542 int mX = VO_DGA_INVALID_RES, mY = VO_DGA_INVALID_RES, mVBI =
543 100000, mMaxY = 0, i, j = 0;
544 int dga_modenum;
545 XDGAMode *modeline;
546 XDGADevice *dgadevice;
547 #else
548 #ifdef CONFIG_XF86VM
549 unsigned int vm_event, vm_error;
550 unsigned int vm_ver, vm_rev;
551 int i, j = 0, have_vm = 0;
552 int mX = VO_DGA_INVALID_RES, mY = VO_DGA_INVALID_RES, mVBI =
553 100000, mMaxY = 0, dga_modenum;
554 #endif
555 int bank, ram;
556 #endif
558 vo_dga_src_format = format;
560 wanted_width = d_width;
561 wanted_height = d_height;
563 if (!wanted_height)
564 wanted_height = height;
565 if (!wanted_width)
566 wanted_width = width;
568 if (!vo_dbpp)
570 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR)
572 vo_dga_src_mode = vd_ModeValid(format & 0xff);
574 } else
576 vo_dga_src_mode = vd_ModeValid(vo_dbpp);
578 vo_dga_hw_mode = SRC_MODE.vdm_hw_mode;
580 if (!vo_dga_src_mode)
582 mp_msg(MSGT_VO, MSGL_ERR, "vo_dga: unsupported video format!\n");
583 return 1;
586 vo_dga_vp_width = vo_screenwidth;
587 vo_dga_vp_height = vo_screenheight;
589 mp_msg(MSGT_VO, MSGL_V, "vo_dga: XServer res: %dx%d\n",
590 vo_dga_vp_width, vo_dga_vp_height);
592 // choose a suitable mode ...
594 #ifdef CONFIG_DGA2
595 // Code to change the video mode added by Michael Graffam
596 // mgraffam@idsi.net
598 mp_msg(MSGT_VO, MSGL_V, "vo_dga: vo_modelines=%p, vo_modecount=%d\n",
599 vo_modelines, vo_modecount);
601 if (vo_modelines == NULL)
603 mp_msg(MSGT_VO, MSGL_ERR, "vo_dga: can't get modelines\n");
604 return 1;
607 mp_msg(MSGT_VO, MSGL_INFO,
608 "vo_dga: DGA 2.0 available :-) Can switch resolution AND depth!\n");
609 for (i = 0; i < vo_modecount; i++)
611 if (vd_ModeEqual(vo_modelines[i].depth,
612 vo_modelines[i].bitsPerPixel,
613 vo_modelines[i].redMask,
614 vo_modelines[i].greenMask,
615 vo_modelines[i].blueMask, vo_dga_hw_mode))
618 mp_msg(MSGT_VO, MSGL_V, "maxy: %4d, depth: %2d, %4dx%4d, ",
619 vo_modelines[i].maxViewportY, vo_modelines[i].depth,
620 vo_modelines[i].imageWidth,
621 vo_modelines[i].imageHeight);
622 if (check_res
623 (i, wanted_width, wanted_height, vo_modelines[i].depth,
624 vo_modelines[i].viewportWidth,
625 vo_modelines[i].viewportHeight,
626 (unsigned) vo_modelines[i].verticalRefresh,
627 vo_modelines[i].maxViewportY, &mX, &mY, &mVBI, &mMaxY))
628 j = i;
631 mp_msg(MSGT_VO, MSGL_INFO,
632 "vo_dga: Selected hardware mode %4d x %4d @ %3d Hz @ depth %2d, bitspp %2d.\n",
633 mX, mY, mVBI, HW_MODE.vdm_depth, HW_MODE.vdm_bitspp);
634 mp_msg(MSGT_VO, MSGL_INFO,
635 "vo_dga: Video parameters by codec: %3d x %3d, depth %2d, bitspp %2d.\n",
636 width, height, SRC_MODE.vdm_depth, SRC_MODE.vdm_bitspp);
637 vo_dga_vp_width = mX;
638 vo_dga_vp_height = mY;
640 if ((flags & VOFLAG_SWSCALE) || (flags & VOFLAG_FULLSCREEN))
641 { /* -zoom or -fs */
642 scale_dstW = (d_width + 7) & ~7;
643 scale_dstH = d_height;
644 scale_srcW = width;
645 scale_srcH = height;
646 aspect_save_screenres(mX, mY);
647 aspect_save_orig(scale_srcW, scale_srcH);
648 aspect_save_prescale(scale_dstW, scale_dstH);
649 if (flags & VOFLAG_FULLSCREEN) /* -fs */
650 aspect(&scale_dstW, &scale_dstH, A_ZOOM);
651 else if (flags & VOFLAG_SWSCALE) /* -fs */
652 aspect(&scale_dstW, &scale_dstH, A_NOZOOM);
653 mp_msg(MSGT_VO, MSGL_INFO,
654 "vo_dga: Aspect corrected size for SwScaler: %4d x %4d.\n",
655 scale_dstW, scale_dstH);
656 /* XXX this is a hack, but I'm lazy ;-) :: atmos */
657 width = scale_dstW;
658 height = scale_dstH;
661 vo_dga_width = vo_modelines[j].bytesPerScanline / HW_MODE.vdm_bytespp;
662 dga_modenum = vo_modelines[j].num;
663 modeline = vo_modelines + j;
665 #else
667 #ifdef CONFIG_XF86VM
669 mp_msg(MSGT_VO, MSGL_INFO,
670 "vo_dga: DGA 1.0 compatibility code: Using XF86VidMode for mode switching!\n");
672 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
674 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
675 mp_msg(MSGT_VO, MSGL_INFO,
676 "vo_dga: XF86VidMode Extension v%i.%i\n", vm_ver, vm_rev);
677 have_vm = 1;
678 } else
680 mp_msg(MSGT_VO, MSGL_ERR,
681 "vo_dga: XF86VidMode Extension not available.\n");
684 #define GET_VREFRESH(dotclk, x, y)( (((dotclk)/(x))*1000)/(y) )
686 if (have_vm)
688 int modecount;
690 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
691 &vo_dga_vidmodes);
693 if (vo_dga_vidmodes != NULL)
695 for (i = 0; i < modecount; i++)
697 if (check_res(i, wanted_width, wanted_height,
698 vo_dga_modes[vo_dga_hw_mode].vdm_depth,
699 vo_dga_vidmodes[i]->hdisplay,
700 vo_dga_vidmodes[i]->vdisplay,
701 GET_VREFRESH(vo_dga_vidmodes[i]->dotclock,
702 vo_dga_vidmodes[i]->htotal,
703 vo_dga_vidmodes[i]->vtotal),
704 0, &mX, &mY, &mVBI, &mMaxY))
705 j = i;
708 mp_msg(MSGT_VO, MSGL_INFO,
709 "vo_dga: Selected video mode %4d x %4d @ %3d Hz @ depth %2d, bitspp %2d, video %3d x %3d.\n",
710 mX, mY, mVBI,
711 vo_dga_modes[vo_dga_hw_mode].vdm_depth,
712 vo_dga_modes[vo_dga_hw_mode].vdm_bitspp, width, height);
713 } else
715 mp_msg(MSGT_VO, MSGL_INFO,
716 "vo_dga: XF86VidMode returned no screens - using current resolution.\n");
718 dga_modenum = j;
719 vo_dga_vp_width = mX;
720 vo_dga_vp_height = mY;
723 #else
724 mp_msg(MSGT_VO, MSGL_INFO,
725 "vo_dga: Only have DGA 1.0 extension and no XF86VidMode :-(\n");
726 mp_msg(MSGT_VO, MSGL_INFO,
727 " Thus, resolution switching is NOT possible.\n");
729 #endif
730 #endif
732 vo_dga_src_width = width;
733 vo_dga_src_height = height;
735 if (vo_dga_src_width > vo_dga_vp_width ||
736 vo_dga_src_height > vo_dga_vp_height)
738 mp_msg(MSGT_VO, MSGL_ERR,
739 "vo_dga: Sorry, video larger than viewport is not yet supported!\n");
740 // ugly, do something nicer in the future ...
741 #ifndef CONFIG_DGA2
742 #ifdef CONFIG_XF86VM
743 if (vo_dga_vidmodes)
745 XFree(vo_dga_vidmodes);
746 vo_dga_vidmodes = NULL;
748 #endif
749 #endif
750 return 1;
753 if (vo_dga_vp_width == VO_DGA_INVALID_RES)
755 mp_msg(MSGT_VO, MSGL_ERR,
756 "vo_dga: Something is wrong with your DGA. There doesn't seem to be a\n"
757 " single suitable mode!\n"
758 " Please file a bug report (see DOCS/HTML/en/bugreports.html)\n");
759 #ifndef CONFIG_DGA2
760 #ifdef CONFIG_XF86VM
761 if (vo_dga_vidmodes)
763 XFree(vo_dga_vidmodes);
764 vo_dga_vidmodes = NULL;
766 #endif
767 #endif
768 return 1;
770 // now let's start the DGA thing
772 if (!vo_config_count || width != prev_width || height != prev_height)
774 #ifdef CONFIG_DGA2
776 if (!XDGAOpenFramebuffer(mDisplay, mScreen))
778 mp_msg(MSGT_VO, MSGL_ERR,
779 "vo_dga: Framebuffer mapping failed!!!\n");
780 return 1;
783 dgadevice = XDGASetMode(mDisplay, mScreen, dga_modenum);
784 XDGASync(mDisplay, mScreen);
786 vo_dga_base = dgadevice->data;
787 XFree(dgadevice);
789 XDGASetViewport(mDisplay, mScreen, 0, 0, XDGAFlipRetrace);
791 #else
793 #ifdef CONFIG_XF86VM
794 if (have_vm)
796 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
797 // Two calls are needed to switch modes on my ATI Rage 128. Why?
798 // for riva128 one call is enough!
799 XF86VidModeSwitchToMode(mDisplay, mScreen,
800 vo_dga_vidmodes[dga_modenum]);
801 XF86VidModeSwitchToMode(mDisplay, mScreen,
802 vo_dga_vidmodes[dga_modenum]);
804 #endif
806 XF86DGAGetViewPortSize(mDisplay, mScreen,
807 &vo_dga_vp_width, &vo_dga_vp_height);
809 XF86DGAGetVideo(mDisplay, mScreen,
810 (char **) &vo_dga_base, &vo_dga_width, &bank,
811 &ram);
813 XF86DGADirectVideo(mDisplay, mScreen,
814 XF86DGADirectGraphics | XF86DGADirectMouse |
815 XF86DGADirectKeyb);
817 XF86DGASetViewPort(mDisplay, mScreen, 0, 0);
819 #endif
821 // do some more checkings here ...
823 mp_msg(MSGT_VO, MSGL_V,
824 "vo_dga: bytes/line: %d, screen res: %dx%d, depth: %d, base: %p, bpp: %d\n",
825 vo_dga_width, vo_dga_vp_width, vo_dga_vp_height,
826 HW_MODE.vdm_bytespp, vo_dga_base, HW_MODE.vdm_bitspp);
828 x_off = (vo_dga_vp_width - vo_dga_src_width) >> 1;
829 y_off = (vo_dga_vp_height - vo_dga_src_height) >> 1;
831 vo_dga_bytes_per_line = vo_dga_src_width * HW_MODE.vdm_bytespp;
832 vo_dga_lines = vo_dga_src_height;
834 vo_dga_src_offset = 0;
835 vo_dga_vp_offset =
836 (y_off * vo_dga_width + x_off) * HW_MODE.vdm_bytespp;
838 vo_dga_vp_skip = (vo_dga_width - vo_dga_src_width) * HW_MODE.vdm_bytespp; // todo
840 mp_msg(MSGT_VO, MSGL_V, "vo_dga: vp_off=%d, vp_skip=%d, bpl=%d\n",
841 vo_dga_vp_offset, vo_dga_vp_skip, vo_dga_bytes_per_line);
844 XGrabKeyboard(mDisplay, DefaultRootWindow(mDisplay), True,
845 GrabModeAsync, GrabModeAsync, CurrentTime);
846 if (vo_grabpointer)
847 XGrabPointer(mDisplay, DefaultRootWindow(mDisplay), True,
848 ButtonPressMask, GrabModeAsync, GrabModeAsync,
849 None, None, CurrentTime);
851 if (!vo_config_count || width != prev_width || height != prev_height)
853 init_video_buffers(vo_dga_base,
854 vo_dga_vp_height,
855 vo_dga_width * HW_MODE.vdm_bytespp,
856 #ifdef CONFIG_DGA2
857 modeline->maxViewportY,
858 #else
859 vo_dga_vp_height,
860 #endif
861 vo_doublebuffering);
862 prev_width = width;
863 prev_height = height;
866 mp_msg(MSGT_VO, MSGL_V, "vo_dga: Using %d frame buffer%s.\n",
867 vo_dga_nr_video_buffers,
868 vo_dga_nr_video_buffers == 1 ? "" : "s");
870 vo_dga_is_running = 1;
871 return 0;
874 static int dga_depths_init = 0;
876 static int preinit(const char *arg)
878 if (arg)
880 mp_msg(MSGT_VO, MSGL_INFO, "vo_dga: Unknown subdevice: %s\n", arg);
881 return ENOSYS;
884 if (!vo_init())
885 return -1; // Can't open X11
887 if (dga_depths_init == 0)
888 { // FIXME!?
889 int i;
891 vo_dga_XServer_mode = vd_ValidateMode(vo_depthonscreen);
893 if (vo_dga_XServer_mode == 0)
895 #ifndef CONFIG_DGA2
896 mp_msg(MSGT_VO, MSGL_ERR,
897 "vo_dga: Your X-Server is not running in a ");
898 mp_msg(MSGT_VO, MSGL_ERR,
899 "resolution supported by DGA driver!\n");
900 #endif
901 } //else{
902 // mp_msg(MSGT_VO, MSGL_V, "vo_dga: X running at: %s\n",
903 // vd_GetModeString(vo_dga_XServer_mode));
906 #ifdef CONFIG_DGA2
907 vo_modelines = XDGAQueryModes(mDisplay, mScreen, &vo_modecount);
908 if (vo_modelines)
910 for (i = 0; i < vo_modecount; i++)
912 mp_msg(MSGT_VO, MSGL_V,
913 "vo_dga: (%03d) depth=%d, bpp=%d, r=%08lx, g=%08lx, b=%08lx, %d x %d\n",
914 i, vo_modelines[i].depth,
915 vo_modelines[i].bitsPerPixel,
916 vo_modelines[i].redMask, vo_modelines[i].greenMask,
917 vo_modelines[i].blueMask,
918 vo_modelines[i].viewportWidth,
919 vo_modelines[i].viewportHeight);
920 vd_EnableMode(vo_modelines[i].depth,
921 vo_modelines[i].bitsPerPixel,
922 vo_modelines[i].redMask,
923 vo_modelines[i].greenMask,
924 vo_modelines[i].blueMask);
927 #endif
928 dga_depths_init = 1;
930 if (!vo_dga_modes[1].vdm_supported
931 && vo_dga_modes[2].vdm_supported)
933 vo_dga_modes[1].vdm_supported = 1;
936 if (!vo_dga_modes[3].vdm_supported
937 && vo_dga_modes[4].vdm_supported)
939 vo_dga_modes[3].vdm_supported = 1;
942 for (i = 1; i < vo_dga_mode_num; i++)
944 mp_msg(MSGT_VO, MSGL_INFO, "vo_dga: Mode: %s",
945 vd_GetModeString(i));
946 if (vo_dbpp && vo_dbpp != vo_dga_modes[i].vdm_mplayer_depth)
948 vo_dga_modes[i].vdm_supported = 0;
949 mp_msg(MSGT_VO, MSGL_INFO, " ...disabled by -bpp %d",
950 vo_dbpp);
952 mp_msg(MSGT_VO, MSGL_INFO, "\n");
956 return 0;
959 static uint32_t get_image(mp_image_t * mpi)
961 if (!IMGFMT_IS_BGR(mpi->imgfmt) ||
962 (IMGFMT_BGR_DEPTH(mpi->imgfmt) !=
963 vo_dga_modes[vo_dga_hw_mode].vdm_mplayer_depth)
964 || (mpi->type == MP_IMGTYPE_STATIC && vo_dga_nr_video_buffers > 1)
965 || (mpi->type == MP_IMGTYPE_IP && vo_dga_nr_video_buffers < 2)
966 || (mpi->type == MP_IMGTYPE_IPB))
967 return VO_FALSE;
969 if ((mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) ||
970 (mpi->flags & MP_IMGFLAG_ACCEPT_WIDTH &&
971 ((vo_dga_bytes_per_line + vo_dga_vp_skip) % (mpi->bpp / 8)) == 0)
972 || (mpi->width * (mpi->bpp / 8) ==
973 (vo_dga_bytes_per_line + vo_dga_vp_skip)))
976 mpi->planes[0] = CURRENT_VIDEO_BUFFER.data + vo_dga_vp_offset;
977 mpi->stride[0] = vo_dga_bytes_per_line + vo_dga_vp_skip;
978 mpi->width =
979 (vo_dga_bytes_per_line + vo_dga_vp_skip) / (mpi->bpp / 8);
980 mpi->flags |= MP_IMGFLAG_DIRECT;
981 return VO_TRUE;
984 return VO_FALSE;
987 static int control(uint32_t request, void *data)
989 switch (request)
991 case VOCTRL_GET_IMAGE:
992 return get_image(data);
993 case VOCTRL_QUERY_FORMAT:
994 return query_format(*((uint32_t *) data));
996 return VO_NOTIMPL;
999 //---------------------------------------------------------