framebuffer_service.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include "fdevent.h"
  25. #include "adb.h"
  26. #include <linux/fb.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/mman.h>
  29. /*
  30. * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
  31. * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
  32. * not already defined, then define it here.
  33. */
  34. #ifndef TEMP_FAILURE_RETRY
  35. /* Used to retry syscalls that can return EINTR. */
  36. #define TEMP_FAILURE_RETRY(exp) ({ \
  37. typeof (exp) _rc; \
  38. do { \
  39. _rc = (exp); \
  40. } while (_rc == -1 && errno == EINTR); \
  41. _rc; })
  42. #endif
  43. /* TODO:
  44. ** - sync with vsync to avoid tearing
  45. */
  46. /* This version number defines the format of the fbinfo struct.
  47. It must match versioning in ddms where this data is consumed. */
  48. #define DDMS_RAWIMAGE_VERSION 1
  49. struct fbinfo {
  50. unsigned int version;
  51. unsigned int bpp;
  52. unsigned int size;
  53. unsigned int width;
  54. unsigned int height;
  55. unsigned int red_offset;
  56. unsigned int red_length;
  57. unsigned int blue_offset;
  58. unsigned int blue_length;
  59. unsigned int green_offset;
  60. unsigned int green_length;
  61. unsigned int alpha_offset;
  62. unsigned int alpha_length;
  63. } __attribute__((packed));
  64. void framebuffer_service(int fd, void *cookie)
  65. {
  66. struct fbinfo fbinfo;
  67. unsigned int i;
  68. char buf[1024];
  69. int fd_screencap;
  70. int w, h, f;
  71. int fds[2];
  72. if (pipe(fds) < 0) goto done;
  73. pid_t pid = fork();
  74. if (pid < 0) goto done;
  75. if (pid == 0) {
  76. dup2(fds[1], STDOUT_FILENO);
  77. close(fds[0]);
  78. close(fds[1]);
  79. const char* command = "screencap";
  80. const char *args[2] = {command, NULL};
  81. execvp(command, (char**)args);
  82. exit(1);
  83. }
  84. fd_screencap = fds[0];
  85. /* read w, h & format */
  86. if(readx(fd_screencap, &w, 4)) goto done;
  87. if(readx(fd_screencap, &h, 4)) goto done;
  88. if(readx(fd_screencap, &f, 4)) goto done;
  89. fbinfo.version = DDMS_RAWIMAGE_VERSION;
  90. /* 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. }
  165. /* write header */
  166. if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
  167. /* write data */
  168. for(i = 0; i < fbinfo.size; i += sizeof(buf)) {
  169. if(readx(fd_screencap, buf, sizeof(buf))) goto done;
  170. if(writex(fd, buf, sizeof(buf))) goto done;
  171. }
  172. if(readx(fd_screencap, buf, fbinfo.size % sizeof(buf))) goto done;
  173. if(writex(fd, buf, fbinfo.size % sizeof(buf))) goto done;
  174. done:
  175. TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
  176. close(fds[0]);
  177. close(fds[1]);
  178. close(fd);
  179. }