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