vo_tga: specify 15 bpp tga format endianness
[mplayer/glamo.git] / libvo / vo_fbdev2.c
blob60bdee9a177a3716f1dbc067f9fd29f22996f86d
1 /*
2 * video driver for framebuffer device
3 * copyright (C) 2003 Joey Parrish <joey@nicewarrior.org>
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
29 #include <sys/mman.h>
30 #include <sys/ioctl.h>
31 #include <linux/fb.h>
33 #include "config.h"
34 #include "video_out.h"
35 #include "video_out_internal.h"
36 #include "fastmemcpy.h"
37 #include "sub.h"
38 #include "mp_msg.h"
40 static const vo_info_t info = {
41 "Framebuffer Device",
42 "fbdev2",
43 "Joey Parrish <joey@nicewarrior.org>",
47 const LIBVO_EXTERN(fbdev2)
49 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
51 p->bits_per_pixel = (bpp + 1) & ~1;
52 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
53 p->transp.offset = p->transp.length = 0;
54 p->blue.offset = 0;
55 switch (bpp) {
56 case 32:
57 p->transp.offset = 24;
58 p->transp.length = 8;
59 case 24:
60 p->red.offset = 16;
61 p->red.length = 8;
62 p->green.offset = 8;
63 p->green.length = 8;
64 p->blue.length = 8;
65 break;
66 case 16:
67 p->red.offset = 11;
68 p->green.length = 6;
69 p->red.length = 5;
70 p->green.offset = 5;
71 p->blue.length = 5;
72 break;
73 case 15:
74 p->red.offset = 10;
75 p->green.length = 5;
76 p->red.length = 5;
77 p->green.offset = 5;
78 p->blue.length = 5;
79 break;
80 case 12:
81 p->red.offset = 8;
82 p->green.length = 4;
83 p->red.length = 4;
84 p->green.offset = 4;
85 p->blue.length = 4;
86 break;
90 static char *fb_dev_name = NULL; // such as /dev/fb0
91 static int fb_dev_fd; // handle for fb_dev_name
92 static uint8_t *frame_buffer = NULL; // mmap'd access to fbdev
93 static uint8_t *center = NULL; // where to begin writing our image (centered?)
94 static struct fb_fix_screeninfo fb_finfo; // fixed info
95 static struct fb_var_screeninfo fb_vinfo; // variable info
96 static struct fb_var_screeninfo fb_orig_vinfo; // variable info to restore later
97 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
98 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
99 static int fb_cmap_changed = 0; // to restore map
100 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
101 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
102 static size_t fb_size; // size of frame_buffer
103 static int fb_line_len; // length of one line in bytes
104 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
105 unsigned char *srca, int stride, unsigned char *dst,
106 int dstride);
108 static uint8_t *next_frame = NULL; // for double buffering
109 static int in_width;
110 static int in_height;
111 static int out_width;
112 static int out_height;
114 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
116 int i, cols, rcols, gcols, bcols;
117 uint16_t *red, *green, *blue;
118 struct fb_cmap *cmap;
120 rcols = 1 << var->red.length;
121 gcols = 1 << var->green.length;
122 bcols = 1 << var->blue.length;
124 /* Make our palette the length of the deepest color */
125 cols = (rcols > gcols ? rcols : gcols);
126 cols = (cols > bcols ? cols : bcols);
128 red = malloc(cols * sizeof(red[0]));
129 if(!red) {
130 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't allocate red palette with %d entries.\n", cols);
131 return NULL;
133 for(i=0; i< rcols; i++)
134 red[i] = (65535/(rcols-1)) * i;
136 green = malloc(cols * sizeof(green[0]));
137 if(!green) {
138 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't allocate green palette with %d entries.\n", cols);
139 free(red);
140 return NULL;
142 for(i=0; i< gcols; i++)
143 green[i] = (65535/(gcols-1)) * i;
145 blue = malloc(cols * sizeof(blue[0]));
146 if(!blue) {
147 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't allocate blue palette with %d entries.\n", cols);
148 free(red);
149 free(green);
150 return NULL;
152 for(i=0; i< bcols; i++)
153 blue[i] = (65535/(bcols-1)) * i;
155 cmap = malloc(sizeof(struct fb_cmap));
156 if(!cmap) {
157 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't allocate color map\n");
158 free(red);
159 free(green);
160 free(blue);
161 return NULL;
163 cmap->start = 0;
164 cmap->transp = 0;
165 cmap->len = cols;
166 cmap->red = red;
167 cmap->blue = blue;
168 cmap->green = green;
169 cmap->transp = NULL;
171 return cmap;
174 static int fb_preinit(int reset)
176 static int fb_preinit_done = 0;
177 static int fb_err = -1;
179 if (reset) {
180 fb_preinit_done = 0;
181 return 0;
184 if (fb_preinit_done)
185 return fb_err;
186 fb_preinit_done = 1;
188 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
189 fb_dev_name = strdup("/dev/fb0");
191 mp_msg(MSGT_VO, MSGL_V, "[fbdev2] Using device %s\n", fb_dev_name);
193 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
194 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't open %s: %s\n", fb_dev_name, strerror(errno));
195 goto err_out;
197 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
198 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't get VSCREENINFO: %s\n", strerror(errno));
199 goto err_out;
201 fb_orig_vinfo = fb_vinfo;
203 fb_bpp = fb_vinfo.bits_per_pixel;
205 /* 16 and 15 bpp is reported as 16 bpp */
206 if (fb_bpp == 16)
207 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
208 fb_vinfo.blue.length;
210 fb_err = 0;
211 return 0;
212 err_out:
213 if (fb_dev_fd >= 0) close(fb_dev_fd);
214 fb_dev_fd = -1;
215 fb_err = -1;
216 return -1;
219 static int preinit(const char *subdevice)
221 if (subdevice)
223 if (fb_dev_name) free(fb_dev_name);
224 fb_dev_name = strdup(subdevice);
226 return fb_preinit(0);
229 static int config(uint32_t width, uint32_t height, uint32_t d_width,
230 uint32_t d_height, uint32_t flags, char *title,
231 uint32_t format)
233 struct fb_cmap *cmap;
234 int fs = flags & VOFLAG_FULLSCREEN;
236 out_width = width;
237 out_height = height;
238 in_width = width;
239 in_height = height;
241 if (fs) {
242 out_width = fb_vinfo.xres;
243 out_height = fb_vinfo.yres;
246 if (out_width < in_width || out_height < in_height) {
247 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Screensize is smaller than video size (%dx%d < %dx%d)\n",
248 out_width, out_height, in_width, in_height);
249 return 1;
252 switch (fb_bpp) {
253 case 32: draw_alpha_p = vo_draw_alpha_rgb32; break;
254 case 24: draw_alpha_p = vo_draw_alpha_rgb24; break;
255 case 16: draw_alpha_p = vo_draw_alpha_rgb16; break;
256 case 15: draw_alpha_p = vo_draw_alpha_rgb15; break;
257 case 12: draw_alpha_p = vo_draw_alpha_rgb12; break;
258 default: return 1;
261 if (vo_config_count == 0) {
262 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
263 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't get FSCREENINFO: %s\n", strerror(errno));
264 return 1;
267 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
268 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] type %d not supported\n", fb_finfo.type);
269 return 1;
272 switch (fb_finfo.visual) {
273 case FB_VISUAL_TRUECOLOR:
274 break;
275 case FB_VISUAL_DIRECTCOLOR:
276 mp_msg(MSGT_VO, MSGL_V, "[fbdev2] creating cmap for directcolor\n");
277 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
278 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] can't get cmap: %s\n", strerror(errno));
279 return 1;
281 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
282 return 1;
283 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
284 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] can't put cmap: %s\n", strerror(errno));
285 return 1;
287 fb_cmap_changed = 1;
288 free(cmap->red);
289 free(cmap->green);
290 free(cmap->blue);
291 free(cmap);
292 break;
293 default:
294 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] visual: %d not yet supported\n", fb_finfo.visual);
295 return 1;
298 fb_size = fb_finfo.smem_len;
299 fb_line_len = fb_finfo.line_length;
300 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) {
301 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
302 return 1;
306 center = frame_buffer +
307 ( (out_width - in_width) / 2 ) * fb_pixel_size +
308 ( (out_height - in_height) / 2 ) * fb_line_len;
310 #ifndef USE_CONVERT2FB
311 if (!(next_frame = realloc(next_frame, in_width * in_height * fb_pixel_size))) {
312 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't malloc next_frame: %s\n", strerror(errno));
313 return 1;
315 #endif
316 if (fs) memset(frame_buffer, '\0', fb_line_len * fb_vinfo.yres);
318 return 0;
321 static int query_format(uint32_t format)
323 // open the device, etc.
324 if (fb_preinit(0)) return 0;
325 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
326 int fb_target_bpp = format & 0xff;
327 set_bpp(&fb_vinfo, fb_target_bpp);
328 fb_vinfo.xres_virtual = fb_vinfo.xres;
329 fb_vinfo.yres_virtual = fb_vinfo.yres;
330 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo))
331 // Needed for Intel framebuffer with 32 bpp
332 fb_vinfo.transp.length = fb_vinfo.transp.offset = 0;
333 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
334 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't put VSCREENINFO: %s\n", strerror(errno));
335 return 0;
337 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
338 fb_bpp = fb_vinfo.bits_per_pixel;
339 if (fb_bpp == 16)
340 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + fb_vinfo.blue.length;
341 if (fb_bpp == fb_target_bpp)
342 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE;
344 return 0;
347 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
348 unsigned char *srca, int stride)
350 unsigned char *dst;
351 int dstride;
353 #ifdef USE_CONVERT2FB
354 dst = center + (fb_line_len * y0) + (x0 * fb_pixel_size);
355 dstride = fb_line_len;
356 #else
357 dst = next_frame + (in_width * y0 + x0) * fb_pixel_size;
358 dstride = in_width * fb_pixel_size;
359 #endif
360 (*draw_alpha_p)(w, h, src, srca, stride, dst, dstride);
363 static void draw_osd(void)
365 vo_draw_text(in_width, in_height, draw_alpha);
368 // all csp support stride
369 static int draw_frame(uint8_t *src[]) { return 1; }
371 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
373 uint8_t *in = src[0];
374 #ifdef USE_CONVERT2FB
375 uint8_t *dest = center + (fb_line_len * y) + (x * fb_pixel_size);
376 int next = fb_line_len;
377 #else
378 uint8_t *dest = next_frame + (in_width * y + x) * fb_pixel_size;
379 int next = in_width * fb_pixel_size;
380 #endif
381 int i;
383 for (i = 0; i < h; i++) {
384 fast_memcpy(dest, in, w * fb_pixel_size);
385 dest += next;
386 in += stride[0];
388 return 0;
391 static void check_events(void)
395 static void flip_page(void)
397 #ifndef USE_CONVERT2FB
398 int i, out_offset = 0, in_offset = 0;
400 for (i = 0; i < in_height; i++) {
401 fast_memcpy(center + out_offset, next_frame + in_offset,
402 in_width * fb_pixel_size);
403 out_offset += fb_line_len;
404 in_offset += in_width * fb_pixel_size;
406 #endif
409 static void uninit(void)
411 if (fb_cmap_changed) {
412 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
413 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't restore original cmap\n");
414 fb_cmap_changed = 0;
416 if(next_frame) free(next_frame);
417 if (fb_dev_fd >= 0) {
418 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
419 mp_msg(MSGT_VO, MSGL_ERR, "[fbdev2] Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
420 close(fb_dev_fd);
421 fb_dev_fd = -1;
423 if(frame_buffer) munmap(frame_buffer, fb_size);
424 next_frame = frame_buffer = NULL;
425 fb_preinit(1); // so that later calls to preinit don't fail
428 static int control(uint32_t request, void *data)
430 switch (request) {
431 case VOCTRL_QUERY_FORMAT:
432 return query_format(*((uint32_t*)data));
434 return VO_NOTIMPL;