blob: 066be8dae10a9feff6a71778249d9ee4516173a3 [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);
AdrianDC2948b6f2016-02-07 03:44:44 +010076
77#ifdef TW_SECONDARY_BRIGHTNESS_PATH
78 fd = open(TW_SECONDARY_BRIGHTNESS_PATH, O_RDWR);
79 if (fd < 0) {
80 perror("cannot open LCD backlight 2");
81 return;
82 }
83 write(fd, blank ? "000" : brightness, 3);
84 close(fd);
85#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +010086#else
87#ifndef TW_NO_SCREEN_BLANK
88 int ret;
89
90 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
91 if (ret < 0)
92 perror("ioctl(): blank");
93#endif
94#endif
95}
96
97static void set_displayed_framebuffer(unsigned n)
98{
99 if (n > 1 || !double_buffered) return;
100
101 vi.yres_virtual = gr_framebuffer[0].height * 2;
102 vi.yoffset = n * gr_framebuffer[0].height;
103 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
104 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
105 perror("active fb swap failed");
106 }
107 displayed_buffer = n;
108}
109
110static GRSurface* fbdev_init(minui_backend* backend) {
thatb9d8f622016-02-22 21:09:09 +0100111 int retry = 20;
112 int fd = -1;
113 while (fd == -1) {
114 fd = open("/dev/graphics/fb0", O_RDWR);
115 if (fd == -1) {
116 if (--retry) {
117 // wait for init to create the device node
118 perror("cannot open fb0 (retrying)");
119 usleep(100000);
120 } else {
121 perror("cannot open fb0 (giving up)");
122 return NULL;
123 }
124 }
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100125 }
126
127 fb_fix_screeninfo fi;
128 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
thatb9d8f622016-02-22 21:09:09 +0100129 perror("failed to get fb0 info (FBIOGET_FSCREENINFO)");
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100130 close(fd);
131 return NULL;
132 }
133
134 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
thatb9d8f622016-02-22 21:09:09 +0100135 perror("failed to get fb0 info (FBIOGET_VSCREENINFO)");
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100136 close(fd);
137 return NULL;
138 }
139
140 // We print this out for informational purposes only, but
141 // throughout we assume that the framebuffer device uses an RGBX
142 // pixel format. This is the case for every development device I
143 // have access to. For some of those devices (eg, hammerhead aka
144 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
145 // different format (XBGR) but actually produces the correct
146 // results on the display when you write RGBX.
147 //
148 // If you have a device that actually *needs* another pixel format
149 // (ie, BGRX, or 565), patches welcome...
150
151 printf("fb0 reports (possibly inaccurate):\n"
152 " vi.bits_per_pixel = %d\n"
153 " vi.red.offset = %3d .length = %3d\n"
154 " vi.green.offset = %3d .length = %3d\n"
155 " vi.blue.offset = %3d .length = %3d\n",
156 vi.bits_per_pixel,
157 vi.red.offset, vi.red.length,
158 vi.green.offset, vi.green.length,
159 vi.blue.offset, vi.blue.length);
160
161 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
162 if (bits == MAP_FAILED) {
163 perror("failed to mmap framebuffer");
164 close(fd);
165 return NULL;
166 }
167
168 memset(bits, 0, fi.smem_len);
169
Ethan Yonker31f855b2016-01-28 22:31:48 -0600170#ifdef RECOVERY_FORCE_RGB_565
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100171 printf("Forcing pixel format: RGB_565\n");
172 vi.blue.offset = 0;
173 vi.green.offset = 5;
174 vi.red.offset = 11;
175 vi.blue.length = 5;
176 vi.green.length = 6;
177 vi.red.length = 5;
178 vi.blue.msb_right = 0;
179 vi.green.msb_right = 0;
180 vi.red.msb_right = 0;
181 vi.transp.offset = 0;
182 vi.transp.length = 0;
183 vi.bits_per_pixel = 16;
184#endif
185
186 gr_framebuffer[0].width = vi.xres;
187 gr_framebuffer[0].height = vi.yres;
188 gr_framebuffer[0].row_bytes = fi.line_length;
189 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
Ethan Yonker31f855b2016-01-28 22:31:48 -0600190#ifdef RECOVERY_GRAPHICS_FORCE_USE_LINELENGTH
191 printf("Forcing line length\n");
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100192 vi.xres_virtual = fi.line_length / gr_framebuffer[0].pixel_bytes;
193#endif
194 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
195 if (vi.bits_per_pixel == 16) {
196 printf("setting GGL_PIXEL_FORMAT_RGB_565\n");
197 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
Ziyan4fea38a2016-01-28 01:19:35 +0100198 } else if (vi.red.offset == 8 || vi.red.offset == 16) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100199 printf("setting GGL_PIXEL_FORMAT_BGRA_8888\n");
200 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_BGRA_8888;
201 } else if (vi.red.offset == 0) {
202 printf("setting GGL_PIXEL_FORMAT_RGBA_8888\n");
203 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBA_8888;
204 } else if (vi.red.offset == 24) {
205 printf("setting GGL_PIXEL_FORMAT_RGBX_8888\n");
206 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
207 } else {
208 if (vi.red.length == 8) {
209 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGBX_8888\n");
210 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
211 } else {
212 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGB_565\n");
213 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
214 }
215 }
216
217 // Drawing directly to the framebuffer takes about 5 times longer.
218 // Instead, we will allocate some memory and draw to that, then
219 // memcpy the data into the framebuffer later.
220 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
221 if (!gr_draw) {
222 perror("failed to allocate gr_draw");
223 close(fd);
224 munmap(bits, fi.smem_len);
225 return NULL;
226 }
227 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
228 gr_draw->data = (unsigned char*) calloc(gr_draw->height * gr_draw->row_bytes, 1);
229 if (!gr_draw->data) {
230 perror("failed to allocate in-memory surface");
231 close(fd);
232 free(gr_draw);
233 munmap(bits, fi.smem_len);
234 return NULL;
235 }
236
237 /* check if we can use double buffering */
238 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
239 double_buffered = true;
240 printf("double buffered\n");
241
242 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
243 gr_framebuffer[1].data = gr_framebuffer[0].data +
244 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
245
246 } else {
247 double_buffered = false;
248 printf("single buffered\n");
249 }
250#if defined(RECOVERY_BGRA)
251 printf("RECOVERY_BGRA\n");
252#endif
253 fb_fd = fd;
254 set_displayed_framebuffer(0);
255
256 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
257
258 fbdev_blank(backend, true);
259 fbdev_blank(backend, false);
260
261 smem_len = fi.smem_len;
262
263 return gr_draw;
264}
265
266static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100267#if defined(RECOVERY_BGRA)
Ethan Yonker871b07f2016-02-01 10:31:59 -0600268 // In case of BGRA, do some byte swapping
269 unsigned int idx;
270 unsigned char tmp;
271 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
272 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
273 idx += 4) {
274 tmp = ucfb_vaddr[idx];
275 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
276 ucfb_vaddr[idx + 2] = tmp;
277 }
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100278#endif
Ethan Yonker871b07f2016-02-01 10:31:59 -0600279#ifndef BOARD_HAS_FLIPPED_SCREEN
280 if (double_buffered) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100281 // Copy from the in-memory surface to the framebuffer.
282 memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data,
283 gr_draw->height * gr_draw->row_bytes);
284 set_displayed_framebuffer(1-displayed_buffer);
285 } else {
286 // Copy from the in-memory surface to the framebuffer.
287 memcpy(gr_framebuffer[0].data, gr_draw->data,
288 gr_draw->height * gr_draw->row_bytes);
289 }
Ethan Yonker871b07f2016-02-01 10:31:59 -0600290#else
291 int gr_active_fb = 0;
292 if (double_buffered)
293 gr_active_fb = 1-displayed_buffer;
294
that506fc802016-02-09 01:42:08 +0100295 /* flip buffer 180 degrees for devices with physically inverted screens */
296 unsigned int row_pixels = gr_draw->row_bytes / gr_framebuffer[0].pixel_bytes;
297 if (gr_framebuffer[0].pixel_bytes == 4) {
298 for (unsigned int y = 0; y < gr_draw->height; ++y) {
299 uint32_t* dst = reinterpret_cast<uint32_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
300 uint32_t* src = reinterpret_cast<uint32_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
301 for (unsigned int x = 0; x < gr_draw->width; ++x)
302 *(dst++) = *(--src);
303 }
304 } else {
305 for (unsigned int y = 0; y < gr_draw->height; ++y) {
306 uint16_t* dst = reinterpret_cast<uint16_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
307 uint16_t* src = reinterpret_cast<uint16_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
308 for (unsigned int x = 0; x < gr_draw->width; ++x)
309 *(dst++) = *(--src);
Ethan Yonker871b07f2016-02-01 10:31:59 -0600310 }
311 }
that506fc802016-02-09 01:42:08 +0100312
Ethan Yonker871b07f2016-02-01 10:31:59 -0600313 if (double_buffered)
314 set_displayed_framebuffer(1-displayed_buffer);
315#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100316 return gr_draw;
317}
318
319static void fbdev_exit(minui_backend* backend __unused) {
320 close(fb_fd);
321 fb_fd = -1;
322
323 if (gr_draw) {
324 free(gr_draw->data);
325 free(gr_draw);
326 }
327 gr_draw = NULL;
328 munmap(gr_framebuffer[0].data, smem_len);
329}