blob: a91ea8726186e7094494e15e906d1190f74f8493 [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>
19#include <unistd.h>
20
21#include <fcntl.h>
22#include <stdio.h>
23
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070024#include <sys/cdefs.h>
Doug Zongker5290f202014-03-11 13:22:04 -070025#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28
29#include <linux/fb.h>
30#include <linux/kd.h>
31
32#include "minui.h"
33#include "graphics.h"
34
35static gr_surface fbdev_init(minui_backend*);
36static gr_surface fbdev_flip(minui_backend*);
37static void fbdev_blank(minui_backend*, bool);
38static void fbdev_exit(minui_backend*);
39
40static GRSurface gr_framebuffer[2];
41static bool double_buffered;
42static GRSurface* gr_draw = NULL;
43static int displayed_buffer;
44
45static struct fb_var_screeninfo vi;
46static int fb_fd = -1;
47
48static minui_backend my_backend = {
49 .init = fbdev_init,
50 .flip = fbdev_flip,
51 .blank = fbdev_blank,
52 .exit = fbdev_exit,
53};
54
55minui_backend* open_fbdev() {
56 return &my_backend;
57}
58
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070059static void fbdev_blank(minui_backend* backend __unused, bool blank)
Doug Zongker5290f202014-03-11 13:22:04 -070060{
61 int ret;
62
63 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
64 if (ret < 0)
65 perror("ioctl(): blank");
66}
67
68static void set_displayed_framebuffer(unsigned n)
69{
70 if (n > 1 || !double_buffered) return;
71
72 vi.yres_virtual = gr_framebuffer[0].height * 2;
73 vi.yoffset = n * gr_framebuffer[0].height;
74 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
75 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
76 perror("active fb swap failed");
77 }
78 displayed_buffer = n;
79}
80
81static gr_surface fbdev_init(minui_backend* backend) {
82 int fd;
83 void *bits;
84
85 struct fb_fix_screeninfo fi;
86
87 fd = open("/dev/graphics/fb0", O_RDWR);
88 if (fd < 0) {
89 perror("cannot open fb0");
90 return NULL;
91 }
92
93 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
94 perror("failed to get fb0 info");
95 close(fd);
96 return NULL;
97 }
98
99 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
100 perror("failed to get fb0 info");
101 close(fd);
102 return NULL;
103 }
104
105 // We print this out for informational purposes only, but
106 // throughout we assume that the framebuffer device uses an RGBX
107 // pixel format. This is the case for every development device I
108 // have access to. For some of those devices (eg, hammerhead aka
109 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
110 // different format (XBGR) but actually produces the correct
111 // results on the display when you write RGBX.
112 //
113 // If you have a device that actually *needs* another pixel format
114 // (ie, BGRX, or 565), patches welcome...
115
116 printf("fb0 reports (possibly inaccurate):\n"
117 " vi.bits_per_pixel = %d\n"
118 " vi.red.offset = %3d .length = %3d\n"
119 " vi.green.offset = %3d .length = %3d\n"
120 " vi.blue.offset = %3d .length = %3d\n",
121 vi.bits_per_pixel,
122 vi.red.offset, vi.red.length,
123 vi.green.offset, vi.green.length,
124 vi.blue.offset, vi.blue.length);
125
126 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
127 if (bits == MAP_FAILED) {
128 perror("failed to mmap framebuffer");
129 close(fd);
130 return NULL;
131 }
132
133 gr_framebuffer[0].width = vi.xres;
134 gr_framebuffer[0].height = vi.yres;
135 gr_framebuffer[0].row_bytes = fi.line_length;
136 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
137 gr_framebuffer[0].data = bits;
138 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
139
140 /* check if we can use double buffering */
141 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
142 double_buffered = true;
143
144 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
145 gr_framebuffer[1].data = gr_framebuffer[0].data +
146 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
147
148 gr_draw = gr_framebuffer+1;
149
150 } else {
151 double_buffered = false;
152
153 // Without double-buffering, we allocate RAM for a buffer to
154 // draw in, and then "flipping" the buffer consists of a
155 // memcpy from the buffer we allocated to the framebuffer.
156
157 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
158 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
159 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
160 if (!gr_draw->data) {
161 perror("failed to allocate in-memory surface");
162 return NULL;
163 }
164 }
165
166 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
167 fb_fd = fd;
168 set_displayed_framebuffer(0);
169
170 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
171
172 fbdev_blank(backend, true);
173 fbdev_blank(backend, false);
174
175 return gr_draw;
176}
177
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700178static gr_surface fbdev_flip(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700179 if (double_buffered) {
180 // Change gr_draw to point to the buffer currently displayed,
181 // then flip the driver so we're displaying the other buffer
182 // instead.
183 gr_draw = gr_framebuffer + displayed_buffer;
184 set_displayed_framebuffer(1-displayed_buffer);
185 } else {
186 // Copy from the in-memory surface to the framebuffer.
Daniel Leungf7663962014-03-19 13:41:31 -0700187
188#if defined(RECOVERY_BGRA)
189 unsigned int idx;
190 unsigned char* ucfb_vaddr = (unsigned char*)gr_framebuffer[0].data;
191 unsigned char* ucbuffer_vaddr = (unsigned char*)gr_draw->data;
192 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); idx += 4) {
193 ucfb_vaddr[idx ] = ucbuffer_vaddr[idx + 2];
194 ucfb_vaddr[idx + 1] = ucbuffer_vaddr[idx + 1];
195 ucfb_vaddr[idx + 2] = ucbuffer_vaddr[idx ];
196 ucfb_vaddr[idx + 3] = ucbuffer_vaddr[idx + 3];
197 }
198#else
Doug Zongker5290f202014-03-11 13:22:04 -0700199 memcpy(gr_framebuffer[0].data, gr_draw->data,
200 gr_draw->height * gr_draw->row_bytes);
Daniel Leungf7663962014-03-19 13:41:31 -0700201#endif
Doug Zongker5290f202014-03-11 13:22:04 -0700202 }
203 return gr_draw;
204}
205
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700206static void fbdev_exit(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700207 close(fb_fd);
208 fb_fd = -1;
209
210 if (!double_buffered && gr_draw) {
211 free(gr_draw->data);
212 free(gr_draw);
213 }
214 gr_draw = NULL;
215}