blob: 4a5b5b513f681cee7e7e0e5410bcfe5fa9c15194 [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
36static gr_surface fbdev_init(minui_backend*);
37static gr_surface fbdev_flip(minui_backend*);
38static 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
46static struct fb_var_screeninfo vi;
47static 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{
62 int ret;
63
64 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
65 if (ret < 0)
66 perror("ioctl(): blank");
67}
68
69static void set_displayed_framebuffer(unsigned n)
70{
71 if (n > 1 || !double_buffered) return;
72
73 vi.yres_virtual = gr_framebuffer[0].height * 2;
74 vi.yoffset = n * gr_framebuffer[0].height;
75 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
76 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
77 perror("active fb swap failed");
78 }
79 displayed_buffer = n;
80}
81
82static gr_surface fbdev_init(minui_backend* backend) {
83 int fd;
84 void *bits;
85
86 struct fb_fix_screeninfo fi;
87
88 fd = open("/dev/graphics/fb0", O_RDWR);
89 if (fd < 0) {
90 perror("cannot open fb0");
91 return NULL;
92 }
93
94 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
95 perror("failed to get fb0 info");
96 close(fd);
97 return NULL;
98 }
99
100 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
101 perror("failed to get fb0 info");
102 close(fd);
103 return NULL;
104 }
105
106 // We print this out for informational purposes only, but
107 // throughout we assume that the framebuffer device uses an RGBX
108 // pixel format. This is the case for every development device I
109 // have access to. For some of those devices (eg, hammerhead aka
110 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
111 // different format (XBGR) but actually produces the correct
112 // results on the display when you write RGBX.
113 //
114 // If you have a device that actually *needs* another pixel format
115 // (ie, BGRX, or 565), patches welcome...
116
117 printf("fb0 reports (possibly inaccurate):\n"
118 " vi.bits_per_pixel = %d\n"
119 " vi.red.offset = %3d .length = %3d\n"
120 " vi.green.offset = %3d .length = %3d\n"
121 " vi.blue.offset = %3d .length = %3d\n",
122 vi.bits_per_pixel,
123 vi.red.offset, vi.red.length,
124 vi.green.offset, vi.green.length,
125 vi.blue.offset, vi.blue.length);
126
127 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
128 if (bits == MAP_FAILED) {
129 perror("failed to mmap framebuffer");
130 close(fd);
131 return NULL;
132 }
133
Joey Panba545d72014-06-04 20:47:46 +0800134 memset(bits, 0, fi.smem_len);
135
Doug Zongker5290f202014-03-11 13:22:04 -0700136 gr_framebuffer[0].width = vi.xres;
137 gr_framebuffer[0].height = vi.yres;
138 gr_framebuffer[0].row_bytes = fi.line_length;
139 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
140 gr_framebuffer[0].data = bits;
141 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
142
143 /* check if we can use double buffering */
144 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
145 double_buffered = true;
146
147 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
148 gr_framebuffer[1].data = gr_framebuffer[0].data +
149 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
150
151 gr_draw = gr_framebuffer+1;
152
153 } else {
154 double_buffered = false;
155
156 // Without double-buffering, we allocate RAM for a buffer to
157 // draw in, and then "flipping" the buffer consists of a
158 // memcpy from the buffer we allocated to the framebuffer.
159
160 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
161 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
162 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
163 if (!gr_draw->data) {
164 perror("failed to allocate in-memory surface");
165 return NULL;
166 }
167 }
168
169 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
170 fb_fd = fd;
171 set_displayed_framebuffer(0);
172
173 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
174
175 fbdev_blank(backend, true);
176 fbdev_blank(backend, false);
177
178 return gr_draw;
179}
180
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700181static gr_surface fbdev_flip(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700182 if (double_buffered) {
Heather Lee Wilsone5879c32014-11-14 09:57:54 -0800183#if defined(RECOVERY_BGRA)
184 // In case of BGRA, do some byte swapping
185 unsigned int idx;
186 unsigned char tmp;
187 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
188 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
189 idx += 4) {
190 tmp = ucfb_vaddr[idx];
191 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
192 ucfb_vaddr[idx + 2] = tmp;
193 }
194#endif
Doug Zongker5290f202014-03-11 13:22:04 -0700195 // Change gr_draw to point to the buffer currently displayed,
196 // then flip the driver so we're displaying the other buffer
197 // instead.
198 gr_draw = gr_framebuffer + displayed_buffer;
199 set_displayed_framebuffer(1-displayed_buffer);
200 } else {
201 // Copy from the in-memory surface to the framebuffer.
202 memcpy(gr_framebuffer[0].data, gr_draw->data,
203 gr_draw->height * gr_draw->row_bytes);
204 }
205 return gr_draw;
206}
207
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700208static void fbdev_exit(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700209 close(fb_fd);
210 fb_fd = -1;
211
212 if (!double_buffered && gr_draw) {
213 free(gr_draw->data);
214 free(gr_draw);
215 }
216 gr_draw = NULL;
217}