2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
17 #include <sys/ioctl.h>
21 #include <linux/matroxfb.h>
31 struct fb_var_screeninfo fbvar_old;
32 struct fb_var_screeninfo fbvar_new;
36 int top_border, bottom_border;
38 size_t mem_saved_size;
41 void *vout_fbdev_flip(struct vout_fbdev *fbdev)
45 if (fbdev->buffer_count < 2)
48 draw_buf = fbdev->buffer_write;
49 fbdev->buffer_write++;
50 if (fbdev->buffer_write >= fbdev->buffer_count)
51 fbdev->buffer_write = 0;
53 fbdev->fbvar_new.yoffset =
54 (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
57 ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
59 return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
62 void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
65 ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
68 /* it is recommended to call vout_fbdev_clear() before this */
69 void *vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp,
70 int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt)
72 int w_total = left_border + w + right_border;
73 int h_total = top_border + h + bottom_border;
77 // unblank to be sure the mode is really accepted
78 ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);
80 if (fbdev->fbvar_new.bits_per_pixel != bpp ||
81 fbdev->fbvar_new.xres != w ||
82 fbdev->fbvar_new.yres != h ||
83 fbdev->fbvar_new.xres_virtual != w_total||
84 fbdev->fbvar_new.yres_virtual < h_total ||
85 fbdev->fbvar_new.xoffset != left_border ||
86 fbdev->buffer_count != buffer_cnt)
88 if (fbdev->fbvar_new.bits_per_pixel != bpp ||
89 w != fbdev->fbvar_new.xres || h != fbdev->fbvar_new.yres)
90 printf(PFX "switching to %dx%d@%d\n", w, h, bpp);
92 fbdev->fbvar_new.xres = w;
93 fbdev->fbvar_new.yres = h;
94 fbdev->fbvar_new.xres_virtual = w_total;
95 fbdev->fbvar_new.yres_virtual = h_total * buffer_cnt;
96 fbdev->fbvar_new.xoffset = left_border;
97 fbdev->fbvar_new.yoffset = top_border;
98 fbdev->fbvar_new.bits_per_pixel = bpp;
99 fbdev->fbvar_new.nonstd = 0; // can set YUV here on omapfb
100 fbdev->buffer_count = buffer_cnt;
101 fbdev->buffer_write = buffer_cnt > 1 ? 1 : 0;
103 // seems to help a bit to avoid glitches
104 vout_fbdev_wait_vsync(fbdev);
106 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
108 // retry with no multibuffering
109 fbdev->fbvar_new.yres_virtual = h_total;
110 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
112 perror(PFX "FBIOPUT_VSCREENINFO ioctl");
115 fbdev->buffer_count = 1;
116 fbdev->buffer_write = 0;
117 fprintf(stderr, PFX "Warning: failed to increase virtual resolution, "
118 "multibuffering disabled\n");
123 fbdev->fb_size = w_total * h_total * bpp / 8;
124 fbdev->top_border = top_border;
125 fbdev->bottom_border = bottom_border;
127 mem_size = fbdev->fb_size * fbdev->buffer_count;
128 if (fbdev->mem_size >= mem_size)
131 if (fbdev->mem != NULL)
132 munmap(fbdev->mem, fbdev->mem_size);
134 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
135 if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
136 fprintf(stderr, PFX "Warning: can't map %zd bytes, doublebuffering disabled\n", mem_size);
137 fbdev->buffer_count = 1;
138 fbdev->buffer_write = 0;
139 mem_size = fbdev->fb_size;
140 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
142 if (fbdev->mem == MAP_FAILED) {
145 perror(PFX "mmap framebuffer");
149 fbdev->mem_size = mem_size;
152 return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
155 void vout_fbdev_clear(struct vout_fbdev *fbdev)
157 memset(fbdev->mem, 0, fbdev->mem_size);
160 void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
162 int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8;
165 if (y + count > fbdev->top_border + fbdev->fbvar_new.yres)
166 count = fbdev->top_border + fbdev->fbvar_new.yres - y;
168 if (y >= 0 && count > 0)
169 for (i = 0; i < fbdev->buffer_count; i++)
170 memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
173 int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
178 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt)
180 struct vout_fbdev *fbdev;
185 fbdev = calloc(1, sizeof(*fbdev));
189 fbdev->fd = open(fbdev_name, O_RDWR);
190 if (fbdev->fd == -1) {
191 fprintf(stderr, PFX "%s: ", fbdev_name);
196 ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old);
198 perror(PFX "FBIOGET_VSCREENINFO ioctl");
202 fbdev->fbvar_new = fbdev->fbvar_old;
204 req_w = fbdev->fbvar_new.xres;
207 req_h = fbdev->fbvar_new.yres;
211 pret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt);
215 printf(PFX "%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres,
216 fbdev->fbvar_new.yres, fbdev->fbvar_new.bits_per_pixel);
217 *w = fbdev->fbvar_new.xres;
218 *h = fbdev->fbvar_new.yres;
220 memset(fbdev->mem, 0, fbdev->mem_size);
224 ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret);
226 fprintf(stderr, PFX "Warning: vsync doesn't seem to be supported\n");
228 if (fbdev->buffer_count > 1) {
229 fbdev->buffer_write = 0;
230 fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
231 ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
233 fbdev->buffer_count = 1;
234 fprintf(stderr, PFX "Warning: can't pan display, doublebuffering disabled\n");
238 printf("fbdev initialized.\n");
248 static void vout_fbdev_release(struct vout_fbdev *fbdev)
250 ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old);
251 if (fbdev->mem != MAP_FAILED)
252 munmap(fbdev->mem, fbdev->mem_size);
256 int vout_fbdev_save(struct vout_fbdev *fbdev)
260 if (fbdev == NULL || fbdev->mem == NULL || fbdev->mem == MAP_FAILED) {
261 fprintf(stderr, PFX "bad args for save\n");
265 if (fbdev->mem_saved_size < fbdev->mem_size) {
266 tmp = realloc(fbdev->mem_saved, fbdev->mem_size);
269 fbdev->mem_saved = tmp;
271 memcpy(fbdev->mem_saved, fbdev->mem, fbdev->mem_size);
272 fbdev->mem_saved_size = fbdev->mem_size;
274 vout_fbdev_release(fbdev);
278 int vout_fbdev_restore(struct vout_fbdev *fbdev)
282 if (fbdev == NULL || fbdev->mem != NULL) {
283 fprintf(stderr, PFX "bad args/state for restore\n");
287 fbdev->mem = mmap(0, fbdev->mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
288 if (fbdev->mem == MAP_FAILED) {
289 perror(PFX "restore: memory restore failed");
292 memcpy(fbdev->mem, fbdev->mem_saved, fbdev->mem_size);
294 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
296 perror(PFX "restore: FBIOPUT_VSCREENINFO");
303 void vout_fbdev_finish(struct vout_fbdev *fbdev)
305 vout_fbdev_release(fbdev);