blob: e42dcbf05d41c6a69a497a2c4a3a966388eb6d1c [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>
Yuriy Zabroda5a536ef2012-07-17 14:52:47 +030022#include <errno.h>
23#include <sys/types.h>
24#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +020026#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include "adb.h"
28
29#include <linux/fb.h>
30#include <sys/ioctl.h>
31#include <sys/mman.h>
32
33/* TODO:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034** - sync with vsync to avoid tearing
35*/
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070036/* This version number defines the format of the fbinfo struct.
37 It must match versioning in ddms where this data is consumed. */
38#define DDMS_RAWIMAGE_VERSION 1
39struct fbinfo {
40 unsigned int version;
41 unsigned int bpp;
42 unsigned int size;
43 unsigned int width;
44 unsigned int height;
45 unsigned int red_offset;
46 unsigned int red_length;
47 unsigned int blue_offset;
48 unsigned int blue_length;
49 unsigned int green_offset;
50 unsigned int green_length;
51 unsigned int alpha_offset;
52 unsigned int alpha_length;
53} __attribute__((packed));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
55void framebuffer_service(int fd, void *cookie)
56{
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070057 struct fbinfo fbinfo;
Chris Dearman85373f42013-09-25 02:19:40 -070058 unsigned int i, bsize;
Mathias Agopian0715f912010-09-26 18:44:28 -070059 char buf[640];
60 int fd_screencap;
61 int w, h, f;
62 int fds[2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Chris Dearman85373f42013-09-25 02:19:40 -070064 if (pipe(fds) < 0) goto pipefail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Mathias Agopian0715f912010-09-26 18:44:28 -070066 pid_t pid = fork();
67 if (pid < 0) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Mathias Agopian0715f912010-09-26 18:44:28 -070069 if (pid == 0) {
70 dup2(fds[1], STDOUT_FILENO);
71 close(fds[0]);
72 close(fds[1]);
73 const char* command = "screencap";
74 const char *args[2] = {command, NULL};
75 execvp(command, (char**)args);
76 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 }
78
Bao Haojuncdb1b1b2014-05-14 21:03:48 +080079 close(fds[1]);
Mathias Agopian0715f912010-09-26 18:44:28 -070080 fd_screencap = fds[0];
81
82 /* read w, h & format */
83 if(readx(fd_screencap, &w, 4)) goto done;
84 if(readx(fd_screencap, &h, 4)) goto done;
85 if(readx(fd_screencap, &f, 4)) goto done;
86
Mathias Agopian0715f912010-09-26 18:44:28 -070087 fbinfo.version = DDMS_RAWIMAGE_VERSION;
Mathias Agopianc1fbf7c2011-02-08 20:11:33 -080088 /* see hardware/hardware.h */
89 switch (f) {
90 case 1: /* RGBA_8888 */
91 fbinfo.bpp = 32;
92 fbinfo.size = w * h * 4;
93 fbinfo.width = w;
94 fbinfo.height = h;
95 fbinfo.red_offset = 0;
96 fbinfo.red_length = 8;
97 fbinfo.green_offset = 8;
98 fbinfo.green_length = 8;
99 fbinfo.blue_offset = 16;
100 fbinfo.blue_length = 8;
101 fbinfo.alpha_offset = 24;
102 fbinfo.alpha_length = 8;
103 break;
104 case 2: /* RGBX_8888 */
105 fbinfo.bpp = 32;
106 fbinfo.size = w * h * 4;
107 fbinfo.width = w;
108 fbinfo.height = h;
109 fbinfo.red_offset = 0;
110 fbinfo.red_length = 8;
111 fbinfo.green_offset = 8;
112 fbinfo.green_length = 8;
113 fbinfo.blue_offset = 16;
114 fbinfo.blue_length = 8;
115 fbinfo.alpha_offset = 24;
116 fbinfo.alpha_length = 0;
117 break;
118 case 3: /* RGB_888 */
119 fbinfo.bpp = 24;
120 fbinfo.size = w * h * 3;
121 fbinfo.width = w;
122 fbinfo.height = h;
123 fbinfo.red_offset = 0;
124 fbinfo.red_length = 8;
125 fbinfo.green_offset = 8;
126 fbinfo.green_length = 8;
127 fbinfo.blue_offset = 16;
128 fbinfo.blue_length = 8;
129 fbinfo.alpha_offset = 24;
130 fbinfo.alpha_length = 0;
131 break;
132 case 4: /* RGB_565 */
133 fbinfo.bpp = 16;
134 fbinfo.size = w * h * 2;
135 fbinfo.width = w;
136 fbinfo.height = h;
137 fbinfo.red_offset = 11;
138 fbinfo.red_length = 5;
139 fbinfo.green_offset = 5;
140 fbinfo.green_length = 6;
141 fbinfo.blue_offset = 0;
142 fbinfo.blue_length = 5;
143 fbinfo.alpha_offset = 0;
144 fbinfo.alpha_length = 0;
145 break;
146 case 5: /* BGRA_8888 */
147 fbinfo.bpp = 32;
148 fbinfo.size = w * h * 4;
149 fbinfo.width = w;
150 fbinfo.height = h;
151 fbinfo.red_offset = 16;
152 fbinfo.red_length = 8;
153 fbinfo.green_offset = 8;
154 fbinfo.green_length = 8;
155 fbinfo.blue_offset = 0;
156 fbinfo.blue_length = 8;
157 fbinfo.alpha_offset = 24;
158 fbinfo.alpha_length = 8;
159 break;
160 default:
161 goto done;
162 }
Mathias Agopian0715f912010-09-26 18:44:28 -0700163
164 /* write header */
165 if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
166
167 /* write data */
Chris Dearman85373f42013-09-25 02:19:40 -0700168 for(i = 0; i < fbinfo.size; i += bsize) {
169 bsize = sizeof(buf);
170 if (i + bsize > fbinfo.size)
171 bsize = fbinfo.size - i;
172 if(readx(fd_screencap, buf, bsize)) goto done;
173 if(writex(fd, buf, bsize)) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -0700174 }
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -0700175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176done:
Mathias Agopian0715f912010-09-26 18:44:28 -0700177 close(fds[0]);
Bao Haojuncdb1b1b2014-05-14 21:03:48 +0800178
179 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
Chris Dearman85373f42013-09-25 02:19:40 -0700180pipefail:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 close(fd);
182}