Framebuffer程序画的图像显示不出来是什么问题?

sean0_0 2018-07-02 02:00:57
#include "FrameBufferOpt.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <sys/ioctl.h>

static int Frame_fd;
static int *FrameBuffer = NULL;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static long int screensize;

int Read_Info_FrameBuffer(void)
{
Frame_fd = open("/dev/fb0", O_RDWR);
if(-1 == Frame_fd)
{
perror("open frame buffer fail");
return -1;
}
if(ioctl(Frame_fd,FBIOGET_FSCREENINFO,&finfo))
{
perror("reading fixed information fail");
close(Frame_fd);
return -1;
}
if (ioctl(Frame_fd,FBIOGET_VSCREENINFO,&vinfo))
{
perror("reading variable information fail");
close(Frame_fd);
return -1;
}
screensize = finfo.smem_len;
printf ("Fixed screen info:\n"
"\tid: %s\n"
"\tsmem_start: 0x%lx\n"
"\tsmem_len: %d\n"
"\ttype: %d\n"
"\ttype_aux: %d\n"
"\tvisual: %d\n"
"\txpanstep: %d\n"
"\typanstep: %d\n"
"\tywrapstep: %d\n"
"\tline_length: %d\n"
"\tmmio_start: 0x%lx\n"
"\tmmio_len: %d\n"
"\taccel: %d\n"
"\n",
finfo.id, finfo.smem_start, finfo.smem_len, finfo.type,
finfo.type_aux, finfo.visual, finfo.xpanstep, finfo.ypanstep,
finfo.ywrapstep, finfo.line_length, finfo.mmio_start,
finfo.mmio_len, finfo.accel);

printf ("Variable screen info:\n"
"\txres: %d\n"
"\tyres: %d\n"
"\txres_virtual: %d\n"
"\tyres_virtual: %d\n"
"\tyoffset: %d\n"
"\txoffset: %d\n"
"\tbits_per_pixel: %d\n"
"\tgrayscale: %d\n"
"\tred: offset: %2d, length: %2d, msb_right: %2d\n"
"\tgreen: offset: %2d, length: %2d, msb_right: %2d\n"
"\tblue: offset: %2d, length: %2d, msb_right: %2d\n"
"\ttransp: offset: %2d, length: %2d, msb_right: %2d\n"
"\tnonstd: %d\n"
"\tactivate: %d\n"
"\theight: %d\n"
"\twidth: %d\n"
"\taccel_flags: 0x%x\n"
"\tpixclock: %d\n"
"\tleft_margin: %d\n"
"\tright_margin: %d\n"
"\tupper_margin: %d\n"
"\tlower_margin: %d\n"
"\thsync_len: %d\n"
"\tvsync_len: %d\n"
"\tsync: %d\n"
"\tvmode: %d\n"
"\n",
vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual,
vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel,
vinfo.grayscale, vinfo.red.offset, vinfo.red.length,
vinfo.red.msb_right, vinfo.green.offset, vinfo.green.length,
vinfo.green.msb_right, vinfo.blue.offset, vinfo.blue.length,
vinfo.blue.msb_right, vinfo.transp.offset, vinfo.transp.length,
vinfo.transp.msb_right, vinfo.nonstd, vinfo.activate,
vinfo.height, vinfo.width, vinfo.accel_flags, vinfo.pixclock,
vinfo.left_margin, vinfo.right_margin, vinfo.upper_margin,
vinfo.lower_margin, vinfo.hsync_len, vinfo.vsync_len,
vinfo.sync, vinfo.vmode);

return 0;
}

//初始化framebuffer
int Map_FrameBuffer(void)
{
//dma搬运
FrameBuffer = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, Frame_fd, 0);
if(FrameBuffer == (void *)-1)
{
perror("memory map fail");
return -1;
}
return 0;
}

//画大小为width*height的同色矩阵,8alpha+8reds+8greens+8blues
void drawRect_rgb32(int x0, int y0, int width, int height, int color)
{
const int bytesPerPixel = 4;
const int stride = finfo.line_length / bytesPerPixel;

int *dest = (int *) (FrameBuffer)
+ (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);

int x, y;
for (y = 0; y < height; ++y)
{
for (x = 0; x < width; ++x)
{
dest[x] = color;
}
dest += stride;
}
}

