0

我想在 OpenGL 窗口中显示当前屏幕/dev/fb0,我需要获得更快的像素映射,我已经编写了一些代码,但不幸的是它没有达到我的预期?这是因为使用 /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();

…………

4

1 回答 1

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;                            
               }        
       }

通过使用上面的代码片段,我解决了我的问题

于 2019-04-27T15:44:27.550 回答