blob: 24294be0ce9b22ecbd862720a4969fabbb98020b [file] [log] [blame]
Ethan Yonkerfbb43532015-12-28 21:54:50 +01001/*
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>
19#include <string.h>
20#include <unistd.h>
21
22#include <fcntl.h>
23#include <stdio.h>
24
25#include <sys/cdefs.h>
26#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#include <pixelflinger/pixelflinger.h>
36
37static GRSurface* fbdev_init(minui_backend*);
38static GRSurface* fbdev_flip(minui_backend*);
39static void fbdev_blank(minui_backend*, bool);
40static void fbdev_exit(minui_backend*);
41
42static GRSurface gr_framebuffer[2];
43static bool double_buffered;
44static GRSurface* gr_draw = NULL;
45static int displayed_buffer;
46
47static fb_var_screeninfo vi;
48static int fb_fd = -1;
49static __u32 smem_len;
50
51static minui_backend my_backend = {
52 .init = fbdev_init,
53 .flip = fbdev_flip,
54 .blank = fbdev_blank,
55 .exit = fbdev_exit,
56};
57
58minui_backend* open_fbdev() {
59 return &my_backend;
60}
61
62static void fbdev_blank(minui_backend* backend __unused, bool blank)
63{
64#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
65 int fd;
66 char brightness[4];
67 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
68
69 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
70 if (fd < 0) {
71 perror("cannot open LCD backlight");
72 return;
73 }
74 write(fd, blank ? "000" : brightness, 3);
75 close(fd);
76#else
77#ifndef TW_NO_SCREEN_BLANK
78 int ret;
79
80 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
81 if (ret < 0)
82 perror("ioctl(): blank");
83#endif
84#endif
85}
86
87static void set_displayed_framebuffer(unsigned n)
88{
89 if (n > 1 || !double_buffered) return;
90
91 vi.yres_virtual = gr_framebuffer[0].height * 2;
92 vi.yoffset = n * gr_framebuffer[0].height;
93 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
94 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
95 perror("active fb swap failed");
96 }
97 displayed_buffer = n;
98}
99
100static GRSurface* fbdev_init(minui_backend* backend) {
101 int fd = open("/dev/graphics/fb0", O_RDWR);
102 if (fd == -1) {
103 perror("cannot open fb0");
104 return NULL;
105 }
106
107 fb_fix_screeninfo fi;
108 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
109 perror("failed to get fb0 info");
110 close(fd);
111 return NULL;
112 }
113
114 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
115 perror("failed to get fb0 info");
116 close(fd);
117 return NULL;
118 }
119
120 // We print this out for informational purposes only, but
121 // throughout we assume that the framebuffer device uses an RGBX
122 // pixel format. This is the case for every development device I
123 // have access to. For some of those devices (eg, hammerhead aka
124 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
125 // different format (XBGR) but actually produces the correct
126 // results on the display when you write RGBX.
127 //
128 // If you have a device that actually *needs* another pixel format
129 // (ie, BGRX, or 565), patches welcome...
130
131 printf("fb0 reports (possibly inaccurate):\n"
132 " vi.bits_per_pixel = %d\n"
133 " vi.red.offset = %3d .length = %3d\n"
134 " vi.green.offset = %3d .length = %3d\n"
135 " vi.blue.offset = %3d .length = %3d\n",
136 vi.bits_per_pixel,
137 vi.red.offset, vi.red.length,
138 vi.green.offset, vi.green.length,
139 vi.blue.offset, vi.blue.length);
140
141 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
142 if (bits == MAP_FAILED) {
143 perror("failed to mmap framebuffer");
144 close(fd);
145 return NULL;
146 }
147
148 memset(bits, 0, fi.smem_len);
149
Ethan Yonker31f855b2016-01-28 22:31:48 -0600150#ifdef RECOVERY_FORCE_RGB_565
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100151 printf("Forcing pixel format: RGB_565\n");
152 vi.blue.offset = 0;
153 vi.green.offset = 5;
154 vi.red.offset = 11;
155 vi.blue.length = 5;
156 vi.green.length = 6;
157 vi.red.length = 5;
158 vi.blue.msb_right = 0;
159 vi.green.msb_right = 0;
160 vi.red.msb_right = 0;
161 vi.transp.offset = 0;
162 vi.transp.length = 0;
163 vi.bits_per_pixel = 16;
164#endif
165
166 gr_framebuffer[0].width = vi.xres;
167 gr_framebuffer[0].height = vi.yres;
168 gr_framebuffer[0].row_bytes = fi.line_length;
169 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
Ethan Yonker31f855b2016-01-28 22:31:48 -0600170#ifdef RECOVERY_GRAPHICS_FORCE_USE_LINELENGTH
171 printf("Forcing line length\n");
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100172 vi.xres_virtual = fi.line_length / gr_framebuffer[0].pixel_bytes;
173#endif
174 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
175 if (vi.bits_per_pixel == 16) {
176 printf("setting GGL_PIXEL_FORMAT_RGB_565\n");
177 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
Ziyan4fea38a2016-01-28 01:19:35 +0100178 } else if (vi.red.offset == 8 || vi.red.offset == 16) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100179 printf("setting GGL_PIXEL_FORMAT_BGRA_8888\n");
180 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_BGRA_8888;
181 } else if (vi.red.offset == 0) {
182 printf("setting GGL_PIXEL_FORMAT_RGBA_8888\n");
183 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBA_8888;
184 } else if (vi.red.offset == 24) {
185 printf("setting GGL_PIXEL_FORMAT_RGBX_8888\n");
186 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
187 } else {
188 if (vi.red.length == 8) {
189 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGBX_8888\n");
190 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
191 } else {
192 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGB_565\n");
193 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
194 }
195 }
196
197 // Drawing directly to the framebuffer takes about 5 times longer.
198 // Instead, we will allocate some memory and draw to that, then
199 // memcpy the data into the framebuffer later.
200 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
201 if (!gr_draw) {
202 perror("failed to allocate gr_draw");
203 close(fd);
204 munmap(bits, fi.smem_len);
205 return NULL;
206 }
207 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
208 gr_draw->data = (unsigned char*) calloc(gr_draw->height * gr_draw->row_bytes, 1);
209 if (!gr_draw->data) {
210 perror("failed to allocate in-memory surface");
211 close(fd);
212 free(gr_draw);
213 munmap(bits, fi.smem_len);
214 return NULL;
215 }
216
217 /* check if we can use double buffering */
218 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
219 double_buffered = true;
220 printf("double buffered\n");
221
222 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
223 gr_framebuffer[1].data = gr_framebuffer[0].data +
224 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
225
226 } else {
227 double_buffered = false;
228 printf("single buffered\n");
229 }
230#if defined(RECOVERY_BGRA)
231 printf("RECOVERY_BGRA\n");
232#endif
233 fb_fd = fd;
234 set_displayed_framebuffer(0);
235
236 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
237
238 fbdev_blank(backend, true);
239 fbdev_blank(backend, false);
240
241 smem_len = fi.smem_len;
242
243 return gr_draw;
244}
245
246static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100247#if defined(RECOVERY_BGRA)
Ethan Yonker871b07f2016-02-01 10:31:59 -0600248 // In case of BGRA, do some byte swapping
249 unsigned int idx;
250 unsigned char tmp;
251 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
252 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
253 idx += 4) {
254 tmp = ucfb_vaddr[idx];
255 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
256 ucfb_vaddr[idx + 2] = tmp;
257 }
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100258#endif
Ethan Yonker871b07f2016-02-01 10:31:59 -0600259#ifndef BOARD_HAS_FLIPPED_SCREEN
260 if (double_buffered) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100261 // Copy from the in-memory surface to the framebuffer.
262 memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data,
263 gr_draw->height * gr_draw->row_bytes);
264 set_displayed_framebuffer(1-displayed_buffer);
265 } else {
266 // Copy from the in-memory surface to the framebuffer.
267 memcpy(gr_framebuffer[0].data, gr_draw->data,
268 gr_draw->height * gr_draw->row_bytes);
269 }
Ethan Yonker871b07f2016-02-01 10:31:59 -0600270#else
271 int gr_active_fb = 0;
272 if (double_buffered)
273 gr_active_fb = 1-displayed_buffer;
274
275 /* flip buffer 180 degrees for devices with physicaly inverted screens */
276 unsigned int i, j;
277 for (i = 0; i < vi.yres; i++) {
278 for (j = 0; j < vi.xres; j++) {
279 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * gr_framebuffer[0].pixel_bytes,
280 gr_draw->data + ((vi.yres - i - 1) * vi.xres_virtual + vi.xres - j - 1) * gr_framebuffer[0].pixel_bytes,
281 gr_framebuffer[0].pixel_bytes);
282 }
283 }
284 if (double_buffered)
285 set_displayed_framebuffer(1-displayed_buffer);
286#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100287 return gr_draw;
288}
289
290static void fbdev_exit(minui_backend* backend __unused) {
291 close(fb_fd);
292 fb_fd = -1;
293
294 if (gr_draw) {
295 free(gr_draw->data);
296 free(gr_draw);
297 }
298 gr_draw = NULL;
299 munmap(gr_framebuffer[0].data, smem_len);
300}