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