2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/fb.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
2012-07-17 13:52:47 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "sysdeps.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "adb.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "fdevent.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
** - sync with vsync to avoid tearing
|
|
|
|
*/
|
2009-09-16 06:06:12 +02:00
|
|
|
/* This version number defines the format of the fbinfo struct.
|
|
|
|
It must match versioning in ddms where this data is consumed. */
|
|
|
|
#define DDMS_RAWIMAGE_VERSION 1
|
|
|
|
struct fbinfo {
|
|
|
|
unsigned int version;
|
|
|
|
unsigned int bpp;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int red_offset;
|
|
|
|
unsigned int red_length;
|
|
|
|
unsigned int blue_offset;
|
|
|
|
unsigned int blue_length;
|
|
|
|
unsigned int green_offset;
|
|
|
|
unsigned int green_length;
|
|
|
|
unsigned int alpha_offset;
|
|
|
|
unsigned int alpha_length;
|
|
|
|
} __attribute__((packed));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
void framebuffer_service(int fd, void *cookie)
|
|
|
|
{
|
2009-09-16 06:06:12 +02:00
|
|
|
struct fbinfo fbinfo;
|
2013-09-25 11:19:40 +02:00
|
|
|
unsigned int i, bsize;
|
2010-09-27 03:44:28 +02:00
|
|
|
char buf[640];
|
|
|
|
int fd_screencap;
|
|
|
|
int w, h, f;
|
|
|
|
int fds[2];
|
2015-02-26 02:51:28 +01:00
|
|
|
pid_t pid;
|
2010-09-27 03:44:28 +02:00
|
|
|
|
2014-06-06 05:26:25 +02:00
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
|
2010-09-27 03:44:28 +02:00
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
pid = fork();
|
2010-09-27 03:44:28 +02:00
|
|
|
if (pid < 0) goto done;
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
dup2(fds[1], STDOUT_FILENO);
|
2015-02-25 00:51:19 +01:00
|
|
|
adb_close(fds[0]);
|
|
|
|
adb_close(fds[1]);
|
2010-09-27 03:44:28 +02:00
|
|
|
const char* command = "screencap";
|
|
|
|
const char *args[2] = {command, NULL};
|
|
|
|
execvp(command, (char**)args);
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
adb_close(fds[1]);
|
2010-09-27 03:44:28 +02:00
|
|
|
fd_screencap = fds[0];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-09-27 03:44:28 +02:00
|
|
|
/* read w, h & format */
|
2015-02-25 06:26:58 +01:00
|
|
|
if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
|
|
|
|
if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
|
|
|
|
if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
|
2009-09-10 06:39:41 +02:00
|
|
|
|
2009-09-16 06:06:12 +02:00
|
|
|
fbinfo.version = DDMS_RAWIMAGE_VERSION;
|
2011-02-09 05:11:33 +01:00
|
|
|
/* see hardware/hardware.h */
|
|
|
|
switch (f) {
|
|
|
|
case 1: /* RGBA_8888 */
|
|
|
|
fbinfo.bpp = 32;
|
|
|
|
fbinfo.size = w * h * 4;
|
|
|
|
fbinfo.width = w;
|
|
|
|
fbinfo.height = h;
|
|
|
|
fbinfo.red_offset = 0;
|
|
|
|
fbinfo.red_length = 8;
|
|
|
|
fbinfo.green_offset = 8;
|
|
|
|
fbinfo.green_length = 8;
|
|
|
|
fbinfo.blue_offset = 16;
|
|
|
|
fbinfo.blue_length = 8;
|
|
|
|
fbinfo.alpha_offset = 24;
|
|
|
|
fbinfo.alpha_length = 8;
|
|
|
|
break;
|
|
|
|
case 2: /* RGBX_8888 */
|
|
|
|
fbinfo.bpp = 32;
|
|
|
|
fbinfo.size = w * h * 4;
|
|
|
|
fbinfo.width = w;
|
|
|
|
fbinfo.height = h;
|
|
|
|
fbinfo.red_offset = 0;
|
|
|
|
fbinfo.red_length = 8;
|
|
|
|
fbinfo.green_offset = 8;
|
|
|
|
fbinfo.green_length = 8;
|
|
|
|
fbinfo.blue_offset = 16;
|
|
|
|
fbinfo.blue_length = 8;
|
|
|
|
fbinfo.alpha_offset = 24;
|
|
|
|
fbinfo.alpha_length = 0;
|
|
|
|
break;
|
|
|
|
case 3: /* RGB_888 */
|
|
|
|
fbinfo.bpp = 24;
|
|
|
|
fbinfo.size = w * h * 3;
|
|
|
|
fbinfo.width = w;
|
|
|
|
fbinfo.height = h;
|
|
|
|
fbinfo.red_offset = 0;
|
|
|
|
fbinfo.red_length = 8;
|
|
|
|
fbinfo.green_offset = 8;
|
|
|
|
fbinfo.green_length = 8;
|
|
|
|
fbinfo.blue_offset = 16;
|
|
|
|
fbinfo.blue_length = 8;
|
|
|
|
fbinfo.alpha_offset = 24;
|
|
|
|
fbinfo.alpha_length = 0;
|
|
|
|
break;
|
|
|
|
case 4: /* RGB_565 */
|
|
|
|
fbinfo.bpp = 16;
|
|
|
|
fbinfo.size = w * h * 2;
|
|
|
|
fbinfo.width = w;
|
|
|
|
fbinfo.height = h;
|
|
|
|
fbinfo.red_offset = 11;
|
|
|
|
fbinfo.red_length = 5;
|
|
|
|
fbinfo.green_offset = 5;
|
|
|
|
fbinfo.green_length = 6;
|
|
|
|
fbinfo.blue_offset = 0;
|
|
|
|
fbinfo.blue_length = 5;
|
|
|
|
fbinfo.alpha_offset = 0;
|
|
|
|
fbinfo.alpha_length = 0;
|
|
|
|
break;
|
|
|
|
case 5: /* BGRA_8888 */
|
|
|
|
fbinfo.bpp = 32;
|
|
|
|
fbinfo.size = w * h * 4;
|
|
|
|
fbinfo.width = w;
|
|
|
|
fbinfo.height = h;
|
|
|
|
fbinfo.red_offset = 16;
|
|
|
|
fbinfo.red_length = 8;
|
|
|
|
fbinfo.green_offset = 8;
|
|
|
|
fbinfo.green_length = 8;
|
|
|
|
fbinfo.blue_offset = 0;
|
|
|
|
fbinfo.blue_length = 8;
|
|
|
|
fbinfo.alpha_offset = 24;
|
|
|
|
fbinfo.alpha_length = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
2010-09-27 03:44:28 +02:00
|
|
|
|
|
|
|
/* write header */
|
2015-02-25 06:26:58 +01:00
|
|
|
if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-09-27 03:44:28 +02:00
|
|
|
/* write data */
|
2013-09-25 11:19:40 +02:00
|
|
|
for(i = 0; i < fbinfo.size; i += bsize) {
|
|
|
|
bsize = sizeof(buf);
|
|
|
|
if (i + bsize > fbinfo.size)
|
|
|
|
bsize = fbinfo.size - i;
|
2015-02-25 06:26:58 +01:00
|
|
|
if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
|
|
|
|
if(!WriteFdExactly(fd, buf, bsize)) goto done;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2009-09-10 06:39:41 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
done:
|
2015-02-25 00:51:19 +01:00
|
|
|
adb_close(fds[0]);
|
2014-05-14 15:03:48 +02:00
|
|
|
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
|
2013-09-25 11:19:40 +02:00
|
|
|
pipefail:
|
2015-02-25 00:51:19 +01:00
|
|
|
adb_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|