android下操作FrameBuffer

來源:互聯網
上載者:User

一、framebuffer使用基礎

1. Linux是工作在保護模式下,所以使用者態進程是無法象DOS那樣使用顯卡BIOS裡提供的中斷調用來實現直接寫屏, Linux抽象出FrameBuffer這個裝置來供使用者態進程實現直接寫屏。對於使用者而言,framebuffer和/dev下面的其他裝置沒有 什麼區別,使用者可以把framebuffer 看成一塊記憶體,既可以向這塊記憶體中寫入資料,也可以從這塊記憶體中讀取資料。在應 用程式中,一般通過將 FrameBuffer 裝置映射到進程地址空間的方式使用。

2.在這種機制下,儘管Framebuffer需要真正的顯卡驅動的支援,但所有顯示任務都有CPU完成,因此CPU負擔很重.

3.PCI 裝置可以將自己的控制寄存器映射到實體記憶體空間,而後,對這些控制寄存器的訪問,給變成了對實體記憶體的 訪問。因此,這些寄存器又被稱為"memio"。一旦被映射到實體記憶體,Linux 的普通進程就可以通過 mmap 將這些記憶體 I/O 映射到進程地址空間,這樣就可以直接存取這些寄存器了。

4.幀緩衝裝置屬於字元裝置,採用了“檔案層-驅動層”的介面方式。Linux為幀緩衝裝置定義的驅動層介面為struct fb_info結構。在檔案層次上,使用者調用struct file_operations的函數操作,其中間接調用struct fb_ops的函數來操作硬體.當向 核心註冊FB裝置的時候,也註冊了struct fb_ops的指標.當開啟fb裝置時,先調用fb_drivers[]的xxxfb_init()來初始化裝置;

5.第一個被註冊的framebuffer的minor等於0,第二個被註冊的framebuffer的minor等於1,以此類推。 如/dev/fb0,/dev/fb1。

可以用命令: #dd if=/dev/zero of=/dev/fb 清空螢幕.

如果顯示模式是 1024x768-8 位色,用命令:$ dd if=/dev/zero of=/dev/fb0 bs=1024 count=768

清空螢幕 用命令: #dd if=/dev/fb of=fbfile 可以將fb中的內容儲存下來;
可以重新寫回螢幕: #dd if=fbfile of=/dev/fb

二、framebuffer內部結構:
Framebuffer對應的源檔案在linux/drivers/video/目錄下。總的抽象裝置檔案為fbcon.c,在這個目錄下還有與各種顯卡驅動相關的源檔案。FrameBuffer裝置驅動基於如下檔案:

1) linux/include/linux/fb.h定義一些變數結構和宏。
2) linux/drivers/video/fbmem.c實現裝置入口和初始化。
3) xxxfb.c: 自己添加的裝置驅動檔案,如struct fb_info;實現進入點函數: xxxfb_init; xxxfb_setup;
1.首先是fb.h。framebuffer裝置很大程度上依靠了下面資料結構。
1).Struct fb_var_screeninfo描述圖形卡的特性的。通常是被使用者佈建的。

2).Struct fb_fix_screeninfo定義了圖形卡的硬體特性,是不能改變的。

