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 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 18 | #include <string.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 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 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 32 | #include "minui/minui.h" |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 33 | #include "graphics.h" |
| 34 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 35 | static GRSurface* fbdev_init(minui_backend*); |
| 36 | static GRSurface* fbdev_flip(minui_backend*); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 45 | static fb_var_screeninfo vi; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 81 | static GRSurface* fbdev_init(minui_backend* backend) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 82 | int fd = open("/dev/graphics/fb0", O_RDWR); |
| 83 | if (fd == -1) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 84 | perror("cannot open fb0"); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 88 | fb_fix_screeninfo fi; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 89 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 90 | perror("failed to get fb0 info"); |
| 91 | close(fd); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 96 | perror("failed to get fb0 info"); |
| 97 | close(fd); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | // We print this out for informational purposes only, but |
| 102 | // throughout we assume that the framebuffer device uses an RGBX |
| 103 | // pixel format. This is the case for every development device I |
| 104 | // have access to. For some of those devices (eg, hammerhead aka |
| 105 | // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a |
| 106 | // different format (XBGR) but actually produces the correct |
| 107 | // results on the display when you write RGBX. |
| 108 | // |
| 109 | // If you have a device that actually *needs* another pixel format |
| 110 | // (ie, BGRX, or 565), patches welcome... |
| 111 | |
| 112 | printf("fb0 reports (possibly inaccurate):\n" |
| 113 | " vi.bits_per_pixel = %d\n" |
| 114 | " vi.red.offset = %3d .length = %3d\n" |
| 115 | " vi.green.offset = %3d .length = %3d\n" |
| 116 | " vi.blue.offset = %3d .length = %3d\n", |
| 117 | vi.bits_per_pixel, |
| 118 | vi.red.offset, vi.red.length, |
| 119 | vi.green.offset, vi.green.length, |
| 120 | vi.blue.offset, vi.blue.length); |
| 121 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 122 | 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] | 123 | if (bits == MAP_FAILED) { |
| 124 | perror("failed to mmap framebuffer"); |
| 125 | close(fd); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Joey Pan | ba545d7 | 2014-06-04 20:47:46 +0800 | [diff] [blame] | 129 | memset(bits, 0, fi.smem_len); |
| 130 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 131 | gr_framebuffer[0].width = vi.xres; |
| 132 | gr_framebuffer[0].height = vi.yres; |
| 133 | gr_framebuffer[0].row_bytes = fi.line_length; |
| 134 | gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 135 | gr_framebuffer[0].data = static_cast<uint8_t*>(bits); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 136 | memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); |
| 137 | |
| 138 | /* check if we can use double buffering */ |
| 139 | if (vi.yres * fi.line_length * 2 <= fi.smem_len) { |
| 140 | double_buffered = true; |
| 141 | |
| 142 | memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); |
| 143 | gr_framebuffer[1].data = gr_framebuffer[0].data + |
| 144 | gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; |
| 145 | |
| 146 | gr_draw = gr_framebuffer+1; |
| 147 | |
| 148 | } else { |
| 149 | double_buffered = false; |
| 150 | |
| 151 | // Without double-buffering, we allocate RAM for a buffer to |
| 152 | // draw in, and then "flipping" the buffer consists of a |
| 153 | // memcpy from the buffer we allocated to the framebuffer. |
| 154 | |
| 155 | gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); |
| 156 | memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); |
| 157 | gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes); |
| 158 | if (!gr_draw->data) { |
| 159 | perror("failed to allocate in-memory surface"); |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); |
| 165 | fb_fd = fd; |
| 166 | set_displayed_framebuffer(0); |
| 167 | |
| 168 | printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); |
| 169 | |
| 170 | fbdev_blank(backend, true); |
| 171 | fbdev_blank(backend, false); |
| 172 | |
| 173 | return gr_draw; |
| 174 | } |
| 175 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 176 | static GRSurface* fbdev_flip(minui_backend* backend __unused) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 177 | if (double_buffered) { |
| 178 | // Change gr_draw to point to the buffer currently displayed, |
| 179 | // then flip the driver so we're displaying the other buffer |
| 180 | // instead. |
| 181 | gr_draw = gr_framebuffer + displayed_buffer; |
| 182 | set_displayed_framebuffer(1-displayed_buffer); |
| 183 | } else { |
| 184 | // Copy from the in-memory surface to the framebuffer. |
| 185 | memcpy(gr_framebuffer[0].data, gr_draw->data, |
| 186 | gr_draw->height * gr_draw->row_bytes); |
| 187 | } |
| 188 | return gr_draw; |
| 189 | } |
| 190 | |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 191 | static void fbdev_exit(minui_backend* backend __unused) { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 192 | close(fb_fd); |
| 193 | fb_fd = -1; |
| 194 | |
| 195 | if (!double_buffered && gr_draw) { |
| 196 | free(gr_draw->data); |
| 197 | free(gr_draw); |
| 198 | } |
| 199 | gr_draw = NULL; |
| 200 | } |