blob: dd4c9d465ebd9806360dae8b467bd70610b29f5e [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
Tao Baoacf4dd12017-02-07 14:32:05 -080017#include <fcntl.h>
18#include <linux/fb.h>
19#include <stdio.h>
Doug Zongker5290f202014-03-11 13:22:04 -070020#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080021#include <string.h>
Doug Zongker5290f202014-03-11 13:22:04 -070022#include <sys/ioctl.h>
23#include <sys/mman.h>
24#include <sys/types.h>
Tao Baoacf4dd12017-02-07 14:32:05 -080025#include <unistd.h>
Doug Zongker5290f202014-03-11 13:22:04 -070026
Tao Bao0ecbd762017-01-16 21:16:58 -080027#include "minui/minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070028#include "graphics.h"
29
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070030static GRSurface* fbdev_init(minui_backend*);
31static GRSurface* fbdev_flip(minui_backend*);
Doug Zongker5290f202014-03-11 13:22:04 -070032static void fbdev_blank(minui_backend*, bool);
33static void fbdev_exit(minui_backend*);
34
35static GRSurface gr_framebuffer[2];
36static bool double_buffered;
Tao Baoacf4dd12017-02-07 14:32:05 -080037static GRSurface* gr_draw = nullptr;
Doug Zongker5290f202014-03-11 13:22:04 -070038static int displayed_buffer;
39
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070040static fb_var_screeninfo vi;
Doug Zongker5290f202014-03-11 13:22:04 -070041static int fb_fd = -1;
42
43static minui_backend my_backend = {
Tao Baoacf4dd12017-02-07 14:32:05 -080044 .init = fbdev_init,
45 .flip = fbdev_flip,
46 .blank = fbdev_blank,
47 .exit = fbdev_exit,
Doug Zongker5290f202014-03-11 13:22:04 -070048};
49
50minui_backend* open_fbdev() {
Tao Baoacf4dd12017-02-07 14:32:05 -080051 return &my_backend;
Doug Zongker5290f202014-03-11 13:22:04 -070052}
53
Tao Baoacf4dd12017-02-07 14:32:05 -080054static void fbdev_blank(minui_backend* backend __unused, bool blank) {
55 int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
56 if (ret < 0) {
57 perror("ioctl(): blank");
58 }
Doug Zongker5290f202014-03-11 13:22:04 -070059}
60
Tao Baoacf4dd12017-02-07 14:32:05 -080061static void set_displayed_framebuffer(unsigned n) {
62 if (n > 1 || !double_buffered) return;
Doug Zongker5290f202014-03-11 13:22:04 -070063
Tao Baoacf4dd12017-02-07 14:32:05 -080064 vi.yres_virtual = gr_framebuffer[0].height * 2;
65 vi.yoffset = n * gr_framebuffer[0].height;
66 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
67 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
68 perror("active fb swap failed");
69 }
70 displayed_buffer = n;
Doug Zongker5290f202014-03-11 13:22:04 -070071}
72
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070073static GRSurface* fbdev_init(minui_backend* backend) {
Tao Baoacf4dd12017-02-07 14:32:05 -080074 int fd = open("/dev/graphics/fb0", O_RDWR);
75 if (fd == -1) {
76 perror("cannot open fb0");
77 return nullptr;
78 }
79
80 fb_fix_screeninfo fi;
81 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
82 perror("failed to get fb0 info");
83 close(fd);
84 return nullptr;
85 }
86
87 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
88 perror("failed to get fb0 info");
89 close(fd);
90 return nullptr;
91 }
92
93 // We print this out for informational purposes only, but
94 // throughout we assume that the framebuffer device uses an RGBX
95 // pixel format. This is the case for every development device I
96 // have access to. For some of those devices (eg, hammerhead aka
97 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
98 // different format (XBGR) but actually produces the correct
99 // results on the display when you write RGBX.
100 //
101 // If you have a device that actually *needs* another pixel format
102 // (ie, BGRX, or 565), patches welcome...
103
104 printf(
105 "fb0 reports (possibly inaccurate):\n"
106 " vi.bits_per_pixel = %d\n"
107 " vi.red.offset = %3d .length = %3d\n"
108 " vi.green.offset = %3d .length = %3d\n"
109 " vi.blue.offset = %3d .length = %3d\n",
110 vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length,
111 vi.blue.offset, vi.blue.length);
112
113 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
114 if (bits == MAP_FAILED) {
115 perror("failed to mmap framebuffer");
116 close(fd);
117 return nullptr;
118 }
119
120 memset(bits, 0, fi.smem_len);
121
122 gr_framebuffer[0].width = vi.xres;
123 gr_framebuffer[0].height = vi.yres;
124 gr_framebuffer[0].row_bytes = fi.line_length;
125 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
126 gr_framebuffer[0].data = static_cast<uint8_t*>(bits);
127 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
128
129 /* check if we can use double buffering */
130 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
131 double_buffered = true;
132
133 memcpy(gr_framebuffer + 1, gr_framebuffer, sizeof(GRSurface));
134 gr_framebuffer[1].data =
135 gr_framebuffer[0].data + gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
136
137 gr_draw = gr_framebuffer + 1;
138
139 } else {
140 double_buffered = false;
141
142 // Without double-buffering, we allocate RAM for a buffer to
143 // draw in, and then "flipping" the buffer consists of a
144 // memcpy from the buffer we allocated to the framebuffer.
145
146 gr_draw = static_cast<GRSurface*>(malloc(sizeof(GRSurface)));
147 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
148 gr_draw->data = static_cast<unsigned char*>(malloc(gr_draw->height * gr_draw->row_bytes));
149 if (!gr_draw->data) {
150 perror("failed to allocate in-memory surface");
151 return nullptr;
Doug Zongker5290f202014-03-11 13:22:04 -0700152 }
Tao Baoacf4dd12017-02-07 14:32:05 -0800153 }
Doug Zongker5290f202014-03-11 13:22:04 -0700154
Tao Baoacf4dd12017-02-07 14:32:05 -0800155 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
156 fb_fd = fd;
157 set_displayed_framebuffer(0);
Doug Zongker5290f202014-03-11 13:22:04 -0700158
Tao Baoacf4dd12017-02-07 14:32:05 -0800159 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
Doug Zongker5290f202014-03-11 13:22:04 -0700160
Tao Baoacf4dd12017-02-07 14:32:05 -0800161 fbdev_blank(backend, true);
162 fbdev_blank(backend, false);
Doug Zongker5290f202014-03-11 13:22:04 -0700163
Tao Baoacf4dd12017-02-07 14:32:05 -0800164 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700165}
166
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700167static GRSurface* fbdev_flip(minui_backend* backend __unused) {
Tao Baoacf4dd12017-02-07 14:32:05 -0800168 if (double_buffered) {
169 // Change gr_draw to point to the buffer currently displayed,
170 // then flip the driver so we're displaying the other buffer
171 // instead.
172 gr_draw = gr_framebuffer + displayed_buffer;
173 set_displayed_framebuffer(1 - displayed_buffer);
174 } else {
175 // Copy from the in-memory surface to the framebuffer.
176 memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes);
177 }
178 return gr_draw;
Doug Zongker5290f202014-03-11 13:22:04 -0700179}
180
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700181static void fbdev_exit(minui_backend* backend __unused) {
Tao Baoacf4dd12017-02-07 14:32:05 -0800182 close(fb_fd);
183 fb_fd = -1;
Doug Zongker5290f202014-03-11 13:22:04 -0700184
Tao Baoacf4dd12017-02-07 14:32:05 -0800185 if (!double_buffered && gr_draw) {
186 free(gr_draw->data);
187 free(gr_draw);
188 }
189 gr_draw = nullptr;
Doug Zongker5290f202014-03-11 13:22:04 -0700190}