summaryrefslogtreecommitdiffstats
path: root/libvo/vo_fbdev.c
blob: 88aa36a0d0ed53575cbe69d45c2578cb777c6acb (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <linux/vt.h>

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"

#include "yuv2rgb.h"

LIBVO_EXTERN(fbdev)

//#include "yuv2rgb.h"

static vo_info_t vo_info = {
	"Framebuffer Device",
	"fbdev",
	"Szabolcs Berecz <szabi@inf.elte.hu>",
	""
};

static int vt_active;
static int vt_fd;

char *fb_dev_name = NULL;
static int fb_dev_fd;
static size_t fb_size;
static uint8_t *frame_buffer;
static int fb_bpp;

static int in_width;
static int in_height;
static int out_width;
static int out_height;
static uint8_t *next_frame;
static int screen_width;
static uint32_t pixel_format;

static int init_done = 0;

static uint32_t init(uint32_t width, uint32_t height, uint32_t d_width,
		uint32_t d_height, uint32_t fullscreen, char *title,
		uint32_t format)
{
	int fd, vt;
	char vt_name[11];
	struct vt_stat vt_state;
	struct vt_mode vt_mode;
	struct fb_fix_screeninfo fix_info;
	struct fb_var_screeninfo var_info;

	/* get a free vt */
	if ((fd = open("/dev/tty0", O_WRONLY, 0)) == -1) {
		printf("Can't open /dev/tty0: %s\n", strerror(errno));
		return 1;
	}
	if (ioctl(fd, VT_OPENQRY, &vt) < 0 || vt == -1) {
		printf("Can't open a free VT: %s\n", strerror(errno));
		return 1;
	}
	close(fd);

	/* open the vt */
	snprintf(vt_name, 10, "/dev/tty%d", vt);
	if ((vt_fd = open(vt_name, O_RDWR | O_NONBLOCK, 0)) == -1) {
		printf("Can't open %s: %s\n", vt_name, strerror(errno));
		return 1;
	}

	/* save the current vtnum */
	if (!ioctl(vt_fd, VT_GETSTATE, &vt_state))
		vt_active = vt_state.v_active;

	/* detach the controlling tty */
	if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
		ioctl(fd, TIOCNOTTY, 0);
		close(fd);
	}
#if 0
	/* switch to the new vt */
	if (ioctl(vt_fd, VT_ACTIVATE, vt_active))
		printf("ioctl VT_ACTIVATE: %s\n", strerror(errno));
	if (ioctl(vt_fd, VT_WAITACTIVE, vt_active))
		printf("ioctl VT_WAITACTIVE: %s\n", strerror(errno));
	if (ioctl(vt_fd, VT_GETMODE, &vt_mode) < 0) {
		printf("ioctl VT_GETMODE: %s\n", strerror(errno));
		return 1;
	}
	signal(SIGUSR1, vt_request);
	vt_mode.mode = VT_PROCESS;
	vt_mode.relsig = SIGUSR1;
	vt_mode.acqsig = SIGUSR1;
	if (ioctl(vt_fd, VT_SETMODE, &vt_mode) < 0) {
		printf("ioctl VT_SETMODE: %s\n", strerror(errno));
		return 1;
	}
#endif

	if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
		fb_dev_name = "/dev/fb0";
	if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
		printf("Can't open %s: %s\n", fb_dev_name, strerror(errno));
		return 1;
	}
	if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &var_info)) {
		printf("Can't get VSCREENINFO: %s\n", strerror(errno));
		return 1;
	}

	if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fix_info)) {
		printf("Can't get VSCREENINFO: %s\n", strerror(errno));
		return 1;
	}
	switch (fix_info.type) {
		case FB_TYPE_VGA_PLANES:
			printf("FB_TYPE_VGA_PLANES not supported.\n");
			return 1;
			break;
		case FB_TYPE_PLANES:
			printf("FB_TYPE_PLANES not supported.\n");
			return 1;
			break;
		case FB_TYPE_INTERLEAVED_PLANES:
			printf("FB_TYPE_INTERLEAVED_PLANES not supported.\n");
			return 1;
			break;
#ifdef FB_TYPE_TEXT
		case FB_TYPE_TEXT:
			printf("FB_TYPE_TEXT not supported.\n");
			return 1;
			break;
#endif
		case FB_TYPE_PACKED_PIXELS:
			/* OK */
			break;
		default:
			printf("unknown FB_TYPE: %d\n", fix_info.type);
			return 1;
	}			
	fb_size = fix_info.smem_len;
	if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
				MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) {
		printf("Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
		return 1;
	}
	close(fb_dev_fd);
	fb_bpp = var_info.bits_per_pixel;
	screen_width = fix_info.line_length;

	printf("vo_fbdev: frame_buffer @ %p\n", frame_buffer);
	printf("vo_fbdev: frame_buffer size: %d bytes\n", fb_size);
	printf("vo_fbdev: bits_per_pixel: %d\n", fb_bpp);
	printf("vo_fbdev: visual: %d\n", fix_info.visual);

	in_width = width;
	in_height = height;
	out_width = width;
	out_height = height;
	pixel_format = format;
	if (!(next_frame = (uint8_t *) malloc(in_width * in_height * (fb_bpp / 8)))) {
		printf("Can't malloc next_frame: %s\n", strerror(errno));
		return 1;
	}

	if (format == IMGFMT_YV12)
		yuv2rgb_init(fb_bpp, MODE_RGB);
	init_done = 1;
	return 0;
}

