blob: 6c3a2252f4d4855165f05660932b60f7f37971d7 [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
Yuriy Zabroda5a536ef2012-07-17 14:52:47 +030017#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080018#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <linux/fb.h>
Dan Albert76649012015-02-24 15:51:19 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <sys/ioctl.h>
24#include <sys/mman.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <sys/types.h>
26#include <sys/wait.h>
27#include <unistd.h>
28
29#include "sysdeps.h"
30
31#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080032#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35/* TODO:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036** - sync with vsync to avoid tearing
37*/
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070038/* This version number defines the format of the fbinfo struct.
39 It must match versioning in ddms where this data is consumed. */
Romain Guy3a4e80a2017-05-31 19:13:47 -070040#define DDMS_RAWIMAGE_VERSION 2
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070041struct fbinfo {
42 unsigned int version;
43 unsigned int bpp;
Romain Guy3a4e80a2017-05-31 19:13:47 -070044 unsigned int colorSpace;
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070045 unsigned int size;
46 unsigned int width;
47 unsigned int height;
48 unsigned int red_offset;
49 unsigned int red_length;
50 unsigned int blue_offset;
51 unsigned int blue_length;
52 unsigned int green_offset;
53 unsigned int green_length;
54 unsigned int alpha_offset;
55 unsigned int alpha_length;
56} __attribute__((packed));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58void framebuffer_service(int fd, void *cookie)
59{
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070060 struct fbinfo fbinfo;
Chris Dearman85373f42013-09-25 02:19:40 -070061 unsigned int i, bsize;
Mathias Agopian0715f912010-09-26 18:44:28 -070062 char buf[640];
63 int fd_screencap;
Romain Guy3a4e80a2017-05-31 19:13:47 -070064 int w, h, f, c;
Mathias Agopian0715f912010-09-26 18:44:28 -070065 int fds[2];
Dan Albertbac34742015-02-25 17:51:28 -080066 pid_t pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Nick Kralevich8fcb6312014-06-05 20:26:25 -070068 if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Dan Albertbac34742015-02-25 17:51:28 -080070 pid = fork();
Mathias Agopian0715f912010-09-26 18:44:28 -070071 if (pid < 0) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Mathias Agopian0715f912010-09-26 18:44:28 -070073 if (pid == 0) {
74 dup2(fds[1], STDOUT_FILENO);
Dan Albert76649012015-02-24 15:51:19 -080075 adb_close(fds[0]);
76 adb_close(fds[1]);
Mathias Agopian0715f912010-09-26 18:44:28 -070077 const char* command = "screencap";
78 const char *args[2] = {command, NULL};
79 execvp(command, (char**)args);
80 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 }
82
Dan Albert76649012015-02-24 15:51:19 -080083 adb_close(fds[1]);
Mathias Agopian0715f912010-09-26 18:44:28 -070084 fd_screencap = fds[0];
85
Romain Guy3a4e80a2017-05-31 19:13:47 -070086 /* read w, h, format & color space */
Dan Albertcc731cc2015-02-24 21:26:58 -080087 if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
88 if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
89 if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
Romain Guy3a4e80a2017-05-31 19:13:47 -070090 if(!ReadFdExactly(fd_screencap, &c, 4)) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -070091
Mathias Agopian0715f912010-09-26 18:44:28 -070092 fbinfo.version = DDMS_RAWIMAGE_VERSION;
Romain Guy3a4e80a2017-05-31 19:13:47 -070093 fbinfo.colorSpace = c;
Mathias Agopianc1fbf7c2011-02-08 20:11:33 -080094 /* see hardware/hardware.h */
95 switch (f) {
96 case 1: /* RGBA_8888 */
97 fbinfo.bpp = 32;
98 fbinfo.size = w * h * 4;
99 fbinfo.width = w;
100 fbinfo.height = h;
101 fbinfo.red_offset = 0;
102 fbinfo.red_length = 8;
103 fbinfo.green_offset = 8;
104 fbinfo.green_length = 8;
105 fbinfo.blue_offset = 16;
106 fbinfo.blue_length = 8;
107 fbinfo.alpha_offset = 24;
108 fbinfo.alpha_length = 8;
109 break;
110 case 2: /* RGBX_8888 */
111 fbinfo.bpp = 32;
112 fbinfo.size = w * h * 4;
113 fbinfo.width = w;
114 fbinfo.height = h;
115 fbinfo.red_offset = 0;
116 fbinfo.red_length = 8;
117 fbinfo.green_offset = 8;
118 fbinfo.green_length = 8;
119 fbinfo.blue_offset = 16;
120 fbinfo.blue_length = 8;
121 fbinfo.alpha_offset = 24;
122 fbinfo.alpha_length = 0;
123 break;
124 case 3: /* RGB_888 */
125 fbinfo.bpp = 24;
126 fbinfo.size = w * h * 3;
127 fbinfo.width = w;
128 fbinfo.height = h;
129 fbinfo.red_offset = 0;
130 fbinfo.red_length = 8;
131 fbinfo.green_offset = 8;
132 fbinfo.green_length = 8;
133 fbinfo.blue_offset = 16;
134 fbinfo.blue_length = 8;
135 fbinfo.alpha_offset = 24;
136 fbinfo.alpha_length = 0;
137 break;
138 case 4: /* RGB_565 */
139 fbinfo.bpp = 16;
140 fbinfo.size = w * h * 2;
141 fbinfo.width = w;
142 fbinfo.height = h;
143 fbinfo.red_offset = 11;
144 fbinfo.red_length = 5;
145 fbinfo.green_offset = 5;
146 fbinfo.green_length = 6;
147 fbinfo.blue_offset = 0;
148 fbinfo.blue_length = 5;
149 fbinfo.alpha_offset = 0;
150 fbinfo.alpha_length = 0;
151 break;
152 case 5: /* BGRA_8888 */
153 fbinfo.bpp = 32;
154 fbinfo.size = w * h * 4;
155 fbinfo.width = w;
156 fbinfo.height = h;
157 fbinfo.red_offset = 16;
158 fbinfo.red_length = 8;
159 fbinfo.green_offset = 8;
160 fbinfo.green_length = 8;
161 fbinfo.blue_offset = 0;
162 fbinfo.blue_length = 8;
163 fbinfo.alpha_offset = 24;
164 fbinfo.alpha_length = 8;
165 break;
166 default:
167 goto done;
168 }
Mathias Agopian0715f912010-09-26 18:44:28 -0700169
170 /* write header */
Dan Albertcc731cc2015-02-24 21:26:58 -0800171 if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -0700172
173 /* write data */
Chris Dearman85373f42013-09-25 02:19:40 -0700174 for(i = 0; i < fbinfo.size; i += bsize) {
175 bsize = sizeof(buf);
176 if (i + bsize > fbinfo.size)
177 bsize = fbinfo.size - i;
Dan Albertcc731cc2015-02-24 21:26:58 -0800178 if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
179 if(!WriteFdExactly(fd, buf, bsize)) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -0700180 }
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -0700181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182done:
Dan Albert76649012015-02-24 15:51:19 -0800183 adb_close(fds[0]);
Bao Haojuncdb1b1b2014-05-14 21:03:48 +0800184
185 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
Chris Dearman85373f42013-09-25 02:19:40 -0700186pipefail:
Dan Albert76649012015-02-24 15:51:19 -0800187 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188}