The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
| 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 23 | #include "fdevent.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include "adb.h" |
| 25 | |
| 26 | #include <linux/fb.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/mman.h> |
| 29 | |
| 30 | /* TODO: |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | ** - sync with vsync to avoid tearing |
| 32 | */ |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 33 | /* This version number defines the format of the fbinfo struct. |
| 34 | It must match versioning in ddms where this data is consumed. */ |
| 35 | #define DDMS_RAWIMAGE_VERSION 1 |
| 36 | struct fbinfo { |
| 37 | unsigned int version; |
| 38 | unsigned int bpp; |
| 39 | unsigned int size; |
| 40 | unsigned int width; |
| 41 | unsigned int height; |
| 42 | unsigned int red_offset; |
| 43 | unsigned int red_length; |
| 44 | unsigned int blue_offset; |
| 45 | unsigned int blue_length; |
| 46 | unsigned int green_offset; |
| 47 | unsigned int green_length; |
| 48 | unsigned int alpha_offset; |
| 49 | unsigned int alpha_length; |
| 50 | } __attribute__((packed)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
| 52 | void framebuffer_service(int fd, void *cookie) |
| 53 | { |
| 54 | struct fb_var_screeninfo vinfo; |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 55 | int fb, offset; |
| 56 | char x[256]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 58 | struct fbinfo fbinfo; |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 59 | unsigned i, bytespp; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
| 61 | fb = open("/dev/graphics/fb0", O_RDONLY); |
| 62 | if(fb < 0) goto done; |
| 63 | |
| 64 | if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done; |
| 65 | fcntl(fb, F_SETFD, FD_CLOEXEC); |
| 66 | |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 67 | bytespp = vinfo.bits_per_pixel / 8; |
| 68 | |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 69 | fbinfo.version = DDMS_RAWIMAGE_VERSION; |
| 70 | fbinfo.bpp = vinfo.bits_per_pixel; |
| 71 | fbinfo.size = vinfo.xres * vinfo.yres * bytespp; |
| 72 | fbinfo.width = vinfo.xres; |
| 73 | fbinfo.height = vinfo.yres; |
| 74 | fbinfo.red_offset = vinfo.red.offset; |
| 75 | fbinfo.red_length = vinfo.red.length; |
| 76 | fbinfo.green_offset = vinfo.green.offset; |
| 77 | fbinfo.green_length = vinfo.green.length; |
| 78 | fbinfo.blue_offset = vinfo.blue.offset; |
| 79 | fbinfo.blue_length = vinfo.blue.length; |
| 80 | fbinfo.alpha_offset = vinfo.transp.offset; |
| 81 | fbinfo.alpha_length = vinfo.transp.length; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 83 | /* HACK: for several of our 3d cores a specific alignment |
| 84 | * is required so the start of the fb may not be an integer number of lines |
| 85 | * from the base. As a result we are storing the additional offset in |
| 86 | * xoffset. This is not the correct usage for xoffset, it should be added |
| 87 | * to each line, not just once at the beginning */ |
| 88 | offset = vinfo.xoffset * bytespp; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 90 | offset += vinfo.xres * vinfo.yoffset * bytespp; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 92 | if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done; |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 93 | |
| 94 | lseek(fb, offset, SEEK_SET); |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 95 | for(i = 0; i < fbinfo.size; i += 256) { |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 96 | if(readx(fb, &x, 256)) goto done; |
| 97 | if(writex(fd, &x, 256)) goto done; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Rebecca Schultz Zavin | 69f4c10 | 2009-09-15 21:06:12 -0700 | [diff] [blame] | 100 | if(readx(fb, &x, fbinfo.size % 256)) goto done; |
| 101 | if(writex(fd, &x, fbinfo.size % 256)) goto done; |
Rebecca Schultz Zavin | c1fc0ca | 2009-09-09 21:39:41 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | done: |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | if(fb >= 0) close(fb); |
| 105 | close(fd); |
| 106 | } |