目錄
一、測試程序
二、排查原因
三、為什么 Xorg 會導致程序無法工作?
一、測試程序
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>static int width; //LCD X 分辨率
static int height; //LCD Y 分辨率
static unsigned int *screen_base = NULL; //映射后的顯存基地址
/********************************************************************
* 函數名稱: lcd_draw_point
* 功能描述: 打點
* 輸入參數: x, y, color
* 返 回 值: 無
********************************************************************/
static void lcd_draw_point(unsigned int x, unsigned int y, unsigned int color)
{/* 對傳入參數的校驗 */if (x >= width)x = width - 1;if (y >= height)y = height - 1;/* 填充顏色 */screen_base[y * width + x] = color;
}
/********************************************************************
* 函數名稱: lcd_draw_line
* 功能描述: 畫線(水平或垂直線)
* 輸入參數: x, y, dir, length, color
* 返 回 值: 無
********************************************************************/
static void lcd_draw_line(unsigned int x, unsigned int y, int dir,
unsigned int length, unsigned int color)
{unsigned int end;unsigned long temp;/* 對傳入參數的校驗 */if (x >= width)x = width - 1;if (y >= height)y = height - 1;/* 填充顏色 */temp = y * width + x;//定位到起點if (dir) { //水平線end = x + length - 1;if (end >= width)end = width - 1;for ( ; x <= end; x++, temp++)screen_base[temp] = color;}else { //垂直線end = y + length - 1;if (end >= height)end = height - 1;for ( ; y <= end; y++, temp += width)screen_base[temp] = color;}
}
/********************************************************************
* 函數名稱: lcd_draw_rectangle
* 功能描述: 畫矩形
* 輸入參數: start_x, end_x, start_y, end_y, color
* 返 回 值: 無
********************************************************************/
static void lcd_draw_rectangle(unsigned int start_x, unsigned int end_x,
unsigned int start_y, unsigned int end_y,
unsigned int color)
{int x_len = end_x - start_x + 1;int y_len = end_y - start_y - 1;lcd_draw_line(start_x, start_y, 1, x_len, color);//上邊lcd_draw_line(start_x, end_y, 1, x_len, color); //下邊lcd_draw_line(start_x, start_y + 1, 0, y_len, color);//左邊lcd_draw_line(end_x, start_y + 1, 0, y_len, color);//右邊
}
/********************************************************************
* 函數名稱: lcd_fill
* 功能描述: 將一個矩形區域填充為參數 color 所指定的顏色
* 輸入參數: start_x, end_x, start_y, end_y, color
* 返 回 值: 無
********************************************************************/
static void lcd_fill(unsigned int start_x, unsigned int end_x,
unsigned int start_y, unsigned int end_y,
unsigned int color)
{unsigned long temp;unsigned int x;/* 對傳入參數的校驗 */if (end_x >= width)end_x = width - 1;if (end_y >= height)end_y = height - 1;/* 填充顏色 */temp = start_y * width; //定位到起點行首for ( ; start_y <= end_y; start_y++, temp+=width) {for (x = start_x; x <= end_x; x++)screen_base[temp + x] = color;}
}int main(int argc, char *argv[])
{struct fb_fix_screeninfo fb_fix;struct fb_var_screeninfo fb_var;unsigned int screen_size;int fd;/* 打開 framebuffer 設備 */if (0 > (fd = open("/dev/fb0", O_RDWR))) {perror("open error");exit(EXIT_FAILURE);}/* 獲取參數信息 */ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);printf("Solution: %d*%d\n""pixel depth bpp: %d\n""Number of bytes in a row: %d\n""Pixel format: R<%d %d> G<%d %d> B<%d %d> A<%d %d>\n",fb_var.xres, fb_var.yres, fb_var.bits_per_pixel,fb_fix.line_length,fb_var.red.offset, fb_var.red.length,fb_var.green.offset, fb_var.green.length,fb_var.blue.offset, fb_var.blue.length, fb_var.transp.offset, fb_var.transp.length);screen_size = fb_fix.line_length * fb_var.yres;width = fb_fix.line_length / (fb_var.bits_per_pixel / 8);height = fb_var.yres;/* 將顯示緩沖區映射到進程地址空間 */screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);if (MAP_FAILED == (void *)screen_base) {perror("mmap error");close(fd);exit(EXIT_FAILURE);}/* 畫正方形方塊 */int w = height * 0.25;//方塊的寬度為 1/4 屏幕高度lcd_fill(0, fb_var.xres-1, 0, height-1, 0x0); //清屏(屏幕顯示黑色)lcd_fill(0, w, 0, w, 0xFF0000); //紅色方塊lcd_fill(fb_var.xres-w, fb_var.xres-1, 0, w, 0xFF00); //綠色方塊lcd_fill(0, w, height-w, height-1, 0xFF); //藍色方塊lcd_fill(fb_var.xres-w, fb_var.xres-1, height-w, height-1, 0xFFFF00);//黃色方塊/* 畫線: 十字交叉線 */lcd_draw_line(0, height * 0.5, 1, fb_var.xres, 0xFFFFFF);//白色線lcd_draw_line(fb_var.xres * 0.5, 0, 0, height, 0xFFFFFF);//白色線/* 畫矩形 */unsigned int s_x, s_y, e_x, e_y;s_x = 0.25 * fb_var.xres;s_y = w;e_x = fb_var.xres - s_x;e_y = height - s_y;for ( ; (s_x <= e_x) && (s_y <= e_y);s_x+=5, s_y+=5, e_x-=5, e_y-=5)lcd_draw_rectangle(s_x, e_x, s_y, e_y, 0xFFFFFF);/* 退出 */munmap(screen_base, screen_size); //取消映射close(fd); //關閉文件exit(EXIT_SUCCESS); //退出進程
}
執行結果:
lientek@ubuntu:/home/linux_test/lcd$ sudo ./demo
Solution: 800*600
pixel depth bpp: 32
Number of bytes in a row: 4704
Pixel format: R<16 8> G<8 8> B<0 8> A<0 0>
桌面無圖形繪制效果
二、排查原因
????????程序無法在虛擬機的 Ubuntu 桌面上繪制圖形,主要原因在于現代 Linux 桌面環境(如 Ubuntu 使用的 GNOME、Unity 等)通常運行在 X Window 系統或 Wayland 等顯示服務器上,而程序直接操作了/dev/fb0
(幀緩沖設備),這在桌面環境中存在一些兼容性問題。
????????顯示服務器沖突:在桌面環境中,Xorg 或 Wayland 已經獨占了幀緩沖設備,普通用戶程序無法直接訪問/dev/fb0
進行繪圖
確定當前使用的是 Xorg 還是 Wayland:
alientek@ubuntu:/home/linux_test/lcd$ echo $XDG_SESSION_TYPE
x11
三、為什么 Xorg 會導致程序無法工作?
Xorg 作為顯示服務器,會獨占?/dev/fb0
?設備來管理桌面顯示。當 Xorg 運行時:
-
它會優先打開并鎖定?
/dev/fb0
,程序雖然可能成功?open("/dev/fb0")
(取決于權限),但寫入的內容會被 Xorg 的桌面渲染覆蓋(因為 Xorg 會持續刷新屏幕)。 -
程序直接操作顯存的像素數據,但 Xorg 有自己的渲染管線(如通過顯卡驅動、窗口管理器等),兩者的繪制邏輯沖突,導致你的圖形無法顯示在桌面。