static uint32_t query_format(uint32_t format)
{
	if (format == IMGFMT_YV12)
		return 1;
	if (format == IMGFMT_RGB32 && fb_bpp == 32)
		return 1;
	switch (format) {
		case IMGFMT_YV12:
			return 1;
		case IMGFMT_RGB32:
			if (fb_bpp == 32)
				return 1;
			break;
		case IMGFMT_RGB24:
			if (fb_bpp == 24)
				return 1;
			break;
		case IMGFMT_RGB16:
			if (fb_bpp == 16)
				return 1;
			break;
		case IMGFMT_RGB15:
			if (fb_bpp == 15)
				return 1;
			break;
	}
	return 0;
}

static const vo_info_t *get_info(void)
{
	return &vo_info;
}

static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
		unsigned char *srca, int stride)
{
	int x, y;
	uint8_t *dst;

	if (pixel_format == IMGFMT_YV12) {
		for (y = 0; y < h; y++){
			dst = next_frame + (in_width * (y0 + y) + x0) * (fb_bpp / 8);
			for (x = 0; x < w; x++) {
				if (srca[x]) {
					dst[0] = (dst[0]*(srca[x]^255)+src[x]*(srca[x]))>>8;
					dst[1] = (dst[1]*(srca[x]^255)+src[x]*(srca[x]))>>8;
					dst[2] = (dst[2]*(srca[x]^255)+src[x]*(srca[x]))>>8;
				}
				dst += fb_bpp / 8;
			}
			src += stride;
			srca += stride;
		}
	}
}

static uint32_t draw_frame(uint8_t *src[])
{
	if (pixel_format == IMGFMT_YV12) {
		yuv2rgb(next_frame, src[0], src[1], src[2], in_width,
				in_height, in_width * (fb_bpp / 8),
				in_width, in_width / 2);
	} else {
		int i;
		for (i = 0; i < in_height; i++)
			memcpy(next_frame, src[0], in_width * (fb_bpp / 8));
	}
	return 0;
}

static uint32_t draw_slice(uint8_t *src[], int stride[], int w, int h, int x,
		int y)
{
	uint8_t *dest;

	dest = next_frame + (in_width * y + x) * (fb_bpp / 8);
	yuv2rgb(dest, src[0], src[1], src[2], w, h, in_width * (fb_bpp / 8),
			stride[0], stride[1]);
	return 0;
}

static void check_events(void)
{
}

static void flip_page(void)
{
	int i, out_offset = 0, in_offset = 0;

	vo_draw_text(in_width, in_height, draw_alpha);
	check_events();
	for (i = 0; i < in_height; i++) {
		memcpy(frame_buffer + out_offset, next_frame + in_offset,
				in_width * (fb_bpp / 8));
		out_offset += screen_width;
		in_offset += in_width * (fb_bpp / 8);
	}
}

static void uninit(void)
{
	if (vt_active >= 0)
		ioctl(vt_fd, VT_ACTIVATE, vt_active);
	printf("vo_fbdev: uninit\n");
	free(next_frame);
	munmap(frame_buffer, fb_size);
}