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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/**
* @file lv_linux_fbdev.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_linux_fbdev.h"
#if LV_USE_LINUX_FBDEV
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#if LV_LINUX_FBDEV_BSD
#include <sys/fcntl.h>
#include <sys/consio.h>
#include <sys/fbio.h>
#else
#include <linux/fb.h>
#endif /* LV_LINUX_FBDEV_BSD */
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
struct bsd_fb_var_info {
uint32_t xoffset;
uint32_t yoffset;
uint32_t xres;
uint32_t yres;
int bits_per_pixel;
};
struct bsd_fb_fix_info {
long int line_length;
long int smem_len;
};
typedef struct {
const char * devname;
lv_color_format_t color_format;
#if LV_LINUX_FBDEV_BSD
struct bsd_fb_var_info vinfo;
struct bsd_fb_fix_info finfo;
#else
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
#endif /* LV_LINUX_FBDEV_BSD */
char * fbp;
long int screensize;
int fbfd;
bool force_refresh;
} lv_linux_fb_t;
/**********************
* STATIC PROTOTYPES
**********************/
static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * color_p);
static uint32_t tick_get_cb(void);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
#if LV_LINUX_FBDEV_BSD
#define FBIOBLANK FBIO_BLANK
#endif /* LV_LINUX_FBDEV_BSD */
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#endif
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_display_t * lv_linux_fbdev_create(void)
{
static bool inited = false;
if(!inited) {
lv_tick_set_cb(tick_get_cb);
inited = true;
}
lv_linux_fb_t * dsc = lv_malloc_zeroed(sizeof(lv_linux_fb_t));
LV_ASSERT_MALLOC(dsc);
if(dsc == NULL) return NULL;
lv_display_t * disp = lv_display_create(800, 480);
if(disp == NULL) {
lv_free(dsc);
return NULL;
}
dsc->fbfd = -1;
lv_display_set_driver_data(disp, dsc);
lv_display_set_flush_cb(disp, flush_cb);
return disp;
}
void lv_linux_fbdev_set_file(lv_display_t * disp, const char * file)
{
char * devname = lv_malloc(lv_strlen(file) + 1);
LV_ASSERT_MALLOC(devname);
if(devname == NULL) return;
lv_strcpy(devname, file);
lv_linux_fb_t * dsc = lv_display_get_driver_data(disp);
dsc->devname = devname;
if(dsc->fbfd > 0) close(dsc->fbfd);
/* Open the file for reading and writing*/
dsc->fbfd = open(dsc->devname, O_RDWR);
if(dsc->fbfd == -1) {
perror("Error: cannot open framebuffer device");
return;
}
LV_LOG_INFO("The framebuffer device was opened successfully");
/* Make sure that the display is on.*/
if(ioctl(dsc->fbfd, FBIOBLANK, FB_BLANK_UNBLANK) != 0) {
perror("ioctl(FBIOBLANK)");
/* Don't return. Some framebuffer drivers like efifb or simplefb don't implement FBIOBLANK.*/
}
#if LV_LINUX_FBDEV_BSD
struct fbtype fb;
unsigned line_length;
/*Get fb type*/
if(ioctl(dsc->fbfd, FBIOGTYPE, &fb) != 0) {
perror("ioctl(FBIOGTYPE)");
return;
}
/*Get screen width*/
if(ioctl(dsc->fbfd, FBIO_GETLINEWIDTH, &line_length) != 0) {
perror("ioctl(FBIO_GETLINEWIDTH)");
return;
}
dsc->vinfo.xres = (unsigned) fb.fb_width;
dsc->vinfo.yres = (unsigned) fb.fb_height;
dsc->vinfo.bits_per_pixel = fb.fb_depth;
dsc->vinfo.xoffset = 0;
dsc->vinfo.yoffset = 0;
dsc->finfo.line_length = line_length;
dsc->finfo.smem_len = dsc->finfo.line_length * dsc->vinfo.yres;
#else /* LV_LINUX_FBDEV_BSD */
/* Get fixed screen information*/
if(ioctl(dsc->fbfd, FBIOGET_FSCREENINFO, &dsc->finfo) == -1) {
perror("Error reading fixed information");
return;
}
/* Get variable screen information*/
if(ioctl(dsc->fbfd, FBIOGET_VSCREENINFO, &dsc->vinfo) == -1) {
perror("Error reading variable information");
return;
}
#endif /* LV_LINUX_FBDEV_BSD */
LV_LOG_INFO("%dx%d, %dbpp", dsc->vinfo.xres, dsc->vinfo.yres, dsc->vinfo.bits_per_pixel);
/* Figure out the size of the screen in bytes*/
dsc->screensize = dsc->finfo.smem_len;/*finfo.line_length * vinfo.yres;*/
/* Map the device to memory*/
dsc->fbp = (char *)mmap(0, dsc->screensize, PROT_READ | PROT_WRITE, MAP_SHARED, dsc->fbfd, 0);
if((intptr_t)dsc->fbp == -1) {
perror("Error: failed to map framebuffer device to memory");
return;
}
/* Don't initialise the memory to retain what's currently displayed / avoid clearing the screen.
* This is important for applications that only draw to a subsection of the full framebuffer.*/
LV_LOG_INFO("The framebuffer device was mapped to memory successfully");
switch(dsc->vinfo.bits_per_pixel) {
case 16:
lv_display_set_color_format(disp, LV_COLOR_FORMAT_RGB565);
break;
case 24:
lv_display_set_color_format(disp, LV_COLOR_FORMAT_RGB888);
break;
case 32:
lv_display_set_color_format(disp, LV_COLOR_FORMAT_XRGB8888);
break;
default:
LV_LOG_WARN("Not supported color format (%d bits)", dsc->vinfo.bits_per_pixel);
return;
}
int32_t hor_res = dsc->vinfo.xres;
int32_t ver_res = dsc->vinfo.yres;
int32_t width = dsc->vinfo.width;
uint32_t draw_buf_size = hor_res * (dsc->vinfo.bits_per_pixel >> 3);
if(LV_LINUX_FBDEV_RENDER_MODE == LV_DISPLAY_RENDER_MODE_PARTIAL) {
draw_buf_size *= LV_LINUX_FBDEV_BUFFER_SIZE;
}
else {
draw_buf_size *= ver_res;
}
uint8_t * draw_buf = NULL;
uint8_t * draw_buf_2 = NULL;
draw_buf = malloc(draw_buf_size);
if(LV_LINUX_FBDEV_BUFFER_COUNT == 2) {
draw_buf_2 = malloc(draw_buf_size);
}
lv_display_set_buffers(disp, draw_buf, draw_buf_2, draw_buf_size, LV_LINUX_FBDEV_RENDER_MODE);
lv_display_set_resolution(disp, hor_res, ver_res);
if(width > 0) {
lv_display_set_dpi(disp, DIV_ROUND_UP(hor_res * 254, width * 10));
}
LV_LOG_INFO("Resolution is set to %" LV_PRId32 "x%" LV_PRId32 " at %" LV_PRId32 "dpi",
hor_res, ver_res, lv_display_get_dpi(disp));
}
void lv_linux_fbdev_set_force_refresh(lv_display_t * disp, bool enabled)
{
lv_linux_fb_t * dsc = lv_display_get_driver_data(disp);
dsc->force_refresh = enabled;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * color_p)
{
lv_linux_fb_t * dsc = lv_display_get_driver_data(disp);
if(dsc->fbp == NULL ||
area->x2 < 0 || area->y2 < 0 ||
area->x1 > (int32_t)dsc->vinfo.xres - 1 || area->y1 > (int32_t)dsc->vinfo.yres - 1) {
lv_display_flush_ready(disp);
return;
}
int32_t w = lv_area_get_width(area);
uint32_t px_size = lv_color_format_get_size(lv_display_get_color_format(disp));
uint32_t color_pos = (area->x1 + dsc->vinfo.xoffset) * px_size + area->y1 * dsc->finfo.line_length;
uint32_t fb_pos = color_pos + dsc->vinfo.yoffset * dsc->finfo.line_length;
uint8_t * fbp = (uint8_t *)dsc->fbp;
int32_t y;
if(LV_LINUX_FBDEV_RENDER_MODE == LV_DISPLAY_RENDER_MODE_DIRECT) {
for(y = area->y1; y <= area->y2; y++) {
lv_memcpy(&fbp[fb_pos], &color_p[color_pos], w * px_size);
fb_pos += dsc->finfo.line_length;
color_pos += dsc->finfo.line_length;
}
}
else {
for(y = area->y1; y <= area->y2; y++) {
lv_memcpy(&fbp[fb_pos], color_p, w * px_size);
fb_pos += dsc->finfo.line_length;
color_p += w * px_size;
}
}
if(dsc->force_refresh) {
dsc->vinfo.activate |= FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
if(ioctl(dsc->fbfd, FBIOPUT_VSCREENINFO, &(dsc->vinfo)) == -1) {
perror("Error setting var screen info");
}
}
lv_display_flush_ready(disp);
}
static uint32_t tick_get_cb(void)
{
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
uint64_t time_ms;
time_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;
return time_ms;
}
#endif /*LV_USE_LINUX_FBDEV*/
|