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