【问题标题】:didnot get expected output on reading /dev/fb0在读取 /dev/fb0 时没有得到预期的输出
【发布时间】:2019-09-16 14:11:15
【问题描述】:

我想使用/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();

.......

【问题讨论】:

    标签: c++ linux opengl glut opengl-compat


    【解决方案1】:
      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;                            
                   }        
           }
    

    通过使用上面的 sn-p 我已经解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2020-09-01
      • 2017-09-15
      相关资源
      最近更新 更多