1 /*
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 "graphics_fbdev.h"
18
19 #include <fcntl.h>
20 #include <linux/fb.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <memory>
30
31 #include <android-base/unique_fd.h>
32
33 #include "minui/minui.h"
34
Create(size_t width,size_t height,size_t row_bytes,size_t pixel_bytes)35 std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(size_t width, size_t height,
36 size_t row_bytes, size_t pixel_bytes) {
37 // Cannot use std::make_unique to access non-public ctor.
38 return std::unique_ptr<GRSurfaceFbdev>(new GRSurfaceFbdev(width, height, row_bytes, pixel_bytes));
39 }
40
Blank(bool blank)41 void MinuiBackendFbdev::Blank(bool blank) {
42 int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
43 if (ret < 0) perror("ioctl(): blank");
44 }
45
Blank(bool blank,DrmConnector index)46 void MinuiBackendFbdev::Blank(bool blank, DrmConnector index) {
47 fprintf(stderr, "Unsupported multiple connectors, blank = %d, index = %d\n", blank, index);
48 }
49
HasMultipleConnectors()50 bool MinuiBackendFbdev::HasMultipleConnectors() {
51 fprintf(stderr, "Unsupported multiple connectors\n");
52 return false;
53 }
54
SetDisplayedFramebuffer(size_t n)55 void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) {
56 if (n > 1 || !double_buffered) return;
57
58 vi.yres_virtual = gr_framebuffer[0]->height * 2;
59 vi.yoffset = n * gr_framebuffer[0]->height;
60 vi.bits_per_pixel = gr_framebuffer[0]->pixel_bytes * 8;
61 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
62 perror("active fb swap failed");
63 }
64 displayed_buffer = n;
65 }
66
Init()67 GRSurface* MinuiBackendFbdev::Init() {
68 android::base::unique_fd fd(open("/dev/graphics/fb0", O_RDWR | O_CLOEXEC));
69 if (fd == -1) {
70 perror("cannot open fb0");
71 return nullptr;
72 }
73
74 fb_fix_screeninfo fi;
75 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
76 perror("failed to get fb0 info");
77 return nullptr;
78 }
79
80 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
81 perror("failed to get fb0 info");
82 return nullptr;
83 }
84
85 // We print this out for informational purposes only, but
86 // throughout we assume that the framebuffer device uses an RGBX
87 // pixel format. This is the case for every development device I
88 // have access to. For some of those devices (eg, hammerhead aka
89 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
90 // different format (XBGR) but actually produces the correct
91 // results on the display when you write RGBX.
92 //
93 // If you have a device that actually *needs* another pixel format
94 // (ie, BGRX, or 565), patches welcome...
95
96 printf(
97 "fb0 reports (possibly inaccurate):\n"
98 " vi.bits_per_pixel = %d\n"
99 " vi.red.offset = %3d .length = %3d\n"
100 " vi.green.offset = %3d .length = %3d\n"
101 " vi.blue.offset = %3d .length = %3d\n",
102 vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length,
103 vi.blue.offset, vi.blue.length);
104
105 void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
106 if (bits == MAP_FAILED) {
107 perror("failed to mmap framebuffer");
108 return nullptr;
109 }
110
111 memset(bits, 0, fi.smem_len);
112
113 gr_framebuffer[0] =
114 GRSurfaceFbdev::Create(vi.xres, vi.yres, fi.line_length, vi.bits_per_pixel / 8);
115 gr_framebuffer[0]->buffer_ = static_cast<uint8_t*>(bits);
116 memset(gr_framebuffer[0]->buffer_, 0, gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes);
117
118 gr_framebuffer[1] =
119 GRSurfaceFbdev::Create(gr_framebuffer[0]->width, gr_framebuffer[0]->height,
120 gr_framebuffer[0]->row_bytes, gr_framebuffer[0]->pixel_bytes);
121
122 /* check if we can use double buffering */
123 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
124 double_buffered = true;
125
126 gr_framebuffer[1]->buffer_ =
127 gr_framebuffer[0]->buffer_ + gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes;
128 } else {
129 double_buffered = false;
130
131 // Without double-buffering, we allocate RAM for a buffer to draw in, and then "flipping" the
132 // buffer consists of a memcpy from the buffer we allocated to the framebuffer.
133 memory_buffer.resize(gr_framebuffer[1]->height * gr_framebuffer[1]->row_bytes);
134 gr_framebuffer[1]->buffer_ = memory_buffer.data();
135 }
136
137 gr_draw = gr_framebuffer[1].get();
138 memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes);
139 fb_fd = std::move(fd);
140 SetDisplayedFramebuffer(0);
141
142 printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height);
143 Blank(false);
144
145 return gr_draw;
146 }
147
Flip()148 GRSurface* MinuiBackendFbdev::Flip() {
149 if (double_buffered) {
150 // Change gr_draw to point to the buffer currently displayed, then flip the driver so we're
151 // displaying the other buffer instead.
152 gr_draw = gr_framebuffer[displayed_buffer].get();
153 SetDisplayedFramebuffer(1 - displayed_buffer);
154 } else {
155 // Copy from the in-memory surface to the framebuffer.
156 memcpy(gr_framebuffer[0]->buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes);
157 }
158 return gr_draw;
159 }
160