我想使用/dev/fb0
在OpenGL窗口中显示当前屏幕,我需要获得更快的像素映射,已经编写了一些代码,但是不幸的是,它没有达到我的期望?是由于使用/ dev / fb0还是代码本身而引起的问题?任何有关如何比c ++中的/ dev / fb0更快地获取屏幕地图的建议都将受到赞赏。
#used below Perl code just to check whether image formation is correct manually
#But it didn't work (png didn't form as a screen)
#!/usr/bin/perl -w
$w = shift || 240;
$h = shift || 320;
$pixels = $w * $h;
open OUT, "|pnmtopng" or die "Can't pipe pnmtopng: $!\n";
printf OUT "P6%d %d\n255\n", $w, $h;
while ((read STDIN, $raw, 2) and $pixels--) {
$short = unpack('S', $raw);
print OUT pack("C4444",
($short & 0xf800) >> 8,
($short & 0x7e0) >> 3,
($short & 0x1f) << 3);
}
close OUT;
也使用了glDrawpixels像素,但没有进行打印:
//内部渲染功能(使用此功能,我将在黑屏上显示白色|)
glClear(GL_COLOR_BUFFER_BIT);
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
int wrap = 1;
long int screensize = 0;
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd == -1) {
.....
}
printf("The framebuffer device was opened successfully.\n");
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
....
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
.....
}
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
GLubyte * fbp = nullptr;
fbp = (GLubyte * ) mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if(fbp != MAP_FAILED){
printf("mmap seems worked\n");
};
if ((int)*fbp == -1) {
......
exit(4);
}
printf("The framebuffer device was mapped to memory successfully.\n");
//display
glBitmap(1366,768,0,0,0,0,fbp);
glFlush();
.......
答案 0 :(得分:0)
unsigned char pixelBuffer[vinfo.xres][vinfo.yres][3];//[1366][768][3]
for(int y = 0 ; y < vinfo.yres ; y++){
for(int x = 0 ; x < vinfo.xres ; x++){
int pb_offset = 3 * x;
size_t fb_offset = x * (bytes_per_pixel)+ y * finfo.line_length;
uint32_t pixel = 0;
switch (vinfo.bits_per_pixel)
{
case 16:
pixel = *((uint16_t *)(fbp + fb_offset));
break;
case 24:
pixel += *(fbp + fb_offset);
pixel += *(fbp + fb_offset + 1) << 8;
pixel += *(fbp + fb_offset + 2) << 16;
break;
case 32:
pixel = *((uint32_t *)(fbp + fb_offset));
break;
default:
// nothing to do
break;
}
unsigned char r = (pixel >> vinfo.red.offset) & r_mask;
unsigned char g = (pixel >> vinfo.green.offset) & g_mask;
unsigned char b = (pixel >> vinfo.blue.offset) & b_mask;
pixelBuffer[x][y][0] = (r * 0xFF)/r_mask;
pixelBuffer[x][y][1] = (g * 0xFF)/g_mask;
pixelBuffer[x][y][2] = (b * 0xFF)/b_mask;
}
}
使用上面的代码片段,我已经解决了我的问题