aboutsummaryrefslogtreecommitdiff
path: root/frontend/linux/fbdev.c
blob: ec3d5c7ec33c3b53fedd9a0520f96925bacaac39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * (C) Gražvydas "notaz" Ignotas, 2009-2010
 *
 * This work is licensed under the terms of any of these licenses
 * (at your option):
 *  - GNU GPL, version 2 or later.
 *  - GNU LGPL, version 2.1 or later.
 * See the COPYING file in the top-level directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <linux/fb.h>
#include <linux/matroxfb.h>

#include "fbdev.h"

#define PFX "fbdev: "

struct vout_fbdev {
	int	fd;
	void	*mem;
	size_t	mem_size;
	struct	fb_var_screeninfo fbvar_old;
	struct	fb_var_screeninfo fbvar_new;
	int	buffer_write;
	int	fb_size;
	int	buffer_count;
	int	top_border, bottom_border;
	void	*mem_saved;
	size_t	mem_saved_size;
};

void *vout_fbdev_flip(struct vout_fbdev *fbdev)
{
	int draw_buf;

	if (fbdev->buffer_count < 2)
		return fbdev->mem;

	draw_buf = fbdev->buffer_write;
	fbdev->buffer_write++;
	if (fbdev->buffer_write >= fbdev->buffer_count)
		fbdev->buffer_write = 0;

	fbdev->fbvar_new.yoffset = 
		(fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
		fbdev->top_border;

	ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);

	return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
}

void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
{
	int arg = 0;
	ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
}

/* it is recommended to call vout_fbdev_clear() before this */
void *vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp,
		      int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt)
{
	int w_total = left_border + w + right_border;
	int h_total = top_border + h + bottom_border;
	size_t mem_size;
	int ret;

	// unblank to be sure the mode is really accepted
	ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);

	if (fbdev->fbvar_new.bits_per_pixel != bpp ||
			fbdev->fbvar_new.xres != w ||
			fbdev->fbvar_new.yres != h ||
			fbdev->fbvar_new.xres_virtual != w_total||
			fbdev->fbvar_new.yres_virtual < h_total ||
			fbdev->fbvar_new.xoffset != left_border ||
			fbdev->buffer_count != buffer_cnt)
	{
		if (fbdev->fbvar_new.bits_per_pixel != bpp ||
				w != fbdev->fbvar_new.xres || h != fbdev->fbvar_new.yres)
			printf(PFX "switching to %dx%d@%d\n", w, h, bpp);

		fbdev->fbvar_new.xres = w;
		fbdev->fbvar_new.yres = h;
		fbdev->fbvar_new.xres_virtual = w_total;
		fbdev->fbvar_new.yres_virtual = h_total * buffer_cnt;
		fbdev->fbvar_new.xoffset = left_border;
		fbdev->fbvar_new.yoffset = top_border;
		fbdev->fbvar_new.bits_per_pixel = bpp;
		fbdev->fbvar_new.nonstd = 0; // can set YUV here on omapfb
		fbdev->buffer_count = buffer_cnt;
		fbdev->buffer_write = buffer_cnt > 1 ? 1 : 0;

		// seems to help a bit to avoid glitches
		vout_fbdev_wait_vsync(fbdev);

		ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
		if (ret == -1) {
			// retry with no multibuffering
			fbdev->fbvar_new.yres_virtual = h_total;
			ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
			if (ret == -1) {
				perror(PFX "FBIOPUT_VSCREENINFO ioctl");
				return NULL;
			}
			fbdev->buffer_count = 1;
			fbdev->buffer_write = 0;
			fprintf(stderr, PFX "Warning: failed to increase virtual resolution, "
					"multibuffering disabled\n");
		}

	}

	fbdev->fb_size = w_total * h_total * bpp / 8;
	fbdev->top_border = top_border;
	fbdev->bottom_border = bottom_border;

	mem_size = fbdev->fb_size * fbdev->buffer_count;
	if (fbdev->mem_size >= mem_size)
		goto out;

	if (fbdev->mem != NULL)
		munmap(fbdev->mem, fbdev->mem_size);

	fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
	if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
		fprintf(stderr, PFX "Warning: can't map %zd bytes, doublebuffering disabled\n", mem_size);
		fbdev->buffer_count = 1;
		fbdev->buffer_write = 0;
		mem_size = fbdev->fb_size;
		fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
	}
	if (fbdev->mem == MAP_FAILED) {
		fbdev->mem = NULL;
		fbdev->mem_size = 0;
		perror(PFX "mmap framebuffer");
		return NULL;
	}

	fbdev->mem_size = mem_size;

