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