-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.cpp
More file actions
215 lines (171 loc) · 5.21 KB
/
device.cpp
File metadata and controls
215 lines (171 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Simple fbdev - Simple demo that uses linux framebuffer device
// Copyright (C) 2017-2018 Dawid Gan <deveee@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "device.hpp"
#include <cstdio>
#include <cstring>
Device* Device::m_device = NULL;
Device::Device()
{
m_device = this;
m_fbfd = -1;
m_kdfd = -1;
m_framebuffer = NULL;
m_backbuffer = NULL;
m_page_size = 0;
m_cur_page = 1;
m_has_orig_vinfo = false;
m_fbiopan_supported = true;
memset(&m_vinfo, 0, sizeof(fb_var_screeninfo));
memset(&m_orig_vinfo, 0, sizeof(fb_var_screeninfo));
memset(&m_finfo, 0, sizeof(fb_fix_screeninfo));
}
Device::~Device()
{
if (m_kdfd >= 0)
{
ioctl(m_kdfd, KDSETMODE, KD_TEXT);
close(m_kdfd);
}
if (!m_fbiopan_supported)
{
delete[] m_backbuffer;
}
if (m_framebuffer != NULL)
{
munmap(m_framebuffer, m_finfo.smem_len);
}
if (m_has_orig_vinfo)
{
ioctl(m_fbfd, FBIOPUT_VSCREENINFO, &m_orig_vinfo);
}
if (m_fbfd >= 0)
{
close(m_fbfd);
}
}
bool Device::init()
{
const char* fb_paths[] = {"/dev/fb0",
"/dev/graphics/fb0",
"/mnt/dev/graphics/fb0"};
for (const char* fb_path : fb_paths)
{
m_fbfd = open(fb_path, O_RDWR);
if (m_fbfd != -1)
break;
}
if (m_fbfd == -1)
{
printf("Error: Cannot open framebuffer device.\n");
return false;
}
int err = ioctl(m_fbfd, FBIOGET_VSCREENINFO, &m_vinfo);
if (err != 0)
{
printf("Error: Cannot read variable screen info.\n");
return false;
}
memcpy(&m_orig_vinfo, &m_vinfo, sizeof(struct fb_var_screeninfo));
m_has_orig_vinfo = true;
m_vinfo.bits_per_pixel = 32;
err = ioctl(m_fbfd, FBIOPUT_VSCREENINFO, &m_vinfo);
if (err != 0)
{
printf("Error: Cannot set 32bit mode.\n");
return false;
}
m_vinfo.xres_virtual = m_vinfo.xres;
m_vinfo.yres_virtual = m_vinfo.yres * 2;
err = ioctl(m_fbfd, FBIOPUT_VSCREENINFO, &m_vinfo);
if (err != 0)
{
printf("Warning: Page flipping not supported.\n");
m_fbiopan_supported = false;
m_vinfo.xres_virtual = m_vinfo.xres;
m_vinfo.yres_virtual = m_vinfo.yres;
err = ioctl(m_fbfd, FBIOPUT_VSCREENINFO, &m_vinfo);
}
if (err != 0)
{
printf("Error: Cannot set variable screen info.\n");
return false;
}
err = ioctl(m_fbfd, FBIOGET_FSCREENINFO, &m_finfo);
if (err != 0)
{
printf("Error: Cannot read fixed screen info.\n");
return false;
}
m_page_size = m_finfo.line_length * m_vinfo.yres;
m_framebuffer = (uint8_t*)mmap(0, m_finfo.smem_len, PROT_READ | PROT_WRITE,
MAP_SHARED, m_fbfd, 0);
if ((intptr_t)m_framebuffer == -1)
{
printf("Error: Cannot map framebuffer device.\n");
return false;
}
if (m_fbiopan_supported)
{
m_backbuffer = m_framebuffer + m_page_size;
}
else
{
m_backbuffer = new uint8_t[m_finfo.smem_len];
}
m_kdfd = open("/dev/tty", O_WRONLY);
if (m_kdfd >= 0)
{
ioctl(m_kdfd, KDSETMODE, KD_GRAPHICS);
}
return true;
}
void Device::clearScreen(uint8_t color)
{
memset(m_backbuffer, color, m_page_size);
}
void Device::drawPixel(int x, int y, uint8_t color[3])
{
int pix_offset = x * 4 + y * m_finfo.line_length;
m_backbuffer[pix_offset + 0] = color[2];
m_backbuffer[pix_offset + 1] = color[1];
m_backbuffer[pix_offset + 2] = color[0];
m_backbuffer[pix_offset + 3] = 255;
}
void Device::sleep(unsigned int time_ms)
{
struct timespec ts;
ts.tv_sec = (time_t)(time_ms / 1000);
ts.tv_nsec = (long)(time_ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
void Device::swapBuffers()
{
if (m_fbiopan_supported)
{
m_vinfo.yoffset = m_cur_page * m_vinfo.yres;
ioctl(m_fbfd, FBIOPAN_DISPLAY, &m_vinfo);
m_cur_page = (m_cur_page + 1) % 2;
m_backbuffer = m_framebuffer + m_cur_page * m_page_size;
}
else
{
memcpy(m_framebuffer, m_backbuffer, m_finfo.smem_len);
m_vinfo.activate = FB_ACTIVATE_VBL;
ioctl(m_fbfd, FBIOPUT_VSCREENINFO, &m_vinfo);
}
uint32_t dummy = 0;
ioctl(m_fbfd, FBIO_WAITFORVSYNC, &dummy);
}