out:
	return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
}

void vout_fbdev_clear(struct vout_fbdev *fbdev)
{
	memset(fbdev->mem, 0, fbdev->mem_size);
}

void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
{
	int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8;
	int i;

	if (y + count > fbdev->top_border + fbdev->fbvar_new.yres)
		count = fbdev->top_border + fbdev->fbvar_new.yres - y;

	if (y >= 0 && count > 0)
		for (i = 0; i < fbdev->buffer_count; i++)
			memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
}

int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
{
	return fbdev->fd;
}

struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt)
{
	struct vout_fbdev *fbdev;
	int req_w, req_h;
	void *pret;
	int ret;

	fbdev = calloc(1, sizeof(*fbdev));
	if (fbdev == NULL)
		return NULL;

	fbdev->fd = open(fbdev_name, O_RDWR);
	if (fbdev->fd == -1) {
		fprintf(stderr, PFX "%s: ", fbdev_name);
		perror("open");
		goto fail_open;
	}

	ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old);
	if (ret == -1) {
		perror(PFX "FBIOGET_VSCREENINFO ioctl");
		goto fail;
	}

	fbdev->fbvar_new = fbdev->fbvar_old;

	req_w = fbdev->fbvar_new.xres;
	if (*w != 0)
		req_w = *w;
	req_h = fbdev->fbvar_new.yres;
	if (*h != 0)
		req_h = *h;

	pret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt);
	if (pret == NULL)
		goto fail;

	printf(PFX "%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres,
		fbdev->fbvar_new.yres, fbdev->fbvar_new.bits_per_pixel);
	*w = fbdev->fbvar_new.xres;
	*h = fbdev->fbvar_new.yres;

	memset(fbdev->mem, 0, fbdev->mem_size);

	// some checks
	ret = 0;
	ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret);
	if (ret != 0)
		fprintf(stderr, PFX "Warning: vsync doesn't seem to be supported\n");

	if (fbdev->buffer_count > 1) {
		fbdev->buffer_write = 0;
		fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
		ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
		if (ret != 0) {
			fbdev->buffer_count = 1;
			fprintf(stderr, PFX "Warning: can't pan display, doublebuffering disabled\n");
		}
	}

	printf("fbdev initialized.\n");
	return fbdev;

fail:
	close(fbdev->fd);
fail_open:
	free(fbdev);
	return NULL;
}

static void vout_fbdev_release(struct vout_fbdev *fbdev)
{
	ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old);
	if (fbdev->mem != MAP_FAILED)
		munmap(fbdev->mem, fbdev->mem_size);
	fbdev->mem = NULL;
}

int vout_fbdev_save(struct vout_fbdev *fbdev)
{
	void *tmp;

	if (fbdev == NULL || fbdev->mem == NULL || fbdev->mem == MAP_FAILED) {
		fprintf(stderr, PFX "bad args for save\n");
		return -1;
	}

	if (fbdev->mem_saved_size < fbdev->mem_size) {
		tmp = realloc(fbdev->mem_saved, fbdev->mem_size);
		if (tmp == NULL)
			return -1;
		fbdev->mem_saved = tmp;
	}
	memcpy(fbdev->mem_saved, fbdev->mem, fbdev->mem_size);
	fbdev->mem_saved_size = fbdev->mem_size;

	vout_fbdev_release(fbdev);
	return 0;
}

int vout_fbdev_restore(struct vout_fbdev *fbdev)
{
	int ret;

	if (fbdev == NULL || fbdev->mem != NULL) {
		fprintf(stderr, PFX "bad args/state for restore\n");
		return -1;
	}

	fbdev->mem = mmap(0, fbdev->mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
	if (fbdev->mem == MAP_FAILED) {
		perror(PFX "restore: memory restore failed");
		return -1;
	}
	memcpy(fbdev->mem, fbdev->mem_saved, fbdev->mem_size);

	ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
	if (ret == -1) {
		perror(PFX "restore: FBIOPUT_VSCREENINFO");
		return -1;
	}

	return 0;
}

void vout_fbdev_finish(struct vout_fbdev *fbdev)
{
	vout_fbdev_release(fbdev);
	if (fbdev->fd >= 0)
		close(fbdev->fd);
	fbdev->fd = -1;
	free(fbdev);
}