blob: 9d17d2cacc59bd2f5b2c1c4c7a5456a467ec8abc [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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
Yuriy Zabroda5a536ef2012-07-17 14:52:47 +030017#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080018#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <linux/fb.h>
Dan Albert76649012015-02-24 15:51:19 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <sys/ioctl.h>
24#include <sys/mman.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <sys/types.h>
26#include <sys/wait.h>
27#include <unistd.h>
28
29#include "sysdeps.h"
30
31#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080032#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35/* TODO:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036** - sync with vsync to avoid tearing
37*/
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070038/* 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));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
57void framebuffer_service(int fd, void *cookie)
58{
Rebecca Schultz Zavin154b7d72009-09-15 21:06:12 -070059 struct fbinfo fbinfo;
Chris Dearman85373f42013-09-25 02:19:40 -070060 unsigned int i, bsize;
Mathias Agopian0715f912010-09-26 18:44:28 -070061 char buf[640];
62 int fd_screencap;
63 int w, h, f;
64 int fds[2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Nick Kralevich8fcb6312014-06-05 20:26:25 -070066 if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Mathias Agopian0715f912010-09-26 18:44:28 -070068 pid_t pid = fork();
69 if (pid < 0) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Mathias Agopian0715f912010-09-26 18:44:28 -070071 if (pid == 0) {
72 dup2(fds[1], STDOUT_FILENO);
Dan Albert76649012015-02-24 15:51:19 -080073 adb_close(fds[0]);
74 adb_close(fds[1]);
Mathias Agopian0715f912010-09-26 18:44:28 -070075 const char* command = "screencap";
76 const char *args[2] = {command, NULL};
77 execvp(command, (char**)args);
78 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 }
80
Dan Albert76649012015-02-24 15:51:19 -080081 adb_close(fds[1]);
Mathias Agopian0715f912010-09-26 18:44:28 -070082 fd_screencap = fds[0];
83
84 /* read w, h & format */
Dan Albertcc731cc2015-02-24 21:26:58 -080085 if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
86 if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
87 if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -070088
Mathias Agopian0715f912010-09-26 18:44:28 -070089 fbinfo.version = DDMS_RAWIMAGE_VERSION;
Mathias Agopianc1fbf7c2011-02-08 20:11:33 -080090 /* see hardware/hardware.h */
91 switch (f) {
92 case 1: /* RGBA_8888 */
93 fbinfo.bpp = 32;
94 fbinfo.size = w * h * 4;
95 fbinfo.width = w;
96 fbinfo.height = h;
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;
105 break;
106 case 2: /* RGBX_8888 */
107 fbinfo.bpp = 32;
108 fbinfo.size = w * h * 4;
109 fbinfo.width = w;
110 fbinfo.height = h;
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;
119 break;
120 case 3: /* RGB_888 */
121 fbinfo.bpp = 24;
122 fbinfo.size = w * h * 3;
123 fbinfo.width = w;
124 fbinfo.height = h;
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;
133 break;
134 case 4: /* RGB_565 */
135 fbinfo.bpp = 16;
136 fbinfo.size = w * h * 2;
137 fbinfo.width = w;
138 fbinfo.height = h;
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;
147 break;
148 case 5: /* BGRA_8888 */
149 fbinfo.bpp = 32;
150 fbinfo.size = w * h * 4;
151 fbinfo.width = w;
152 fbinfo.height = h;
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;
161 break;
162 default:
163 goto done;
164 }
Mathias Agopian0715f912010-09-26 18:44:28 -0700165
166 /* write header */
Dan Albertcc731cc2015-02-24 21:26:58 -0800167 if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -0700168
169 /* write data */
Chris Dearman85373f42013-09-25 02:19:40 -0700170 for(i = 0; i < fbinfo.size; i += bsize) {
171 bsize = sizeof(buf);
172 if (i + bsize > fbinfo.size)
173 bsize = fbinfo.size - i;
Dan Albertcc731cc2015-02-24 21:26:58 -0800174 if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
175 if(!WriteFdExactly(fd, buf, bsize)) goto done;
Mathias Agopian0715f912010-09-26 18:44:28 -0700176 }
Rebecca Schultz Zavin04bee292009-09-09 21:39:41 -0700177
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178done:
Dan Albert76649012015-02-24 15:51:19 -0800179 adb_close(fds[0]);
Bao Haojuncdb1b1b2014-05-14 21:03:48 +0800180
181 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
Chris Dearman85373f42013-09-25 02:19:40 -0700182pipefail:
Dan Albert76649012015-02-24 15:51:19 -0800183 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184}