blob: 2f456947082821020a54cee4be3760d66f7a586e [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*/
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070033/* 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));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52void framebuffer_service(int fd, void *cookie)
53{
54 struct fb_var_screeninfo vinfo;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070055 int fb, offset;
56 char x[256];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070058 struct fbinfo fbinfo;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070059 unsigned i, bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
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
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070067 bytespp = vinfo.bits_per_pixel / 8;
68
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070069 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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070083 /* 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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070090 offset += vinfo.xres * vinfo.yoffset * bytespp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070092 if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070093
94 lseek(fb, offset, SEEK_SET);
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070095 for(i = 0; i < fbinfo.size; i += 256) {
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -070096 if(readx(fb, &x, 256)) goto done;
97 if(writex(fd, &x, 256)) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 }
99
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -0700100 if(readx(fb, &x, fbinfo.size % 256)) goto done;
101 if(writex(fd, &x, fbinfo.size % 256)) goto done;
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -0700102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103done:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 if(fb >= 0) close(fb);
105 close(fd);
106}