blob: 353a070fc9c28685d40f0f617554090a70bef05c [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"
Doug Zongker5290f202014-03-11 13:22:04 -070018
19#include <fcntl.h>
Tao Baoacf4dd12017-02-07 14:32:05 -080020#include <linux/fb.h>
Doug Zongker5290f202014-03-11 13:22:04 -070021#include <stdio.h>
Doug Zongker5290f202014-03-11 13:22:04 -070022#include <stdlib.h>
23#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) {
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050042#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 Zongker5290f202014-03-11 13:22:04 -070055 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 Yonkerc798c9c2015-10-09 11:15:26 -050060#endif
Doug Zongker5290f202014-03-11 13:22:04 -070061}
62
Tao Bao4a22b282018-10-31 00:00:31 -070063void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) {
Tao Baoacf4dd12017-02-07 14:32:05 -080064 if (n > 1 || !double_buffered) return;
Doug Zongker5290f202014-03-11 13:22:04 -070065
Tao Bao4a22b282018-10-31 00:00:31 -070066 vi.yres_virtual = gr_framebuffer[0]->height * 2;
67 vi.yoffset = n * gr_framebuffer[0]->height;
68 vi.bits_per_pixel = gr_framebuffer[0]->pixel_bytes * 8;
Tao Baoacf4dd12017-02-07 14:32:05 -080069 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
70 perror("active fb swap failed");
71 }
72 displayed_buffer = n;
Doug Zongker5290f202014-03-11 13:22:04 -070073}
74
Tao Bao557fa1f2017-02-07 12:51:00 -080075GRSurface* MinuiBackendFbdev::Init() {
Tao Baob5492432019-01-16 09:29:17 -080076 android::base::unique_fd fd(open("/dev/graphics/fb0", O_RDWR | O_CLOEXEC));
Tao Baoacf4dd12017-02-07 14:32:05 -080077 if (fd == -1) {
78 perror("cannot open fb0");
79 return nullptr;
80 }
81
82 fb_fix_screeninfo fi;
83 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
84 perror("failed to get fb0 info");
Tao Baoacf4dd12017-02-07 14:32:05 -080085 return nullptr;
86 }
87
88 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
89 perror("failed to get fb0 info");
Tao Baoacf4dd12017-02-07 14:32:05 -080090 return nullptr;
91 }
92
93 // We print this out for informational purposes only, but
94 // throughout we assume that the framebuffer device uses an RGBX
95 // pixel format. This is the case for every development device I
96 // have access to. For some of those devices (eg, hammerhead aka
97 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
98 // different format (XBGR) but actually produces the correct
99 // results on the display when you write RGBX.
100 //
101 // If you have a device that actually *needs* another pixel format
102 // (ie, BGRX, or 565), patches welcome...
103
104 printf(
105 "fb0 reports (possibly inaccurate):\n"
106 " vi.bits_per_pixel = %d\n"
107 " vi.red.offset = %3d .length = %3d\n"
108 " vi.green.offset = %3d .length = %3d\n"
109 " vi.blue.offset = %3d .length = %3d\n",
110 vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length,
111 vi.blue.offset, vi.blue.length);
112
113 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
114 if (bits == MAP_FAILED) {
115 perror("failed to mmap framebuffer");
Tao Baoacf4dd12017-02-07 14:32:05 -0800116 return nullptr;
117 }
118
119 memset(bits, 0, fi.smem_len);
120
Tao Bao4a22b282018-10-31 00:00:31 -0700121 gr_framebuffer[0] =
122 GRSurfaceFbdev::Create(vi.xres, vi.yres, fi.line_length, vi.bits_per_pixel / 8);
123 gr_framebuffer[0]->buffer_ = static_cast<uint8_t*>(bits);
124 memset(gr_framebuffer[0]->buffer_, 0, gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes);
125
126 gr_framebuffer[1] =
127 GRSurfaceFbdev::Create(gr_framebuffer[0]->width, gr_framebuffer[0]->height,
128 gr_framebuffer[0]->row_bytes, gr_framebuffer[0]->pixel_bytes);
Tao Baoacf4dd12017-02-07 14:32:05 -0800129
130 /* check if we can use double buffering */
131 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
132 double_buffered = true;
133
Tao Bao4a22b282018-10-31 00:00:31 -0700134 gr_framebuffer[1]->buffer_ =
135 gr_framebuffer[0]->buffer_ + gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes;
Tao Baoacf4dd12017-02-07 14:32:05 -0800136 } else {
137 double_buffered = false;
138
Tao Bao4a22b282018-10-31 00:00:31 -0700139 // Without double-buffering, we allocate RAM for a buffer to draw in, and then "flipping" the
140 // buffer consists of a memcpy from the buffer we allocated to the framebuffer.
141 memory_buffer.resize(gr_framebuffer[1]->height * gr_framebuffer[1]->row_bytes);
142 gr_framebuffer[1]->buffer_ = memory_buffer.data();
Tao Baoacf4dd12017-02-07 14:32:05 -0800143 }
Doug Zongker5290f202014-03-11 13:22:04 -0700144
Tao Bao4a22b282018-10-31 00:00:31 -0700145 gr_draw = gr_framebuffer[1].get();
Tao Bao92bdb5a2018-10-21 12:12:37 -0700146 memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes);
Tao Baof65d48b2018-11-02 09:34:49 -0700147 fb_fd = std::move(fd);
Tao Bao557fa1f2017-02-07 12:51:00 -0800148 SetDisplayedFramebuffer(0);
Doug Zongker5290f202014-03-11 13:22:04 -0700149
Tao Baodd789822018-11-26 16:28:07 -0800150 printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height);
Doug Zongker5290f202014-03-11 13:22:04 -0700151
Tao Bao557fa1f2017-02-07 12:51:00 -0800152 Blank(true);
153 Blank(false);
Doug Zongker5290f202014-03-11 13:22:04 -0700154
Tao Baoacf4dd12017-02-07 14:32:05 -0800155 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700156}
157
Tao Bao557fa1f2017-02-07 12:51:00 -0800158GRSurface* MinuiBackendFbdev::Flip() {
Tao Baoacf4dd12017-02-07 14:32:05 -0800159 if (double_buffered) {
Tao Bao4a22b282018-10-31 00:00:31 -0700160 // Change gr_draw to point to the buffer currently displayed, then flip the driver so we're
161 // displaying the other buffer instead.
162 gr_draw = gr_framebuffer[displayed_buffer].get();
Tao Bao557fa1f2017-02-07 12:51:00 -0800163 SetDisplayedFramebuffer(1 - displayed_buffer);
Tao Baoacf4dd12017-02-07 14:32:05 -0800164 } else {
165 // Copy from the in-memory surface to the framebuffer.
Tao Bao4a22b282018-10-31 00:00:31 -0700166 memcpy(gr_framebuffer[0]->buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes);
Tao Baoacf4dd12017-02-07 14:32:05 -0800167 }
168 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700169}