blob: 2a6418ade9de9f901c02044c4382451e8489a2f4 [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"
Dan Albertb302d122015-02-24 15:51:19 -080036#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080037
38/* TODO:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039** - sync with vsync to avoid tearing
40*/
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070041/* This version number defines the format of the fbinfo struct.
42 It must match versioning in ddms where this data is consumed. */
Romain Guye07c7402017-05-31 19:13:47 -070043#define DDMS_RAWIMAGE_VERSION 2
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070044struct fbinfo {
45 unsigned int version;
46 unsigned int bpp;
Romain Guye07c7402017-05-31 19:13:47 -070047 unsigned int colorSpace;
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070048 unsigned int size;
49 unsigned int width;
50 unsigned int height;
51 unsigned int red_offset;
52 unsigned int red_length;
53 unsigned int blue_offset;
54 unsigned int blue_length;
55 unsigned int green_offset;
56 unsigned int green_length;
57 unsigned int alpha_offset;
58 unsigned int alpha_length;
59} __attribute__((packed));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
Josh Gao4c28dde2018-07-25 16:51:59 -070061void framebuffer_service(unique_fd fd) {
Rebecca Schultz Zavin69f4c102009-09-15 21:06:12 -070062 struct fbinfo fbinfo;
Chris Dearmanc6c01442013-09-25 02:19:40 -070063 unsigned int i, bsize;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070064 char buf[640];
65 int fd_screencap;
Romain Guye07c7402017-05-31 19:13:47 -070066 int w, h, f, c;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070067 int fds[2];
Dan Albertf30d73c2015-02-25 17:51:28 -080068 pid_t pid;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069
Luis Hector Chavezce7a2842018-07-18 19:40:12 -070070 if (pipe2(fds, O_CLOEXEC) < 0) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071
Dan Albertf30d73c2015-02-25 17:51:28 -080072 pid = fork();
Mathias Agopian7a8195e2010-09-26 18:44:28 -070073 if (pid < 0) goto done;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074
Mathias Agopian7a8195e2010-09-26 18:44:28 -070075 if (pid == 0) {
76 dup2(fds[1], STDOUT_FILENO);
Dan Albertb302d122015-02-24 15:51:19 -080077 adb_close(fds[0]);
78 adb_close(fds[1]);
Mathias Agopian7a8195e2010-09-26 18:44:28 -070079 const char* command = "screencap";
Yi Kong86e67182018-07-13 18:15:16 -070080 const char *args[2] = {command, nullptr};
Mathias Agopian7a8195e2010-09-26 18:44:28 -070081 execvp(command, (char**)args);
Elliott Hughes0119a912018-10-22 17:02:51 -070082 perror_exit("exec screencap failed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080083 }
84
Dan Albertb302d122015-02-24 15:51:19 -080085 adb_close(fds[1]);
Mathias Agopian7a8195e2010-09-26 18:44:28 -070086 fd_screencap = fds[0];
87
Romain Guye07c7402017-05-31 19:13:47 -070088 /* read w, h, format & color space */
Dan Albert66a91b02015-02-24 21:26:58 -080089 if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
90 if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
91 if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
Romain Guye07c7402017-05-31 19:13:47 -070092 if(!ReadFdExactly(fd_screencap, &c, 4)) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -070093
Mathias Agopian7a8195e2010-09-26 18:44:28 -070094 fbinfo.version = DDMS_RAWIMAGE_VERSION;
Romain Guye07c7402017-05-31 19:13:47 -070095 fbinfo.colorSpace = c;
Mathias Agopian3eb48e42011-02-08 20:11:33 -080096 /* see hardware/hardware.h */
97 switch (f) {
98 case 1: /* RGBA_8888 */
99 fbinfo.bpp = 32;
100 fbinfo.size = w * h * 4;
101 fbinfo.width = w;
102 fbinfo.height = h;
103 fbinfo.red_offset = 0;
104 fbinfo.red_length = 8;
105 fbinfo.green_offset = 8;
106 fbinfo.green_length = 8;
107 fbinfo.blue_offset = 16;
108 fbinfo.blue_length = 8;
109 fbinfo.alpha_offset = 24;
110 fbinfo.alpha_length = 8;
111 break;
112 case 2: /* RGBX_8888 */
113 fbinfo.bpp = 32;
114 fbinfo.size = w * h * 4;
115 fbinfo.width = w;
116 fbinfo.height = h;
117 fbinfo.red_offset = 0;
118 fbinfo.red_length = 8;
119 fbinfo.green_offset = 8;
120 fbinfo.green_length = 8;
121 fbinfo.blue_offset = 16;
122 fbinfo.blue_length = 8;
123 fbinfo.alpha_offset = 24;
124 fbinfo.alpha_length = 0;
125 break;
126 case 3: /* RGB_888 */
127 fbinfo.bpp = 24;
128 fbinfo.size = w * h * 3;
129 fbinfo.width = w;
130 fbinfo.height = h;
131 fbinfo.red_offset = 0;
132 fbinfo.red_length = 8;
133 fbinfo.green_offset = 8;
134 fbinfo.green_length = 8;
135 fbinfo.blue_offset = 16;
136 fbinfo.blue_length = 8;
137 fbinfo.alpha_offset = 24;
138 fbinfo.alpha_length = 0;
139 break;
140 case 4: /* RGB_565 */
141 fbinfo.bpp = 16;
142 fbinfo.size = w * h * 2;
143 fbinfo.width = w;
144 fbinfo.height = h;
145 fbinfo.red_offset = 11;
146 fbinfo.red_length = 5;
147 fbinfo.green_offset = 5;
148 fbinfo.green_length = 6;
149 fbinfo.blue_offset = 0;
150 fbinfo.blue_length = 5;
151 fbinfo.alpha_offset = 0;
152 fbinfo.alpha_length = 0;
153 break;
154 case 5: /* BGRA_8888 */
155 fbinfo.bpp = 32;
156 fbinfo.size = w * h * 4;
157 fbinfo.width = w;
158 fbinfo.height = h;
159 fbinfo.red_offset = 16;
160 fbinfo.red_length = 8;
161 fbinfo.green_offset = 8;
162 fbinfo.green_length = 8;
163 fbinfo.blue_offset = 0;
164 fbinfo.blue_length = 8;
165 fbinfo.alpha_offset = 24;
166 fbinfo.alpha_length = 8;
167 break;
168 default:
169 goto done;
170 }
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700171
172 /* write header */
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700173 if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700174
175 /* write data */
Chris Dearmanc6c01442013-09-25 02:19:40 -0700176 for(i = 0; i < fbinfo.size; i += bsize) {
177 bsize = sizeof(buf);
178 if (i + bsize > fbinfo.size)
179 bsize = fbinfo.size - i;
Dan Albert66a91b02015-02-24 21:26:58 -0800180 if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
Luis Hector Chavezce7a2842018-07-18 19:40:12 -0700181 if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
Mathias Agopian7a8195e2010-09-26 18:44:28 -0700182 }
Rebecca Schultz Zavinc1fc0ca2009-09-09 21:39:41 -0700183
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184done:
Dan Albertb302d122015-02-24 15:51:19 -0800185 adb_close(fds[0]);
Bao Haojunb38be942014-05-14 21:03:48 +0800186
Yi Kong86e67182018-07-13 18:15:16 -0700187 TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188}