blob: bb91a574de5a64c632da5ef9c6e8d65ed8cdf937 [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 Bao0ecbd762017-01-16 21:16:58 -080029#include "minui/minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070030
Tao Bao557fa1f2017-02-07 12:51:00 -080031MinuiBackendFbdev::MinuiBackendFbdev() : gr_draw(nullptr), fb_fd(-1) {}
Doug Zongker5290f202014-03-11 13:22:04 -070032
Tao Bao557fa1f2017-02-07 12:51:00 -080033void MinuiBackendFbdev::Blank(bool blank) {
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050034#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 Zongker5290f202014-03-11 13:22:04 -070047 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 Yonkerc798c9c2015-10-09 11:15:26 -050052#endif
Doug Zongker5290f202014-03-11 13:22:04 -070053}
54
Tao Bao557fa1f2017-02-07 12:51:00 -080055void MinuiBackendFbdev::SetDisplayedFramebuffer(unsigned n) {
Tao Baoacf4dd12017-02-07 14:32:05 -080056 if (n > 1 || !double_buffered) return;
Doug Zongker5290f202014-03-11 13:22:04 -070057
Tao Baoacf4dd12017-02-07 14:32:05 -080058 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 Zongker5290f202014-03-11 13:22:04 -070065}
66
Tao Bao557fa1f2017-02-07 12:51:00 -080067GRSurface* MinuiBackendFbdev::Init() {
Tao Baoacf4dd12017-02-07 14:32:05 -080068 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 Zongker5290f202014-03-11 13:22:04 -0700146 }
Tao Baoacf4dd12017-02-07 14:32:05 -0800147 }
Doug Zongker5290f202014-03-11 13:22:04 -0700148
Tao Baoacf4dd12017-02-07 14:32:05 -0800149 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
150 fb_fd = fd;
Tao Bao557fa1f2017-02-07 12:51:00 -0800151 SetDisplayedFramebuffer(0);
Doug Zongker5290f202014-03-11 13:22:04 -0700152
Tao Baoacf4dd12017-02-07 14:32:05 -0800153 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
Doug Zongker5290f202014-03-11 13:22:04 -0700154
Tao Bao557fa1f2017-02-07 12:51:00 -0800155 Blank(true);
156 Blank(false);
Doug Zongker5290f202014-03-11 13:22:04 -0700157
Tao Baoacf4dd12017-02-07 14:32:05 -0800158 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700159}
160
Tao Bao557fa1f2017-02-07 12:51:00 -0800161GRSurface* MinuiBackendFbdev::Flip() {
Tao Baoacf4dd12017-02-07 14:32:05 -0800162 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 Bao557fa1f2017-02-07 12:51:00 -0800167 SetDisplayedFramebuffer(1 - displayed_buffer);
Tao Baoacf4dd12017-02-07 14:32:05 -0800168 } 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 Zongker5290f202014-03-11 13:22:04 -0700173}
174
Tao Bao557fa1f2017-02-07 12:51:00 -0800175MinuiBackendFbdev::~MinuiBackendFbdev() {
Tao Baoacf4dd12017-02-07 14:32:05 -0800176 close(fb_fd);
177 fb_fd = -1;
Doug Zongker5290f202014-03-11 13:22:04 -0700178
Tao Baoacf4dd12017-02-07 14:32:05 -0800179 if (!double_buffered && gr_draw) {
180 free(gr_draw->data);
181 free(gr_draw);
182 }
183 gr_draw = nullptr;
Doug Zongker5290f202014-03-11 13:22:04 -0700184}