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 | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 17 | #include "graphics_fbdev.h" |
| 18 | |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <linux/fb.h> |
| 21 | #include <stdio.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 23 | #include <string.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/types.h> |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 28 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
Tao Bao | f65d48b | 2018-11-02 09:34:49 -0700 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
| 32 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 33 | #include "minui/minui.h" |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 34 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 35 | std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(size_t width, size_t height, |
| 36 | size_t row_bytes, size_t pixel_bytes) { |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 37 | // Cannot use std::make_unique to access non-public ctor. |
| 38 | return std::unique_ptr<GRSurfaceFbdev>(new GRSurfaceFbdev(width, height, row_bytes, pixel_bytes)); |
| 39 | } |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 40 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 41 | void MinuiBackendFbdev::Blank(bool blank) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 42 | #if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS) |
| 43 | int fd; |
| 44 | char brightness[4]; |
| 45 | snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2); |
| 46 | |
| 47 | fd = open(TW_BRIGHTNESS_PATH, O_RDWR); |
| 48 | if (fd < 0) { |
| 49 | perror("cannot open LCD backlight"); |
| 50 | return; |
| 51 | } |
| 52 | write(fd, blank ? "000" : brightness, 3); |
| 53 | close(fd); |
| 54 | #else |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 55 | int ret; |
| 56 | |
| 57 | ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 58 | if (ret < 0) |
| 59 | perror("ioctl(): blank"); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 60 | #endif |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 63 | void MinuiBackendFbdev::Blank(bool blank, DrmConnector index) { |
Tim Zimmermann | 997277d | 2022-09-03 12:06:48 +0200 | [diff] [blame] | 64 | if (index == DRM_MAIN) { |
| 65 | MinuiBackendFbdev::Blank(blank); |
| 66 | } else { |
| 67 | fprintf(stderr, "Unsupported multiple connectors, blank = %d, index = %d\n", blank, index); |
| 68 | } |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Weizhung Ding | afd0a1b | 2022-07-19 16:34:43 +0800 | [diff] [blame] | 71 | bool MinuiBackendFbdev::HasMultipleConnectors() { |
| 72 | fprintf(stderr, "Unsupported multiple connectors\n"); |
| 73 | return false; |
| 74 | } |
| 75 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 76 | void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) { |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 77 | if (n > 1 || !double_buffered) return; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 78 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 79 | vi.yres_virtual = gr_framebuffer[0]->height * 2; |
| 80 | vi.yoffset = n * gr_framebuffer[0]->height; |
| 81 | vi.bits_per_pixel = gr_framebuffer[0]->pixel_bytes * 8; |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 82 | if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 83 | perror("active fb swap failed"); |
| 84 | } |
| 85 | displayed_buffer = n; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 88 | GRSurface* MinuiBackendFbdev::Init() { |
Tao Bao | b549243 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 89 | android::base::unique_fd fd(open("/dev/graphics/fb0", O_RDWR | O_CLOEXEC)); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 90 | if (fd == -1) { |
| 91 | perror("cannot open fb0"); |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | fb_fix_screeninfo fi; |
| 96 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 97 | perror("failed to get fb0 info"); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 102 | perror("failed to get fb0 info"); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 103 | return nullptr; |
| 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( |
| 118 | "fb0 reports (possibly inaccurate):\n" |
| 119 | " vi.bits_per_pixel = %d\n" |
| 120 | " vi.red.offset = %3d .length = %3d\n" |
| 121 | " vi.green.offset = %3d .length = %3d\n" |
| 122 | " vi.blue.offset = %3d .length = %3d\n", |
| 123 | vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length, |
| 124 | vi.blue.offset, vi.blue.length); |
| 125 | |
| 126 | void* 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"); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | memset(bits, 0, fi.smem_len); |
| 133 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 134 | gr_framebuffer[0] = |
| 135 | GRSurfaceFbdev::Create(vi.xres, vi.yres, fi.line_length, vi.bits_per_pixel / 8); |
| 136 | gr_framebuffer[0]->buffer_ = static_cast<uint8_t*>(bits); |
| 137 | memset(gr_framebuffer[0]->buffer_, 0, gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes); |
| 138 | |
| 139 | gr_framebuffer[1] = |
| 140 | GRSurfaceFbdev::Create(gr_framebuffer[0]->width, gr_framebuffer[0]->height, |
| 141 | gr_framebuffer[0]->row_bytes, gr_framebuffer[0]->pixel_bytes); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 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 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 147 | gr_framebuffer[1]->buffer_ = |
| 148 | gr_framebuffer[0]->buffer_ + gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes; |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 149 | } else { |
| 150 | double_buffered = false; |
| 151 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 152 | // Without double-buffering, we allocate RAM for a buffer to draw in, and then "flipping" the |
| 153 | // buffer consists of a memcpy from the buffer we allocated to the framebuffer. |
| 154 | memory_buffer.resize(gr_framebuffer[1]->height * gr_framebuffer[1]->row_bytes); |
| 155 | gr_framebuffer[1]->buffer_ = memory_buffer.data(); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 156 | } |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 157 | |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 158 | gr_draw = gr_framebuffer[1].get(); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 159 | memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes); |
Tao Bao | f65d48b | 2018-11-02 09:34:49 -0700 | [diff] [blame] | 160 | fb_fd = std::move(fd); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 161 | SetDisplayedFramebuffer(0); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 162 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 163 | printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 164 | Blank(false); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 165 | |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 166 | return gr_draw; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 169 | GRSurface* MinuiBackendFbdev::Flip() { |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 170 | if (double_buffered) { |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 171 | // Change gr_draw to point to the buffer currently displayed, then flip the driver so we're |
| 172 | // displaying the other buffer instead. |
| 173 | gr_draw = gr_framebuffer[displayed_buffer].get(); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 174 | SetDisplayedFramebuffer(1 - displayed_buffer); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 175 | } else { |
| 176 | // Copy from the in-memory surface to the framebuffer. |
Tao Bao | 4a22b28 | 2018-10-31 00:00:31 -0700 | [diff] [blame] | 177 | memcpy(gr_framebuffer[0]->buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes); |
Tao Bao | acf4dd1 | 2017-02-07 14:32:05 -0800 | [diff] [blame] | 178 | } |
| 179 | return gr_draw; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 180 | } |