| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <unistd.h> |
| 20 | #include <linux/fb.h> |
| 21 | #include <linux/matroxfb.h> |
| 22 | |
| 23 | #include "fbdev.h" |
| 24 | |
| 25 | struct vout_fbdev { |
| 26 | int fd; |
| 27 | void *mem; |
| 28 | size_t mem_size; |
| 29 | struct fb_var_screeninfo fbvar_old; |
| 30 | struct fb_var_screeninfo fbvar_new; |
| 31 | int buffer_write; |
| 32 | int fb_size; |
| 33 | int buffer_count; |
| 34 | int top_border, bottom_border; |
| 35 | }; |
| 36 | |
| 37 | void *vout_fbdev_flip(struct vout_fbdev *fbdev) |
| 38 | { |
| 39 | int draw_buf; |
| 40 | |
| 41 | if (fbdev->buffer_count < 2) |
| 42 | return fbdev->mem; |
| 43 | |
| 44 | draw_buf = fbdev->buffer_write; |
| 45 | fbdev->buffer_write++; |
| 46 | if (fbdev->buffer_write >= fbdev->buffer_count) |
| 47 | fbdev->buffer_write = 0; |
| 48 | |
| 49 | fbdev->fbvar_new.yoffset = |
| 50 | (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf + |
| 51 | fbdev->top_border; |
| 52 | |
| 53 | ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new); |
| 54 | |
| 55 | return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write; |
| 56 | } |
| 57 | |
| 58 | void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev) |
| 59 | { |
| 60 | int arg = 0; |
| 61 | ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg); |
| 62 | } |
| 63 | |
| 64 | /* it is recommended to call vout_fbdev_clear() before this */ |
| 65 | void *vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp, |
| 66 | int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt) |
| 67 | { |
| 68 | int w_total = left_border + w + right_border; |
| 69 | int h_total = top_border + h + bottom_border; |
| 70 | size_t mem_size; |
| 71 | int ret; |
| 72 | |
| 73 | // unblank to be sure the mode is really accepted |
| 74 | ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK); |
| 75 | |
| 76 | if (fbdev->fbvar_new.bits_per_pixel != bpp || |
| 77 | fbdev->fbvar_new.xres != w || |
| 78 | fbdev->fbvar_new.yres != h || |
| 79 | fbdev->fbvar_new.xres_virtual != w_total|| |
| 80 | fbdev->fbvar_new.yres_virtual < h_total || |
| 81 | fbdev->fbvar_new.xoffset != left_border || |
| 82 | fbdev->buffer_count != buffer_cnt) |
| 83 | { |
| 84 | if (fbdev->fbvar_new.bits_per_pixel != bpp || |
| 85 | w != fbdev->fbvar_new.xres || h != fbdev->fbvar_new.yres) |
| 86 | printf(" switching to %dx%d@%d\n", w, h, bpp); |
| 87 | |
| 88 | fbdev->fbvar_new.xres = w; |
| 89 | fbdev->fbvar_new.yres = h; |
| 90 | fbdev->fbvar_new.xres_virtual = w_total; |
| 91 | fbdev->fbvar_new.yres_virtual = h_total * buffer_cnt; |
| 92 | fbdev->fbvar_new.xoffset = left_border; |
| 93 | fbdev->fbvar_new.yoffset = top_border; |
| 94 | fbdev->fbvar_new.bits_per_pixel = bpp; |
| 95 | fbdev->fbvar_new.nonstd = 0; // can set YUV here on omapfb |
| 96 | fbdev->buffer_count = buffer_cnt; |
| 97 | fbdev->buffer_write = 1; |
| 98 | |
| 99 | // seems to help a bit to avoid glitches |
| 100 | vout_fbdev_wait_vsync(fbdev); |
| 101 | |
| 102 | ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new); |
| 103 | if (ret == -1) { |
| 104 | // retry with no multibuffering |
| 105 | fbdev->fbvar_new.yres_virtual = h_total; |
| 106 | ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new); |
| 107 | if (ret == -1) { |
| 108 | perror("FBIOPUT_VSCREENINFO ioctl"); |
| 109 | return NULL; |
| 110 | } |
| 111 | fbdev->buffer_count = 1; |
| 112 | fbdev->buffer_write = 0; |
| 113 | fprintf(stderr, "Warning: failed to increase virtual resolution, " |
| 114 | "multibuffering disabled\n"); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | fbdev->fb_size = w_total * h_total * bpp / 8; |
| 120 | fbdev->top_border = top_border; |
| 121 | fbdev->bottom_border = bottom_border; |
| 122 | |
| 123 | mem_size = fbdev->fb_size * fbdev->buffer_count; |
| 124 | if (fbdev->mem_size >= mem_size) |
| 125 | goto out; |
| 126 | |
| 127 | if (fbdev->mem != NULL) |
| 128 | munmap(fbdev->mem, fbdev->mem_size); |
| 129 | |
| 130 | fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0); |
| 131 | if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) { |
| 132 | fprintf(stderr, "Warning: can't map %zd bytes, doublebuffering disabled\n", mem_size); |
| 133 | fbdev->buffer_count = 1; |
| 134 | fbdev->buffer_write = 0; |
| 135 | mem_size = fbdev->fb_size; |
| 136 | fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0); |
| 137 | } |
| 138 | if (fbdev->mem == MAP_FAILED) { |
| 139 | fbdev->mem = NULL; |
| 140 | fbdev->mem_size = 0; |
| 141 | perror("mmap framebuffer"); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | fbdev->mem_size = mem_size; |
| 146 | |
| 147 | out: |
| 148 | return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write; |
| 149 | } |
| 150 | |
| 151 | void vout_fbdev_clear(struct vout_fbdev *fbdev) |
| 152 | { |
| 153 | memset(fbdev->mem, 0, fbdev->mem_size); |
| 154 | } |
| 155 | |
| 156 | void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count) |
| 157 | { |
| 158 | int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8; |
| 159 | int i; |
| 160 | |
| 161 | if (y + count > fbdev->top_border + fbdev->fbvar_new.yres) |
| 162 | count = fbdev->top_border + fbdev->fbvar_new.yres - y; |
| 163 | |
| 164 | if (y >= 0 && count > 0) |
| 165 | for (i = 0; i < fbdev->buffer_count; i++) |
| 166 | memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count); |
| 167 | } |
| 168 | |
| 169 | int vout_fbdev_get_fd(struct vout_fbdev *fbdev) |
| 170 | { |
| 171 | return fbdev->fd; |
| 172 | } |
| 173 | |
| 174 | struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt) |
| 175 | { |
| 176 | struct vout_fbdev *fbdev; |
| 177 | int req_w, req_h; |
| 178 | void *pret; |
| 179 | int ret; |
| 180 | |
| 181 | fbdev = calloc(1, sizeof(*fbdev)); |
| 182 | if (fbdev == NULL) |
| 183 | return NULL; |
| 184 | |
| 185 | fbdev->fd = open(fbdev_name, O_RDWR); |
| 186 | if (fbdev->fd == -1) { |
| 187 | fprintf(stderr, "%s: ", fbdev_name); |
| 188 | perror("open"); |
| 189 | goto fail_open; |
| 190 | } |
| 191 | |
| 192 | ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old); |
| 193 | if (ret == -1) { |
| 194 | perror("FBIOGET_VSCREENINFO ioctl"); |
| 195 | goto fail; |
| 196 | } |
| 197 | |
| 198 | fbdev->fbvar_new = fbdev->fbvar_old; |
| 199 | |
| 200 | req_w = fbdev->fbvar_new.xres; |
| 201 | if (*w != 0) |
| 202 | req_w = *w; |
| 203 | req_h = fbdev->fbvar_new.yres; |
| 204 | if (*h != 0) |
| 205 | req_h = *h; |
| 206 | |
| 207 | pret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt); |
| 208 | if (pret == NULL) |
| 209 | goto fail; |
| 210 | |
| 211 | printf("%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres, fbdev->fbvar_new.yres, |
| 212 | fbdev->fbvar_new.bits_per_pixel); |
| 213 | *w = fbdev->fbvar_new.xres; |
| 214 | *h = fbdev->fbvar_new.yres; |
| 215 | |
| 216 | memset(fbdev->mem, 0, fbdev->mem_size); |
| 217 | |
| 218 | // some checks |
| 219 | ret = 0; |
| 220 | ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret); |
| 221 | if (ret != 0) |
| 222 | fprintf(stderr, "Warning: vsync doesn't seem to be supported\n"); |
| 223 | |
| 224 | if (fbdev->buffer_count > 1) { |
| 225 | fbdev->buffer_write = 0; |
| 226 | fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1); |
| 227 | ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new); |
| 228 | if (ret != 0) { |
| 229 | fbdev->buffer_count = 1; |
| 230 | fprintf(stderr, "Warning: can't pan display, doublebuffering disabled\n"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | printf("fbdev initialized.\n"); |
| 235 | return fbdev; |
| 236 | |
| 237 | fail: |
| 238 | close(fbdev->fd); |
| 239 | fail_open: |
| 240 | free(fbdev); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | void vout_fbdev_finish(struct vout_fbdev *fbdev) |
| 245 | { |
| 246 | ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old); |
| 247 | if (fbdev->mem != MAP_FAILED) |
| 248 | munmap(fbdev->mem, fbdev->mem_size); |
| 249 | if (fbdev->fd >= 0) |
| 250 | close(fbdev->fd); |
| 251 | fbdev->mem = NULL; |
| 252 | fbdev->fd = -1; |
| 253 | free(fbdev); |
| 254 | } |
| 255 | |