blob: 2711af74cacf2927311568bbb44bf2f33c8adc4e [file] [log] [blame]
Doug Zongker5290f202014-03-11 13:22:04 -07001/*
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 Bao557fa1f2017-02-07 12:51:00 -080017#include "graphics_fbdev.h"
18
Tao Baoacf4dd12017-02-07 14:32:05 -080019#include <fcntl.h>
20#include <linux/fb.h>
21#include <stdio.h>
Doug Zongker5290f202014-03-11 13:22:04 -070022#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080023#include <string.h>
Doug Zongker5290f202014-03-11 13:22:04 -070024#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/types.h>
Tao Baoacf4dd12017-02-07 14:32:05 -080027#include <unistd.h>
Doug Zongker5290f202014-03-11 13:22:04 -070028
Tao Bao4a22b282018-10-31 00:00:31 -070029#include <memory>
30
Tao Baof65d48b2018-11-02 09:34:49 -070031#include <android-base/unique_fd.h>
32
Tao Bao0ecbd762017-01-16 21:16:58 -080033#include "minui/minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070034
Tao Baodd789822018-11-26 16:28:07 -080035std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(size_t width, size_t height,
36 size_t row_bytes, size_t pixel_bytes) {
Tao Bao4a22b282018-10-31 00:00:31 -070037 // 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 Zongker5290f202014-03-11 13:22:04 -070040
Tao Bao557fa1f2017-02-07 12:51:00 -080041void MinuiBackendFbdev::Blank(bool blank) {
Tao Baoacf4dd12017-02-07 14:32:05 -080042 int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
Tao Bao557fa1f2017-02-07 12:51:00 -080043 if (ret < 0) perror("ioctl(): blank");
Doug Zongker5290f202014-03-11 13:22:04 -070044}
45
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +080046void MinuiBackendFbdev::Blank(bool blank, DrmConnector index) {
Tim Zimmermann997277d2022-09-03 12:06:48 +020047 if (index == DRM_MAIN) {
48 MinuiBackendFbdev::Blank(blank);
49 } else {
50 fprintf(stderr, "Unsupported multiple connectors, blank = %d, index = %d\n", blank, index);
51 }
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +080052}
53
Weizhung Dingafd0a1b2022-07-19 16:34:43 +080054bool MinuiBackendFbdev::HasMultipleConnectors() {
55 fprintf(stderr, "Unsupported multiple connectors\n");
56 return false;
57}
58
Tao Bao4a22b282018-10-31 00:00:31 -070059void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) {
Tao Baoacf4dd12017-02-07 14:32:05 -080060 if (n > 1 || !double_buffered) return;
Doug Zongker5290f202014-03-11 13:22:04 -070061
Tao Bao4a22b282018-10-31 00:00:31 -070062 vi.yres_virtual = gr_framebuffer[0]->height * 2;
63 vi.yoffset = n * gr_framebuffer[0]->height;
64 vi.bits_per_pixel = gr_framebuffer[0]->pixel_bytes * 8;
Tao Baoacf4dd12017-02-07 14:32:05 -080065 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
66 perror("active fb swap failed");
67 }
68 displayed_buffer = n;
Doug Zongker5290f202014-03-11 13:22:04 -070069}
70
Tao Bao557fa1f2017-02-07 12:51:00 -080071GRSurface* MinuiBackendFbdev::Init() {
Tao Baob5492432019-01-16 09:29:17 -080072 android::base::unique_fd fd(open("/dev/graphics/fb0", O_RDWR | O_CLOEXEC));
Tao Baoacf4dd12017-02-07 14:32:05 -080073 if (fd == -1) {
74 perror("cannot open fb0");
75 return nullptr;
76 }
77
78 fb_fix_screeninfo fi;
79 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
80 perror("failed to get fb0 info");
Tao Baoacf4dd12017-02-07 14:32:05 -080081 return nullptr;
82 }
83
84 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
85 perror("failed to get fb0 info");
Tao Baoacf4dd12017-02-07 14:32:05 -080086 return nullptr;
87 }
88
89 // We print this out for informational purposes only, but
90 // throughout we assume that the framebuffer device uses an RGBX
91 // pixel format. This is the case for every development device I
92 // have access to. For some of those devices (eg, hammerhead aka
93 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
94 // different format (XBGR) but actually produces the correct
95 // results on the display when you write RGBX.
96 //
97 // If you have a device that actually *needs* another pixel format
98 // (ie, BGRX, or 565), patches welcome...
99
100 printf(
101 "fb0 reports (possibly inaccurate):\n"
102 " vi.bits_per_pixel = %d\n"
103 " vi.red.offset = %3d .length = %3d\n"
104 " vi.green.offset = %3d .length = %3d\n"
105 " vi.blue.offset = %3d .length = %3d\n",
106 vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length,
107 vi.blue.offset, vi.blue.length);
108
109 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
110 if (bits == MAP_FAILED) {
111 perror("failed to mmap framebuffer");
Tao Baoacf4dd12017-02-07 14:32:05 -0800112 return nullptr;
113 }
114
115 memset(bits, 0, fi.smem_len);
116
Tao Bao4a22b282018-10-31 00:00:31 -0700117 gr_framebuffer[0] =
118 GRSurfaceFbdev::Create(vi.xres, vi.yres, fi.line_length, vi.bits_per_pixel / 8);
119 gr_framebuffer[0]->buffer_ = static_cast<uint8_t*>(bits);
120 memset(gr_framebuffer[0]->buffer_, 0, gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes);
121
122 gr_framebuffer[1] =
123 GRSurfaceFbdev::Create(gr_framebuffer[0]->width, gr_framebuffer[0]->height,
124 gr_framebuffer[0]->row_bytes, gr_framebuffer[0]->pixel_bytes);
Tao Baoacf4dd12017-02-07 14:32:05 -0800125
126 /* check if we can use double buffering */
127 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
128 double_buffered = true;
129
Tao Bao4a22b282018-10-31 00:00:31 -0700130 gr_framebuffer[1]->buffer_ =
131 gr_framebuffer[0]->buffer_ + gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes;
Tao Baoacf4dd12017-02-07 14:32:05 -0800132 } else {
133 double_buffered = false;
134
Tao Bao4a22b282018-10-31 00:00:31 -0700135 // Without double-buffering, we allocate RAM for a buffer to draw in, and then "flipping" the
136 // buffer consists of a memcpy from the buffer we allocated to the framebuffer.
137 memory_buffer.resize(gr_framebuffer[1]->height * gr_framebuffer[1]->row_bytes);
138 gr_framebuffer[1]->buffer_ = memory_buffer.data();
Tao Baoacf4dd12017-02-07 14:32:05 -0800139 }
Doug Zongker5290f202014-03-11 13:22:04 -0700140
Tao Bao4a22b282018-10-31 00:00:31 -0700141 gr_draw = gr_framebuffer[1].get();
Tao Bao92bdb5a2018-10-21 12:12:37 -0700142 memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes);
Tao Baof65d48b2018-11-02 09:34:49 -0700143 fb_fd = std::move(fd);
Tao Bao557fa1f2017-02-07 12:51:00 -0800144 SetDisplayedFramebuffer(0);
Doug Zongker5290f202014-03-11 13:22:04 -0700145
Tao Baodd789822018-11-26 16:28:07 -0800146 printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height);
Tao Bao557fa1f2017-02-07 12:51:00 -0800147 Blank(false);
Doug Zongker5290f202014-03-11 13:22:04 -0700148
Tao Baoacf4dd12017-02-07 14:32:05 -0800149 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700150}
151
Tao Bao557fa1f2017-02-07 12:51:00 -0800152GRSurface* MinuiBackendFbdev::Flip() {
Tao Baoacf4dd12017-02-07 14:32:05 -0800153 if (double_buffered) {
Tao Bao4a22b282018-10-31 00:00:31 -0700154 // Change gr_draw to point to the buffer currently displayed, then flip the driver so we're
155 // displaying the other buffer instead.
156 gr_draw = gr_framebuffer[displayed_buffer].get();
Tao Bao557fa1f2017-02-07 12:51:00 -0800157 SetDisplayedFramebuffer(1 - displayed_buffer);
Tao Baoacf4dd12017-02-07 14:32:05 -0800158 } else {
159 // Copy from the in-memory surface to the framebuffer.
Tao Bao4a22b282018-10-31 00:00:31 -0700160 memcpy(gr_framebuffer[0]->buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes);
Tao Baoacf4dd12017-02-07 14:32:05 -0800161 }
162 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700163}