Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <stdbool.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include <sys/cdefs.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <linux/fb.h> |
| 31 | #include <linux/kd.h> |
| 32 | |
| 33 | #include "minui.h" |
| 34 | #include "graphics.h" |
| 35 | #include <pixelflinger/pixelflinger.h> |
| 36 | |
| 37 | static GRSurface* fbdev_init(minui_backend*); |
| 38 | static GRSurface* fbdev_flip(minui_backend*); |
| 39 | static void fbdev_blank(minui_backend*, bool); |
| 40 | static void fbdev_exit(minui_backend*); |
| 41 | |
| 42 | static GRSurface gr_framebuffer[2]; |
| 43 | static bool double_buffered; |
| 44 | static GRSurface* gr_draw = NULL; |
| 45 | static int displayed_buffer; |
| 46 | |
| 47 | static fb_var_screeninfo vi; |
| 48 | static int fb_fd = -1; |
| 49 | static __u32 smem_len; |
| 50 | |
| 51 | static minui_backend my_backend = { |
| 52 | .init = fbdev_init, |
| 53 | .flip = fbdev_flip, |
| 54 | .blank = fbdev_blank, |
| 55 | .exit = fbdev_exit, |
| 56 | }; |
| 57 | |
| 58 | minui_backend* open_fbdev() { |
| 59 | return &my_backend; |
| 60 | } |
| 61 | |
| 62 | static void fbdev_blank(minui_backend* backend __unused, bool blank) |
| 63 | { |
| 64 | #if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS) |
| 65 | int fd; |
| 66 | char brightness[4]; |
| 67 | snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2); |
| 68 | |
| 69 | fd = open(TW_BRIGHTNESS_PATH, O_RDWR); |
| 70 | if (fd < 0) { |
| 71 | perror("cannot open LCD backlight"); |
| 72 | return; |
| 73 | } |
| 74 | write(fd, blank ? "000" : brightness, 3); |
| 75 | close(fd); |
| 76 | #else |
| 77 | #ifndef TW_NO_SCREEN_BLANK |
| 78 | int ret; |
| 79 | |
| 80 | ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 81 | if (ret < 0) |
| 82 | perror("ioctl(): blank"); |
| 83 | #endif |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | static void set_displayed_framebuffer(unsigned n) |
| 88 | { |
| 89 | if (n > 1 || !double_buffered) return; |
| 90 | |
| 91 | vi.yres_virtual = gr_framebuffer[0].height * 2; |
| 92 | vi.yoffset = n * gr_framebuffer[0].height; |
| 93 | vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; |
| 94 | if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 95 | perror("active fb swap failed"); |
| 96 | } |
| 97 | displayed_buffer = n; |
| 98 | } |
| 99 | |
| 100 | static GRSurface* fbdev_init(minui_backend* backend) { |
| 101 | int fd = open("/dev/graphics/fb0", O_RDWR); |
| 102 | if (fd == -1) { |
| 103 | perror("cannot open fb0"); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | fb_fix_screeninfo fi; |
| 108 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 109 | perror("failed to get fb0 info"); |
| 110 | close(fd); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 115 | perror("failed to get fb0 info"); |
| 116 | close(fd); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | // We print this out for informational purposes only, but |
| 121 | // throughout we assume that the framebuffer device uses an RGBX |
| 122 | // pixel format. This is the case for every development device I |
| 123 | // have access to. For some of those devices (eg, hammerhead aka |
| 124 | // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a |
| 125 | // different format (XBGR) but actually produces the correct |
| 126 | // results on the display when you write RGBX. |
| 127 | // |
| 128 | // If you have a device that actually *needs* another pixel format |
| 129 | // (ie, BGRX, or 565), patches welcome... |
| 130 | |
| 131 | printf("fb0 reports (possibly inaccurate):\n" |
| 132 | " vi.bits_per_pixel = %d\n" |
| 133 | " vi.red.offset = %3d .length = %3d\n" |
| 134 | " vi.green.offset = %3d .length = %3d\n" |
| 135 | " vi.blue.offset = %3d .length = %3d\n", |
| 136 | vi.bits_per_pixel, |
| 137 | vi.red.offset, vi.red.length, |
| 138 | vi.green.offset, vi.green.length, |
| 139 | vi.blue.offset, vi.blue.length); |
| 140 | |
| 141 | void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 142 | if (bits == MAP_FAILED) { |
| 143 | perror("failed to mmap framebuffer"); |
| 144 | close(fd); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | memset(bits, 0, fi.smem_len); |
| 149 | |
| 150 | #ifdef RECOVERY_RGB_565 |
| 151 | printf("Forcing pixel format: RGB_565\n"); |
| 152 | vi.blue.offset = 0; |
| 153 | vi.green.offset = 5; |
| 154 | vi.red.offset = 11; |
| 155 | vi.blue.length = 5; |
| 156 | vi.green.length = 6; |
| 157 | vi.red.length = 5; |
| 158 | vi.blue.msb_right = 0; |
| 159 | vi.green.msb_right = 0; |
| 160 | vi.red.msb_right = 0; |
| 161 | vi.transp.offset = 0; |
| 162 | vi.transp.length = 0; |
| 163 | vi.bits_per_pixel = 16; |
| 164 | #endif |
| 165 | |
| 166 | gr_framebuffer[0].width = vi.xres; |
| 167 | gr_framebuffer[0].height = vi.yres; |
| 168 | gr_framebuffer[0].row_bytes = fi.line_length; |
| 169 | gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; |
| 170 | #ifdef RECOVERY_GRAPHICS_USE_LINELENGTH |
| 171 | vi.xres_virtual = fi.line_length / gr_framebuffer[0].pixel_bytes; |
| 172 | #endif |
| 173 | gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits); |
| 174 | if (vi.bits_per_pixel == 16) { |
| 175 | printf("setting GGL_PIXEL_FORMAT_RGB_565\n"); |
| 176 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565; |
Ziyan | 4fea38a | 2016-01-28 01:19:35 +0100 | [diff] [blame^] | 177 | } else if (vi.red.offset == 8 || vi.red.offset == 16) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 178 | printf("setting GGL_PIXEL_FORMAT_BGRA_8888\n"); |
| 179 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_BGRA_8888; |
| 180 | } else if (vi.red.offset == 0) { |
| 181 | printf("setting GGL_PIXEL_FORMAT_RGBA_8888\n"); |
| 182 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBA_8888; |
| 183 | } else if (vi.red.offset == 24) { |
| 184 | printf("setting GGL_PIXEL_FORMAT_RGBX_8888\n"); |
| 185 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 186 | } else { |
| 187 | if (vi.red.length == 8) { |
| 188 | printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGBX_8888\n"); |
| 189 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 190 | } else { |
| 191 | printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGB_565\n"); |
| 192 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Drawing directly to the framebuffer takes about 5 times longer. |
| 197 | // Instead, we will allocate some memory and draw to that, then |
| 198 | // memcpy the data into the framebuffer later. |
| 199 | gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); |
| 200 | if (!gr_draw) { |
| 201 | perror("failed to allocate gr_draw"); |
| 202 | close(fd); |
| 203 | munmap(bits, fi.smem_len); |
| 204 | return NULL; |
| 205 | } |
| 206 | memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); |
| 207 | gr_draw->data = (unsigned char*) calloc(gr_draw->height * gr_draw->row_bytes, 1); |
| 208 | if (!gr_draw->data) { |
| 209 | perror("failed to allocate in-memory surface"); |
| 210 | close(fd); |
| 211 | free(gr_draw); |
| 212 | munmap(bits, fi.smem_len); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | /* check if we can use double buffering */ |
| 217 | if (vi.yres * fi.line_length * 2 <= fi.smem_len) { |
| 218 | double_buffered = true; |
| 219 | printf("double buffered\n"); |
| 220 | |
| 221 | memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); |
| 222 | gr_framebuffer[1].data = gr_framebuffer[0].data + |
| 223 | gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; |
| 224 | |
| 225 | } else { |
| 226 | double_buffered = false; |
| 227 | printf("single buffered\n"); |
| 228 | } |
| 229 | #if defined(RECOVERY_BGRA) |
| 230 | printf("RECOVERY_BGRA\n"); |
| 231 | #endif |
| 232 | fb_fd = fd; |
| 233 | set_displayed_framebuffer(0); |
| 234 | |
| 235 | printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); |
| 236 | |
| 237 | fbdev_blank(backend, true); |
| 238 | fbdev_blank(backend, false); |
| 239 | |
| 240 | smem_len = fi.smem_len; |
| 241 | |
| 242 | return gr_draw; |
| 243 | } |
| 244 | |
| 245 | static GRSurface* fbdev_flip(minui_backend* backend __unused) { |
| 246 | if (double_buffered) { |
| 247 | #if defined(RECOVERY_BGRA) |
| 248 | // In case of BGRA, do some byte swapping |
| 249 | unsigned int idx; |
| 250 | unsigned char tmp; |
| 251 | unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data; |
| 252 | for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); |
| 253 | idx += 4) { |
| 254 | tmp = ucfb_vaddr[idx]; |
| 255 | ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2]; |
| 256 | ucfb_vaddr[idx + 2] = tmp; |
| 257 | } |
| 258 | #endif |
| 259 | // Copy from the in-memory surface to the framebuffer. |
| 260 | memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data, |
| 261 | gr_draw->height * gr_draw->row_bytes); |
| 262 | set_displayed_framebuffer(1-displayed_buffer); |
| 263 | } else { |
| 264 | // Copy from the in-memory surface to the framebuffer. |
| 265 | memcpy(gr_framebuffer[0].data, gr_draw->data, |
| 266 | gr_draw->height * gr_draw->row_bytes); |
| 267 | } |
| 268 | return gr_draw; |
| 269 | } |
| 270 | |
| 271 | static void fbdev_exit(minui_backend* backend __unused) { |
| 272 | close(fb_fd); |
| 273 | fb_fd = -1; |
| 274 | |
| 275 | if (gr_draw) { |
| 276 | free(gr_draw->data); |
| 277 | free(gr_draw); |
| 278 | } |
| 279 | gr_draw = NULL; |
| 280 | munmap(gr_framebuffer[0].data, smem_len); |
| 281 | } |