Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [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> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 19 | #include <string.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 36 | static GRSurface* fbdev_init(minui_backend*); |
| 37 | static GRSurface* fbdev_flip(minui_backend*); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 38 | static void fbdev_blank(minui_backend*, bool); |
| 39 | static void fbdev_exit(minui_backend*); |
| 40 | |
| 41 | static GRSurface gr_framebuffer[2]; |
| 42 | static bool double_buffered; |
| 43 | static GRSurface* gr_draw = NULL; |
| 44 | static int displayed_buffer; |
| 45 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 46 | static fb_var_screeninfo vi; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 47 | static int fb_fd = -1; |
| 48 | |
| 49 | static minui_backend my_backend = { |
| 50 | .init = fbdev_init, |
| 51 | .flip = fbdev_flip, |
| 52 | .blank = fbdev_blank, |
| 53 | .exit = fbdev_exit, |
| 54 | }; |
| 55 | |
| 56 | minui_backend* open_fbdev() { |
| 57 | return &my_backend; |
| 58 | } |
| 59 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 60 | static void fbdev_blank(minui_backend* backend __unused, bool blank) |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 61 | { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 62 | #if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS) |
| 63 | int fd; |
| 64 | char brightness[4]; |
| 65 | snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2); |
| 66 | |
| 67 | fd = open(TW_BRIGHTNESS_PATH, O_RDWR); |
| 68 | if (fd < 0) { |
| 69 | perror("cannot open LCD backlight"); |
| 70 | return; |
| 71 | } |
| 72 | write(fd, blank ? "000" : brightness, 3); |
| 73 | close(fd); |
| 74 | #else |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 75 | int ret; |
| 76 | |
| 77 | ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 78 | if (ret < 0) |
| 79 | perror("ioctl(): blank"); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 80 | #endif |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void set_displayed_framebuffer(unsigned n) |
| 84 | { |
| 85 | if (n > 1 || !double_buffered) return; |
| 86 | |
| 87 | vi.yres_virtual = gr_framebuffer[0].height * 2; |
| 88 | vi.yoffset = n * gr_framebuffer[0].height; |
| 89 | vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; |
| 90 | if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 91 | perror("active fb swap failed"); |
| 92 | } |
| 93 | displayed_buffer = n; |
| 94 | } |
| 95 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 96 | static GRSurface* fbdev_init(minui_backend* backend) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 97 | int fd = open("/dev/graphics/fb0", O_RDWR); |
| 98 | if (fd == -1) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 99 | perror("cannot open fb0"); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 103 | fb_fix_screeninfo fi; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 104 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 105 | perror("failed to get fb0 info"); |
| 106 | close(fd); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 111 | perror("failed to get fb0 info"); |
| 112 | close(fd); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | // We print this out for informational purposes only, but |
| 117 | // throughout we assume that the framebuffer device uses an RGBX |
| 118 | // pixel format. This is the case for every development device I |
| 119 | // have access to. For some of those devices (eg, hammerhead aka |
| 120 | // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a |
| 121 | // different format (XBGR) but actually produces the correct |
| 122 | // results on the display when you write RGBX. |
| 123 | // |
| 124 | // If you have a device that actually *needs* another pixel format |
| 125 | // (ie, BGRX, or 565), patches welcome... |
| 126 | |
| 127 | printf("fb0 reports (possibly inaccurate):\n" |
| 128 | " vi.bits_per_pixel = %d\n" |
| 129 | " vi.red.offset = %3d .length = %3d\n" |
| 130 | " vi.green.offset = %3d .length = %3d\n" |
| 131 | " vi.blue.offset = %3d .length = %3d\n", |
| 132 | vi.bits_per_pixel, |
| 133 | vi.red.offset, vi.red.length, |
| 134 | vi.green.offset, vi.green.length, |
| 135 | vi.blue.offset, vi.blue.length); |
| 136 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 137 | void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 138 | if (bits == MAP_FAILED) { |
| 139 | perror("failed to mmap framebuffer"); |
| 140 | close(fd); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Joey Pan | ba545d7 | 2014-06-04 20:47:46 +0800 | [diff] [blame] | 144 | memset(bits, 0, fi.smem_len); |
| 145 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 146 | gr_framebuffer[0].width = vi.xres; |
| 147 | gr_framebuffer[0].height = vi.yres; |
| 148 | gr_framebuffer[0].row_bytes = fi.line_length; |
| 149 | gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 150 | gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 151 | memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); |
| 152 | |
| 153 | /* check if we can use double buffering */ |
| 154 | if (vi.yres * fi.line_length * 2 <= fi.smem_len) { |
| 155 | double_buffered = true; |
| 156 | |
| 157 | memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); |
| 158 | gr_framebuffer[1].data = gr_framebuffer[0].data + |
| 159 | gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; |
| 160 | |
| 161 | gr_draw = gr_framebuffer+1; |
| 162 | |
| 163 | } else { |
| 164 | double_buffered = false; |
| 165 | |
| 166 | // Without double-buffering, we allocate RAM for a buffer to |
| 167 | // draw in, and then "flipping" the buffer consists of a |
| 168 | // memcpy from the buffer we allocated to the framebuffer. |
| 169 | |
| 170 | gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); |
| 171 | memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); |
| 172 | gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes); |
| 173 | if (!gr_draw->data) { |
| 174 | perror("failed to allocate in-memory surface"); |
| 175 | return NULL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); |
| 180 | fb_fd = fd; |
| 181 | set_displayed_framebuffer(0); |
| 182 | |
| 183 | printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); |
| 184 | |
| 185 | fbdev_blank(backend, true); |
| 186 | fbdev_blank(backend, false); |
| 187 | |
| 188 | return gr_draw; |
| 189 | } |
| 190 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 191 | static GRSurface* fbdev_flip(minui_backend* backend __unused) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 192 | if (double_buffered) { |
| 193 | // Change gr_draw to point to the buffer currently displayed, |
| 194 | // then flip the driver so we're displaying the other buffer |
| 195 | // instead. |
| 196 | gr_draw = gr_framebuffer + displayed_buffer; |
| 197 | set_displayed_framebuffer(1-displayed_buffer); |
| 198 | } else { |
| 199 | // Copy from the in-memory surface to the framebuffer. |
| 200 | memcpy(gr_framebuffer[0].data, gr_draw->data, |
| 201 | gr_draw->height * gr_draw->row_bytes); |
| 202 | } |
| 203 | return gr_draw; |
| 204 | } |
| 205 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 206 | static void fbdev_exit(minui_backend* backend __unused) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 207 | close(fb_fd); |
| 208 | fb_fd = -1; |
| 209 | |
| 210 | if (!double_buffered && gr_draw) { |
| 211 | free(gr_draw->data); |
| 212 | free(gr_draw); |
| 213 | } |
| 214 | gr_draw = NULL; |
| 215 | } |