blob: 512a7d0e784dc37a23d3866dfb1c765de74103de [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
17#include <stdbool.h>
18#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Doug Zongker5290f202014-03-11 13:22:04 -070020#include <unistd.h>
21
22#include <fcntl.h>
23#include <stdio.h>
24
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070025#include <sys/cdefs.h>
Doug Zongker5290f202014-03-11 13:22:04 -070026#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29
30#include <linux/fb.h>
31#include <linux/kd.h>
32
33#include "minui.h"
34#include "graphics.h"
35
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070036static GRSurface* fbdev_init(minui_backend*);
37static GRSurface* fbdev_flip(minui_backend*);
Doug Zongker5290f202014-03-11 13:22:04 -070038static void fbdev_blank(minui_backend*, bool);
39static void fbdev_exit(minui_backend*);
40
41static GRSurface gr_framebuffer[2];
42static bool double_buffered;
43static GRSurface* gr_draw = NULL;
44static int displayed_buffer;
45
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070046static fb_var_screeninfo vi;
Doug Zongker5290f202014-03-11 13:22:04 -070047static int fb_fd = -1;
48
49static minui_backend my_backend = {
50 .init = fbdev_init,
51 .flip = fbdev_flip,
52 .blank = fbdev_blank,
53 .exit = fbdev_exit,
54};
55
56minui_backend* open_fbdev() {
57 return &my_backend;
58}
59
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070060static void fbdev_blank(minui_backend* backend __unused, bool blank)
Doug Zongker5290f202014-03-11 13:22:04 -070061{
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050062#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
63 int fd;
64 char brightness[4];
65 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
66
67 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
68 if (fd < 0) {
69 perror("cannot open LCD backlight");
70 return;
71 }
72 write(fd, blank ? "000" : brightness, 3);
73 close(fd);
74#else
Doug Zongker5290f202014-03-11 13:22:04 -070075 int ret;
76
77 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
78 if (ret < 0)
79 perror("ioctl(): blank");
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050080#endif
Doug Zongker5290f202014-03-11 13:22:04 -070081}
82
83static void set_displayed_framebuffer(unsigned n)
84{
85 if (n > 1 || !double_buffered) return;
86
87 vi.yres_virtual = gr_framebuffer[0].height * 2;
88 vi.yoffset = n * gr_framebuffer[0].height;
89 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
90 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
91 perror("active fb swap failed");
92 }
93 displayed_buffer = n;
94}
95
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070096static GRSurface* fbdev_init(minui_backend* backend) {
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070097 int fd = open("/dev/graphics/fb0", O_RDWR);
98 if (fd == -1) {
Doug Zongker5290f202014-03-11 13:22:04 -070099 perror("cannot open fb0");
100 return NULL;
101 }
102
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700103 fb_fix_screeninfo fi;
Doug Zongker5290f202014-03-11 13:22:04 -0700104 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
105 perror("failed to get fb0 info");
106 close(fd);
107 return NULL;
108 }
109
110 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
111 perror("failed to get fb0 info");
112 close(fd);
113 return NULL;
114 }
115
116 // We print this out for informational purposes only, but
117 // throughout we assume that the framebuffer device uses an RGBX
118 // pixel format. This is the case for every development device I
119 // have access to. For some of those devices (eg, hammerhead aka
120 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
121 // different format (XBGR) but actually produces the correct
122 // results on the display when you write RGBX.
123 //
124 // If you have a device that actually *needs* another pixel format
125 // (ie, BGRX, or 565), patches welcome...
126
127 printf("fb0 reports (possibly inaccurate):\n"
128 " vi.bits_per_pixel = %d\n"
129 " vi.red.offset = %3d .length = %3d\n"
130 " vi.green.offset = %3d .length = %3d\n"
131 " vi.blue.offset = %3d .length = %3d\n",
132 vi.bits_per_pixel,
133 vi.red.offset, vi.red.length,
134 vi.green.offset, vi.green.length,
135 vi.blue.offset, vi.blue.length);
136
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700137 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Doug Zongker5290f202014-03-11 13:22:04 -0700138 if (bits == MAP_FAILED) {
139 perror("failed to mmap framebuffer");
140 close(fd);
141 return NULL;
142 }
143
Joey Panba545d72014-06-04 20:47:46 +0800144 memset(bits, 0, fi.smem_len);
145
Doug Zongker5290f202014-03-11 13:22:04 -0700146 gr_framebuffer[0].width = vi.xres;
147 gr_framebuffer[0].height = vi.yres;
148 gr_framebuffer[0].row_bytes = fi.line_length;
149 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700150 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
Doug Zongker5290f202014-03-11 13:22:04 -0700151 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
152
153 /* check if we can use double buffering */
154 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
155 double_buffered = true;
156
157 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
158 gr_framebuffer[1].data = gr_framebuffer[0].data +
159 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
160
161 gr_draw = gr_framebuffer+1;
162
163 } else {
164 double_buffered = false;
165
166 // Without double-buffering, we allocate RAM for a buffer to
167 // draw in, and then "flipping" the buffer consists of a
168 // memcpy from the buffer we allocated to the framebuffer.
169
170 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
171 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
172 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
173 if (!gr_draw->data) {
174 perror("failed to allocate in-memory surface");
175 return NULL;
176 }
177 }
178
179 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
180 fb_fd = fd;
181 set_displayed_framebuffer(0);
182
183 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
184
185 fbdev_blank(backend, true);
186 fbdev_blank(backend, false);
187
188 return gr_draw;
189}
190
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700191static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700192 if (double_buffered) {
Heather Lee Wilsone5879c32014-11-14 09:57:54 -0800193#if defined(RECOVERY_BGRA)
194 // In case of BGRA, do some byte swapping
195 unsigned int idx;
196 unsigned char tmp;
197 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
198 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
199 idx += 4) {
200 tmp = ucfb_vaddr[idx];
201 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
202 ucfb_vaddr[idx + 2] = tmp;
203 }
204#endif
Doug Zongker5290f202014-03-11 13:22:04 -0700205 // Change gr_draw to point to the buffer currently displayed,
206 // then flip the driver so we're displaying the other buffer
207 // instead.
208 gr_draw = gr_framebuffer + displayed_buffer;
209 set_displayed_framebuffer(1-displayed_buffer);
210 } else {
211 // Copy from the in-memory surface to the framebuffer.
212 memcpy(gr_framebuffer[0].data, gr_draw->data,
213 gr_draw->height * gr_draw->row_bytes);
214 }
215 return gr_draw;
216}
217
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700218static void fbdev_exit(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700219 close(fb_fd);
220 fb_fd = -1;
221
222 if (!double_buffered && gr_draw) {
223 free(gr_draw->data);
224 free(gr_draw);
225 }
226 gr_draw = NULL;
227}