1. 程式人生 > >linux直接寫framebuffer linux 直接 對 Frame Buffer 操作,寫畫面快取例子,c語言讀寫framebuffer

linux直接寫framebuffer linux 直接 對 Frame Buffer 操作,寫畫面快取例子,c語言讀寫framebuffer

提供的中斷呼叫來實現直接寫屏,故Linux抽象出FrameBuffer這個裝置來供使用者態
  程序實現直接寫屏。
  在繼續下面的之前,先說明幾個背景知識:
  1、FrameBuffer主要是根據VESA標準的實現的,所以只能實現最簡單的功能。
  2、由於涉及核心的問題,FrameBuffer是不允許在系統起來後修改顯示模式等一系
  列操作。(好象很多人都想要這樣幹,這是不被允許的,當然如果你自己與驅動
  的話,是可以實現的)
  3、對FrameBuffer的操作,會直接影響到本機的所有控制檯的輸出,包括XWIN的圖
  形介面。


  好,現在可以讓我們開始實現直接寫屏:
  1、開啟一個FrameBuffer裝置
  2、通過mmap呼叫把顯示卡的實體記憶體空間對映到使用者空間
  3、直接寫記憶體。

//////////////////////////////////////////////////////////////////////////////////////
//這個程式是可以用的,在我的開發板上面編譯通過,顯示彩色
/////////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h> 
#include <linux/fb.h>
#include <time.h>
#include <sys/mman.h>
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;
long int location = 0;
fbfd = open("/dev/fb0", O_RDWR);// Open the file for reading and writing
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
exit(0);
}
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(0);
}


// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) 
{            
printf("Error reading variable information.\n");
exit(0);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
        // 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(0);
}


printf("The framebuffer device was mapped to memory successfully.\n");
x = 100; y = 100;


// Where we are going to put the pixel   
// Figure out where in memory to put the pixel
for (y = 100; y < 300; y++)
for (x = 100; x < 300; x++) 
{
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

int b = 10;//assume 16bpp
int g = (x-100)/6;// A little green
int r = 31-(y-100)/16;// A lot of red
unsigned short int t = r<<11 | g << 5 | b;
*((unsigned short int*)(fbp + location)) = t;
}
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}

相關推薦

linux直接framebuffer linux 直接 Frame Buffer 操作畫面快取例子c語言framebuffer

提供的中斷呼叫來實現直接寫屏,故Linux抽象出FrameBuffer這個裝置來供使用者態   程序實現直接寫屏。   在繼續下面的之前,先說明幾個背景知識:   1、FrameBuffer主要是根據VESA標準的實現的,所以只能實現最簡單的功能。   2、由於涉及核心的問

C語言數據

cor 數據 and main () term int blog abi //1-5題 #include "stdio.h" typedef struct { char name[10];//姓名 int subject1,subject2,subject3

C語言 二進制文件

c 統計 字符串 二進制文件 查找了比較多的資源, 發現沒有辦法把text 文件轉成binary文件僅作為記錄,不過這個例子可以去除換行符。#include <stdio.h> #include <string.h> #define N 255 int main()

C語言文件

c/c++char buff[1000]={0}; //以w的方式打開,文件會被清空;文件不存在會創建 //以r的方式打開,文件必先存在,否則會打開錯誤 //+表示同時可讀可寫 FILE *fp = fopen("1.txt","r+"); if(fp) { char *

C語言-檔案I/O

C語言讀寫檔案的步驟一般是: 建立或開啟檔案 > 讀或寫檔案 > 關閉檔案 當然讀或寫的過程中還可以通過操作當前檔案偏移量來控制讀寫位置。 下面分別介紹這些函式。 標頭檔案: #include <fcntl.h> //此標頭檔案定義了以下oflag O_RDO

第28月第3天 c語言文件

