framebuffer_service.c revision 154b7d7de4071ed73cde81eef3af47d1a24d7c6b
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 "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** - sync with vsync to avoid tearing
32*/
33/* This version number defines the format of the fbinfo struct.
34   It must match versioning in ddms where this data is consumed. */
35#define DDMS_RAWIMAGE_VERSION 1
36struct fbinfo {
37    unsigned int version;
38    unsigned int bpp;
39    unsigned int size;
40    unsigned int width;
41    unsigned int height;
42    unsigned int red_offset;
43    unsigned int red_length;
44    unsigned int blue_offset;
45    unsigned int blue_length;
46    unsigned int green_offset;
47    unsigned int green_length;
48    unsigned int alpha_offset;
49    unsigned int alpha_length;
50} __attribute__((packed));
51
52void framebuffer_service(int fd, void *cookie)
53{
54    struct fb_var_screeninfo vinfo;
55    int fb, offset;
56    char x[256];
57
58    struct fbinfo fbinfo;
59    unsigned i, bytespp;
60
61    fb = open("/dev/graphics/fb0", O_RDONLY);
62    if(fb < 0) goto done;
63
64    if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done;
65    fcntl(fb, F_SETFD, FD_CLOEXEC);
66
67    bytespp = vinfo.bits_per_pixel / 8;
68
69    fbinfo.version = DDMS_RAWIMAGE_VERSION;
70    fbinfo.bpp = vinfo.bits_per_pixel;
71    fbinfo.size = vinfo.xres * vinfo.yres * bytespp;
72    fbinfo.width = vinfo.xres;
73    fbinfo.height = vinfo.yres;
74    fbinfo.red_offset = vinfo.red.offset;
75    fbinfo.red_length = vinfo.red.length;
76    fbinfo.green_offset = vinfo.green.offset;
77    fbinfo.green_length = vinfo.green.length;
78    fbinfo.blue_offset = vinfo.blue.offset;
79    fbinfo.blue_length = vinfo.blue.length;
80    fbinfo.alpha_offset = vinfo.transp.offset;
81    fbinfo.alpha_length = vinfo.transp.length;
82
83    /* HACK: for several of our 3d cores a specific alignment
84     * is required so the start of the fb may not be an integer number of lines
85     * from the base.  As a result we are storing the additional offset in
86     * xoffset. This is not the correct usage for xoffset, it should be added
87     * to each line, not just once at the beginning */
88    offset = vinfo.xoffset * bytespp;
89
90    offset += vinfo.xres * vinfo.yoffset * bytespp;
91
92    if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
93
94    lseek(fb, offset, SEEK_SET);
95    for(i = 0; i < fbinfo.size; i += 256) {
96      if(readx(fb, &x, 256)) goto done;
97      if(writex(fd, &x, 256)) goto done;
98    }
99
100    if(readx(fb, &x, fbinfo.size % 256)) goto done;
101    if(writex(fd, &x, fbinfo.size % 256)) goto done;
102
103done:
104    if(fb >= 0) close(fb);
105    close(fd);
106}
107