blob: 676f8e9db1914b8be089bf332993bd3f47f1a60f [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070017#include "framebuffer_service.h"
18
Yuriy Zabroda9c06b512012-07-17 14:52:47 +030019#include <errno.h>
Dan Albertb302d122015-02-24 15:51:19 -080020#include <fcntl.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <linux/fb.h>
Dan Albertb302d122015-02-24 15:51:19 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <sys/ioctl.h>
26#include <sys/mman.h>
Dan Albertb302d122015-02-24 15:51:19 -080027#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30
31#include "sysdeps.h"
32
33#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080034#include "adb_io.h"
Elliott Hughes0119a912018-10-22 17:02:51 -070035#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036
37/* TODO:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038** - sync with vsync to avoid tearing
39*/
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070040/* This version number defines the format of the fbinfo struct.
41 It must match versioning in ddms where this data is consumed. */
Romain Guye07c7402017-05-31 19:13:47 -070042#define DDMS_RAWIMAGE_VERSION 2
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070043struct fbinfo {
44 unsigned int version;
45 unsigned int bpp;
Romain Guye07c7402017-05-31 19:13:47 -070046 unsigned int colorSpace;
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070047 unsigned int size;
48 unsigned int width;
49 unsigned int height;
50 unsigned int red_offset;
51 unsigned int red_length;
52 unsigned int blue_offset;
53 unsigned int blue_length;
54 unsigned int green_offset;
55 unsigned int green_length;
56 unsigned int alpha_offset;
57 unsigned int alpha_length;
58} __attribute__((packed));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
Josh Gao4c28dde2018-07-25 16:51:59 -070060void framebuffer_service(unique_fd fd) {
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070061 struct fbinfo fbinfo;
Chris Dearmanc6c01442013-09-25 02:19:40 -070062 unsigned int i, bsize;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070063 char buf[640];
64 int fd_screencap;
Romain Guye07c7402017-05-31 19:13:47 -070065 int w, h, f, c;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070066 int fds[2];
Dan Albertf30d73c2015-02-25 17:51:28 -080067 pid_t pid;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080068
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070069 if (pipe2(fds, O_CLOEXEC) < 0) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070
Dan Albertf30d73c2015-02-25 17:51:28 -080071 pid = fork();
Mathias Agopian7a8195e2010-09-26 18:44:28 -070072 if (pid < 0) goto done;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073
Mathias Agopian7a8195e2010-09-26 18:44:28 -070074 if (pid == 0) {
75 dup2(fds[1], STDOUT_FILENO);
Dan Albertb302d122015-02-24 15:51:19 -080076 adb_close(fds[0]);
77 adb_close(fds[1]);
Mathias Agopian7a8195e2010-09-26 18:44:28 -070078 const char* command = "screencap";
Yi Kong86e67182018-07-13 18:15:16 -070079 const char *args[2] = {command, nullptr};
Mathias Agopian7a8195e2010-09-26 18:44:28 -070080 execvp(command, (char**)args);
Elliott Hughes0119a912018-10-22 17:02:51 -070081 perror_exit("exec screencap failed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 }
83
Dan Albertb302d122015-02-24 15:51:19 -080084 adb_close(fds[1]);
Mathias Agopian7a8195e2010-09-26 18:44:28 -070085 fd_screencap = fds[0];
86
Romain Guye07c7402017-05-31 19:13:47 -070087 /* read w, h, format & color space */
Dan Albert66a91b02015-02-24 21:26:58 -080088 if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
89 if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
90 if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
Romain Guye07c7402017-05-31 19:13:47 -070091 if(!ReadFdExactly(fd_screencap, &c, 4)) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070092
Mathias Agopian7a8195e2010-09-26 18:44:28 -070093 fbinfo.version = DDMS_RAWIMAGE_VERSION;
Romain Guye07c7402017-05-31 19:13:47 -070094 fbinfo.colorSpace = c;
Mathias Agopian3eb48e42011-02-08 20:11:33 -080095 /* see hardware/hardware.h */
96 switch (f) {
97 case 1: /* RGBA_8888 */
98 fbinfo.bpp = 32;
99 fbinfo.size = w * h * 4;
100 fbinfo.width = w;
101 fbinfo.height = h;
102 fbinfo.red_offset = 0;
103 fbinfo.red_length = 8;
104 fbinfo.green_offset = 8;
105 fbinfo.green_length = 8;
106 fbinfo.blue_offset = 16;
107 fbinfo.blue_length = 8;
108 fbinfo.alpha_offset = 24;
109 fbinfo.alpha_length = 8;
110 break;
111 case 2: /* RGBX_8888 */
112 fbinfo.bpp = 32;
113 fbinfo.size = w * h * 4;
114 fbinfo.width = w;
115 fbinfo.height = h;
116 fbinfo.red_offset = 0;
117 fbinfo.red_length = 8;
118 fbinfo.green_offset = 8;
119 fbinfo.green_length = 8;
120 fbinfo.blue_offset = 16;
121 fbinfo.blue_length = 8;
122 fbinfo.alpha_offset = 24;
123 fbinfo.alpha_length = 0;
124 break;
125 case 3: /* RGB_888 */
126 fbinfo.bpp = 24;
127 fbinfo.size = w * h * 3;
128 fbinfo.width = w;
129 fbinfo.height = h;
130 fbinfo.red_offset = 0;
131 fbinfo.red_length = 8;
132 fbinfo.green_offset = 8;
133 fbinfo.green_length = 8;
134 fbinfo.blue_offset = 16;
135 fbinfo.blue_length = 8;
136 fbinfo.alpha_offset = 24;
137 fbinfo.alpha_length = 0;
138 break;
139 case 4: /* RGB_565 */
140 fbinfo.bpp = 16;
141 fbinfo.size = w * h * 2;
142 fbinfo.width = w;
143 fbinfo.height = h;
144 fbinfo.red_offset = 11;
145 fbinfo.red_length = 5;
146 fbinfo.green_offset = 5;
147 fbinfo.green_length = 6;
148 fbinfo.blue_offset = 0;
149 fbinfo.blue_length = 5;
150 fbinfo.alpha_offset = 0;
151 fbinfo.alpha_length = 0;
152 break;
153 case 5: /* BGRA_8888 */
154 fbinfo.bpp = 32;
155 fbinfo.size = w * h * 4;
156 fbinfo.width = w;
157 fbinfo.height = h;
158 fbinfo.red_offset = 16;
159 fbinfo.red_length = 8;
160 fbinfo.green_offset = 8;
161 fbinfo.green_length = 8;
162 fbinfo.blue_offset = 0;
163 fbinfo.blue_length = 8;
164 fbinfo.alpha_offset = 24;
165 fbinfo.alpha_length = 8;
166 break;
167 default:
168 goto done;
169 }
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700170
171 /* write header */
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700172 if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700173
174 /* write data */
Chris Dearmanc6c01442013-09-25 02:19:40 -0700175 for(i = 0; i < fbinfo.size; i += bsize) {
176 bsize = sizeof(buf);
177 if (i + bsize > fbinfo.size)
178 bsize = fbinfo.size - i;
Dan Albert66a91b02015-02-24 21:26:58 -0800179 if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700180 if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700181 }
Rebecca Schultz Zavinc1fc0ca2009-09-09 21:39:41 -0700182
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183done:
Dan Albertb302d122015-02-24 15:51:19 -0800184 adb_close(fds[0]);
Bao Haojunb38be942014-05-14 21:03:48 +0800185
Yi Kong86e67182018-07-13 18:15:16 -0700186 TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187}