emc ado urn != || spa new tolower see 1. int ConfigIniFile::OpenFile( const char* szFileName ) { FILE *fp; size_t nLe

C語言檔案

#include<stdio.h> int main(void) { FILE *fp; fp=fopen("D:\\textwrite.txt","w");// if(fp==NULL) { printf("檔案開啟時錯誤!\n"); retur

推箱子小遊戲 C語言了四十關遊戲有多種模式適合新手學習交流 有全部完整程式碼可直接編譯執行

}void middleMap(int Map[20][20],int *n,int *m,char *ch) {//中等關卡    static int cs=0;    int guan;    //fscanf(fp1, "%d", &guanshu2);    //fclose(fp1);//

Linux學習筆記(演算法與資料結構)之 二叉搜尋樹程式碼(C語言

1、程式碼在VS2010的C++編譯器中編譯通過,可能有極少部分語法不符合C99標準;bool型別無法使用,用int代替 2、由於VS配置問題,沒有分.c和.h檔案書寫;如果要分,最好將Create_Node和Destory_Node加上static關鍵字修飾,他們只會在所

C# xml

document path nodetype public oot art ade appdomain sed 1 class XmlClass 2 { 3 public string Pathname; //總列表 4 p

C#中INI配置文件(轉)

換行 value .config pri mas 文本 data- ini文件 def 在作應用系統開發時,管理配置是必不可少的。例如數據庫服務器的配置、安裝和更新配置等等。由於Xml的興起,現在的配置文件大都是以xml文檔來存儲。比如Visual Studio.Net自身

C#中INI文件

ots files inter services urn ons int ipa mes C#中讀寫INI文件   c#的類沒有直接提供對ini文件的操作支持,可以自己包裝win api的WritePrivateProfileString和GetPrivateProfile

C語音文件

stdin fputc 錯誤代碼 結構 ascii fwrite 文件的打開與關閉 包括 連接 1.fopen()   fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen實現三個功能:為使用而

Go 語言 Excel

Excel Go Golang XLSX Go語言 Excelize 是?Golang 編寫的一個用來操作 Office Excel 文檔類庫,基於 ECMA-376 Office OpenXML 標準。可以使用它來讀取、寫入 XLSX 文件。相比較其他的開源類庫,Excelize 支持

基於MGR+Atlas的分離嘗試以及MGR+Keepalived+Atlas自動故障轉移+分離設想

字符 SQ keepalive 分享圖片 測試環境 物理 沒有 efi 列表 復制環境準備 讀寫分離理論上講,跟復制模式沒有關系,atlas負責的是重定向讀寫,至於復制模式自己選擇,這裏是測試環境,之前測試MGR的單機多實例,MGR單主模式的復制模式,就順便借助MG

C#使用鎖解決多線程並發寫入文件時線程同步的問題

fin 問題 final [] 解決 同步 write main sta 讀寫鎖是以 ReaderWriterLockSlim 對象作為鎖管理資源的,不同的 ReaderWriterLockSlim 對象中鎖定同一個文件也會被視為不同的鎖進行管理,這種差異可能會再次導致文件

數據結構C語言、代順序表、鏈表C/C++編程作業、代C/C++Data Structures、代R語言編程作業

which atom stl boolean add debug buffer lean word COMP20003 Algorithms and Data StructuresSecond (Spring) Semester 2018[Assignment 1]Olym

C檔案函式

fopen() 函式原型 函式原型:FILE * fopen(const char * path, const char * mode); FILE *fp ; fp = fopen("D:\\a.txt","r"); \\是一種轉義字元,他表示一個\,就像\n表示回車一樣,即

C/C++檔案操作 —— windowsAPI

轉自:http://blog.sina.com.cn/s/blog_6e7fed390100z0j1.html 基於C的檔案操作 在ANSI C中,對檔案的操作分為兩種方式,即流式檔案操作和I/O檔案操作,下面就分別介紹之。 一、流式檔案操作 這種方式的檔案操作有一個重要的結構

C#中INI檔案的方法例子

[DllImport(“kernel32”)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImp