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