2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
11 #include <sys/types.h>
14 #include <sys/ioctl.h>
18 #include <linux/matroxfb.h>
26 struct fb_var_screeninfo fbvar_old;
27 struct fb_var_screeninfo fbvar_new;
31 int top_border, bottom_border;
34 void *vout_fbdev_flip(struct vout_fbdev *fbdev)
38 if (fbdev->buffer_count < 2)
41 draw_buf = fbdev->buffer_write;
42 fbdev->buffer_write++;
43 if (fbdev->buffer_write >= fbdev->buffer_count)
44 fbdev->buffer_write = 0;
46 fbdev->fbvar_new.yoffset =
47 (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
50 ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
52 return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
55 void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
58 ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
61 int vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp,
62 int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt)
64 int w_total = left_border + w + right_border;
65 int h_total = top_border + h + bottom_border;
69 // unblank to be sure the mode is really accepted
70 ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);
72 if (fbdev->fbvar_new.bits_per_pixel != bpp ||
73 w != fbdev->fbvar_new.xres ||
74 h != fbdev->fbvar_new.yres ||
75 w_total != fbdev->fbvar_new.xres_virtual ||
76 h_total > fbdev->fbvar_new.yres_virtual ||
77 left_border != fbdev->fbvar_new.xoffset) {
78 fbdev->fbvar_new.xres = w;
79 fbdev->fbvar_new.yres = h;
80 fbdev->fbvar_new.xres_virtual = w_total;
81 fbdev->fbvar_new.yres_virtual = h_total;
82 fbdev->fbvar_new.xoffset = left_border;
83 fbdev->fbvar_new.bits_per_pixel = bpp;
84 printf(" switching to %dx%d@%d\n", w, h, bpp);
85 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
87 perror("FBIOPUT_VSCREENINFO ioctl");
92 fbdev->buffer_count = buffer_cnt;
94 if (fbdev->fbvar_new.yres_virtual < h_total * fbdev->buffer_count) {
95 fbdev->fbvar_new.yres_virtual = h_total * fbdev->buffer_count;
96 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
98 fbdev->buffer_count = 1;
99 fprintf(stderr, "Warning: failed to increase virtual resolution, "
100 "doublebuffering disabled\n");
104 fbdev->fb_size = w_total * h_total * bpp / 8;
105 fbdev->top_border = top_border;
106 fbdev->bottom_border = bottom_border;
108 mem_size = fbdev->fb_size * fbdev->buffer_count;
109 if (fbdev->mem_size >= mem_size)
112 if (fbdev->mem != NULL)
113 munmap(fbdev->mem, fbdev->mem_size);
115 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
116 if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
117 fprintf(stderr, "Warning: can't map %zd bytes, doublebuffering disabled\n", fbdev->mem_size);
118 fbdev->buffer_count = 1;
119 mem_size = fbdev->fb_size;
120 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
122 if (fbdev->mem == MAP_FAILED) {
125 perror("mmap framebuffer");
129 fbdev->mem_size = mem_size;
133 void vout_fbdev_clear(struct vout_fbdev *fbdev)
135 memset(fbdev->mem, 0, fbdev->mem_size);
138 void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
140 int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8;
143 if (y + count > fbdev->top_border + fbdev->fbvar_new.yres)
144 count = fbdev->top_border + fbdev->fbvar_new.yres - y;
146 if (y >= 0 && count > 0)
147 for (i = 0; i < fbdev->buffer_count; i++)
148 memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
151 int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
156 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt)
158 struct vout_fbdev *fbdev;
162 fbdev = calloc(1, sizeof(*fbdev));
166 fbdev->fd = open(fbdev_name, O_RDWR);
167 if (fbdev->fd == -1) {
168 fprintf(stderr, "%s: ", fbdev_name);
173 ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old);
175 perror("FBIOGET_VSCREENINFO ioctl");
179 fbdev->fbvar_new = fbdev->fbvar_old;
181 req_w = fbdev->fbvar_new.xres;
184 req_h = fbdev->fbvar_new.yres;
188 ret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt);
192 printf("%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres, fbdev->fbvar_new.yres,
193 fbdev->fbvar_new.bits_per_pixel);
194 *w = fbdev->fbvar_new.xres;
195 *h = fbdev->fbvar_new.yres;
197 memset(fbdev->mem, 0, fbdev->mem_size);
201 ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret);
203 fprintf(stderr, "Warning: vsync doesn't seem to be supported\n");
205 if (fbdev->buffer_count > 1) {
206 fbdev->buffer_write = 0;
207 fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
208 ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
210 fbdev->buffer_count = 1;
211 fprintf(stderr, "Warning: can't pan display, doublebuffering disabled\n");
215 printf("fbdev initialized.\n");
225 void vout_fbdev_finish(struct vout_fbdev *fbdev)
227 ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old);
228 if (fbdev->mem != MAP_FAILED)
229 munmap(fbdev->mem, fbdev->mem_size);