blob: 1c5cd2fe310d509c5b2cb15ffd9c9371c806da39 [file] [log] [blame]
xj123f7cc2022-06-06 11:35:21 +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>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
26#include "fdevent.h"
27#include "adb.h"
28
29#include <linux/fb.h>
30#include <sys/ioctl.h>
31#include <sys/mman.h>
32
33#include <cutils/fs.h>
34
35/* TODO:
36** - sync with vsync to avoid tearing
37*/
38/* This version number defines the format of the fbinfo struct.
39 It must match versioning in ddms where this data is consumed. */
40#define DDMS_RAWIMAGE_VERSION 1
41struct fbinfo {
42 unsigned int version;
43 unsigned int bpp;
44 unsigned int size;
45 unsigned int width;
46 unsigned int height;
47 unsigned int red_offset;
48 unsigned int red_length;
49 unsigned int blue_offset;
50 unsigned int blue_length;
51 unsigned int green_offset;
52 unsigned int green_length;
53 unsigned int alpha_offset;
54 unsigned int alpha_length;
55} __attribute__((packed));
56
57void framebuffer_service(int fd, void *cookie)
58{
59 struct fbinfo fbinfo;
60 unsigned int i, bsize;
61 char buf[640];
62 int fd_screencap;
63 int w, h, f;
64 int fds[2];
65
66 if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
67
68 pid_t pid = fork();
69 if (pid < 0) goto done;
70
71 if (pid == 0) {
72 dup2(fds[1], STDOUT_FILENO);
73 close(fds[0]);
74 close(fds[1]);
75 const char* command = "screencap";
76 const char *args[2] = {command, NULL};
77 execvp(command, (char**)args);
78 exit(1);
79 }
80
81 fd_screencap = fds[0];
82
83 /* read w, h & format */
84 if(readx(fd_screencap, &w, 4)) goto done;
85 if(readx(fd_screencap, &h, 4)) goto done;
86 if(readx(fd_screencap, &f, 4)) goto done;
87
88 fbinfo.version = DDMS_RAWIMAGE_VERSION;
89 /* see hardware/hardware.h */
90 switch (f) {
91 case 1: /* RGBA_8888 */
92 fbinfo.bpp = 32;
93 fbinfo.size = w * h * 4;
94 fbinfo.width = w;
95 fbinfo.height = h;
96 fbinfo.red_offset = 0;
97 fbinfo.red_length = 8;
98 fbinfo.green_offset = 8;
99 fbinfo.green_length = 8;
100 fbinfo.blue_offset = 16;
101 fbinfo.blue_length = 8;
102 fbinfo.alpha_offset = 24;
103 fbinfo.alpha_length = 8;
104 break;
105 case 2: /* RGBX_8888 */
106 fbinfo.bpp = 32;
107 fbinfo.size = w * h * 4;
108 fbinfo.width = w;
109 fbinfo.height = h;
110 fbinfo.red_offset = 0;
111 fbinfo.red_length = 8;
112 fbinfo.green_offset = 8;
113 fbinfo.green_length = 8;
114 fbinfo.blue_offset = 16;
115 fbinfo.blue_length = 8;
116 fbinfo.alpha_offset = 24;
117 fbinfo.alpha_length = 0;
118 break;
119 case 3: /* RGB_888 */
120 fbinfo.bpp = 24;
121 fbinfo.size = w * h * 3;
122 fbinfo.width = w;
123 fbinfo.height = h;
124 fbinfo.red_offset = 0;
125 fbinfo.red_length = 8;
126 fbinfo.green_offset = 8;
127 fbinfo.green_length = 8;
128 fbinfo.blue_offset = 16;
129 fbinfo.blue_length = 8;
130 fbinfo.alpha_offset = 24;
131 fbinfo.alpha_length = 0;
132 break;
133 case 4: /* RGB_565 */
134 fbinfo.bpp = 16;
135 fbinfo.size = w * h * 2;
136 fbinfo.width = w;
137 fbinfo.height = h;
138 fbinfo.red_offset = 11;
139 fbinfo.red_length = 5;
140 fbinfo.green_offset = 5;
141 fbinfo.green_length = 6;
142 fbinfo.blue_offset = 0;
143 fbinfo.blue_length = 5;
144 fbinfo.alpha_offset = 0;
145 fbinfo.alpha_length = 0;
146 break;
147 case 5: /* BGRA_8888 */
148 fbinfo.bpp = 32;
149 fbinfo.size = w * h * 4;
150 fbinfo.width = w;
151 fbinfo.height = h;
152 fbinfo.red_offset = 16;
153 fbinfo.red_length = 8;
154 fbinfo.green_offset = 8;
155 fbinfo.green_length = 8;
156 fbinfo.blue_offset = 0;
157 fbinfo.blue_length = 8;
158 fbinfo.alpha_offset = 24;
159 fbinfo.alpha_length = 8;
160 break;
161 default:
162 goto done;
163 }
164
165 /* write header */
166 if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
167
168 /* write data */
169 for(i = 0; i < fbinfo.size; i += bsize) {
170 bsize = sizeof(buf);
171 if (i + bsize > fbinfo.size)
172 bsize = fbinfo.size - i;
173 if(readx(fd_screencap, buf, bsize)) goto done;
174 if(writex(fd, buf, bsize)) goto done;
175 }
176
177done:
178 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
179
180 close(fds[0]);
181 close(fds[1]);
182pipefail:
183 close(fd);
184}