3).Struct fb_cmap描述裝置無關的顏色映射資訊。可以通過FBIOGETCMAP和FBIOPUTCMAP 對應的ioctl操作設定或擷取
顏色映射資訊.
4).Struct fb_info定義了當前圖形卡framebuffer裝置狀態,一個圖形卡可能有兩個framebuffer,在這種情況下,就需要兩
個fb_info結構。這個結構是唯一在核心空間可見的。在這個結構中有一個fb_ops指標,指向驅動裝置工作所需的函數集。
5).struct fb_ops使用者應用可以使用ioctl()系統調用來操作裝置,這個結構就是用以支援ioctl()的這些操作的。(注: fb_ops
結構與file_operations 結構不同,fb_ops是底層操作的抽象,而file_operations是提供給上層系統調用的介面,可以直接調
用.)ioctl()系統調用在檔案fbmem.c中實現,通過觀察可以發現ioctl()命令與fb_ops’s 中函數的關係:
FBIOGET_VSCREENINFO fb_get_var
FBIOPUT_VSCREENINFO fb_set_var
FBIOGET_FSCREENINFO fb_get_fix
FBIOPUTCMAP fb_set_cmap
FBIOGETCMAP fb_get_cmap
FBIOPAN_DISPLAY fb_pan_display
如果我們定義了fb_XXX_XXX 方法,使用者程式就可以使用FBIOXXXX宏的ioctl()操作來操作硬體。

2.其次是fbmem.c。fbmem.c 處於Framebuffer裝置驅動技術的中心位置.它為上層應用程式提供系統調用也為下一層的特定硬體驅動提供介面;那些底層硬體驅動需要用到這兒的介面來向系統核心註冊它們自己. fbmem.c 為所有支援FrameBuffer的裝置驅動提供了通用的介面.

1) 全域變數
struct fb_info *registered_fb[FB_MAX];
int num_registered_fb;
這兩變數記錄了所有fb_info 結構的執行個體,fb_info 結構描述顯卡的目前狀態,所有裝置對應的fb_info 結構都儲存在
這個數組中,當一個FrameBuffer裝置驅動向系統註冊自己時,其對應的fb_info 結構就會添加到這個結構中,同時
num_registered_fb 為自動加1.

2) fbmem.c 實現了如下函數.

register_framebuffer(struct fb_info *fb_info);
unregister_framebuffer(struct fb_info *fb_info);

這兩個是提供給下層FrameBuffer裝置驅動的介面,裝置驅動通過這兩函數向系統註冊或登出自己。幾乎底層裝置驅動所
要做的所有事情就是填充fb_info結構然後向系統註冊或登出它。
3.xxxfb.c。自己添加的裝置驅動檔案,如以下等內容。
static struct fb_ops xxxfb_ops = {
owner: THIS_MODULE,
fb_open: xxxfb_open,
/* only if you need it to do something */
fb_release: xxxfb_release, /* only if you need it to do something */
fb_get_fix: fbgen_get_fix,
fb_get_var: fbgen_get_var,
fb_set_var: fbgen_set_var,
fb_get_cmap: fbgen_get_cmap,
fb_set_cmap: fbgen_set_cmap,
fb_pan_display: fbgen_pan_display,
fb_ioctl: xxxfb_ioctl, /* optional */
};

三、framebuffer驅動:

1.首先需要添加下面的代碼到fbmem.c
static struct {
const char *name;
int (*init)(void);
int (*setup)(char*);
}

fb_drivers[] __initdata = {

#ifdef CONFIG_FB_YOURCARD //紅色為添加部分
{ "driver_name", xxxfb_init, xxxfb_setup },
#endif
2.其次在xxxfb.c 中根據自己的需要重新分配顯存大小。例如:
#define VIDEOMEMSIZE (1*1024*1024) /* 1 MB */

再次根據自己的硬體裝置修改相應的var 資訊。主要修改
xxxfb_set_var(struct fb_var_screeninfo *var, int con, struct fb_info *info)

下面是函數fb_set_var()的執行步驟:
1)檢測是否必須設定模式
2)設定模式
3)設定顏色映射
4) 根據以前的設定重新設定LCD控制器的各寄存器。

四、配置添加驅動:
1.make menuconfig時,首先進入Character devices,選中裡面的Virtualterminal.如果希望控制台在液晶上輸出,則選中Support for console on virtual terminal。

2.退到上一層介面我們就可以看到Console device 的選項,進入後將游標落在Framebuffer Support 上,按斷行符號鍵進入,在裡面選擇自己所需要的framebuffer裝置即可。

