You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
ndg/display/fbdev.c

293 lines
8.0 KiB
C

/**
* @file fbdev.c
*
*/
/*********************
* INCLUDES
*********************/
#include "fbdev.h"
#if USE_FBDEV || USE_BSD_FBDEV
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#if USE_BSD_FBDEV
#include <sys/fcntl.h>
#include <sys/time.h>
#include <sys/consio.h>
#include <sys/fbio.h>
#else /* USE_BSD_FBDEV */
#include <linux/fb.h>
#endif /* USE_BSD_FBDEV */
/*********************
* DEFINES
*********************/
#ifndef FBDEV_PATH
#define FBDEV_PATH "/dev/fb0"
#endif
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#endif
/**********************
* TYPEDEFS
**********************/
/**********************
* STRUCTURES
**********************/
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;
};
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
#if USE_BSD_FBDEV
static struct bsd_fb_var_info vinfo;
static struct bsd_fb_fix_info finfo;
#else
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
#endif /* USE_BSD_FBDEV */
static char *fbp = 0;
static long int screensize = 0;
static int fbfd = 0;
/**********************
* MACROS
**********************/
#if USE_BSD_FBDEV
#define FBIOBLANK FBIO_BLANK
#endif /* USE_BSD_FBDEV */
/**********************
* GLOBAL FUNCTIONS
**********************/
void fbdev_init(void)
{
// Open the file for reading and writing
fbfd = open(FBDEV_PATH, O_RDWR);
if(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(fbfd, FBIOBLANK, FB_BLANK_UNBLANK) != 0) {
perror("ioctl(FBIOBLANK)");
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
// Don't return. Some framebuffer drivers like efifb or simplefb don't implement FBIOBLANK.
}
#if USE_BSD_FBDEV
struct fbtype fb;
unsigned line_length;
//Get fb type
if (ioctl(fbfd, FBIOGTYPE, &fb) != 0) {
perror("ioctl(FBIOGTYPE)");
return;
}
//Get screen width
if (ioctl(fbfd, FBIO_GETLINEWIDTH, &line_length) != 0) {
perror("ioctl(FBIO_GETLINEWIDTH)");
return;
}
vinfo.xres = (unsigned) fb.fb_width;
vinfo.yres = (unsigned) fb.fb_height;
vinfo.bits_per_pixel = fb.fb_depth;
vinfo.xoffset = 0;
vinfo.yoffset = 0;
finfo.line_length = line_length;
finfo.smem_len = finfo.line_length * vinfo.yres;
#else /* USE_BSD_FBDEV */
// Get fixed screen information
if(ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
perror("Error reading fixed information");
return;
}
// Get variable screen information
if(ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("Error reading variable information");
return;
}
#endif /* USE_BSD_FBDEV */
LV_LOG_INFO("%dx%d, %dbpp", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
// Figure out the size of the screen in bytes
screensize = finfo.smem_len; //finfo.line_length * vinfo.yres;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if((intptr_t)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");
}
void fbdev_exit(void)
{
close(fbfd);
}
/**
* Flush a buffer to the marked area
* @param drv pointer to driver where this function belongs
* @param area an area where to copy `color_p`
* @param color_p an array of pixels to copy to the `area` part of the screen
*/
void fbdev_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_p)
{
if(fbp == NULL ||
area->x2 < 0 ||
area->y2 < 0 ||
area->x1 > (int32_t)vinfo.xres - 1 ||
area->y1 > (int32_t)vinfo.yres - 1) {
lv_disp_flush_ready(drv);
return;
}
/*Truncate the area to the screen*/
int32_t act_x1 = area->x1 < 0 ? 0 : area->x1;
int32_t act_y1 = area->y1 < 0 ? 0 : area->y1;
int32_t act_x2 = area->x2 > (int32_t)vinfo.xres - 1 ? (int32_t)vinfo.xres - 1 : area->x2;
int32_t act_y2 = area->y2 > (int32_t)vinfo.yres - 1 ? (int32_t)vinfo.yres - 1 : area->y2;
lv_coord_t w = (act_x2 - act_x1 + 1);
long int location = 0;
long int byte_location = 0;
unsigned char bit_location = 0;
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
/*32 bit per pixel*/
if(vinfo.bits_per_pixel == 32) {
uint32_t * fbp32 = (uint32_t *)fbp;
int32_t y;
for(y = act_y1; y <= act_y2; y++) {
location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length / 4;
memcpy(&fbp32[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1) * 4);
color_p += w;
}
}
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
/*24 bit per pixel*/
else if(vinfo.bits_per_pixel == 24 && LV_COLOR_DEPTH == 32) {
uint8_t * fbp8 = (uint8_t *)fbp;
lv_coord_t x;
int32_t y;
uint8_t *pixel;
for(y = act_y1; y <= act_y2; y++) {
location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length / 3;
for (x = 0; x < w; ++x) {
pixel = (uint8_t *)(&color_p[x]);
fbp8[3 * (location + x)] = pixel[0];
fbp8[3 * (location + x) + 1] = pixel[1];
fbp8[3 * (location + x) + 2] = pixel[2];
}
color_p += w;
}
}
/*16 bit per pixel*/
else if(vinfo.bits_per_pixel == 16) {
uint16_t * fbp16 = (uint16_t *)fbp;
int32_t y;
for(y = act_y1; y <= act_y2; y++) {
location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length / 2;
memcpy(&fbp16[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1) * 2);
color_p += w;
}
}
/*8 bit per pixel*/
else if(vinfo.bits_per_pixel == 8) {
uint8_t * fbp8 = (uint8_t *)fbp;
int32_t y;
for(y = act_y1; y <= act_y2; y++) {
location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length;
memcpy(&fbp8[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1));
color_p += w;
}
}
/*1 bit per pixel*/
else if(vinfo.bits_per_pixel == 1) {
uint8_t * fbp8 = (uint8_t *)fbp;
int32_t x;
int32_t y;
for(y = act_y1; y <= act_y2; y++) {
for(x = act_x1; x <= act_x2; x++) {
location = (x + vinfo.xoffset) + (y + vinfo.yoffset) * vinfo.xres;
byte_location = location / 8; /* find the byte we need to change */
bit_location = location % 8; /* inside the byte found, find the bit we need to change */
fbp8[byte_location] &= ~(((uint8_t)(1)) << bit_location);
fbp8[byte_location] |= ((uint8_t)(color_p->full)) << bit_location;
color_p++;
}
color_p += area->x2 - act_x2;
}
} else {
/*Not supported bit per pixel*/
}
//May be some direct update command is required
//ret = ioctl(state->fd, FBIO_UPDATE, (unsigned long)((uintptr_t)rect));
lv_disp_flush_ready(drv);
}
void fbdev_get_sizes(uint32_t *width, uint32_t *height, uint32_t *dpi) {
if (width)
*width = vinfo.xres;
if (height)
*height = vinfo.yres;
if (dpi && vinfo.height)
*dpi = DIV_ROUND_UP(vinfo.xres * 254, vinfo.width * 10);
}
void fbdev_set_offset(uint32_t xoffset, uint32_t yoffset) {
vinfo.xoffset = xoffset;
vinfo.yoffset = yoffset;
}
/**********************
* STATIC FUNCTIONS
**********************/
#endif