1 /*
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 "framebuffer_service.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <linux/fb.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30
31 #include "sysdeps.h"
32
33 #include "adb.h"
34 #include "adb_io.h"
35 #include "adb_utils.h"
36 #include "fdevent.h"
37
38 /* TODO:
39 ** - sync with vsync to avoid tearing
40 */
41 /* This version number defines the format of the fbinfo struct.
42 It must match versioning in ddms where this data is consumed. */
43 #define DDMS_RAWIMAGE_VERSION 2
44 struct fbinfo {
45 unsigned int version;
46 unsigned int bpp;
47 unsigned int colorSpace;
48 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));
60
framebuffer_service(unique_fd fd)61 void framebuffer_service(unique_fd fd) {
62 struct fbinfo fbinfo;
63 unsigned int i, bsize;
64 char buf[640];
65 int fd_screencap;
66 int w, h, f, c;
67 int fds[2];
68 pid_t pid;
69
70 if (pipe2(fds, O_CLOEXEC) < 0) return;
71
72 pid = fork();
73 if (pid < 0) goto done;
74
75 if (pid == 0) {
76 dup2(fds[1], STDOUT_FILENO);
77 adb_close(fds[0]);
78 adb_close(fds[1]);
79 const char* command = "screencap";
80 const char *args[2] = {command, nullptr};
81 execvp(command, (char**)args);
82 perror_exit("exec screencap failed");
83 }
84
85 adb_close(fds[1]);
86 fd_screencap = fds[0];
87
88 /* read w, h, format & color space */
89 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;
92 if(!ReadFdExactly(fd_screencap, &c, 4)) goto done;
93
94 fbinfo.version = DDMS_RAWIMAGE_VERSION;
95 fbinfo.colorSpace = c;
96 /* 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 }
171
172 /* write header */
173 if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
174
175 /* write data */
176 for(i = 0; i < fbinfo.size; i += bsize) {
177 bsize = sizeof(buf);
178 if (i + bsize > fbinfo.size)
179 bsize = fbinfo.size - i;
180 if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
181 if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
182 }
183
184 done:
185 adb_close(fds[0]);
186
187 TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
188 }
189