3.在Advanced low level 中可以配置bpp packed pixel support,然後選中Selectcompiled-in fonts 即可。

等作業系統運行以後就會在/dev下面看到fb 這個裝置。它的major應該是29,第一個裝置的minor應該是0。

五、其它:
編譯核心時,選擇framebuffer模式,啟動時螢幕上有一企鵝圖片,不知這是如何造成的這個圖片可以去掉或改動嗎?
答:可以將drivers/video/fbcon.c: fbcon_setup()中if (logo) { } 代碼去掉。

六、操作framebuffer的主要步驟如下:

1、開啟一個可用的FrameBuffer裝置;
2、通過mmap調用把顯卡的實體記憶體空間映射到使用者空間;
3、更改記憶體空間裡的像素資料並顯示;
4、退出時關閉framebuffer裝置。
下面的這個例子簡單地用framebuffer畫了一個漸層的進度條,代碼 framebuf.c 如下:
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
inline static unsigned short int make16color(unsigned char r, unsigned char g, unsigned char b)
{
return ((((r >> 3) & 31) << 11) | (((g >> 2) & 63) << 5) | ((b >> 3) & 31) );
}
int main() {
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
int guage_height = 20, step = 10;
long int location = 0;
// Open the file for reading and writing
fbfd = open("/dev/graphics/fb0", O_RDWR);
if (!fbfd) {
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
printf("xoffset:%d, yoffset:%d, line_length: %d\n", vinfo.xoffset, vinfo.yoffset, finfo.line_length );
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);
if ((int)fbp == -1) {
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
printf("The framebuffer device was mapped to memory successfully.\n");
//set to black color first
memset(fbp, 0, screensize);
//draw rectangle
y = (vinfo.yres - guage_height) / 2 - 2; // Where we are going to put the pixel
for (x = step - 2; x < vinfo.xres - step + 2; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*((unsigned short int*)(fbp + location)) = 255;
}
y = (vinfo.yres + guage_height) / 2 + 2; // Where we are going to put the pixel
for (x = step - 2; x < vinfo.xres - step + 2; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*((unsigned short int*)(fbp + location)) = 255;
}
x = step - 2;
for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*((unsigned short int*)(fbp + location)) = 255;
}
x = vinfo.xres - step + 2;
for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*((unsigned short int*)(fbp + location)) = 255;
}
// Figure out where in memory to put the pixel
for ( x = step; x < vinfo.xres - step; x++ ) {
for ( y = (vinfo.yres - guage_height) / 2; y < (vinfo.yres + guage_height) / 2; y++ ) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
if ( vinfo.bits_per_pixel == 32 ) {
*(fbp + location) = 100; // Some blue
*(fbp + location + 1) = 15+(x-100)/2; // A little green
*(fbp + location + 2) = 200-(y-100)/5; // A lot of red
*(fbp + location + 3) = 0; // No transparency
} else { //assume 16bpp
unsigned char b = 255 * x / (vinfo.xres - step);
unsigned char g = 255; // (x - 100)/6 A little green
unsigned char r = 255; // A lot of red
unsigned short int t = make16color(r, g, b);
*((unsigned short int*)(fbp + location)) = t;
}
}
//printf("x = %d, temp = %d\n", x, temp);
//sleep to see it
usleep(200);
}
//clean framebuffer
munmap(fbp, screensize);
close(fbfd);
return 0;
}
複製代碼
  注意,在Android環境,framebuffer裝置不是象linux一樣的 /dev/fb0,而是 /dev/graphics/fb0 ,
fbfd = open("/dev/graphics/fb0", O_RDWR);
開啟framebuffer裝置,
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
將裝置map到一塊記憶體,然後就可以操作這塊記憶體空間來顯示你想畫的圖形了。
最後別忘了關閉裝置:
munmap(fbp, screensize);
close(fbfd);
如下:

複製搜尋

複製搜尋
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.