2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/ioctl.h>
25 #include <sys/types.h>
33 #include "transport.h"
36 ** - sync with vsync to avoid tearing
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
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));
57 void framebuffer_service(int fd, void *cookie)
60 unsigned int i, bsize;
66 if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
69 if (pid < 0) goto done;
72 dup2(fds[1], STDOUT_FILENO);
75 const char* command = "screencap";
76 const char *args[2] = {command, NULL};
77 execvp(command, (char**)args);
82 fd_screencap = fds[0];
84 /* read w, h & format */
85 if(readx(fd_screencap, &w, 4)) goto done;
86 if(readx(fd_screencap, &h, 4)) goto done;
87 if(readx(fd_screencap, &f, 4)) goto done;
89 fbinfo.version = DDMS_RAWIMAGE_VERSION;
90 /* see hardware/hardware.h */
92 case 1: /* RGBA_8888 */
94 fbinfo.size = w * h * 4;
97 fbinfo.red_offset = 0;
98 fbinfo.red_length = 8;
99 fbinfo.green_offset = 8;
100 fbinfo.green_length = 8;
101 fbinfo.blue_offset = 16;
102 fbinfo.blue_length = 8;
103 fbinfo.alpha_offset = 24;
104 fbinfo.alpha_length = 8;
106 case 2: /* RGBX_8888 */
108 fbinfo.size = w * h * 4;
111 fbinfo.red_offset = 0;
112 fbinfo.red_length = 8;
113 fbinfo.green_offset = 8;
114 fbinfo.green_length = 8;
115 fbinfo.blue_offset = 16;
116 fbinfo.blue_length = 8;
117 fbinfo.alpha_offset = 24;
118 fbinfo.alpha_length = 0;
120 case 3: /* RGB_888 */
122 fbinfo.size = w * h * 3;
125 fbinfo.red_offset = 0;
126 fbinfo.red_length = 8;
127 fbinfo.green_offset = 8;
128 fbinfo.green_length = 8;
129 fbinfo.blue_offset = 16;
130 fbinfo.blue_length = 8;
131 fbinfo.alpha_offset = 24;
132 fbinfo.alpha_length = 0;
134 case 4: /* RGB_565 */
136 fbinfo.size = w * h * 2;
139 fbinfo.red_offset = 11;
140 fbinfo.red_length = 5;
141 fbinfo.green_offset = 5;
142 fbinfo.green_length = 6;
143 fbinfo.blue_offset = 0;
144 fbinfo.blue_length = 5;
145 fbinfo.alpha_offset = 0;
146 fbinfo.alpha_length = 0;
148 case 5: /* BGRA_8888 */
150 fbinfo.size = w * h * 4;
153 fbinfo.red_offset = 16;
154 fbinfo.red_length = 8;
155 fbinfo.green_offset = 8;
156 fbinfo.green_length = 8;
157 fbinfo.blue_offset = 0;
158 fbinfo.blue_length = 8;
159 fbinfo.alpha_offset = 24;
160 fbinfo.alpha_length = 8;
167 if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
170 for(i = 0; i < fbinfo.size; i += bsize) {
172 if (i + bsize > fbinfo.size)
173 bsize = fbinfo.size - i;
174 if(readx(fd_screencap, buf, bsize)) goto done;
175 if(writex(fd, buf, bsize)) goto done;
181 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));