//画大小为width*height的同色矩阵,5reds+5greens+5blues
void drawRect_rgb16(int x0, int y0, int width, int height, int color)
{
const int bytesPerPixel = 2;
const int stride = finfo.line_length / bytesPerPixel;
const int red = (color & 0xff0000) >> (16 + 3);
const int green = (color & 0xff00) >> (8 + 2);
const int blue = (color & 0xff) >> 3;
const short color16 = blue | (green << 5) | (red << (5 + 6));

short *dest = (short *) (FrameBuffer)
+ (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);

int x, y;
for (y = 0; y < height; ++y)
{
for (x = 0; x < width; ++x)
{
dest[x] = color16;
}
dest += stride;
}
}

//画大小为width*height的同色矩阵,5reds+5greens+5blues
void drawRect_rgb15(int x0, int y0, int width, int height, int color)
{
const int bytesPerPixel = 2;
const int stride = finfo.line_length / bytesPerPixel;
const int red = (color & 0xff0000) >> (16 + 3);
const int green = (color & 0xff00) >> (8 + 3);
const int blue = (color & 0xff) >> 3;
const short color15 = blue | (green << 5) | (red << (5 + 5)) | 0x8000;

short *dest = (short *) (FrameBuffer)
+ (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);

int x, y;
for (y = 0; y < height; ++y)
{
for (x = 0; x < width; ++x)
{
dest[x] = color15;
}
dest += stride;
}
}

void Sean_drawRect(int x0, int y0, int width, int height, int color)
{
switch (vinfo.bits_per_pixel)
{
case 32:
drawRect_rgb32 (x0, y0, width, height, color);
break;
case 16:
drawRect_rgb16 (x0, y0, width, height, color);
break;
case 15:
drawRect_rgb15 (x0, y0, width, height, color);
break;
default:
printf ("Warning: drawRect() not implemented for color depth %i\n",
vinfo.bits_per_pixel);
break;
}
}

void drawRect_Test(void)
{
Sean_drawRect(vinfo.xres / 8, vinfo.yres / 8,
vinfo.xres / 4, vinfo.yres / 4, 0xffff0000);
Sean_drawRect(vinfo.xres * 3 / 8, vinfo.yres * 3 / 8,
vinfo.xres / 4, vinfo.yres / 4, 0xff00ff00);
Sean_drawRect(vinfo.xres * 5 / 8, vinfo.yres * 5 / 8,
vinfo.xres / 4, vinfo.yres / 4, 0xff0000ff);
}

//退出framebuffer
int Exit_Framebuffer(void)
{
munmap(FrameBuffer, screensize);
close(Frame_fd);
return 0 ;
}


int main(void)
{
Read_Info_FrameBuffer();
Map_FrameBuffer();

/***********************************************************/
drawRect_Test();
/************************************************************/

sleep (5);

Exit_Framebuffer();

return 0;
}


...全文
420 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklinux 2021-04-21
  • 打赏
  • 举报
回复
在桌面上是由桌面系统接管刷显存了,这程序执行的话刚在显存上画出来就被桌面系统重画了,显示不出来的
csdevilcry 2021-04-21
  • 打赏
  • 举报
回复
引用 2 楼 jklinux 的回复:
你得在黑黑的终端上执行这个程序的。按ctrl+alt+f2可进入
试了以下可以了 但是为什么啊??????? 怎么在桌面显示不出来啊 还有如果要在桌面显示的话需要别的什么么 或者需要别的什么函数么????? 求详细点讲解一下么???
Sun ZW 2021-01-28
  • 打赏
  • 举报
回复
同问 能说的详细点吗
sean0_0 2018-07-02
  • 打赏
  • 举报
回复
谢谢!可以了。初学者
jklinux 2018-07-02
  • 打赏
  • 举报
回复
你得在黑黑的终端上执行这个程序的。按ctrl+alt+f2可进入

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