blob: cb48d41477bc77992dfa8841e10e067cfe1ed2c9 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +020023#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include "adb.h"
25
26#include <linux/fb.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29
30/* TODO:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031** - sync with vsync to avoid tearing
32*/
33
34void framebuffer_service(int fd, void *cookie)
35{
36 struct fb_var_screeninfo vinfo;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070037 int fb, offset;
38 char x[256];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40 unsigned fbinfo[4];
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070041 unsigned i, bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43 fb = open("/dev/graphics/fb0", O_RDONLY);
44 if(fb < 0) goto done;
45
46 if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done;
47 fcntl(fb, F_SETFD, FD_CLOEXEC);
48
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070049 bytespp = vinfo.bits_per_pixel / 8;
50
51 fbinfo[0] = vinfo.bits_per_pixel;
52 fbinfo[1] = vinfo.xres * vinfo.yres * bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053 fbinfo[2] = vinfo.xres;
54 fbinfo[3] = vinfo.yres;
55
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070056 /* HACK: for several of our 3d cores a specific alignment
57 * is required so the start of the fb may not be an integer number of lines
58 * from the base. As a result we are storing the additional offset in
59 * xoffset. This is not the correct usage for xoffset, it should be added
60 * to each line, not just once at the beginning */
61 offset = vinfo.xoffset * bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070063 offset += vinfo.xres * vinfo.yoffset * bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070065 if(writex(fd, fbinfo, sizeof(fbinfo))) goto done;
66
67 lseek(fb, offset, SEEK_SET);
68 for(i = 0; i < fbinfo[1]; i += 256) {
69 if(readx(fb, &x, 256)) goto done;
70 if(writex(fd, &x, 256)) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071 }
72
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070073 if(readx(fb, &x, fbinfo[1] % 256)) goto done;
74 if(writex(fd, &x, fbinfo[1] % 256)) goto done;
75
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076done:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 if(fb >= 0) close(fb);
78 close(fd);
79}