framebuffer_service.c revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <string.h>
21#include <fcntl.h>
22
23#include <cutils/fdevent.h>
24#include "adb.h"
25
26#include <linux/fb.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29
30/* TODO:
31** - grab the current buffer, not the first buffer
32** - sync with vsync to avoid tearing
33*/
34
35void framebuffer_service(int fd, void *cookie)
36{
37    struct fb_var_screeninfo vinfo;
38    int fb;
39    void *ptr = MAP_FAILED;
40    char x;
41
42    unsigned fbinfo[4];
43
44    fb = open("/dev/graphics/fb0", O_RDONLY);
45    if(fb < 0) goto done;
46
47    if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done;
48    fcntl(fb, F_SETFD, FD_CLOEXEC);
49
50    fbinfo[0] = 16;
51    fbinfo[1] = vinfo.xres * vinfo.yres * 2;
52    fbinfo[2] = vinfo.xres;
53    fbinfo[3] = vinfo.yres;
54
55    ptr = mmap(0, fbinfo[1], PROT_READ, MAP_SHARED, fb, 0);
56    if(ptr == MAP_FAILED) goto done;
57
58    if(writex(fd, fbinfo, sizeof(unsigned) * 4)) goto done;
59
60    for(;;) {
61        if(readx(fd, &x, 1)) goto done;
62        if(writex(fd, ptr, fbinfo[1])) goto done;
63    }
64
65done:
66    if(ptr != MAP_FAILED) munmap(ptr, fbinfo[1]);
67    if(fb >= 0) close(fb);
68    close(fd);
69}
70
71