blob: 2d70249ed866f51e623e416563d70a00974ce43c [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
Doug Zongker5290f202014-03-11 13:22:04 -070017#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080018#include <string.h>
Doug Zongker5290f202014-03-11 13:22:04 -070019#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
Tao Bao0ecbd762017-01-16 21:16:58 -080032#include "minui/minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070033#include "graphics.h"
34
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070035static GRSurface* fbdev_init(minui_backend*);
36static GRSurface* fbdev_flip(minui_backend*);
Doug Zongker5290f202014-03-11 13:22:04 -070037static 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
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070045static fb_var_screeninfo vi;
Doug Zongker5290f202014-03-11 13:22:04 -070046static 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
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070081static GRSurface* fbdev_init(minui_backend* backend) {
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070082 int fd = open("/dev/graphics/fb0", O_RDWR);
83 if (fd == -1) {
Doug Zongker5290f202014-03-11 13:22:04 -070084 perror("cannot open fb0");
85 return NULL;
86 }
87
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070088 fb_fix_screeninfo fi;
Doug Zongker5290f202014-03-11 13:22:04 -070089 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
90 perror("failed to get fb0 info");
91 close(fd);
92 return NULL;
93 }
94
95 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
96 perror("failed to get fb0 info");
97 close(fd);
98 return NULL;
99 }
100
101 // We print this out for informational purposes only, but
102 // throughout we assume that the framebuffer device uses an RGBX
103 // pixel format. This is the case for every development device I
104 // have access to. For some of those devices (eg, hammerhead aka
105 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
106 // different format (XBGR) but actually produces the correct
107 // results on the display when you write RGBX.
108 //
109 // If you have a device that actually *needs* another pixel format
110 // (ie, BGRX, or 565), patches welcome...
111
112 printf("fb0 reports (possibly inaccurate):\n"
113 " vi.bits_per_pixel = %d\n"
114 " vi.red.offset = %3d .length = %3d\n"
115 " vi.green.offset = %3d .length = %3d\n"
116 " vi.blue.offset = %3d .length = %3d\n",
117 vi.bits_per_pixel,
118 vi.red.offset, vi.red.length,
119 vi.green.offset, vi.green.length,
120 vi.blue.offset, vi.blue.length);
121
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700122 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Doug Zongker5290f202014-03-11 13:22:04 -0700123 if (bits == MAP_FAILED) {
124 perror("failed to mmap framebuffer");
125 close(fd);
126 return NULL;
127 }
128
Joey Panba545d72014-06-04 20:47:46 +0800129 memset(bits, 0, fi.smem_len);
130
Doug Zongker5290f202014-03-11 13:22:04 -0700131 gr_framebuffer[0].width = vi.xres;
132 gr_framebuffer[0].height = vi.yres;
133 gr_framebuffer[0].row_bytes = fi.line_length;
134 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800135 gr_framebuffer[0].data = static_cast<uint8_t*>(bits);
Doug Zongker5290f202014-03-11 13:22:04 -0700136 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
137
138 /* check if we can use double buffering */
139 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
140 double_buffered = true;
141
142 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
143 gr_framebuffer[1].data = gr_framebuffer[0].data +
144 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
145
146 gr_draw = gr_framebuffer+1;
147
148 } else {
149 double_buffered = false;
150
151 // Without double-buffering, we allocate RAM for a buffer to
152 // draw in, and then "flipping" the buffer consists of a
153 // memcpy from the buffer we allocated to the framebuffer.
154
155 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
156 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
157 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
158 if (!gr_draw->data) {
159 perror("failed to allocate in-memory surface");
160 return NULL;
161 }
162 }
163
164 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
165 fb_fd = fd;
166 set_displayed_framebuffer(0);
167
168 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
169
170 fbdev_blank(backend, true);
171 fbdev_blank(backend, false);
172
173 return gr_draw;
174}
175
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700176static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700177 if (double_buffered) {
178 // Change gr_draw to point to the buffer currently displayed,
179 // then flip the driver so we're displaying the other buffer
180 // instead.
181 gr_draw = gr_framebuffer + displayed_buffer;
182 set_displayed_framebuffer(1-displayed_buffer);
183 } else {
184 // Copy from the in-memory surface to the framebuffer.
185 memcpy(gr_framebuffer[0].data, gr_draw->data,
186 gr_draw->height * gr_draw->row_bytes);
187 }
188 return gr_draw;
189}
190
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700191static void fbdev_exit(minui_backend* backend __unused) {
Doug Zongker5290f202014-03-11 13:22:04 -0700192 close(fb_fd);
193 fb_fd = -1;
194
195 if (!double_buffered && gr_draw) {
196 free(gr_draw->data);
197 free(gr_draw);
198 }
199 gr_draw = NULL;
200}