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