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); |
AdrianDC | 2948b6f | 2016-02-07 03:44:44 +0100 | [diff] [blame] | 76 | |
| 77 | #ifdef TW_SECONDARY_BRIGHTNESS_PATH |
| 78 | fd = open(TW_SECONDARY_BRIGHTNESS_PATH, O_RDWR); |
| 79 | if (fd < 0) { |
| 80 | perror("cannot open LCD backlight 2"); |
| 81 | return; |
| 82 | } |
| 83 | write(fd, blank ? "000" : brightness, 3); |
| 84 | close(fd); |
| 85 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 86 | #else |
| 87 | #ifndef TW_NO_SCREEN_BLANK |
| 88 | int ret; |
| 89 | |
| 90 | ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 91 | if (ret < 0) |
| 92 | perror("ioctl(): blank"); |
| 93 | #endif |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | static void set_displayed_framebuffer(unsigned n) |
| 98 | { |
| 99 | if (n > 1 || !double_buffered) return; |
| 100 | |
| 101 | vi.yres_virtual = gr_framebuffer[0].height * 2; |
| 102 | vi.yoffset = n * gr_framebuffer[0].height; |
| 103 | vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; |
| 104 | if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 105 | perror("active fb swap failed"); |
Muhammad Fahad Baig | 0ac2293 | 2016-04-07 09:19:33 +0100 | [diff] [blame] | 106 | #ifdef TW_FBIOPAN |
| 107 | } else { |
| 108 | if (ioctl(fb_fd, FBIOPAN_DISPLAY, &vi) < 0) { |
| 109 | perror("pan failed"); |
| 110 | } |
| 111 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 112 | } |
| 113 | displayed_buffer = n; |
| 114 | } |
| 115 | |
| 116 | static GRSurface* fbdev_init(minui_backend* backend) { |
that | b9d8f62 | 2016-02-22 21:09:09 +0100 | [diff] [blame] | 117 | int retry = 20; |
| 118 | int fd = -1; |
| 119 | while (fd == -1) { |
| 120 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 121 | if (fd == -1) { |
| 122 | if (--retry) { |
| 123 | // wait for init to create the device node |
| 124 | perror("cannot open fb0 (retrying)"); |
| 125 | usleep(100000); |
| 126 | } else { |
| 127 | perror("cannot open fb0 (giving up)"); |
| 128 | return NULL; |
| 129 | } |
| 130 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Aaron Kling | b9e547e | 2016-05-26 14:49:33 -0500 | [diff] [blame] | 133 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 134 | perror("failed to get fb0 info (FBIOGET_VSCREENINFO)"); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 135 | close(fd); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Aaron Kling | b9e547e | 2016-05-26 14:49:33 -0500 | [diff] [blame] | 139 | #ifdef RECOVERY_FORCE_RGB_565 |
| 140 | // Changing fb_var_screeninfo can affect fb_fix_screeninfo, |
| 141 | // so this needs done before querying for fi. |
| 142 | printf("Forcing pixel format: RGB_565\n"); |
| 143 | vi.blue.offset = 0; |
| 144 | vi.green.offset = 5; |
| 145 | vi.red.offset = 11; |
| 146 | vi.blue.length = 5; |
| 147 | vi.green.length = 6; |
| 148 | vi.red.length = 5; |
| 149 | vi.blue.msb_right = 0; |
| 150 | vi.green.msb_right = 0; |
| 151 | vi.red.msb_right = 0; |
| 152 | vi.transp.offset = 0; |
| 153 | vi.transp.length = 0; |
| 154 | vi.bits_per_pixel = 16; |
| 155 | |
| 156 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 157 | perror("failed to put force_rgb_565 fb0 info"); |
| 158 | close(fd); |
| 159 | return NULL; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | fb_fix_screeninfo fi; |
| 164 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 165 | perror("failed to get fb0 info (FBIOGET_FSCREENINFO)"); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 166 | close(fd); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | // We print this out for informational purposes only, but |
| 171 | // throughout we assume that the framebuffer device uses an RGBX |
| 172 | // pixel format. This is the case for every development device I |
| 173 | // have access to. For some of those devices (eg, hammerhead aka |
| 174 | // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a |
| 175 | // different format (XBGR) but actually produces the correct |
| 176 | // results on the display when you write RGBX. |
| 177 | // |
| 178 | // If you have a device that actually *needs* another pixel format |
| 179 | // (ie, BGRX, or 565), patches welcome... |
| 180 | |
| 181 | printf("fb0 reports (possibly inaccurate):\n" |
| 182 | " vi.bits_per_pixel = %d\n" |
| 183 | " vi.red.offset = %3d .length = %3d\n" |
| 184 | " vi.green.offset = %3d .length = %3d\n" |
| 185 | " vi.blue.offset = %3d .length = %3d\n", |
| 186 | vi.bits_per_pixel, |
| 187 | vi.red.offset, vi.red.length, |
| 188 | vi.green.offset, vi.green.length, |
| 189 | vi.blue.offset, vi.blue.length); |
| 190 | |
| 191 | void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 192 | if (bits == MAP_FAILED) { |
| 193 | perror("failed to mmap framebuffer"); |
| 194 | close(fd); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | memset(bits, 0, fi.smem_len); |
| 199 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 200 | gr_framebuffer[0].width = vi.xres; |
| 201 | gr_framebuffer[0].height = vi.yres; |
| 202 | gr_framebuffer[0].row_bytes = fi.line_length; |
| 203 | gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; |
Ethan Yonker | 31f855b | 2016-01-28 22:31:48 -0600 | [diff] [blame] | 204 | #ifdef RECOVERY_GRAPHICS_FORCE_USE_LINELENGTH |
| 205 | printf("Forcing line length\n"); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 206 | vi.xres_virtual = fi.line_length / gr_framebuffer[0].pixel_bytes; |
| 207 | #endif |
| 208 | gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits); |
| 209 | if (vi.bits_per_pixel == 16) { |
| 210 | printf("setting GGL_PIXEL_FORMAT_RGB_565\n"); |
| 211 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565; |
Ziyan | 4fea38a | 2016-01-28 01:19:35 +0100 | [diff] [blame] | 212 | } else if (vi.red.offset == 8 || vi.red.offset == 16) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 213 | printf("setting GGL_PIXEL_FORMAT_BGRA_8888\n"); |
| 214 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_BGRA_8888; |
| 215 | } else if (vi.red.offset == 0) { |
| 216 | printf("setting GGL_PIXEL_FORMAT_RGBA_8888\n"); |
| 217 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBA_8888; |
| 218 | } else if (vi.red.offset == 24) { |
| 219 | printf("setting GGL_PIXEL_FORMAT_RGBX_8888\n"); |
| 220 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 221 | } else { |
| 222 | if (vi.red.length == 8) { |
| 223 | printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGBX_8888\n"); |
| 224 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 225 | } else { |
| 226 | printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGB_565\n"); |
| 227 | gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Drawing directly to the framebuffer takes about 5 times longer. |
| 232 | // Instead, we will allocate some memory and draw to that, then |
| 233 | // memcpy the data into the framebuffer later. |
| 234 | gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); |
| 235 | if (!gr_draw) { |
| 236 | perror("failed to allocate gr_draw"); |
| 237 | close(fd); |
| 238 | munmap(bits, fi.smem_len); |
| 239 | return NULL; |
| 240 | } |
| 241 | memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); |
| 242 | gr_draw->data = (unsigned char*) calloc(gr_draw->height * gr_draw->row_bytes, 1); |
| 243 | if (!gr_draw->data) { |
| 244 | perror("failed to allocate in-memory surface"); |
| 245 | close(fd); |
| 246 | free(gr_draw); |
| 247 | munmap(bits, fi.smem_len); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | /* check if we can use double buffering */ |
Ethan Yonker | 4a71d4f | 2016-07-29 17:42:36 -0500 | [diff] [blame] | 252 | #ifndef RECOVERY_GRAPHICS_FORCE_SINGLE_BUFFER |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 253 | if (vi.yres * fi.line_length * 2 <= fi.smem_len) { |
| 254 | double_buffered = true; |
| 255 | printf("double buffered\n"); |
| 256 | |
| 257 | memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); |
| 258 | gr_framebuffer[1].data = gr_framebuffer[0].data + |
| 259 | gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; |
| 260 | |
| 261 | } else { |
Ethan Yonker | 4a71d4f | 2016-07-29 17:42:36 -0500 | [diff] [blame] | 262 | #else |
| 263 | { |
| 264 | printf("RECOVERY_GRAPHICS_FORCE_SINGLE_BUFFER := true\n"); |
| 265 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 266 | double_buffered = false; |
| 267 | printf("single buffered\n"); |
| 268 | } |
| 269 | #if defined(RECOVERY_BGRA) |
| 270 | printf("RECOVERY_BGRA\n"); |
| 271 | #endif |
| 272 | fb_fd = fd; |
| 273 | set_displayed_framebuffer(0); |
| 274 | |
| 275 | printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); |
| 276 | |
| 277 | fbdev_blank(backend, true); |
| 278 | fbdev_blank(backend, false); |
| 279 | |
| 280 | smem_len = fi.smem_len; |
| 281 | |
| 282 | return gr_draw; |
| 283 | } |
| 284 | |
| 285 | static GRSurface* fbdev_flip(minui_backend* backend __unused) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 286 | #if defined(RECOVERY_BGRA) |
Ethan Yonker | 871b07f | 2016-02-01 10:31:59 -0600 | [diff] [blame] | 287 | // In case of BGRA, do some byte swapping |
| 288 | unsigned int idx; |
| 289 | unsigned char tmp; |
| 290 | unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data; |
| 291 | for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); |
| 292 | idx += 4) { |
| 293 | tmp = ucfb_vaddr[idx]; |
| 294 | ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2]; |
| 295 | ucfb_vaddr[idx + 2] = tmp; |
| 296 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 297 | #endif |
Ethan Yonker | 871b07f | 2016-02-01 10:31:59 -0600 | [diff] [blame] | 298 | #ifndef BOARD_HAS_FLIPPED_SCREEN |
| 299 | if (double_buffered) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 300 | // Copy from the in-memory surface to the framebuffer. |
| 301 | memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data, |
| 302 | gr_draw->height * gr_draw->row_bytes); |
| 303 | set_displayed_framebuffer(1-displayed_buffer); |
| 304 | } else { |
| 305 | // Copy from the in-memory surface to the framebuffer. |
| 306 | memcpy(gr_framebuffer[0].data, gr_draw->data, |
| 307 | gr_draw->height * gr_draw->row_bytes); |
| 308 | } |
Ethan Yonker | 871b07f | 2016-02-01 10:31:59 -0600 | [diff] [blame] | 309 | #else |
| 310 | int gr_active_fb = 0; |
| 311 | if (double_buffered) |
| 312 | gr_active_fb = 1-displayed_buffer; |
| 313 | |
that | 506fc80 | 2016-02-09 01:42:08 +0100 | [diff] [blame] | 314 | /* flip buffer 180 degrees for devices with physically inverted screens */ |
| 315 | unsigned int row_pixels = gr_draw->row_bytes / gr_framebuffer[0].pixel_bytes; |
| 316 | if (gr_framebuffer[0].pixel_bytes == 4) { |
| 317 | for (unsigned int y = 0; y < gr_draw->height; ++y) { |
| 318 | uint32_t* dst = reinterpret_cast<uint32_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels; |
| 319 | uint32_t* src = reinterpret_cast<uint32_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width; |
| 320 | for (unsigned int x = 0; x < gr_draw->width; ++x) |
| 321 | *(dst++) = *(--src); |
| 322 | } |
| 323 | } else { |
| 324 | for (unsigned int y = 0; y < gr_draw->height; ++y) { |
| 325 | uint16_t* dst = reinterpret_cast<uint16_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels; |
| 326 | uint16_t* src = reinterpret_cast<uint16_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width; |
| 327 | for (unsigned int x = 0; x < gr_draw->width; ++x) |
| 328 | *(dst++) = *(--src); |
Ethan Yonker | 871b07f | 2016-02-01 10:31:59 -0600 | [diff] [blame] | 329 | } |
| 330 | } |
that | 506fc80 | 2016-02-09 01:42:08 +0100 | [diff] [blame] | 331 | |
Ethan Yonker | 871b07f | 2016-02-01 10:31:59 -0600 | [diff] [blame] | 332 | if (double_buffered) |
| 333 | set_displayed_framebuffer(1-displayed_buffer); |
| 334 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 335 | return gr_draw; |
| 336 | } |
| 337 | |
| 338 | static void fbdev_exit(minui_backend* backend __unused) { |
| 339 | close(fb_fd); |
| 340 | fb_fd = -1; |
| 341 | |
| 342 | if (gr_draw) { |
| 343 | free(gr_draw->data); |
| 344 | free(gr_draw); |
| 345 | } |
| 346 | gr_draw = NULL; |
| 347 | munmap(gr_framebuffer[0].data, smem_len); |
| 348 | } |