blob: afc4151c92a0801a735262746e234997f593f068 [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) {
111 int fd = open("/dev/graphics/fb0", O_RDWR);
112 if (fd == -1) {
113 perror("cannot open fb0");
114 return NULL;
115 }
116
117 fb_fix_screeninfo fi;
118 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
119 perror("failed to get fb0 info");
120 close(fd);
121 return NULL;
122 }
123
124 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
125 perror("failed to get fb0 info");
126 close(fd);
127 return NULL;
128 }
129
130 // We print this out for informational purposes only, but
131 // throughout we assume that the framebuffer device uses an RGBX
132 // pixel format. This is the case for every development device I
133 // have access to. For some of those devices (eg, hammerhead aka
134 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
135 // different format (XBGR) but actually produces the correct
136 // results on the display when you write RGBX.
137 //
138 // If you have a device that actually *needs* another pixel format
139 // (ie, BGRX, or 565), patches welcome...
140
141 printf("fb0 reports (possibly inaccurate):\n"
142 " vi.bits_per_pixel = %d\n"
143 " vi.red.offset = %3d .length = %3d\n"
144 " vi.green.offset = %3d .length = %3d\n"
145 " vi.blue.offset = %3d .length = %3d\n",
146 vi.bits_per_pixel,
147 vi.red.offset, vi.red.length,
148 vi.green.offset, vi.green.length,
149 vi.blue.offset, vi.blue.length);
150
151 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
152 if (bits == MAP_FAILED) {
153 perror("failed to mmap framebuffer");
154 close(fd);
155 return NULL;
156 }
157
158 memset(bits, 0, fi.smem_len);
159
Ethan Yonker31f855b2016-01-28 22:31:48 -0600160#ifdef RECOVERY_FORCE_RGB_565
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100161 printf("Forcing pixel format: RGB_565\n");
162 vi.blue.offset = 0;
163 vi.green.offset = 5;
164 vi.red.offset = 11;
165 vi.blue.length = 5;
166 vi.green.length = 6;
167 vi.red.length = 5;
168 vi.blue.msb_right = 0;
169 vi.green.msb_right = 0;
170 vi.red.msb_right = 0;
171 vi.transp.offset = 0;
172 vi.transp.length = 0;
173 vi.bits_per_pixel = 16;
174#endif
175
176 gr_framebuffer[0].width = vi.xres;
177 gr_framebuffer[0].height = vi.yres;
178 gr_framebuffer[0].row_bytes = fi.line_length;
179 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
Ethan Yonker31f855b2016-01-28 22:31:48 -0600180#ifdef RECOVERY_GRAPHICS_FORCE_USE_LINELENGTH
181 printf("Forcing line length\n");
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100182 vi.xres_virtual = fi.line_length / gr_framebuffer[0].pixel_bytes;
183#endif
184 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
185 if (vi.bits_per_pixel == 16) {
186 printf("setting GGL_PIXEL_FORMAT_RGB_565\n");
187 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
Ziyan4fea38a2016-01-28 01:19:35 +0100188 } else if (vi.red.offset == 8 || vi.red.offset == 16) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100189 printf("setting GGL_PIXEL_FORMAT_BGRA_8888\n");
190 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_BGRA_8888;
191 } else if (vi.red.offset == 0) {
192 printf("setting GGL_PIXEL_FORMAT_RGBA_8888\n");
193 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBA_8888;
194 } else if (vi.red.offset == 24) {
195 printf("setting GGL_PIXEL_FORMAT_RGBX_8888\n");
196 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
197 } else {
198 if (vi.red.length == 8) {
199 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGBX_8888\n");
200 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGBX_8888;
201 } else {
202 printf("No valid pixel format detected, trying GGL_PIXEL_FORMAT_RGB_565\n");
203 gr_framebuffer[0].format = GGL_PIXEL_FORMAT_RGB_565;
204 }
205 }
206
207 // Drawing directly to the framebuffer takes about 5 times longer.
208 // Instead, we will allocate some memory and draw to that, then
209 // memcpy the data into the framebuffer later.
210 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
211 if (!gr_draw) {
212 perror("failed to allocate gr_draw");
213 close(fd);
214 munmap(bits, fi.smem_len);
215 return NULL;
216 }
217 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
218 gr_draw->data = (unsigned char*) calloc(gr_draw->height * gr_draw->row_bytes, 1);
219 if (!gr_draw->data) {
220 perror("failed to allocate in-memory surface");
221 close(fd);
222 free(gr_draw);
223 munmap(bits, fi.smem_len);
224 return NULL;
225 }
226
227 /* check if we can use double buffering */
228 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
229 double_buffered = true;
230 printf("double buffered\n");
231
232 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
233 gr_framebuffer[1].data = gr_framebuffer[0].data +
234 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
235
236 } else {
237 double_buffered = false;
238 printf("single buffered\n");
239 }
240#if defined(RECOVERY_BGRA)
241 printf("RECOVERY_BGRA\n");
242#endif
243 fb_fd = fd;
244 set_displayed_framebuffer(0);
245
246 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
247
248 fbdev_blank(backend, true);
249 fbdev_blank(backend, false);
250
251 smem_len = fi.smem_len;
252
253 return gr_draw;
254}
255
256static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100257#if defined(RECOVERY_BGRA)
Ethan Yonker871b07f2016-02-01 10:31:59 -0600258 // In case of BGRA, do some byte swapping
259 unsigned int idx;
260 unsigned char tmp;
261 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
262 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
263 idx += 4) {
264 tmp = ucfb_vaddr[idx];
265 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
266 ucfb_vaddr[idx + 2] = tmp;
267 }
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100268#endif
Ethan Yonker871b07f2016-02-01 10:31:59 -0600269#ifndef BOARD_HAS_FLIPPED_SCREEN
270 if (double_buffered) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100271 // Copy from the in-memory surface to the framebuffer.
272 memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data,
273 gr_draw->height * gr_draw->row_bytes);
274 set_displayed_framebuffer(1-displayed_buffer);
275 } else {
276 // Copy from the in-memory surface to the framebuffer.
277 memcpy(gr_framebuffer[0].data, gr_draw->data,
278 gr_draw->height * gr_draw->row_bytes);
279 }
Ethan Yonker871b07f2016-02-01 10:31:59 -0600280#else
281 int gr_active_fb = 0;
282 if (double_buffered)
283 gr_active_fb = 1-displayed_buffer;
284
that506fc802016-02-09 01:42:08 +0100285 /* flip buffer 180 degrees for devices with physically inverted screens */
286 unsigned int row_pixels = gr_draw->row_bytes / gr_framebuffer[0].pixel_bytes;
287 if (gr_framebuffer[0].pixel_bytes == 4) {
288 for (unsigned int y = 0; y < gr_draw->height; ++y) {
289 uint32_t* dst = reinterpret_cast<uint32_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
290 uint32_t* src = reinterpret_cast<uint32_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
291 for (unsigned int x = 0; x < gr_draw->width; ++x)
292 *(dst++) = *(--src);
293 }
294 } else {
295 for (unsigned int y = 0; y < gr_draw->height; ++y) {
296 uint16_t* dst = reinterpret_cast<uint16_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
297 uint16_t* src = reinterpret_cast<uint16_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
298 for (unsigned int x = 0; x < gr_draw->width; ++x)
299 *(dst++) = *(--src);
Ethan Yonker871b07f2016-02-01 10:31:59 -0600300 }
301 }
that506fc802016-02-09 01:42:08 +0100302
Ethan Yonker871b07f2016-02-01 10:31:59 -0600303 if (double_buffered)
304 set_displayed_framebuffer(1-displayed_buffer);
305#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100306 return gr_draw;
307}
308
309static void fbdev_exit(minui_backend* backend __unused) {
310 close(fb_fd);
311 fb_fd = -1;
312
313 if (gr_draw) {
314 free(gr_draw->data);
315 free(gr_draw);
316 }
317 gr_draw = NULL;
318 munmap(gr_framebuffer[0].data, smem_len);
319}