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