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